… | |
… | |
21 | * You should have received a copy of the GNU General Public License |
21 | * You should have received a copy of the GNU General Public License |
22 | * along with this program; if not, write to the Free Software |
22 | * along with this program; if not, write to the Free Software |
23 | * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
24 | */ |
24 | */ |
25 | |
25 | |
26 | #include <EXTERN.h> |
|
|
27 | #include <perl.h> |
|
|
28 | #include <XSUB.h> |
|
|
29 | |
|
|
30 | #undef save_long // clashes with libproto.h |
|
|
31 | |
|
|
32 | #define PLUGIN_NAME "perl" |
26 | #define PLUGIN_NAME "perl" |
33 | #define PLUGIN_VERSION "cfperl 0.3" |
27 | #define PLUGIN_VERSION "cfperl 0.5" |
34 | |
28 | |
35 | #ifndef __CEXTRACT__ |
29 | #ifndef __CEXTRACT__ |
36 | #include <plugin.h> |
30 | #include <plugin.h> |
37 | #endif |
31 | #endif |
38 | |
32 | |
… | |
… | |
48 | #include <sproto.h> |
42 | #include <sproto.h> |
49 | |
43 | |
50 | #include "cfperl.h" |
44 | #include "cfperl.h" |
51 | #include "shstr.h" |
45 | #include "shstr.h" |
52 | |
46 | |
|
|
47 | #include <EXTERN.h> |
|
|
48 | #include <perl.h> |
|
|
49 | #include <XSUB.h> |
|
|
50 | |
53 | #include "perlxsi.c" |
51 | #include "perlxsi.c" |
54 | |
52 | |
55 | extern sint64 *levels; // the experience table |
53 | extern sint64 *levels; // the experience table |
56 | |
54 | |
57 | typedef object object_ornull; |
55 | typedef object object_ornull; |
… | |
… | |
59 | |
57 | |
60 | typedef double val64; |
58 | typedef double val64; |
61 | #define newSVval64 newSVnv |
59 | #define newSVval64 newSVnv |
62 | #define SvVAL64 SvNV |
60 | #define SvVAL64 SvNV |
63 | |
61 | |
64 | static f_plug_api gethook; |
62 | static f_plug_api gethook = cfapi_get_hooks; |
65 | static f_plug_api systemDirectory; |
63 | static f_plug_api object_set_property = cfapi_object_set_property; |
66 | static f_plug_api object_set_property; |
64 | static f_plug_api object_insert = cfapi_object_insert; |
67 | static f_plug_api map_get_map; |
65 | |
68 | static f_plug_api object_insert; |
66 | static bool perl_booted; |
69 | |
67 | |
70 | /* this is a stupid way to do things, and awkward to use for plug-in authors */ |
68 | /* this is a stupid way to do things, and awkward to use for plug-in authors */ |
71 | typedef struct |
69 | typedef struct |
72 | { |
70 | { |
73 | object* who; |
71 | object* who; |
… | |
… | |
97 | #define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int)))) |
95 | #define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int)))) |
98 | |
96 | |
99 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
97 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
100 | |
98 | |
101 | static SV * |
99 | static SV * |
102 | newSVptr (void *ptr, const char *klass) |
100 | newSVptr (void *ptr, const char *klass, HV *hv = newHV ()) |
103 | { |
101 | { |
104 | SV *sv; |
102 | SV *sv; |
105 | |
103 | |
106 | if (!ptr) |
104 | if (!ptr) |
107 | return &PL_sv_undef; |
105 | return &PL_sv_undef; |
108 | |
106 | |
109 | sv = (SV *)newHV (); |
|
|
110 | sv_magic (sv, 0, PERL_MAGIC_ext, (char *)ptr, 0); |
107 | sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); |
111 | return sv_bless (newRV_noinc (sv), gv_stashpv (klass, 1)); |
108 | return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
112 | } |
109 | } |
113 | |
110 | |
114 | template<class extendable> |
111 | template<class attachable> |
115 | SV * |
112 | SV * |
116 | newSVextendable (extendable *obj, const char *klass) |
113 | newSVattachable (attachable *obj, const char *klass) |
117 | { |
114 | { |
118 | if (!obj) |
115 | if (!obj) |
119 | return &PL_sv_undef; |
116 | return &PL_sv_undef; |
120 | |
117 | |
121 | if (!obj->self) |
118 | if (!obj->self) |
122 | obj->self = newSVptr (obj, klass); |
119 | obj->self = newSVptr (obj, klass); |
123 | |
120 | |
124 | return newSVsv (static_cast<SV *>(obj->self)); |
121 | return newSVsv (obj->self); |
125 | } |
122 | } |
126 | |
123 | |
127 | static void |
124 | static void |
128 | SVptr_cache_set (void *ptr, SV *sv) |
125 | SVptr_cache_set (void *ptr, SV *sv) |
129 | { |
126 | { |
… | |
… | |
228 | break; |
225 | break; |
229 | |
226 | |
230 | case DT_OBJECT: |
227 | case DT_OBJECT: |
231 | { |
228 | { |
232 | object *obj = va_arg (ap, object *); |
229 | object *obj = va_arg (ap, object *); |
233 | sv = newSVextendable (obj, obj && obj->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap"); |
230 | sv = newSVattachable (obj, obj && obj->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap"); |
234 | } |
231 | } |
235 | break; |
232 | break; |
236 | |
233 | |
237 | case DT_MAP: |
234 | case DT_MAP: |
238 | // va_arg (object *) when void * is passed is an XSI extension |
235 | // va_arg (object *) when void * is passed is an XSI extension |
239 | sv = newSVextendable (va_arg (ap, mapstruct *), "cf::map::wrap"); |
236 | sv = newSVattachable (va_arg (ap, mapstruct *), "cf::map::wrap"); |
240 | break; |
237 | break; |
241 | |
238 | |
242 | case DT_PLAYER: |
239 | case DT_PLAYER: |
243 | sv = newSVextendable (va_arg (ap, player *), "cf::player::wrap"); |
240 | sv = newSVattachable (va_arg (ap, player *), "cf::player::wrap"); |
244 | break; |
241 | break; |
245 | |
242 | |
246 | case DT_ARCH: |
243 | case DT_ARCH: |
247 | sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap"); |
244 | sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap"); |
248 | break; |
245 | break; |
… | |
… | |
305 | } |
302 | } |
306 | |
303 | |
307 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
304 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
308 | |
305 | |
309 | SV * |
306 | SV * |
310 | registry_of (extendable_base *obj) |
307 | registry_of (attachable_base *ext) |
311 | { |
308 | { |
312 | if (!obj->cb) |
309 | if (!ext->cb) |
313 | obj->cb = newAV (); |
310 | ext->cb = newAV (); |
314 | |
311 | |
315 | return newRV_inc ((SV *)obj->cb); |
312 | return newRV_inc ((SV *)ext->cb); |
316 | } |
313 | } |
317 | |
314 | |
318 | void extendable_base::clear () |
315 | void attachable_base::clear () |
319 | { |
316 | { |
320 | //if (self) fprintf (stderr, "free_ob_self %p %s (%p,%p)\n", this, ((object *)this)->name, self, cb);//D |
317 | if (self) |
|
|
318 | { |
|
|
319 | if (cb) |
|
|
320 | if (SvROK (*av_fetch (cb, EVENT_OBJECT_DESTROY, 1))) |
|
|
321 | INVOKE_OBJECT (DESTROY, static_cast<object *>(this)); |
|
|
322 | else if (SvROK (*av_fetch (cb, EVENT_MAP_DESTROY, 1))) |
|
|
323 | INVOKE_MAP (DESTROY, static_cast<mapstruct *>(this)); |
321 | |
324 | |
|
|
325 | // disconnect Perl from C, to avoid crashes |
|
|
326 | sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext); |
|
|
327 | |
|
|
328 | // clear the perl hash, might or might not be a good idea |
|
|
329 | hv_clear ((HV *)SvRV ((SV *)self)); |
|
|
330 | |
322 | if (self) SvREFCNT_dec (self); |
331 | SvREFCNT_dec (self); |
|
|
332 | self = 0; |
|
|
333 | } |
|
|
334 | |
|
|
335 | if (cb) |
|
|
336 | { |
323 | if (cb) SvREFCNT_dec (cb); |
337 | SvREFCNT_dec (cb); |
|
|
338 | cb = 0; |
|
|
339 | } |
324 | |
340 | |
|
|
341 | if (attach) |
|
|
342 | { |
325 | if (attach) free_string (attach); |
343 | free_string (attach); |
326 | |
|
|
327 | self = 0; |
|
|
328 | cb = 0; |
|
|
329 | attach = 0; |
344 | attach = 0; |
|
|
345 | } |
330 | } |
346 | } |
331 | |
347 | |
332 | void extendable_base::optimise () |
348 | void attachable_base::optimise () |
333 | { |
349 | { |
334 | // optional, and not used yet anyways |
350 | if (!self) |
335 | // // empty and unreferenced? nuke it |
351 | return; |
336 | // if (SvREFCNT (sv) == 1 && SvREFCNT (SvRV (sv)) == 1 && !HvFILL ((HV *)(SvRV (sv)))) |
|
|
337 | // { |
|
|
338 | // hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD); |
|
|
339 | // todo++; |
|
|
340 | // } |
|
|
341 | |
352 | |
342 | } |
353 | HV *hv = (HV *)SvRV ((SV *)self); |
343 | |
354 | |
344 | void extendable_base::reattach (data_type type, void *self) |
355 | if (SvREFCNT ((SV *)self) == 1 |
345 | { |
356 | && SvREFCNT ((SV *)hv) == 1 |
346 | dSP; |
357 | && !HvKEYS (hv)) |
347 | ENTER; |
358 | { |
348 | SAVETMPS; |
359 | SvREFCNT_dec ((SV *)self); |
349 | PUSHMARK (SP); |
360 | self = 0; |
350 | EXTEND (SP, 1); |
361 | } |
351 | PUSHs (sv_2mortal (newSVdt (type, self))); |
|
|
352 | PUTBACK; |
|
|
353 | call_pv ("cf::reattach", G_DISCARD | G_VOID | G_EVAL); |
|
|
354 | FREETMPS; |
|
|
355 | LEAVE; |
|
|
356 | |
|
|
357 | if (cb && type == DT_OBJECT) |
|
|
358 | INVOKE_OBJECT (LOAD, (object *)self); |
|
|
359 | } |
362 | } |
360 | |
363 | |
361 | void |
364 | void attachable_base::instantiate (data_type type, void *obj) |
362 | object::instantiate_ () |
|
|
363 | { |
365 | { |
364 | dSP; |
366 | dSP; |
365 | ENTER; |
367 | ENTER; |
366 | SAVETMPS; |
368 | SAVETMPS; |
367 | PUSHMARK (SP); |
369 | PUSHMARK (SP); |
368 | EXTEND (SP, 2); |
370 | EXTEND (SP, 2); |
369 | PUSHs (sv_2mortal (newSVdt (DT_OBJECT, static_cast<object *>(this)))); |
371 | PUSHs (sv_2mortal (newSVdt (type, obj))); |
370 | PUSHs (sv_2mortal (newSVpv (attach, 0))); |
372 | PUSHs (sv_2mortal (newSVpv (attach, 0))); |
|
|
373 | |
|
|
374 | free_string (attach); |
|
|
375 | attach = 0; |
|
|
376 | |
371 | PUTBACK; |
377 | PUTBACK; |
372 | call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL); |
378 | call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL); |
373 | FREETMPS; |
379 | FREETMPS; |
374 | LEAVE; |
380 | LEAVE; |
|
|
381 | |
|
|
382 | switch (type) |
|
|
383 | { |
|
|
384 | case DT_OBJECT: INVOKE_OBJECT (INSTANTIATE, obj); break; |
|
|
385 | case DT_MAP: INVOKE_MAP (INSTANTIATE, obj); break; |
|
|
386 | } |
375 | } |
387 | } |
376 | |
388 | |
377 | ///////////////////////////////////////////////////////////////////////////// |
389 | ///////////////////////////////////////////////////////////////////////////// |
378 | |
390 | |
|
|
391 | void reattach (data_type type, void *obj) |
|
|
392 | { |
|
|
393 | //TODO only do this when the object has _attachment's |
|
|
394 | |
|
|
395 | dSP; |
|
|
396 | ENTER; |
|
|
397 | SAVETMPS; |
|
|
398 | PUSHMARK (SP); |
|
|
399 | XPUSHs (sv_2mortal (newSVdt (type, obj))); |
|
|
400 | PUTBACK; |
|
|
401 | call_pv ("cf::reattach", G_DISCARD | G_VOID | G_EVAL); |
|
|
402 | FREETMPS; |
|
|
403 | LEAVE; |
|
|
404 | |
|
|
405 | switch (type) |
|
|
406 | { |
|
|
407 | case DT_OBJECT: INVOKE_OBJECT (REATTACH, obj); break; |
|
|
408 | case DT_PLAYER: INVOKE_PLAYER (REATTACH, obj); break; |
|
|
409 | case DT_MAP: INVOKE_MAP (REATTACH, obj); break; |
|
|
410 | } |
|
|
411 | } |
|
|
412 | |
|
|
413 | template<class subclass> |
|
|
414 | void reattach (attachable<subclass> *obj) |
|
|
415 | { |
|
|
416 | obj->optimise (); |
|
|
417 | |
|
|
418 | if (obj->self) |
|
|
419 | reattach (subclass::get_dt (), (subclass *)obj); |
|
|
420 | } |
|
|
421 | |
|
|
422 | object_freezer::object_freezer (const char *filename) |
|
|
423 | { |
|
|
424 | this->filename = (SV *)newSVpv (filename, 0); |
|
|
425 | |
|
|
426 | char filename2 [4096]; |
|
|
427 | snprintf (filename2, 4096, "%s~", filename); |
|
|
428 | |
|
|
429 | av = newAV (); |
|
|
430 | |
|
|
431 | // TODO: fast dynbuf implementation... yeah, we need obstacks |
|
|
432 | text = newSV (10 * 1024 * 1024); // only temporarily used, so be generous |
|
|
433 | } |
|
|
434 | |
|
|
435 | object_freezer::~object_freezer () |
|
|
436 | { |
|
|
437 | dSP; |
|
|
438 | ENTER; |
|
|
439 | SAVETMPS; |
|
|
440 | PUSHMARK (SP); |
|
|
441 | XPUSHs (sv_2mortal ((SV *)filename)); |
|
|
442 | XPUSHs (sv_2mortal (newRV_noinc (text))); |
|
|
443 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
444 | PUTBACK; |
|
|
445 | call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL); |
|
|
446 | FREETMPS; |
|
|
447 | LEAVE; |
|
|
448 | } |
|
|
449 | |
|
|
450 | void object_freezer::put (attachable_base *ext) |
|
|
451 | { |
|
|
452 | ext->optimise (); |
|
|
453 | |
|
|
454 | if (ext->self) |
|
|
455 | { |
|
|
456 | int idx = AvFILLp ((AV *)av) + 1; |
|
|
457 | av_store (av, idx, SvREFCNT_inc (ext->self)); |
|
|
458 | |
|
|
459 | sv_catpvf (text, "oid %d\n", idx); |
|
|
460 | } |
|
|
461 | } |
|
|
462 | |
|
|
463 | int fprintf (object_freezer &freezer, const char *format, ...) |
|
|
464 | { |
|
|
465 | va_list ap; |
|
|
466 | |
|
|
467 | va_start (ap, format); |
|
|
468 | sv_vcatpvfn (freezer.text, format, strlen (format), &ap, 0, 0, 0); |
|
|
469 | va_end (ap); |
|
|
470 | } |
|
|
471 | |
|
|
472 | int fputs (const char *s, object_freezer &freezer) |
|
|
473 | { |
|
|
474 | sv_catpvn (freezer.text, s, strlen (s)); |
|
|
475 | } |
|
|
476 | |
|
|
477 | object_thawer::object_thawer (const char *filename) |
|
|
478 | { |
|
|
479 | av = 0; |
|
|
480 | fp = 0; |
|
|
481 | |
|
|
482 | if (!filename) |
|
|
483 | return; |
|
|
484 | |
|
|
485 | fp = fopen (filename, "r"); |
|
|
486 | if (!fp) |
|
|
487 | { |
|
|
488 | LOG (llevError, "object_thawer: unable to open '%s': %s.\n", filename, strerror (errno)); |
|
|
489 | return; |
|
|
490 | } |
|
|
491 | |
|
|
492 | if (perl_booted) |
|
|
493 | { |
|
|
494 | dSP; |
|
|
495 | ENTER; |
|
|
496 | SAVETMPS; |
|
|
497 | PUSHMARK (SP); |
|
|
498 | XPUSHs (sv_2mortal (newSVpv (filename, 0))); |
|
|
499 | PUTBACK; |
|
|
500 | |
|
|
501 | if (0 < call_pv ("cf::object_thawer_load", G_SCALAR | G_EVAL)) |
|
|
502 | { |
|
|
503 | SPAGAIN; |
|
|
504 | SV *sv = POPs; |
|
|
505 | if (SvROK (sv)) |
|
|
506 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
|
|
507 | } |
|
|
508 | |
|
|
509 | FREETMPS; |
|
|
510 | LEAVE; |
|
|
511 | } |
|
|
512 | } |
|
|
513 | |
|
|
514 | void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid) |
|
|
515 | { |
|
|
516 | if (!av || oid < 0) // this is actually an error of sorts |
|
|
517 | return; |
|
|
518 | |
|
|
519 | // we have to "re-instantiate"/reattach to an object, so nuke ext->attach |
|
|
520 | ext->clear (); |
|
|
521 | |
|
|
522 | SV **svp = av_fetch ((AV *)av, oid, 0); |
|
|
523 | |
|
|
524 | if (!svp || !SvROK (*svp)) |
|
|
525 | { |
|
|
526 | printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
|
|
527 | return; |
|
|
528 | } |
|
|
529 | |
|
|
530 | ext->self = *svp; *svp = &PL_sv_undef; |
|
|
531 | sv_magic (SvRV (ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0); |
|
|
532 | |
|
|
533 | reattach (type, obj); |
|
|
534 | } |
|
|
535 | |
|
|
536 | object_thawer::~object_thawer () |
|
|
537 | { |
|
|
538 | if (fp) fclose (fp); |
|
|
539 | if (av) SvREFCNT_dec ((AV *)av); |
|
|
540 | } |
|
|
541 | |
|
|
542 | ///////////////////////////////////////////////////////////////////////////// |
|
|
543 | |
379 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
544 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
380 | { |
545 | { |
381 | gethook = gethooksptr; |
|
|
382 | printf (PLUGIN_VERSION " init\n"); |
|
|
383 | |
|
|
384 | return 0; |
546 | return 0; |
385 | } |
547 | } |
386 | |
548 | |
387 | static CommArray_s rtn_cmd; |
549 | static CommArray_s rtn_cmd; |
388 | |
550 | |
… | |
… | |
468 | extern "C" int cfperl_postInitPlugin () |
630 | extern "C" int cfperl_postInitPlugin () |
469 | { |
631 | { |
470 | int hooktype = 1; |
632 | int hooktype = 1; |
471 | int rtype = 0; |
633 | int rtype = 0; |
472 | |
634 | |
473 | printf (PLUGIN_VERSION " post init\n"); |
|
|
474 | |
|
|
475 | systemDirectory = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_directory"); |
|
|
476 | object_set_property = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_set_property"); |
|
|
477 | map_get_map = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_map_get_map"); |
|
|
478 | object_insert = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_insert"); |
|
|
479 | |
|
|
480 | cf_init_plugin (gethook); |
635 | cf_init_plugin (gethook); |
481 | |
636 | |
482 | return 0; |
637 | return 0; |
483 | } |
638 | } |
484 | |
639 | |
… | |
… | |
488 | } |
643 | } |
489 | |
644 | |
490 | void |
645 | void |
491 | cfperl_init () |
646 | cfperl_init () |
492 | { |
647 | { |
|
|
648 | PERL_SYS_INIT3 (&settings.argc, &settings.argv, 0); |
|
|
649 | perl = perl_alloc (); |
|
|
650 | perl_construct (perl); |
|
|
651 | |
|
|
652 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
|
|
653 | |
493 | char *argv[] = { |
654 | char *argv[] = { |
494 | "", |
655 | "", |
495 | "-e" |
656 | "-e" |
496 | "BEGIN {" |
|
|
497 | " cf->bootstrap;" |
657 | "cf->bootstrap;" |
498 | " unshift @INC, cf::datadir ();" |
658 | "unshift @INC, cf::datadir ();" |
499 | "}" |
|
|
500 | "" |
|
|
501 | "use cf;" |
|
|
502 | }; |
659 | }; |
503 | |
|
|
504 | perl = perl_alloc (); |
|
|
505 | perl_construct (perl); |
|
|
506 | |
|
|
507 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
|
|
508 | |
660 | |
509 | if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) |
661 | if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) |
510 | { |
662 | { |
511 | printf ("unable to initialize perl-interpreter, aborting.\n"); |
663 | printf ("unable to initialize perl-interpreter, aborting.\n"); |
512 | |
|
|
513 | exit (EXIT_FAILURE); |
664 | exit (EXIT_FAILURE); |
514 | //perl_destruct (perl); |
|
|
515 | //perl_free (perl); |
|
|
516 | //perl = 0; |
|
|
517 | //return; |
|
|
518 | } |
665 | } |
519 | |
666 | |
520 | obj_cache = newHV (); |
667 | obj_cache = newHV (); |
|
|
668 | } |
|
|
669 | |
|
|
670 | void cfperl_boot () |
|
|
671 | { |
|
|
672 | perl_booted = true; |
|
|
673 | |
|
|
674 | eval_pv ("require cf", 1); |
521 | } |
675 | } |
522 | |
676 | |
523 | void cfperl_main () |
677 | void cfperl_main () |
524 | { |
678 | { |
525 | dSP; |
679 | dSP; |
… | |
… | |
532 | static event_klass klass_of[NUM_EVENT_TYPES] = { |
686 | static event_klass klass_of[NUM_EVENT_TYPES] = { |
533 | # define def(type,name) KLASS_ ## type, |
687 | # define def(type,name) KLASS_ ## type, |
534 | # include "eventinc.h" |
688 | # include "eventinc.h" |
535 | # undef def |
689 | # undef def |
536 | }; |
690 | }; |
|
|
691 | |
|
|
692 | #define KLASS_OF(event) (((unsigned int)event) < NUM_EVENT_TYPES ? klass_of [event] : KLASS_NONE) |
537 | |
693 | |
538 | static void |
694 | static void |
539 | gather_callbacks (AV *&callbacks, AV *registry, event_type event) |
695 | gather_callbacks (AV *&callbacks, AV *registry, event_type event) |
540 | { |
696 | { |
541 | // event must be in array |
697 | // event must be in array |
… | |
… | |
584 | // 2a. per-type callback |
740 | // 2a. per-type callback |
585 | // 4. global callbacks |
741 | // 4. global callbacks |
586 | |
742 | |
587 | gather_callbacks (callbacks, cb_global, event); |
743 | gather_callbacks (callbacks, cb_global, event); |
588 | |
744 | |
589 | switch (klass_of [event]) |
745 | switch (KLASS_OF (event)) |
590 | { |
746 | { |
591 | case KLASS_GLOBAL: |
747 | case KLASS_GLOBAL: |
592 | break; |
748 | break; |
593 | |
749 | |
594 | case KLASS_OBJECT: |
750 | case KLASS_OBJECT: |
595 | dt = (data_type) va_arg (ap, int); |
751 | dt = (data_type) va_arg (ap, int); |
596 | assert (("first argument must be of type object", dt == DT_OBJECT)); |
752 | assert (("first argument must be of type object", dt == DT_OBJECT)); |
597 | op = va_arg (ap, object *); |
753 | op = va_arg (ap, object *); |
598 | |
754 | |
599 | if (op->cb) |
755 | if (op->cb) |
600 | gather_callbacks (callbacks, (AV *)op->cb, event); |
756 | gather_callbacks (callbacks, op->cb, event); |
|
|
757 | |
|
|
758 | if (op->type) |
|
|
759 | { |
|
|
760 | if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type)) |
|
|
761 | { |
|
|
762 | SV *registry = AvARRAY (cb_type)[op->type + op->subtype * NUM_SUBTYPES]; |
|
|
763 | |
|
|
764 | if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV) |
|
|
765 | gather_callbacks (callbacks, (AV *)SvRV (registry), event); |
|
|
766 | } |
|
|
767 | |
|
|
768 | if (op->type <= AvFILLp (cb_type)) |
|
|
769 | { |
|
|
770 | SV *registry = AvARRAY (cb_type)[op->type]; |
|
|
771 | |
|
|
772 | if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV) |
|
|
773 | gather_callbacks (callbacks, (AV *)SvRV (registry), event); |
|
|
774 | } |
|
|
775 | } |
601 | |
776 | |
602 | gather_callbacks (callbacks, cb_object, event); |
777 | gather_callbacks (callbacks, cb_object, event); |
603 | |
|
|
604 | if (op->type > 0 && op->type <= AvFILLp (cb_type)) |
|
|
605 | { |
|
|
606 | SV *registry = AvARRAY (cb_type)[op->type]; |
|
|
607 | |
|
|
608 | if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV) |
|
|
609 | gather_callbacks (callbacks, (AV *)SvRV (registry), event); |
|
|
610 | } |
|
|
611 | |
778 | |
612 | break; |
779 | break; |
613 | |
780 | |
614 | case KLASS_PLAYER: |
781 | case KLASS_PLAYER: |
615 | dt = (data_type) va_arg (ap, int); |
782 | dt = (data_type) va_arg (ap, int); |
616 | assert (("first argument must be of type player", dt == DT_PLAYER)); |
783 | assert (("first argument must be of type player", dt == DT_PLAYER)); |
617 | pl = va_arg (ap, player *); |
784 | pl = va_arg (ap, player *); |
618 | |
785 | |
619 | if (op->cb) |
786 | if (pl->cb) |
620 | gather_callbacks (callbacks, (AV *)op->cb, event); |
787 | gather_callbacks (callbacks, pl->cb, event); |
621 | |
788 | |
622 | gather_callbacks (callbacks, cb_player, event); |
789 | gather_callbacks (callbacks, cb_player, event); |
623 | break; |
790 | break; |
624 | |
791 | |
625 | case KLASS_MAP: |
792 | case KLASS_MAP: |
626 | dt = (data_type) va_arg (ap, int); |
793 | dt = (data_type) va_arg (ap, int); |
627 | assert (("first argument must be of type object", dt == DT_MAP)); |
794 | assert (("first argument must be of type object", dt == DT_MAP)); |
628 | map = va_arg (ap, mapstruct *); |
795 | map = va_arg (ap, mapstruct *); |
629 | |
796 | |
630 | if (op->cb) |
797 | if (map->cb) |
631 | gather_callbacks (callbacks, (AV *)op->cb, event); |
798 | gather_callbacks (callbacks, map->cb, event); |
632 | |
799 | |
633 | gather_callbacks (callbacks, cb_map, event); |
800 | gather_callbacks (callbacks, cb_map, event); |
634 | break; |
801 | break; |
635 | |
802 | |
636 | default: |
803 | default: |
… | |
… | |
649 | EXTEND (SP, 3); |
816 | EXTEND (SP, 3); |
650 | |
817 | |
651 | PUSHs (sv_2mortal (newSViv (event))); // only used for debugging nowadays |
818 | PUSHs (sv_2mortal (newSViv (event))); // only used for debugging nowadays |
652 | PUSHs (sv_2mortal (newRV_noinc ((SV *)callbacks))); |
819 | PUSHs (sv_2mortal (newRV_noinc ((SV *)callbacks))); |
653 | |
820 | |
654 | switch (klass_of [event]) |
821 | switch (KLASS_OF (event)) |
655 | { |
822 | { |
656 | case KLASS_OBJECT: PUSHs (sv_2mortal (newSVdt (DT_OBJECT, op))); break; |
823 | case KLASS_OBJECT: PUSHs (sv_2mortal (newSVdt (DT_OBJECT, op))); break; |
657 | case KLASS_PLAYER: PUSHs (sv_2mortal (newSVdt (DT_PLAYER, pl))); break; |
824 | case KLASS_PLAYER: PUSHs (sv_2mortal (newSVdt (DT_PLAYER, pl))); break; |
658 | case KLASS_MAP: PUSHs (sv_2mortal (newSVdt (DT_MAP, map))); break; |
825 | case KLASS_MAP: PUSHs (sv_2mortal (newSVdt (DT_MAP, map))); break; |
659 | } |
826 | } |
… | |
… | |
849 | const_iv (BUILDER) |
1016 | const_iv (BUILDER) |
850 | const_iv (MATERIAL) |
1017 | const_iv (MATERIAL) |
851 | const_iv (ITEM_TRANSFORMER) |
1018 | const_iv (ITEM_TRANSFORMER) |
852 | const_iv (QUEST) |
1019 | const_iv (QUEST) |
853 | |
1020 | |
|
|
1021 | const_iv (NUM_SUBTYPES) |
|
|
1022 | |
854 | const_iv (ST_BD_BUILD) |
1023 | const_iv (ST_BD_BUILD) |
855 | const_iv (ST_BD_REMOVE) |
1024 | const_iv (ST_BD_REMOVE) |
856 | |
1025 | |
857 | const_iv (ST_MAT_FLOOR) |
1026 | const_iv (ST_MAT_FLOOR) |
858 | const_iv (ST_MAT_WALL) |
1027 | const_iv (ST_MAT_WALL) |
… | |
… | |
1199 | |
1368 | |
1200 | const_iv (KLASS_GLOBAL) |
1369 | const_iv (KLASS_GLOBAL) |
1201 | const_iv (KLASS_OBJECT) |
1370 | const_iv (KLASS_OBJECT) |
1202 | const_iv (KLASS_PLAYER) |
1371 | const_iv (KLASS_PLAYER) |
1203 | const_iv (KLASS_MAP) |
1372 | const_iv (KLASS_MAP) |
1204 | |
|
|
1205 | const_iv (EVENT_OBJECT_INSTANTIATE) |
|
|
1206 | }; |
1373 | }; |
1207 | |
1374 | |
1208 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
1375 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
1209 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
1376 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
1210 | |
1377 | |
1211 | static const struct { |
1378 | static const struct { |
1212 | const char *name; |
1379 | const char *name; |
|
|
1380 | int skip; |
1213 | IV klass; |
1381 | IV klass; |
1214 | IV iv; |
1382 | IV iv; |
1215 | } *eiv, event_iv[] = { |
1383 | } *eiv, event_iv[] = { |
1216 | # define def(klass,name) { # name, (IV)KLASS_ ## klass, (IV)EVENT_ ## klass ## _ ## name }, |
1384 | # define def(klass,name) { "EVENT_" # klass "_" # name, sizeof ("EVENT_" # klass), (IV)KLASS_ ## klass, (IV)EVENT_ ## klass ## _ ## name }, |
1217 | # include "eventinc.h" |
1385 | # include "eventinc.h" |
1218 | # undef def |
1386 | # undef def |
1219 | }; |
1387 | }; |
1220 | |
1388 | |
1221 | AV *av = get_av ("cf::EVENT", 1); |
1389 | AV *av = get_av ("cf::EVENT", 1); |
1222 | |
1390 | |
1223 | for (eiv = event_iv + sizeof (event_iv) / sizeof (event_iv [0]); eiv-- > event_iv; ) |
1391 | for (eiv = event_iv + sizeof (event_iv) / sizeof (event_iv [0]); eiv-- > event_iv; ) |
1224 | { |
1392 | { |
1225 | AV *event = newAV (); |
1393 | AV *event = newAV (); |
1226 | av_push (event, newSVpv ((char *)eiv->name, 0)); |
1394 | av_push (event, newSVpv ((char *)eiv->name + eiv->skip, 0)); |
1227 | av_push (event, newSViv (eiv->klass)); |
1395 | av_push (event, newSViv (eiv->klass)); |
1228 | av_store (av, eiv->iv, newRV_noinc ((SV *)event)); |
1396 | av_store (av, eiv->iv, newRV_noinc ((SV *)event)); |
|
|
1397 | newCONSTSUB (stash, (char *)eiv->name, newSViv (eiv->iv)); |
1229 | } |
1398 | } |
1230 | |
1399 | |
1231 | static const struct { |
1400 | static const struct { |
1232 | int dtype; |
1401 | int dtype; |
1233 | const char *name; |
1402 | const char *name; |
… | |
… | |
1254 | prop (CFAPI_INT, MAP_PROP_WINDDIR) |
1423 | prop (CFAPI_INT, MAP_PROP_WINDDIR) |
1255 | prop (CFAPI_INT, MAP_PROP_SKY) |
1424 | prop (CFAPI_INT, MAP_PROP_SKY) |
1256 | prop (CFAPI_INT, MAP_PROP_WPARTX) |
1425 | prop (CFAPI_INT, MAP_PROP_WPARTX) |
1257 | prop (CFAPI_INT, MAP_PROP_WPARTY) |
1426 | prop (CFAPI_INT, MAP_PROP_WPARTY) |
1258 | prop (CFAPI_STRING, MAP_PROP_MESSAGE) |
1427 | prop (CFAPI_STRING, MAP_PROP_MESSAGE) |
1259 | prop (CFAPI_PMAP, MAP_PROP_NEXT) |
|
|
1260 | prop (CFAPI_PREGION, MAP_PROP_REGION) |
1428 | prop (CFAPI_PREGION, MAP_PROP_REGION) |
1261 | prop (CFAPI_POBJECT, OBJECT_PROP_OB_ABOVE) |
|
|
1262 | prop (CFAPI_POBJECT, OBJECT_PROP_OB_BELOW) |
|
|
1263 | prop (CFAPI_POBJECT, OBJECT_PROP_NEXT_ACTIVE_OB) |
1429 | prop (CFAPI_POBJECT, OBJECT_PROP_NEXT_ACTIVE_OB) |
1264 | prop (CFAPI_POBJECT, OBJECT_PROP_PREV_ACTIVE_OB) |
1430 | prop (CFAPI_POBJECT, OBJECT_PROP_PREV_ACTIVE_OB) |
1265 | prop (CFAPI_POBJECT, OBJECT_PROP_INVENTORY) |
1431 | prop (CFAPI_POBJECT, OBJECT_PROP_INVENTORY) |
1266 | prop (CFAPI_POBJECT, OBJECT_PROP_ENVIRONMENT) |
1432 | prop (CFAPI_POBJECT, OBJECT_PROP_ENVIRONMENT) |
1267 | prop (CFAPI_POBJECT, OBJECT_PROP_HEAD) |
|
|
1268 | prop (CFAPI_POBJECT, OBJECT_PROP_CONTAINER) |
1433 | prop (CFAPI_POBJECT, OBJECT_PROP_CONTAINER) |
1269 | prop (CFAPI_PMAP, OBJECT_PROP_MAP) |
1434 | prop (CFAPI_PMAP, OBJECT_PROP_MAP) |
1270 | prop (CFAPI_INT, OBJECT_PROP_COUNT) |
1435 | prop (CFAPI_INT, OBJECT_PROP_COUNT) |
1271 | prop (CFAPI_INT, OBJECT_PROP_REFCOUNT) |
1436 | prop (CFAPI_INT, OBJECT_PROP_REFCOUNT) |
1272 | prop (CFAPI_STRING, OBJECT_PROP_NAME) |
1437 | prop (CFAPI_STRING, OBJECT_PROP_NAME) |
… | |
… | |
1362 | { |
1527 | { |
1363 | hv_store (prop_type, cprop->name, strlen (cprop->name), newSViv (cprop->dtype), 0); |
1528 | hv_store (prop_type, cprop->name, strlen (cprop->name), newSViv (cprop->dtype), 0); |
1364 | hv_store (prop_idx, cprop->name, strlen (cprop->name), newSViv (cprop->idx ), 0); |
1529 | hv_store (prop_idx, cprop->name, strlen (cprop->name), newSViv (cprop->idx ), 0); |
1365 | } |
1530 | } |
1366 | |
1531 | |
1367 | cb_global = get_av ("cf::CB_GLOBAL", 1); |
|
|
1368 | cb_object = get_av ("cf::CB_OBJECT", 1); |
|
|
1369 | cb_player = get_av ("cf::CB_PLAYER", 1); |
|
|
1370 | cb_type = get_av ("cf::CB_TYPE" , 1); |
|
|
1371 | cb_map = get_av ("cf::CB_MAP" , 1); |
|
|
1372 | |
|
|
1373 | //I_EVENT_API (PACKAGE); |
1532 | //I_EVENT_API (PACKAGE); |
|
|
1533 | } |
|
|
1534 | |
|
|
1535 | void _reload_1 () |
|
|
1536 | CODE: |
|
|
1537 | cb_global = get_av ("cf::CB_GLOBAL", 1); |
|
|
1538 | cb_object = get_av ("cf::CB_OBJECT", 1); |
|
|
1539 | cb_player = get_av ("cf::CB_PLAYER", 1); |
|
|
1540 | cb_type = get_av ("cf::CB_TYPE" , 1); |
|
|
1541 | cb_map = get_av ("cf::CB_MAP" , 1); |
|
|
1542 | |
|
|
1543 | void _reload_2 () |
|
|
1544 | CODE: |
|
|
1545 | { |
|
|
1546 | // reattach to all attachable objects in the game. |
|
|
1547 | for (player *pl = first_player; pl; pl = pl->next) |
|
|
1548 | reattach (pl); |
|
|
1549 | |
|
|
1550 | for (mapstruct *map = first_map; map; map = map->next) |
|
|
1551 | reattach (map); |
|
|
1552 | |
|
|
1553 | for (object *op = objects; op; op = op->next) |
|
|
1554 | reattach (op); |
1374 | } |
1555 | } |
1375 | |
1556 | |
1376 | NV floor (NV x) |
1557 | NV floor (NV x) |
1377 | |
1558 | |
1378 | NV ceil (NV x) |
1559 | NV ceil (NV x) |
… | |
… | |
1388 | PROTOTYPE: $$ |
1569 | PROTOTYPE: $$ |
1389 | |
1570 | |
1390 | char *path_combine_and_normalize (char *base, char *path) |
1571 | char *path_combine_and_normalize (char *base, char *path) |
1391 | PROTOTYPE: $$ |
1572 | PROTOTYPE: $$ |
1392 | |
1573 | |
1393 | char * |
1574 | const char * |
1394 | cf_get_maps_directory (char *path) |
1575 | get_maps_directory (char *path) |
1395 | PROTOTYPE: $ |
1576 | PROTOTYPE: $ |
1396 | ALIAS: maps_directory = 0 |
1577 | ALIAS: maps_directory = 0 |
|
|
1578 | CODE: |
|
|
1579 | RETVAL = create_pathname (path); |
|
|
1580 | OUTPUT: RETVAL |
1397 | |
1581 | |
1398 | void |
1582 | void |
1399 | sub_generation_inc () |
1583 | sub_generation_inc () |
1400 | CODE: |
1584 | CODE: |
1401 | PL_sub_generation++; |
1585 | PL_sub_generation++; |
… | |
… | |
1410 | confdir = 3 |
1594 | confdir = 3 |
1411 | localdir = 4 |
1595 | localdir = 4 |
1412 | playerdir = 5 |
1596 | playerdir = 5 |
1413 | datadir = 6 |
1597 | datadir = 6 |
1414 | CODE: |
1598 | CODE: |
1415 | { |
1599 | switch (ix) |
1416 | int unused_type; |
1600 | { |
1417 | RETVAL = (char *)systemDirectory (&unused_type, ix); |
1601 | case 0: RETVAL = settings.mapdir ; break; |
1418 | } |
1602 | case 1: RETVAL = settings.uniquedir; break; |
|
|
1603 | case 2: RETVAL = settings.tmpdir ; break; |
|
|
1604 | case 3: RETVAL = settings.confdir ; break; |
|
|
1605 | case 4: RETVAL = settings.localdir ; break; |
|
|
1606 | case 5: RETVAL = settings.playerdir; break; |
|
|
1607 | case 6: RETVAL = settings.datadir ; break; |
|
|
1608 | } |
1419 | OUTPUT: RETVAL |
1609 | OUTPUT: RETVAL |
1420 | |
1610 | |
1421 | int |
1611 | int |
1422 | cf_find_animation (char *text) |
1612 | cf_find_animation (char *text) |
1423 | PROTOTYPE: $ |
1613 | PROTOTYPE: $ |
1424 | |
1614 | |
1425 | int random_roll(int min, int max, object *op, int goodbad); |
1615 | int random_roll(int min, int max, object *op, int goodbad); |
1426 | |
1616 | |
1427 | const char *cost_string_from_value(uint64 cost, int approx = 0) |
1617 | const char *cost_string_from_value(uint64 cost, int approx = 0) |
1428 | |
1618 | |
1429 | int invoke (int event, SV *arg1, ...) |
1619 | int invoke (int event, ...) |
1430 | CODE: |
1620 | CODE: |
1431 | { |
1621 | if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL"); |
1432 | // parse first arg and call invoke_object etc as seen fit |
1622 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
1433 | //TODO |
1623 | for (int i = 1; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); |
1434 | //cfperl_invoke (event, |
1624 | RETVAL = INVOKE_((event_type)event, ARG_AV (av)); |
1435 | } |
1625 | OUTPUT: RETVAL |
1436 | |
1626 | |
1437 | int |
1627 | int |
1438 | exp_to_level (val64 exp) |
1628 | exp_to_level (val64 exp) |
1439 | CODE: |
1629 | CODE: |
1440 | { |
1630 | { |
… | |
… | |
1471 | RETVAL = newSVpv (resist_plus[atnr], 0); |
1661 | RETVAL = newSVpv (resist_plus[atnr], 0); |
1472 | else |
1662 | else |
1473 | XSRETURN_UNDEF; |
1663 | XSRETURN_UNDEF; |
1474 | OUTPUT: RETVAL |
1664 | OUTPUT: RETVAL |
1475 | |
1665 | |
|
|
1666 | int |
|
|
1667 | _valid (SV *obj) |
|
|
1668 | CODE: |
|
|
1669 | RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext); |
|
|
1670 | OUTPUT: |
|
|
1671 | RETVAL |
|
|
1672 | |
1476 | MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ |
1673 | MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ |
|
|
1674 | |
|
|
1675 | int invoke (object *op, int event, ...) |
|
|
1676 | CODE: |
|
|
1677 | if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT"); |
|
|
1678 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
1679 | for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); |
|
|
1680 | RETVAL = INVOKE_((event_type)event, ARG_OBJECT (op), ARG_AV (av)); |
|
|
1681 | OUTPUT: RETVAL |
|
|
1682 | |
|
|
1683 | SV *registry (object *op) |
|
|
1684 | CODE: |
|
|
1685 | RETVAL = registry_of (op); |
|
|
1686 | OUTPUT: |
|
|
1687 | RETVAL |
|
|
1688 | |
|
|
1689 | object *head (object *op) |
|
|
1690 | PROTOTYPE: $ |
|
|
1691 | ALIAS: |
|
|
1692 | more = 1 |
|
|
1693 | above = 2 |
|
|
1694 | below = 3 |
|
|
1695 | CODE: |
|
|
1696 | switch (ix) |
|
|
1697 | { |
|
|
1698 | case 0: RETVAL = op->head ? op->head : op; break; // DOH! |
|
|
1699 | case 1: RETVAL = op->more; break; |
|
|
1700 | case 2: RETVAL = op->above; break; |
|
|
1701 | case 3: RETVAL = op->below; break; |
|
|
1702 | } |
|
|
1703 | OUTPUT: |
|
|
1704 | RETVAL |
1477 | |
1705 | |
1478 | SV * |
1706 | SV * |
1479 | get_property (object *obj, int type, int idx) |
1707 | get_property (object *obj, int type, int idx) |
1480 | CODE: |
1708 | CODE: |
1481 | RETVAL = newSVcfapi (type, cf_object_get_property (obj, idx)); |
1709 | RETVAL = newSVcfapi (type, cf_object_get_property (obj, idx)); |
… | |
… | |
1560 | |
1788 | |
1561 | int need_identify (const object *obj); |
1789 | int need_identify (const object *obj); |
1562 | |
1790 | |
1563 | int apply_shop_mat (object *shop_mat, object *op); |
1791 | int apply_shop_mat (object *shop_mat, object *op); |
1564 | |
1792 | |
1565 | void cf_object_move (object *op, int dir, object *originator = op) |
1793 | int move (object *op, int dir, object *originator = op) |
|
|
1794 | CODE: |
|
|
1795 | RETVAL = move_ob (op, dir, originator); |
|
|
1796 | OUTPUT: |
|
|
1797 | RETVAL |
1566 | |
1798 | |
1567 | void cf_object_apply (object *op, object *author, int flags = 0) |
1799 | void cf_object_apply (object *op, object *author, int flags = 0) |
1568 | |
1800 | |
1569 | void cf_object_apply_below (object *op) |
1801 | void cf_object_apply_below (object *op) |
1570 | |
1802 | |
… | |
… | |
1611 | int cf_object_out_of_map (object *op, int x, int y) |
1843 | int cf_object_out_of_map (object *op, int x, int y) |
1612 | |
1844 | |
1613 | void cf_object_drop (object *op, object *author) |
1845 | void cf_object_drop (object *op, object *author) |
1614 | |
1846 | |
1615 | void cf_object_take (object *op, object *author) |
1847 | void cf_object_take (object *op, object *author) |
1616 | |
|
|
1617 | void cf_object_say (object *op, char *msg) |
|
|
1618 | |
|
|
1619 | void cf_object_speak (object *op, char *msg) |
|
|
1620 | |
1848 | |
1621 | object *cf_object_insert_object (object *op, object *container) |
1849 | object *cf_object_insert_object (object *op, object *container) |
1622 | |
1850 | |
1623 | const char *cf_object_get_msg (object *ob) |
1851 | const char *cf_object_get_msg (object *ob) |
1624 | ALIAS: msg = 0 |
1852 | ALIAS: msg = 0 |
… | |
… | |
1678 | CODE: |
1906 | CODE: |
1679 | { |
1907 | { |
1680 | int unused_type; |
1908 | int unused_type; |
1681 | RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y); |
1909 | RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y); |
1682 | } |
1910 | } |
1683 | |
|
|
1684 | SV *registry (object *op) |
|
|
1685 | CODE: |
|
|
1686 | RETVAL = registry_of (op); |
|
|
1687 | OUTPUT: |
|
|
1688 | RETVAL |
|
|
1689 | |
1911 | |
1690 | # syntatic sugar for easier use in event callbacks. |
1912 | # syntatic sugar for easier use in event callbacks. |
1691 | const char *options (object *op) |
1913 | const char *options (object *op) |
1692 | CODE: |
1914 | CODE: |
1693 | RETVAL = op->name; |
1915 | RETVAL = op->name; |
… | |
… | |
1766 | |
1988 | |
1767 | void kill_player (object *op) |
1989 | void kill_player (object *op) |
1768 | |
1990 | |
1769 | MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ |
1991 | MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ |
1770 | |
1992 | |
|
|
1993 | int invoke (player *pl, int event, ...) |
|
|
1994 | CODE: |
|
|
1995 | if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER"); |
|
|
1996 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
1997 | for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); |
|
|
1998 | RETVAL = INVOKE_((event_type)event, ARG_PLAYER (pl), ARG_AV (av)); |
|
|
1999 | OUTPUT: RETVAL |
|
|
2000 | |
1771 | SV *registry (player *pl) |
2001 | SV *registry (player *pl) |
1772 | CODE: |
2002 | CODE: |
1773 | RETVAL = registry_of (pl); |
2003 | RETVAL = registry_of (pl); |
1774 | OUTPUT: |
2004 | OUTPUT: |
1775 | RETVAL |
2005 | RETVAL |
… | |
… | |
1899 | OUTPUT: RETVAL |
2129 | OUTPUT: RETVAL |
1900 | |
2130 | |
1901 | |
2131 | |
1902 | MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ |
2132 | MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ |
1903 | |
2133 | |
|
|
2134 | mapstruct *first () |
|
|
2135 | PROTOTYPE: |
|
|
2136 | CODE: |
|
|
2137 | RETVAL = first_map; |
|
|
2138 | OUTPUT: RETVAL |
|
|
2139 | |
|
|
2140 | mapstruct *next (mapstruct *map) |
|
|
2141 | PROTOTYPE: |
|
|
2142 | CODE: |
|
|
2143 | RETVAL = map->next; |
|
|
2144 | OUTPUT: RETVAL |
|
|
2145 | |
|
|
2146 | int invoke (mapstruct *map, int event, ...) |
|
|
2147 | CODE: |
|
|
2148 | if (KLASS_OF (event) != KLASS_MAP) croak ("event class must be MAP"); |
|
|
2149 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
2150 | for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); |
|
|
2151 | RETVAL = INVOKE_((event_type)event, ARG_MAP (map), ARG_AV (av)); |
|
|
2152 | OUTPUT: RETVAL |
|
|
2153 | |
1904 | SV *registry (mapstruct *map) |
2154 | SV *registry (mapstruct *map) |
1905 | CODE: |
2155 | CODE: |
1906 | RETVAL = registry_of (map); |
2156 | RETVAL = registry_of (map); |
1907 | OUTPUT: |
2157 | OUTPUT: |
1908 | RETVAL |
2158 | RETVAL |
… | |
… | |
1927 | |
2177 | |
1928 | mapstruct *new (int width, int height) |
2178 | mapstruct *new (int width, int height) |
1929 | PROTOTYPE: |
2179 | PROTOTYPE: |
1930 | CODE: |
2180 | CODE: |
1931 | { |
2181 | { |
1932 | int unused_type; |
2182 | RETVAL = get_empty_map (width, height); |
1933 | RETVAL = (mapstruct*) map_get_map (&unused_type, 0, width, height); |
|
|
1934 | } |
2183 | } |
1935 | OUTPUT: |
2184 | OUTPUT: |
1936 | RETVAL |
2185 | RETVAL |
1937 | |
2186 | |
1938 | void delete_map (mapstruct *map) |
2187 | void delete_map (mapstruct *map) |
… | |
… | |
1953 | XSRETURN_UNDEF; |
2202 | XSRETURN_UNDEF; |
1954 | RETVAL = map->tile_path [dir]; |
2203 | RETVAL = map->tile_path [dir]; |
1955 | OUTPUT: |
2204 | OUTPUT: |
1956 | RETVAL |
2205 | RETVAL |
1957 | |
2206 | |
1958 | mapstruct *cf_map_get_map (char *name) |
2207 | mapstruct *ready_map_name (char *name, int flags = 0) |
1959 | PROTOTYPE: $ |
2208 | PROTOTYPE: $;$ |
1960 | ALIAS: map = 0 |
2209 | ALIAS: |
|
|
2210 | find = 0 |
|
|
2211 | get_map = 1 |
1961 | |
2212 | |
1962 | mapstruct *has_been_loaded (char *name) |
2213 | mapstruct *has_been_loaded (char *name) |
1963 | PROTOTYPE: $ |
2214 | PROTOTYPE: $ |
1964 | |
|
|
1965 | mapstruct *cf_map_get_first () |
|
|
1966 | PROTOTYPE: |
|
|
1967 | ALIAS: first = 0 |
|
|
1968 | |
2215 | |
1969 | # whoever "designed" the plug-in api should have wasted |
2216 | # whoever "designed" the plug-in api should have wasted |
1970 | # his/her time with staying away from the project - would have |
2217 | # his/her time with staying away from the project - would have |
1971 | # saved others a lot of time, without doubt. |
2218 | # saved others a lot of time, without doubt. |
1972 | void set_path (mapstruct *where, char *path) |
2219 | void set_path (mapstruct *where, char *path) |
… | |
… | |
1987 | |
2234 | |
1988 | void set_unique (mapstruct *map, bool unique) |
2235 | void set_unique (mapstruct *map, bool unique) |
1989 | CODE: |
2236 | CODE: |
1990 | map->unique = unique; |
2237 | map->unique = unique; |
1991 | |
2238 | |
|
|
2239 | void |
|
|
2240 | trigger (mapstruct *map, long connection, bool state = true) |
|
|
2241 | CODE: |
|
|
2242 | activate_connection (map, connection, state); |
|
|
2243 | |
|
|
2244 | void |
|
|
2245 | get_connection (mapstruct *map, long connection) |
|
|
2246 | PPCODE: |
|
|
2247 | oblinkpt *obp = get_connection_links (map, connection); |
|
|
2248 | if (obp) |
|
|
2249 | for (objectlink *ol = obp->link; ol; ol = ol->next) |
|
|
2250 | XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, ol->ob))); |
|
|
2251 | |
1992 | object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags) |
2252 | object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags) |
1993 | |
2253 | |
1994 | object *cf_map_insert_object (mapstruct *where, object* op, int x, int y) |
2254 | object *cf_map_insert_object (mapstruct *where, object* op, int x, int y) |
1995 | |
2255 | |
1996 | object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny) |
2256 | object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny) |
… | |
… | |
2000 | cf_map_normalise (mapstruct *map, int x, int y) |
2260 | cf_map_normalise (mapstruct *map, int x, int y) |
2001 | PPCODE: |
2261 | PPCODE: |
2002 | { |
2262 | { |
2003 | mapstruct *nmap = 0; |
2263 | mapstruct *nmap = 0; |
2004 | I16 nx = 0, ny = 0; |
2264 | I16 nx = 0, ny = 0; |
2005 | int flags = cf_map_get_flags (map, &nmap, x, y, &nx, &ny); |
2265 | int flags = get_map_flags (map, &nmap, x, y, &nx, &ny); |
2006 | |
2266 | |
2007 | EXTEND (SP, 4); |
2267 | EXTEND (SP, 4); |
2008 | PUSHs (sv_2mortal (newSViv (flags))); |
2268 | PUSHs (sv_2mortal (newSViv (flags))); |
2009 | |
2269 | |
2010 | if (GIMME_V == G_ARRAY) |
2270 | if (GIMME_V == G_ARRAY) |
… | |
… | |
2022 | { |
2282 | { |
2023 | object *o; |
2283 | object *o; |
2024 | mapstruct *nmap = 0; |
2284 | mapstruct *nmap = 0; |
2025 | I16 nx, ny; |
2285 | I16 nx, ny; |
2026 | |
2286 | |
2027 | cf_map_get_flags (map, &nmap, x, y, &nx, &ny); |
2287 | get_map_flags (map, &nmap, x, y, &nx, &ny); |
2028 | |
2288 | |
2029 | if (nmap) |
2289 | if (nmap) |
2030 | for (o = GET_MAP_OB (nmap, nx, ny); o; o = o->above) |
2290 | for (o = GET_MAP_OB (nmap, nx, ny); o; o = o->above) |
2031 | XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o))); |
2291 | XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o))); |
2032 | } |
2292 | } |
… | |
… | |
2057 | case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; |
2317 | case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; |
2058 | } |
2318 | } |
2059 | OUTPUT: |
2319 | OUTPUT: |
2060 | RETVAL |
2320 | RETVAL |
2061 | |
2321 | |
2062 | # "serialise" map perl data into a ref |
|
|
2063 | void |
|
|
2064 | _get_obs (mapstruct *map) |
|
|
2065 | PPCODE: |
|
|
2066 | { |
|
|
2067 | object *o; |
|
|
2068 | int x, y; |
|
|
2069 | AV *obs = newAV (); |
|
|
2070 | int nonnull = 0; |
|
|
2071 | |
2322 | |
2072 | for (y = 0; y < MAP_HEIGHT (map); y++) |
2323 | MODULE = cf PACKAGE = cf::arch |
2073 | for (x = 0; x < MAP_WIDTH (map); x++) |
|
|
2074 | { |
|
|
2075 | AV *av = newAV (); |
|
|
2076 | |
2324 | |
2077 | for (o = GET_MAP_OB (map, x, y); o; o = o->above) |
2325 | archetype *first() |
2078 | { |
|
|
2079 | SV *sv = SVptr_cache_get (o); |
|
|
2080 | |
|
|
2081 | if (sv && HvFILL (SvRV (sv))) |
|
|
2082 | { |
|
|
2083 | nonnull = 1; |
|
|
2084 | sv = newSVsv (sv); |
|
|
2085 | } |
|
|
2086 | else |
|
|
2087 | sv = &PL_sv_undef; |
|
|
2088 | |
|
|
2089 | av_push (av, sv); |
|
|
2090 | } |
|
|
2091 | |
|
|
2092 | av_store (obs, x + y * MAP_HEIGHT (map), newRV_noinc ((SV *)av)); |
|
|
2093 | } |
|
|
2094 | |
|
|
2095 | if (nonnull) |
|
|
2096 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)obs))); |
|
|
2097 | else |
|
|
2098 | SvREFCNT_dec (obs); |
|
|
2099 | } |
|
|
2100 | |
|
|
2101 | # "deserialise" perl map data into the map |
|
|
2102 | void |
|
|
2103 | _set_obs (mapstruct *map, SV *sv) |
|
|
2104 | CODE: |
|
|
2105 | { |
|
|
2106 | object *o; |
|
|
2107 | AV *av; |
|
|
2108 | int x, y; |
|
|
2109 | AV *obs = (AV *)SvRV (sv); |
|
|
2110 | |
|
|
2111 | for (y = 0; y < MAP_HEIGHT (map); y++) |
|
|
2112 | for (x = 0; x < MAP_WIDTH (map); x++) |
|
|
2113 | { |
|
|
2114 | sv = *av_fetch (obs, x + y * MAP_HEIGHT (map), 1); |
|
|
2115 | |
|
|
2116 | if (!SvROK (sv)) |
|
|
2117 | continue; |
|
|
2118 | |
|
|
2119 | av = (AV *)SvRV (sv); |
|
|
2120 | |
|
|
2121 | for (o = GET_MAP_OB (map, x, y); o; o = o->above) |
|
|
2122 | { |
|
|
2123 | sv = av_shift (av); |
|
|
2124 | |
|
|
2125 | if (SvROK (sv)) |
|
|
2126 | { |
|
|
2127 | sv_magic ((SV *)SvRV (sv), 0, PERL_MAGIC_ext, (char *)o, 0); |
|
|
2128 | SVptr_cache_set (o, sv); |
|
|
2129 | } |
|
|
2130 | } |
|
|
2131 | } |
|
|
2132 | } |
|
|
2133 | |
|
|
2134 | |
|
|
2135 | MODULE = cf PACKAGE = cf::arch PREFIX = cf_archetype_ |
|
|
2136 | |
|
|
2137 | archetype *cf_archetype_get_first() |
|
|
2138 | PROTOTYPE: |
2326 | PROTOTYPE: |
2139 | ALIAS: first = 0 |
2327 | CODE: |
|
|
2328 | RETVAL = first_archetype; |
|
|
2329 | OUTPUT: RETVAL |
2140 | |
2330 | |
2141 | archetype *cf_archetype_get_next (archetype *arch) |
2331 | archetype *next (archetype *arch) |
2142 | ALIAS: next = 0 |
2332 | CODE: |
|
|
2333 | RETVAL = arch->next; |
|
|
2334 | OUTPUT: RETVAL |
2143 | |
2335 | |
2144 | archetype *cf_archetype_get_head (archetype *arch) |
2336 | archetype *head (archetype *arch) |
2145 | ALIAS: head = 0 |
2337 | CODE: |
|
|
2338 | RETVAL = arch->head; |
|
|
2339 | OUTPUT: RETVAL |
2146 | |
2340 | |
2147 | archetype *cf_archetype_get_more (archetype *arch) |
2341 | archetype *more (archetype *arch) |
2148 | ALIAS: more = 0 |
2342 | CODE: |
|
|
2343 | RETVAL = arch->more; |
|
|
2344 | OUTPUT: RETVAL |
2149 | |
2345 | |
2150 | const char *cf_archetype_get_name (archetype *arch) |
2346 | const char *name (archetype *arch) |
2151 | ALIAS: name = 0 |
2347 | CODE: |
|
|
2348 | RETVAL = arch->name; |
|
|
2349 | OUTPUT: RETVAL |
2152 | |
2350 | |
2153 | object *cf_archetype_get_clone (archetype *arch) |
2351 | object *clone (archetype *arch) |
2154 | ALIAS: clone = 0 |
2352 | CODE: |
|
|
2353 | RETVAL = &arch->clone; |
|
|
2354 | OUTPUT: RETVAL |
2155 | |
2355 | |
2156 | MODULE = cf PACKAGE = cf::party PREFIX = cf_party_ |
2356 | MODULE = cf PACKAGE = cf::party |
2157 | |
2357 | |
2158 | partylist *cf_party_get_first () |
2358 | partylist *first () |
2159 | PROTOTYPE: |
2359 | PROTOTYPE: |
2160 | ALIAS: first = 0 |
2360 | CODE: |
|
|
2361 | RETVAL = get_firstparty (); |
|
|
2362 | OUTPUT: RETVAL |
2161 | |
2363 | |
2162 | partylist *cf_party_get_next (partylist *party) |
2364 | partylist *next (partylist *party) |
2163 | ALIAS: next = 0 |
2365 | CODE: |
|
|
2366 | RETVAL = party->next; |
|
|
2367 | OUTPUT: RETVAL |
2164 | |
2368 | |
2165 | const char *cf_party_get_name (partylist *party) |
2369 | const char *name (partylist *party) |
|
|
2370 | CODE: |
|
|
2371 | RETVAL = party->partyname; |
|
|
2372 | OUTPUT: RETVAL |
2166 | |
2373 | |
2167 | const char *cf_party_get_password (partylist *party) |
2374 | const char *password (partylist *party) |
2168 | ALIAS: password = 0 |
2375 | CODE: |
|
|
2376 | RETVAL = party->passwd; |
|
|
2377 | OUTPUT: RETVAL |
2169 | |
2378 | |
2170 | player *cf_party_get_first_player (partylist *party) |
2379 | MODULE = cf PACKAGE = cf::region |
2171 | ALIAS: first_player = 0 |
|
|
2172 | |
2380 | |
2173 | player *cf_party_get_next_player (partylist *party, player *op) |
2381 | region *first () |
2174 | ALIAS: next_player = 0 |
|
|
2175 | |
|
|
2176 | |
|
|
2177 | MODULE = cf PACKAGE = cf::region PREFIX = cf_region_ |
|
|
2178 | |
|
|
2179 | region *cf_region_get_first () |
|
|
2180 | PROTOTYPE: |
2382 | PROTOTYPE: |
2181 | ALIAS: first = 0 |
2383 | CODE: |
|
|
2384 | RETVAL = first_region; |
|
|
2385 | OUTPUT: RETVAL |
2182 | |
2386 | |
|
|
2387 | region *next (region *reg) |
|
|
2388 | CODE: |
|
|
2389 | RETVAL = reg->next; |
|
|
2390 | OUTPUT: RETVAL |
|
|
2391 | |
|
|
2392 | const char *name (region *reg) |
|
|
2393 | CODE: |
|
|
2394 | RETVAL = reg->name; |
|
|
2395 | OUTPUT: RETVAL |
|
|
2396 | |
|
|
2397 | region *parent (region *reg) |
|
|
2398 | CODE: |
|
|
2399 | RETVAL = reg->parent; |
|
|
2400 | OUTPUT: RETVAL |
|
|
2401 | |
2183 | const char *cf_region_get_name (region *reg) |
2402 | const char *longname (region *reg) |
2184 | ALIAS: name = 0 |
2403 | CODE: |
|
|
2404 | RETVAL = reg->longname; |
|
|
2405 | OUTPUT: RETVAL |
2185 | |
2406 | |
2186 | region *cf_region_get_next (region *reg) |
2407 | const char *msg (region *reg) |
2187 | ALIAS: next = 0 |
2408 | CODE: |
|
|
2409 | RETVAL = reg->msg; |
|
|
2410 | OUTPUT: RETVAL |
2188 | |
2411 | |
2189 | region *cf_region_get_parent (region *reg) |
2412 | MODULE = cf PACKAGE = cf::living |
2190 | ALIAS: parent = 0 |
|
|
2191 | |
|
|
2192 | const char *cf_region_get_longname (region *reg) |
|
|
2193 | ALIAS: longname = 0 |
|
|
2194 | |
|
|
2195 | const char *cf_region_get_message (region *reg) |
|
|
2196 | ALIAS: message = 0 |
|
|
2197 | |
|
|
2198 | |
|
|
2199 | MODULE = cf PACKAGE = cf::living PREFIX = cf_living_ |
|
|
2200 | |
2413 | |
2201 | val64 |
2414 | val64 |
2202 | exp (living *liv, val64 new_val = 0) |
2415 | exp (living *liv, val64 new_val = 0) |
2203 | PROTOTYPE: $;$ |
2416 | PROTOTYPE: $;$ |
2204 | ALIAS: |
2417 | ALIAS: |