… | |
… | |
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.5" |
27 | #define PLUGIN_VERSION "cfperl 0.5" |
34 | |
|
|
35 | #ifndef __CEXTRACT__ |
|
|
36 | #include <plugin.h> |
|
|
37 | #endif |
|
|
38 | |
|
|
39 | #undef MODULEAPI |
|
|
40 | #ifdef WIN32 |
|
|
41 | #else |
|
|
42 | #define MODULEAPI |
|
|
43 | #endif |
|
|
44 | |
28 | |
45 | #include <plugin_common.h> |
29 | #include <plugin_common.h> |
46 | #include <sounds.h> |
30 | #include <sounds.h> |
47 | #include <cstdarg> |
31 | #include <cstdarg> |
48 | #include <sproto.h> |
32 | #include <sproto.h> |
49 | |
33 | |
50 | #include "cfperl.h" |
34 | #include "cfperl.h" |
51 | #include "shstr.h" |
35 | #include "shstr.h" |
52 | |
36 | |
|
|
37 | #include <EXTERN.h> |
|
|
38 | #include <perl.h> |
|
|
39 | #include <XSUB.h> |
|
|
40 | |
53 | #include "perlxsi.c" |
41 | #include "perlxsi.c" |
54 | |
42 | |
55 | extern sint64 *levels; // the experience table |
43 | extern sint64 *levels; // the experience table |
56 | |
44 | |
57 | typedef object object_ornull; |
45 | typedef object object_ornull; |
… | |
… | |
61 | #define newSVval64 newSVnv |
49 | #define newSVval64 newSVnv |
62 | #define SvVAL64 SvNV |
50 | #define SvVAL64 SvNV |
63 | |
51 | |
64 | static f_plug_api gethook = cfapi_get_hooks; |
52 | static f_plug_api gethook = cfapi_get_hooks; |
65 | static f_plug_api object_set_property = cfapi_object_set_property; |
53 | static f_plug_api object_set_property = cfapi_object_set_property; |
66 | static f_plug_api map_get_map = cfapi_map_get_map; |
|
|
67 | static f_plug_api object_insert = cfapi_object_insert; |
54 | static f_plug_api object_insert = cfapi_object_insert; |
68 | |
55 | |
69 | /* this is a stupid way to do things, and awkward to use for plug-in authors */ |
56 | /* this is a stupid way to do things, and awkward to use for plug-in authors */ |
70 | typedef struct |
57 | typedef struct |
71 | { |
58 | { |
… | |
… | |
117 | return &PL_sv_undef; |
104 | return &PL_sv_undef; |
118 | |
105 | |
119 | if (!obj->self) |
106 | if (!obj->self) |
120 | obj->self = newSVptr (obj, klass); |
107 | obj->self = newSVptr (obj, klass); |
121 | |
108 | |
122 | return newSVsv (static_cast<SV *>(obj->self)); |
109 | return newSVsv (obj->self); |
123 | } |
110 | } |
124 | |
111 | |
125 | static void |
112 | static void |
126 | SVptr_cache_set (void *ptr, SV *sv) |
113 | SVptr_cache_set (void *ptr, SV *sv) |
127 | { |
114 | { |
… | |
… | |
316 | void attachable_base::clear () |
303 | void attachable_base::clear () |
317 | { |
304 | { |
318 | if (self) |
305 | if (self) |
319 | { |
306 | { |
320 | if (cb) |
307 | if (cb) |
321 | if (SvROK (*av_fetch ((AV *)cb, EVENT_OBJECT_DESTROY, 1))) |
308 | if (SvROK (*av_fetch (cb, EVENT_OBJECT_DESTROY, 1))) |
322 | INVOKE_OBJECT (DESTROY, static_cast<object *>(this)); |
309 | INVOKE_OBJECT (DESTROY, static_cast<object *>(this)); |
323 | else if (SvROK (*av_fetch ((AV *)cb, EVENT_MAP_DESTROY, 1))) |
310 | else if (SvROK (*av_fetch (cb, EVENT_MAP_DESTROY, 1))) |
324 | INVOKE_MAP (DESTROY, static_cast<mapstruct *>(this)); |
311 | INVOKE_MAP (DESTROY, static_cast<mapstruct *>(this)); |
325 | |
312 | |
326 | // disconnect Perl from C, to avoid crashes |
313 | // disconnect Perl from C, to avoid crashes |
327 | sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext); |
314 | sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext); |
328 | |
315 | |
… | |
… | |
337 | { |
324 | { |
338 | SvREFCNT_dec (cb); |
325 | SvREFCNT_dec (cb); |
339 | cb = 0; |
326 | cb = 0; |
340 | } |
327 | } |
341 | |
328 | |
342 | if (attach) |
|
|
343 | { |
|
|
344 | free_string (attach); |
|
|
345 | attach = 0; |
329 | attach = 0; |
346 | } |
|
|
347 | } |
330 | } |
348 | |
331 | |
349 | void attachable_base::optimise () |
332 | void attachable_base::optimise () |
350 | { |
333 | { |
351 | if (!self) |
334 | if (!self) |
… | |
… | |
370 | PUSHMARK (SP); |
353 | PUSHMARK (SP); |
371 | EXTEND (SP, 2); |
354 | EXTEND (SP, 2); |
372 | PUSHs (sv_2mortal (newSVdt (type, obj))); |
355 | PUSHs (sv_2mortal (newSVdt (type, obj))); |
373 | PUSHs (sv_2mortal (newSVpv (attach, 0))); |
356 | PUSHs (sv_2mortal (newSVpv (attach, 0))); |
374 | |
357 | |
375 | free_string (attach); |
|
|
376 | attach = 0; |
358 | attach = 0; |
377 | |
359 | |
378 | PUTBACK; |
360 | PUTBACK; |
379 | call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL); |
361 | call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL); |
380 | FREETMPS; |
362 | FREETMPS; |
… | |
… | |
415 | void reattach (attachable<subclass> *obj) |
397 | void reattach (attachable<subclass> *obj) |
416 | { |
398 | { |
417 | obj->optimise (); |
399 | obj->optimise (); |
418 | |
400 | |
419 | if (obj->self) |
401 | if (obj->self) |
420 | reattach (subclass::get_dt (), (subclass *)obj); |
402 | reattach ((data_type) cftype<subclass>::dt, (subclass *)obj); |
421 | } |
403 | } |
422 | |
404 | |
|
|
405 | #include "kw_hash.h" |
|
|
406 | |
423 | object_freezer::object_freezer (const char *filename) |
407 | object_freezer::object_freezer () |
|
|
408 | : dynbuf (128 * 1024, 64 * 1024) |
424 | { |
409 | { |
425 | this->filename = (SV *)newSVpv (filename, 0); |
|
|
426 | |
|
|
427 | char filename2 [4096]; |
|
|
428 | snprintf (filename2, 4096, "%s~", filename); |
|
|
429 | |
|
|
430 | fp = fopen (filename2, "w"); |
|
|
431 | |
|
|
432 | if (!fp) |
|
|
433 | LOG (llevError, "cannot open file '%s' for writing: %s\n", filename2, strerror (errno)); |
|
|
434 | |
|
|
435 | av = (AV *)newAV (); |
410 | av = newAV (); |
436 | } |
411 | } |
437 | |
412 | |
438 | object_freezer::~object_freezer () |
413 | object_freezer::~object_freezer () |
439 | { |
414 | { |
440 | if (fp) |
415 | SvREFCNT_dec (av); |
|
|
416 | } |
|
|
417 | |
|
|
418 | void object_freezer::put (attachable_base *ext) |
|
|
419 | { |
|
|
420 | ext->optimise (); |
|
|
421 | |
|
|
422 | if (ext->self) |
441 | { |
423 | { |
442 | fclose (fp); |
424 | int idx = AvFILLp ((AV *)av) + 1; |
|
|
425 | av_store (av, idx, SvREFCNT_inc (ext->self)); |
443 | |
426 | |
|
|
427 | add ((void *)"oid ", 4); |
|
|
428 | add ((sint32)idx); |
|
|
429 | add ('\n'); |
|
|
430 | } |
|
|
431 | } |
|
|
432 | |
|
|
433 | bool object_freezer::save (const char *filename) |
|
|
434 | { |
|
|
435 | dSP; |
|
|
436 | ENTER; |
|
|
437 | SAVETMPS; |
|
|
438 | PUSHMARK (SP); |
|
|
439 | EXTEND (SP, 3); |
|
|
440 | PUSHs (sv_2mortal (newSVpv (filename, 0))); |
|
|
441 | PUSHs (sv_2mortal (newRV_noinc (newSVpvn ((char *)linearise (), size ())))); |
|
|
442 | PUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
443 | PUTBACK; |
|
|
444 | call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL); |
|
|
445 | FREETMPS; |
|
|
446 | LEAVE; |
|
|
447 | } |
|
|
448 | |
|
|
449 | int fprintf (object_freezer &freezer, const char *format, ...) |
|
|
450 | { |
|
|
451 | va_list ap; |
|
|
452 | |
|
|
453 | va_start (ap, format); |
|
|
454 | |
|
|
455 | int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap); |
|
|
456 | |
|
|
457 | if (len >= 0) |
|
|
458 | freezer.alloc (len); |
|
|
459 | |
|
|
460 | va_end (ap); |
|
|
461 | } |
|
|
462 | |
|
|
463 | int fputs (const char *s, object_freezer &freezer) |
|
|
464 | { |
|
|
465 | freezer.add (s); |
|
|
466 | } |
|
|
467 | |
|
|
468 | object_thawer::object_thawer (const char *filename) |
|
|
469 | { |
|
|
470 | static const char eof[] = "\n\n\n\0\0\0"; |
|
|
471 | |
|
|
472 | av = 0; |
|
|
473 | text = 0; |
|
|
474 | line = 0; |
|
|
475 | |
|
|
476 | if (filename) |
|
|
477 | { |
444 | dSP; |
478 | dSP; |
445 | ENTER; |
479 | ENTER; |
446 | SAVETMPS; |
480 | SAVETMPS; |
447 | PUSHMARK (SP); |
481 | PUSHMARK (SP); |
448 | XPUSHs (sv_2mortal ((SV *)filename)); |
482 | XPUSHs (sv_2mortal (newSVpv (filename, 0))); |
449 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
450 | PUTBACK; |
483 | PUTBACK; |
451 | call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL); |
484 | |
|
|
485 | if (2 == call_pv ("cf::object_thawer_load", G_ARRAY | G_EVAL)) |
|
|
486 | { |
|
|
487 | SPAGAIN; |
|
|
488 | |
|
|
489 | // second value - perl objects |
|
|
490 | { |
|
|
491 | SV *sv = POPs; |
|
|
492 | if (SvROK (sv)) |
|
|
493 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
|
|
494 | } |
|
|
495 | |
|
|
496 | // first value - text part, pad with 3 zeroes |
|
|
497 | { |
|
|
498 | SV *sv = POPs; |
|
|
499 | STRLEN len; |
|
|
500 | char *sv_ = SvPVbyte (sv, len); |
|
|
501 | text = newSV (len + sizeof (eof)); |
|
|
502 | SvCUR_set (text, len); |
|
|
503 | memcpy (SvPVX (text), sv_, len); |
|
|
504 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
|
|
505 | |
|
|
506 | line = SvPVX (text); |
|
|
507 | } |
|
|
508 | } |
|
|
509 | |
|
|
510 | PUTBACK; |
452 | FREETMPS; |
511 | FREETMPS; |
453 | LEAVE; |
512 | LEAVE; |
454 | } |
513 | } |
455 | else |
|
|
456 | SvREFCNT_dec ((SV *)filename); |
|
|
457 | } |
|
|
458 | |
|
|
459 | void object_freezer::put (attachable_base *ext) |
|
|
460 | { |
|
|
461 | ext->optimise (); |
|
|
462 | |
|
|
463 | if (ext->self) |
|
|
464 | { |
|
|
465 | int idx = AvFILLp ((AV *)av) + 1; |
|
|
466 | av_store ((AV *)av, idx, SvREFCNT_inc ((SV *)ext->self)); |
|
|
467 | fprintf (fp, "oid %d\n", idx); |
|
|
468 | } |
|
|
469 | } |
|
|
470 | |
|
|
471 | object_thawer::object_thawer (FILE *fp, const char *filename) |
|
|
472 | { |
|
|
473 | this->fp = fp; |
|
|
474 | av = 0; |
|
|
475 | |
|
|
476 | if (!filename) |
|
|
477 | return; |
|
|
478 | |
|
|
479 | dSP; |
|
|
480 | ENTER; |
|
|
481 | SAVETMPS; |
|
|
482 | PUSHMARK (SP); |
|
|
483 | XPUSHs (sv_2mortal (newSVpv (filename, 0))); |
|
|
484 | PUTBACK; |
|
|
485 | |
|
|
486 | if (0 < call_pv ("cf::object_thawer_load", G_SCALAR | G_EVAL)) |
|
|
487 | { |
|
|
488 | SPAGAIN; |
|
|
489 | SV *sv = POPs; |
|
|
490 | if (SvROK (sv)) |
|
|
491 | av = SvREFCNT_inc (SvRV (sv)); |
|
|
492 | } |
|
|
493 | |
|
|
494 | FREETMPS; |
|
|
495 | LEAVE; |
|
|
496 | } |
514 | } |
497 | |
515 | |
498 | void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid) |
516 | void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid) |
499 | { |
517 | { |
500 | if (!av || oid < 0) // this is actually an error of sorts |
518 | if (!av || oid < 0) // this is actually an error of sorts |
… | |
… | |
510 | printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
528 | printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
511 | return; |
529 | return; |
512 | } |
530 | } |
513 | |
531 | |
514 | ext->self = *svp; *svp = &PL_sv_undef; |
532 | ext->self = *svp; *svp = &PL_sv_undef; |
515 | sv_magic (SvRV ((SV *)ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0); |
533 | sv_magic (SvRV (ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0); |
516 | |
534 | |
517 | reattach (type, obj); |
535 | reattach (type, obj); |
518 | } |
536 | } |
519 | |
537 | |
520 | object_thawer::~object_thawer () |
538 | object_thawer::~object_thawer () |
521 | { |
539 | { |
|
|
540 | if (text) SvREFCNT_dec (text); |
522 | if (av) SvREFCNT_dec ((AV *)av); |
541 | if (av) SvREFCNT_dec (av); |
|
|
542 | } |
|
|
543 | |
|
|
544 | char *fgets (char *s, int n, object_thawer &thawer) |
|
|
545 | { |
|
|
546 | char *p = thawer.line; |
|
|
547 | char *q = s; |
|
|
548 | |
|
|
549 | if (!p) |
|
|
550 | return 0; |
|
|
551 | |
|
|
552 | while (--n) |
|
|
553 | { |
|
|
554 | if (!*p) |
|
|
555 | break; |
|
|
556 | |
|
|
557 | *q++ = *p; |
|
|
558 | |
|
|
559 | if (*p++ == '\n') |
|
|
560 | break; |
|
|
561 | } |
|
|
562 | |
|
|
563 | *q = 0; |
|
|
564 | thawer.line = p; |
|
|
565 | |
|
|
566 | return s == q ? 0 : s; |
|
|
567 | } |
|
|
568 | |
|
|
569 | keyword object_thawer::get_kv () |
|
|
570 | { |
|
|
571 | if (!line) |
|
|
572 | return KW_EOF; |
|
|
573 | |
|
|
574 | for (;;) |
|
|
575 | { |
|
|
576 | char *p = line; |
|
|
577 | |
|
|
578 | if (!*p) |
|
|
579 | return KW_EOF; |
|
|
580 | |
|
|
581 | // parse keyword |
|
|
582 | while (*p > ' ') |
|
|
583 | p++; |
|
|
584 | |
|
|
585 | int klen = p - line; |
|
|
586 | |
|
|
587 | if (*p++ != '\n') |
|
|
588 | { |
|
|
589 | // parse value |
|
|
590 | while (*p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 |
|
|
591 | ++p; |
|
|
592 | |
|
|
593 | last_value = p; |
|
|
594 | |
|
|
595 | while (*p != '\n') |
|
|
596 | p++; |
|
|
597 | |
|
|
598 | *p++ = 0; |
|
|
599 | } |
|
|
600 | else |
|
|
601 | last_value = 0; |
|
|
602 | |
|
|
603 | line [klen] = 0; |
|
|
604 | keyword_idx *kw = kw_lex::match (line, klen); |
|
|
605 | |
|
|
606 | //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D |
|
|
607 | |
|
|
608 | if (kw) |
|
|
609 | { |
|
|
610 | line = p; |
|
|
611 | return kw->index; |
|
|
612 | } |
|
|
613 | else if (!*line || *line == '#') |
|
|
614 | { |
|
|
615 | // empty/comment line |
|
|
616 | line = p; |
|
|
617 | } |
|
|
618 | else |
|
|
619 | return KW_ERROR; |
|
|
620 | } |
|
|
621 | } |
|
|
622 | |
|
|
623 | void object_thawer::get (shstr &sh) const |
|
|
624 | { |
|
|
625 | if (last_value) |
|
|
626 | sh = last_value; |
|
|
627 | else |
|
|
628 | { |
|
|
629 | sh = "<value missing>"; |
|
|
630 | LOG (llevError, "keyword requires value: <%s>\n", line);//TODO: add filename |
|
|
631 | } |
|
|
632 | } |
|
|
633 | |
|
|
634 | void object_thawer::get_ml (keyword kend, shstr &sh) |
|
|
635 | { |
|
|
636 | char kw[128]; |
|
|
637 | |
|
|
638 | // multi-line strings are delimited by "\nendXXX\n" |
|
|
639 | kw [0] = '\n'; |
|
|
640 | strcpy (kw + 1, keyword_str [kend]); |
|
|
641 | |
|
|
642 | char *end = strstr (line, kw); |
|
|
643 | |
|
|
644 | if (!end) |
|
|
645 | { |
|
|
646 | sh = 0; |
|
|
647 | return; |
|
|
648 | } |
|
|
649 | |
|
|
650 | *end = 0; |
|
|
651 | sh = line; |
|
|
652 | |
|
|
653 | line = end + keyword_len [kend] + 1; |
|
|
654 | |
|
|
655 | while (*line++ != '\n') |
|
|
656 | ; |
|
|
657 | } |
|
|
658 | |
|
|
659 | sint32 object_thawer::get_sint32 () const |
|
|
660 | { |
|
|
661 | char *p = last_value; |
|
|
662 | |
|
|
663 | if (!p) |
|
|
664 | return 0; |
|
|
665 | |
|
|
666 | sint32 val = 0; |
|
|
667 | bool negate; |
|
|
668 | |
|
|
669 | if (*p == '-') |
|
|
670 | { |
|
|
671 | negate = true; |
|
|
672 | ++p; |
|
|
673 | } |
|
|
674 | else |
|
|
675 | negate = false; |
|
|
676 | |
|
|
677 | do |
|
|
678 | { |
|
|
679 | val *= 10; |
|
|
680 | val += *p++ - '0'; |
|
|
681 | } |
|
|
682 | while (*p); |
|
|
683 | |
|
|
684 | return negate ? -val : val; |
|
|
685 | } |
|
|
686 | |
|
|
687 | sint64 object_thawer::get_sint64 () const |
|
|
688 | { |
|
|
689 | return last_value ? atoll (last_value) : 0; |
|
|
690 | } |
|
|
691 | |
|
|
692 | double object_thawer::get_double () const |
|
|
693 | { |
|
|
694 | return last_value ? atof (last_value) : 0; |
523 | } |
695 | } |
524 | |
696 | |
525 | ///////////////////////////////////////////////////////////////////////////// |
697 | ///////////////////////////////////////////////////////////////////////////// |
526 | |
698 | |
527 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
699 | extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) |
… | |
… | |
626 | } |
798 | } |
627 | |
799 | |
628 | void |
800 | void |
629 | cfperl_init () |
801 | cfperl_init () |
630 | { |
802 | { |
|
|
803 | PERL_SYS_INIT3 (&settings.argc, &settings.argv, 0); |
|
|
804 | perl = perl_alloc (); |
|
|
805 | perl_construct (perl); |
|
|
806 | |
|
|
807 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
|
|
808 | |
631 | char *argv[] = { |
809 | char *argv[] = { |
632 | "", |
810 | "", |
633 | "-e" |
811 | "-e" |
634 | "BEGIN {" |
|
|
635 | " cf->bootstrap;" |
812 | "cf->bootstrap;" |
636 | " unshift @INC, cf::datadir ();" |
813 | "unshift @INC, cf::datadir ();" |
637 | "}" |
|
|
638 | "" |
|
|
639 | "use cf;" |
814 | "require cf;" |
640 | }; |
815 | }; |
641 | |
|
|
642 | perl = perl_alloc (); |
|
|
643 | perl_construct (perl); |
|
|
644 | |
|
|
645 | PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
|
|
646 | |
816 | |
647 | if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) |
817 | if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) |
648 | { |
818 | { |
649 | printf ("unable to initialize perl-interpreter, aborting.\n"); |
819 | printf ("unable to initialize perl-interpreter, aborting.\n"); |
650 | |
|
|
651 | exit (EXIT_FAILURE); |
820 | exit (EXIT_FAILURE); |
652 | //perl_destruct (perl); |
|
|
653 | //perl_free (perl); |
|
|
654 | //perl = 0; |
|
|
655 | //return; |
|
|
656 | } |
821 | } |
657 | |
822 | |
658 | obj_cache = newHV (); |
823 | obj_cache = newHV (); |
659 | } |
824 | } |
660 | |
825 | |
… | |
… | |
735 | dt = (data_type) va_arg (ap, int); |
900 | dt = (data_type) va_arg (ap, int); |
736 | assert (("first argument must be of type object", dt == DT_OBJECT)); |
901 | assert (("first argument must be of type object", dt == DT_OBJECT)); |
737 | op = va_arg (ap, object *); |
902 | op = va_arg (ap, object *); |
738 | |
903 | |
739 | if (op->cb) |
904 | if (op->cb) |
740 | gather_callbacks (callbacks, (AV *)op->cb, event); |
905 | gather_callbacks (callbacks, op->cb, event); |
741 | |
906 | |
742 | if (op->type) |
907 | if (op->type) |
743 | { |
908 | { |
744 | if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type)) |
909 | if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type)) |
745 | { |
910 | { |
… | |
… | |
766 | dt = (data_type) va_arg (ap, int); |
931 | dt = (data_type) va_arg (ap, int); |
767 | assert (("first argument must be of type player", dt == DT_PLAYER)); |
932 | assert (("first argument must be of type player", dt == DT_PLAYER)); |
768 | pl = va_arg (ap, player *); |
933 | pl = va_arg (ap, player *); |
769 | |
934 | |
770 | if (pl->cb) |
935 | if (pl->cb) |
771 | gather_callbacks (callbacks, (AV *)pl->cb, event); |
936 | gather_callbacks (callbacks, pl->cb, event); |
772 | |
937 | |
773 | gather_callbacks (callbacks, cb_player, event); |
938 | gather_callbacks (callbacks, cb_player, event); |
774 | break; |
939 | break; |
775 | |
940 | |
776 | case KLASS_MAP: |
941 | case KLASS_MAP: |
777 | dt = (data_type) va_arg (ap, int); |
942 | dt = (data_type) va_arg (ap, int); |
778 | assert (("first argument must be of type object", dt == DT_MAP)); |
943 | assert (("first argument must be of type object", dt == DT_MAP)); |
779 | map = va_arg (ap, mapstruct *); |
944 | map = va_arg (ap, mapstruct *); |
780 | |
945 | |
781 | if (map->cb) |
946 | if (map->cb) |
782 | gather_callbacks (callbacks, (AV *)map->cb, event); |
947 | gather_callbacks (callbacks, map->cb, event); |
783 | |
948 | |
784 | gather_callbacks (callbacks, cb_map, event); |
949 | gather_callbacks (callbacks, cb_map, event); |
785 | break; |
950 | break; |
786 | |
951 | |
787 | default: |
952 | default: |
… | |
… | |
1239 | |
1404 | |
1240 | const_iv (SK_EXP_ADD_SKILL) |
1405 | const_iv (SK_EXP_ADD_SKILL) |
1241 | const_iv (SK_EXP_TOTAL) |
1406 | const_iv (SK_EXP_TOTAL) |
1242 | const_iv (SK_EXP_NONE) |
1407 | const_iv (SK_EXP_NONE) |
1243 | const_iv (SK_SUBTRACT_SKILL_EXP) |
1408 | const_iv (SK_SUBTRACT_SKILL_EXP) |
|
|
1409 | const_iv (SK_EXP_SKILL_ONLY) |
1244 | |
1410 | |
1245 | const_iv (SK_LOCKPICKING) |
1411 | const_iv (SK_LOCKPICKING) |
1246 | const_iv (SK_HIDING) |
1412 | const_iv (SK_HIDING) |
1247 | const_iv (SK_SMITHERY) |
1413 | const_iv (SK_SMITHERY) |
1248 | const_iv (SK_BOWYER) |
1414 | const_iv (SK_BOWYER) |
… | |
… | |
1407 | prop (CFAPI_INT, MAP_PROP_WINDDIR) |
1573 | prop (CFAPI_INT, MAP_PROP_WINDDIR) |
1408 | prop (CFAPI_INT, MAP_PROP_SKY) |
1574 | prop (CFAPI_INT, MAP_PROP_SKY) |
1409 | prop (CFAPI_INT, MAP_PROP_WPARTX) |
1575 | prop (CFAPI_INT, MAP_PROP_WPARTX) |
1410 | prop (CFAPI_INT, MAP_PROP_WPARTY) |
1576 | prop (CFAPI_INT, MAP_PROP_WPARTY) |
1411 | prop (CFAPI_STRING, MAP_PROP_MESSAGE) |
1577 | prop (CFAPI_STRING, MAP_PROP_MESSAGE) |
1412 | prop (CFAPI_PMAP, MAP_PROP_NEXT) |
|
|
1413 | prop (CFAPI_PREGION, MAP_PROP_REGION) |
1578 | prop (CFAPI_PREGION, MAP_PROP_REGION) |
1414 | prop (CFAPI_POBJECT, OBJECT_PROP_OB_ABOVE) |
|
|
1415 | prop (CFAPI_POBJECT, OBJECT_PROP_OB_BELOW) |
|
|
1416 | prop (CFAPI_POBJECT, OBJECT_PROP_NEXT_ACTIVE_OB) |
1579 | prop (CFAPI_POBJECT, OBJECT_PROP_NEXT_ACTIVE_OB) |
1417 | prop (CFAPI_POBJECT, OBJECT_PROP_PREV_ACTIVE_OB) |
1580 | prop (CFAPI_POBJECT, OBJECT_PROP_PREV_ACTIVE_OB) |
1418 | prop (CFAPI_POBJECT, OBJECT_PROP_INVENTORY) |
1581 | prop (CFAPI_POBJECT, OBJECT_PROP_INVENTORY) |
1419 | prop (CFAPI_POBJECT, OBJECT_PROP_ENVIRONMENT) |
1582 | prop (CFAPI_POBJECT, OBJECT_PROP_ENVIRONMENT) |
1420 | prop (CFAPI_POBJECT, OBJECT_PROP_HEAD) |
|
|
1421 | prop (CFAPI_POBJECT, OBJECT_PROP_CONTAINER) |
1583 | prop (CFAPI_POBJECT, OBJECT_PROP_CONTAINER) |
1422 | prop (CFAPI_PMAP, OBJECT_PROP_MAP) |
1584 | prop (CFAPI_PMAP, OBJECT_PROP_MAP) |
1423 | prop (CFAPI_INT, OBJECT_PROP_COUNT) |
1585 | prop (CFAPI_INT, OBJECT_PROP_COUNT) |
1424 | prop (CFAPI_INT, OBJECT_PROP_REFCOUNT) |
1586 | prop (CFAPI_INT, OBJECT_PROP_REFCOUNT) |
1425 | prop (CFAPI_STRING, OBJECT_PROP_NAME) |
1587 | prop (CFAPI_STRING, OBJECT_PROP_NAME) |
… | |
… | |
1649 | RETVAL = newSVpv (resist_plus[atnr], 0); |
1811 | RETVAL = newSVpv (resist_plus[atnr], 0); |
1650 | else |
1812 | else |
1651 | XSRETURN_UNDEF; |
1813 | XSRETURN_UNDEF; |
1652 | OUTPUT: RETVAL |
1814 | OUTPUT: RETVAL |
1653 | |
1815 | |
|
|
1816 | int |
|
|
1817 | _valid (SV *obj) |
|
|
1818 | CODE: |
|
|
1819 | RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext); |
|
|
1820 | OUTPUT: |
|
|
1821 | RETVAL |
|
|
1822 | |
1654 | MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ |
1823 | MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ |
1655 | |
1824 | |
1656 | int invoke (object *op, int event, ...) |
1825 | int invoke (object *op, int event, ...) |
1657 | CODE: |
1826 | CODE: |
1658 | if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT"); |
1827 | if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT"); |
… | |
… | |
1663 | |
1832 | |
1664 | SV *registry (object *op) |
1833 | SV *registry (object *op) |
1665 | CODE: |
1834 | CODE: |
1666 | RETVAL = registry_of (op); |
1835 | RETVAL = registry_of (op); |
1667 | OUTPUT: |
1836 | OUTPUT: |
|
|
1837 | RETVAL |
|
|
1838 | |
|
|
1839 | object *head (object *op) |
|
|
1840 | PROTOTYPE: $ |
|
|
1841 | ALIAS: |
|
|
1842 | more = 1 |
|
|
1843 | above = 2 |
|
|
1844 | below = 3 |
|
|
1845 | CODE: |
|
|
1846 | switch (ix) |
|
|
1847 | { |
|
|
1848 | case 0: RETVAL = op->head ? op->head : op; break; // DOH! |
|
|
1849 | case 1: RETVAL = op->more; break; |
|
|
1850 | case 2: RETVAL = op->above; break; |
|
|
1851 | case 3: RETVAL = op->below; break; |
|
|
1852 | } |
|
|
1853 | OUTPUT: |
1668 | RETVAL |
1854 | RETVAL |
1669 | |
1855 | |
1670 | SV * |
1856 | SV * |
1671 | get_property (object *obj, int type, int idx) |
1857 | get_property (object *obj, int type, int idx) |
1672 | CODE: |
1858 | CODE: |
… | |
… | |
1752 | |
1938 | |
1753 | int need_identify (const object *obj); |
1939 | int need_identify (const object *obj); |
1754 | |
1940 | |
1755 | int apply_shop_mat (object *shop_mat, object *op); |
1941 | int apply_shop_mat (object *shop_mat, object *op); |
1756 | |
1942 | |
1757 | void cf_object_move (object *op, int dir, object *originator = op) |
1943 | int move (object *op, int dir, object *originator = op) |
|
|
1944 | CODE: |
|
|
1945 | RETVAL = move_ob (op, dir, originator); |
|
|
1946 | OUTPUT: |
|
|
1947 | RETVAL |
1758 | |
1948 | |
1759 | void cf_object_apply (object *op, object *author, int flags = 0) |
1949 | void cf_object_apply (object *op, object *author, int flags = 0) |
1760 | |
1950 | |
1761 | void cf_object_apply_below (object *op) |
1951 | void cf_object_apply_below (object *op) |
1762 | |
1952 | |
… | |
… | |
1803 | int cf_object_out_of_map (object *op, int x, int y) |
1993 | int cf_object_out_of_map (object *op, int x, int y) |
1804 | |
1994 | |
1805 | void cf_object_drop (object *op, object *author) |
1995 | void cf_object_drop (object *op, object *author) |
1806 | |
1996 | |
1807 | void cf_object_take (object *op, object *author) |
1997 | void cf_object_take (object *op, object *author) |
1808 | |
|
|
1809 | void cf_object_say (object *op, char *msg) |
|
|
1810 | |
|
|
1811 | void cf_object_speak (object *op, char *msg) |
|
|
1812 | |
1998 | |
1813 | object *cf_object_insert_object (object *op, object *container) |
1999 | object *cf_object_insert_object (object *op, object *container) |
1814 | |
2000 | |
1815 | const char *cf_object_get_msg (object *ob) |
2001 | const char *cf_object_get_msg (object *ob) |
1816 | ALIAS: msg = 0 |
2002 | ALIAS: msg = 0 |
… | |
… | |
2093 | OUTPUT: RETVAL |
2279 | OUTPUT: RETVAL |
2094 | |
2280 | |
2095 | |
2281 | |
2096 | MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ |
2282 | MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ |
2097 | |
2283 | |
|
|
2284 | mapstruct *first () |
|
|
2285 | PROTOTYPE: |
|
|
2286 | CODE: |
|
|
2287 | RETVAL = first_map; |
|
|
2288 | OUTPUT: RETVAL |
|
|
2289 | |
|
|
2290 | mapstruct *next (mapstruct *map) |
|
|
2291 | PROTOTYPE: |
|
|
2292 | CODE: |
|
|
2293 | RETVAL = map->next; |
|
|
2294 | OUTPUT: RETVAL |
|
|
2295 | |
2098 | int invoke (mapstruct *map, int event, ...) |
2296 | int invoke (mapstruct *map, int event, ...) |
2099 | CODE: |
2297 | CODE: |
2100 | if (KLASS_OF (event) != KLASS_MAP) croak ("event class must be MAP"); |
2298 | if (KLASS_OF (event) != KLASS_MAP) croak ("event class must be MAP"); |
2101 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
2299 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
2102 | for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); |
2300 | for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); |
… | |
… | |
2129 | |
2327 | |
2130 | mapstruct *new (int width, int height) |
2328 | mapstruct *new (int width, int height) |
2131 | PROTOTYPE: |
2329 | PROTOTYPE: |
2132 | CODE: |
2330 | CODE: |
2133 | { |
2331 | { |
2134 | int unused_type; |
2332 | RETVAL = get_empty_map (width, height); |
2135 | RETVAL = (mapstruct*) map_get_map (&unused_type, 0, width, height); |
|
|
2136 | } |
2333 | } |
2137 | OUTPUT: |
2334 | OUTPUT: |
2138 | RETVAL |
2335 | RETVAL |
2139 | |
2336 | |
2140 | void delete_map (mapstruct *map) |
2337 | void delete_map (mapstruct *map) |
… | |
… | |
2155 | XSRETURN_UNDEF; |
2352 | XSRETURN_UNDEF; |
2156 | RETVAL = map->tile_path [dir]; |
2353 | RETVAL = map->tile_path [dir]; |
2157 | OUTPUT: |
2354 | OUTPUT: |
2158 | RETVAL |
2355 | RETVAL |
2159 | |
2356 | |
2160 | mapstruct *cf_map_get_map (char *name) |
2357 | mapstruct *ready_map_name (char *name, int flags = 0) |
2161 | PROTOTYPE: $ |
2358 | PROTOTYPE: $;$ |
2162 | ALIAS: map = 0 |
2359 | ALIAS: |
|
|
2360 | find = 0 |
|
|
2361 | get_map = 1 |
2163 | |
2362 | |
2164 | mapstruct *has_been_loaded (char *name) |
2363 | mapstruct *has_been_loaded (char *name) |
2165 | PROTOTYPE: $ |
2364 | PROTOTYPE: $ |
2166 | |
|
|
2167 | mapstruct *cf_map_get_first () |
|
|
2168 | PROTOTYPE: |
|
|
2169 | ALIAS: first = 0 |
|
|
2170 | |
2365 | |
2171 | # whoever "designed" the plug-in api should have wasted |
2366 | # whoever "designed" the plug-in api should have wasted |
2172 | # his/her time with staying away from the project - would have |
2367 | # his/her time with staying away from the project - would have |
2173 | # saved others a lot of time, without doubt. |
2368 | # saved others a lot of time, without doubt. |
2174 | void set_path (mapstruct *where, char *path) |
2369 | void set_path (mapstruct *where, char *path) |
… | |
… | |
2188 | RETVAL |
2383 | RETVAL |
2189 | |
2384 | |
2190 | void set_unique (mapstruct *map, bool unique) |
2385 | void set_unique (mapstruct *map, bool unique) |
2191 | CODE: |
2386 | CODE: |
2192 | map->unique = unique; |
2387 | map->unique = unique; |
|
|
2388 | |
|
|
2389 | void |
|
|
2390 | trigger (mapstruct *map, long connection, bool state = true) |
|
|
2391 | CODE: |
|
|
2392 | activate_connection (map, connection, state); |
|
|
2393 | |
|
|
2394 | void |
|
|
2395 | get_connection (mapstruct *map, long connection) |
|
|
2396 | PPCODE: |
|
|
2397 | oblinkpt *obp = get_connection_links (map, connection); |
|
|
2398 | if (obp) |
|
|
2399 | for (objectlink *ol = obp->link; ol; ol = ol->next) |
|
|
2400 | XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, ol->ob))); |
2193 | |
2401 | |
2194 | object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags) |
2402 | object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags) |
2195 | |
2403 | |
2196 | object *cf_map_insert_object (mapstruct *where, object* op, int x, int y) |
2404 | object *cf_map_insert_object (mapstruct *where, object* op, int x, int y) |
2197 | |
2405 | |
… | |
… | |
2259 | case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; |
2467 | case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; |
2260 | } |
2468 | } |
2261 | OUTPUT: |
2469 | OUTPUT: |
2262 | RETVAL |
2470 | RETVAL |
2263 | |
2471 | |
2264 | # "deserialise" perl map data into the map # TODO# compatibility cruft, remove |
|
|
2265 | void |
|
|
2266 | _set_obs (mapstruct *map, SV *sv) |
|
|
2267 | CODE: |
|
|
2268 | { |
|
|
2269 | object *o; |
|
|
2270 | AV *av; |
|
|
2271 | int x, y; |
|
|
2272 | AV *obs = (AV *)SvRV (sv); |
|
|
2273 | |
|
|
2274 | for (y = 0; y < MAP_HEIGHT (map); y++) |
|
|
2275 | for (x = 0; x < MAP_WIDTH (map); x++) |
|
|
2276 | { |
|
|
2277 | sv = *av_fetch (obs, x + y * MAP_HEIGHT (map), 1); |
|
|
2278 | |
|
|
2279 | if (!SvROK (sv)) |
|
|
2280 | continue; |
|
|
2281 | |
|
|
2282 | av = (AV *)SvRV (sv); |
|
|
2283 | |
|
|
2284 | for (o = GET_MAP_OB (map, x, y); o; o = o->above) |
|
|
2285 | { |
|
|
2286 | sv = av_shift (av); |
|
|
2287 | |
|
|
2288 | if (SvROK (sv)) |
|
|
2289 | { |
|
|
2290 | sv_magic ((SV *)SvRV (sv), 0, PERL_MAGIC_ext, (char *)o, 0); |
|
|
2291 | SVptr_cache_set (o, sv); |
|
|
2292 | } |
|
|
2293 | } |
|
|
2294 | } |
|
|
2295 | } |
|
|
2296 | |
|
|
2297 | |
2472 | |
2298 | MODULE = cf PACKAGE = cf::arch |
2473 | MODULE = cf PACKAGE = cf::arch |
|
|
2474 | |
|
|
2475 | archetype *find (const char *name) |
|
|
2476 | CODE: |
|
|
2477 | RETVAL = find_archetype (name); |
|
|
2478 | OUTPUT: |
|
|
2479 | RETVAL |
2299 | |
2480 | |
2300 | archetype *first() |
2481 | archetype *first() |
2301 | PROTOTYPE: |
2482 | PROTOTYPE: |
2302 | CODE: |
2483 | CODE: |
2303 | RETVAL = first_archetype; |
2484 | RETVAL = first_archetype; |