--- deliantra/server/server/cfperl.xs 2006/11/24 09:44:14 1.69 +++ deliantra/server/server/cfperl.xs 2007/03/02 13:32:52 1.171 @@ -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,7 +21,7 @@ * 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 -*/ + */ #define PLUGIN_NAME "perl" #define PLUGIN_VERSION "cfperl 0.5" @@ -31,13 +31,20 @@ #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 @@ -45,9 +52,15 @@ 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; @@ -55,33 +68,227 @@ static PerlInterpreter *perl; -static AV *cb_global, *cb_object, *cb_player, *cb_type, *cb_map; +double runtime; + +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; +} + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +unordered_vector attachable::mortals; + +attachable::~attachable () +{ + flags |=0x3300;//D + assert (!self); + assert (!cb); +} + +int +attachable::refcnt_cnt () const +{ + return refcnt + (self ? SvREFCNT (self) - 1 : 0); +} + +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 + } +} + +void +attachable::optimise () +{ + if (self + && SvREFCNT (self) == 1 + && !HvTOTALKEYS (self)) + flags |= 0x40,//D + sever_self (); +} + +// check wether the object really is dead +void +attachable::do_check () +{ + if (refcnt_cnt () > 0) + return; + + destroy (); +} + +void +attachable::do_destroy () +{ + invoke (EVENT_ATTACHABLE_DESTROY, DT_END); + + if (cb) + { + SvREFCNT_dec (cb); + cb = 0; + } + + if (self) + sever_self (); + + flags |= 0x20; //D + mortals.push_back (this); +} + +void +attachable::destroy () +{ + if (destroyed ()) + return; + + 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 (void *ptr, const char *klass, HV *hv = newHV ()) +newSVptr (void *ptr, HV *stash, HV *hv = newHV ()) { SV *sv; if (!ptr) return &PL_sv_undef; - sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); - return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); + sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, 0, (char *)ptr, 0); + return sv_bless (newRV_noinc ((SV *)hv), stash); } -template -SV * -newSVattachable (attachable *obj, const char *klass) +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) + { + 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}; + +static SV * +newSVattachable (attachable *obj, HV *stash) { if (!obj) return &PL_sv_undef; if (!obj->self) - obj->self = newSVptr (obj, klass); + { + 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); + + if (Gv_AMG (stash)) // handle overload correctly, as the perl core does not + SvAMAGIC_on (sv); - return newSVsv (obj->self); + return sv; + } } static void @@ -132,30 +339,30 @@ 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 (player * v) { return newSVattachable (v, "cf::player::wrap"); } -inline SV *to_sv (object * v) { return newSVattachable (v, v && v->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap"); } -inline SV *to_sv (maptile * v) { return newSVattachable (v, "cf::map::wrap"); } -inline SV *to_sv (archetype * v) { return newSVptr (v, "cf::arch::wrap"); } -inline SV *to_sv (partylist * v) { return newSVptr (v, "cf::party::wrap"); } -inline SV *to_sv (region * v) { return newSVptr (v, "cf::region::wrap"); } -inline SV *to_sv (living * v) { return newSVptr (v, "cf::living::wrap"); } +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); } -//TODO: -inline SV *to_sv (New_Face * v) { return to_sv (v->name); } +inline SV *to_sv (facetile * v) { return to_sv (v->name); } inline SV *to_sv (treasurelist * v) { return to_sv (v->name); } inline SV *to_sv (UUID v) { char buf[128]; - snprintf (buf, 128, "<1,%llx>", (unsigned long long)v.seq); + 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; } //TODO: verify that all simple pointers are strdup-managed +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); } @@ -169,16 +376,18 @@ 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, player * &v) { v = (player *)SvPTR_ornull (sv, "cf::player"); } -inline void sv_to (SV *sv, object * &v) { v = (object *)SvPTR_ornull (sv, "cf::object"); } -inline void sv_to (SV *sv, archetype * &v) { v = (archetype *)SvPTR_ornull (sv, "cf::arch"); } -inline void sv_to (SV *sv, maptile * &v) { v = (maptile *)SvPTR_ornull (sv, "cf::map"); } +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, New_Face * &v) { v = &new_faces[FindFace (SvPV_nolen (sv), 0)]; } //TODO -inline void sv_to (SV *sv, treasurelist * &v) { v = find_treasurelist (SvPV_nolen (sv)); } // TODO +inline void sv_to (SV *sv, facetile * &v) { v = &new_faces[FindFace (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; } @@ -186,17 +395,22 @@ 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, UUID &v) { unsigned int version; - unsigned long long seq; - if (2 != sscanf (SvPV_nolen (sv), "<%d.%llx>", &version, &seq) || 1 != version) + if (2 != sscanf (SvPV_nolen (sv), "<%d.%" SCNx64 ">", &version, &v.seq) || 1 != version) croak ("unparsable uuid: %s", SvPV_nolen (sv)); - - v.seq = seq; } +inline void sv_to (SV *sv, object::flags_t::reference v) { v = SvTRUE (sv); } + static SV * newSVdt_va (va_list &ap, data_type type) { @@ -240,6 +454,10 @@ sv = to_sv (va_arg (ap, maptile *)); break; + case DT_CLIENT: + sv = to_sv (va_arg (ap, client *)); + break; + case DT_PLAYER: sv = to_sv (va_arg (ap, player *)); break; @@ -308,7 +526,7 @@ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SV * -registry_of (attachable_base *ext) +registry (attachable *ext) { if (!ext->cb) ext->cb = newAV (); @@ -316,448 +534,6 @@ return newRV_inc ((SV *)ext->cb); } -void attachable_base::clear () -{ - if (self) - { - if (cb) - if (SvROK (*av_fetch (cb, EVENT_OBJECT_DESTROY, 1))) - INVOKE_OBJECT (DESTROY, static_cast(this)); - else if (SvROK (*av_fetch (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; - } - - 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))); - - 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 ((data_type) cftype::dt, (subclass *)obj); -} - -#include "kw_hash.h" - -object_freezer::object_freezer () -: dynbuf (128 * 1024, 64 * 1024) -{ - av = newAV (); -} - -object_freezer::~object_freezer () -{ - SvREFCNT_dec (av); -} - -void object_freezer::put (attachable_base *ext) -{ - ext->optimise (); - - if (ext->self) - { - int idx = AvFILLp ((AV *)av) + 1; - av_store (av, idx, SvREFCNT_inc (ext->self)); - - add ((void *)"oid ", 4); - add ((sint32)idx); - add ('\n'); - } -} - -bool object_freezer::save (const char *filename) -{ - dSP; - ENTER; - SAVETMPS; - PUSHMARK (SP); - EXTEND (SP, 3); - PUSHs (sv_2mortal (newSVpv (filename, 0))); - PUSHs (sv_2mortal (newRV_noinc (newSVpvn ((char *)linearise (), size ())))); - PUSHs (sv_2mortal (newRV_inc ((SV *)av))); - PUTBACK; - call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL); - FREETMPS; - LEAVE; -} - -char *object_freezer::as_string () -{ - dSP; - ENTER; - SAVETMPS; - PUSHMARK (SP); - EXTEND (SP, 3); - PUSHs (sv_2mortal (newRV_noinc (newSVpvn ((char *)linearise (), size ())))); - PUSHs (sv_2mortal (newRV_inc ((SV *)av))); - PUTBACK; - - char *res = call_pv ("cf::object_freezer_as_string", G_SCALAR | G_EVAL) > 0 - ? strdup (SvPVbyte_nolen (POPs)) - : strdup ("[fatal error]"); - - FREETMPS; - LEAVE; - - return res; -} - -int fprintf (object_freezer &freezer, const char *format, ...) -{ - va_list ap; - - va_start (ap, format); - - int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap); - - if (len >= 0) - freezer.alloc (len); - - va_end (ap); -} - -int fputs (const char *s, object_freezer &freezer) -{ - freezer.add (s); -} - -static const char thawer_eof[] = "\n\n\n\0\0\0"; - -object_thawer::object_thawer (const char *filename) -{ - static const char eof[] = "\n\n\n\0\0\0"; - - av = 0; - text = 0; - line = 0; - - if (filename) - { - dSP; - ENTER; - SAVETMPS; - PUSHMARK (SP); - XPUSHs (sv_2mortal (newSVpv (filename, 0))); - PUTBACK; - - if (2 == call_pv ("cf::object_thawer_load", G_ARRAY | G_EVAL)) - { - SPAGAIN; - - // second value - perl objects - { - SV *sv = POPs; - if (SvROK (sv)) - av = (AV *)SvREFCNT_inc (SvRV (sv)); - } - - // first value - text part, pad with 3 zeroes - { - SV *sv = POPs; - STRLEN len; - char *sv_ = SvPVbyte (sv, len); - text = newSV (len + sizeof (eof)); - SvCUR_set (text, len); - memcpy (SvPVX (text), sv_, len); - memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure - - line = SvPVX (text); - } - } - - PUTBACK; - FREETMPS; - LEAVE; - } -} - -object_thawer::object_thawer (const char *data, AV *perlav) -{ - av = perlav; - text = newSVpv (data, 0); - sv_catpv (text, thawer_eof); - line = SvPVbyte_nolen (text); -} - -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 (ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0); - - reattach (type, obj); -} - -object_thawer::~object_thawer () -{ - if (text) SvREFCNT_dec (text); - if (av) SvREFCNT_dec (av); -} - -char *fgets (char *s, int n, object_thawer &thawer) -{ - char *p = thawer.line; - char *q = s; - - if (!p) - return 0; - - while (--n) - { - if (!*p) - break; - - *q++ = *p; - - if (*p++ == '\n') - break; - } - - *q = 0; - thawer.line = p; - - return s == q ? 0 : s; -} - -keyword object_thawer::get_kv () -{ - if (!line) - return KW_EOF; - - for (;;) - { - char *p = line; - - if (!*p) - return KW_EOF; - - // parse keyword - while (*p > ' ') - p++; - - int klen = p - line; - - if (*p++ != '\n') - { - // parse value - while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 - ++p; - - last_value = p; - - while (*p != '\n') - p++; - - *p++ = 0; - } - else - last_value = 0; - - line [klen] = 0; - keyword_idx *kw = kw_lex::match (line, klen); - - //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D - - if (kw) - { - line = p; - return kw->index; - } - else if (!*line || *line == '#') - { - // empty/comment line - line = p; - } - else - return KW_ERROR; - } -} - -void object_thawer::get (shstr &sh) const -{ - if (last_value) - sh = last_value; - else - { - sh = ""; - LOG (llevError, "keyword requires value: <%s>\n", line);//TODO: add filename - } -} - -void object_thawer::get_ml (keyword kend, shstr &sh) -{ - char kw[128]; - - int klen = keyword_len [kend]; - - kw [0] = '\n'; - memcpy (kw + 1, keyword_str [kend], klen); - kw [klen + 1] = '\n'; - kw [klen + 2] = 0; - - // first test for completely empty msg... "endXXX\n" - if (!strncmp (line, kw + 1, klen + 1)) - { - sh = 0; - - line += klen + 1; - - return; - } - else - { - // multi-line strings are delimited by "\nendXXX\n" or "endXXX\n" (NULL) - - char *end = strstr (line, kw); - - if (!end) - { - sh = 0; - return; - } - - *end = 0; - sh = line; - - line = end + keyword_len [kend] + 1; - - while (*line++ != '\n') - ; - } -} - -sint32 object_thawer::get_sint32 () const -{ - char *p = last_value; - - if (!p) - return 0; - - sint32 val = 0; - bool negate; - - if (*p == '-') - { - negate = true; - ++p; - } - else - negate = false; - - do - { - val *= 10; - val += *p++ - '0'; - } - while (*p); - - return negate ? -val : val; -} - -sint64 object_thawer::get_sint64 () const -{ - return last_value ? atoll (last_value) : 0; -} - -double object_thawer::get_double () const -{ - return last_value ? atof (last_value) : 0; -} - ///////////////////////////////////////////////////////////////////////////// extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) @@ -770,34 +546,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, ...) @@ -810,35 +559,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); @@ -873,7 +603,8 @@ char *argv[] = { "", "-e" - "cf->bootstrap;" + "use Event; use Coro;" // required for bootstrap + "cf->bootstrap;" // required for datadir :*> "unshift @INC, cf::datadir ();" "require cf;" }; @@ -883,6 +614,14 @@ printf ("unable to initialize perl-interpreter, aborting.\n"); exit (EXIT_FAILURE); } + + { + dSP; + + PUSHMARK (SP); + PUTBACK; + call_pv ("cf::init", G_DISCARD | G_VOID); + } } void cfperl_main () @@ -894,6 +633,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" @@ -932,109 +689,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; - - object *op; - player *pl; - maptile *map; + ::gather_callbacks (callbacks, cb_attachable, event); - // 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, 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, 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, maptile *); +bool +attachable::invoke (event_type event, ...) +{ + data_type dt; + va_list ap; - if (map->cb) - gather_callbacks (callbacks, 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 (;;) { @@ -1042,8 +795,7 @@ if (dt == DT_END) break; - - if (dt == DT_AV) + else if (dt == DT_AV) { AV *av = va_arg (ap, AV *); @@ -1056,14 +808,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; } @@ -1088,13 +836,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, "VERSION", newSVpv (VERSION, sizeof (VERSION) - 1)); + newCONSTSUB (stash_cf, "VERSION", newSVpv (VERSION, sizeof (VERSION) - 1)); static const struct { const char *name; @@ -1116,7 +1055,6 @@ const_iv (POISON) const_iv (BOOK) const_iv (CLOCK) - const_iv (LIGHTNING) const_iv (ARROW) const_iv (BOW) const_iv (WEAPON) @@ -1146,7 +1084,6 @@ const_iv (TELEPORTER) const_iv (CREATOR) const_iv (SKILL) - const_iv (EXPERIENCE) const_iv (EARTHWALL) const_iv (GOLEM) const_iv (THROWN_OBJ) @@ -1176,17 +1113,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) @@ -1203,8 +1134,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) @@ -1229,7 +1158,6 @@ const_iv (BUILDER) const_iv (MATERIAL) const_iv (ITEM_TRANSFORMER) - const_iv (QUEST) const_iv (NUM_SUBTYPES) @@ -1292,7 +1220,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) @@ -1377,7 +1304,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) @@ -1425,13 +1353,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) @@ -1572,25 +1500,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; @@ -1611,39 +1575,50 @@ 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)); } - - //I_EVENT_API (PACKAGE); } -void _init_vars () - 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 _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_all_players (pl) + pl->reattach (); - for (maptile *map = first_map; map; map = map->next) - reattach (map); + //TODO + //for (map_container::iterator i = maps.begin (); i != maps.end (); ++i) + // i->second->reattach (); - for (object *op = object::first; 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) @@ -1693,22 +1668,36 @@ } OUTPUT: RETVAL -void _exit (int status = 0) +void abort () -int cf_find_animation (char *text) - PROTOTYPE: $ +void fork_abort (char *cause = "cf::fork_abort") -int random_roll(int min, int max, object *op, int goodbad); +void cleanup (const char *cause, bool make_core = false) -const char *cost_string_from_value(uint64 cost, int approx = 0) +void emergency_save () -int invoke (int event, ...) +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) @@ -1749,13 +1738,47 @@ 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 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 genacc object ../include/object.h | @@ -1765,49 +1788,43 @@ 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 -void mortals () - PPCODE: - EXTEND (SP, object::mortals.size ()); - for (AUTODECL (i, object::mortals.begin ()); i != object::mortals.end (); ++i) - PUSHs (to_sv (*i)); +int objects_size () + CODE: + RETVAL = objects.size (); + OUTPUT: RETVAL -object *first () +object *objects (U32 index) CODE: - RETVAL = object::first; + RETVAL = index < objects.size () ? objects [index] : 0; OUTPUT: RETVAL -# missing properties +int actives_size () + CODE: + RETVAL = actives.size (); + OUTPUT: RETVAL -int flag (object *op, int flag, int value = 1) - PROTOTYPE: $$;$ +object *actives (U32 index) CODE: - RETVAL = QUERY_FLAG (op, flag); - if (items >= 3) - if (value) - SET_FLAG (op, flag); - else - CLEAR_FLAG (op, flag); - OUTPUT: RETVAL + RETVAL = index < actives.size () ? actives [index] : 0; + OUTPUT: RETVAL + +# missing properties object *head (object *op) PROTOTYPE: $ CODE: - RETVAL = op->head ? op->head : op; + RETVAL = op->head_ (); OUTPUT: RETVAL int is_head (object *op) PROTOTYPE: $ CODE: - RETVAL = !op->head; + RETVAL = op->head_ () == op; OUTPUT: RETVAL void @@ -1817,17 +1834,25 @@ { object *o; for (o = obj->inv; o; o = o->below) - XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o))); + XPUSHs (sv_2mortal (to_sv (o))); } +void +set_animation (object *op, int idx) + CODE: + SET_ANIMATION (op, idx); + +int +num_animations (object *op) + CODE: + RETVAL = NUM_ANIMATIONS (op); + OUTPUT: RETVAL + object *find_best_object_match (object *op, const char *match) object *find_marked_object (object *op) -int cf_object_get_resistance (object *op, int rtype) - ALIAS: resistance = 0 - -int need_identify (const object *obj); +int need_identify (object *obj); int apply_shop_mat (object *shop_mat, object *op); @@ -1837,13 +1862,15 @@ 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) @@ -1851,51 +1878,64 @@ 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 cast_spell (object *op, object *caster, int dir, object *spell_ob, char *stringarg = 0) -int cf_object_cast_ability (object *caster, object *ctoo, int dir, object *sp_, 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) +void spring_trap (object *op, object *victim) -int cf_object_out_of_map (object *op, int x, int y) +int check_trigger (object *op, object *cause) -void cf_object_drop (object *op, object *author) +void drop (object *who, object *op) -void cf_object_take (object *op, object *author) - -object *cf_object_insert_object (object *op, object *container) - -object *cf_object_insert_in_ob (object *ob, object *where) +void pick_up (object *who, object *op) int cf_object_teleport (object *op, maptile *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) @@ -1915,18 +1955,9 @@ 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 @@ -1993,6 +2024,8 @@ 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_ player *player (object *op) @@ -2000,7 +2033,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) @@ -2024,89 +2061,56 @@ void clear_los (object *op) -int command_reset (object *op, char *params) - -int command_teleport (object *op, char *params) - int command_summon (object *op, char *params) int command_arrest (object *op, char *params) -int command_kick (object *op, char *params) - int command_banish (object *op, char *params) MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ -INCLUDE: $PERL genacc player ../include/newserver.h ../include/player.h | - -char * -client (player *pl) - CODE: - RETVAL = pl->socket.client; - OUTPUT: - RETVAL - -char * -host (player *pl) - CODE: - RETVAL = pl->socket.host; - OUTPUT: - RETVAL +INCLUDE: $PERL 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); -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 void -buggy_mapscroll (player *pl, int value = 1) - CODE: - pl->socket.buggy_mapscroll = value; - -void send (player *pl, SV *packet) CODE: { 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 @@ -2135,102 +2139,116 @@ 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_ -maptile *first () - PROTOTYPE: - CODE: - RETVAL = first_map; - OUTPUT: RETVAL - 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 (maptile *map) - CODE: - RETVAL = registry_of (map); - OUTPUT: - RETVAL INCLUDE: $PERL genacc maptile ../include/map.h | -maptile *new (int width, int height) +void +maptile::instantiate () + +maptile *new () PROTOTYPE: CODE: -{ - RETVAL = get_empty_map (width, height); -} + RETVAL = new maptile; OUTPUT: RETVAL -void delete_map (maptile *map) +void +maptile::players () + PPCODE: + if (GIMME_V == G_SCALAR) + XPUSHs (sv_2mortal (to_sv (THIS->players))); + else if (GIMME_V == G_ARRAY) + { + EXTEND (SP, THIS->players); + for_all_players (pl) + if (pl->ob && pl->ob->map == THIS) + PUSHs (sv_2mortal (to_sv (pl->ob))); + } -void clean_tmp_map (maptile *map) +void +maptile::add_underlay (SV *data, int offset, int stride, SV *palette) + CODE: +{ + if (!SvROK (palette) || SvTYPE (SvRV (palette)) != SVt_PVAV) + croak ("maptile::add_underlay: palette must be arrayref"); -void play_sound_map (maptile *map, int x, int y, int sound_num) + palette = SvRV (palette); -maptile *tile_map (maptile *map, unsigned int dir) - CODE: - RETVAL = dir < 4 ? map->tile_map [dir] : 0; - OUTPUT: - RETVAL + STRLEN idxlen; + const uint8_t *idx = (const uint8_t *)SvPVbyte (data, idxlen); -char *tile_path (maptile *map, unsigned int dir) - CODE: - if (dir >= 4) - XSRETURN_UNDEF; - RETVAL = map->tile_path [dir]; - OUTPUT: - RETVAL + 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; -maptile *ready_map_name (char *name, int flags = 0) - PROTOTYPE: $;$ - ALIAS: - find = 0 - get_map = 1 + { + 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); + } + } + } -maptile *has_been_loaded (char *name) - PROTOTYPE: $ + skip_space: ; + } +} -int in_memory (maptile *map) +void +maptile::set_regiondata (SV *data, int offset, int stride, SV *palette) CODE: - RETVAL = map->in_memory; - OUTPUT: - RETVAL +{ + if (!SvROK (palette) || SvTYPE (SvRV (palette)) != SVt_PVAV) + croak ("maptile::set_regiondata: palette must be arrayref"); + + palette = SvRV (palette); + + STRLEN idxlen; + const uint8_t *idx = (const uint8_t *)SvPVbyte (data, idxlen); + + region **regionmap = (region **)malloc ( + (av_len ((AV *)palette) + 1) * sizeof (region *)); + uint8_t *regions = salloc (THIS->size ()); + + for (int i = av_len ((AV *)palette) + 1; i--; ) + regionmap [i] = region::find ( + SvPVutf8_nolen (*av_fetch ((AV *)palette, i, 1))); + + for (int y = 0; y < THIS->height; ++y) + memcpy (regions + y * THIS->width, idx + offset + y * stride, THIS->width); + + sfree (THIS->regions, THIS->size ()); + free (THIS->regionmap); + + 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) void trigger (maptile *map, long connection, bool state = true) @@ -2253,7 +2271,7 @@ C_ARGS: str, map, nx, ny void -cf_map_normalise (maptile *map, int x, int y) +get_map_flags (maptile *map, int x, int y) PPCODE: { maptile *nmap = 0; @@ -2299,7 +2317,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) { @@ -2312,9 +2330,99 @@ 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 + OUTPUT: RETVAL +void fix_walls (maptile *map, int x, int y) + +void fix_walls_around (maptile *map, int x, int y) + +# 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; + + RETVAL = self->generate_random_map (&rmp); +} + OUTPUT: + RETVAL MODULE = cf PACKAGE = cf::arch @@ -2340,55 +2448,66 @@ RETVAL = get_firstparty (); OUTPUT: RETVAL -partylist *next (partylist *party) - CODE: - RETVAL = party->next; - OUTPUT: RETVAL +INCLUDE: $PERL genacc partylist ../include/player.h | + +MODULE = cf PACKAGE = cf::region -const char *name (partylist *party) +void +list () + PPCODE: + for_all_regions (rgn) + XPUSHs (sv_2mortal (to_sv (rgn))); + +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 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 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 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 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 -INCLUDE: $PERL genacc living ../include/living.h | +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); +}