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.17 by root, Sun Aug 27 17:59:26 2006 UTC

28#include <XSUB.h> 28#include <XSUB.h>
29 29
30#undef save_long // clashes with libproto.h 30#undef save_long // clashes with libproto.h
31 31
32#define PLUGIN_NAME "perl" 32#define PLUGIN_NAME "perl"
33#define PLUGIN_VERSION "cfperl 0.3" 33#define PLUGIN_VERSION "cfperl 0.5"
34 34
35#ifndef __CEXTRACT__ 35#ifndef __CEXTRACT__
36#include <plugin.h> 36#include <plugin.h>
37#endif 37#endif
38 38
46#include <sounds.h> 46#include <sounds.h>
47#include <cstdarg> 47#include <cstdarg>
48#include <sproto.h> 48#include <sproto.h>
49 49
50#include "cfperl.h" 50#include "cfperl.h"
51#include "shstr.h"
51 52
52#include "perlxsi.c" 53#include "perlxsi.c"
53
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, HV *hv = newHV ())
141{ 103{
142 SV *sv; 104 SV *sv;
143 105
144 if (!ptr) 106 if (!ptr)
145 return &PL_sv_undef; 107 return &PL_sv_undef;
146 108
147 sv = (SV *)newHV ();
148 sv_magic (sv, 0, PERL_MAGIC_ext, (char *)ptr, 0); 109 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
149 return sv_bless (newRV_noinc (sv), gv_stashpv (klass, 1)); 110 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
111}
112
113template<class attachable>
114SV *
115newSVattachable (attachable *obj, const char *klass)
116{
117 if (!obj)
118 return &PL_sv_undef;
119
120 if (!obj->self)
121 obj->self = newSVptr (obj, klass);
122
123 return newSVsv (static_cast<SV *>(obj->self));
150} 124}
151 125
152static void 126static void
153SVptr_cache_set (void *ptr, SV *sv) 127SVptr_cache_set (void *ptr, SV *sv)
154{ 128{
223{ 197{
224 SV *sv; 198 SV *sv;
225 199
226 switch (type) 200 switch (type)
227 { 201 {
228 // first three exist only for cfapi-compatibility
229 case DT_INT_PTR:
230 sv = newSViv (*va_arg (ap, int *));
231 break;
232
233 case DT_INT64_PTR:
234 sv = newSVval64 ((val64)*va_arg (ap, sint64 *));
235 break;
236
237 case DT_DOUBLE_PTR:
238 sv = newSVnv (*va_arg (ap, double *));
239 break;
240
241 case DT_INT: 202 case DT_INT:
242 sv = newSViv (va_arg (ap, int)); 203 sv = newSViv (va_arg (ap, int));
243 break; 204 break;
244 205
245 case DT_INT64: 206 case DT_INT64:
266 break; 227 break;
267 228
268 case DT_OBJECT: 229 case DT_OBJECT:
269 { 230 {
270 object *obj = va_arg (ap, object *); 231 object *obj = va_arg (ap, object *);
271 232 sv = newSVattachable (obj, obj && obj->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap");
272 if (!obj)
273 sv = &PL_sv_undef;
274 else
275 {
276 if (!obj->self)
277 switch (obj->type)
278 {
279 case MAP:
280 obj->self = static_cast<void *>(newSVptr (obj, "cf::object::map::wrap"));
281 break;
282
283 case PLAYER:
284 obj->self = static_cast<void *>(newSVptr (obj, "cf::object::player::wrap"));
285 break;
286
287 default:
288 obj->self = static_cast<void *>(newSVptr (obj, "cf::object::wrap"));
289 break;
290 }
291
292 sv = newSVsv (static_cast<SV *>(obj->self));
293 }
294 } 233 }
295 break; 234 break;
296 235
297 case DT_MAP: 236 case DT_MAP:
237 // va_arg (object *) when void * is passed is an XSI extension
298 sv = newSVptr (va_arg (ap, mapstruct *), "cf::map::wrap"); 238 sv = newSVattachable (va_arg (ap, mapstruct *), "cf::map::wrap");
299 break; 239 break;
300 240
301 case DT_PLAYER: 241 case DT_PLAYER:
302 sv = newSVptr (va_arg (ap, player *), "cf::player::wrap"); 242 sv = newSVattachable (va_arg (ap, player *), "cf::player::wrap");
303 break; 243 break;
304 244
305 case DT_ARCH: 245 case DT_ARCH:
306 sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap"); 246 sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap");
307 break; 247 break;
341 va_list ap; 281 va_list ap;
342 va_start (ap, type); 282 va_start (ap, type);
343 283
344 switch (type) 284 switch (type)
345 { 285 {
346 case CFAPI_INT: sv = newSVdt_va (ap, DT_INT_PTR ); break; 286 case CFAPI_INT: sv = newSViv (*va_arg (ap, int * )); break;
347 case CFAPI_LONG: sv = newSVdt_va (ap, DT_INT64_PTR ); break; 287 case CFAPI_LONG: sv = newSVval64 (*va_arg (ap, sint64 *)); break;
348 case CFAPI_DOUBLE: sv = newSVdt_va (ap, DT_DOUBLE_PTR); break; 288 case CFAPI_DOUBLE: sv = newSVnv (*va_arg (ap, double *)); break;
349 case CFAPI_STRING: sv = newSVdt_va (ap, DT_STRING); break; 289 case CFAPI_STRING: sv = newSVdt_va (ap, DT_STRING); break;
350 case CFAPI_POBJECT: sv = newSVdt_va (ap, DT_OBJECT); break; 290 case CFAPI_POBJECT: sv = newSVdt_va (ap, DT_OBJECT); break;
351 case CFAPI_PMAP: sv = newSVdt_va (ap, DT_MAP ); break; 291 case CFAPI_PMAP: sv = newSVdt_va (ap, DT_MAP ); break;
352 case CFAPI_PPLAYER: sv = newSVdt_va (ap, DT_PLAYER); break; 292 case CFAPI_PPLAYER: sv = newSVdt_va (ap, DT_PLAYER); break;
353 case CFAPI_PARCH: sv = newSVdt_va (ap, DT_ARCH ); break; 293 case CFAPI_PARCH: sv = newSVdt_va (ap, DT_ARCH ); break;
361 va_end (ap); 301 va_end (ap);
362 302
363 return sv; 303 return sv;
364} 304}
365 305
306//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
307
308SV *
309registry_of (attachable_base *ext)
310{
311 if (!ext->cb)
312 ext->cb = newAV ();
313
314 return newRV_inc ((SV *)ext->cb);
315}
316
317void attachable_base::clear ()
318{
319 //if (self) fprintf (stderr, "free_ob_self %p %s (%p,%p)\n", this, ((object *)this)->name, self, cb);//D
320
321 if (self) SvREFCNT_dec (self);
322 if (cb) SvREFCNT_dec (cb);
323
324 if (attach) free_string (attach);
325
326 self = 0;
327 cb = 0;
328 attach = 0;
329}
330
331void attachable_base::optimise ()
332{
333 if (!self)
334 return;
335
336 HV *hv = (HV *)SvRV ((SV *)self);
337
338 if (SvREFCNT ((SV *)self) == 1
339 && SvREFCNT ((SV *)hv) == 1
340 && !HvKEYS (hv))
341 {
342 SvREFCNT_dec ((SV *)self);
343 self = 0;
344 }
345}
346
347void
348object::instantiate_ ()
349{
350 dSP;
351 ENTER;
352 SAVETMPS;
353 PUSHMARK (SP);
354 EXTEND (SP, 2);
355 PUSHs (sv_2mortal (newSVdt (DT_OBJECT, static_cast<object *>(this))));
356 PUSHs (sv_2mortal (newSVpv (attach, 0)));
357 PUTBACK;
358 call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL);
359 FREETMPS;
360 LEAVE;
361
362 INVOKE_OBJECT (INSTANTIATE, this);
363}
364
366///////////////////////////////////////////////////////////////////////////// 365/////////////////////////////////////////////////////////////////////////////
367 366
367object_freezer::object_freezer (const char *filename)
368: filename (filename)
369{
370 av = (AV *)newAV ();
371 idx = 0;
372 av_extend ((AV *)av, 1024);
373}
374
375object_freezer::~object_freezer ()
376{
377 dSP;
378 ENTER;
379 SAVETMPS;
380 PUSHMARK (SP);
381 XPUSHs (sv_2mortal (newSVpv (filename, 0)));
382 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
383 PUTBACK;
384 call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL);
385 FREETMPS;
386 LEAVE;
387}
388
389void object_freezer::put (attachable_base *ext)
390{
391 ext->optimise ();
392
393 if (ext->self)
394 av_store ((AV *)av, idx, SvREFCNT_inc ((SV *)ext->self));
395
396 ++idx;
397}
398
399object_thawer::object_thawer (const char *filename)
400{
401 av = 0;
402 idx = 0;
403
404 if (!filename)
405 return;
406
407 dSP;
408 ENTER;
409 SAVETMPS;
410 PUSHMARK (SP);
411 XPUSHs (sv_2mortal (newSVpv (filename, 0)));
412 PUTBACK;
413
414 if (0 < call_pv ("cf::object_thawer_load", G_SCALAR | G_EVAL))
415 {
416 SPAGAIN;
417 SV *sv = POPs;
418 if (SvROK (sv))
419 av = SvREFCNT_inc (SvRV (sv));
420 }
421
422 FREETMPS;
423 LEAVE;
424}
425
426void reattach (data_type type, void *obj)
427{
428 dSP;
429 ENTER;
430 SAVETMPS;
431 PUSHMARK (SP);
432 XPUSHs (sv_2mortal (newSVdt (type, obj)));
433 PUTBACK;
434 call_pv ("cf::reattach", G_DISCARD | G_VOID | G_EVAL);
435 FREETMPS;
436 LEAVE;
437
438 switch (type)
439 {
440 case DT_OBJECT: INVOKE_OBJECT (REATTACH, obj); break;
441 case DT_PLAYER: INVOKE_PLAYER (REATTACH, obj); break;
442 case DT_MAP: INVOKE_MAP (REATTACH, obj); break;
443 }
444}
445
446template<class subclass>
447void reattach (attachable<subclass> *obj)
448{
449 obj->optimise ();
450
451 if (obj->self)
452 reattach (subclass::get_dt (), (subclass *)obj);
453}
454
455void object_thawer::get (data_type type, void *obj, attachable_base *ext)
456{
457 if (!av)
458 return;
459
460 // we have to "re-instantiate"/reattach to an object, so nuke ext->attach
461 ext->clear ();
462
463 SV **svp = av_fetch ((AV *)av, idx, 0);
464
465 ++idx;
466
467 if (!svp)
468 return;
469
470 if (SvROK (*svp))
471 {
472 ext->self = SvREFCNT_inc (*svp);
473 sv_magic (SvRV ((SV *)ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0);
474
475 reattach (type, obj);
476 }
477}
478
479object_thawer::~object_thawer ()
480{
481 if (av)
482 SvREFCNT_dec ((SV *)av);
483}
484
485/////////////////////////////////////////////////////////////////////////////
486
368extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) 487extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr)
369{ 488{
370 gethook = gethooksptr; 489 gethook = gethooksptr;
371 printf (PLUGIN_VERSION " init\n");
372 490
373 return 0; 491 return 0;
374} 492}
375 493
376static CommArray_s rtn_cmd; 494static CommArray_s rtn_cmd;
457extern "C" int cfperl_postInitPlugin () 575extern "C" int cfperl_postInitPlugin ()
458{ 576{
459 int hooktype = 1; 577 int hooktype = 1;
460 int rtype = 0; 578 int rtype = 0;
461 579
462 printf (PLUGIN_VERSION " post init\n");
463
464 registerGlobalEvent = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_register_global_event");
465 unregisterGlobalEvent = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_unregister_global_event");
466 systemDirectory = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_directory"); 580 systemDirectory = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_directory");
467 object_set_property = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_set_property"); 581 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"); 582 map_get_map = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_map_get_map");
469 object_insert = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_insert"); 583 object_insert = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_insert");
470 584
471 cf_init_plugin (gethook); 585 cf_init_plugin (gethook);
472 586
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; 587 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} 588}
644 589
645extern "C" int cfperl_closePlugin () 590extern "C" int cfperl_closePlugin ()
646{ 591{
647 return 0; 592 return 0;
685 dSP; 630 dSP;
686 631
687 PUSHMARK (SP); 632 PUSHMARK (SP);
688 PUTBACK; 633 PUTBACK;
689 call_pv ("cf::main", G_DISCARD | G_VOID); 634 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} 635}
698 636
699static event_klass klass_of[NUM_EVENT_TYPES] = { 637static event_klass klass_of[NUM_EVENT_TYPES] = {
700# define def(type,name) KLASS_ ## type, 638# define def(type,name) KLASS_ ## type,
701# include "eventinc.h" 639# include "eventinc.h"
759 break; 697 break;
760 698
761 case KLASS_OBJECT: 699 case KLASS_OBJECT:
762 dt = (data_type) va_arg (ap, int); 700 dt = (data_type) va_arg (ap, int);
763 assert (("first argument must be of type object", dt == DT_OBJECT)); 701 assert (("first argument must be of type object", dt == DT_OBJECT));
764
765 op = va_arg (ap, object *); 702 op = va_arg (ap, object *);
766 703
704 if (op->cb)
705 gather_callbacks (callbacks, (AV *)op->cb, event);
706
707 if (op->type)
708 {
709 if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type))
710 {
711 SV *registry = AvARRAY (cb_type)[op->type + op->subtype * NUM_SUBTYPES];
712
713 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
714 gather_callbacks (callbacks, (AV *)SvRV (registry), event);
715 }
716
717 if (op->type <= AvFILLp (cb_type))
718 {
719 SV *registry = AvARRAY (cb_type)[op->type];
720
721 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
722 gather_callbacks (callbacks, (AV *)SvRV (registry), event);
723 }
724 }
725
767 gather_callbacks (callbacks, cb_object, event); 726 gather_callbacks (callbacks, cb_object, event);
768 //TODO: per-object
769
770 if (op->type > 0 && op->type <= AvFILLp (cb_type))
771 {
772 SV *registry = AvARRAY (cb_type)[op->type];
773
774 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
775 gather_callbacks (callbacks, (AV *)SvRV (registry), event);
776 }
777 727
778 break; 728 break;
779 729
780 case KLASS_PLAYER: 730 case KLASS_PLAYER:
781 dt = (data_type) va_arg (ap, int); 731 dt = (data_type) va_arg (ap, int);
782 assert (("first argument must be of type player", dt == DT_PLAYER)); 732 assert (("first argument must be of type player", dt == DT_PLAYER));
783
784 pl = va_arg (ap, player *); 733 pl = va_arg (ap, player *);
734
735 if (pl->cb)
736 gather_callbacks (callbacks, (AV *)pl->cb, event);
737
785 gather_callbacks (callbacks, cb_player, event); 738 gather_callbacks (callbacks, cb_player, event);
786 break; 739 break;
787 740
788 case KLASS_MAP: 741 case KLASS_MAP:
789 dt = (data_type) va_arg (ap, int); 742 dt = (data_type) va_arg (ap, int);
790 assert (("first argument must be of type object", dt == DT_MAP)); 743 assert (("first argument must be of type object", dt == DT_MAP));
791
792 map = va_arg (ap, mapstruct *); 744 map = va_arg (ap, mapstruct *);
745
746 if (map->cb)
747 gather_callbacks (callbacks, (AV *)map->cb, event);
748
793 gather_callbacks (callbacks, cb_map, event); 749 gather_callbacks (callbacks, cb_map, event);
794 break; 750 break;
795 751
796 default: 752 default:
797 assert (("unsupported event klass in cfperl_invoke", 0)); 753 assert (("unsupported event klass in cfperl_invoke", 0));
823 dt = (data_type) va_arg (ap, int); 779 dt = (data_type) va_arg (ap, int);
824 780
825 if (dt == DT_END) 781 if (dt == DT_END)
826 break; 782 break;
827 783
784 if (dt == DT_AV)
785 {
786 AV *av = va_arg (ap, AV *);
787
788 for (int i = 0; i <= av_len (av); ++i)
789 XPUSHs (*av_fetch (av, i, 1));
790 }
791 else
828 XPUSHs (sv_2mortal (newSVdt_va (ap, dt))); 792 XPUSHs (sv_2mortal (newSVdt_va (ap, dt)));
829 } 793 }
830 794
831 va_end (ap); 795 va_end (ap);
832 796
833 PUTBACK; 797 PUTBACK;
834 int count = call_pv ("cf::invoke", G_SCALAR | G_EVAL); 798 int count = call_pv ("cf::do_invoke", G_SCALAR | G_EVAL);
835 SPAGAIN; 799 SPAGAIN;
836 800
837 count = count > 0 ? POPi : 0; 801 count = count > 0 ? POPi : 0;
838 802
839 FREETMPS; 803 FREETMPS;
840 LEAVE; 804 LEAVE;
841 805
842 return count; 806 return count;
807}
808
809SV *
810cfperl_result (int idx)
811{
812 AV *av = get_av ("cfperl::invoke_results", 0);
813 if (!av)
814 return &PL_sv_undef;
815
816 SV **sv = av_fetch (av, idx, 0);
817 if (!sv)
818 return &PL_sv_undef;
819
820 return *sv;
821}
822
823int
824cfperl_result_INT (int idx)
825{
826 return SvIV (cfperl_result (idx));
843} 827}
844 828
845MODULE = cf PACKAGE = cf PREFIX = cf_ 829MODULE = cf PACKAGE = cf PREFIX = cf_
846 830
847BOOT: 831BOOT:
981 const_iv (BUILDER) 965 const_iv (BUILDER)
982 const_iv (MATERIAL) 966 const_iv (MATERIAL)
983 const_iv (ITEM_TRANSFORMER) 967 const_iv (ITEM_TRANSFORMER)
984 const_iv (QUEST) 968 const_iv (QUEST)
985 969
970 const_iv (NUM_SUBTYPES)
971
986 const_iv (ST_BD_BUILD) 972 const_iv (ST_BD_BUILD)
987 const_iv (ST_BD_REMOVE) 973 const_iv (ST_BD_REMOVE)
988 974
989 const_iv (ST_MAT_FLOOR) 975 const_iv (ST_MAT_FLOOR)
990 const_iv (ST_MAT_WALL) 976 const_iv (ST_MAT_WALL)
1338 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1324 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
1339 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1325 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
1340 1326
1341 static const struct { 1327 static const struct {
1342 const char *name; 1328 const char *name;
1329 int skip;
1343 IV klass; 1330 IV klass;
1344 IV iv; 1331 IV iv;
1345 } *eiv, event_iv[] = { 1332 } *eiv, event_iv[] = {
1346# define def(klass,name) { # name, (IV)KLASS_ ## klass, (IV)EV_ ## klass ## _ ## name }, 1333# define def(klass,name) { "EVENT_" # klass "_" # name, sizeof ("EVENT_" # klass), (IV)KLASS_ ## klass, (IV)EVENT_ ## klass ## _ ## name },
1347# include "eventinc.h" 1334# include "eventinc.h"
1348# undef def 1335# undef def
1349 }; 1336 };
1350 1337
1351 AV *av = get_av ("cf::EVENT", 1); 1338 AV *av = get_av ("cf::EVENT", 1);
1352 1339
1353 for (eiv = event_iv + sizeof (event_iv) / sizeof (event_iv [0]); eiv-- > event_iv; ) 1340 for (eiv = event_iv + sizeof (event_iv) / sizeof (event_iv [0]); eiv-- > event_iv; )
1354 { 1341 {
1355 AV *event = newAV (); 1342 AV *event = newAV ();
1356 av_push (event, newSVpv ((char *)eiv->name, 0)); 1343 av_push (event, newSVpv ((char *)eiv->name + eiv->skip, 0));
1357 av_push (event, newSViv (eiv->klass)); 1344 av_push (event, newSViv (eiv->klass));
1358 av_store (av, eiv->iv, newRV_noinc ((SV *)event)); 1345 av_store (av, eiv->iv, newRV_noinc ((SV *)event));
1346 newCONSTSUB (stash, (char *)eiv->name, newSViv (eiv->iv));
1359 } 1347 }
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 1348
1396 static const struct { 1349 static const struct {
1397 int dtype; 1350 int dtype;
1398 const char *name; 1351 const char *name;
1399 IV idx; 1352 IV idx;
1527 { 1480 {
1528 hv_store (prop_type, cprop->name, strlen (cprop->name), newSViv (cprop->dtype), 0); 1481 hv_store (prop_type, cprop->name, strlen (cprop->name), newSViv (cprop->dtype), 0);
1529 hv_store (prop_idx, cprop->name, strlen (cprop->name), newSViv (cprop->idx ), 0); 1482 hv_store (prop_idx, cprop->name, strlen (cprop->name), newSViv (cprop->idx ), 0);
1530 } 1483 }
1531 1484
1532 cb_global = get_av ("cf::CB_GLOBAL", 1);
1533 cb_object = get_av ("cf::CB_OBJECT", 1);
1534 cb_player = get_av ("cf::CB_PLAYER", 1);
1535 cb_type = get_av ("cf::CB_TYPE" , 1);
1536 cb_map = get_av ("cf::CB_MAP" , 1);
1537
1538 //I_EVENT_API (PACKAGE); 1485 //I_EVENT_API (PACKAGE);
1486}
1487
1488void _reload_1 ()
1489 CODE:
1490 cb_global = get_av ("cf::CB_GLOBAL", 1);
1491 cb_object = get_av ("cf::CB_OBJECT", 1);
1492 cb_player = get_av ("cf::CB_PLAYER", 1);
1493 cb_type = get_av ("cf::CB_TYPE" , 1);
1494 cb_map = get_av ("cf::CB_MAP" , 1);
1495
1496void _reload_2 ()
1497 CODE:
1498{
1499 // reattach to all attachable objects in the game.
1500 for (player *pl = first_player; pl; pl = pl->next)
1501 reattach (pl);
1502
1503 for (mapstruct *map = first_map; map; map = map->next)
1504 reattach (map);
1505
1506 for (object *op = objects; op; op = op->next)
1507 reattach (op);
1539} 1508}
1540 1509
1541NV floor (NV x) 1510NV floor (NV x)
1542 1511
1543NV ceil (NV x) 1512NV ceil (NV x)
1588 PROTOTYPE: $ 1557 PROTOTYPE: $
1589 1558
1590int random_roll(int min, int max, object *op, int goodbad); 1559int random_roll(int min, int max, object *op, int goodbad);
1591 1560
1592const char *cost_string_from_value(uint64 cost, int approx = 0) 1561const char *cost_string_from_value(uint64 cost, int approx = 0)
1562
1563int invoke (int event, SV *arg1, ...)
1564 CODE:
1565{
1566 // parse first arg and call invoke_object etc as seen fit
1567 //TODO
1568 //cfperl_invoke (event,
1569}
1593 1570
1594int 1571int
1595exp_to_level (val64 exp) 1572exp_to_level (val64 exp)
1596 CODE: 1573 CODE:
1597{ 1574{
1836{ 1813{
1837 int unused_type; 1814 int unused_type;
1838 RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y); 1815 RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y);
1839} 1816}
1840 1817
1818SV *registry (object *op)
1819 CODE:
1820 RETVAL = registry_of (op);
1821 OUTPUT:
1822 RETVAL
1823
1841# syntatic sugar for easier use in event callbacks. 1824# syntatic sugar for easier use in event callbacks.
1842const char *options (object *op) 1825const char *options (object *op)
1843 CODE: 1826 CODE:
1844 RETVAL = op->name; 1827 RETVAL = op->name;
1845 OUTPUT: 1828 OUTPUT:
1915 1898
1916void cf_player_set_party (object *op, partylist *party) 1899void cf_player_set_party (object *op, partylist *party)
1917 1900
1918void kill_player (object *op) 1901void kill_player (object *op)
1919 1902
1920MODULE = cf PACKAGE = cf::object::map PREFIX = cf_
1921
1922MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ 1903MODULE = cf PACKAGE = cf::player PREFIX = cf_player_
1904
1905SV *registry (player *pl)
1906 CODE:
1907 RETVAL = registry_of (pl);
1908 OUTPUT:
1909 RETVAL
1923 1910
1924player *cf_player_find (char *name) 1911player *cf_player_find (char *name)
1925 PROTOTYPE: $ 1912 PROTOTYPE: $
1926 1913
1927void cf_player_move (player *pl, int dir) 1914void cf_player_move (player *pl, int dir)
2045 RETVAL = &pl->last_stats; 2032 RETVAL = &pl->last_stats;
2046 OUTPUT: RETVAL 2033 OUTPUT: RETVAL
2047 2034
2048 2035
2049MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ 2036MODULE = cf PACKAGE = cf::map PREFIX = cf_map_
2037
2038SV *registry (mapstruct *map)
2039 CODE:
2040 RETVAL = registry_of (map);
2041 OUTPUT:
2042 RETVAL
2050 2043
2051SV * 2044SV *
2052get_property (mapstruct *obj, int type, int idx) 2045get_property (mapstruct *obj, int type, int idx)
2053 CODE: 2046 CODE:
2054 RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx)); 2047 RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx));
2198 case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; 2191 case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break;
2199 } 2192 }
2200 OUTPUT: 2193 OUTPUT:
2201 RETVAL 2194 RETVAL
2202 2195
2203# "serialise" map perl data into a ref 2196# "deserialise" perl map data into the map # TODO# compatibility cruft, remove
2204void
2205_get_obs (mapstruct *map)
2206 PPCODE:
2207{
2208 object *o;
2209 int x, y;
2210 AV *obs = newAV ();
2211 int nonnull = 0;
2212
2213 for (y = 0; y < MAP_HEIGHT (map); y++)
2214 for (x = 0; x < MAP_WIDTH (map); x++)
2215 {
2216 AV *av = newAV ();
2217
2218 for (o = GET_MAP_OB (map, x, y); o; o = o->above)
2219 {
2220 SV *sv = SVptr_cache_get (o);
2221
2222 if (sv && HvFILL (SvRV (sv)))
2223 {
2224 nonnull = 1;
2225 sv = newSVsv (sv);
2226 }
2227 else
2228 sv = &PL_sv_undef;
2229
2230 av_push (av, sv);
2231 }
2232
2233 av_store (obs, x + y * MAP_HEIGHT (map), newRV_noinc ((SV *)av));
2234 }
2235
2236 if (nonnull)
2237 XPUSHs (sv_2mortal (newRV_noinc ((SV *)obs)));
2238 else
2239 SvREFCNT_dec (obs);
2240}
2241
2242# "deserialise" perl map data into the map
2243void 2197void
2244_set_obs (mapstruct *map, SV *sv) 2198_set_obs (mapstruct *map, SV *sv)
2245 CODE: 2199 CODE:
2246{ 2200{
2247 object *o; 2201 object *o;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines