--- deliantra/server/plugins/cfperl/cfperl.xs 2006/03/17 01:04:44 1.36 +++ deliantra/server/plugins/cfperl/cfperl.xs 2006/05/07 19:24:27 1.51 @@ -1,6 +1,5 @@ /*****************************************************************************/ /* CrossFire, A Multiplayer game for the X Window System */ -/* */ /*****************************************************************************/ /* @@ -31,7 +30,7 @@ #undef save_long // clashes with libproto.h #define PLUGIN_NAME "perl" -#define PLUGIN_VERSION "cfperl 0.2" +#define PLUGIN_VERSION "cfperl 0.3" #ifndef __CEXTRACT__ #include @@ -44,6 +43,7 @@ #endif #include +#include #include @@ -66,6 +66,7 @@ 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 @@ -78,6 +79,14 @@ static HV *obj_cache; static PerlInterpreter *perl; +#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)))) + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // garbage collect some perl objects, if possible @@ -218,11 +227,11 @@ break; case CFAPI_LONG: - sv = newSViv (*va_arg (args, long *)); + sv = newSVnv ((double)*va_arg (args, sint64 *)); /* oh, the humanity! */ break; case CFAPI_DOUBLE: - sv = newSViv (*va_arg (args, double *)); + sv = newSVnv (*va_arg (args, double *)); break; case CFAPI_STRING: @@ -239,7 +248,7 @@ if (!obj) sv = &PL_sv_undef; else - switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE)) + switch (obj->type) { case MAP: sv = newSVptr_cached (obj, "cf::object::map"); @@ -287,46 +296,6 @@ ///////////////////////////////////////////////////////////////////////////// -void -inject_event (const char *func, CFPContext *context) -{ - dSP; - - ENTER; - SAVETMPS; - - PUSHMARK (SP); - - HV *hv = newHV (); -#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0) - 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); - hv_context (CFAPI_STRING , ,options); - hv_context (CFAPI_STRING , ,extension); - - XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); - - PUTBACK; - int count = call_pv (func, G_SCALAR | G_EVAL); - SPAGAIN; - - if (SvTRUE (ERRSV)) - LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV)); - - context->returnvalue = count > 0 ? POPi : 0; - - PUTBACK; - FREETMPS; - LEAVE; -} - -///////////////////////////////////////////////////////////////////////////// - int initPlugin (const char *iversion, f_plug_api gethooksptr) { @@ -495,104 +464,109 @@ globalEventListener (int *type, ...) { va_list args; - static int rv = 0; - CFPContext context; - char *buf; - player *pl; - object *op; + static int rv; + int event_code; if (!perl) return; - memset (&context, 0, sizeof (context)); - va_start (args, type); - context.event_code = va_arg (args, int); + event_code = va_arg (args, int); - switch (context.event_code) + if (event_code == EVENT_FREE_OB) { - case EVENT_CRASH: - 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; + player *pl; + object *op; + SV *sv; - case EVENT_MAPENTER: - case EVENT_MAPLEAVE: - case EVENT_FREE_OB: - case EVENT_BORN: - case EVENT_REMOVE: - context.activator = va_arg (args, object *); - break; + op = va_arg (args, object *); + sv = hv_delete (obj_cache, (char *)&op, sizeof (void *), 0); - case EVENT_PLAYER_DEATH: - context.who = va_arg (args, object *); - break; + if (sv) + clearSVptr (sv); - case EVENT_GKILL: - context.who = va_arg (args, object *); - context.activator = va_arg (args, object *); - break; + rv = 0; + } + else + { + dSP; - case EVENT_LOGIN: - case EVENT_LOGOUT: - pl = va_arg (args, player *); - context.activator = pl->ob; - buf = va_arg (args, char *); - if (buf != 0) - strncpy (context.message, buf, sizeof (context.message)); - break; + ENTER; + SAVETMPS; - case EVENT_SHOUT: - case EVENT_MUZZLE: - case EVENT_KICK: - context.activator = va_arg (args, object *); - buf = va_arg (args, char *); - if (buf != 0) - strncpy (context.message, buf, sizeof (context.message)); - break; + PUSHMARK (SP); - case EVENT_CLOCK: - clean_obj_cache (); - break; + EXTEND (SP, 10); + PUSHs (sv_2mortal (newSViv (event_code))); - case EVENT_TELL: - break; + switch (event_code) + { + case EVENT_CRASH: + 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)); - break; - } + case EVENT_PLAYER_LOAD: + case EVENT_PLAYER_SAVE: + PUSH_OB; + PUSH_PV; + break; + + case EVENT_MAPLOAD: + case EVENT_MAPOUT: + case EVENT_MAPIN: + case EVENT_MAPCLEAN: + case EVENT_MAPRESET: + PUSH_MAP; + break; + + case EVENT_MAPENTER: + case EVENT_MAPLEAVE: + case EVENT_BORN: + case EVENT_REMOVE: + case EVENT_PLAYER_DEATH: + PUSH_OB; + break; + + case EVENT_GKILL: + PUSH_OB; + PUSH_OB; + break; + + case EVENT_LOGIN: + case EVENT_LOGOUT: + PUSH_PL; + PUSH_PV; + break; + + case EVENT_SHOUT: + case EVENT_MUZZLE: + case EVENT_KICK: + PUSH_OB; + PUSH_PV; + break; + + case EVENT_CLOCK: + clean_obj_cache (); + break; - va_end (args); + case EVENT_TELL: + break; + } - if (context.event_code == EVENT_FREE_OB) - { - SV *sv = hv_delete (obj_cache, (char *)&context.activator, sizeof (void *), 0); + va_end (args); - if (sv) - clearSVptr (sv); + PUTBACK; + int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL); + SPAGAIN; + + if (SvTRUE (ERRSV)) + LOG (llevError, "global event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV)); + + rv = count > 0 ? POPi : 0; + + PUTBACK; + FREETMPS; + LEAVE; } - else - inject_event ("cf::inject_global_event", &context); - - rv = context.returnvalue; return &rv; } @@ -600,36 +574,93 @@ void * eventListener (int *type, ...) { - static int rv = 0; + static int rv; va_list args; - char *buf; - CFPContext context; + int event_code; + object *who, *activator, *third, *event; + char *message, *extension, *options; if (!perl) return; - memset (&context, 0, sizeof (context)); - va_start (args, type); - - context.who = va_arg (args, object *); - context.event_code = va_arg (args, int); - context.activator = va_arg (args, object *); - context.third = va_arg (args, object *); - - buf = va_arg (args, char *); - if (buf != 0) - strncpy (context.message, buf, sizeof (context.message)); - - context.fix = va_arg (args, int); - strncpy (context.extension, va_arg (args, char *), sizeof (context.extension)); - strncpy (context.options, va_arg (args, char *), sizeof (context.options)); - context.returnvalue = 0; + who = va_arg (args, object *); + event_code = va_arg (args, int); + activator = va_arg (args, object *); + third = va_arg (args, object *); + message = va_arg (args, char *); + va_arg (args, int); // fix yourself + extension = va_arg (args, char *); + options = va_arg (args, char *); + event = va_arg (args, object *); va_end (args); - inject_event ("cf::inject_event", &context); + { + dSP; + + ENTER; + SAVETMPS; + + PUSHMARK (SP); + EXTEND (SP, 10); + + PUSHcfapi (STRING, extension); + PUSHs (sv_2mortal (newSViv (event_code))); + + PUSHcfapi (POBJECT, event); + PUSHcfapi (POBJECT, who); + + switch (event_code) + { + case EVENT_STOP: // $ob (e.g. arrow) + case EVENT_TIME: // $ob + case EVENT_TIMER: // $ob + break; + + case EVENT_APPLY: // $ob, $who + case EVENT_DROP: // $ob, $who + case EVENT_CLOSE: // $ob, $who + case EVENT_DEATH: // $ob[, $killer] + case EVENT_MOVE: // $ob, $enemy + case EVENT_THROW: // $ob, $thrower + PUSHcfapi (POBJECT, activator); + break; + + case EVENT_ATTACK: // $ob, $who, $victim (?? please god enlighten me) + PUSHcfapi (POBJECT, activator); + PUSHcfapi (POBJECT, third); + break; + + case EVENT_TRIGGER: // $ob, $originator, [$victim], [$msg] + PUSHcfapi (POBJECT, activator); + PUSHcfapi (POBJECT, third); + PUSHcfapi (POBJECT, message); + break; + + case EVENT_SAY: // $ob, $who, $msg + PUSHcfapi (POBJECT, activator); + PUSHcfapi (STRING, message); + break; + + default: + LOG (llevError, "perl plugin called for unsupported event type %d", event_code); + break; + } + + PUTBACK; + int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL); + SPAGAIN; + + if (SvTRUE (ERRSV)) + LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV)); + + rv = count > 0 ? POPi : 0; + + PUTBACK; + FREETMPS; + LEAVE; + } - rv = context.returnvalue; return &rv; } @@ -1016,6 +1047,78 @@ const_iv (M_BONE) const_iv (M_ICE) const_iv (M_SPECIAL) + + const_iv (SK_EXP_ADD_SKILL) + const_iv (SK_EXP_TOTAL) + const_iv (SK_EXP_NONE) + const_iv (SK_SUBTRACT_SKILL_EXP) + + const_iv (SK_LOCKPICKING) + const_iv (SK_HIDING) + const_iv (SK_SMITHERY) + const_iv (SK_BOWYER) + const_iv (SK_JEWELER) + const_iv (SK_ALCHEMY) + const_iv (SK_STEALING) + const_iv (SK_LITERACY) + const_iv (SK_BARGAINING) + const_iv (SK_JUMPING) + const_iv (SK_DET_MAGIC) + const_iv (SK_ORATORY) + const_iv (SK_SINGING) + const_iv (SK_DET_CURSE) + const_iv (SK_FIND_TRAPS) + const_iv (SK_MEDITATION) + const_iv (SK_PUNCHING) + const_iv (SK_FLAME_TOUCH) + const_iv (SK_KARATE) + const_iv (SK_CLIMBING) + const_iv (SK_WOODSMAN) + const_iv (SK_INSCRIPTION) + const_iv (SK_ONE_HANDED_WEAPON) + const_iv (SK_MISSILE_WEAPON) + const_iv (SK_THROWING) + const_iv (SK_USE_MAGIC_ITEM) + const_iv (SK_DISARM_TRAPS) + const_iv (SK_SET_TRAP) + const_iv (SK_THAUMATURGY) + const_iv (SK_PRAYING) + const_iv (SK_CLAWING) + const_iv (SK_LEVITATION) + const_iv (SK_SUMMONING) + const_iv (SK_PYROMANCY) + const_iv (SK_EVOCATION) + const_iv (SK_SORCERY) + const_iv (SK_TWO_HANDED_WEAPON) + const_iv (SK_SPARK_TOUCH) + const_iv (SK_SHIVER) + const_iv (SK_ACID_SPLASH) + const_iv (SK_POISON_NAIL) + + const_iv (SOUND_NEW_PLAYER) + const_iv (SOUND_FIRE_ARROW) + const_iv (SOUND_LEARN_SPELL) + const_iv (SOUND_FUMBLE_SPELL) + const_iv (SOUND_WAND_POOF) + const_iv (SOUND_OPEN_DOOR) + const_iv (SOUND_PUSH_PLAYER) + const_iv (SOUND_PLAYER_HITS1) + const_iv (SOUND_PLAYER_HITS2) + const_iv (SOUND_PLAYER_HITS3) + const_iv (SOUND_PLAYER_HITS4) + const_iv (SOUND_PLAYER_IS_HIT1) + const_iv (SOUND_PLAYER_IS_HIT2) + const_iv (SOUND_PLAYER_IS_HIT3) + const_iv (SOUND_PLAYER_KILLS) + const_iv (SOUND_PET_IS_KILLED) + const_iv (SOUND_PLAYER_DIES) + const_iv (SOUND_OB_EVAPORATE) + const_iv (SOUND_OB_EXPLODE) + const_iv (SOUND_CLOCK) + const_iv (SOUND_TURN_HANDLE) + const_iv (SOUND_FALL_HOLE) + const_iv (SOUND_DRINK_POISON) + const_iv (SOUND_CAST_SPELL_0) }; for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) @@ -1169,7 +1272,6 @@ prop (CFAPI_STRING, OBJECT_PROP_SHORT_NAME) prop (CFAPI_INT, OBJECT_PROP_MAGICAL) prop (CFAPI_INT, OBJECT_PROP_LUCK) - prop (CFAPI_LONG, OBJECT_PROP_EXP) prop (CFAPI_POBJECT, OBJECT_PROP_OWNER) prop (CFAPI_POBJECT, OBJECT_PROP_PRESENT) prop (CFAPI_INT, OBJECT_PROP_CHEATER) @@ -1397,6 +1499,12 @@ object *cf_create_object_by_name (const char *name) +void change_exp (object *op, double exp, const char *skill_name = 0, int flag = 0) + +void player_lvl_adj (object *who, object *skill = 0) + +int kill_object (object *op, int dam = 0, object *hitter = 0, int type = AT_PHYSICAL) + MODULE = cf PACKAGE = cf::object PREFIX = cf_ void cf_fix_object (object *pl) @@ -1422,6 +1530,17 @@ RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y); } +# syntatic sugar for easier use in event callbacks. +const char *options (object *op) + CODE: + RETVAL = op->name; + OUTPUT: + RETVAL + +const char *get_ob_key_value (object *op, const char *key) + +bool set_ob_key_value (object *op, const char *key, const char *value = 0, int add_key = 1) + object *get_nearest_player (object *ob) ALIAS: nearest_player = 0 PREINIT: @@ -1463,7 +1582,7 @@ player *player (object *op) CODE: - RETVAL = cf_player_find (cf_query_name (op)); + RETVAL = op->contr; OUTPUT: RETVAL void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE) @@ -1488,6 +1607,9 @@ void cf_player_set_party (object *op, partylist *party) +void change_skill (object *op, double exp, char *skill_name = 0, int flag = 0) + +void kill_player (object *op) MODULE = cf PACKAGE = cf::object::map PREFIX = cf_ @@ -1500,6 +1622,8 @@ void MapNewmapCmd (player *pl) +void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0); + # nonstandard object *ob (player *pl) CODE: @@ -1516,6 +1640,15 @@ RETVAL = pl->next; OUTPUT: RETVAL +int +listening (player *pl, int new_value = -1) + CODE: + RETVAL = pl->listening; + if (new_value >= 0) + pl->listening = new_value; + OUTPUT: + RETVAL + void get_savebed (player *pl) ALIAS: savebed = 0 @@ -1540,6 +1673,16 @@ 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: @@ -1583,6 +1726,12 @@ OUTPUT: RETVAL +void delete_map (mapstruct *map) + +void clean_tmp_map (mapstruct *map) + +void play_sound_map (mapstruct *map, int x, int y, int sound_num) + mapstruct *cf_map_get_map (char *name) PROTOTYPE: $ ALIAS: map = 0 @@ -1591,6 +1740,23 @@ PROTOTYPE: ALIAS: first = 0 +# whoever "designed" the plug-in api should have wasted +# his/her time with staying away form the project - would have +# saved others a lot of time, without doubt. +void set_path (mapstruct *where, char *path) + CODE: + strcpy (where->path, path); + +bool unique (mapstruct *map) + CODE: + RETVAL = map->unique; + OUTPUT: + RETVAL + +void set_unique (mapstruct *map, bool unique) + CODE: + map->unique = unique; + object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags) object *cf_map_insert_object (mapstruct *where, object* op, int x, int y) @@ -1713,6 +1879,7 @@ } } + MODULE = cf PACKAGE = cf::arch PREFIX = cf_archetype_ archetype *cf_archetype_get_first()