ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/plugins/cfperl/cfperl.xs
(Generate patch)

Comparing deliantra/server/plugins/cfperl/cfperl.xs (file contents):
Revision 1.12 by root, Tue Feb 7 03:01:29 2006 UTC vs.
Revision 1.15 by root, Wed Feb 8 03:46:15 2006 UTC

28#include <perl.h> 28#include <perl.h>
29#include <XSUB.h> 29#include <XSUB.h>
30 30
31#undef save_long // clashes with libproto.h 31#undef save_long // clashes with libproto.h
32 32
33#define PLUGIN_NAME "cfperl" 33#define PLUGIN_NAME "perl"
34#define PLUGIN_VERSION "cfperl 0.0" 34#define PLUGIN_VERSION "cfperl 0.0"
35 35
36#ifndef __CEXTRACT__ 36#ifndef __CEXTRACT__
37#include <plugin.h> 37#include <plugin.h>
38#endif 38#endif
62 object* activator; 62 object* activator;
63 object* third; 63 object* third;
64 char message[1024]; 64 char message[1024];
65 int fix; 65 int fix;
66 int event_code; 66 int event_code;
67 char options[1024]; 67 char extension[1024]; // name field, should invoke specific perl extension
68 char options[1024]; // slaying field of event_connectors
68 int returnvalue; 69 int returnvalue;
69} CFPContext; 70} CFPContext;
70 71
71//static int current_command = -999; 72//static int current_command = -999;
72 73
74static HV *obj_cache;
73static PerlInterpreter *perl; 75static PerlInterpreter *perl;
74 76
75////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 77//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
78
79// garbage collect some perl objects, if possible
80// all objects no longer referenced and empty are
81// eligible for destruction.
82void
83clean_obj_cache ()
84{
85 int todo = 10;
86 do
87 {
88 I32 klen;
89 char *key;
90 HE *he = hv_iternext (obj_cache);
91
92 if (he)
93 {
94 SV *sv = SvRV (hv_iterval (obj_cache, he));
95
96 // emopty and unreferened? nuke it
97 if (SvREFCNT (sv) == 1 && !HvFILL ((HV *)sv))
98 {
99 hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD);
100 todo++;
101 }
102 }
103 else
104 break;
105 }
106 while (--todo);
107}
76 108
77static SV * 109static SV *
78newSVptr (void *ptr, const char *klass) 110newSVptr (void *ptr, const char *klass)
79{ 111{
80 if (!ptr) 112 if (!ptr)
81 return &PL_sv_undef; 113 return &PL_sv_undef;
82 114
83 HV *hv = newHV (); 115 HV *hv = newHV ();
84 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); 116 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
85 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 117 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
118}
119
120static SV *
121newSVptr_cached (void *ptr, const char *klass)
122{
123 SV *sv, **he;
124
125 if (!ptr)
126 return &PL_sv_undef;
127
128 he = hv_fetch (obj_cache, (char *)&ptr, sizeof (ptr), 0);
129
130 if (he)
131 sv = *he;
132 else
133 {
134 sv = newSVptr (ptr, klass);
135 hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0);
136 }
137
138 return newSVsv (sv);
86} 139}
87 140
88static void 141static void
89clearSVptr (SV *sv) 142clearSVptr (SV *sv)
90{ 143{
146 sv = &PL_sv_undef; 199 sv = &PL_sv_undef;
147 else 200 else
148 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE)) 201 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE))
149 { 202 {
150 case MAP: 203 case MAP:
151 sv = newSVptr (obj, "cf::object::map"); 204 sv = newSVptr_cached (obj, "cf::object::map");
152 break; 205 break;
153 206
154 case PLAYER: 207 case PLAYER:
155 sv = newSVptr (obj, "cf::object::player"); 208 sv = newSVptr_cached (obj, "cf::object::player");
156 break; 209 break;
157 210
158 default: 211 default:
159 sv = newSVptr (obj, "cf::object"); 212 sv = newSVptr_cached (obj, "cf::object");
160 break; 213 break;
161 } 214 }
162 } 215 }
163 break; 216 break;
164 217
192} 245}
193 246
194///////////////////////////////////////////////////////////////////////////// 247/////////////////////////////////////////////////////////////////////////////
195 248
196void 249void
197inject_event (CFPContext *context) 250inject_event (const char *func, CFPContext *context)
198{ 251{
199 dSP; 252 dSP;
200 253
201 ENTER; 254 ENTER;
202 SAVETMPS; 255 SAVETMPS;
203 256
204 PUSHMARK (SP); 257 PUSHMARK (SP);
205
206 EXTEND (SP, 2);
207 //PUSHs (sv_2mortal (newSViv (type)));
208 258
209 HV *hv = newHV (); 259 HV *hv = newHV ();
210#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0) 260#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0)
211 hv_context (CFAPI_POBJECT, ,who); 261 hv_context (CFAPI_POBJECT, ,who);
212 hv_context (CFAPI_POBJECT, ,activator); 262 hv_context (CFAPI_POBJECT, ,activator);
213 hv_context (CFAPI_POBJECT, ,third); 263 hv_context (CFAPI_POBJECT, ,third);
214 hv_context (CFAPI_STRING , ,message); 264 hv_context (CFAPI_STRING , ,message);
215 hv_context (CFAPI_INT ,&,fix); 265 hv_context (CFAPI_INT ,&,fix);
216 hv_context (CFAPI_INT ,&,event_code); 266 hv_context (CFAPI_INT ,&,event_code);
217 hv_context (CFAPI_STRING , ,options); 267 hv_context (CFAPI_STRING , ,options);
268 hv_context (CFAPI_STRING , ,extension);
218 269
219 PUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 270 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
220 271
221 PUTBACK; 272 PUTBACK;
222 int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL); 273 int count = call_pv (func, G_SCALAR | G_EVAL);
223 SPAGAIN; 274 SPAGAIN;
224 275
225 if (SvTRUE (ERRSV)) 276 if (SvTRUE (ERRSV))
226 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV)); 277 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV));
227 278
357 registerGlobalEvent (NULL, EVENT_REMOVE, PLUGIN_NAME, globalEventListener); 408 registerGlobalEvent (NULL, EVENT_REMOVE, PLUGIN_NAME, globalEventListener);
358 registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener); 409 registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener);
359 registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener); 410 registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener);
360 registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener); 411 registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener);
361 registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener); 412 registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener);
413 registerGlobalEvent (NULL, EVENT_FREE_OB, PLUGIN_NAME, globalEventListener);
362 414
363 char *argv[] = { 415 char *argv[] = {
364 "", 416 "",
365 "-e" 417 "-e"
366 "BEGIN {" 418 "BEGIN {"
380 432
381 perl_destruct (perl); 433 perl_destruct (perl);
382 perl_free (perl); 434 perl_free (perl);
383 perl = 0; 435 perl = 0;
384 } 436 }
437 else
438 {
439 obj_cache = newHV ();
440 }
385 441
386 return 0; 442 return 0;
387} 443}
388 444
389void * 445void *
408 { 464 {
409 case EVENT_CRASH: 465 case EVENT_CRASH:
410 printf ("Unimplemented for now\n"); 466 printf ("Unimplemented for now\n");
411 break; 467 break;
412 468
469 case EVENT_MAPENTER:
470 case EVENT_MAPLEAVE:
471 case EVENT_FREE_OB:
413 case EVENT_BORN: 472 case EVENT_BORN:
473 case EVENT_REMOVE:
414 context.activator = va_arg (args, object *); 474 context.activator = va_arg (args, object *);
415 break; 475 break;
416 476
417 case EVENT_PLAYER_DEATH: 477 case EVENT_PLAYER_DEATH:
418 context.who = va_arg (args, object *); 478 context.who = va_arg (args, object *);
422 context.who = va_arg (args, object *); 482 context.who = va_arg (args, object *);
423 context.activator = va_arg (args, object *); 483 context.activator = va_arg (args, object *);
424 break; 484 break;
425 485
426 case EVENT_LOGIN: 486 case EVENT_LOGIN:
427 pl = va_arg (args, player *);
428 context.activator = pl->ob;
429 buf = va_arg (args, char *);
430 if (buf != 0)
431 strcpy (context.message, buf);
432 break;
433
434 case EVENT_LOGOUT: 487 case EVENT_LOGOUT:
435 pl = va_arg (args, player *); 488 pl = va_arg (args, player *);
436 context.activator = pl->ob; 489 context.activator = pl->ob;
437 buf = va_arg (args, char *); 490 buf = va_arg (args, char *);
438 if (buf != 0) 491 if (buf != 0)
439 strcpy (context.message, buf); 492 strncpy (context.message, buf, sizeof (context.message));
440 break;
441
442 case EVENT_REMOVE:
443 context.activator = va_arg (args, object *);
444 break; 493 break;
445 494
446 case EVENT_SHOUT: 495 case EVENT_SHOUT:
447 context.activator = va_arg (args, object *);
448 buf = va_arg (args, char *);
449 if (buf != 0)
450 strcpy (context.message, buf);
451 break;
452
453 case EVENT_MUZZLE: 496 case EVENT_MUZZLE:
454 context.activator = va_arg (args, object *);
455 buf = va_arg (args, char *);
456 if (buf != 0)
457 strcpy (context.message, buf);
458 break;
459
460 case EVENT_KICK: 497 case EVENT_KICK:
461 context.activator = va_arg (args, object *); 498 context.activator = va_arg (args, object *);
462 buf = va_arg (args, char *); 499 buf = va_arg (args, char *);
463 if (buf != 0) 500 if (buf != 0)
464 strcpy (context.message, buf); 501 strncpy (context.message, buf, sizeof (context.message));
465 break;
466
467 case EVENT_MAPENTER:
468 context.activator = va_arg (args, object *);
469 break;
470
471 case EVENT_MAPLEAVE:
472 context.activator = va_arg (args, object *);
473 break; 502 break;
474 503
475 case EVENT_CLOCK: 504 case EVENT_CLOCK:
505 clean_obj_cache ();
506 break;
507
508 case EVENT_TELL:
476 break; 509 break;
477 510
478 case EVENT_MAPRESET: 511 case EVENT_MAPRESET:
479 buf = va_arg (args, char *); 512 buf = va_arg (args, char *);
480 if (buf != 0) 513 if (buf != 0)
481 strcpy (context.message, buf); 514 strncpy (context.message, buf, sizeof (context.message));
482 break;
483
484 case EVENT_TELL:
485 break; 515 break;
486 } 516 }
487 517
488 va_end (args); 518 va_end (args);
489 519
490 inject_event (&context); 520 if (context.event_code == EVENT_FREE_OB)
521 {
522 SV *sv = hv_delete (obj_cache, (char *)&context.activator, sizeof (object *), 0);
523
524 if (sv)
525 {
526 clearSVptr (sv);
527 SvREFCNT_dec (sv);
528 }
529 }
530 else
531 inject_event ("cf::inject_global_event", &context);
491 532
492 rv = context.returnvalue; 533 rv = context.returnvalue;
493 534
494 return &rv; 535 return &rv;
495} 536}
511 552
512 context.who = va_arg (args, object *); 553 context.who = va_arg (args, object *);
513 context.event_code = va_arg (args, int); 554 context.event_code = va_arg (args, int);
514 context.activator = va_arg (args, object *); 555 context.activator = va_arg (args, object *);
515 context.third = va_arg (args, object *); 556 context.third = va_arg (args, object *);
557
516 buf = va_arg (args, char *); 558 buf = va_arg (args, char *);
517
518 if (buf != 0) 559 if (buf != 0)
519 strcpy (context.message, buf); 560 strncpy (context.message, buf, sizeof (context.message));
520 561
521 context.fix = va_arg (args, int); 562 context.fix = va_arg (args, int);
563 strncpy (context.extension, va_arg (args, char *), sizeof (context.extension));
522 strcpy (context.options, va_arg (args, char *)); 564 strncpy (context.options, va_arg (args, char *), sizeof (context.options));
523 context.returnvalue = 0; 565 context.returnvalue = 0;
524 va_end (args); 566 va_end (args);
525 567
526 inject_event (&context); 568 inject_event ("cf::inject_event", &context);
527 569
528 rv = context.returnvalue; 570 rv = context.returnvalue;
529 return &rv; 571 return &rv;
530} 572}
531 573
846 const_iv (P_NO_CLERIC) 888 const_iv (P_NO_CLERIC)
847 const_iv (P_NEED_UPDATE) 889 const_iv (P_NEED_UPDATE)
848 const_iv (P_NO_ERROR) 890 const_iv (P_NO_ERROR)
849 const_iv (P_OUT_OF_MAP) 891 const_iv (P_OUT_OF_MAP)
850 const_iv (P_NEW_MAP) 892 const_iv (P_NEW_MAP)
893
894 const_iv (UP_OBJ_INSERT)
895 const_iv (UP_OBJ_REMOVE)
896 const_iv (UP_OBJ_CHANGE)
897 const_iv (UP_OBJ_FACE)
898
899 const_iv (INS_NO_MERGE)
900 const_iv (INS_ABOVE_FLOOR_ONLY)
901 const_iv (INS_NO_WALK_ON)
902 const_iv (INS_ON_TOP)
903 const_iv (INS_BELOW_ORIGINATOR)
904 const_iv (INS_MAP_LOAD)
905
906 const_iv (WILL_APPLY_HANDLE)
907 const_iv (WILL_APPLY_TREASURE)
908 const_iv (WILL_APPLY_EARTHWALL)
909 const_iv (WILL_APPLY_DOOR)
910 const_iv (WILL_APPLY_FOOD)
851 }; 911 };
852 912
853 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 913 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
854 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 914 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
855 915
869 const_event (TIME) 929 const_event (TIME)
870 const_event (THROW) 930 const_event (THROW)
871 const_event (TRIGGER) 931 const_event (TRIGGER)
872 const_event (CLOSE) 932 const_event (CLOSE)
873 const_event (TIMER) 933 const_event (TIMER)
934 const_event (MOVE)
874 935
875 const_event (BORN) 936 const_event (BORN)
876 const_event (CLOCK) 937 const_event (CLOCK)
877 const_event (CRASH) 938 const_event (CRASH)
878 const_event (PLAYER_DEATH) 939 const_event (PLAYER_DEATH)
885 const_event (REMOVE) 946 const_event (REMOVE)
886 const_event (SHOUT) 947 const_event (SHOUT)
887 const_event (TELL) 948 const_event (TELL)
888 const_event (MUZZLE) 949 const_event (MUZZLE)
889 const_event (KICK) 950 const_event (KICK)
951 //const_event (FREE_OB)
890 }; 952 };
891 953
892 AV *av = get_av ("cf::EVENT", 1); 954 AV *av = get_av ("cf::EVENT", 1);
893 955
894 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; ) 956 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; )
1094 break; 1156 break;
1095 default: 1157 default:
1096 croak ("unhandled type '%d' in set_property '%d'", type, idx); 1158 croak ("unhandled type '%d' in set_property '%d'", type, idx);
1097 } 1159 }
1098 1160
1099SV * 1161void
1100inv (object *obj) 1162inv (object *obj)
1101 PROTOTYPE: $ 1163 PROTOTYPE: $
1102 PPCODE: 1164 PPCODE:
1103{ 1165{
1104 object *o; 1166 object *o;
1105 for (o = obj->inv; o; o = o->below) 1167 for (o = obj->inv; o; o = o->below)
1106 XPUSHs (newSVcfapi (CFAPI_POBJECT, o)); 1168 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1107} 1169}
1108 1170
1109int cf_object_get_resistance (object *op, int rtype) 1171int cf_object_get_resistance (object *op, int rtype)
1110 ALIAS: resistance = 0 1172 ALIAS: resistance = 0
1111 1173
1114 1176
1115void cf_object_set_flag (object *op, int flag, int value) 1177void cf_object_set_flag (object *op, int flag, int value)
1116 1178
1117void cf_object_move (object *op, object *originator, int dir) 1179void cf_object_move (object *op, object *originator, int dir)
1118 1180
1119void cf_object_apply (object *op, object *author, int flags) 1181void cf_object_apply (object *op, object *author, int flags = 0)
1120 1182
1121void cf_object_apply_below (object *op) 1183void cf_object_apply_below (object *op)
1122 1184
1123void cf_object_remove (object *op) 1185void cf_object_remove (object *op)
1124 1186
1205void cf_fix_object (object *pl) 1267void cf_fix_object (object *pl)
1206 ALIAS: fix = 0 1268 ALIAS: fix = 0
1207 1269
1208object *cf_insert_ob_in_ob (object *ob, object *where) 1270object *cf_insert_ob_in_ob (object *ob, object *where)
1209 1271
1272void rangevector (object *ob, object *other, int flags = 0)
1273 PROTOTYPE: $$;$
1274 ALIAS:
1275 direction = 1
1276 distance = 2
1277 CODE:
1278{
1279 rv_vector rv;
1280 // get_rangevector uses these if the objects are not on the same map
1281 // which is not the same as "on_same_map".
1282 rv.distance_x = 32767;
1283 rv.distance_y = 32767;
1284 get_rangevector (ob, other, &rv, flags);
1285 EXTEND (SP, 5);
1286 if (!ix || ix == 2) PUSHs (newSVuv (rv.distance));
1287 if (!ix) PUSHs (newSViv (rv.distance_x));
1288 if (!ix) PUSHs (newSViv (rv.distance_y));
1289 if (!ix || ix == 1) PUSHs (newSViv (rv.direction));
1290 if (!ix) PUSHs (newSVcfapi (CFAPI_POBJECT, rv.part));
1291}
1292
1293bool on_same_map_as (object *ob, object *other)
1294 CODE:
1295 RETVAL = on_same_map (ob, other);
1296 OUTPUT: RETVAL
1297
1210 1298
1211MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_ 1299MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_
1212 1300
1213player *player (object *op) 1301player *player (object *op)
1214 CODE: 1302 CODE:
1245# nonstandard 1333# nonstandard
1246object *ob (player *pl) 1334object *ob (player *pl)
1247 CODE: 1335 CODE:
1248 RETVAL = pl->ob; 1336 RETVAL = pl->ob;
1249 OUTPUT: RETVAL 1337 OUTPUT: RETVAL
1338
1339player *first ()
1340 CODE:
1341 RETVAL = first_player;
1342 OUTPUT: RETVAL
1343
1344player *next (player *pl)
1345 CODE:
1346 RETVAL = pl->next;
1347 OUTPUT: RETVAL
1348
1349void
1350list ()
1351 PPCODE:
1352{
1353 player *pl;
1354 for (pl = first_player; pl; pl = pl->next)
1355 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl));
1356}
1250 1357
1251 1358
1252MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ 1359MODULE = cf PACKAGE = cf::map PREFIX = cf_map_
1253 1360
1254SV * 1361SV *
1294object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny) 1401object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny)
1295 C_ARGS: str, map, nx, ny 1402 C_ARGS: str, map, nx, ny
1296 1403
1297#int cf_map_get_flags (mapstruct* map, mapstruct** nmap, I16 x, I16 y, I16 *nx, I16 *ny) 1404#int cf_map_get_flags (mapstruct* map, mapstruct** nmap, I16 x, I16 y, I16 *nx, I16 *ny)
1298 1405
1299SV * 1406void
1300at (mapstruct *obj, unsigned int x, unsigned int y) 1407at (mapstruct *obj, unsigned int x, unsigned int y)
1301 PROTOTYPE: $$$ 1408 PROTOTYPE: $$$
1302 INIT: 1409 INIT:
1303 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_UNDEF; 1410 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_EMPTY;
1304 PPCODE: 1411 PPCODE:
1305{ 1412{
1306 object *o; 1413 object *o;
1307 for (o = GET_MAP_OB (obj, x, y); o; o = o->above) 1414 for (o = GET_MAP_OB (obj, x, y); o; o = o->above)
1308 XPUSHs (newSVcfapi (CFAPI_POBJECT, o)); 1415 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1309} 1416}
1310 1417
1311SV * 1418SV *
1312bot_at (mapstruct *obj, unsigned int x, unsigned int y) 1419bot_at (mapstruct *obj, unsigned int x, unsigned int y)
1313 PROTOTYPE: $$$ 1420 PROTOTYPE: $$$

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines