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

Comparing deliantra/server/server/cfperl.xs (file contents):
Revision 1.19 by root, Mon Aug 28 07:54:11 2006 UTC vs.
Revision 1.42 by root, Mon Sep 4 11:08:00 2006 UTC

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
55extern sint64 *levels; // the experience table 43extern sint64 *levels; // the experience table
56 44
57typedef object object_ornull; 45typedef object object_ornull;
61#define newSVval64 newSVnv 49#define newSVval64 newSVnv
62#define SvVAL64 SvNV 50#define SvVAL64 SvNV
63 51
64static f_plug_api gethook = cfapi_get_hooks; 52static f_plug_api gethook = cfapi_get_hooks;
65static f_plug_api object_set_property = cfapi_object_set_property; 53static f_plug_api object_set_property = cfapi_object_set_property;
66static f_plug_api map_get_map = cfapi_map_get_map;
67static f_plug_api object_insert = cfapi_object_insert; 54static 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 */
70typedef struct 57typedef 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
125static void 112static void
126SVptr_cache_set (void *ptr, SV *sv) 113SVptr_cache_set (void *ptr, SV *sv)
127{ 114{
313 return newRV_inc ((SV *)ext->cb); 300 return newRV_inc ((SV *)ext->cb);
314} 301}
315 302
316void attachable_base::clear () 303void attachable_base::clear ()
317{ 304{
318 //if (self) fprintf (stderr, "free_ob_self %p %s (%p,%p)\n", this, ((object *)this)->name, self, cb);//D 305 if (self)
306 {
307 if (cb)
308 if (SvROK (*av_fetch (cb, EVENT_OBJECT_DESTROY, 1)))
309 INVOKE_OBJECT (DESTROY, static_cast<object *>(this));
310 else if (SvROK (*av_fetch (cb, EVENT_MAP_DESTROY, 1)))
311 INVOKE_MAP (DESTROY, static_cast<mapstruct *>(this));
319 312
313 // disconnect Perl from C, to avoid crashes
314 sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext);
315
316 // clear the perl hash, might or might not be a good idea
317 hv_clear ((HV *)SvRV ((SV *)self));
318
320 if (self) SvREFCNT_dec (self); 319 SvREFCNT_dec (self);
320 self = 0;
321 }
322
323 if (cb)
324 {
321 if (cb) SvREFCNT_dec (cb); 325 SvREFCNT_dec (cb);
326 cb = 0;
327 }
322 328
323 if (attach) free_string (attach);
324
325 self = 0;
326 cb = 0;
327 attach = 0; 329 attach = 0;
328} 330}
329 331
330void attachable_base::optimise () 332void attachable_base::optimise ()
331{ 333{
332 if (!self) 334 if (!self)
341 SvREFCNT_dec ((SV *)self); 343 SvREFCNT_dec ((SV *)self);
342 self = 0; 344 self = 0;
343 } 345 }
344} 346}
345 347
346void 348void attachable_base::instantiate (data_type type, void *obj)
347object::instantiate_ ()
348{ 349{
349 dSP; 350 dSP;
350 ENTER; 351 ENTER;
351 SAVETMPS; 352 SAVETMPS;
352 PUSHMARK (SP); 353 PUSHMARK (SP);
353 EXTEND (SP, 2); 354 EXTEND (SP, 2);
354 PUSHs (sv_2mortal (newSVdt (DT_OBJECT, static_cast<object *>(this)))); 355 PUSHs (sv_2mortal (newSVdt (type, obj)));
355 PUSHs (sv_2mortal (newSVpv (attach, 0))); 356 PUSHs (sv_2mortal (newSVpv (attach, 0)));
357
358 attach = 0;
359
356 PUTBACK; 360 PUTBACK;
357 call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL); 361 call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL);
358 FREETMPS; 362 FREETMPS;
359 LEAVE; 363 LEAVE;
360 364
361 INVOKE_OBJECT (INSTANTIATE, this); 365 switch (type)
366 {
367 case DT_OBJECT: INVOKE_OBJECT (INSTANTIATE, obj); break;
368 case DT_MAP: INVOKE_MAP (INSTANTIATE, obj); break;
369 }
362} 370}
363 371
364///////////////////////////////////////////////////////////////////////////// 372/////////////////////////////////////////////////////////////////////////////
365 373
366void reattach (data_type type, void *obj) 374void reattach (data_type type, void *obj)
367{ 375{
376 //TODO only do this when the object has _attachment's
377
368 dSP; 378 dSP;
369 ENTER; 379 ENTER;
370 SAVETMPS; 380 SAVETMPS;
371 PUSHMARK (SP); 381 PUSHMARK (SP);
372 XPUSHs (sv_2mortal (newSVdt (type, obj))); 382 XPUSHs (sv_2mortal (newSVdt (type, obj)));
387void reattach (attachable<subclass> *obj) 397void reattach (attachable<subclass> *obj)
388{ 398{
389 obj->optimise (); 399 obj->optimise ();
390 400
391 if (obj->self) 401 if (obj->self)
392 reattach (subclass::get_dt (), (subclass *)obj); 402 reattach ((data_type) cftype<subclass>::dt, (subclass *)obj);
393} 403}
394 404
405#include "kw_hash.h"
406
395object_freezer::object_freezer (const char *filename) 407object_freezer::object_freezer ()
396: filename (filename) 408: dynbuf (128 * 1024, 64 * 1024)
397{ 409{
398 av = (AV *)newAV (); 410 av = newAV ();
399} 411}
400 412
401object_freezer::~object_freezer () 413object_freezer::~object_freezer ()
414{
415 SvREFCNT_dec (av);
416}
417
418void object_freezer::put (attachable_base *ext)
419{
420 ext->optimise ();
421
422 if (ext->self)
423 {
424 int idx = AvFILLp ((AV *)av) + 1;
425 av_store (av, idx, SvREFCNT_inc (ext->self));
426
427 add ((void *)"oid ", 4);
428 add ((sint32)idx);
429 add ('\n');
430 }
431}
432
433bool object_freezer::save (const char *filename)
402{ 434{
403 dSP; 435 dSP;
404 ENTER; 436 ENTER;
405 SAVETMPS; 437 SAVETMPS;
406 PUSHMARK (SP); 438 PUSHMARK (SP);
439 EXTEND (SP, 3);
407 XPUSHs (sv_2mortal (newSVpv (filename, 0))); 440 PUSHs (sv_2mortal (newSVpv (filename, 0)));
441 PUSHs (sv_2mortal (newRV_noinc (newSVpvn ((char *)linearise (), size ()))));
408 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 442 PUSHs (sv_2mortal (newRV_inc ((SV *)av)));
409 PUTBACK; 443 PUTBACK;
410 call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL); 444 call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL);
411 FREETMPS; 445 FREETMPS;
412 LEAVE; 446 LEAVE;
413} 447}
414 448
415void object_freezer::put (FILE *fp, attachable_base *ext) 449int fprintf (object_freezer &freezer, const char *format, ...)
416{ 450{
417 ext->optimise (); 451 va_list ap;
418 452
419 if (ext->self) 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
463int fputs (const char *s, object_freezer &freezer)
464{
465 freezer.add (s);
466}
467
468object_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)
420 { 477 {
421 int idx = AvFILLp ((AV *)av) + 1; 478 dSP;
422 av_store ((AV *)av, idx, SvREFCNT_inc ((SV *)ext->self)); 479 ENTER;
423 fprintf (fp, "oid %d\n", idx); 480 SAVETMPS;
481 PUSHMARK (SP);
482 XPUSHs (sv_2mortal (newSVpv (filename, 0)));
483 PUTBACK;
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;
511 FREETMPS;
512 LEAVE;
424 } 513 }
425}
426
427object_thawer::object_thawer (const char *filename)
428{
429 av = 0;
430
431 if (!filename)
432 return;
433
434 dSP;
435 ENTER;
436 SAVETMPS;
437 PUSHMARK (SP);
438 XPUSHs (sv_2mortal (newSVpv (filename, 0)));
439 PUTBACK;
440
441 if (0 < call_pv ("cf::object_thawer_load", G_SCALAR | G_EVAL))
442 {
443 SPAGAIN;
444 SV *sv = POPs;
445 if (SvROK (sv))
446 av = SvREFCNT_inc (SvRV (sv));
447 }
448
449 FREETMPS;
450 LEAVE;
451} 514}
452 515
453void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid) 516void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid)
454{ 517{
455 if (!av || oid < 0) // this is actually an error of sorts 518 if (!av || oid < 0) // this is actually an error of sorts
465 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);
466 return; 529 return;
467 } 530 }
468 531
469 ext->self = *svp; *svp = &PL_sv_undef; 532 ext->self = *svp; *svp = &PL_sv_undef;
470 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);
471 534
472 reattach (type, obj); 535 reattach (type, obj);
473} 536}
474 537
475object_thawer::~object_thawer () 538object_thawer::~object_thawer ()
476{ 539{
540 if (text) SvREFCNT_dec (text);
541 if (av) SvREFCNT_dec (av);
542}
543
544char *fgets (char *s, int n, object_thawer &thawer)
545{
546 char *p = thawer.line;
547 char *q = s;
548
477 if (av) 549 if (!p)
478 SvREFCNT_dec ((SV *)av); 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
569keyword 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
623void 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
634void 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
659sint32 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
687sint64 object_thawer::get_sint64 () const
688{
689 return last_value ? atoll (last_value) : 0;
690}
691
692double object_thawer::get_double () const
693{
694 return last_value ? atof (last_value) : 0;
479} 695}
480 696
481///////////////////////////////////////////////////////////////////////////// 697/////////////////////////////////////////////////////////////////////////////
482 698
483extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) 699extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr)
582} 798}
583 799
584void 800void
585cfperl_init () 801cfperl_init ()
586{ 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
587 char *argv[] = { 809 char *argv[] = {
588 "", 810 "",
589 "-e" 811 "-e"
590 "BEGIN {"
591 " cf->bootstrap;" 812 "cf->bootstrap;"
592 " unshift @INC, cf::datadir ();" 813 "unshift @INC, cf::datadir ();"
593 "}"
594 ""
595 "use cf;" 814 "require cf;"
596 }; 815 };
597
598 perl = perl_alloc ();
599 perl_construct (perl);
600
601 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
602 816
603 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))
604 { 818 {
605 printf ("unable to initialize perl-interpreter, aborting.\n"); 819 printf ("unable to initialize perl-interpreter, aborting.\n");
606
607 exit (EXIT_FAILURE); 820 exit (EXIT_FAILURE);
608 //perl_destruct (perl);
609 //perl_free (perl);
610 //perl = 0;
611 //return;
612 } 821 }
613 822
614 obj_cache = newHV (); 823 obj_cache = newHV ();
615} 824}
616 825
691 dt = (data_type) va_arg (ap, int); 900 dt = (data_type) va_arg (ap, int);
692 assert (("first argument must be of type object", dt == DT_OBJECT)); 901 assert (("first argument must be of type object", dt == DT_OBJECT));
693 op = va_arg (ap, object *); 902 op = va_arg (ap, object *);
694 903
695 if (op->cb) 904 if (op->cb)
696 gather_callbacks (callbacks, (AV *)op->cb, event); 905 gather_callbacks (callbacks, op->cb, event);
697 906
698 if (op->type) 907 if (op->type)
699 { 908 {
700 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))
701 { 910 {
722 dt = (data_type) va_arg (ap, int); 931 dt = (data_type) va_arg (ap, int);
723 assert (("first argument must be of type player", dt == DT_PLAYER)); 932 assert (("first argument must be of type player", dt == DT_PLAYER));
724 pl = va_arg (ap, player *); 933 pl = va_arg (ap, player *);
725 934
726 if (pl->cb) 935 if (pl->cb)
727 gather_callbacks (callbacks, (AV *)pl->cb, event); 936 gather_callbacks (callbacks, pl->cb, event);
728 937
729 gather_callbacks (callbacks, cb_player, event); 938 gather_callbacks (callbacks, cb_player, event);
730 break; 939 break;
731 940
732 case KLASS_MAP: 941 case KLASS_MAP:
733 dt = (data_type) va_arg (ap, int); 942 dt = (data_type) va_arg (ap, int);
734 assert (("first argument must be of type object", dt == DT_MAP)); 943 assert (("first argument must be of type object", dt == DT_MAP));
735 map = va_arg (ap, mapstruct *); 944 map = va_arg (ap, mapstruct *);
736 945
737 if (map->cb) 946 if (map->cb)
738 gather_callbacks (callbacks, (AV *)map->cb, event); 947 gather_callbacks (callbacks, map->cb, event);
739 948
740 gather_callbacks (callbacks, cb_map, event); 949 gather_callbacks (callbacks, cb_map, event);
741 break; 950 break;
742 951
743 default: 952 default:
1195 1404
1196 const_iv (SK_EXP_ADD_SKILL) 1405 const_iv (SK_EXP_ADD_SKILL)
1197 const_iv (SK_EXP_TOTAL) 1406 const_iv (SK_EXP_TOTAL)
1198 const_iv (SK_EXP_NONE) 1407 const_iv (SK_EXP_NONE)
1199 const_iv (SK_SUBTRACT_SKILL_EXP) 1408 const_iv (SK_SUBTRACT_SKILL_EXP)
1409 const_iv (SK_EXP_SKILL_ONLY)
1200 1410
1201 const_iv (SK_LOCKPICKING) 1411 const_iv (SK_LOCKPICKING)
1202 const_iv (SK_HIDING) 1412 const_iv (SK_HIDING)
1203 const_iv (SK_SMITHERY) 1413 const_iv (SK_SMITHERY)
1204 const_iv (SK_BOWYER) 1414 const_iv (SK_BOWYER)
1363 prop (CFAPI_INT, MAP_PROP_WINDDIR) 1573 prop (CFAPI_INT, MAP_PROP_WINDDIR)
1364 prop (CFAPI_INT, MAP_PROP_SKY) 1574 prop (CFAPI_INT, MAP_PROP_SKY)
1365 prop (CFAPI_INT, MAP_PROP_WPARTX) 1575 prop (CFAPI_INT, MAP_PROP_WPARTX)
1366 prop (CFAPI_INT, MAP_PROP_WPARTY) 1576 prop (CFAPI_INT, MAP_PROP_WPARTY)
1367 prop (CFAPI_STRING, MAP_PROP_MESSAGE) 1577 prop (CFAPI_STRING, MAP_PROP_MESSAGE)
1368 prop (CFAPI_PMAP, MAP_PROP_NEXT)
1369 prop (CFAPI_PREGION, MAP_PROP_REGION) 1578 prop (CFAPI_PREGION, MAP_PROP_REGION)
1370 prop (CFAPI_POBJECT, OBJECT_PROP_OB_ABOVE)
1371 prop (CFAPI_POBJECT, OBJECT_PROP_OB_BELOW)
1372 prop (CFAPI_POBJECT, OBJECT_PROP_NEXT_ACTIVE_OB) 1579 prop (CFAPI_POBJECT, OBJECT_PROP_NEXT_ACTIVE_OB)
1373 prop (CFAPI_POBJECT, OBJECT_PROP_PREV_ACTIVE_OB) 1580 prop (CFAPI_POBJECT, OBJECT_PROP_PREV_ACTIVE_OB)
1374 prop (CFAPI_POBJECT, OBJECT_PROP_INVENTORY) 1581 prop (CFAPI_POBJECT, OBJECT_PROP_INVENTORY)
1375 prop (CFAPI_POBJECT, OBJECT_PROP_ENVIRONMENT) 1582 prop (CFAPI_POBJECT, OBJECT_PROP_ENVIRONMENT)
1376 prop (CFAPI_POBJECT, OBJECT_PROP_HEAD)
1377 prop (CFAPI_POBJECT, OBJECT_PROP_CONTAINER) 1583 prop (CFAPI_POBJECT, OBJECT_PROP_CONTAINER)
1378 prop (CFAPI_PMAP, OBJECT_PROP_MAP) 1584 prop (CFAPI_PMAP, OBJECT_PROP_MAP)
1379 prop (CFAPI_INT, OBJECT_PROP_COUNT) 1585 prop (CFAPI_INT, OBJECT_PROP_COUNT)
1380 prop (CFAPI_INT, OBJECT_PROP_REFCOUNT) 1586 prop (CFAPI_INT, OBJECT_PROP_REFCOUNT)
1381 prop (CFAPI_STRING, OBJECT_PROP_NAME) 1587 prop (CFAPI_STRING, OBJECT_PROP_NAME)
1561const char *cost_string_from_value(uint64 cost, int approx = 0) 1767const char *cost_string_from_value(uint64 cost, int approx = 0)
1562 1768
1563int invoke (int event, ...) 1769int invoke (int event, ...)
1564 CODE: 1770 CODE:
1565 if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL"); 1771 if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL");
1566 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 1; i < items; i++) av_push (av, ST (i)); 1772 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1773 for (int i = 1; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1567 RETVAL = INVOKE_((event_type)event, ARG_AV (av)); 1774 RETVAL = INVOKE_((event_type)event, ARG_AV (av));
1568 OUTPUT: RETVAL 1775 OUTPUT: RETVAL
1569 1776
1570int 1777int
1571exp_to_level (val64 exp) 1778exp_to_level (val64 exp)
1604 RETVAL = newSVpv (resist_plus[atnr], 0); 1811 RETVAL = newSVpv (resist_plus[atnr], 0);
1605 else 1812 else
1606 XSRETURN_UNDEF; 1813 XSRETURN_UNDEF;
1607 OUTPUT: RETVAL 1814 OUTPUT: RETVAL
1608 1815
1816int
1817_valid (SV *obj)
1818 CODE:
1819 RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext);
1820 OUTPUT:
1821 RETVAL
1822
1609MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ 1823MODULE = cf PACKAGE = cf::object PREFIX = cf_object_
1610 1824
1611int invoke (object *op, int event, ...) 1825int invoke (object *op, int event, ...)
1612 CODE: 1826 CODE:
1613 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");
1614 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 2; i < items; i++) av_push (av, ST (i)); 1828 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1829 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1615 RETVAL = INVOKE_((event_type)event, ARG_OBJECT (op), ARG_AV (av)); 1830 RETVAL = INVOKE_((event_type)event, ARG_OBJECT (op), ARG_AV (av));
1616 OUTPUT: RETVAL 1831 OUTPUT: RETVAL
1617 1832
1618SV *registry (object *op) 1833SV *registry (object *op)
1619 CODE: 1834 CODE:
1620 RETVAL = registry_of (op); 1835 RETVAL = registry_of (op);
1621 OUTPUT: 1836 OUTPUT:
1837 RETVAL
1838
1839object *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:
1622 RETVAL 1854 RETVAL
1623 1855
1624SV * 1856SV *
1625get_property (object *obj, int type, int idx) 1857get_property (object *obj, int type, int idx)
1626 CODE: 1858 CODE:
1706 1938
1707int need_identify (const object *obj); 1939int need_identify (const object *obj);
1708 1940
1709int apply_shop_mat (object *shop_mat, object *op); 1941int apply_shop_mat (object *shop_mat, object *op);
1710 1942
1711void cf_object_move (object *op, int dir, object *originator = op) 1943int move (object *op, int dir, object *originator = op)
1944 CODE:
1945 RETVAL = move_ob (op, dir, originator);
1946 OUTPUT:
1947 RETVAL
1712 1948
1713void cf_object_apply (object *op, object *author, int flags = 0) 1949void cf_object_apply (object *op, object *author, int flags = 0)
1714 1950
1715void cf_object_apply_below (object *op) 1951void cf_object_apply_below (object *op)
1716 1952
1757int cf_object_out_of_map (object *op, int x, int y) 1993int cf_object_out_of_map (object *op, int x, int y)
1758 1994
1759void cf_object_drop (object *op, object *author) 1995void cf_object_drop (object *op, object *author)
1760 1996
1761void cf_object_take (object *op, object *author) 1997void cf_object_take (object *op, object *author)
1762
1763void cf_object_say (object *op, char *msg)
1764
1765void cf_object_speak (object *op, char *msg)
1766 1998
1767object *cf_object_insert_object (object *op, object *container) 1999object *cf_object_insert_object (object *op, object *container)
1768 2000
1769const char *cf_object_get_msg (object *ob) 2001const char *cf_object_get_msg (object *ob)
1770 ALIAS: msg = 0 2002 ALIAS: msg = 0
1909MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ 2141MODULE = cf PACKAGE = cf::player PREFIX = cf_player_
1910 2142
1911int invoke (player *pl, int event, ...) 2143int invoke (player *pl, int event, ...)
1912 CODE: 2144 CODE:
1913 if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER"); 2145 if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER");
1914 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 2; i < items; i++) av_push (av, ST (i)); 2146 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2147 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1915 RETVAL = INVOKE_((event_type)event, ARG_PLAYER (pl), ARG_AV (av)); 2148 RETVAL = INVOKE_((event_type)event, ARG_PLAYER (pl), ARG_AV (av));
1916 OUTPUT: RETVAL 2149 OUTPUT: RETVAL
1917 2150
1918SV *registry (player *pl) 2151SV *registry (player *pl)
1919 CODE: 2152 CODE:
2046 OUTPUT: RETVAL 2279 OUTPUT: RETVAL
2047 2280
2048 2281
2049MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ 2282MODULE = cf PACKAGE = cf::map PREFIX = cf_map_
2050 2283
2284mapstruct *first ()
2285 PROTOTYPE:
2286 CODE:
2287 RETVAL = first_map;
2288 OUTPUT: RETVAL
2289
2290mapstruct *next (mapstruct *map)
2291 PROTOTYPE:
2292 CODE:
2293 RETVAL = map->next;
2294 OUTPUT: RETVAL
2295
2051int invoke (mapstruct *map, int event, ...) 2296int invoke (mapstruct *map, int event, ...)
2052 CODE: 2297 CODE:
2053 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");
2054 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); for (int i = 2; i < items; i++) av_push (av, ST (i)); 2299 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2300 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
2055 RETVAL = INVOKE_((event_type)event, ARG_MAP (map), ARG_AV (av)); 2301 RETVAL = INVOKE_((event_type)event, ARG_MAP (map), ARG_AV (av));
2056 OUTPUT: 2302 OUTPUT: RETVAL
2057 RETVAL
2058 2303
2059SV *registry (mapstruct *map) 2304SV *registry (mapstruct *map)
2060 CODE: 2305 CODE:
2061 RETVAL = registry_of (map); 2306 RETVAL = registry_of (map);
2062 OUTPUT: 2307 OUTPUT:
2082 2327
2083mapstruct *new (int width, int height) 2328mapstruct *new (int width, int height)
2084 PROTOTYPE: 2329 PROTOTYPE:
2085 CODE: 2330 CODE:
2086{ 2331{
2087 int unused_type; 2332 RETVAL = get_empty_map (width, height);
2088 RETVAL = (mapstruct*) map_get_map (&unused_type, 0, width, height);
2089} 2333}
2090 OUTPUT: 2334 OUTPUT:
2091 RETVAL 2335 RETVAL
2092 2336
2093void delete_map (mapstruct *map) 2337void delete_map (mapstruct *map)
2108 XSRETURN_UNDEF; 2352 XSRETURN_UNDEF;
2109 RETVAL = map->tile_path [dir]; 2353 RETVAL = map->tile_path [dir];
2110 OUTPUT: 2354 OUTPUT:
2111 RETVAL 2355 RETVAL
2112 2356
2113mapstruct *cf_map_get_map (char *name) 2357mapstruct *ready_map_name (char *name, int flags = 0)
2114 PROTOTYPE: $ 2358 PROTOTYPE: $;$
2115 ALIAS: map = 0 2359 ALIAS:
2360 find = 0
2361 get_map = 1
2116 2362
2117mapstruct *has_been_loaded (char *name) 2363mapstruct *has_been_loaded (char *name)
2118 PROTOTYPE: $ 2364 PROTOTYPE: $
2119
2120mapstruct *cf_map_get_first ()
2121 PROTOTYPE:
2122 ALIAS: first = 0
2123 2365
2124# whoever "designed" the plug-in api should have wasted 2366# whoever "designed" the plug-in api should have wasted
2125# his/her time with staying away from the project - would have 2367# his/her time with staying away from the project - would have
2126# saved others a lot of time, without doubt. 2368# saved others a lot of time, without doubt.
2127void set_path (mapstruct *where, char *path) 2369void set_path (mapstruct *where, char *path)
2141 RETVAL 2383 RETVAL
2142 2384
2143void set_unique (mapstruct *map, bool unique) 2385void set_unique (mapstruct *map, bool unique)
2144 CODE: 2386 CODE:
2145 map->unique = unique; 2387 map->unique = unique;
2388
2389void
2390trigger (mapstruct *map, long connection, bool state = true)
2391 CODE:
2392 activate_connection (map, connection, state);
2393
2394void
2395get_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)));
2146 2401
2147object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags) 2402object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags)
2148 2403
2149object *cf_map_insert_object (mapstruct *where, object* op, int x, int y) 2404object *cf_map_insert_object (mapstruct *where, object* op, int x, int y)
2150 2405
2212 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;
2213 } 2468 }
2214 OUTPUT: 2469 OUTPUT:
2215 RETVAL 2470 RETVAL
2216 2471
2217# "deserialise" perl map data into the map # TODO# compatibility cruft, remove
2218void
2219_set_obs (mapstruct *map, SV *sv)
2220 CODE:
2221{
2222 object *o;
2223 AV *av;
2224 int x, y;
2225 AV *obs = (AV *)SvRV (sv);
2226
2227 for (y = 0; y < MAP_HEIGHT (map); y++)
2228 for (x = 0; x < MAP_WIDTH (map); x++)
2229 {
2230 sv = *av_fetch (obs, x + y * MAP_HEIGHT (map), 1);
2231
2232 if (!SvROK (sv))
2233 continue;
2234
2235 av = (AV *)SvRV (sv);
2236
2237 for (o = GET_MAP_OB (map, x, y); o; o = o->above)
2238 {
2239 sv = av_shift (av);
2240
2241 if (SvROK (sv))
2242 {
2243 sv_magic ((SV *)SvRV (sv), 0, PERL_MAGIC_ext, (char *)o, 0);
2244 SVptr_cache_set (o, sv);
2245 }
2246 }
2247 }
2248}
2249
2250 2472
2251MODULE = cf PACKAGE = cf::arch 2473MODULE = cf PACKAGE = cf::arch
2474
2475archetype *find (const char *name)
2476 CODE:
2477 RETVAL = find_archetype (name);
2478 OUTPUT:
2479 RETVAL
2252 2480
2253archetype *first() 2481archetype *first()
2254 PROTOTYPE: 2482 PROTOTYPE:
2255 CODE: 2483 CODE:
2256 RETVAL = first_archetype; 2484 RETVAL = first_archetype;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines