… | |
… | |
28 | #include <XSUB.h> |
28 | #include <XSUB.h> |
29 | |
29 | |
30 | #undef save_long // clashes with libproto.h |
30 | #undef save_long // clashes with libproto.h |
31 | |
31 | |
32 | #define PLUGIN_NAME "perl" |
32 | #define PLUGIN_NAME "perl" |
33 | #define PLUGIN_VERSION "cfperl 0.3" |
33 | #define PLUGIN_VERSION "cfperl 0.5" |
34 | |
34 | |
35 | #ifndef __CEXTRACT__ |
35 | #ifndef __CEXTRACT__ |
36 | #include <plugin.h> |
36 | #include <plugin.h> |
37 | #endif |
37 | #endif |
38 | |
38 | |
… | |
… | |
46 | #include <sounds.h> |
46 | #include <sounds.h> |
47 | #include <cstdarg> |
47 | #include <cstdarg> |
48 | #include <sproto.h> |
48 | #include <sproto.h> |
49 | |
49 | |
50 | #include "cfperl.h" |
50 | #include "cfperl.h" |
|
|
51 | #include "shstr.h" |
51 | |
52 | |
52 | #include "perlxsi.c" |
53 | #include "perlxsi.c" |
53 | |
|
|
54 | static void *globalEventListener (int *type, ...); |
|
|
55 | |
54 | |
56 | extern sint64 *levels; // the experience table |
55 | extern sint64 *levels; // the experience table |
57 | |
56 | |
58 | typedef object object_ornull; |
57 | typedef object object_ornull; |
59 | typedef mapstruct mapstruct_ornull; |
58 | typedef mapstruct mapstruct_ornull; |
… | |
… | |
61 | typedef double val64; |
60 | typedef double val64; |
62 | #define newSVval64 newSVnv |
61 | #define newSVval64 newSVnv |
63 | #define SvVAL64 SvNV |
62 | #define SvVAL64 SvNV |
64 | |
63 | |
65 | static f_plug_api gethook; |
64 | static f_plug_api gethook; |
66 | static f_plug_api registerGlobalEvent; |
|
|
67 | static f_plug_api unregisterGlobalEvent; |
|
|
68 | static f_plug_api systemDirectory; |
65 | static f_plug_api systemDirectory; |
69 | static f_plug_api object_set_property; |
66 | static f_plug_api object_set_property; |
70 | static f_plug_api map_get_map; |
67 | static f_plug_api map_get_map; |
71 | static f_plug_api object_insert; |
68 | static f_plug_api object_insert; |
72 | |
69 | |
… | |
… | |
87 | } CFPContext; |
84 | } CFPContext; |
88 | |
85 | |
89 | static HV *obj_cache; |
86 | static HV *obj_cache; |
90 | static PerlInterpreter *perl; |
87 | static PerlInterpreter *perl; |
91 | |
88 | |
|
|
89 | static AV *cb_global, *cb_object, *cb_player, *cb_type, *cb_map; |
|
|
90 | |
92 | #define PUSHcfapi(type,value) PUSHs (sv_2mortal (newSVcfapi (CFAPI_ ## type, (value)))) |
91 | #define PUSHcfapi(type,value) PUSHs (sv_2mortal (newSVcfapi (CFAPI_ ## type, (value)))) |
93 | #define PUSHcfapi_va(type,ctype) PUSHcfapi (type, va_arg (args, ctype)) |
92 | #define PUSHcfapi_va(type,ctype) PUSHcfapi (type, va_arg (args, ctype)) |
94 | #define PUSH_OB PUSHcfapi_va(POBJECT, object *) |
93 | #define PUSH_OB PUSHcfapi_va(POBJECT, object *) |
95 | #define PUSH_PL PUSHcfapi_va(PPLAYER, player *) |
94 | #define PUSH_PL PUSHcfapi_va(PPLAYER, player *) |
96 | #define PUSH_MAP PUSHcfapi_va(PMAP, mapstruct *) |
95 | #define PUSH_MAP PUSHcfapi_va(PMAP, mapstruct *) |
97 | #define PUSH_PV PUSHcfapi_va(STRING, const char *) |
96 | #define PUSH_PV PUSHcfapi_va(STRING, const char *) |
98 | #define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int)))) |
97 | #define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int)))) |
99 | |
98 | |
100 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
99 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
101 | |
100 | |
102 | // garbage collect some perl objects, if possible |
|
|
103 | // all objects no longer referenced and empty are |
|
|
104 | // eligible for destruction. |
|
|
105 | static void |
|
|
106 | clean_obj_cache () |
|
|
107 | { |
|
|
108 | static int count; |
|
|
109 | |
|
|
110 | if (++count & 7) |
|
|
111 | return; |
|
|
112 | |
|
|
113 | int todo = 1000; |
|
|
114 | do |
|
|
115 | { |
|
|
116 | I32 klen; |
|
|
117 | char *key; |
|
|
118 | HE *he = hv_iternext (obj_cache); |
|
|
119 | |
|
|
120 | if (he) |
|
|
121 | { |
|
|
122 | SV *sv = hv_iterval (obj_cache, he); |
|
|
123 | |
|
|
124 | // empty and unreferenced? nuke it |
|
|
125 | if (SvREFCNT (sv) == 1 && SvREFCNT (SvRV (sv)) == 1 && !HvFILL ((HV *)(SvRV (sv)))) |
|
|
126 | { |
|
|
127 | hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD); |
|
|
128 | todo++; |
|
|
129 | } |
|
|
130 | } |
|
|
131 | else |
|
|
132 | break; |
|
|
133 | } |
|
|
134 | while (--todo); |
|
|
135 | } |
|
|
136 | |
|
|
137 | static SV * |
101 | static SV * |
138 | newSVptr (void *ptr, const char *klass) |
102 | newSVptr (void *ptr, const char *klass, HV *hv = newHV ()) |
139 | { |
103 | { |
140 | SV *sv; |
104 | SV *sv; |
141 | |
105 | |
142 | if (!ptr) |
106 | if (!ptr) |
143 | return &PL_sv_undef; |
107 | return &PL_sv_undef; |
144 | |
108 | |
145 | sv = newSV (0); |
|
|
146 | sv_magic (sv, 0, PERL_MAGIC_ext, (char *)ptr, 0); |
109 | sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); |
147 | return sv_bless (newRV_noinc (sv), gv_stashpv (klass, 1)); |
110 | return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
111 | } |
|
|
112 | |
|
|
113 | template<class attachable> |
|
|
114 | SV * |
|
|
115 | newSVattachable (attachable *obj, const char *klass) |
|
|
116 | { |
|
|
117 | if (!obj) |
|
|
118 | return &PL_sv_undef; |
|
|
119 | |
|
|
120 | if (!obj->self) |
|
|
121 | obj->self = newSVptr (obj, klass); |
|
|
122 | |
|
|
123 | return newSVsv (static_cast<SV *>(obj->self)); |
148 | } |
124 | } |
149 | |
125 | |
150 | static void |
126 | static void |
151 | SVptr_cache_set (void *ptr, SV *sv) |
127 | SVptr_cache_set (void *ptr, SV *sv) |
152 | { |
128 | { |
… | |
… | |
222 | SV *sv; |
198 | SV *sv; |
223 | |
199 | |
224 | switch (type) |
200 | switch (type) |
225 | { |
201 | { |
226 | case DT_INT: |
202 | case DT_INT: |
227 | sv = newSViv (*va_arg (ap, int *)); |
203 | sv = newSViv (va_arg (ap, int)); |
228 | break; |
204 | break; |
229 | |
205 | |
230 | case DT_LONG: |
206 | case DT_INT64: |
231 | sv = newSVval64 ((val64)*va_arg (ap, sint64 *)); |
207 | sv = newSVval64 ((val64)va_arg (ap, sint64)); |
232 | break; |
208 | break; |
233 | |
209 | |
234 | case DT_DOUBLE: |
210 | case DT_DOUBLE: |
235 | sv = newSVnv (*va_arg (ap, double *)); |
211 | sv = newSVnv (va_arg (ap, double)); |
236 | break; |
212 | break; |
237 | |
213 | |
238 | case DT_STRING: |
214 | case DT_STRING: |
239 | { |
215 | { |
240 | char *str = va_arg (ap, char *); |
216 | char *str = (char *)va_arg (ap, const char *); |
241 | sv = str ? newSVpv (str, 0) : &PL_sv_undef; |
217 | sv = str ? newSVpv (str, 0) : &PL_sv_undef; |
242 | } |
218 | } |
243 | break; |
219 | break; |
244 | |
220 | |
245 | case DT_DATA: |
221 | case DT_DATA: |
246 | { |
222 | { |
247 | char *str = (char *)va_arg (ap, void *); |
223 | char *str = (char *)va_arg (ap, const void *); |
248 | int len = va_arg (ap, int); |
224 | int len = va_arg (ap, int); |
249 | sv = str ? newSVpv (str, len) : &PL_sv_undef; |
225 | sv = str ? newSVpv (str, len) : &PL_sv_undef; |
250 | } |
226 | } |
251 | break; |
227 | break; |
252 | |
228 | |
253 | case DT_OBJECT: |
229 | case DT_OBJECT: |
254 | { |
230 | { |
255 | object *obj = va_arg (ap, object *); |
231 | object *obj = va_arg (ap, object *); |
256 | |
232 | sv = newSVattachable (obj, obj && obj->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap"); |
257 | if (!obj) |
|
|
258 | sv = &PL_sv_undef; |
|
|
259 | else |
|
|
260 | switch (obj->type) |
|
|
261 | { |
|
|
262 | case MAP: |
|
|
263 | sv = newSVptr_cached (obj, "cf::object::map::wrap"); |
|
|
264 | break; |
|
|
265 | |
|
|
266 | case PLAYER: |
|
|
267 | sv = newSVptr_cached (obj, "cf::object::player::wrap"); |
|
|
268 | break; |
|
|
269 | |
|
|
270 | default: |
|
|
271 | sv = newSVptr_cached (obj, "cf::object::wrap"); |
|
|
272 | break; |
|
|
273 | } |
|
|
274 | } |
233 | } |
275 | break; |
234 | break; |
276 | |
235 | |
277 | case DT_MAP: |
236 | case DT_MAP: |
|
|
237 | // va_arg (object *) when void * is passed is an XSI extension |
278 | sv = newSVptr (va_arg (ap, mapstruct *), "cf::map::wrap"); |
238 | sv = newSVattachable (va_arg (ap, mapstruct *), "cf::map::wrap"); |
279 | break; |
239 | break; |
280 | |
240 | |
281 | case DT_PLAYER: |
241 | case DT_PLAYER: |
282 | sv = newSVptr (va_arg (ap, player *), "cf::player::wrap"); |
242 | sv = newSVattachable (va_arg (ap, player *), "cf::player::wrap"); |
283 | break; |
243 | break; |
284 | |
244 | |
285 | case DT_ARCH: |
245 | case DT_ARCH: |
286 | sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap"); |
246 | sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap"); |
287 | break; |
247 | break; |
… | |
… | |
321 | va_list ap; |
281 | va_list ap; |
322 | va_start (ap, type); |
282 | va_start (ap, type); |
323 | |
283 | |
324 | switch (type) |
284 | switch (type) |
325 | { |
285 | { |
326 | case CFAPI_INT: sv = newSVdt_va (ap, DT_INT ); break; |
286 | case CFAPI_INT: sv = newSViv (*va_arg (ap, int * )); break; |
327 | case CFAPI_LONG: sv = newSVdt_va (ap, DT_LONG ); break; |
287 | case CFAPI_LONG: sv = newSVval64 (*va_arg (ap, sint64 *)); break; |
328 | case CFAPI_DOUBLE: sv = newSVdt_va (ap, DT_DOUBLE); break; |
288 | case CFAPI_DOUBLE: sv = newSVnv (*va_arg (ap, double *)); break; |
329 | case CFAPI_STRING: sv = newSVdt_va (ap, DT_STRING); break; |
289 | case CFAPI_STRING: sv = newSVdt_va (ap, DT_STRING); break; |
330 | case CFAPI_POBJECT: sv = newSVdt_va (ap, DT_OBJECT); break; |
290 | case CFAPI_POBJECT: sv = newSVdt_va (ap, DT_OBJECT); break; |
331 | case CFAPI_PMAP: sv = newSVdt_va (ap, DT_MAP ); break; |
291 | case CFAPI_PMAP: sv = newSVdt_va (ap, DT_MAP ); break; |
332 | case CFAPI_PPLAYER: sv = newSVdt_va (ap, DT_PLAYER); break; |
292 | case CFAPI_PPLAYER: sv = newSVdt_va (ap, DT_PLAYER); break; |
333 | case CFAPI_PARCH: sv = newSVdt_va (ap, DT_ARCH ); break; |
293 | case CFAPI_PARCH: sv = newSVdt_va (ap, DT_ARCH ); break; |
… | |
… | |
341 | va_end (ap); |
301 | va_end (ap); |
342 | |
302 | |
343 | return sv; |
303 | return sv; |
344 | } |
304 | } |
345 | |
305 | |
|
|
306 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
307 | |
|
|
308 | SV * |
|
|
309 | registry_of (attachable_base *ext) |
|
|
310 | { |
|
|
311 | if (!ext->cb) |
|
|
312 | ext->cb = newAV (); |
|
|
313 | |
|
|
314 | return newRV_inc ((SV *)ext->cb); |
|
|
315 | } |
|
|
316 | |
|
|
317 | void attachable_base::clear () |
|
|
318 | { |
|
|
319 | //if (self) fprintf (stderr, "free_ob_self %p %s (%p,%p)\n", this, ((object *)this)->name, self, cb);//D |
|
|
320 | |
|
|
321 | if (self) SvREFCNT_dec (self); |
|
|
322 | if (cb) SvREFCNT_dec (cb); |
|
|
323 | |
|
|
324 | if (attach) free_string (attach); |
|
|
325 | |
|
|
326 | self = 0; |
|
|
327 | cb = 0; |
|
|
328 | attach = 0; |
|
|
329 | } |
|
|
330 | |
|
|
331 | void attachable_base::optimise () |
|
|
332 | { |
|
|
333 | if (!self) |
|
|
334 | return; |
|
|
335 | |
|
|
336 | HV *hv = (HV *)SvRV ((SV *)self); |
|
|
337 | |
|
|
338 | if (SvREFCNT ((SV *)self) == 1 |
|
|
339 | && SvREFCNT ((SV *)hv) == 1 |
|
|
340 | && !HvKEYS (hv)) |
|
|
341 | { |
|
|
342 | SvREFCNT_dec ((SV *)self); |
|
|
343 | self = 0; |
|
|
344 | } |
|
|
345 | } |
|
|
346 | |
|
|
347 | void |
|
|
348 | object::instantiate_ () |
|
|
349 | { |
|
|
350 | dSP; |
|
|
351 | ENTER; |
|
|
352 | SAVETMPS; |
|
|
353 | PUSHMARK (SP); |
|
|
354 | EXTEND (SP, 2); |
|
|
355 | PUSHs (sv_2mortal (newSVdt (DT_OBJECT, static_cast<object *>(this)))); |
|
|
356 | PUSHs (sv_2mortal (newSVpv (attach, 0))); |
|
|
357 | PUTBACK; |
|
|
358 | call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL); |
|
|
359 | FREETMPS; |
|
|
360 | LEAVE; |
|
|
361 | |
|
|
362 | INVOKE_OBJECT (INSTANTIATE, this); |
|
|
363 | } |
|
|
364 | |
346 | ///////////////////////////////////////////////////////////////////////////// |
365 | ///////////////////////////////////////////////////////////////////////////// |
347 | |
366 | |
|
|
367 | void reattach (data_type type, void *obj) |
|
|
368 | { |
|
|
369 | dSP; |
|
|
370 | ENTER; |
|
|
371 | SAVETMPS; |
|
|
372 | PUSHMARK (SP); |
|
|
373 | XPUSHs (sv_2mortal (newSVdt (type, obj))); |
|
|
374 | PUTBACK; |
|
|
375 | call_pv ("cf::reattach", G_DISCARD | G_VOID | G_EVAL); |
|
|
376 | FREETMPS; |
|
|
377 | LEAVE; |
|
|
378 | |
|
|
379 | switch (type) |
|
|
380 | { |
|
|
381 | case DT_OBJECT: INVOKE_OBJECT (REATTACH, obj); break; |
|
|
382 | case DT_PLAYER: INVOKE_PLAYER (REATTACH, obj); break; |
|
|
383 | case DT_MAP: INVOKE_MAP (REATTACH, obj); break; |
|
|
384 | } |
|
|
385 | } |
|
|
386 | |
|
|
387 | template<class subclass> |
|
|
388 | void reattach (attachable<subclass> *obj) |
|
|
389 | { |
|
|
390 | obj->optimise (); |
|
|
391 | |
|
|
392 | if (obj->self) |
|
|
393 | reattach (subclass::get_dt (), (subclass *)obj); |
|
|
394 | } |
|
|
395 | |
|
|
396 | object_freezer::object_freezer (const char *filename) |
|
|
397 | : filename (filename) |
|
|
398 | { |
|
|
399 | av = (AV *)newAV (); |
|
|
400 | } |
|
|
401 | |
|
|
402 | object_freezer::~object_freezer () |
|
|
403 | { |
|
|
404 | dSP; |
|
|
405 | ENTER; |
|
|
406 | SAVETMPS; |
|
|
407 | PUSHMARK (SP); |
|
|
408 | XPUSHs (sv_2mortal (newSVpv (filename, 0))); |
|
|
409 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
410 | PUTBACK; |
|
|
411 | call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL); |
|
|
412 | FREETMPS; |
|
|
413 | LEAVE; |
|
|
414 | } |
|
|
415 | |
|
|
416 | void object_freezer::put (FILE *fp, attachable_base *ext) |
|
|
417 | { |
|
|
418 | ext->optimise (); |
|
|
419 | |
|
|
420 | if (ext->self) |
|
|
421 | { |
|
|
422 | int idx = AvFILLp ((AV *)av) + 1; |
|
|
423 | av_store ((AV *)av, idx, SvREFCNT_inc ((SV *)ext->self)); |
|
|
424 | fprintf (fp, "oid %d\n", idx); |
|
|
425 | } |
|
|
426 | } |
|
|
427 | |
|
|
428 | object_thawer::object_thawer (const char *filename) |
|
|
429 | { |
|
|
430 | av = 0; |
|
|
431 | |
|
|
432 | if (!filename) |
|
|
433 | return; |
|
|
434 | |
|
|
435 | dSP; |
|
|
436 | ENTER; |
|
|
437 | SAVETMPS; |
|
|
438 | PUSHMARK (SP); |
|
|
439 | XPUSHs (sv_2mortal (newSVpv (filename, 0))); |
|
|
440 | PUTBACK; |
|
|
441 | |
|
|
442 | if (0 < call_pv ("cf::object_thawer_load", G_SCALAR | G_EVAL)) |
|
|
443 | { |
|
|
444 | SPAGAIN; |
|
|
445 | SV *sv = POPs; |
|
|
446 | if (SvROK (sv)) |
|
|
447 | av = SvREFCNT_inc (SvRV (sv)); |
|
|
448 | } |
|
|
449 | |
|
|
450 | FREETMPS; |
|
|
451 | LEAVE; |
|
|
452 | } |
|
|
453 | |
|
|
454 | void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid) |
|
|
455 | { |
|
|
456 | if (!av || oid < 0) // this is actually an error of sorts |
|
|
457 | return; |
|
|
458 | |
|
|
459 | // we have to "re-instantiate"/reattach to an object, so nuke ext->attach |
|
|
460 | ext->clear (); |
|
|
461 | |
|
|
462 | SV **svp = av_fetch ((AV *)av, oid, 0); |
|
|
463 | |
|
|
464 | if (!svp || !SvROK (*svp)) |
|
|
465 | { |
|
|
466 | printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
|
|
467 | return; |
|
|
468 | } |
|
|
469 | |
|
|
470 | ext->self = *svp; *svp = &PL_sv_undef; |
|
|
471 | sv_magic (SvRV ((SV *)ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0); |
|
|
472 | |
|
|
473 | reattach (type, obj); |
|
|
474 | } |
|
|
475 | |
|
|
476 | object_thawer::~object_thawer () |
|
|
477 | { |
|
|
478 | if (av) |
|
|
479 | SvREFCNT_dec ((SV *)av); |
|
|
480 | } |
|
|
481 | |
|
|
482 | ///////////////////////////////////////////////////////////////////////////// |
|
|
483 | |
348 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
484 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
349 | { |
485 | { |
350 | gethook = gethooksptr; |
486 | gethook = gethooksptr; |
351 | printf (PLUGIN_VERSION " init\n"); |
|
|
352 | |
487 | |
353 | return 0; |
488 | return 0; |
354 | } |
489 | } |
355 | |
490 | |
356 | static CommArray_s rtn_cmd; |
491 | static CommArray_s rtn_cmd; |
… | |
… | |
437 | extern "C" int cfperl_postInitPlugin () |
572 | extern "C" int cfperl_postInitPlugin () |
438 | { |
573 | { |
439 | int hooktype = 1; |
574 | int hooktype = 1; |
440 | int rtype = 0; |
575 | int rtype = 0; |
441 | |
576 | |
442 | printf (PLUGIN_VERSION " post init\n"); |
|
|
443 | |
|
|
444 | registerGlobalEvent = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_register_global_event"); |
|
|
445 | unregisterGlobalEvent = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_unregister_global_event"); |
|
|
446 | systemDirectory = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_directory"); |
577 | systemDirectory = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_directory"); |
447 | object_set_property = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_set_property"); |
578 | object_set_property = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_set_property"); |
448 | map_get_map = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_map_get_map"); |
579 | map_get_map = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_map_get_map"); |
449 | object_insert = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_insert"); |
580 | object_insert = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_insert"); |
450 | |
581 | |
451 | cf_init_plugin (gethook); |
582 | cf_init_plugin (gethook); |
452 | |
583 | |
453 | /* Pick the global events you want to monitor from this plugin */ |
|
|
454 | registerGlobalEvent (NULL, EVENT_CLOCK, PLUGIN_NAME, globalEventListener); |
|
|
455 | //registerGlobalEvent (NULL, EVENT_CRASH, PLUGIN_NAME, globalEventListener); |
|
|
456 | registerGlobalEvent (NULL, EVENT_FIND_UNARMED_SKILL, PLUGIN_NAME, globalEventListener); |
|
|
457 | registerGlobalEvent (NULL, EVENT_PLAYER_USE_SKILL, PLUGIN_NAME, globalEventListener); |
|
|
458 | registerGlobalEvent (NULL, EVENT_CAST_SPELL, PLUGIN_NAME, globalEventListener); |
|
|
459 | registerGlobalEvent (NULL, EVENT_MONSTER_USE_SKILL, PLUGIN_NAME, globalEventListener); |
|
|
460 | registerGlobalEvent (NULL, EVENT_GKILL, PLUGIN_NAME, globalEventListener); |
|
|
461 | registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener); |
|
|
462 | registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener); |
|
|
463 | registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener); |
|
|
464 | registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener); |
|
|
465 | registerGlobalEvent (NULL, EVENT_EXTCMD, PLUGIN_NAME, globalEventListener); |
|
|
466 | |
|
|
467 | return 0; |
584 | return 0; |
468 | } |
|
|
469 | |
|
|
470 | static void * |
|
|
471 | globalEventListener (int *type, ...) |
|
|
472 | { |
|
|
473 | va_list args; |
|
|
474 | static int rv; |
|
|
475 | int event_code; |
|
|
476 | |
|
|
477 | if (!perl) |
|
|
478 | return NULL; |
|
|
479 | |
|
|
480 | va_start (args, type); |
|
|
481 | event_code = va_arg (args, int); |
|
|
482 | |
|
|
483 | if (event_code == EVENT_CLOCK) |
|
|
484 | { |
|
|
485 | clean_obj_cache (); |
|
|
486 | } |
|
|
487 | else |
|
|
488 | { |
|
|
489 | dSP; |
|
|
490 | |
|
|
491 | ENTER; |
|
|
492 | SAVETMPS; |
|
|
493 | |
|
|
494 | PUSHMARK (SP); |
|
|
495 | |
|
|
496 | EXTEND (SP, 10); |
|
|
497 | PUSHs (sv_2mortal (newSViv (event_code))); |
|
|
498 | |
|
|
499 | switch (event_code) |
|
|
500 | { |
|
|
501 | case EVENT_CRASH: |
|
|
502 | break; |
|
|
503 | |
|
|
504 | case EVENT_GKILL: |
|
|
505 | PUSH_OB; |
|
|
506 | PUSH_OB; |
|
|
507 | break; |
|
|
508 | |
|
|
509 | case EVENT_SHOUT: |
|
|
510 | case EVENT_MUZZLE: |
|
|
511 | case EVENT_KICK: |
|
|
512 | PUSH_OB; |
|
|
513 | PUSH_PV; |
|
|
514 | break; |
|
|
515 | |
|
|
516 | case EVENT_FIND_UNARMED_SKILL: |
|
|
517 | PUSH_OB; |
|
|
518 | break; |
|
|
519 | |
|
|
520 | case EVENT_PLAYER_USE_SKILL: |
|
|
521 | case EVENT_MONSTER_USE_SKILL: |
|
|
522 | case EVENT_CAST_SPELL: |
|
|
523 | PUSH_OB; |
|
|
524 | PUSH_OB; |
|
|
525 | PUSH_OB; |
|
|
526 | PUSH_IV; |
|
|
527 | PUSH_PV; |
|
|
528 | break; |
|
|
529 | |
|
|
530 | case EVENT_EXTCMD: |
|
|
531 | PUSH_PL; |
|
|
532 | { |
|
|
533 | char *buf = va_arg (args, char *); |
|
|
534 | int len = va_arg (args, int); |
|
|
535 | PUSHs (sv_2mortal (newSVpvn (buf, len))); |
|
|
536 | } |
|
|
537 | break; |
|
|
538 | |
|
|
539 | case EVENT_TELL: |
|
|
540 | break; |
|
|
541 | } |
|
|
542 | |
|
|
543 | va_end (args); |
|
|
544 | |
|
|
545 | PUTBACK; |
|
|
546 | int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL); |
|
|
547 | SPAGAIN; |
|
|
548 | |
|
|
549 | if (SvTRUE (ERRSV)) |
|
|
550 | LOG (llevError, "global event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV)); |
|
|
551 | |
|
|
552 | rv = count > 0 ? POPi : 0; |
|
|
553 | |
|
|
554 | PUTBACK; |
|
|
555 | FREETMPS; |
|
|
556 | LEAVE; |
|
|
557 | } |
|
|
558 | |
|
|
559 | return &rv; |
|
|
560 | } |
|
|
561 | |
|
|
562 | extern "C" void *cfperl_eventListener (int *type, ...) |
|
|
563 | { |
|
|
564 | static int rv; |
|
|
565 | va_list args; |
|
|
566 | int event_code; |
|
|
567 | object *who, *activator, *third, *event; |
|
|
568 | char *message, *extension, *options; |
|
|
569 | |
|
|
570 | if (!perl) |
|
|
571 | return NULL; |
|
|
572 | |
|
|
573 | va_start (args, type); |
|
|
574 | who = va_arg (args, object *); |
|
|
575 | event_code = va_arg (args, int); |
|
|
576 | activator = va_arg (args, object *); |
|
|
577 | third = va_arg (args, object *); |
|
|
578 | message = va_arg (args, char *); |
|
|
579 | va_arg (args, int); // fix yourself |
|
|
580 | extension = va_arg (args, char *); |
|
|
581 | options = va_arg (args, char *); |
|
|
582 | event = va_arg (args, object *); |
|
|
583 | va_end (args); |
|
|
584 | |
|
|
585 | { |
|
|
586 | dSP; |
|
|
587 | |
|
|
588 | ENTER; |
|
|
589 | SAVETMPS; |
|
|
590 | |
|
|
591 | PUSHMARK (SP); |
|
|
592 | EXTEND (SP, 10); |
|
|
593 | |
|
|
594 | PUSHcfapi (STRING, extension); |
|
|
595 | PUSHs (sv_2mortal (newSViv (event_code))); |
|
|
596 | |
|
|
597 | PUSHcfapi (POBJECT, event); |
|
|
598 | PUSHcfapi (POBJECT, who); |
|
|
599 | |
|
|
600 | switch (event_code) |
|
|
601 | { |
|
|
602 | case EVENT_STOP: // $ob (e.g. arrow) |
|
|
603 | case EVENT_TIME: // $ob |
|
|
604 | case EVENT_TIMER: // $ob |
|
|
605 | break; |
|
|
606 | |
|
|
607 | case EVENT_APPLY: // $ob, $who |
|
|
608 | case EVENT_DROP: // $ob, $who |
|
|
609 | case EVENT_CLOSE: // $ob, $who |
|
|
610 | case EVENT_DEATH: // $ob[, $killer] |
|
|
611 | case EVENT_MOVE: // $ob, $enemy |
|
|
612 | case EVENT_THROW: // $ob, $thrower |
|
|
613 | PUSHcfapi (POBJECT, activator); |
|
|
614 | break; |
|
|
615 | |
|
|
616 | case EVENT_DROP_ON: // $ob, $who, $what |
|
|
617 | case EVENT_ATTACK: // $ob, $who, $victim (?? please god enlighten me) |
|
|
618 | PUSHcfapi (POBJECT, activator); |
|
|
619 | PUSHcfapi (POBJECT, third); |
|
|
620 | break; |
|
|
621 | |
|
|
622 | case EVENT_TRIGGER: // $ob, $originator, [$victim], [$msg] |
|
|
623 | PUSHcfapi (POBJECT, activator); |
|
|
624 | PUSHcfapi (POBJECT, third); |
|
|
625 | PUSHcfapi (POBJECT, message); |
|
|
626 | break; |
|
|
627 | |
|
|
628 | case EVENT_SAY: // $ob, $who, $msg |
|
|
629 | PUSHcfapi (POBJECT, activator); |
|
|
630 | PUSHcfapi (STRING, message); |
|
|
631 | break; |
|
|
632 | |
|
|
633 | default: |
|
|
634 | LOG (llevError, "perl plugin called for unsupported event type %d", event_code); |
|
|
635 | break; |
|
|
636 | } |
|
|
637 | |
|
|
638 | PUTBACK; |
|
|
639 | int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL); |
|
|
640 | SPAGAIN; |
|
|
641 | |
|
|
642 | if (SvTRUE (ERRSV)) |
|
|
643 | LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV)); |
|
|
644 | |
|
|
645 | rv = count > 0 ? POPi : 0; |
|
|
646 | |
|
|
647 | PUTBACK; |
|
|
648 | FREETMPS; |
|
|
649 | LEAVE; |
|
|
650 | } |
|
|
651 | |
|
|
652 | return &rv; |
|
|
653 | } |
585 | } |
654 | |
586 | |
655 | extern "C" int cfperl_closePlugin () |
587 | extern "C" int cfperl_closePlugin () |
656 | { |
588 | { |
657 | return 0; |
589 | return 0; |
… | |
… | |
682 | |
614 | |
683 | exit (EXIT_FAILURE); |
615 | exit (EXIT_FAILURE); |
684 | //perl_destruct (perl); |
616 | //perl_destruct (perl); |
685 | //perl_free (perl); |
617 | //perl_free (perl); |
686 | //perl = 0; |
618 | //perl = 0; |
|
|
619 | //return; |
687 | } |
620 | } |
688 | else |
621 | |
689 | obj_cache = newHV (); |
622 | obj_cache = newHV (); |
690 | } |
623 | } |
691 | |
624 | |
692 | void cfperl_main () |
625 | void cfperl_main () |
693 | { |
626 | { |
694 | dSP; |
627 | dSP; |
… | |
… | |
696 | PUSHMARK (SP); |
629 | PUSHMARK (SP); |
697 | PUTBACK; |
630 | PUTBACK; |
698 | call_pv ("cf::main", G_DISCARD | G_VOID); |
631 | call_pv ("cf::main", G_DISCARD | G_VOID); |
699 | } |
632 | } |
700 | |
633 | |
701 | void cfperl_free_ob (object *op) |
634 | static event_klass klass_of[NUM_EVENT_TYPES] = { |
702 | { |
635 | # define def(type,name) KLASS_ ## type, |
703 | SV *sv = hv_delete (obj_cache, (char *)&op, sizeof (void *), 0); |
636 | # include "eventinc.h" |
|
|
637 | # undef def |
|
|
638 | }; |
704 | |
639 | |
705 | if (sv) |
640 | #define KLASS_OF(event) (((unsigned int)event) < NUM_EVENT_TYPES ? klass_of [event] : KLASS_NONE) |
706 | clearSVptr (sv); |
|
|
707 | } |
|
|
708 | |
641 | |
|
|
642 | static void |
|
|
643 | gather_callbacks (AV *&callbacks, AV *registry, event_type event) |
|
|
644 | { |
|
|
645 | // event must be in array |
|
|
646 | if (event >= 0 && event <= AvFILLp (registry)) |
|
|
647 | { |
|
|
648 | SV *cbs_ = AvARRAY (registry)[event]; |
|
|
649 | |
|
|
650 | // element must be list of callback entries |
|
|
651 | if (cbs_ && SvROK (cbs_) && SvTYPE (SvRV (cbs_)) == SVt_PVAV) |
|
|
652 | { |
|
|
653 | AV *cbs = (AV *)SvRV (cbs_); |
|
|
654 | |
|
|
655 | // no callback entries, no callbacks to call |
|
|
656 | if (AvFILLp (cbs) >= 0) |
|
|
657 | { |
|
|
658 | if (!callbacks) |
|
|
659 | { |
|
|
660 | callbacks = newAV (); |
|
|
661 | av_extend (callbacks, 16); |
|
|
662 | } |
|
|
663 | |
|
|
664 | // never use SvREFCNT_inc to copy values, but its ok here :) |
|
|
665 | for (int i = 0; i <= AvFILLp (cbs); ++i) |
|
|
666 | av_push (callbacks, SvREFCNT_inc (AvARRAY (cbs)[i])); |
|
|
667 | } |
|
|
668 | } |
|
|
669 | } |
|
|
670 | } |
|
|
671 | |
709 | int cfperl_invoke (event_type event, ...) |
672 | bool cfperl_invoke (event_type event, ...) |
710 | { |
673 | { |
|
|
674 | data_type dt; |
|
|
675 | va_list ap; |
|
|
676 | |
|
|
677 | va_start (ap, event); |
|
|
678 | |
|
|
679 | AV *callbacks = 0; |
|
|
680 | |
|
|
681 | object *op; |
|
|
682 | player *pl; |
|
|
683 | mapstruct *map; |
|
|
684 | |
|
|
685 | // callback call ordering is: |
|
|
686 | // 1. per-object callback (NYI) |
|
|
687 | // 2. per-class object |
|
|
688 | // 2a. per-type callback |
|
|
689 | // 4. global callbacks |
|
|
690 | |
|
|
691 | gather_callbacks (callbacks, cb_global, event); |
|
|
692 | |
|
|
693 | switch (KLASS_OF (event)) |
|
|
694 | { |
|
|
695 | case KLASS_GLOBAL: |
|
|
696 | break; |
|
|
697 | |
|
|
698 | case KLASS_OBJECT: |
|
|
699 | dt = (data_type) va_arg (ap, int); |
|
|
700 | assert (("first argument must be of type object", dt == DT_OBJECT)); |
|
|
701 | op = va_arg (ap, object *); |
|
|
702 | |
|
|
703 | if (op->cb) |
|
|
704 | gather_callbacks (callbacks, (AV *)op->cb, event); |
|
|
705 | |
|
|
706 | if (op->type) |
|
|
707 | { |
|
|
708 | if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type)) |
|
|
709 | { |
|
|
710 | SV *registry = AvARRAY (cb_type)[op->type + op->subtype * NUM_SUBTYPES]; |
|
|
711 | |
|
|
712 | if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV) |
|
|
713 | gather_callbacks (callbacks, (AV *)SvRV (registry), event); |
|
|
714 | } |
|
|
715 | |
|
|
716 | if (op->type <= AvFILLp (cb_type)) |
|
|
717 | { |
|
|
718 | SV *registry = AvARRAY (cb_type)[op->type]; |
|
|
719 | |
|
|
720 | if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV) |
|
|
721 | gather_callbacks (callbacks, (AV *)SvRV (registry), event); |
|
|
722 | } |
|
|
723 | } |
|
|
724 | |
|
|
725 | gather_callbacks (callbacks, cb_object, event); |
|
|
726 | |
|
|
727 | break; |
|
|
728 | |
|
|
729 | case KLASS_PLAYER: |
|
|
730 | dt = (data_type) va_arg (ap, int); |
|
|
731 | assert (("first argument must be of type player", dt == DT_PLAYER)); |
|
|
732 | pl = va_arg (ap, player *); |
|
|
733 | |
|
|
734 | if (pl->cb) |
|
|
735 | gather_callbacks (callbacks, (AV *)pl->cb, event); |
|
|
736 | |
|
|
737 | gather_callbacks (callbacks, cb_player, event); |
|
|
738 | break; |
|
|
739 | |
|
|
740 | case KLASS_MAP: |
|
|
741 | dt = (data_type) va_arg (ap, int); |
|
|
742 | assert (("first argument must be of type object", dt == DT_MAP)); |
|
|
743 | map = va_arg (ap, mapstruct *); |
|
|
744 | |
|
|
745 | if (map->cb) |
|
|
746 | gather_callbacks (callbacks, (AV *)map->cb, event); |
|
|
747 | |
|
|
748 | gather_callbacks (callbacks, cb_map, event); |
|
|
749 | break; |
|
|
750 | |
|
|
751 | default: |
|
|
752 | assert (("unsupported event klass in cfperl_invoke", 0)); |
|
|
753 | } |
|
|
754 | |
|
|
755 | // short-circuit processing if no callbacks found/defined |
|
|
756 | if (!callbacks) |
|
|
757 | return 0; |
|
|
758 | |
711 | dSP; |
759 | dSP; |
712 | va_list ap; |
|
|
713 | |
|
|
714 | ENTER; |
760 | ENTER; |
715 | SAVETMPS; |
761 | SAVETMPS; |
716 | |
762 | |
717 | va_start (ap, event); |
|
|
718 | PUSHMARK (SP); |
763 | PUSHMARK (SP); |
719 | EXTEND (SP, 2); |
764 | EXTEND (SP, 3); |
720 | PUSHs (sv_2mortal (newSViv (event))); |
|
|
721 | |
765 | |
722 | // todo, optimise here wether we actually want to invoke an event |
766 | PUSHs (sv_2mortal (newSViv (event))); // only used for debugging nowadays |
|
|
767 | PUSHs (sv_2mortal (newRV_noinc ((SV *)callbacks))); |
|
|
768 | |
|
|
769 | switch (KLASS_OF (event)) |
|
|
770 | { |
|
|
771 | case KLASS_OBJECT: PUSHs (sv_2mortal (newSVdt (DT_OBJECT, op))); break; |
|
|
772 | case KLASS_PLAYER: PUSHs (sv_2mortal (newSVdt (DT_PLAYER, pl))); break; |
|
|
773 | case KLASS_MAP: PUSHs (sv_2mortal (newSVdt (DT_MAP, map))); break; |
|
|
774 | } |
723 | |
775 | |
724 | for (;;) |
776 | for (;;) |
725 | { |
777 | { |
726 | data_type dt = (data_type) va_arg (ap, int); |
778 | dt = (data_type) va_arg (ap, int); |
727 | |
779 | |
728 | if (dt == DT_END) |
780 | if (dt == DT_END) |
729 | break; |
781 | break; |
730 | |
782 | |
|
|
783 | if (dt == DT_AV) |
|
|
784 | { |
|
|
785 | AV *av = va_arg (ap, AV *); |
|
|
786 | |
|
|
787 | for (int i = 0; i <= av_len (av); ++i) |
|
|
788 | XPUSHs (*av_fetch (av, i, 1)); |
|
|
789 | } |
|
|
790 | else |
731 | PUSHs (sv_2mortal (newSVdt_va (ap, dt))); |
791 | XPUSHs (sv_2mortal (newSVdt_va (ap, dt))); |
732 | } |
792 | } |
733 | |
793 | |
734 | va_end (ap); |
794 | va_end (ap); |
735 | |
795 | |
736 | PUTBACK; |
796 | PUTBACK; |
737 | int count = call_pv ("cf::invoke", G_SCALAR | G_EVAL); |
797 | int count = call_pv ("cf::do_invoke", G_SCALAR | G_EVAL); |
738 | SPAGAIN; |
798 | SPAGAIN; |
739 | |
799 | |
740 | count = count > 0 ? POPi : 0; |
800 | count = count > 0 ? POPi : 0; |
741 | |
801 | |
742 | FREETMPS; |
802 | FREETMPS; |
743 | LEAVE; |
803 | LEAVE; |
744 | |
804 | |
745 | return count; |
805 | return count; |
|
|
806 | } |
|
|
807 | |
|
|
808 | SV * |
|
|
809 | cfperl_result (int idx) |
|
|
810 | { |
|
|
811 | AV *av = get_av ("cfperl::invoke_results", 0); |
|
|
812 | if (!av) |
|
|
813 | return &PL_sv_undef; |
|
|
814 | |
|
|
815 | SV **sv = av_fetch (av, idx, 0); |
|
|
816 | if (!sv) |
|
|
817 | return &PL_sv_undef; |
|
|
818 | |
|
|
819 | return *sv; |
|
|
820 | } |
|
|
821 | |
|
|
822 | int |
|
|
823 | cfperl_result_INT (int idx) |
|
|
824 | { |
|
|
825 | return SvIV (cfperl_result (idx)); |
746 | } |
826 | } |
747 | |
827 | |
748 | MODULE = cf PACKAGE = cf PREFIX = cf_ |
828 | MODULE = cf PACKAGE = cf PREFIX = cf_ |
749 | |
829 | |
750 | BOOT: |
830 | BOOT: |
… | |
… | |
884 | const_iv (BUILDER) |
964 | const_iv (BUILDER) |
885 | const_iv (MATERIAL) |
965 | const_iv (MATERIAL) |
886 | const_iv (ITEM_TRANSFORMER) |
966 | const_iv (ITEM_TRANSFORMER) |
887 | const_iv (QUEST) |
967 | const_iv (QUEST) |
888 | |
968 | |
|
|
969 | const_iv (NUM_SUBTYPES) |
|
|
970 | |
889 | const_iv (ST_BD_BUILD) |
971 | const_iv (ST_BD_BUILD) |
890 | const_iv (ST_BD_REMOVE) |
972 | const_iv (ST_BD_REMOVE) |
891 | |
973 | |
892 | const_iv (ST_MAT_FLOOR) |
974 | const_iv (ST_MAT_FLOOR) |
893 | const_iv (ST_MAT_WALL) |
975 | const_iv (ST_MAT_WALL) |
… | |
… | |
1241 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
1323 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
1242 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
1324 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
1243 | |
1325 | |
1244 | static const struct { |
1326 | static const struct { |
1245 | const char *name; |
1327 | const char *name; |
|
|
1328 | int skip; |
1246 | IV klass; |
1329 | IV klass; |
1247 | IV iv; |
1330 | IV iv; |
1248 | } *eiv, event_iv[] = { |
1331 | } *eiv, event_iv[] = { |
1249 | # define def(klass,name) { # name, (IV)KLASS_ ## klass, (IV)EV_ ## klass ## name }, |
1332 | # define def(klass,name) { "EVENT_" # klass "_" # name, sizeof ("EVENT_" # klass), (IV)KLASS_ ## klass, (IV)EVENT_ ## klass ## _ ## name }, |
1250 | # include "eventinc.h" |
1333 | # include "eventinc.h" |
1251 | # undef def |
1334 | # undef def |
1252 | }; |
1335 | }; |
1253 | |
1336 | |
1254 | AV *av = get_av ("cf::EVENT", 1); |
1337 | AV *av = get_av ("cf::EVENT", 1); |
1255 | |
1338 | |
1256 | for (eiv = event_iv + sizeof (event_iv) / sizeof (event_iv [0]); eiv-- > event_iv; ) |
1339 | for (eiv = event_iv + sizeof (event_iv) / sizeof (event_iv [0]); eiv-- > event_iv; ) |
1257 | { |
1340 | { |
1258 | AV *event = newAV (); |
1341 | AV *event = newAV (); |
1259 | av_push (event, newSVpv ((char *)eiv->name, 0)); |
1342 | av_push (event, newSVpv ((char *)eiv->name + eiv->skip, 0)); |
1260 | av_push (event, newSViv (eiv->klass)); |
1343 | av_push (event, newSViv (eiv->klass)); |
1261 | av_store (av, eiv->iv, newRV_noinc ((SV *)event)); |
1344 | av_store (av, eiv->iv, newRV_noinc ((SV *)event)); |
|
|
1345 | newCONSTSUB (stash, (char *)eiv->name, newSViv (eiv->iv)); |
1262 | } |
1346 | } |
1263 | |
|
|
1264 | static const struct { |
|
|
1265 | const char *name; |
|
|
1266 | IV iv; |
|
|
1267 | } *event, event_list[] = { |
|
|
1268 | # define const_event(name) { # name, (IV)EVENT_ ## name }, |
|
|
1269 | const_event (NONE) |
|
|
1270 | const_event (APPLY) |
|
|
1271 | const_event (ATTACK) |
|
|
1272 | const_event (DEATH) |
|
|
1273 | const_event (DROP) |
|
|
1274 | const_event (DROP_ON) |
|
|
1275 | const_event (PICKUP) |
|
|
1276 | const_event (SAY) |
|
|
1277 | const_event (STOP) |
|
|
1278 | const_event (TIME) |
|
|
1279 | const_event (THROW) |
|
|
1280 | const_event (TRIGGER) |
|
|
1281 | const_event (CLOSE) |
|
|
1282 | const_event (TIMER) |
|
|
1283 | const_event (MOVE) |
|
|
1284 | |
|
|
1285 | //const_event (CLOCK) |
|
|
1286 | const_event (CRASH) |
|
|
1287 | const_event (GKILL) |
|
|
1288 | const_event (SHOUT) |
|
|
1289 | const_event (TELL) |
|
|
1290 | const_event (MUZZLE) |
|
|
1291 | const_event (KICK) |
|
|
1292 | const_event (PLAYER_USE_SKILL) |
|
|
1293 | const_event (MONSTER_USE_SKILL) |
|
|
1294 | const_event (CAST_SPELL) |
|
|
1295 | const_event (FIND_UNARMED_SKILL) |
|
|
1296 | const_event (EXTCMD) |
|
|
1297 | //const_event (FREE_OB) |
|
|
1298 | }; |
|
|
1299 | |
|
|
1300 | av = get_av ("cf::PLUGIN_EVENT", 1); |
|
|
1301 | |
|
|
1302 | for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; ) |
|
|
1303 | av_store (av, event->iv, newSVpv ((char *)event->name, 0)); |
|
|
1304 | |
1347 | |
1305 | static const struct { |
1348 | static const struct { |
1306 | int dtype; |
1349 | int dtype; |
1307 | const char *name; |
1350 | const char *name; |
1308 | IV idx; |
1351 | IV idx; |
… | |
… | |
1439 | } |
1482 | } |
1440 | |
1483 | |
1441 | //I_EVENT_API (PACKAGE); |
1484 | //I_EVENT_API (PACKAGE); |
1442 | } |
1485 | } |
1443 | |
1486 | |
|
|
1487 | void _reload_1 () |
|
|
1488 | CODE: |
|
|
1489 | cb_global = get_av ("cf::CB_GLOBAL", 1); |
|
|
1490 | cb_object = get_av ("cf::CB_OBJECT", 1); |
|
|
1491 | cb_player = get_av ("cf::CB_PLAYER", 1); |
|
|
1492 | cb_type = get_av ("cf::CB_TYPE" , 1); |
|
|
1493 | cb_map = get_av ("cf::CB_MAP" , 1); |
|
|
1494 | |
|
|
1495 | void _reload_2 () |
|
|
1496 | CODE: |
|
|
1497 | { |
|
|
1498 | // reattach to all attachable objects in the game. |
|
|
1499 | for (player *pl = first_player; pl; pl = pl->next) |
|
|
1500 | reattach (pl); |
|
|
1501 | |
|
|
1502 | for (mapstruct *map = first_map; map; map = map->next) |
|
|
1503 | reattach (map); |
|
|
1504 | |
|
|
1505 | for (object *op = objects; op; op = op->next) |
|
|
1506 | reattach (op); |
|
|
1507 | } |
|
|
1508 | |
1444 | NV floor (NV x) |
1509 | NV floor (NV x) |
1445 | |
1510 | |
1446 | NV ceil (NV x) |
1511 | NV ceil (NV x) |
1447 | |
1512 | |
1448 | void server_tick () |
1513 | void server_tick () |
… | |
… | |
1492 | |
1557 | |
1493 | int random_roll(int min, int max, object *op, int goodbad); |
1558 | int random_roll(int min, int max, object *op, int goodbad); |
1494 | |
1559 | |
1495 | const char *cost_string_from_value(uint64 cost, int approx = 0) |
1560 | const char *cost_string_from_value(uint64 cost, int approx = 0) |
1496 | |
1561 | |
|
|
1562 | int invoke (int event, ...) |
|
|
1563 | CODE: |
|
|
1564 | if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL"); |
|
|
1565 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 1; i < items; i++) av_push (av, ST (i)); |
|
|
1566 | RETVAL = INVOKE_((event_type)event, ARG_AV (av)); |
|
|
1567 | OUTPUT: RETVAL |
|
|
1568 | |
1497 | int |
1569 | int |
1498 | exp_to_level (val64 exp) |
1570 | exp_to_level (val64 exp) |
1499 | CODE: |
1571 | CODE: |
1500 | { |
1572 | { |
1501 | int i = 0; |
1573 | int i = 0; |
… | |
… | |
1532 | else |
1604 | else |
1533 | XSRETURN_UNDEF; |
1605 | XSRETURN_UNDEF; |
1534 | OUTPUT: RETVAL |
1606 | OUTPUT: RETVAL |
1535 | |
1607 | |
1536 | MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ |
1608 | MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ |
|
|
1609 | |
|
|
1610 | int invoke (object *op, int event, ...) |
|
|
1611 | CODE: |
|
|
1612 | if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT"); |
|
|
1613 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 2; i < items; i++) av_push (av, ST (i)); |
|
|
1614 | RETVAL = INVOKE_((event_type)event, ARG_OBJECT (op), ARG_AV (av)); |
|
|
1615 | OUTPUT: RETVAL |
|
|
1616 | |
|
|
1617 | SV *registry (object *op) |
|
|
1618 | CODE: |
|
|
1619 | RETVAL = registry_of (op); |
|
|
1620 | OUTPUT: |
|
|
1621 | RETVAL |
1537 | |
1622 | |
1538 | SV * |
1623 | SV * |
1539 | get_property (object *obj, int type, int idx) |
1624 | get_property (object *obj, int type, int idx) |
1540 | CODE: |
1625 | CODE: |
1541 | RETVAL = newSVcfapi (type, cf_object_get_property (obj, idx)); |
1626 | RETVAL = newSVcfapi (type, cf_object_get_property (obj, idx)); |
… | |
… | |
1818 | |
1903 | |
1819 | void cf_player_set_party (object *op, partylist *party) |
1904 | void cf_player_set_party (object *op, partylist *party) |
1820 | |
1905 | |
1821 | void kill_player (object *op) |
1906 | void kill_player (object *op) |
1822 | |
1907 | |
1823 | MODULE = cf PACKAGE = cf::object::map PREFIX = cf_ |
|
|
1824 | |
|
|
1825 | MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ |
1908 | MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ |
|
|
1909 | |
|
|
1910 | int invoke (player *pl, int event, ...) |
|
|
1911 | CODE: |
|
|
1912 | if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER"); |
|
|
1913 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 2; i < items; i++) av_push (av, ST (i)); |
|
|
1914 | RETVAL = INVOKE_((event_type)event, ARG_PLAYER (pl), ARG_AV (av)); |
|
|
1915 | OUTPUT: RETVAL |
|
|
1916 | |
|
|
1917 | SV *registry (player *pl) |
|
|
1918 | CODE: |
|
|
1919 | RETVAL = registry_of (pl); |
|
|
1920 | OUTPUT: |
|
|
1921 | RETVAL |
1826 | |
1922 | |
1827 | player *cf_player_find (char *name) |
1923 | player *cf_player_find (char *name) |
1828 | PROTOTYPE: $ |
1924 | PROTOTYPE: $ |
1829 | |
1925 | |
1830 | void cf_player_move (player *pl, int dir) |
1926 | void cf_player_move (player *pl, int dir) |
… | |
… | |
1948 | RETVAL = &pl->last_stats; |
2044 | RETVAL = &pl->last_stats; |
1949 | OUTPUT: RETVAL |
2045 | OUTPUT: RETVAL |
1950 | |
2046 | |
1951 | |
2047 | |
1952 | MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ |
2048 | MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ |
|
|
2049 | |
|
|
2050 | int invoke (mapstruct *map, int event, ...) |
|
|
2051 | CODE: |
|
|
2052 | if (KLASS_OF (event) != KLASS_MAP) croak ("event class must be MAP"); |
|
|
2053 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 2; i < items; i++) av_push (av, ST (i)); |
|
|
2054 | RETVAL = INVOKE_((event_type)event, ARG_MAP (map), ARG_AV (av)); |
|
|
2055 | OUTPUT: |
|
|
2056 | RETVAL |
|
|
2057 | |
|
|
2058 | SV *registry (mapstruct *map) |
|
|
2059 | CODE: |
|
|
2060 | RETVAL = registry_of (map); |
|
|
2061 | OUTPUT: |
|
|
2062 | RETVAL |
1953 | |
2063 | |
1954 | SV * |
2064 | SV * |
1955 | get_property (mapstruct *obj, int type, int idx) |
2065 | get_property (mapstruct *obj, int type, int idx) |
1956 | CODE: |
2066 | CODE: |
1957 | RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx)); |
2067 | RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx)); |
… | |
… | |
2101 | case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; |
2211 | case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; |
2102 | } |
2212 | } |
2103 | OUTPUT: |
2213 | OUTPUT: |
2104 | RETVAL |
2214 | RETVAL |
2105 | |
2215 | |
2106 | # "serialise" map perl data into a ref |
2216 | # "deserialise" perl map data into the map # TODO# compatibility cruft, remove |
2107 | void |
|
|
2108 | _get_obs (mapstruct *map) |
|
|
2109 | PPCODE: |
|
|
2110 | { |
|
|
2111 | object *o; |
|
|
2112 | int x, y; |
|
|
2113 | AV *obs = newAV (); |
|
|
2114 | int nonnull = 0; |
|
|
2115 | |
|
|
2116 | for (y = 0; y < MAP_HEIGHT (map); y++) |
|
|
2117 | for (x = 0; x < MAP_WIDTH (map); x++) |
|
|
2118 | { |
|
|
2119 | AV *av = newAV (); |
|
|
2120 | |
|
|
2121 | for (o = GET_MAP_OB (map, x, y); o; o = o->above) |
|
|
2122 | { |
|
|
2123 | SV *sv = SVptr_cache_get (o); |
|
|
2124 | |
|
|
2125 | if (sv && HvFILL (SvRV (sv))) |
|
|
2126 | { |
|
|
2127 | nonnull = 1; |
|
|
2128 | sv = newSVsv (sv); |
|
|
2129 | } |
|
|
2130 | else |
|
|
2131 | sv = &PL_sv_undef; |
|
|
2132 | |
|
|
2133 | av_push (av, sv); |
|
|
2134 | } |
|
|
2135 | |
|
|
2136 | av_store (obs, x + y * MAP_HEIGHT (map), newRV_noinc ((SV *)av)); |
|
|
2137 | } |
|
|
2138 | |
|
|
2139 | if (nonnull) |
|
|
2140 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)obs))); |
|
|
2141 | else |
|
|
2142 | SvREFCNT_dec (obs); |
|
|
2143 | } |
|
|
2144 | |
|
|
2145 | # "deserialise" perl map data into the map |
|
|
2146 | void |
2217 | void |
2147 | _set_obs (mapstruct *map, SV *sv) |
2218 | _set_obs (mapstruct *map, SV *sv) |
2148 | CODE: |
2219 | CODE: |
2149 | { |
2220 | { |
2150 | object *o; |
2221 | object *o; |