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.10 by root, Fri Aug 25 17:11:53 2006 UTC vs.
Revision 1.12 by root, Sat Aug 26 23:36:33 2006 UTC

46#include <sounds.h> 46#include <sounds.h>
47#include <cstdarg> 47#include <cstdarg>
48#include <sproto.h> 48#include <sproto.h>
49 49
50#include "cfperl.h" 50#include "cfperl.h"
51#include "shstr.h"
51 52
52#include "perlxsi.c" 53#include "perlxsi.c"
53
54static void *globalEventListener (int *type, ...);
55 54
56extern sint64 *levels; // the experience table 55extern sint64 *levels; // the experience table
57 56
58typedef object object_ornull; 57typedef object object_ornull;
59typedef mapstruct mapstruct_ornull; 58typedef mapstruct mapstruct_ornull;
61typedef double val64; 60typedef double val64;
62#define newSVval64 newSVnv 61#define newSVval64 newSVnv
63#define SvVAL64 SvNV 62#define SvVAL64 SvNV
64 63
65static f_plug_api gethook; 64static f_plug_api gethook;
66static f_plug_api registerGlobalEvent;
67static f_plug_api unregisterGlobalEvent;
68static f_plug_api systemDirectory; 65static f_plug_api systemDirectory;
69static f_plug_api object_set_property; 66static f_plug_api object_set_property;
70static f_plug_api map_get_map; 67static f_plug_api map_get_map;
71static f_plug_api object_insert; 68static f_plug_api object_insert;
72 69
99#define PUSH_PV PUSHcfapi_va(STRING, const char *) 96#define PUSH_PV PUSHcfapi_va(STRING, const char *)
100#define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int)))) 97#define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int))))
101 98
102////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 99//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103 100
104// garbage collect some perl objects, if possible
105// all objects no longer referenced and empty are
106// eligible for destruction.
107static void
108clean_obj_cache ()
109{
110 static int count;
111
112 if (++count & 7)
113 return;
114
115 int todo = 1000;
116 do
117 {
118 I32 klen;
119 char *key;
120 HE *he = hv_iternext (obj_cache);
121
122 if (he)
123 {
124 SV *sv = hv_iterval (obj_cache, he);
125
126 // empty and unreferenced? nuke it
127 if (SvREFCNT (sv) == 1 && SvREFCNT (SvRV (sv)) == 1 && !HvFILL ((HV *)(SvRV (sv))))
128 {
129 hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD);
130 todo++;
131 }
132 }
133 else
134 break;
135 }
136 while (--todo);
137}
138
139static SV * 101static SV *
140newSVptr (void *ptr, const char *klass) 102newSVptr (void *ptr, const char *klass)
141{ 103{
142 SV *sv; 104 SV *sv;
143 105
145 return &PL_sv_undef; 107 return &PL_sv_undef;
146 108
147 sv = (SV *)newHV (); 109 sv = (SV *)newHV ();
148 sv_magic (sv, 0, PERL_MAGIC_ext, (char *)ptr, 0); 110 sv_magic (sv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
149 return sv_bless (newRV_noinc (sv), gv_stashpv (klass, 1)); 111 return sv_bless (newRV_noinc (sv), gv_stashpv (klass, 1));
112}
113
114template<class extendable>
115SV *
116newSVextendable (extendable *obj, const char *klass)
117{
118 if (!obj)
119 return &PL_sv_undef;
120
121 if (!obj->self)
122 obj->self = newSVptr (obj, klass);
123
124 return newSVsv (static_cast<SV *>(obj->self));
150} 125}
151 126
152static void 127static void
153SVptr_cache_set (void *ptr, SV *sv) 128SVptr_cache_set (void *ptr, SV *sv)
154{ 129{
223{ 198{
224 SV *sv; 199 SV *sv;
225 200
226 switch (type) 201 switch (type)
227 { 202 {
228 // first three exist only for cfapi-compatibility
229 case DT_INT_PTR:
230 sv = newSViv (*va_arg (ap, int *));
231 break;
232
233 case DT_INT64_PTR:
234 sv = newSVval64 ((val64)*va_arg (ap, sint64 *));
235 break;
236
237 case DT_DOUBLE_PTR:
238 sv = newSVnv (*va_arg (ap, double *));
239 break;
240
241 case DT_INT: 203 case DT_INT:
242 sv = newSViv (va_arg (ap, int)); 204 sv = newSViv (va_arg (ap, int));
243 break; 205 break;
244 206
245 case DT_INT64: 207 case DT_INT64:
266 break; 228 break;
267 229
268 case DT_OBJECT: 230 case DT_OBJECT:
269 { 231 {
270 object *obj = va_arg (ap, object *); 232 object *obj = va_arg (ap, object *);
271 233 sv = newSVextendable (obj, obj && obj->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap");
272 if (!obj)
273 sv = &PL_sv_undef;
274 else
275 {
276 if (!obj->self)
277 switch (obj->type)
278 {
279 case MAP:
280 obj->self = static_cast<void *>(newSVptr (obj, "cf::object::map::wrap"));
281 break;
282
283 case PLAYER:
284 obj->self = static_cast<void *>(newSVptr (obj, "cf::object::player::wrap"));
285 break;
286
287 default:
288 obj->self = static_cast<void *>(newSVptr (obj, "cf::object::wrap"));
289 break;
290 }
291
292 sv = newSVsv (static_cast<SV *>(obj->self));
293 }
294 } 234 }
295 break; 235 break;
296 236
297 case DT_MAP: 237 case DT_MAP:
238 // va_arg (object *) when void * is passed is an XSI extension
298 sv = newSVptr (va_arg (ap, mapstruct *), "cf::map::wrap"); 239 sv = newSVextendable (va_arg (ap, mapstruct *), "cf::map::wrap");
299 break; 240 break;
300 241
301 case DT_PLAYER: 242 case DT_PLAYER:
302 sv = newSVptr (va_arg (ap, player *), "cf::player::wrap"); 243 sv = newSVextendable (va_arg (ap, player *), "cf::player::wrap");
303 break; 244 break;
304 245
305 case DT_ARCH: 246 case DT_ARCH:
306 sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap"); 247 sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap");
307 break; 248 break;
341 va_list ap; 282 va_list ap;
342 va_start (ap, type); 283 va_start (ap, type);
343 284
344 switch (type) 285 switch (type)
345 { 286 {
346 case CFAPI_INT: sv = newSVdt_va (ap, DT_INT_PTR ); break; 287 case CFAPI_INT: sv = newSViv (*va_arg (ap, int * )); break;
347 case CFAPI_LONG: sv = newSVdt_va (ap, DT_INT64_PTR ); break; 288 case CFAPI_LONG: sv = newSVval64 (*va_arg (ap, sint64 *)); break;
348 case CFAPI_DOUBLE: sv = newSVdt_va (ap, DT_DOUBLE_PTR); break; 289 case CFAPI_DOUBLE: sv = newSVnv (*va_arg (ap, double *)); break;
349 case CFAPI_STRING: sv = newSVdt_va (ap, DT_STRING); break; 290 case CFAPI_STRING: sv = newSVdt_va (ap, DT_STRING); break;
350 case CFAPI_POBJECT: sv = newSVdt_va (ap, DT_OBJECT); break; 291 case CFAPI_POBJECT: sv = newSVdt_va (ap, DT_OBJECT); break;
351 case CFAPI_PMAP: sv = newSVdt_va (ap, DT_MAP ); break; 292 case CFAPI_PMAP: sv = newSVdt_va (ap, DT_MAP ); break;
352 case CFAPI_PPLAYER: sv = newSVdt_va (ap, DT_PLAYER); break; 293 case CFAPI_PPLAYER: sv = newSVdt_va (ap, DT_PLAYER); break;
353 case CFAPI_PARCH: sv = newSVdt_va (ap, DT_ARCH ); break; 294 case CFAPI_PARCH: sv = newSVdt_va (ap, DT_ARCH ); break;
359 } 300 }
360 301
361 va_end (ap); 302 va_end (ap);
362 303
363 return sv; 304 return sv;
305}
306
307//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
308
309SV *
310registry_of (extendable_base *obj)
311{
312 if (!obj->cb)
313 obj->cb = newAV ();
314
315 return newRV_inc ((SV *)obj->cb);
316}
317
318void extendable_base::clear ()
319{
320 //if (self) fprintf (stderr, "free_ob_self %p %s (%p,%p)\n", this, ((object *)this)->name, self, cb);//D
321
322 if (self) SvREFCNT_dec (self);
323 if (cb) SvREFCNT_dec (cb);
324
325 if (attach) free_string (attach);
326
327 self = 0;
328 cb = 0;
329 attach = 0;
330}
331
332void extendable_base::optimise ()
333{
334 // optional, and not used yet anyways
335// // empty and unreferenced? nuke it
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
342}
343
344void extendable_base::reattach (data_type type, void *self)
345{
346 dSP;
347 ENTER;
348 SAVETMPS;
349 PUSHMARK (SP);
350 EXTEND (SP, 1);
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}
360
361void
362object::instantiate_ ()
363{
364 dSP;
365 ENTER;
366 SAVETMPS;
367 PUSHMARK (SP);
368 EXTEND (SP, 2);
369 PUSHs (sv_2mortal (newSVdt (DT_OBJECT, static_cast<object *>(this))));
370 PUSHs (sv_2mortal (newSVpv (attach, 0)));
371 PUTBACK;
372 call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL);
373 FREETMPS;
374 LEAVE;
364} 375}
365 376
366///////////////////////////////////////////////////////////////////////////// 377/////////////////////////////////////////////////////////////////////////////
367 378
368extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) 379extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr)
459 int hooktype = 1; 470 int hooktype = 1;
460 int rtype = 0; 471 int rtype = 0;
461 472
462 printf (PLUGIN_VERSION " post init\n"); 473 printf (PLUGIN_VERSION " post init\n");
463 474
464 registerGlobalEvent = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_register_global_event");
465 unregisterGlobalEvent = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_unregister_global_event");
466 systemDirectory = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_directory"); 475 systemDirectory = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_directory");
467 object_set_property = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_set_property"); 476 object_set_property = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_set_property");
468 map_get_map = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_map_get_map"); 477 map_get_map = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_map_get_map");
469 object_insert = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_insert"); 478 object_insert = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_insert");
470 479
471 cf_init_plugin (gethook); 480 cf_init_plugin (gethook);
472 481
473 /* Pick the global events you want to monitor from this plugin */
474 registerGlobalEvent (NULL, EVENT_CLOCK, PLUGIN_NAME, globalEventListener);
475 //registerGlobalEvent (NULL, EVENT_CRASH, PLUGIN_NAME, globalEventListener);
476 registerGlobalEvent (NULL, EVENT_FIND_UNARMED_SKILL, PLUGIN_NAME, globalEventListener);
477 registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener);
478 registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener);
479 registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener);
480 registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener);
481
482 return 0; 482 return 0;
483}
484
485static void *
486globalEventListener (int *type, ...)
487{
488 va_list args;
489 static int rv;
490 int event_code;
491
492 if (!perl)
493 return NULL;
494
495 va_start (args, type);
496 event_code = va_arg (args, int);
497
498 if (event_code == EVENT_CLOCK)
499 {
500 clean_obj_cache ();
501 }
502 else
503 {
504 dSP;
505
506 ENTER;
507 SAVETMPS;
508
509 PUSHMARK (SP);
510
511 EXTEND (SP, 10);
512 PUSHs (sv_2mortal (newSViv (event_code)));
513
514 switch (event_code)
515 {
516 case EVENT_CRASH:
517 break;
518
519 case EVENT_SHOUT:
520 case EVENT_MUZZLE:
521 case EVENT_KICK:
522 PUSH_OB;
523 PUSH_PV;
524 break;
525
526 case EVENT_FIND_UNARMED_SKILL:
527 PUSH_OB;
528 break;
529
530 case EVENT_TELL:
531 break;
532 }
533
534 va_end (args);
535
536 PUTBACK;
537 int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL);
538 SPAGAIN;
539
540 if (SvTRUE (ERRSV))
541 LOG (llevError, "global event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV));
542
543 rv = count > 0 ? POPi : 0;
544
545 PUTBACK;
546 FREETMPS;
547 LEAVE;
548 }
549
550 return &rv;
551}
552
553extern "C" void *cfperl_eventListener (int *type, ...)
554{
555 static int rv;
556 va_list args;
557 int event_code;
558 object *who, *activator, *third, *event;
559 char *message, *extension, *options;
560
561 if (!perl)
562 return NULL;
563
564 va_start (args, type);
565 who = va_arg (args, object *);
566 event_code = va_arg (args, int);
567 activator = va_arg (args, object *);
568 third = va_arg (args, object *);
569 message = va_arg (args, char *);
570 va_arg (args, int); // fix yourself
571 extension = va_arg (args, char *);
572 options = va_arg (args, char *);
573 event = va_arg (args, object *);
574 va_end (args);
575
576 {
577 dSP;
578
579 ENTER;
580 SAVETMPS;
581
582 PUSHMARK (SP);
583 EXTEND (SP, 10);
584
585 PUSHcfapi (STRING, extension);
586 PUSHs (sv_2mortal (newSViv (event_code)));
587
588 PUSHcfapi (POBJECT, event);
589 PUSHcfapi (POBJECT, who);
590
591 switch (event_code)
592 {
593 case EVENT_STOP: // $ob (e.g. arrow)
594 case EVENT_TIME: // $ob
595 case EVENT_TIMER: // $ob
596 break;
597
598 case EVENT_APPLY: // $ob, $who
599 case EVENT_DROP: // $ob, $who
600 case EVENT_CLOSE: // $ob, $who
601 case EVENT_MOVE: // $ob, $enemy
602 case EVENT_THROW: // $ob, $thrower
603 PUSHcfapi (POBJECT, activator);
604 break;
605
606 case EVENT_DROP_ON: // $ob, $who, $what
607 case EVENT_ATTACK: // $ob, $who, $victim (?? please god enlighten me)
608 PUSHcfapi (POBJECT, activator);
609 PUSHcfapi (POBJECT, third);
610 break;
611
612 case EVENT_TRIGGER: // $ob, $originator, [$victim], [$msg]
613 PUSHcfapi (POBJECT, activator);
614 PUSHcfapi (POBJECT, third);
615 PUSHcfapi (POBJECT, message);
616 break;
617
618 case EVENT_SAY: // $ob, $who, $msg
619 PUSHcfapi (POBJECT, activator);
620 PUSHcfapi (STRING, message);
621 break;
622
623 default:
624 LOG (llevError, "perl plugin called for unsupported event type %d", event_code);
625 break;
626 }
627
628 PUTBACK;
629 int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL);
630 SPAGAIN;
631
632 if (SvTRUE (ERRSV))
633 LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV));
634
635 rv = count > 0 ? POPi : 0;
636
637 PUTBACK;
638 FREETMPS;
639 LEAVE;
640 }
641
642 return &rv;
643} 483}
644 484
645extern "C" int cfperl_closePlugin () 485extern "C" int cfperl_closePlugin ()
646{ 486{
647 return 0; 487 return 0;
685 dSP; 525 dSP;
686 526
687 PUSHMARK (SP); 527 PUSHMARK (SP);
688 PUTBACK; 528 PUTBACK;
689 call_pv ("cf::main", G_DISCARD | G_VOID); 529 call_pv ("cf::main", G_DISCARD | G_VOID);
690}
691
692void cfperl_free_ob (object *op)
693{
694 if (op->self) fprintf (stderr, "free_ob_self %p %s\n", op, op->name);//D
695 if (op->self) SvREFCNT_dec (op->self), op->self = 0;
696 if (op->cb) SvREFCNT_dec (op->cb), op->cb = 0;
697} 530}
698 531
699static event_klass klass_of[NUM_EVENT_TYPES] = { 532static event_klass klass_of[NUM_EVENT_TYPES] = {
700# define def(type,name) KLASS_ ## type, 533# define def(type,name) KLASS_ ## type,
701# include "eventinc.h" 534# include "eventinc.h"
759 break; 592 break;
760 593
761 case KLASS_OBJECT: 594 case KLASS_OBJECT:
762 dt = (data_type) va_arg (ap, int); 595 dt = (data_type) va_arg (ap, int);
763 assert (("first argument must be of type object", dt == DT_OBJECT)); 596 assert (("first argument must be of type object", dt == DT_OBJECT));
764
765 op = va_arg (ap, object *); 597 op = va_arg (ap, object *);
766 598
599 if (op->cb)
600 gather_callbacks (callbacks, (AV *)op->cb, event);
601
767 gather_callbacks (callbacks, cb_object, event); 602 gather_callbacks (callbacks, cb_object, event);
768 //TODO: per-object
769 603
770 if (op->type > 0 && op->type <= AvFILLp (cb_type)) 604 if (op->type > 0 && op->type <= AvFILLp (cb_type))
771 { 605 {
772 SV *registry = AvARRAY (cb_type)[op->type]; 606 SV *registry = AvARRAY (cb_type)[op->type];
773 607
778 break; 612 break;
779 613
780 case KLASS_PLAYER: 614 case KLASS_PLAYER:
781 dt = (data_type) va_arg (ap, int); 615 dt = (data_type) va_arg (ap, int);
782 assert (("first argument must be of type player", dt == DT_PLAYER)); 616 assert (("first argument must be of type player", dt == DT_PLAYER));
783
784 pl = va_arg (ap, player *); 617 pl = va_arg (ap, player *);
618
619 if (op->cb)
620 gather_callbacks (callbacks, (AV *)op->cb, event);
621
785 gather_callbacks (callbacks, cb_player, event); 622 gather_callbacks (callbacks, cb_player, event);
786 break; 623 break;
787 624
788 case KLASS_MAP: 625 case KLASS_MAP:
789 dt = (data_type) va_arg (ap, int); 626 dt = (data_type) va_arg (ap, int);
790 assert (("first argument must be of type object", dt == DT_MAP)); 627 assert (("first argument must be of type object", dt == DT_MAP));
791
792 map = va_arg (ap, mapstruct *); 628 map = va_arg (ap, mapstruct *);
629
630 if (op->cb)
631 gather_callbacks (callbacks, (AV *)op->cb, event);
632
793 gather_callbacks (callbacks, cb_map, event); 633 gather_callbacks (callbacks, cb_map, event);
794 break; 634 break;
795 635
796 default: 636 default:
797 assert (("unsupported event klass in cfperl_invoke", 0)); 637 assert (("unsupported event klass in cfperl_invoke", 0));
823 dt = (data_type) va_arg (ap, int); 663 dt = (data_type) va_arg (ap, int);
824 664
825 if (dt == DT_END) 665 if (dt == DT_END)
826 break; 666 break;
827 667
668 if (dt == DT_AV)
669 {
670 AV *av = va_arg (ap, AV *);
671
672 for (int i = 0; i <= av_len (av); ++i)
673 XPUSHs (*av_fetch (av, i, 1));
674 }
675 else
828 XPUSHs (sv_2mortal (newSVdt_va (ap, dt))); 676 XPUSHs (sv_2mortal (newSVdt_va (ap, dt)));
829 } 677 }
830 678
831 va_end (ap); 679 va_end (ap);
832 680
833 PUTBACK; 681 PUTBACK;
834 int count = call_pv ("cf::invoke", G_SCALAR | G_EVAL); 682 int count = call_pv ("cf::do_invoke", G_SCALAR | G_EVAL);
835 SPAGAIN; 683 SPAGAIN;
836 684
837 count = count > 0 ? POPi : 0; 685 count = count > 0 ? POPi : 0;
838 686
839 FREETMPS; 687 FREETMPS;
840 LEAVE; 688 LEAVE;
841 689
842 return count; 690 return count;
691}
692
693SV *
694cfperl_result (int idx)
695{
696 AV *av = get_av ("cfperl::invoke_results", 0);
697 if (!av)
698 return &PL_sv_undef;
699
700 SV **sv = av_fetch (av, idx, 0);
701 if (!sv)
702 return &PL_sv_undef;
703
704 return *sv;
705}
706
707int
708cfperl_result_INT (int idx)
709{
710 return SvIV (cfperl_result (idx));
843} 711}
844 712
845MODULE = cf PACKAGE = cf PREFIX = cf_ 713MODULE = cf PACKAGE = cf PREFIX = cf_
846 714
847BOOT: 715BOOT:
1331 1199
1332 const_iv (KLASS_GLOBAL) 1200 const_iv (KLASS_GLOBAL)
1333 const_iv (KLASS_OBJECT) 1201 const_iv (KLASS_OBJECT)
1334 const_iv (KLASS_PLAYER) 1202 const_iv (KLASS_PLAYER)
1335 const_iv (KLASS_MAP) 1203 const_iv (KLASS_MAP)
1204
1205 const_iv (EVENT_OBJECT_INSTANTIATE)
1336 }; 1206 };
1337 1207
1338 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1208 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
1339 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1209 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
1340 1210
1341 static const struct { 1211 static const struct {
1342 const char *name; 1212 const char *name;
1343 IV klass; 1213 IV klass;
1344 IV iv; 1214 IV iv;
1345 } *eiv, event_iv[] = { 1215 } *eiv, event_iv[] = {
1346# define def(klass,name) { # name, (IV)KLASS_ ## klass, (IV)EV_ ## klass ## _ ## name }, 1216# define def(klass,name) { # name, (IV)KLASS_ ## klass, (IV)EVENT_ ## klass ## _ ## name },
1347# include "eventinc.h" 1217# include "eventinc.h"
1348# undef def 1218# undef def
1349 }; 1219 };
1350 1220
1351 AV *av = get_av ("cf::EVENT", 1); 1221 AV *av = get_av ("cf::EVENT", 1);
1355 AV *event = newAV (); 1225 AV *event = newAV ();
1356 av_push (event, newSVpv ((char *)eiv->name, 0)); 1226 av_push (event, newSVpv ((char *)eiv->name, 0));
1357 av_push (event, newSViv (eiv->klass)); 1227 av_push (event, newSViv (eiv->klass));
1358 av_store (av, eiv->iv, newRV_noinc ((SV *)event)); 1228 av_store (av, eiv->iv, newRV_noinc ((SV *)event));
1359 } 1229 }
1360
1361 static const struct {
1362 const char *name;
1363 IV iv;
1364 } *event, event_list[] = {
1365# define const_event(name) { # name, (IV)EVENT_ ## name },
1366 const_event (NONE)
1367 const_event (APPLY)
1368 const_event (ATTACK)
1369 const_event (DROP)
1370 const_event (DROP_ON)
1371 const_event (PICKUP)
1372 const_event (SAY)
1373 const_event (STOP)
1374 const_event (TIME)
1375 const_event (THROW)
1376 const_event (TRIGGER)
1377 const_event (CLOSE)
1378 const_event (TIMER)
1379 const_event (MOVE)
1380
1381 //const_event (CLOCK)
1382 const_event (CRASH)
1383 const_event (SHOUT)
1384 const_event (TELL)
1385 const_event (MUZZLE)
1386 const_event (KICK)
1387 const_event (FIND_UNARMED_SKILL)
1388 //const_event (FREE_OB)
1389 };
1390
1391 av = get_av ("cf::PLUGIN_EVENT", 1);
1392
1393 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; )
1394 av_store (av, event->iv, newSVpv ((char *)event->name, 0));
1395 1230
1396 static const struct { 1231 static const struct {
1397 int dtype; 1232 int dtype;
1398 const char *name; 1233 const char *name;
1399 IV idx; 1234 IV idx;
1589 1424
1590int random_roll(int min, int max, object *op, int goodbad); 1425int random_roll(int min, int max, object *op, int goodbad);
1591 1426
1592const char *cost_string_from_value(uint64 cost, int approx = 0) 1427const char *cost_string_from_value(uint64 cost, int approx = 0)
1593 1428
1429int invoke (int event, SV *arg1, ...)
1430 CODE:
1431{
1432 // parse first arg and call invoke_object etc as seen fit
1433 //TODO
1434 //cfperl_invoke (event,
1435}
1436
1594int 1437int
1595exp_to_level (val64 exp) 1438exp_to_level (val64 exp)
1596 CODE: 1439 CODE:
1597{ 1440{
1598 int i = 0; 1441 int i = 0;
1836{ 1679{
1837 int unused_type; 1680 int unused_type;
1838 RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y); 1681 RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y);
1839} 1682}
1840 1683
1684SV *registry (object *op)
1685 CODE:
1686 RETVAL = registry_of (op);
1687 OUTPUT:
1688 RETVAL
1689
1841# syntatic sugar for easier use in event callbacks. 1690# syntatic sugar for easier use in event callbacks.
1842const char *options (object *op) 1691const char *options (object *op)
1843 CODE: 1692 CODE:
1844 RETVAL = op->name; 1693 RETVAL = op->name;
1845 OUTPUT: 1694 OUTPUT:
1915 1764
1916void cf_player_set_party (object *op, partylist *party) 1765void cf_player_set_party (object *op, partylist *party)
1917 1766
1918void kill_player (object *op) 1767void kill_player (object *op)
1919 1768
1920MODULE = cf PACKAGE = cf::object::map PREFIX = cf_
1921
1922MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ 1769MODULE = cf PACKAGE = cf::player PREFIX = cf_player_
1770
1771SV *registry (player *pl)
1772 CODE:
1773 RETVAL = registry_of (pl);
1774 OUTPUT:
1775 RETVAL
1923 1776
1924player *cf_player_find (char *name) 1777player *cf_player_find (char *name)
1925 PROTOTYPE: $ 1778 PROTOTYPE: $
1926 1779
1927void cf_player_move (player *pl, int dir) 1780void cf_player_move (player *pl, int dir)
2045 RETVAL = &pl->last_stats; 1898 RETVAL = &pl->last_stats;
2046 OUTPUT: RETVAL 1899 OUTPUT: RETVAL
2047 1900
2048 1901
2049MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ 1902MODULE = cf PACKAGE = cf::map PREFIX = cf_map_
1903
1904SV *registry (mapstruct *map)
1905 CODE:
1906 RETVAL = registry_of (map);
1907 OUTPUT:
1908 RETVAL
2050 1909
2051SV * 1910SV *
2052get_property (mapstruct *obj, int type, int idx) 1911get_property (mapstruct *obj, int type, int idx)
2053 CODE: 1912 CODE:
2054 RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx)); 1913 RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines