… | |
… | |
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 | |
… | |
… | |
99 | #define PUSH_PV PUSHcfapi_va(STRING, const char *) |
96 | #define PUSH_PV PUSHcfapi_va(STRING, const char *) |
100 | #define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int)))) |
97 | #define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int)))) |
101 | |
98 | |
102 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
99 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
103 | |
100 | |
104 | // garbage collect some perl objects, if possible |
|
|
105 | // all objects no longer referenced and empty are |
|
|
106 | // eligible for destruction. |
|
|
107 | static void |
|
|
108 | clean_obj_cache () |
|
|
109 | { |
|
|
110 | static int count; |
|
|
111 | |
|
|
112 | if (++count & 7) |
|
|
113 | return; |
|
|
114 | |
|
|
115 | int todo = 1000; |
|
|
116 | do |
|
|
117 | { |
|
|
118 | I32 klen; |
|
|
119 | char *key; |
|
|
120 | HE *he = hv_iternext (obj_cache); |
|
|
121 | |
|
|
122 | if (he) |
|
|
123 | { |
|
|
124 | SV *sv = hv_iterval (obj_cache, he); |
|
|
125 | |
|
|
126 | // empty and unreferenced? nuke it |
|
|
127 | if (SvREFCNT (sv) == 1 && SvREFCNT (SvRV (sv)) == 1 && !HvFILL ((HV *)(SvRV (sv)))) |
|
|
128 | { |
|
|
129 | hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD); |
|
|
130 | todo++; |
|
|
131 | } |
|
|
132 | } |
|
|
133 | else |
|
|
134 | break; |
|
|
135 | } |
|
|
136 | while (--todo); |
|
|
137 | } |
|
|
138 | |
|
|
139 | static SV * |
101 | static SV * |
140 | newSVptr (void *ptr, const char *klass) |
102 | newSVptr (void *ptr, const char *klass, HV *hv = newHV ()) |
141 | { |
103 | { |
142 | SV *sv; |
104 | SV *sv; |
143 | |
105 | |
144 | if (!ptr) |
106 | if (!ptr) |
145 | return &PL_sv_undef; |
107 | return &PL_sv_undef; |
146 | |
108 | |
147 | sv = (SV *)newHV (); |
|
|
148 | sv_magic (sv, 0, PERL_MAGIC_ext, (char *)ptr, 0); |
109 | sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); |
149 | 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)); |
150 | } |
124 | } |
151 | |
125 | |
152 | static void |
126 | static void |
153 | SVptr_cache_set (void *ptr, SV *sv) |
127 | SVptr_cache_set (void *ptr, SV *sv) |
154 | { |
128 | { |
… | |
… | |
223 | { |
197 | { |
224 | SV *sv; |
198 | SV *sv; |
225 | |
199 | |
226 | switch (type) |
200 | switch (type) |
227 | { |
201 | { |
228 | // first three exist only for cfapi-compatibility |
|
|
229 | case DT_INT_PTR: |
|
|
230 | sv = newSViv (*va_arg (ap, int *)); |
|
|
231 | break; |
|
|
232 | |
|
|
233 | case DT_INT64_PTR: |
|
|
234 | sv = newSVval64 ((val64)*va_arg (ap, sint64 *)); |
|
|
235 | break; |
|
|
236 | |
|
|
237 | case DT_DOUBLE_PTR: |
|
|
238 | sv = newSVnv (*va_arg (ap, double *)); |
|
|
239 | break; |
|
|
240 | |
|
|
241 | case DT_INT: |
202 | case DT_INT: |
242 | sv = newSViv (va_arg (ap, int)); |
203 | sv = newSViv (va_arg (ap, int)); |
243 | break; |
204 | break; |
244 | |
205 | |
245 | case DT_INT64: |
206 | case DT_INT64: |
… | |
… | |
266 | break; |
227 | break; |
267 | |
228 | |
268 | case DT_OBJECT: |
229 | case DT_OBJECT: |
269 | { |
230 | { |
270 | object *obj = va_arg (ap, object *); |
231 | object *obj = va_arg (ap, object *); |
271 | |
232 | sv = newSVattachable (obj, obj && obj->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap"); |
272 | if (!obj) |
|
|
273 | sv = &PL_sv_undef; |
|
|
274 | else |
|
|
275 | { |
|
|
276 | if (!obj->self) |
|
|
277 | switch (obj->type) |
|
|
278 | { |
|
|
279 | case MAP: |
|
|
280 | obj->self = static_cast<void *>(newSVptr (obj, "cf::object::map::wrap")); |
|
|
281 | break; |
|
|
282 | |
|
|
283 | case PLAYER: |
|
|
284 | obj->self = static_cast<void *>(newSVptr (obj, "cf::object::player::wrap")); |
|
|
285 | break; |
|
|
286 | |
|
|
287 | default: |
|
|
288 | obj->self = static_cast<void *>(newSVptr (obj, "cf::object::wrap")); |
|
|
289 | break; |
|
|
290 | } |
|
|
291 | |
|
|
292 | sv = newSVsv (static_cast<SV *>(obj->self)); |
|
|
293 | } |
|
|
294 | } |
233 | } |
295 | break; |
234 | break; |
296 | |
235 | |
297 | case DT_MAP: |
236 | case DT_MAP: |
|
|
237 | // va_arg (object *) when void * is passed is an XSI extension |
298 | sv = newSVptr (va_arg (ap, mapstruct *), "cf::map::wrap"); |
238 | sv = newSVattachable (va_arg (ap, mapstruct *), "cf::map::wrap"); |
299 | break; |
239 | break; |
300 | |
240 | |
301 | case DT_PLAYER: |
241 | case DT_PLAYER: |
302 | sv = newSVptr (va_arg (ap, player *), "cf::player::wrap"); |
242 | sv = newSVattachable (va_arg (ap, player *), "cf::player::wrap"); |
303 | break; |
243 | break; |
304 | |
244 | |
305 | case DT_ARCH: |
245 | case DT_ARCH: |
306 | sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap"); |
246 | sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap"); |
307 | break; |
247 | break; |
… | |
… | |
341 | va_list ap; |
281 | va_list ap; |
342 | va_start (ap, type); |
282 | va_start (ap, type); |
343 | |
283 | |
344 | switch (type) |
284 | switch (type) |
345 | { |
285 | { |
346 | case CFAPI_INT: sv = newSVdt_va (ap, DT_INT_PTR ); break; |
286 | case CFAPI_INT: sv = newSViv (*va_arg (ap, int * )); break; |
347 | case CFAPI_LONG: sv = newSVdt_va (ap, DT_INT64_PTR ); break; |
287 | case CFAPI_LONG: sv = newSVval64 (*va_arg (ap, sint64 *)); break; |
348 | case CFAPI_DOUBLE: sv = newSVdt_va (ap, DT_DOUBLE_PTR); break; |
288 | case CFAPI_DOUBLE: sv = newSVnv (*va_arg (ap, double *)); break; |
349 | case CFAPI_STRING: sv = newSVdt_va (ap, DT_STRING); break; |
289 | case CFAPI_STRING: sv = newSVdt_va (ap, DT_STRING); break; |
350 | case CFAPI_POBJECT: sv = newSVdt_va (ap, DT_OBJECT); break; |
290 | case CFAPI_POBJECT: sv = newSVdt_va (ap, DT_OBJECT); break; |
351 | case CFAPI_PMAP: sv = newSVdt_va (ap, DT_MAP ); break; |
291 | case CFAPI_PMAP: sv = newSVdt_va (ap, DT_MAP ); break; |
352 | case CFAPI_PPLAYER: sv = newSVdt_va (ap, DT_PLAYER); break; |
292 | case CFAPI_PPLAYER: sv = newSVdt_va (ap, DT_PLAYER); break; |
353 | case CFAPI_PARCH: sv = newSVdt_va (ap, DT_ARCH ); break; |
293 | case CFAPI_PARCH: sv = newSVdt_va (ap, DT_ARCH ); break; |
… | |
… | |
361 | va_end (ap); |
301 | va_end (ap); |
362 | |
302 | |
363 | return sv; |
303 | return sv; |
364 | } |
304 | } |
365 | |
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 | |
366 | ///////////////////////////////////////////////////////////////////////////// |
365 | ///////////////////////////////////////////////////////////////////////////// |
367 | |
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 | |
368 | 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) |
369 | { |
485 | { |
370 | gethook = gethooksptr; |
486 | gethook = gethooksptr; |
371 | printf (PLUGIN_VERSION " init\n"); |
|
|
372 | |
487 | |
373 | return 0; |
488 | return 0; |
374 | } |
489 | } |
375 | |
490 | |
376 | static CommArray_s rtn_cmd; |
491 | static CommArray_s rtn_cmd; |
… | |
… | |
457 | extern "C" int cfperl_postInitPlugin () |
572 | extern "C" int cfperl_postInitPlugin () |
458 | { |
573 | { |
459 | int hooktype = 1; |
574 | int hooktype = 1; |
460 | int rtype = 0; |
575 | int rtype = 0; |
461 | |
576 | |
462 | printf (PLUGIN_VERSION " post init\n"); |
|
|
463 | |
|
|
464 | registerGlobalEvent = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_register_global_event"); |
|
|
465 | unregisterGlobalEvent = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_unregister_global_event"); |
|
|
466 | systemDirectory = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_directory"); |
577 | systemDirectory = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_directory"); |
467 | 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"); |
468 | 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"); |
469 | object_insert = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_insert"); |
580 | object_insert = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_insert"); |
470 | |
581 | |
471 | cf_init_plugin (gethook); |
582 | cf_init_plugin (gethook); |
472 | |
583 | |
473 | /* Pick the global events you want to monitor from this plugin */ |
|
|
474 | registerGlobalEvent (NULL, EVENT_CLOCK, PLUGIN_NAME, globalEventListener); |
|
|
475 | //registerGlobalEvent (NULL, EVENT_CRASH, PLUGIN_NAME, globalEventListener); |
|
|
476 | registerGlobalEvent (NULL, EVENT_FIND_UNARMED_SKILL, PLUGIN_NAME, globalEventListener); |
|
|
477 | registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener); |
|
|
478 | registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener); |
|
|
479 | registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener); |
|
|
480 | registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener); |
|
|
481 | |
|
|
482 | return 0; |
584 | return 0; |
483 | } |
|
|
484 | |
|
|
485 | static void * |
|
|
486 | globalEventListener (int *type, ...) |
|
|
487 | { |
|
|
488 | va_list args; |
|
|
489 | static int rv; |
|
|
490 | int event_code; |
|
|
491 | |
|
|
492 | if (!perl) |
|
|
493 | return NULL; |
|
|
494 | |
|
|
495 | va_start (args, type); |
|
|
496 | event_code = va_arg (args, int); |
|
|
497 | |
|
|
498 | if (event_code == EVENT_CLOCK) |
|
|
499 | { |
|
|
500 | clean_obj_cache (); |
|
|
501 | } |
|
|
502 | else |
|
|
503 | { |
|
|
504 | dSP; |
|
|
505 | |
|
|
506 | ENTER; |
|
|
507 | SAVETMPS; |
|
|
508 | |
|
|
509 | PUSHMARK (SP); |
|
|
510 | |
|
|
511 | EXTEND (SP, 10); |
|
|
512 | PUSHs (sv_2mortal (newSViv (event_code))); |
|
|
513 | |
|
|
514 | switch (event_code) |
|
|
515 | { |
|
|
516 | case EVENT_CRASH: |
|
|
517 | break; |
|
|
518 | |
|
|
519 | case EVENT_SHOUT: |
|
|
520 | case EVENT_MUZZLE: |
|
|
521 | case EVENT_KICK: |
|
|
522 | PUSH_OB; |
|
|
523 | PUSH_PV; |
|
|
524 | break; |
|
|
525 | |
|
|
526 | case EVENT_FIND_UNARMED_SKILL: |
|
|
527 | PUSH_OB; |
|
|
528 | break; |
|
|
529 | |
|
|
530 | case EVENT_TELL: |
|
|
531 | break; |
|
|
532 | } |
|
|
533 | |
|
|
534 | va_end (args); |
|
|
535 | |
|
|
536 | PUTBACK; |
|
|
537 | int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL); |
|
|
538 | SPAGAIN; |
|
|
539 | |
|
|
540 | if (SvTRUE (ERRSV)) |
|
|
541 | LOG (llevError, "global event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV)); |
|
|
542 | |
|
|
543 | rv = count > 0 ? POPi : 0; |
|
|
544 | |
|
|
545 | PUTBACK; |
|
|
546 | FREETMPS; |
|
|
547 | LEAVE; |
|
|
548 | } |
|
|
549 | |
|
|
550 | return &rv; |
|
|
551 | } |
|
|
552 | |
|
|
553 | extern "C" void *cfperl_eventListener (int *type, ...) |
|
|
554 | { |
|
|
555 | static int rv; |
|
|
556 | va_list args; |
|
|
557 | int event_code; |
|
|
558 | object *who, *activator, *third, *event; |
|
|
559 | char *message, *extension, *options; |
|
|
560 | |
|
|
561 | if (!perl) |
|
|
562 | return NULL; |
|
|
563 | |
|
|
564 | va_start (args, type); |
|
|
565 | who = va_arg (args, object *); |
|
|
566 | event_code = va_arg (args, int); |
|
|
567 | activator = va_arg (args, object *); |
|
|
568 | third = va_arg (args, object *); |
|
|
569 | message = va_arg (args, char *); |
|
|
570 | va_arg (args, int); // fix yourself |
|
|
571 | extension = va_arg (args, char *); |
|
|
572 | options = va_arg (args, char *); |
|
|
573 | event = va_arg (args, object *); |
|
|
574 | va_end (args); |
|
|
575 | |
|
|
576 | { |
|
|
577 | dSP; |
|
|
578 | |
|
|
579 | ENTER; |
|
|
580 | SAVETMPS; |
|
|
581 | |
|
|
582 | PUSHMARK (SP); |
|
|
583 | EXTEND (SP, 10); |
|
|
584 | |
|
|
585 | PUSHcfapi (STRING, extension); |
|
|
586 | PUSHs (sv_2mortal (newSViv (event_code))); |
|
|
587 | |
|
|
588 | PUSHcfapi (POBJECT, event); |
|
|
589 | PUSHcfapi (POBJECT, who); |
|
|
590 | |
|
|
591 | switch (event_code) |
|
|
592 | { |
|
|
593 | case EVENT_STOP: // $ob (e.g. arrow) |
|
|
594 | case EVENT_TIME: // $ob |
|
|
595 | case EVENT_TIMER: // $ob |
|
|
596 | break; |
|
|
597 | |
|
|
598 | case EVENT_APPLY: // $ob, $who |
|
|
599 | case EVENT_DROP: // $ob, $who |
|
|
600 | case EVENT_CLOSE: // $ob, $who |
|
|
601 | case EVENT_MOVE: // $ob, $enemy |
|
|
602 | case EVENT_THROW: // $ob, $thrower |
|
|
603 | PUSHcfapi (POBJECT, activator); |
|
|
604 | break; |
|
|
605 | |
|
|
606 | case EVENT_DROP_ON: // $ob, $who, $what |
|
|
607 | case EVENT_ATTACK: // $ob, $who, $victim (?? please god enlighten me) |
|
|
608 | PUSHcfapi (POBJECT, activator); |
|
|
609 | PUSHcfapi (POBJECT, third); |
|
|
610 | break; |
|
|
611 | |
|
|
612 | case EVENT_TRIGGER: // $ob, $originator, [$victim], [$msg] |
|
|
613 | PUSHcfapi (POBJECT, activator); |
|
|
614 | PUSHcfapi (POBJECT, third); |
|
|
615 | PUSHcfapi (POBJECT, message); |
|
|
616 | break; |
|
|
617 | |
|
|
618 | case EVENT_SAY: // $ob, $who, $msg |
|
|
619 | PUSHcfapi (POBJECT, activator); |
|
|
620 | PUSHcfapi (STRING, message); |
|
|
621 | break; |
|
|
622 | |
|
|
623 | default: |
|
|
624 | LOG (llevError, "perl plugin called for unsupported event type %d", event_code); |
|
|
625 | break; |
|
|
626 | } |
|
|
627 | |
|
|
628 | PUTBACK; |
|
|
629 | int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL); |
|
|
630 | SPAGAIN; |
|
|
631 | |
|
|
632 | if (SvTRUE (ERRSV)) |
|
|
633 | LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV)); |
|
|
634 | |
|
|
635 | rv = count > 0 ? POPi : 0; |
|
|
636 | |
|
|
637 | PUTBACK; |
|
|
638 | FREETMPS; |
|
|
639 | LEAVE; |
|
|
640 | } |
|
|
641 | |
|
|
642 | return &rv; |
|
|
643 | } |
585 | } |
644 | |
586 | |
645 | extern "C" int cfperl_closePlugin () |
587 | extern "C" int cfperl_closePlugin () |
646 | { |
588 | { |
647 | return 0; |
589 | return 0; |
… | |
… | |
687 | PUSHMARK (SP); |
629 | PUSHMARK (SP); |
688 | PUTBACK; |
630 | PUTBACK; |
689 | call_pv ("cf::main", G_DISCARD | G_VOID); |
631 | call_pv ("cf::main", G_DISCARD | G_VOID); |
690 | } |
632 | } |
691 | |
633 | |
692 | void cfperl_free_ob (object *op) |
|
|
693 | { |
|
|
694 | if (op->self) fprintf (stderr, "free_ob_self %p %s\n", op, op->name);//D |
|
|
695 | if (op->self) SvREFCNT_dec (op->self), op->self = 0; |
|
|
696 | if (op->cb) SvREFCNT_dec (op->cb), op->cb = 0; |
|
|
697 | } |
|
|
698 | |
|
|
699 | static event_klass klass_of[NUM_EVENT_TYPES] = { |
634 | static event_klass klass_of[NUM_EVENT_TYPES] = { |
700 | # define def(type,name) KLASS_ ## type, |
635 | # define def(type,name) KLASS_ ## type, |
701 | # include "eventinc.h" |
636 | # include "eventinc.h" |
702 | # undef def |
637 | # undef def |
703 | }; |
638 | }; |
|
|
639 | |
|
|
640 | #define KLASS_OF(event) (((unsigned int)event) < NUM_EVENT_TYPES ? klass_of [event] : KLASS_NONE) |
704 | |
641 | |
705 | static void |
642 | static void |
706 | gather_callbacks (AV *&callbacks, AV *registry, event_type event) |
643 | gather_callbacks (AV *&callbacks, AV *registry, event_type event) |
707 | { |
644 | { |
708 | // event must be in array |
645 | // event must be in array |
… | |
… | |
751 | // 2a. per-type callback |
688 | // 2a. per-type callback |
752 | // 4. global callbacks |
689 | // 4. global callbacks |
753 | |
690 | |
754 | gather_callbacks (callbacks, cb_global, event); |
691 | gather_callbacks (callbacks, cb_global, event); |
755 | |
692 | |
756 | switch (klass_of [event]) |
693 | switch (KLASS_OF (event)) |
757 | { |
694 | { |
758 | case KLASS_GLOBAL: |
695 | case KLASS_GLOBAL: |
759 | break; |
696 | break; |
760 | |
697 | |
761 | case KLASS_OBJECT: |
698 | case KLASS_OBJECT: |
762 | dt = (data_type) va_arg (ap, int); |
699 | dt = (data_type) va_arg (ap, int); |
763 | assert (("first argument must be of type object", dt == DT_OBJECT)); |
700 | assert (("first argument must be of type object", dt == DT_OBJECT)); |
764 | |
|
|
765 | op = va_arg (ap, object *); |
701 | op = va_arg (ap, object *); |
766 | |
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 | |
767 | gather_callbacks (callbacks, cb_object, event); |
725 | gather_callbacks (callbacks, cb_object, event); |
768 | //TODO: per-object |
|
|
769 | |
|
|
770 | if (op->type > 0 && op->type <= AvFILLp (cb_type)) |
|
|
771 | { |
|
|
772 | SV *registry = AvARRAY (cb_type)[op->type]; |
|
|
773 | |
|
|
774 | if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV) |
|
|
775 | gather_callbacks (callbacks, (AV *)SvRV (registry), event); |
|
|
776 | } |
|
|
777 | |
726 | |
778 | break; |
727 | break; |
779 | |
728 | |
780 | case KLASS_PLAYER: |
729 | case KLASS_PLAYER: |
781 | dt = (data_type) va_arg (ap, int); |
730 | dt = (data_type) va_arg (ap, int); |
782 | assert (("first argument must be of type player", dt == DT_PLAYER)); |
731 | assert (("first argument must be of type player", dt == DT_PLAYER)); |
783 | |
|
|
784 | pl = va_arg (ap, player *); |
732 | pl = va_arg (ap, player *); |
|
|
733 | |
|
|
734 | if (pl->cb) |
|
|
735 | gather_callbacks (callbacks, (AV *)pl->cb, event); |
|
|
736 | |
785 | gather_callbacks (callbacks, cb_player, event); |
737 | gather_callbacks (callbacks, cb_player, event); |
786 | break; |
738 | break; |
787 | |
739 | |
788 | case KLASS_MAP: |
740 | case KLASS_MAP: |
789 | dt = (data_type) va_arg (ap, int); |
741 | dt = (data_type) va_arg (ap, int); |
790 | assert (("first argument must be of type object", dt == DT_MAP)); |
742 | assert (("first argument must be of type object", dt == DT_MAP)); |
791 | |
|
|
792 | map = va_arg (ap, mapstruct *); |
743 | map = va_arg (ap, mapstruct *); |
|
|
744 | |
|
|
745 | if (map->cb) |
|
|
746 | gather_callbacks (callbacks, (AV *)map->cb, event); |
|
|
747 | |
793 | gather_callbacks (callbacks, cb_map, event); |
748 | gather_callbacks (callbacks, cb_map, event); |
794 | break; |
749 | break; |
795 | |
750 | |
796 | default: |
751 | default: |
797 | assert (("unsupported event klass in cfperl_invoke", 0)); |
752 | assert (("unsupported event klass in cfperl_invoke", 0)); |
… | |
… | |
809 | EXTEND (SP, 3); |
764 | EXTEND (SP, 3); |
810 | |
765 | |
811 | PUSHs (sv_2mortal (newSViv (event))); // only used for debugging nowadays |
766 | PUSHs (sv_2mortal (newSViv (event))); // only used for debugging nowadays |
812 | PUSHs (sv_2mortal (newRV_noinc ((SV *)callbacks))); |
767 | PUSHs (sv_2mortal (newRV_noinc ((SV *)callbacks))); |
813 | |
768 | |
814 | switch (klass_of [event]) |
769 | switch (KLASS_OF (event)) |
815 | { |
770 | { |
816 | case KLASS_OBJECT: PUSHs (sv_2mortal (newSVdt (DT_OBJECT, op))); break; |
771 | case KLASS_OBJECT: PUSHs (sv_2mortal (newSVdt (DT_OBJECT, op))); break; |
817 | case KLASS_PLAYER: PUSHs (sv_2mortal (newSVdt (DT_PLAYER, pl))); break; |
772 | case KLASS_PLAYER: PUSHs (sv_2mortal (newSVdt (DT_PLAYER, pl))); break; |
818 | case KLASS_MAP: PUSHs (sv_2mortal (newSVdt (DT_MAP, map))); break; |
773 | case KLASS_MAP: PUSHs (sv_2mortal (newSVdt (DT_MAP, map))); break; |
819 | } |
774 | } |
… | |
… | |
823 | dt = (data_type) va_arg (ap, int); |
778 | dt = (data_type) va_arg (ap, int); |
824 | |
779 | |
825 | if (dt == DT_END) |
780 | if (dt == DT_END) |
826 | break; |
781 | break; |
827 | |
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 |
828 | XPUSHs (sv_2mortal (newSVdt_va (ap, dt))); |
791 | XPUSHs (sv_2mortal (newSVdt_va (ap, dt))); |
829 | } |
792 | } |
830 | |
793 | |
831 | va_end (ap); |
794 | va_end (ap); |
832 | |
795 | |
833 | PUTBACK; |
796 | PUTBACK; |
834 | int count = call_pv ("cf::invoke", G_SCALAR | G_EVAL); |
797 | int count = call_pv ("cf::do_invoke", G_SCALAR | G_EVAL); |
835 | SPAGAIN; |
798 | SPAGAIN; |
836 | |
799 | |
837 | count = count > 0 ? POPi : 0; |
800 | count = count > 0 ? POPi : 0; |
838 | |
801 | |
839 | FREETMPS; |
802 | FREETMPS; |
840 | LEAVE; |
803 | LEAVE; |
841 | |
804 | |
842 | 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)); |
843 | } |
826 | } |
844 | |
827 | |
845 | MODULE = cf PACKAGE = cf PREFIX = cf_ |
828 | MODULE = cf PACKAGE = cf PREFIX = cf_ |
846 | |
829 | |
847 | BOOT: |
830 | BOOT: |
… | |
… | |
981 | const_iv (BUILDER) |
964 | const_iv (BUILDER) |
982 | const_iv (MATERIAL) |
965 | const_iv (MATERIAL) |
983 | const_iv (ITEM_TRANSFORMER) |
966 | const_iv (ITEM_TRANSFORMER) |
984 | const_iv (QUEST) |
967 | const_iv (QUEST) |
985 | |
968 | |
|
|
969 | const_iv (NUM_SUBTYPES) |
|
|
970 | |
986 | const_iv (ST_BD_BUILD) |
971 | const_iv (ST_BD_BUILD) |
987 | const_iv (ST_BD_REMOVE) |
972 | const_iv (ST_BD_REMOVE) |
988 | |
973 | |
989 | const_iv (ST_MAT_FLOOR) |
974 | const_iv (ST_MAT_FLOOR) |
990 | const_iv (ST_MAT_WALL) |
975 | const_iv (ST_MAT_WALL) |
… | |
… | |
1338 | 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; ) |
1339 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
1324 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
1340 | |
1325 | |
1341 | static const struct { |
1326 | static const struct { |
1342 | const char *name; |
1327 | const char *name; |
|
|
1328 | int skip; |
1343 | IV klass; |
1329 | IV klass; |
1344 | IV iv; |
1330 | IV iv; |
1345 | } *eiv, event_iv[] = { |
1331 | } *eiv, event_iv[] = { |
1346 | # 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 }, |
1347 | # include "eventinc.h" |
1333 | # include "eventinc.h" |
1348 | # undef def |
1334 | # undef def |
1349 | }; |
1335 | }; |
1350 | |
1336 | |
1351 | AV *av = get_av ("cf::EVENT", 1); |
1337 | AV *av = get_av ("cf::EVENT", 1); |
1352 | |
1338 | |
1353 | 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; ) |
1354 | { |
1340 | { |
1355 | AV *event = newAV (); |
1341 | AV *event = newAV (); |
1356 | av_push (event, newSVpv ((char *)eiv->name, 0)); |
1342 | av_push (event, newSVpv ((char *)eiv->name + eiv->skip, 0)); |
1357 | av_push (event, newSViv (eiv->klass)); |
1343 | av_push (event, newSViv (eiv->klass)); |
1358 | 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)); |
1359 | } |
1346 | } |
1360 | |
|
|
1361 | static const struct { |
|
|
1362 | const char *name; |
|
|
1363 | IV iv; |
|
|
1364 | } *event, event_list[] = { |
|
|
1365 | # define const_event(name) { # name, (IV)EVENT_ ## name }, |
|
|
1366 | const_event (NONE) |
|
|
1367 | const_event (APPLY) |
|
|
1368 | const_event (ATTACK) |
|
|
1369 | const_event (DROP) |
|
|
1370 | const_event (DROP_ON) |
|
|
1371 | const_event (PICKUP) |
|
|
1372 | const_event (SAY) |
|
|
1373 | const_event (STOP) |
|
|
1374 | const_event (TIME) |
|
|
1375 | const_event (THROW) |
|
|
1376 | const_event (TRIGGER) |
|
|
1377 | const_event (CLOSE) |
|
|
1378 | const_event (TIMER) |
|
|
1379 | const_event (MOVE) |
|
|
1380 | |
|
|
1381 | //const_event (CLOCK) |
|
|
1382 | const_event (CRASH) |
|
|
1383 | const_event (SHOUT) |
|
|
1384 | const_event (TELL) |
|
|
1385 | const_event (MUZZLE) |
|
|
1386 | const_event (KICK) |
|
|
1387 | const_event (FIND_UNARMED_SKILL) |
|
|
1388 | //const_event (FREE_OB) |
|
|
1389 | }; |
|
|
1390 | |
|
|
1391 | av = get_av ("cf::PLUGIN_EVENT", 1); |
|
|
1392 | |
|
|
1393 | for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; ) |
|
|
1394 | av_store (av, event->iv, newSVpv ((char *)event->name, 0)); |
|
|
1395 | |
1347 | |
1396 | static const struct { |
1348 | static const struct { |
1397 | int dtype; |
1349 | int dtype; |
1398 | const char *name; |
1350 | const char *name; |
1399 | IV idx; |
1351 | IV idx; |
… | |
… | |
1527 | { |
1479 | { |
1528 | hv_store (prop_type, cprop->name, strlen (cprop->name), newSViv (cprop->dtype), 0); |
1480 | hv_store (prop_type, cprop->name, strlen (cprop->name), newSViv (cprop->dtype), 0); |
1529 | hv_store (prop_idx, cprop->name, strlen (cprop->name), newSViv (cprop->idx ), 0); |
1481 | hv_store (prop_idx, cprop->name, strlen (cprop->name), newSViv (cprop->idx ), 0); |
1530 | } |
1482 | } |
1531 | |
1483 | |
1532 | cb_global = get_av ("cf::CB_GLOBAL", 1); |
|
|
1533 | cb_object = get_av ("cf::CB_OBJECT", 1); |
|
|
1534 | cb_player = get_av ("cf::CB_PLAYER", 1); |
|
|
1535 | cb_type = get_av ("cf::CB_TYPE" , 1); |
|
|
1536 | cb_map = get_av ("cf::CB_MAP" , 1); |
|
|
1537 | |
|
|
1538 | //I_EVENT_API (PACKAGE); |
1484 | //I_EVENT_API (PACKAGE); |
|
|
1485 | } |
|
|
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); |
1539 | } |
1507 | } |
1540 | |
1508 | |
1541 | NV floor (NV x) |
1509 | NV floor (NV x) |
1542 | |
1510 | |
1543 | NV ceil (NV x) |
1511 | NV ceil (NV x) |
… | |
… | |
1589 | |
1557 | |
1590 | int random_roll(int min, int max, object *op, int goodbad); |
1558 | int random_roll(int min, int max, object *op, int goodbad); |
1591 | |
1559 | |
1592 | const char *cost_string_from_value(uint64 cost, int approx = 0) |
1560 | const char *cost_string_from_value(uint64 cost, int approx = 0) |
1593 | |
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 | |
1594 | int |
1569 | int |
1595 | exp_to_level (val64 exp) |
1570 | exp_to_level (val64 exp) |
1596 | CODE: |
1571 | CODE: |
1597 | { |
1572 | { |
1598 | int i = 0; |
1573 | int i = 0; |
… | |
… | |
1629 | else |
1604 | else |
1630 | XSRETURN_UNDEF; |
1605 | XSRETURN_UNDEF; |
1631 | OUTPUT: RETVAL |
1606 | OUTPUT: RETVAL |
1632 | |
1607 | |
1633 | 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 |
1634 | |
1622 | |
1635 | SV * |
1623 | SV * |
1636 | get_property (object *obj, int type, int idx) |
1624 | get_property (object *obj, int type, int idx) |
1637 | CODE: |
1625 | CODE: |
1638 | RETVAL = newSVcfapi (type, cf_object_get_property (obj, idx)); |
1626 | RETVAL = newSVcfapi (type, cf_object_get_property (obj, idx)); |
… | |
… | |
1915 | |
1903 | |
1916 | void cf_player_set_party (object *op, partylist *party) |
1904 | void cf_player_set_party (object *op, partylist *party) |
1917 | |
1905 | |
1918 | void kill_player (object *op) |
1906 | void kill_player (object *op) |
1919 | |
1907 | |
1920 | MODULE = cf PACKAGE = cf::object::map PREFIX = cf_ |
|
|
1921 | |
|
|
1922 | 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 |
1923 | |
1922 | |
1924 | player *cf_player_find (char *name) |
1923 | player *cf_player_find (char *name) |
1925 | PROTOTYPE: $ |
1924 | PROTOTYPE: $ |
1926 | |
1925 | |
1927 | void cf_player_move (player *pl, int dir) |
1926 | void cf_player_move (player *pl, int dir) |
… | |
… | |
2045 | RETVAL = &pl->last_stats; |
2044 | RETVAL = &pl->last_stats; |
2046 | OUTPUT: RETVAL |
2045 | OUTPUT: RETVAL |
2047 | |
2046 | |
2048 | |
2047 | |
2049 | 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 |
2050 | |
2063 | |
2051 | SV * |
2064 | SV * |
2052 | get_property (mapstruct *obj, int type, int idx) |
2065 | get_property (mapstruct *obj, int type, int idx) |
2053 | CODE: |
2066 | CODE: |
2054 | RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx)); |
2067 | RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx)); |
… | |
… | |
2198 | 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; |
2199 | } |
2212 | } |
2200 | OUTPUT: |
2213 | OUTPUT: |
2201 | RETVAL |
2214 | RETVAL |
2202 | |
2215 | |
2203 | # "serialise" map perl data into a ref |
2216 | # "deserialise" perl map data into the map # TODO# compatibility cruft, remove |
2204 | void |
|
|
2205 | _get_obs (mapstruct *map) |
|
|
2206 | PPCODE: |
|
|
2207 | { |
|
|
2208 | object *o; |
|
|
2209 | int x, y; |
|
|
2210 | AV *obs = newAV (); |
|
|
2211 | int nonnull = 0; |
|
|
2212 | |
|
|
2213 | for (y = 0; y < MAP_HEIGHT (map); y++) |
|
|
2214 | for (x = 0; x < MAP_WIDTH (map); x++) |
|
|
2215 | { |
|
|
2216 | AV *av = newAV (); |
|
|
2217 | |
|
|
2218 | for (o = GET_MAP_OB (map, x, y); o; o = o->above) |
|
|
2219 | { |
|
|
2220 | SV *sv = SVptr_cache_get (o); |
|
|
2221 | |
|
|
2222 | if (sv && HvFILL (SvRV (sv))) |
|
|
2223 | { |
|
|
2224 | nonnull = 1; |
|
|
2225 | sv = newSVsv (sv); |
|
|
2226 | } |
|
|
2227 | else |
|
|
2228 | sv = &PL_sv_undef; |
|
|
2229 | |
|
|
2230 | av_push (av, sv); |
|
|
2231 | } |
|
|
2232 | |
|
|
2233 | av_store (obs, x + y * MAP_HEIGHT (map), newRV_noinc ((SV *)av)); |
|
|
2234 | } |
|
|
2235 | |
|
|
2236 | if (nonnull) |
|
|
2237 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)obs))); |
|
|
2238 | else |
|
|
2239 | SvREFCNT_dec (obs); |
|
|
2240 | } |
|
|
2241 | |
|
|
2242 | # "deserialise" perl map data into the map |
|
|
2243 | void |
2217 | void |
2244 | _set_obs (mapstruct *map, SV *sv) |
2218 | _set_obs (mapstruct *map, SV *sv) |
2245 | CODE: |
2219 | CODE: |
2246 | { |
2220 | { |
2247 | object *o; |
2221 | object *o; |