--- deliantra/server/plugins/cfperl/cfperl.xs 2006/02/10 07:37:13 1.22 +++ deliantra/server/plugins/cfperl/cfperl.xs 2006/03/16 21:08:20 1.33 @@ -31,7 +31,7 @@ #undef save_long // clashes with libproto.h #define PLUGIN_NAME "perl" -#define PLUGIN_VERSION "cfperl 0.1" +#define PLUGIN_VERSION "cfperl 0.2" #ifndef __CEXTRACT__ #include @@ -60,21 +60,21 @@ static f_plug_api map_get_map; static f_plug_api 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; + mapstruct* map; char message[1024]; - int fix; + 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 int current_command = -999; - static HV *obj_cache; static PerlInterpreter *perl; @@ -128,24 +128,37 @@ return sv_bless (newRV_noinc (sv), gv_stashpv (klass, 1)); } +static void +SVptr_cache_set (void *ptr, SV *sv) +{ + hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0); +} + +static SV * +SVptr_cache_get (void *ptr) +{ + SV **he = hv_fetch (obj_cache, (char *)&ptr, sizeof (ptr), 0); + + return he ? *he : 0; +} + static SV * newSVptr_cached (void *ptr, const char *klass) { - SV *sv, **he; + SV *sv; if (!ptr) return &PL_sv_undef; - he = hv_fetch (obj_cache, (char *)&ptr, sizeof (ptr), 0); + sv = SVptr_cache_get (ptr); - if (he) - sv = *he; - else + if (!sv) { 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)); - hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0); + + SVptr_cache_set (ptr, sv); } return newSVsv (sv); @@ -194,6 +207,12 @@ switch (type) { +#if 0 + case CFAPI_INT16: + sv = newSViv (*va_arg (args, sint16_t *)); + break; +#endif + case CFAPI_INT: sv = newSViv (*va_arg (args, int *)); break; @@ -283,6 +302,7 @@ hv_context (CFAPI_POBJECT, ,who); hv_context (CFAPI_POBJECT, ,activator); hv_context (CFAPI_POBJECT, ,third); + hv_context (CFAPI_PMAP, ,map); hv_context (CFAPI_STRING , ,message); hv_context (CFAPI_INT ,&,fix); hv_context (CFAPI_INT ,&,event_code); @@ -428,12 +448,18 @@ registerGlobalEvent (NULL, EVENT_MAPENTER, PLUGIN_NAME, globalEventListener); registerGlobalEvent (NULL, EVENT_MAPLEAVE, PLUGIN_NAME, globalEventListener); registerGlobalEvent (NULL, EVENT_MAPRESET, PLUGIN_NAME, globalEventListener); + registerGlobalEvent (NULL, EVENT_MAPLOAD, PLUGIN_NAME, globalEventListener); + registerGlobalEvent (NULL, EVENT_MAPOUT, PLUGIN_NAME, globalEventListener); + registerGlobalEvent (NULL, EVENT_MAPIN, PLUGIN_NAME, globalEventListener); + registerGlobalEvent (NULL, EVENT_MAPCLEAN, PLUGIN_NAME, globalEventListener); registerGlobalEvent (NULL, EVENT_REMOVE, PLUGIN_NAME, globalEventListener); registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener); registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener); registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener); registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener); registerGlobalEvent (NULL, EVENT_FREE_OB, PLUGIN_NAME, globalEventListener); + registerGlobalEvent (NULL, EVENT_PLAYER_LOAD, PLUGIN_NAME, globalEventListener); + registerGlobalEvent (NULL, EVENT_PLAYER_SAVE, PLUGIN_NAME, globalEventListener); char *argv[] = { "", @@ -489,6 +515,21 @@ printf ("Unimplemented for now\n"); break; + case EVENT_PLAYER_LOAD: + case EVENT_PLAYER_SAVE: + context.who = va_arg (args, object *); + buf = va_arg (args, char *); + if (buf != 0) + strncpy (context.message, buf, sizeof (context.message)); + break; + + case EVENT_MAPLOAD: + case EVENT_MAPOUT: + case EVENT_MAPIN: + case EVENT_MAPCLEAN: + context.map = va_arg (args, mapstruct *); + break; + case EVENT_MAPENTER: case EVENT_MAPLEAVE: case EVENT_FREE_OB: @@ -532,6 +573,7 @@ break; case EVENT_MAPRESET: + /* stupid, should be the map itself, not "message"??? */ buf = va_arg (args, char *); if (buf != 0) strncpy (context.message, buf, sizeof (context.message)); @@ -623,6 +665,7 @@ const_iv (llevMonster) const_iv (PLAYER) + const_iv (TRANSPORT) const_iv (ROD) const_iv (TREASURE) const_iv (POTION) @@ -637,7 +680,6 @@ const_iv (ARMOUR) const_iv (PEDESTAL) const_iv (ALTAR) - const_iv (CONFUSION) const_iv (LOCKED_DOOR) const_iv (SPECIAL_KEY) const_iv (MAP) @@ -748,10 +790,38 @@ const_iv (ST_BD_BUILD) const_iv (ST_BD_REMOVE) + const_iv (ST_MAT_FLOOR) const_iv (ST_MAT_WALL) const_iv (ST_MAT_ITEM) + const_iv (AT_PHYSICAL) + const_iv (AT_MAGIC) + const_iv (AT_FIRE) + const_iv (AT_ELECTRICITY) + const_iv (AT_COLD) + const_iv (AT_CONFUSION) + const_iv (AT_ACID) + const_iv (AT_DRAIN) + const_iv (AT_WEAPONMAGIC) + const_iv (AT_GHOSTHIT) + const_iv (AT_POISON) + const_iv (AT_SLOW) + const_iv (AT_PARALYZE) + const_iv (AT_TURN_UNDEAD) + const_iv (AT_FEAR) + const_iv (AT_CANCELLATION) + const_iv (AT_DEPLETE) + const_iv (AT_DEATH) + const_iv (AT_CHAOS) + const_iv (AT_COUNTERSPELL) + const_iv (AT_GODPOWER) + const_iv (AT_HOLYWORD) + const_iv (AT_BLIND) + const_iv (AT_INTERNAL) + const_iv (AT_LIFE_STEALING) + const_iv (AT_DISEASE) + const_iv (QUEST_IN_PROGRESS) const_iv (QUEST_DONE_QUEST) const_iv (QUEST_DONE_TASK) @@ -928,6 +998,9 @@ const_iv (WILL_APPLY_EARTHWALL) const_iv (WILL_APPLY_DOOR) const_iv (WILL_APPLY_FOOD) + + const_iv (SAVE_MODE) + const_iv (SAVE_DIR_MODE) }; for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) @@ -957,12 +1030,18 @@ const_event (CLOCK) const_event (CRASH) const_event (PLAYER_DEATH) + const_event (PLAYER_LOAD) + const_event (PLAYER_SAVE) const_event (GKILL) const_event (LOGIN) const_event (LOGOUT) const_event (MAPENTER) const_event (MAPLEAVE) const_event (MAPRESET) + const_event (MAPLOAD) + const_event (MAPOUT) + const_event (MAPIN) + const_event (MAPCLEAN) const_event (REMOVE) const_event (SHOUT) const_event (TELL) @@ -1172,12 +1251,46 @@ } break; case CFAPI_STRING: - cf_object_set_string_property (obj, idx, SvPV_nolen (newval)); + 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 + +void +set_attacktype (object *obj, U32 attacktype) + CODE: + obj->attacktype = attacktype; + +U32 +get_attacktype (object *obj) + ALIAS: + attacktype = 0 + CODE: + RETVAL = obj->attacktype; + OUTPUT: RETVAL + +void +set_food (object *obj, int food) + CODE: + obj->stats.food = food; + +int +get_food (object *obj) + ALIAS: + food = 0 + CODE: + RETVAL = obj->stats.food; + OUTPUT: RETVAL + void inv (object *obj) PROTOTYPE: $ @@ -1218,9 +1331,9 @@ int cf_object_pay_amount (object *op, double amount) -int cf_object_cast_spell (object *caster, object *ctoo, int dir, object *sp_, char *flags) +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 *flags) +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) @@ -1266,6 +1379,8 @@ void cf_object_set_key (object *op, char *keyname, char *value) +object *cf_create_object_by_name (const char *name) + MODULE = cf PACKAGE = cf::object PREFIX = cf_ void cf_fix_object (object *pl) @@ -1273,13 +1388,13 @@ object *cf_insert_ob_in_ob (object *ob, object *where) -object *create_object_by_name (const char *name = 0) +# no clean way to get an object from an archetype - stupid idiotic +# dumb kludgy misdesigned plug-in api slowly gets on my nerves. + +object *new (const char *archetype = 0) PROTOTYPE: ;$ - ALIAS: - create_object = 0 - new = 0 CODE: - RETVAL = name ? cf_create_object_by_name (name) : cf_create_object (); + RETVAL = archetype ? get_archetype (archetype) : cf_create_object (); OUTPUT: RETVAL @@ -1444,6 +1559,7 @@ PPCODE: { object *o; + for (o = GET_MAP_OB (obj, x, y); o; o = o->above) XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o))); } @@ -1476,6 +1592,77 @@ OUTPUT: RETVAL +# "serialise" map perl data into a ref +void +_get_obs (mapstruct *map) + PPCODE: +{ + object *o; + int x, y; + AV *obs = newAV (); + int nonnull = 0; + + for (y = 0; y < MAP_HEIGHT (map); y++) + for (x = 0; x < MAP_WIDTH (map); x++) + { + AV *av = newAV (); + + for (o = GET_MAP_OB (map, x, y); o; o = o->above) + { + SV *sv = SVptr_cache_get (o); + + if (sv && HvFILL (SvRV (sv))) + { + nonnull = 1; + sv = newSVsv (sv); + } + else + sv = &PL_sv_undef; + + av_push (av, sv); + } + + av_store (obs, x + y * MAP_HEIGHT (map), newRV_noinc ((SV *)av)); + } + + if (nonnull) + XPUSHs (sv_2mortal (newRV_noinc ((SV *)obs))); + else + SvREFCNT_dec (obs); +} + +# "deserialise" perl map data into the map +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); + + if (!SvROK (sv)) + continue; + + av = (AV *)SvRV (sv); + + for (o = GET_MAP_OB (map, x, y); o; o = o->above) + { + sv = av_shift (av); + + if (SvROK (sv)) + { + sv_magic ((SV *)SvRV (sv), 0, PERL_MAGIC_ext, (char *)o, 0); + SVptr_cache_set (o, sv); + } + } + } +} MODULE = cf PACKAGE = cf::arch PREFIX = cf_archetype_