--- deliantra/server/server/cfperl.xs 2006/08/30 06:06:27 1.28 +++ deliantra/server/server/cfperl.xs 2007/04/03 00:21:38 1.181 @@ -6,7 +6,7 @@ * This code is placed under the GNU General Public Licence (GPL) * * Copyright (C) 2001-2005 by Chachkoff Yann - * Copyright (C) 2006 by Marc Lehmann + * Copyright (C) 2006,2007 by Marc Lehmann * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,141 +21,274 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#include -#include -#include - -#undef save_long // clashes with libproto.h + */ #define PLUGIN_NAME "perl" #define PLUGIN_VERSION "cfperl 0.5" -#ifndef __CEXTRACT__ -#include -#endif - -#undef MODULEAPI -#ifdef WIN32 -#else -#define MODULEAPI -#endif - #include #include #include #include +#include "loader.h" #include "cfperl.h" #include "shstr.h" +#include +#if _POSIX_MEMLOCK +# include +#endif + +#include +#include +#include + +#include "CoroAPI.h" #include "perlxsi.c" extern sint64 *levels; // the experience table -typedef object object_ornull; -typedef mapstruct mapstruct_ornull; +typedef object object_ornull; +typedef maptile maptile_ornull; -typedef double val64; -#define newSVval64 newSVnv -#define SvVAL64 SvNV +#if IVSIZE >= 8 + typedef IV val64; +# define newSVval64 newSViv +# define SvVAL64 SvIV +#else + typedef double val64; +# define newSVval64 newSVnv +# define SvVAL64 SvNV +#endif static f_plug_api gethook = cfapi_get_hooks; static f_plug_api object_set_property = cfapi_object_set_property; -static f_plug_api map_get_map = cfapi_map_get_map; static f_plug_api object_insert = cfapi_object_insert; -/* this is a stupid way to do things, and awkward to use for plug-in authors */ -typedef struct -{ - object* who; - object* activator; - object* third; - object* event; - mapstruct* map; - char message[1024]; - int fix; // seems to be python-only, and should not be part of the API - int event_code; - char extension[1024]; // name field, should invoke specific perl extension - char options[1024]; // slaying field of event_connectors - int returnvalue; -} CFPContext; - -static HV *obj_cache; static PerlInterpreter *perl; -static AV *cb_global, *cb_object, *cb_player, *cb_type, *cb_map; +double runtime; -#define PUSHcfapi(type,value) PUSHs (sv_2mortal (newSVcfapi (CFAPI_ ## type, (value)))) -#define PUSHcfapi_va(type,ctype) PUSHcfapi (type, va_arg (args, ctype)) -#define PUSH_OB PUSHcfapi_va(POBJECT, object *) -#define PUSH_PL PUSHcfapi_va(PPLAYER, player *) -#define PUSH_MAP PUSHcfapi_va(PMAP, mapstruct *) -#define PUSH_PV PUSHcfapi_va(STRING, const char *) -#define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int)))) +global gbl_ev; +static AV *cb_global, *cb_attachable, *cb_object, *cb_player, *cb_client, *cb_type, *cb_map; +static SV *sv_runtime; + +static HV + *stash_cf, + *stash_cf_object_wrap, + *stash_cf_object_player_wrap, + *stash_cf_player_wrap, + *stash_cf_map_wrap, + *stash_cf_client_wrap, + *stash_cf_arch_wrap, + *stash_cf_party_wrap, + *stash_cf_region_wrap, + *stash_cf_living_wrap; + +// helper cast function, returns super class * or 0 +template +static super * +is_a (attachable *at) +{ + //return dynamic_cast(at); // slower, safer + if (typeid (*at) == typeid (super)) + return static_cast(at); + else + return 0; +} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -static SV * -newSVptr (void *ptr, const char *klass, HV *hv = newHV ()) +unordered_vector attachable::mortals; + +attachable::~attachable () { - SV *sv; + flags |=0x3300;//D + assert (!self); + assert (!cb); +} - if (!ptr) - return &PL_sv_undef; +int +attachable::refcnt_cnt () const +{ + return refcnt + (self ? SvREFCNT (self) - 1 : 0); +} - sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); - return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); +void +attachable::sever_self () +{ + if (HV *self = this->self) + { + // keep a refcount because sv_unmagic might call attachable_free, + // which might clear self, causing sv_unmagic to crash on a now + // invalid object. + SvREFCNT_inc (self); + hv_clear (self); + sv_unmagic ((SV *)self, PERL_MAGIC_ext); + SvREFCNT_dec (self); + + // self *must* be null now because thats sv_unmagic's job. + assert (!this->self); + flags |= 0x80; // severed //D + } } -template -SV * -newSVattachable (attachable *obj, const char *klass) +void +attachable::optimise () { - if (!obj) - return &PL_sv_undef; + if (self + && SvREFCNT (self) == 1 + && !HvTOTALKEYS (self)) + flags |= 0x40,//D + sever_self (); +} - if (!obj->self) - obj->self = newSVptr (obj, klass); +// check wether the object really is dead +void +attachable::do_check () +{ + if (refcnt_cnt () > 0) + return; - return newSVsv (static_cast(obj->self)); + destroy (); } -static void -SVptr_cache_set (void *ptr, SV *sv) +void +attachable::do_destroy () { - hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0); + invoke (EVENT_ATTACHABLE_DESTROY, DT_END); + + if (cb) + { + SvREFCNT_dec (cb); + cb = 0; + } + + if (self) + sever_self (); + + flags |= 0x20; //D + mortals.push_back (this); } -static SV * -SVptr_cache_get (void *ptr) +void +attachable::destroy () { - SV **he = hv_fetch (obj_cache, (char *)&ptr, sizeof (ptr), 0); + if (destroyed ()) + return; - return he ? *he : 0; + flags |= F_DESTROYED; + do_destroy (); } +void +attachable::check_mortals () +{ + static int i = 0; + + for (;;) + { + if (i >= mortals.size ()) + { + i = 0; + + if (mortals.size () > 1000) + fprintf (stderr, "mortal queue size (%d) exceeds 1000.\n", (int)mortals.size ()); + + break; + } + + attachable *obj = mortals [i]; + + obj->refcnt_chk (); // unborrow from perl, if necessary + + //if (obj->refcnt > 0 || obj->self) + if (obj->refcnt || obj->self) + { +//printf ("%p rc %d\n", obj, obj->refcnt_cnt ());//D + ++i; // further delay freeing + + if (!(i & 0x3ff)) + break; + } + else + { + mortals.erase (i); + delete obj; + } + } +} + +attachable & +attachable::operator =(const attachable &src) +{ + //if (self || cb) + //INVOKE_OBJECT (CLONE, this, ARG_OBJECT (dst)); + + attach = src.attach; + return *this; +} + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + static SV * -newSVptr_cached (void *ptr, const char *klass) +newSVptr (void *ptr, HV *stash, HV *hv = newHV ()) { SV *sv; if (!ptr) return &PL_sv_undef; - sv = SVptr_cache_get (ptr); + sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, 0, (char *)ptr, 0); + return sv_bless (newRV_noinc ((SV *)hv), stash); +} - if (!sv) +static int +attachable_free (pTHX_ SV *sv, MAGIC *mg) +{ + attachable *at = (attachable *)mg->mg_ptr; + + //TODO: check if transaction behaviour is really required here + if (SV *self = (SV *)at->self) { - HV *hv = newHV (); - sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); - sv = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); + at->self = 0; + SvREFCNT_dec (self); + } + + // next line makes sense, but most objects still have refcnt 0 by default + //at->refcnt_chk (); + return 0; +} + +MGVTBL attachable::vtbl = {0, 0, 0, 0, attachable_free}; - SVptr_cache_set (ptr, sv); +static SV * +newSVattachable (attachable *obj, HV *stash) +{ + if (!obj) + return &PL_sv_undef; + + if (!obj->self) + { + obj->self = newHV (); + sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); + obj->flags |= (obj->flags & 0xc0) << 8; + obj->flags &= ~0xc0;//D + obj->flags |= 0x10;//D + + // now bless the object _once_ + return sv_bless (newRV_inc ((SV *)obj->self), stash); } + else + { + SV *sv = newRV_inc ((SV *)obj->self); - return newSVsv (sv); + if (Gv_AMG (stash)) // handle overload correctly, as the perl core does not + SvAMAGIC_on (sv); + + return sv; + } } static void @@ -191,6 +324,101 @@ return 0; } +inline SV *to_sv (const shstr & v) { return v ? newSVpvn ((const char *)v, v.length ()) : &PL_sv_undef; } +inline SV *to_sv (const char * v) { return newSVpv (v, 0); } +inline SV *to_sv (bool v) { return newSViv (v); } +inline SV *to_sv ( signed char v) { return newSViv (v); } +inline SV *to_sv (unsigned char v) { return newSViv (v); } +inline SV *to_sv ( signed short v) { return newSViv (v); } +inline SV *to_sv (unsigned short v) { return newSVuv (v); } +inline SV *to_sv ( signed int v) { return newSViv (v); } +inline SV *to_sv (unsigned int v) { return newSVuv (v); } +inline SV *to_sv ( signed long v) { return newSViv (v); } +inline SV *to_sv (unsigned long v) { return newSVuv (v); } +inline SV *to_sv ( signed long long v) { return newSVval64 (v); } +inline SV *to_sv (unsigned long long v) { return newSVval64 (v); } +inline SV *to_sv (float v) { return newSVnv (v); } +inline SV *to_sv (double v) { return newSVnv (v); } +inline SV *to_sv (client * v) { return newSVattachable (v, stash_cf_client_wrap); } +inline SV *to_sv (player * v) { return newSVattachable (v, stash_cf_player_wrap); } +inline SV *to_sv (object * v) { return newSVattachable (v, v && v->type == PLAYER ? stash_cf_object_player_wrap : stash_cf_object_wrap); } +inline SV *to_sv (maptile * v) { return newSVattachable (v, stash_cf_map_wrap); } +inline SV *to_sv (archetype * v) { return newSVattachable (v, stash_cf_arch_wrap); } +inline SV *to_sv (partylist * v) { return newSVptr (v, stash_cf_party_wrap); } +inline SV *to_sv (region * v) { return newSVptr (v, stash_cf_region_wrap); } +inline SV *to_sv (living * v) { return newSVptr (v, stash_cf_living_wrap); } + +inline SV *to_sv (object & v) { return to_sv (&v); } +inline SV *to_sv (living & v) { return to_sv (&v); } + +//inline SV *to_sv (faceinfo * v) { return to_sv (v->name); } +inline SV *to_sv (treasurelist * v) { return to_sv (v->name); } +inline SV *to_sv (std::string & v) { return newSVpvn (v.data (), v.size ()); } + +inline SV *to_sv (UUID v) +{ + char buf[128]; + snprintf (buf, 128, "<1.%" PRIx64 ">", v.seq); + return newSVpv (buf, 0); +} + +inline void sv_to (SV *sv, shstr &v) { v = SvOK (sv) ? SvPV_nolen (sv) : 0; } +inline void sv_to (SV *sv, char * &v) { free (v); v = SvOK (sv) ? strdup (SvPV_nolen (sv)) : 0; } +inline void sv_to (SV *sv, bool &v) { v = SvIV (sv); } +inline void sv_to (SV *sv, signed char &v) { v = SvIV (sv); } +inline void sv_to (SV *sv, unsigned char &v) { v = SvIV (sv); } +inline void sv_to (SV *sv, signed short &v) { v = SvIV (sv); } +inline void sv_to (SV *sv, unsigned short &v) { v = SvIV (sv); } +inline void sv_to (SV *sv, signed int &v) { v = SvIV (sv); } +inline void sv_to (SV *sv, unsigned int &v) { v = SvUV (sv); } +inline void sv_to (SV *sv, signed long &v) { v = SvIV (sv); } +inline void sv_to (SV *sv, unsigned long &v) { v = SvUV (sv); } +inline void sv_to (SV *sv, signed long long &v) { v = ( signed long long)SvVAL64 (sv); } +inline void sv_to (SV *sv, unsigned long long &v) { v = (unsigned long long)SvVAL64 (sv); } +inline void sv_to (SV *sv, float &v) { v = SvNV (sv); } +inline void sv_to (SV *sv, double &v) { v = SvNV (sv); } +inline void sv_to (SV *sv, client * &v) { v = (client *)(attachable *)SvPTR_ornull (sv, "cf::client"); } +inline void sv_to (SV *sv, player * &v) { v = (player *)(attachable *)SvPTR_ornull (sv, "cf::player"); } +inline void sv_to (SV *sv, object * &v) { v = (object *)(attachable *)SvPTR_ornull (sv, "cf::object"); } +inline void sv_to (SV *sv, archetype * &v) { v = (archetype *)(attachable *)SvPTR_ornull (sv, "cf::arch"); } +inline void sv_to (SV *sv, maptile * &v) { v = (maptile *)(attachable *)SvPTR_ornull (sv, "cf::map"); } +inline void sv_to (SV *sv, attachable * &v) { v = (attachable *)SvPTR_ornull (sv, "cf::attachable"); } +inline void sv_to (SV *sv, partylist * &v) { v = (partylist *)SvPTR_ornull (sv, "cf::party"); } +inline void sv_to (SV *sv, region * &v) { v = (region *)SvPTR_ornull (sv, "cf::region"); } +inline void sv_to (SV *sv, living * &v) { v = (living *)SvPTR_ornull (sv, "cf::living"); } + +//inline void sv_to (SV *sv, faceinfo * &v) { v = &faces [face_find (SvPV_nolen (sv), 0)]; } +inline void sv_to (SV *sv, treasurelist * &v) { v = find_treasurelist (SvPV_nolen (sv)); } + +template +inline void sv_to (SV *sv, refptr &v) { T *tmp; sv_to (sv, tmp); v = tmp; } + +template +inline void sv_to (SV *sv, char (&v)[N]) { assign (v, SvPV_nolen (sv)); } + +inline void sv_to (SV *sv, rangetype &v) { v = (rangetype) SvIV (sv); } +inline void sv_to (SV *sv, bowtype_t &v) { v = (bowtype_t) SvIV (sv); } +inline void sv_to (SV *sv, petmode_t &v) { v = (petmode_t) SvIV (sv); } +inline void sv_to (SV *sv, usekeytype &v) { v = (usekeytype) SvIV (sv); } +inline void sv_to (SV *sv, unapplymode &v) { v = (unapplymode) SvIV (sv); } + +inline void sv_to (SV *sv, std::string &v) +{ + STRLEN len; + char *data = SvPVbyte (sv, len); + v.assign (data, len); +} + +inline void sv_to (SV *sv, UUID &v) +{ + unsigned int version; + + if (2 != sscanf (SvPV_nolen (sv), "<%d.%" SCNx64 ">", &version, &v.seq) || 1 != version) + croak ("unparsable uuid: %s", SvPV_nolen (sv)); +} + +inline void sv_to (SV *sv, object::flags_t::reference v) { v = SvTRUE (sv); } + static SV * newSVdt_va (va_list &ap, data_type type) { @@ -226,31 +454,32 @@ break; case DT_OBJECT: - { - object *obj = va_arg (ap, object *); - sv = newSVattachable (obj, obj && obj->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap"); - } + sv = to_sv (va_arg (ap, object *)); break; case DT_MAP: // va_arg (object *) when void * is passed is an XSI extension - sv = newSVattachable (va_arg (ap, mapstruct *), "cf::map::wrap"); + sv = to_sv (va_arg (ap, maptile *)); + break; + + case DT_CLIENT: + sv = to_sv (va_arg (ap, client *)); break; case DT_PLAYER: - sv = newSVattachable (va_arg (ap, player *), "cf::player::wrap"); + sv = to_sv (va_arg (ap, player *)); break; case DT_ARCH: - sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap"); + sv = to_sv (va_arg (ap, archetype *)); break; case DT_PARTY: - sv = newSVptr (va_arg (ap, partylist *), "cf::party::wrap"); + sv = to_sv (va_arg (ap, partylist *)); break; case DT_REGION: - sv = newSVptr (va_arg (ap, region *), "cf::region::wrap"); + sv = to_sv (va_arg (ap, region *)); break; default: @@ -305,7 +534,7 @@ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SV * -registry_of (attachable_base *ext) +registry (attachable *ext) { if (!ext->cb) ext->cb = newAV (); @@ -313,226 +542,6 @@ return newRV_inc ((SV *)ext->cb); } -void attachable_base::clear () -{ - if (self) - { - if (cb) - if (SvROK (*av_fetch ((AV *)cb, EVENT_OBJECT_DESTROY, 1))) - INVOKE_OBJECT (DESTROY, static_cast(this)); - else if (SvROK (*av_fetch ((AV *)cb, EVENT_MAP_DESTROY, 1))) - INVOKE_MAP (DESTROY, static_cast(this)); - - // disconnect Perl from C, to avoid crashes - sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext); - - // clear the perl hash, might or might not be a good idea - hv_clear ((HV *)SvRV ((SV *)self)); - - SvREFCNT_dec (self); - self = 0; - } - - if (cb) - { - SvREFCNT_dec (cb); - cb = 0; - } - - if (attach) - { - free_string (attach); - attach = 0; - } -} - -void attachable_base::optimise () -{ - if (!self) - return; - - HV *hv = (HV *)SvRV ((SV *)self); - - if (SvREFCNT ((SV *)self) == 1 - && SvREFCNT ((SV *)hv) == 1 - && !HvKEYS (hv)) - { - SvREFCNT_dec ((SV *)self); - self = 0; - } -} - -void attachable_base::instantiate (data_type type, void *obj) -{ - dSP; - ENTER; - SAVETMPS; - PUSHMARK (SP); - EXTEND (SP, 2); - PUSHs (sv_2mortal (newSVdt (type, obj))); - PUSHs (sv_2mortal (newSVpv (attach, 0))); - - free_string (attach); - attach = 0; - - PUTBACK; - call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL); - FREETMPS; - LEAVE; - - switch (type) - { - case DT_OBJECT: INVOKE_OBJECT (INSTANTIATE, obj); break; - case DT_MAP: INVOKE_MAP (INSTANTIATE, obj); break; - } -} - -///////////////////////////////////////////////////////////////////////////// - -void reattach (data_type type, void *obj) -{ - //TODO only do this when the object has _attachment's - - dSP; - ENTER; - SAVETMPS; - PUSHMARK (SP); - XPUSHs (sv_2mortal (newSVdt (type, obj))); - PUTBACK; - call_pv ("cf::reattach", G_DISCARD | G_VOID | G_EVAL); - FREETMPS; - LEAVE; - - switch (type) - { - case DT_OBJECT: INVOKE_OBJECT (REATTACH, obj); break; - case DT_PLAYER: INVOKE_PLAYER (REATTACH, obj); break; - case DT_MAP: INVOKE_MAP (REATTACH, obj); break; - } -} - -template -void reattach (attachable *obj) -{ - obj->optimise (); - - if (obj->self) - reattach (subclass::get_dt (), (subclass *)obj); -} - -object_freezer::object_freezer (const char *filename) -{ - this->filename = (SV *)newSVpv (filename, 0); - - char filename2 [4096]; - snprintf (filename2, 4096, "%s~", filename); - - fp = fopen (filename2, "w"); - - if (!fp) - LOG (llevError, "cannot open file '%s' for writing: %s\n", filename2, strerror (errno)); - - av = (AV *)newAV (); -} - -object_freezer::~object_freezer () -{ - if (fp) - { - fclose (fp); - - dSP; - ENTER; - SAVETMPS; - PUSHMARK (SP); - XPUSHs (sv_2mortal ((SV *)filename)); - XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); - PUTBACK; - call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL); - FREETMPS; - LEAVE; - } - else - SvREFCNT_dec ((SV *)filename); -} - -void object_freezer::put (attachable_base *ext) -{ - ext->optimise (); - - if (ext->self) - { - int idx = AvFILLp ((AV *)av) + 1; - av_store ((AV *)av, idx, SvREFCNT_inc ((SV *)ext->self)); - fprintf (fp, "oid %d\n", idx); - } -} - -object_thawer::object_thawer (const char *filename) -{ - av = 0; - fp = 0; - - if (!filename) - return; - - fp = fopen (filename, "r"); - if (!fp) - { - LOG (llevError, "object_thawer: unable to open '%s': %s.\n", filename, strerror (errno)); - return; - } - - if (perl) - { - dSP; - ENTER; - SAVETMPS; - PUSHMARK (SP); - XPUSHs (sv_2mortal (newSVpv (filename, 0))); - PUTBACK; - - if (0 < call_pv ("cf::object_thawer_load", G_SCALAR | G_EVAL)) - { - SPAGAIN; - SV *sv = POPs; - if (SvROK (sv)) - av = SvREFCNT_inc (SvRV (sv)); - } - - FREETMPS; - LEAVE; - } -} - -void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid) -{ - if (!av || oid < 0) // this is actually an error of sorts - return; - - // we have to "re-instantiate"/reattach to an object, so nuke ext->attach - ext->clear (); - - SV **svp = av_fetch ((AV *)av, oid, 0); - - if (!svp || !SvROK (*svp)) - { - printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); - return; - } - - ext->self = *svp; *svp = &PL_sv_undef; - sv_magic (SvRV ((SV *)ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0); - - reattach (type, obj); -} - -object_thawer::~object_thawer () -{ - if (fp) fclose (fp); - if (av) SvREFCNT_dec ((AV *)av); -} - ///////////////////////////////////////////////////////////////////////////// extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) @@ -545,34 +554,7 @@ static int runPluginCommand (object *obj, char *params) { - dSP; - - ENTER; - SAVETMPS; - - PUSHMARK (SP); - - EXTEND (SP, 3); - PUSHs (sv_2mortal (newSVpv (rtn_cmd.name, 0))); - PUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, obj))); - - if (params) - PUSHs (sv_2mortal (newSVpv (params, 0))); - - PUTBACK; - int count = call_pv ("cf::inject_command", G_SCALAR | G_EVAL); - SPAGAIN; - - if (SvTRUE (ERRSV)) - LOG (llevError, "command '%s' callback evaluation error: %s", rtn_cmd.name, SvPV_nolen (ERRSV)); - - int returnvalue = count > 0 ? POPi : -1; - - PUTBACK; - FREETMPS; - LEAVE; - - return returnvalue; + return -1; } extern "C" void *cfperl_getPluginProperty (int *type, ...) @@ -585,35 +567,16 @@ //printf ("Property name: %s\n", propname); if (!strcmp (propname, "command?")) - { - if (!perl) - return NULL; - - const char *cmdname = va_arg (args, const char *); - HV *hv = get_hv ("cf::COMMAND", 1); - SV **svp = hv_fetch (hv, cmdname, strlen (cmdname) + 1, 0); - - va_end (args); - - if (svp) - { - // this is totaly broken, should stash it into %COMMAND - rtn_cmd.name = cmdname; - rtn_cmd.time = SvNV (*svp); - rtn_cmd.func = runPluginCommand; - - return &rtn_cmd; - } - } + return NULL; else if (!strcmp (propname, "Identification")) { va_end (args); - return (void*) PLUGIN_NAME; + return (void *)PLUGIN_NAME; } else if (!strcmp (propname, "FullName")) { va_end (args); - return (void*) PLUGIN_VERSION; + return (void *)PLUGIN_VERSION; } else va_end (args); @@ -639,34 +602,35 @@ void cfperl_init () { - char *argv[] = { - "", - "-e" - "BEGIN {" - " cf->bootstrap;" - " unshift @INC, cf::datadir ();" - "}" - "" - "use cf;" - }; - + PERL_SYS_INIT3 (&settings.argc, &settings.argv, 0); perl = perl_alloc (); perl_construct (perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; - if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) + const char *argv[] = { + "", + "-e" + "use Event; use Coro;" // required for bootstrap + "cf->bootstrap;" // required for datadir :*> + "unshift @INC, cf::datadir ();" + "require cf;" + }; + + if (perl_parse (perl, xs_init, 2, (char **)argv, (char **)NULL) + || perl_run (perl)) { printf ("unable to initialize perl-interpreter, aborting.\n"); - exit (EXIT_FAILURE); - //perl_destruct (perl); - //perl_free (perl); - //perl = 0; - //return; } - obj_cache = newHV (); + { + dSP; + + PUSHMARK (SP); + PUTBACK; + call_pv ("cf::init", G_DISCARD | G_VOID); + } } void cfperl_main () @@ -678,6 +642,24 @@ call_pv ("cf::main", G_DISCARD | G_VOID); } +void +attachable::instantiate () +{ + if (attach) + { + invoke (EVENT_ATTACHABLE_INSTANTIATE, ARG_STRING (attach), DT_END); + attach = 0; + } +} + +void +attachable::reattach () +{ + optimise (); + //TODO: check for _attachment's, very important for restarts + invoke (EVENT_ATTACHABLE_REATTACH, DT_END); +} + static event_klass klass_of[NUM_EVENT_TYPES] = { # define def(type,name) KLASS_ ## type, # include "eventinc.h" @@ -716,109 +698,105 @@ } } -bool cfperl_invoke (event_type event, ...) +void +attachable::gather_callbacks (AV *&callbacks, event_type event) const { - data_type dt; - va_list ap; - - va_start (ap, event); - - AV *callbacks = 0; + ::gather_callbacks (callbacks, cb_attachable, event); - object *op; - player *pl; - mapstruct *map; - - // callback call ordering is: - // 1. per-object callback (NYI) - // 2. per-class object - // 2a. per-type callback - // 4. global callbacks + if (cb) + ::gather_callbacks (callbacks, cb, event); +} - gather_callbacks (callbacks, cb_global, event); +void +global::gather_callbacks (AV *&callbacks, event_type event) const +{ + ::gather_callbacks (callbacks, cb_object, event); +} - switch (KLASS_OF (event)) +void +object::gather_callbacks (AV *&callbacks, event_type event) const +{ + if (subtype && type + subtype * NUM_SUBTYPES <= AvFILLp (cb_type)) { - case KLASS_GLOBAL: - break; - - case KLASS_OBJECT: - dt = (data_type) va_arg (ap, int); - assert (("first argument must be of type object", dt == DT_OBJECT)); - op = va_arg (ap, object *); + SV *registry = AvARRAY (cb_type)[type + subtype * NUM_SUBTYPES]; - if (op->cb) - gather_callbacks (callbacks, (AV *)op->cb, event); - - if (op->type) - { - if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type)) - { - SV *registry = AvARRAY (cb_type)[op->type + op->subtype * NUM_SUBTYPES]; - - if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV) - gather_callbacks (callbacks, (AV *)SvRV (registry), event); - } + if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV) + ::gather_callbacks (callbacks, (AV *)SvRV (registry), event); + } - if (op->type <= AvFILLp (cb_type)) - { - SV *registry = AvARRAY (cb_type)[op->type]; + if (type <= AvFILLp (cb_type)) + { + SV *registry = AvARRAY (cb_type)[type]; - if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV) - gather_callbacks (callbacks, (AV *)SvRV (registry), event); - } - } + if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV) + ::gather_callbacks (callbacks, (AV *)SvRV (registry), event); + } - gather_callbacks (callbacks, cb_object, event); + attachable::gather_callbacks (callbacks, event); + ::gather_callbacks (callbacks, cb_object, event); +} - break; +void +archetype::gather_callbacks (AV *&callbacks, event_type event) const +{ + attachable::gather_callbacks (callbacks, event); + //TODO//::gather_callbacks (callbacks, cb_archetype, event); +} - case KLASS_PLAYER: - dt = (data_type) va_arg (ap, int); - assert (("first argument must be of type player", dt == DT_PLAYER)); - pl = va_arg (ap, player *); +void +client::gather_callbacks (AV *&callbacks, event_type event) const +{ + attachable::gather_callbacks (callbacks, event); + ::gather_callbacks (callbacks, cb_client, event); +} - if (pl->cb) - gather_callbacks (callbacks, (AV *)pl->cb, event); +void +player::gather_callbacks (AV *&callbacks, event_type event) const +{ + attachable::gather_callbacks (callbacks, event); + ::gather_callbacks (callbacks, cb_player, event); +} - gather_callbacks (callbacks, cb_player, event); - break; +void +maptile::gather_callbacks (AV *&callbacks, event_type event) const +{ + attachable::gather_callbacks (callbacks, event); + ::gather_callbacks (callbacks, cb_map, event); +} - case KLASS_MAP: - dt = (data_type) va_arg (ap, int); - assert (("first argument must be of type object", dt == DT_MAP)); - map = va_arg (ap, mapstruct *); +bool +attachable::invoke (event_type event, ...) +{ + data_type dt; + va_list ap; - if (map->cb) - gather_callbacks (callbacks, (AV *)map->cb, event); + va_start (ap, event); - gather_callbacks (callbacks, cb_map, event); - break; + // callback call ordering should be: + // 1. per-object callback + // 2. per-class object + // 3. per-type callback + // 4. global callbacks - default: - assert (("unsupported event klass in cfperl_invoke", 0)); - } + AV *callbacks = 0; + gather_callbacks (callbacks, event); // short-circuit processing if no callbacks found/defined if (!callbacks) return 0; - dSP; - ENTER; - SAVETMPS; - - PUSHMARK (SP); - EXTEND (SP, 3); - - PUSHs (sv_2mortal (newSViv (event))); // only used for debugging nowadays - PUSHs (sv_2mortal (newRV_noinc ((SV *)callbacks))); - - switch (KLASS_OF (event)) - { - case KLASS_OBJECT: PUSHs (sv_2mortal (newSVdt (DT_OBJECT, op))); break; - case KLASS_PLAYER: PUSHs (sv_2mortal (newSVdt (DT_PLAYER, pl))); break; - case KLASS_MAP: PUSHs (sv_2mortal (newSVdt (DT_MAP, map))); break; - } + CALL_BEGIN (3); + CALL_ARG_SV (newSViv (event)); // only used for debugging nowadays + CALL_ARG_SV (newRV_noinc ((SV *)callbacks)); + + //TODO: unhack + if (object *op = is_a(this)) CALL_ARG_SV (newSVdt (DT_OBJECT, op)); + else if (player *pl = is_a(this)) CALL_ARG_SV (newSVdt (DT_PLAYER, pl)); + else if (client *ns = is_a(this)) CALL_ARG_SV (newSVdt (DT_CLIENT, ns)); + else if (maptile *m = is_a(this)) CALL_ARG_SV (newSVdt (DT_MAP, m)); + else if (global *gl = is_a(this)) /*nop*/; + else + abort (); //TODO for (;;) { @@ -826,8 +804,7 @@ if (dt == DT_END) break; - - if (dt == DT_AV) + else if (dt == DT_AV) { AV *av = va_arg (ap, AV *); @@ -840,14 +817,10 @@ va_end (ap); - PUTBACK; - int count = call_pv ("cf::do_invoke", G_SCALAR | G_EVAL); - SPAGAIN; - + CALL_CALL ("cf::do_invoke", G_SCALAR); count = count > 0 ? POPi : 0; - FREETMPS; - LEAVE; + CALL_END; return count; } @@ -872,11 +845,204 @@ return SvIV (cfperl_result (idx)); } +double +cfperl_result_DOUBLE (int idx) +{ + return SvNV (cfperl_result (idx)); +} + +///////////////////////////////////////////////////////////////////////////// + +void +cfperl_emergency_save () +{ + CALL_BEGIN (0); + CALL_CALL ("cf::emergency_save", G_VOID); + CALL_END; +} + +void +cfperl_cleanup (int make_core) +{ + CALL_BEGIN (1); + CALL_ARG (make_core); + CALL_CALL ("cf::post_cleanup", G_VOID); + CALL_END; +} + +maptile * +maptile::find_sync (const char *path, maptile *origin) +{ + CALL_BEGIN (2); + CALL_ARG (path); + CALL_ARG (origin); + CALL_CALL ("cf::map::find_sync", G_SCALAR); + + maptile *retval; + + if (count) + sv_to (POPs, retval); + else + retval = 0; + + CALL_END; + + return retval; +} + +maptile * +maptile::find_async (const char *path, maptile *origin) +{ + CALL_BEGIN (2); + CALL_ARG (path); + CALL_ARG (origin); + CALL_CALL ("cf::map::find_async", G_SCALAR); + + maptile *retval; + + if (count) + sv_to (POPs, retval); + else + retval = 0; + + CALL_END; + + return retval; +} + +void +maptile::do_load_sync () +{ + CALL_BEGIN (1); + CALL_ARG (this); + CALL_CALL ("cf::map::do_load_sync", G_SCALAR); + CALL_END; +} + +void +maptile::change_all_map_light (int change) +{ + CALL_BEGIN (1); + CALL_ARG (change); + CALL_CALL ("cf::map::change_all_map_light", G_VOID); + CALL_END; +} + +void +object::enter_exit (object *exit) +{ + if (type != PLAYER) + return; + + CALL_BEGIN (2); + CALL_ARG (this); + CALL_ARG (exit); + CALL_CALL ("cf::object::player::enter_exit", G_VOID); + CALL_END; +} + +///////////////////////////////////////////////////////////////////////////// + +struct EventAPI *watcher_base::GEventAPI; +struct CoroAPI *coroapi::GCoroAPI; + +int coroapi::cede_counter; + +static void iw_dispatch (pe_event *ev) +{ + iw *w = (iw *)ev->ext_data; + w->call (*w); +} + +void +iw::alloc () +{ + pe = GEventAPI->new_idle (0, 0); + + WaREENTRANT_off (pe); + pe->base.callback = (void *)iw_dispatch; + pe->base.ext_data = (void *)this; +} + +static void iow_dispatch (pe_event *ev) +{ + iow *w = (iow *)ev->ext_data; + w->call (*w, ((pe_ioevent *)ev)->got); +} + +void +iow::alloc () +{ + pe = GEventAPI->new_io (0, 0); + + WaREENTRANT_off (pe); + pe->base.callback = (void *)iow_dispatch; + pe->base.ext_data = (void *)this; + + pe->fd = -1; + pe->poll = 0; +} + +void +iow::fd (int fd) +{ + pe->fd = fd; +} + +int +iow::poll () +{ + return pe->poll; +} + +void +iow::poll (int events) +{ + if (pe->poll != events) + { + if (pe->poll) stop (); + pe->poll = events; + if (pe->poll) start (); + } +} + +void +_connect_to_perl () +{ + stash_cf = gv_stashpv ("cf" , 1); + + stash_cf_object_wrap = gv_stashpv ("cf::object::wrap", 1); + stash_cf_object_player_wrap = gv_stashpv ("cf::object::player::wrap", 1); + stash_cf_player_wrap = gv_stashpv ("cf::player::wrap", 1); + stash_cf_map_wrap = gv_stashpv ("cf::map::wrap" , 1); + stash_cf_client_wrap = gv_stashpv ("cf::client::wrap", 1); + stash_cf_arch_wrap = gv_stashpv ("cf::arch::wrap" , 1); + stash_cf_party_wrap = gv_stashpv ("cf::party::wrap" , 1); + stash_cf_region_wrap = gv_stashpv ("cf::region::wrap", 1); + stash_cf_living_wrap = gv_stashpv ("cf::living::wrap", 1); + + sv_runtime = get_sv ("cf::RUNTIME", 1); + sv_upgrade (sv_runtime, SVt_NV); + + cb_global = get_av ("cf::CB_GLOBAL", 1); + cb_attachable = get_av ("cf::CB_ATTACHABLE", 1); + cb_object = get_av ("cf::CB_OBJECT", 1); + cb_player = get_av ("cf::CB_PLAYER", 1); + cb_client = get_av ("cf::CB_CLIENT", 1); + cb_type = get_av ("cf::CB_TYPE" , 1); + cb_map = get_av ("cf::CB_MAP" , 1); +} + MODULE = cf PACKAGE = cf PREFIX = cf_ BOOT: { - HV *stash = gv_stashpv ("cf", 1); + _connect_to_perl (); + + I_EVENT_API (PACKAGE); watcher_base::GEventAPI = GEventAPI; + I_CORO_API (PACKAGE); coroapi::GCoroAPI = GCoroAPI; + + newCONSTSUB (stash_cf, "VERSION", newSVpv (VERSION, sizeof (VERSION) - 1)); static const struct { const char *name; @@ -888,6 +1054,13 @@ const_iv (llevDebug) const_iv (llevMonster) + const_iv (Map0Cmd) + const_iv (Map1Cmd) + const_iv (Map1aCmd) + + const_iv (MAP_CLIENT_X) + const_iv (MAP_CLIENT_Y) + const_iv (MAX_TIME) const_iv (PLAYER) const_iv (TRANSPORT) @@ -898,7 +1071,6 @@ const_iv (POISON) const_iv (BOOK) const_iv (CLOCK) - const_iv (LIGHTNING) const_iv (ARROW) const_iv (BOW) const_iv (WEAPON) @@ -928,7 +1100,6 @@ const_iv (TELEPORTER) const_iv (CREATOR) const_iv (SKILL) - const_iv (EXPERIENCE) const_iv (EARTHWALL) const_iv (GOLEM) const_iv (THROWN_OBJ) @@ -958,17 +1129,11 @@ const_iv (INORGANIC) const_iv (SKILL_TOOL) const_iv (LIGHTER) - const_iv (TRAP_PART) - const_iv (WALL) - const_iv (LIGHT_SOURCE) + const_iv (BUILDABLE_WALL) const_iv (MISC_OBJECT) - const_iv (MONSTER) - const_iv (SPAWN_GENERATOR) const_iv (LAMP) const_iv (DUPLICATOR) - const_iv (TOOL) const_iv (SPELLBOOK) - const_iv (BUILDFAC) const_iv (CLOAK) const_iv (SPINNER) const_iv (GATE) @@ -985,8 +1150,6 @@ const_iv (BRACERS) const_iv (POISONING) const_iv (SAVEBED) - const_iv (POISONCLOUD) - const_iv (FIREHOLES) const_iv (WAND) const_iv (SCROLL) const_iv (DIRECTOR) @@ -1011,7 +1174,6 @@ const_iv (BUILDER) const_iv (MATERIAL) const_iv (ITEM_TRANSFORMER) - const_iv (QUEST) const_iv (NUM_SUBTYPES) @@ -1049,16 +1211,6 @@ const_iv (AT_LIFE_STEALING) const_iv (AT_DISEASE) - const_iv (QUEST_IN_PROGRESS) - const_iv (QUEST_DONE_QUEST) - const_iv (QUEST_DONE_TASK) - const_iv (QUEST_START_QUEST) - const_iv (QUEST_END_QUEST) - const_iv (QUEST_START_TASK) - const_iv (QUEST_END_TASK) - const_iv (QUEST_OVERRIDE) - const_iv (QUEST_ON_ACTIVATE) - const_iv (WEAP_HIT) const_iv (WEAP_SLASH) const_iv (WEAP_PIERCE) @@ -1084,7 +1236,6 @@ const_iv (FLAG_GENERATOR) const_iv (FLAG_IS_THROWN) const_iv (FLAG_AUTO_APPLY) - const_iv (FLAG_TREASURE) const_iv (FLAG_PLAYER_SOLD) const_iv (FLAG_SEE_INVISIBLE) const_iv (FLAG_CAN_ROLL) @@ -1169,7 +1320,8 @@ const_iv (FLAG_CONTENT_ON_GEN) const_iv (FLAG_IS_A_TEMPLATE) const_iv (FLAG_IS_BUILDABLE) - const_iv (FLAG_AFK) + const_iv (FLAG_DESTROY_ON_DEATH) + const_iv (FLAG_NO_MAP_SAVE) const_iv (NDI_BLACK) const_iv (NDI_WHITE) @@ -1189,6 +1341,19 @@ const_iv (NDI_UNIQUE) const_iv (NDI_ALL) + const_iv (UPD_LOCATION) + const_iv (UPD_FLAGS) + const_iv (UPD_WEIGHT) + const_iv (UPD_FACE) + const_iv (UPD_NAME) + const_iv (UPD_ANIM) + const_iv (UPD_ANIMSPEED) + const_iv (UPD_NROF) + + const_iv (UPD_SP_MANA) + const_iv (UPD_SP_GRACE) + const_iv (UPD_SP_DAMAGE) + const_iv (F_APPLIED) const_iv (F_LOCATION) const_iv (F_UNPAID) @@ -1204,13 +1369,13 @@ const_iv (F_SELL) const_iv (P_BLOCKSVIEW) + const_iv (P_PLAYER) const_iv (P_NO_MAGIC) const_iv (P_IS_ALIVE) const_iv (P_NO_CLERIC) - const_iv (P_NEED_UPDATE) - const_iv (P_NO_ERROR) const_iv (P_OUT_OF_MAP) const_iv (P_NEW_MAP) + const_iv (P_UPTODATE) const_iv (UP_OBJ_INSERT) const_iv (UP_OBJ_REMOVE) @@ -1252,6 +1417,7 @@ const_iv (SK_EXP_TOTAL) const_iv (SK_EXP_NONE) const_iv (SK_SUBTRACT_SKILL_EXP) + const_iv (SK_EXP_SKILL_ONLY) const_iv (SK_LOCKPICKING) const_iv (SK_HIDING) @@ -1350,25 +1516,61 @@ const_iv (ATNR_LIFE_STEALING) const_iv (ATNR_DISEASE) - const_iv (MAP_FLUSH) - const_iv (MAP_PLAYER_UNIQUE) - const_iv (MAP_BLOCK) - const_iv (MAP_STYLE) - const_iv (MAP_OVERLAY) - const_iv (MAP_IN_MEMORY) const_iv (MAP_SWAPPED) const_iv (MAP_LOADING) const_iv (MAP_SAVING) + const_iv (KLASS_ATTACHABLE) const_iv (KLASS_GLOBAL) const_iv (KLASS_OBJECT) + const_iv (KLASS_CLIENT) const_iv (KLASS_PLAYER) const_iv (KLASS_MAP) + + const_iv (CS_QUERY_YESNO) + const_iv (CS_QUERY_SINGLECHAR) + const_iv (CS_QUERY_HIDEINPUT) + + const_iv (ST_DEAD) + const_iv (ST_SETUP) + const_iv (ST_PLAYING) + const_iv (ST_CUSTOM) + + const_iv (ST_CHANGE_CLASS) + + const_iv (IO_HEADER) + const_iv (IO_OBJECTS) + const_iv (IO_UNIQUES) + + // random map generator + const_iv (LAYOUT_NONE) + const_iv (LAYOUT_ONION) + const_iv (LAYOUT_MAZE) + const_iv (LAYOUT_SPIRAL) + const_iv (LAYOUT_ROGUELIKE) + const_iv (LAYOUT_SNAKE) + const_iv (LAYOUT_SQUARE_SPIRAL) + + const_iv (RMOPT_RANDOM) + const_iv (RMOPT_CENTERED) + const_iv (RMOPT_LINEAR) + const_iv (RMOPT_BOTTOM_C) + const_iv (RMOPT_BOTTOM_R) + const_iv (RMOPT_IRR_SPACE) + const_iv (RMOPT_WALL_OFF) + const_iv (RMOPT_WALLS_ONLY) + const_iv (RMOPT_NO_DOORS) + + const_iv (SYMMETRY_RANDOM) + const_iv (SYMMETRY_NONE) + const_iv (SYMMETRY_X) + const_iv (SYMMETRY_Y) + const_iv (SYMMETRY_XY) }; for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) - newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); + newCONSTSUB (stash_cf, (char *)civ->name, newSViv (civ->iv)); static const struct { const char *name; @@ -1389,172 +1591,43 @@ av_push (event, newSVpv ((char *)eiv->name + eiv->skip, 0)); av_push (event, newSViv (eiv->klass)); av_store (av, eiv->iv, newRV_noinc ((SV *)event)); - newCONSTSUB (stash, (char *)eiv->name, newSViv (eiv->iv)); + newCONSTSUB (stash_cf, (char *)eiv->name, newSViv (eiv->iv)); } - - static const struct { - int dtype; - const char *name; - IV idx; - } *cprop, prop_table[] = { -# define prop(type, name) { type, # name, (IV) CFAPI_ ## name }, - prop (CFAPI_INT, MAP_PROP_FLAGS) - prop (CFAPI_INT, MAP_PROP_DIFFICULTY) - prop (CFAPI_STRING, MAP_PROP_PATH) - prop (CFAPI_STRING, MAP_PROP_TMPNAME) - prop (CFAPI_STRING, MAP_PROP_NAME) - prop (CFAPI_INT, MAP_PROP_RESET_TIME) - prop (CFAPI_INT, MAP_PROP_RESET_TIMEOUT) - prop (CFAPI_INT, MAP_PROP_PLAYERS) - prop (CFAPI_INT, MAP_PROP_DARKNESS) - prop (CFAPI_INT, MAP_PROP_WIDTH) - prop (CFAPI_INT, MAP_PROP_HEIGHT) - prop (CFAPI_INT, MAP_PROP_ENTER_X) - prop (CFAPI_INT, MAP_PROP_ENTER_Y) - prop (CFAPI_INT, MAP_PROP_TEMPERATURE) - prop (CFAPI_INT, MAP_PROP_PRESSURE) - prop (CFAPI_INT, MAP_PROP_HUMIDITY) - prop (CFAPI_INT, MAP_PROP_WINDSPEED) - prop (CFAPI_INT, MAP_PROP_WINDDIR) - prop (CFAPI_INT, MAP_PROP_SKY) - prop (CFAPI_INT, MAP_PROP_WPARTX) - prop (CFAPI_INT, MAP_PROP_WPARTY) - prop (CFAPI_STRING, MAP_PROP_MESSAGE) - prop (CFAPI_PMAP, MAP_PROP_NEXT) - prop (CFAPI_PREGION, MAP_PROP_REGION) - prop (CFAPI_POBJECT, OBJECT_PROP_NEXT_ACTIVE_OB) - prop (CFAPI_POBJECT, OBJECT_PROP_PREV_ACTIVE_OB) - prop (CFAPI_POBJECT, OBJECT_PROP_INVENTORY) - prop (CFAPI_POBJECT, OBJECT_PROP_ENVIRONMENT) - prop (CFAPI_POBJECT, OBJECT_PROP_CONTAINER) - prop (CFAPI_PMAP, OBJECT_PROP_MAP) - prop (CFAPI_INT, OBJECT_PROP_COUNT) - prop (CFAPI_INT, OBJECT_PROP_REFCOUNT) - prop (CFAPI_STRING, OBJECT_PROP_NAME) - prop (CFAPI_STRING, OBJECT_PROP_NAME_PLURAL) - prop (CFAPI_STRING, OBJECT_PROP_TITLE) - prop (CFAPI_STRING, OBJECT_PROP_RACE) - prop (CFAPI_STRING, OBJECT_PROP_SLAYING) - prop (CFAPI_STRING, OBJECT_PROP_SKILL) - prop (CFAPI_STRING, OBJECT_PROP_MESSAGE) - prop (CFAPI_STRING, OBJECT_PROP_LORE) - prop (CFAPI_INT, OBJECT_PROP_X) - prop (CFAPI_INT, OBJECT_PROP_Y) - prop (CFAPI_DOUBLE, OBJECT_PROP_SPEED) - prop (CFAPI_DOUBLE, OBJECT_PROP_SPEED_LEFT) - prop (CFAPI_INT, OBJECT_PROP_NROF) - prop (CFAPI_INT, OBJECT_PROP_DIRECTION) - prop (CFAPI_INT, OBJECT_PROP_FACING) - prop (CFAPI_INT, OBJECT_PROP_TYPE) - prop (CFAPI_INT, OBJECT_PROP_SUBTYPE) - prop (CFAPI_INT, OBJECT_PROP_CLIENT_TYPE) - prop (CFAPI_INT, OBJECT_PROP_ATTACK_TYPE) - prop (CFAPI_INT, OBJECT_PROP_PATH_ATTUNED) - prop (CFAPI_INT, OBJECT_PROP_PATH_REPELLED) - prop (CFAPI_INT, OBJECT_PROP_PATH_DENIED) - prop (CFAPI_INT, OBJECT_PROP_MATERIAL) - prop (CFAPI_STRING, OBJECT_PROP_MATERIAL_NAME) - prop (CFAPI_INT, OBJECT_PROP_MAGIC) - prop (CFAPI_INT, OBJECT_PROP_VALUE) - prop (CFAPI_INT, OBJECT_PROP_LEVEL) - prop (CFAPI_INT, OBJECT_PROP_LAST_HEAL) - prop (CFAPI_INT, OBJECT_PROP_LAST_SP) - prop (CFAPI_INT, OBJECT_PROP_LAST_GRACE) - prop (CFAPI_INT, OBJECT_PROP_LAST_EAT) - prop (CFAPI_INT, OBJECT_PROP_INVISIBLE_TIME) - prop (CFAPI_INT, OBJECT_PROP_PICK_UP) - prop (CFAPI_INT, OBJECT_PROP_ITEM_POWER) - prop (CFAPI_INT, OBJECT_PROP_GEN_SP_ARMOUR) - prop (CFAPI_INT, OBJECT_PROP_WEIGHT) - prop (CFAPI_INT, OBJECT_PROP_WEIGHT_LIMIT) - prop (CFAPI_INT, OBJECT_PROP_CARRYING) - prop (CFAPI_INT, OBJECT_PROP_GLOW_RADIUS) - prop (CFAPI_LONG, OBJECT_PROP_PERM_EXP) - prop (CFAPI_POBJECT, OBJECT_PROP_CURRENT_WEAPON) - prop (CFAPI_POBJECT, OBJECT_PROP_ENEMY) - prop (CFAPI_POBJECT, OBJECT_PROP_ATTACKED_BY) - prop (CFAPI_INT, OBJECT_PROP_RUN_AWAY) - prop (CFAPI_POBJECT, OBJECT_PROP_CHOSEN_SKILL) - prop (CFAPI_INT, OBJECT_PROP_HIDDEN) - prop (CFAPI_INT, OBJECT_PROP_MOVE_STATUS) - prop (CFAPI_INT, OBJECT_PROP_MOVE_TYPE) - prop (CFAPI_POBJECT, OBJECT_PROP_SPELL_ITEM) - prop (CFAPI_DOUBLE, OBJECT_PROP_EXP_MULTIPLIER) - prop (CFAPI_PARCH, OBJECT_PROP_ARCHETYPE) - prop (CFAPI_PARCH, OBJECT_PROP_OTHER_ARCH) - prop (CFAPI_STRING, OBJECT_PROP_CUSTOM_NAME) - prop (CFAPI_INT, OBJECT_PROP_ANIM_SPEED) - prop (CFAPI_INT, OBJECT_PROP_FRIENDLY) - prop (CFAPI_STRING, OBJECT_PROP_SHORT_NAME) - prop (CFAPI_INT, OBJECT_PROP_MAGICAL) - prop (CFAPI_INT, OBJECT_PROP_LUCK) - prop (CFAPI_POBJECT, OBJECT_PROP_OWNER) - prop (CFAPI_POBJECT, OBJECT_PROP_PRESENT) - prop (CFAPI_INT, OBJECT_PROP_CHEATER) - prop (CFAPI_INT, OBJECT_PROP_MERGEABLE) - prop (CFAPI_INT, OBJECT_PROP_PICKABLE) - prop (CFAPI_INT, OBJECT_PROP_STR) - prop (CFAPI_INT, OBJECT_PROP_DEX) - prop (CFAPI_INT, OBJECT_PROP_CON) - prop (CFAPI_INT, OBJECT_PROP_WIS) - prop (CFAPI_INT, OBJECT_PROP_INT) - prop (CFAPI_INT, OBJECT_PROP_POW) - prop (CFAPI_INT, OBJECT_PROP_CHA) - prop (CFAPI_INT, OBJECT_PROP_WC) - prop (CFAPI_INT, OBJECT_PROP_AC) - prop (CFAPI_INT, OBJECT_PROP_HP) - prop (CFAPI_INT, OBJECT_PROP_SP) - prop (CFAPI_INT, OBJECT_PROP_GP) - prop (CFAPI_INT, OBJECT_PROP_FP) - prop (CFAPI_INT, OBJECT_PROP_MAXHP) - prop (CFAPI_INT, OBJECT_PROP_MAXSP) - prop (CFAPI_INT, OBJECT_PROP_MAXGP) - prop (CFAPI_INT, OBJECT_PROP_DAM) - prop (CFAPI_STRING, OBJECT_PROP_GOD) - prop (CFAPI_STRING, OBJECT_PROP_ARCH_NAME) - prop (CFAPI_INT, OBJECT_PROP_INVISIBLE) - prop (CFAPI_INT, OBJECT_PROP_FACE) - }; - - HV *prop_type = get_hv ("cf::PROP_TYPE", 1); - HV *prop_idx = get_hv ("cf::PROP_IDX", 1); - - for (cprop = prop_table + sizeof (prop_table) / sizeof (prop_table [0]); cprop-- > prop_table; ) - { - hv_store (prop_type, cprop->name, strlen (cprop->name), newSViv (cprop->dtype), 0); - hv_store (prop_idx, cprop->name, strlen (cprop->name), newSViv (cprop->idx ), 0); - } - - //I_EVENT_API (PACKAGE); } -void _reload_1 () - CODE: - cb_global = get_av ("cf::CB_GLOBAL", 1); - cb_object = get_av ("cf::CB_OBJECT", 1); - cb_player = get_av ("cf::CB_PLAYER", 1); - cb_type = get_av ("cf::CB_TYPE" , 1); - cb_map = get_av ("cf::CB_MAP" , 1); +void _connect_to_perl () -void _reload_2 () +void _global_reattach () CODE: { // reattach to all attachable objects in the game. - for (player *pl = first_player; pl; pl = pl->next) - reattach (pl); + for_all_clients (ns) + ns->reattach (); - for (mapstruct *map = first_map; map; map = map->next) - reattach (map); - - for (object *op = objects; op; op = op->next) - reattach (op); + for_all_objects (op) + op->reattach (); } NV floor (NV x) NV ceil (NV x) +NV rndm (...) + CODE: + switch (items) + { + case 0: RETVAL = rndm (); break; + case 1: RETVAL = rndm (SvUV (ST (0))); break; + case 2: RETVAL = rndm (SvIV (ST (0)), SvIV (ST (1))); break; + default: croak ("cf::rndm requires none, one or two parameters."); break; + } + OUTPUT: + RETVAL + void server_tick () + CODE: + runtime = SvNVx (sv_runtime); + server_tick (); void LOG (int level, char *msg) @@ -1580,7 +1653,7 @@ CODE: PL_sub_generation++; -char * +const char * mapdir () PROTOTYPE: ALIAS: @@ -1604,21 +1677,36 @@ } OUTPUT: RETVAL -int -cf_find_animation (char *text) - PROTOTYPE: $ +void abort () -int random_roll(int min, int max, object *op, int goodbad); +void fork_abort (char *cause = "cf::fork_abort") -const char *cost_string_from_value(uint64 cost, int approx = 0) +void cleanup (const char *cause, bool make_core = false) -int invoke (int event, ...) +void emergency_save () + +void _exit (int status = EXIT_SUCCESS) + +UV sv_2watcher (SV *w) CODE: - if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL"); - AV *av = (AV *)sv_2mortal ((SV *)newAV ()); - for (int i = 1; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); - RETVAL = INVOKE_((event_type)event, ARG_AV (av)); - OUTPUT: RETVAL + RETVAL = (UV)GEventAPI->sv_2watcher (w); + OUTPUT: + RETVAL + +#if _POSIX_MEMLOCK + +int mlockall (int flags = MCL_CURRENT | MCL_FUTURE) + +int munlockall () + +#endif + +int find_animation (char *text) + PROTOTYPE: $ + +int random_roll (int min, int max, object *op, int goodbad); + +const char *cost_string_from_value(uint64 cost, int approx = 0) int exp_to_level (val64 exp) @@ -1659,110 +1747,94 @@ XSRETURN_UNDEF; OUTPUT: RETVAL +bool +load_resource_file (const char *filename) + +MODULE = cf PACKAGE = cf::attachable + int -_valid (SV *obj) +valid (SV *obj) CODE: RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext); OUTPUT: RETVAL +void +debug_trace (attachable *obj, bool on = true) + CODE: + obj->flags &= ~attachable::F_DEBUG_TRACE; + if (on) + obj->flags |= attachable::F_DEBUG_TRACE; + +int mortals_size () + CODE: + RETVAL = attachable::mortals.size (); + OUTPUT: RETVAL + +#object *mortals (U32 index) +# CODE: +# RETVAL = index < attachable::mortals.size () ? attachable::mortals [index] : 0; +# OUTPUT: RETVAL + +INCLUDE: $PERL $srcdir/genacc attachable ../include/cfperl.h | + +MODULE = cf PACKAGE = cf::global + +int invoke (SV *klass, int event, ...) + CODE: + if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL"); + AV *av = (AV *)sv_2mortal ((SV *)newAV ()); + for (int i = 1; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); + RETVAL = gbl_ev.invoke ((event_type)event, ARG_AV (av), DT_END); + OUTPUT: RETVAL + MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ +INCLUDE: $PERL $srcdir/genacc object ../include/object.h | + int invoke (object *op, int event, ...) CODE: if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT"); AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); - RETVAL = INVOKE_((event_type)event, ARG_OBJECT (op), ARG_AV (av)); + RETVAL = op->invoke ((event_type)event, ARG_AV (av), DT_END); OUTPUT: RETVAL SV *registry (object *op) - CODE: - RETVAL = registry_of (op); - OUTPUT: - RETVAL -object *head (object *op) - PROTOTYPE: $ - ALIAS: - more = 1 - above = 2 - below = 3 +int objects_size () CODE: - switch (ix) - { - case 0: RETVAL = op->head ? op->head : op; break; // DOH! - case 1: RETVAL = op->more; break; - case 2: RETVAL = op->above; break; - case 3: RETVAL = op->below; break; - } - OUTPUT: - RETVAL - -SV * -get_property (object *obj, int type, int idx) - CODE: - RETVAL = newSVcfapi (type, cf_object_get_property (obj, idx)); + RETVAL = objects.size (); OUTPUT: RETVAL -SV * -set_property (object *obj, int type, int idx, SV *newval) +object *objects (U32 index) CODE: - switch (type) - { - case CFAPI_INT: - cf_object_set_int_property (obj, idx, SvIV (newval)); - break; - case CFAPI_LONG: - cf_object_set_long_property (obj, idx, (long) SvVAL64 (newval)); - break; - case CFAPI_DOUBLE: - { - int unused_type; - object_set_property (&unused_type, obj, idx, (double)SvNV (newval)); - } - break; - case CFAPI_STRING: - cf_object_set_string_property (obj, idx, SvOK (newval) ? SvPV_nolen (newval) : 0); - break; - case CFAPI_POBJECT: - { - int unused_type; - object_set_property (&unused_type, obj, idx, (object *)SvPTR_ornull (newval, "cf::object")); - } - break; - default: - croak ("unhandled type '%d' in set_property '%d'", type, idx); - } - -# missing properties + RETVAL = index < objects.size () ? objects [index] : 0; + OUTPUT: RETVAL -void -set_attacktype (object *obj, U32 attacktype) +int actives_size () CODE: - obj->attacktype = attacktype; + RETVAL = actives.size (); + OUTPUT: RETVAL -U32 -get_attacktype (object *obj) - ALIAS: - attacktype = 0 +object *actives (U32 index) CODE: - RETVAL = obj->attacktype; + RETVAL = index < actives.size () ? actives [index] : 0; OUTPUT: RETVAL -# missing in plug-in api, of course -void -set_food (object *obj, int food) - CODE: - obj->stats.food = food; +# missing properties -int -get_food (object *obj) - ALIAS: - food = 0 +object *head (object *op) + PROTOTYPE: $ CODE: - RETVAL = obj->stats.food; - OUTPUT: RETVAL + RETVAL = op->head_ (); + OUTPUT: RETVAL + +int is_head (object *op) + PROTOTYPE: $ + CODE: + RETVAL = op->head_ () == op; + OUTPUT: RETVAL void inv (object *obj) @@ -1771,93 +1843,114 @@ { object *o; for (o = obj->inv; o; o = o->below) - XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o))); + XPUSHs (sv_2mortal (to_sv (o))); } -int cf_object_get_resistance (object *op, int rtype) - ALIAS: resistance = 0 +void +set_animation (object *op, int idx) + CODE: + SET_ANIMATION (op, idx); + +int +num_animations (object *op) + CODE: + RETVAL = NUM_ANIMATIONS (op); + OUTPUT: RETVAL -int cf_object_get_flag (object *op, int flag) - ALIAS: flag = 0 +object *find_best_object_match (object *op, const char *match) -void cf_object_set_flag (object *op, int flag, int value) +object *find_marked_object (object *op) -int need_identify (const object *obj); +int need_identify (object *obj); int apply_shop_mat (object *shop_mat, object *op); +int move_player (object *op, int dir) + CODE: + RETVAL = move_player (op, dir); + OUTPUT: + RETVAL + int move (object *op, int dir, object *originator = op) CODE: RETVAL = move_ob (op, dir, originator); OUTPUT: RETVAL -void cf_object_apply (object *op, object *author, int flags = 0) - -void cf_object_apply_below (object *op) +void apply (object *applier, object *applied, int flags = 0) + CODE: + manual_apply (applied, applier, flags); -void cf_object_remove (object *op) +void apply_below (object *op) + CODE: + player_apply_below (op); -void cf_object_free (object *op) +int cast_heal (object *op, object *caster, object *spell, int dir = 0) object *cf_object_present_archname_inside (object *op, char *whatstr) int cf_object_transfer (object *op, int x, int y, int r = 0, object_ornull *orig = 0) -int cf_object_change_map (object *op, int x, int y, mapstruct *map) +int cf_object_change_map (object *op, int x, int y, maptile *map) -object *cf_object_clone (object *op, int clonetype = 0) +#//TODO +object *clone_ (object *op, int recursive = 0) + CODE: + if (recursive) + RETVAL = object_create_clone (op); + else + { + RETVAL = object::create (); + op->copy_to (RETVAL); + } + OUTPUT: RETVAL -int cf_object_pay_item (object *op, object *buyer) +int pay_item (object *op, object *buyer) + CODE: + RETVAL = pay_for_item (op, buyer); + OUTPUT: RETVAL -int cf_object_pay_amount (object *op, uint64 amount) +int pay_amount (object *op, uint64 amount) + CODE: + RETVAL = pay_for_amount (amount, op); + OUTPUT: RETVAL void pay_player (object *op, uint64 amount) val64 pay_player_arch (object *op, const char *arch, uint64 amount) -int cf_object_cast_spell (object *caster, object *ctoo, int dir, object *spell_ob, char *stringarg = 0) - -int cf_object_cast_ability (object *caster, object *ctoo, int dir, object *sp_, char *stringarg = 0) +int cast_spell (object *op, object *caster, int dir, object *spell_ob, char *stringarg = 0) -void cf_object_learn_spell (object *op, object *sp) +void learn_spell (object *op, object *sp, int special_prayer = 0) + CODE: + do_learn_spell (op, sp, special_prayer); -void cf_object_forget_spell (object *op, object *sp) +void forget_spell (object *op, object *sp) + CODE: + do_forget_spell (op, query_name (sp)); -object *cf_object_check_for_spell (object *op, char *spellname) +object *check_for_spell (object *op, char *spellname) + CODE: + RETVAL = check_spell_known (op, spellname); + OUTPUT: RETVAL -int cf_object_query_money (object *op) +int query_money (object *op) ALIAS: money = 0 -int cf_object_query_cost (object *op, object *who, int flags) +val64 query_cost (object *op, object *who, int flags) ALIAS: cost = 0 -void cf_object_activate_rune (object *op , object *victim) - -int cf_object_check_trigger (object *op, object *cause) - -int cf_object_out_of_map (object *op, int x, int y) - -void cf_object_drop (object *op, object *author) +void spring_trap (object *op, object *victim) -void cf_object_take (object *op, object *author) +int check_trigger (object *op, object *cause) -void cf_object_say (object *op, char *msg) +void drop (object *who, object *op) -void cf_object_speak (object *op, char *msg) +void pick_up (object *who, object *op) -object *cf_object_insert_object (object *op, object *container) +int cf_object_teleport (object *op, maptile *map, int x, int y) -const char *cf_object_get_msg (object *ob) - ALIAS: msg = 0 - -object *cf_object_insert_in_ob (object *ob, object *where) - -int cf_object_teleport (object *op, mapstruct *map, int x, int y) - -void cf_object_update (object *op, int flags) - -void cf_object_pickup (object *op, object *what) +void update_object (object *op, int action) object *cf_create_object_by_name (const char *name) @@ -1873,22 +1966,13 @@ void use_trigger (object *op); -void add_button_link (object *button, mapstruct *map, int connected); +void add_button_link (object *button, maptile *map, int connected); void remove_button_link (object *op); -void -cf_object_set_resistance (object *op, int rtype, int val) - CODE: - if (rtype >= 0 && rtype < NROFATTACKS) - op->resist[rtype] = val; - MODULE = cf PACKAGE = cf::object PREFIX = cf_ -void cf_fix_object (object *pl) - ALIAS: fix = 0 - object *cf_insert_ob_in_ob (object *ob, object *where) # no clean way to get an object from an archetype - stupid idiotic @@ -1901,7 +1985,7 @@ OUTPUT: RETVAL -object *insert_ob_in_map_at (object *ob, mapstruct *where, object_ornull *orig, int flag, int x, int y) +object *insert_ob_in_map_at (object *ob, maptile *where, object_ornull *orig, int flag, int x, int y) PROTOTYPE: $$$$$$ CODE: { @@ -1949,18 +2033,13 @@ RETVAL = on_same_map (ob, other); OUTPUT: RETVAL -char * -base_name (object *ob, int plural) +const char * +base_name (object *op, int plural = op->nrof > 1) CODE: - RETVAL = cf_query_base_name (ob, plural); - OUTPUT: RETVAL - -living * -stats (object *ob) - CODE: - RETVAL = &ob->stats; + RETVAL = query_base_name (op, plural); OUTPUT: RETVAL +object *decrease_ob_nr (object *op, unsigned long i) MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_ @@ -1969,7 +2048,11 @@ RETVAL = op->contr; OUTPUT: RETVAL -void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE) +void check_score (object *op) + +void message (object *op, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE) + CODE: + new_draw_info (flags, 0, op, txt); object *cf_player_send_inventory (object *op) @@ -1988,81 +2071,50 @@ void kill_player (object *op) +void esrv_update_item (object *op, int what, object *item) + C_ARGS: what, op, item + +void clear_los (object *op) + +int command_summon (object *op, char *params) + +int command_arrest (object *op, char *params) + + MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ +INCLUDE: $PERL $srcdir/genacc player ../include/player.h | + int invoke (player *pl, int event, ...) CODE: if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER"); AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); - RETVAL = INVOKE_((event_type)event, ARG_PLAYER (pl), ARG_AV (av)); + RETVAL = pl->invoke ((event_type)event, ARG_AV (av), DT_END); OUTPUT: RETVAL SV *registry (player *pl) - CODE: - RETVAL = registry_of (pl); - OUTPUT: - RETVAL -player *cf_player_find (char *name) - PROTOTYPE: $ +void +save_stats (player *pl) + CODE: + pl->ob->stats.hp = pl->ob->stats.maxhp; + pl->ob->stats.sp = pl->ob->stats.maxsp; + pl->ob->stats.grace = pl->ob->stats.maxgrace; + pl->orig_stats = pl->ob->stats; void cf_player_move (player *pl, int dir) void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0); -# nonstandard -object *ob (player *pl) - CODE: - RETVAL = pl->ob; - OUTPUT: RETVAL - -player *first () - CODE: - RETVAL = first_player; - OUTPUT: RETVAL - -player *next (player *pl) - CODE: - RETVAL = pl->next; - OUTPUT: RETVAL - bool cell_visible (player *pl, int dx, int dy) CODE: - RETVAL = FABS (dx) <= pl->socket.mapx / 2 && FABS (dy) <= pl->socket.mapy / 2 - && !pl->blocked_los [dx + pl->socket.mapx / 2][dy + pl->socket.mapy / 2]; + RETVAL = FABS (dx) <= pl->ns->mapx / 2 && FABS (dy) <= pl->ns->mapy / 2 + && !pl->blocked_los [dx + pl->ns->mapx / 2][dy + pl->ns->mapy / 2]; OUTPUT: RETVAL -char * -client (player *pl) - CODE: - RETVAL = pl->socket.client; - OUTPUT: - RETVAL - -char * -host (player *pl) - CODE: - RETVAL = pl->socket.host; - OUTPUT: - RETVAL - -char * -killer (player *pl, char *killer = 0) - CODE: - if (killer) - snprintf (pl->killer, sizeof (pl->killer), "%s", killer); - RETVAL = pl->killer; - OUTPUT: - RETVAL - -void -buggy_mapscroll (player *pl, int value = 1) - CODE: - pl->socket.buggy_mapscroll = value; - void send (player *pl, SV *packet) CODE: @@ -2070,7 +2122,8 @@ STRLEN len; char *buf = SvPVbyte (packet, len); - Write_String_To_Socket (&pl->socket, buf, len); + if (pl->ns) + pl->ns->send_packet (buf, len); } int @@ -2082,178 +2135,159 @@ OUTPUT: RETVAL -void get_savebed (player *pl) - ALIAS: - savebed = 0 +void savebed (player *pl, SV *map_path = 0, SV *x = 0, SV *y = 0) + PROTOTYPE: $;$$$ PPCODE: - EXTEND (SP, 3); - PUSHs (sv_2mortal (newSVpv (pl->savebed_map, 0))); - PUSHs (sv_2mortal (newSViv (pl->bed_x))); - PUSHs (sv_2mortal (newSViv (pl->bed_y))); - -void set_savebed (player *pl, char *map_path, int x, int y) - CODE: - strcpy (pl->savebed_map, map_path); - pl->bed_x = x; - pl->bed_y = y; + if (GIMME_V != G_VOID) + { + EXTEND (SP, 3); + PUSHs (sv_2mortal (newSVpv (pl->savebed_map, 0))); + PUSHs (sv_2mortal (newSViv (pl->bed_x))); + PUSHs (sv_2mortal (newSViv (pl->bed_y))); + } + if (map_path) sv_to (map_path, pl->savebed_map); + if (x) sv_to (x, pl->bed_x); + if (y) sv_to (y, pl->bed_y); void list () PPCODE: -{ - player *pl; - for (pl = first_player; pl; pl = pl->next) - XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl)); -} - -bool -peaceful (player *pl, bool new_setting = 0) - PROTOTYPE: $;$ - CODE: - RETVAL = pl->peaceful; - if (items > 1) - pl->peaceful = new_setting; - OUTPUT: - RETVAL - -living * -orig_stats (player *pl) - CODE: - RETVAL = &pl->orig_stats; - OUTPUT: RETVAL - -living * -last_stats (player *pl) - CODE: - RETVAL = &pl->last_stats; - OUTPUT: RETVAL + for_all_players (pl) + XPUSHs (sv_2mortal (to_sv (pl))); MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ -void -trigger (mapstruct *map, long connection, bool state = true) - CODE: - activate_connection (map, connection, state); - -void -get_connection (mapstruct *map, long connection) - PPCODE: -{ - oblinkpt *obp = get_connection_links (map, connection); - if (obp) - for (objectlink *ol = obp->link; ol; ol = ol->next) - XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, ol->ob))); -} - -int invoke (mapstruct *map, int event, ...) +int invoke (maptile *map, int event, ...) CODE: if (KLASS_OF (event) != KLASS_MAP) croak ("event class must be MAP"); AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); - RETVAL = INVOKE_((event_type)event, ARG_MAP (map), ARG_AV (av)); + RETVAL = map->invoke ((event_type)event, ARG_AV (av), DT_END); OUTPUT: RETVAL -SV *registry (mapstruct *map) - CODE: - RETVAL = registry_of (map); - OUTPUT: - RETVAL +SV *registry (maptile *map) -SV * -get_property (mapstruct *obj, int type, int idx) - CODE: - RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx)); - OUTPUT: RETVAL +INCLUDE: $PERL $srcdir/genacc maptile ../include/map.h | -SV * -set_property (mapstruct *obj, int type, int idx, SV *newval) +void +maptile::instantiate () + +maptile *new () + PROTOTYPE: CODE: - switch (type) + RETVAL = new maptile; + OUTPUT: + RETVAL + +void +maptile::players () + PPCODE: + if (GIMME_V == G_SCALAR) + XPUSHs (sv_2mortal (to_sv (THIS->players))); + else if (GIMME_V == G_ARRAY) { - case CFAPI_INT: - cf_map_set_int_property (obj, idx, SvIV (newval)); - break; - default: - croak ("unhandled type '%d' in set_property '%d'", type, idx); + EXTEND (SP, THIS->players); + for_all_players (pl) + if (pl->ob && pl->ob->map == THIS) + PUSHs (sv_2mortal (to_sv (pl->ob))); } -mapstruct *new (int width, int height) - PROTOTYPE: +void +maptile::add_underlay (SV *data, int offset, int stride, SV *palette) CODE: { - int unused_type; - RETVAL = (mapstruct*) map_get_map (&unused_type, 0, width, height); -} - OUTPUT: - RETVAL + if (!SvROK (palette) || SvTYPE (SvRV (palette)) != SVt_PVAV) + croak ("maptile::add_underlay: palette must be arrayref"); -void delete_map (mapstruct *map) + palette = SvRV (palette); -void clean_tmp_map (mapstruct *map) + STRLEN idxlen; + const uint8_t *idx = (const uint8_t *)SvPVbyte (data, idxlen); -void play_sound_map (mapstruct *map, int x, int y, int sound_num) + for (int x = 0; x < THIS->width; ++x) + for (int y = 0; y < THIS->height; ++y) + { + for (object *op = THIS->at (x, y).bot; op; op = op->above) + if (op->flag [FLAG_IS_FLOOR]) + goto skip_space; -mapstruct *tile_map (mapstruct *map, unsigned int dir) - CODE: - RETVAL = dir < 4 ? map->tile_map [dir] : 0; - OUTPUT: - RETVAL + { + int offs = offset + y * stride + x; + if (IN_RANGE_EXC (offs, 0, idxlen)) + { + if (SV **elem = av_fetch ((AV *)palette, idx [offs], 0)) + { + object *ob = get_archetype (SvPVutf8_nolen (*elem)); + ob->flag [FLAG_NO_MAP_SAVE] = true; + THIS->insert (ob, x, y, 0, INS_ABOVE_FLOOR_ONLY); + } + } + } + + skip_space: ; + } +} -char *tile_path (mapstruct *map, unsigned int dir) +void +maptile::set_regiondata (SV *data, int offset, int stride, SV *palette) CODE: - if (dir >= 4) - XSRETURN_UNDEF; - RETVAL = map->tile_path [dir]; - OUTPUT: - RETVAL +{ + if (!SvROK (palette) || SvTYPE (SvRV (palette)) != SVt_PVAV) + croak ("maptile::set_regiondata: palette must be arrayref"); -mapstruct *cf_map_get_map (char *name) - PROTOTYPE: $ - ALIAS: map = 0 + palette = SvRV (palette); -mapstruct *has_been_loaded (char *name) - PROTOTYPE: $ + STRLEN idxlen; + const uint8_t *idx = (const uint8_t *)SvPVbyte (data, idxlen); -mapstruct *cf_map_get_first () - PROTOTYPE: - ALIAS: first = 0 + region **regionmap = (region **)malloc ( + (av_len ((AV *)palette) + 1) * sizeof (region *)); + uint8_t *regions = salloc (THIS->size ()); -# whoever "designed" the plug-in api should have wasted -# his/her time with staying away from the project - would have -# saved others a lot of time, without doubt. -void set_path (mapstruct *where, char *path) - CODE: - strcpy (where->path, path); + for (int i = av_len ((AV *)palette) + 1; i--; ) + regionmap [i] = region::find ( + SvPVutf8_nolen (*av_fetch ((AV *)palette, i, 1))); -int in_memory (mapstruct *map) - CODE: - RETVAL = map->in_memory; - OUTPUT: - RETVAL + for (int y = 0; y < THIS->height; ++y) + memcpy (regions + y * THIS->width, idx + offset + y * stride, THIS->width); -bool unique (mapstruct *map) - CODE: - RETVAL = map->unique; - OUTPUT: - RETVAL + sfree (THIS->regions, THIS->size ()); + free (THIS->regionmap); -void set_unique (mapstruct *map, bool unique) - CODE: - map->unique = unique; + THIS->regions = regions; + THIS->regionmap = regionmap; +} + +void play_sound_map (maptile *map, int x, int y, int sound_num) + +int out_of_map (maptile *map, int x, int y) -object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags) +void +trigger (maptile *map, long connection, bool state = true) + CODE: + activate_connection (map, connection, state); + +void +get_connection (maptile *map, long connection) + PPCODE: + oblinkpt *obp = get_connection_links (map, connection); + if (obp) + for (objectlink *ol = obp->link; ol; ol = ol->next) + XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, (object *)ol->ob))); -object *cf_map_insert_object (mapstruct *where, object* op, int x, int y) +object *cf_map_insert_object_there (maptile *where, object *op, object *originator, int flags) -object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny) +object *cf_map_insert_object (maptile *where, object* op, int x, int y) + +object* cf_map_present_arch_by_name (maptile *map, const char* str, int nx, int ny) C_ARGS: str, map, nx, ny void -cf_map_normalise (mapstruct *map, int x, int y) +get_map_flags (maptile *map, int x, int y) PPCODE: { - mapstruct *nmap = 0; + maptile *nmap = 0; I16 nx = 0, ny = 0; int flags = get_map_flags (map, &nmap, x, y, &nx, &ny); @@ -2269,12 +2303,12 @@ } void -at (mapstruct *map, unsigned int x, unsigned int y) +at (maptile *map, unsigned int x, unsigned int y) PROTOTYPE: $$$ PPCODE: { object *o; - mapstruct *nmap = 0; + maptile *nmap = 0; I16 nx, ny; get_map_flags (map, &nmap, x, y, &nx, &ny); @@ -2285,7 +2319,7 @@ } SV * -bot_at (mapstruct *obj, unsigned int x, unsigned int y) +bot_at (maptile *obj, unsigned int x, unsigned int y) PROTOTYPE: $$$ ALIAS: top_at = 1 @@ -2296,7 +2330,7 @@ move_on_at = 6 move_off_at = 7 INIT: - if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_UNDEF; + if (x >= obj->width || y >= obj->height) XSRETURN_UNDEF; CODE: switch (ix) { @@ -2309,75 +2343,115 @@ case 6: RETVAL = newSVuv ( GET_MAP_MOVE_ON (obj, x, y)); break; case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; } - OUTPUT: - RETVAL - -# "deserialise" perl map data into the map # TODO# compatibility cruft, remove -void -_set_obs (mapstruct *map, SV *sv) - CODE: -{ - object *o; - AV *av; - int x, y; - AV *obs = (AV *)SvRV (sv); - - for (y = 0; y < MAP_HEIGHT (map); y++) - for (x = 0; x < MAP_WIDTH (map); x++) - { - sv = *av_fetch (obs, x + y * MAP_HEIGHT (map), 1); + OUTPUT: RETVAL - if (!SvROK (sv)) - continue; +void fix_walls (maptile *map, int x, int y) - av = (AV *)SvRV (sv); +void fix_walls_around (maptile *map, int x, int y) - for (o = GET_MAP_OB (map, x, y); o; o = o->above) - { - sv = av_shift (av); +# worst xs function of my life +bool +_create_random_map (\ + maptile *self,\ + char *wallstyle,\ + char *wall_name,\ + char *floorstyle,\ + char *monsterstyle,\ + char *treasurestyle,\ + char *layoutstyle,\ + char *doorstyle,\ + char *decorstyle,\ + char *origin_map,\ + char *final_map,\ + char *exitstyle,\ + char *this_map,\ + char *exit_on_final_map,\ + int xsize,\ + int ysize,\ + int expand2x,\ + int layoutoptions1,\ + int layoutoptions2,\ + int layoutoptions3,\ + int symmetry,\ + int difficulty,\ + int difficulty_given,\ + float difficulty_increase,\ + int dungeon_level,\ + int dungeon_depth,\ + int decoroptions,\ + int orientation,\ + int origin_y,\ + int origin_x,\ + U32 random_seed,\ + val64 total_map_hp,\ + int map_layout_style,\ + int treasureoptions,\ + int symmetry_used,\ + region *region,\ + char *custom\ +) + CODE: +{ + random_map_params rmp; + + assign (rmp.wallstyle , wallstyle); + assign (rmp.wall_name , wall_name); + assign (rmp.floorstyle , floorstyle); + assign (rmp.monsterstyle , monsterstyle); + assign (rmp.treasurestyle , treasurestyle); + assign (rmp.layoutstyle , layoutstyle); + assign (rmp.doorstyle , doorstyle); + assign (rmp.decorstyle , decorstyle); + assign (rmp.exitstyle , exitstyle); + assign (rmp.exit_on_final_map, exit_on_final_map); + + rmp.origin_map = origin_map; + rmp.final_map = final_map; + rmp.this_map = this_map; + rmp.xsize = xsize; + rmp.ysize = ysize; + rmp.expand2x = expand2x; + rmp.layoutoptions1 = layoutoptions1; + rmp.layoutoptions2 = layoutoptions2; + rmp.layoutoptions3 = layoutoptions3; + rmp.symmetry = symmetry; + rmp.difficulty = difficulty; + rmp.difficulty_given = difficulty_given; + rmp.difficulty_increase = difficulty_increase; + rmp.dungeon_level = dungeon_level; + rmp.dungeon_depth = dungeon_depth; + rmp.decoroptions = decoroptions; + rmp.orientation = orientation; + rmp.origin_y = origin_y; + rmp.origin_x = origin_x; + rmp.random_seed = random_seed; + rmp.total_map_hp = total_map_hp; + rmp.map_layout_style = map_layout_style; + rmp.treasureoptions = treasureoptions; + rmp.symmetry_used = symmetry_used; + rmp.region = region; + rmp.custom = custom; - if (SvROK (sv)) - { - sv_magic ((SV *)SvRV (sv), 0, PERL_MAGIC_ext, (char *)o, 0); - SVptr_cache_set (o, sv); - } - } - } + RETVAL = self->generate_random_map (&rmp); } - + OUTPUT: + RETVAL MODULE = cf PACKAGE = cf::arch +archetype *find (const char *name) + CODE: + RETVAL = archetype::find (name); + OUTPUT: + RETVAL + archetype *first() PROTOTYPE: CODE: RETVAL = first_archetype; OUTPUT: RETVAL -archetype *next (archetype *arch) - CODE: - RETVAL = arch->next; - OUTPUT: RETVAL - -archetype *head (archetype *arch) - CODE: - RETVAL = arch->head; - OUTPUT: RETVAL - -archetype *more (archetype *arch) - CODE: - RETVAL = arch->more; - OUTPUT: RETVAL - -const char *name (archetype *arch) - CODE: - RETVAL = arch->name; - OUTPUT: RETVAL - -object *clone (archetype *arch) - CODE: - RETVAL = &arch->clone; - OUTPUT: RETVAL +INCLUDE: $PERL $srcdir/genacc archetype ../include/object.h | MODULE = cf PACKAGE = cf::party @@ -2387,103 +2461,122 @@ RETVAL = get_firstparty (); OUTPUT: RETVAL -partylist *next (partylist *party) - CODE: - RETVAL = party->next; - OUTPUT: RETVAL +INCLUDE: $PERL $srcdir/genacc partylist ../include/player.h | + +MODULE = cf PACKAGE = cf::region + +void +list () + PPCODE: + for_all_regions (rgn) + XPUSHs (sv_2mortal (to_sv (rgn))); -const char *name (partylist *party) +region *find (char *name) + PROTOTYPE: $ CODE: - RETVAL = party->partyname; + RETVAL = region::find (name); OUTPUT: RETVAL -const char *password (partylist *party) +region *find_fuzzy (char *name) + PROTOTYPE: $ CODE: - RETVAL = party->passwd; + RETVAL = region::find_fuzzy (name); OUTPUT: RETVAL -MODULE = cf PACKAGE = cf::region +INCLUDE: $PERL $srcdir/genacc region ../include/map.h | -region *first () - PROTOTYPE: - CODE: - RETVAL = first_region; - OUTPUT: RETVAL +MODULE = cf PACKAGE = cf::living -region *next (region *reg) - CODE: - RETVAL = reg->next; - OUTPUT: RETVAL +INCLUDE: $PERL $srcdir/genacc living ../include/living.h | -const char *name (region *reg) - CODE: - RETVAL = reg->name; - OUTPUT: RETVAL +MODULE = cf PACKAGE = cf::settings -region *parent (region *reg) - CODE: - RETVAL = reg->parent; - OUTPUT: RETVAL +INCLUDE: $PERL $srcdir/genacc Settings ../include/global.h | -const char *longname (region *reg) - CODE: - RETVAL = reg->longname; - OUTPUT: RETVAL +MODULE = cf PACKAGE = cf::client -const char *msg (region *reg) - CODE: - RETVAL = reg->msg; - OUTPUT: RETVAL +INCLUDE: $PERL $srcdir/genacc client ../include/client.h | -MODULE = cf PACKAGE = cf::living +int invoke (client *ns, int event, ...) + CODE: + if (KLASS_OF (event) != KLASS_CLIENT) croak ("event class must be CLIENT"); + AV *av = (AV *)sv_2mortal ((SV *)newAV ()); + for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); + RETVAL = ns->invoke ((event_type)event, ARG_AV (av), DT_END); + OUTPUT: RETVAL -val64 -exp (living *liv, val64 new_val = 0) - PROTOTYPE: $;$ - ALIAS: - Str = 1 - Dex = 2 - Con = 3 - Wis = 4 - Cha = 5 - Int = 6 - Pow = 7 - wc = 8 - ac = 9 - hp = 10 - maxhp = 11 - sp = 12 - maxsp = 13 - grace = 14 - maxgrace = 15 - food = 16 - dam = 17 - luck = 18 - CODE: -# define LIVING_ACC(acc,idx) case idx: RETVAL = liv->acc; if (items > 1) liv->acc = (sint64)new_val; break - switch (ix) +SV *registry (client *ns) + +void +list () + PPCODE: + EXTEND (SP, clients.size ()); + for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) + PUSHs (sv_2mortal (to_sv (*i))); + +void +client::send_packet (SV *packet) + CODE: +{ + STRLEN len; + char *buf = SvPVbyte (packet, len); + + THIS->send_packet (buf, len); +} + +MODULE = cf PACKAGE = cf::face PREFIX = face_ + +INCLUDE: $PERL $srcdir/genacc faceset ../include/face.h | + +faceidx face_find (const char *name, faceidx defidx = 0) + +faceidx alloc (const char *name) + CODE: +{ + do { - LIVING_ACC (exp , 0); - LIVING_ACC (Str , 1); - LIVING_ACC (Dex , 2); - LIVING_ACC (Con , 3); - LIVING_ACC (Wis , 4); - LIVING_ACC (Cha , 5); - LIVING_ACC (Int , 6); - LIVING_ACC (Pow , 7); - LIVING_ACC (wc , 8); - LIVING_ACC (ac , 9); - LIVING_ACC (hp , 10); - LIVING_ACC (maxhp , 11); - LIVING_ACC (sp , 12); - LIVING_ACC (maxsp , 13); - LIVING_ACC (grace , 14); - LIVING_ACC (maxgrace, 15); - LIVING_ACC (food , 16); - LIVING_ACC (dam , 17); - LIVING_ACC (luck , 18); + RETVAL = faces.size (); + faces.resize (RETVAL + 1); } -# undef LIVING_ACC - OUTPUT: - RETVAL + while (!RETVAL); // crude way to leave index 0 + + faces [RETVAL].name = name; + facehash.insert (std::make_pair (faces [RETVAL].name, RETVAL)); + + if (!strcmp (name, BLANK_FACE_NAME)) blank_face = RETVAL; + if (!strcmp (name, EMPTY_FACE_NAME)) empty_face = RETVAL; +} + OUTPUT: RETVAL + +void set (faceidx idx, int visibility, int magicmap) + CODE: + faceinfo *f = face_info (idx); + assert (f); + f->visibility = visibility; + f->magicmap = magicmap; + +void set_smooth (faceidx idx, faceidx smooth) + CODE: + faceinfo *f = face_info (idx); + assert (f); + f->smooth = smooth; + +void set_data (faceidx idx, int faceset, SV *data, SV *chksum) + CODE: + facedata *d = face_data (idx, faceset); + assert (d); + sv_to (data, d->data); + STRLEN clen; + char *cdata = SvPVbyte (chksum, clen); + memcpy (d->chksum, cdata, min (CHKSUM_SIZE, clen)); + +void invalidate (faceidx idx) + CODE: + for_all_clients (ns) + ns->faces_sent [idx] = false; + +void invalidate_all () + CODE: + for_all_clients (ns) + ns->faces_sent.reset ();