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

Comparing deliantra/server/plugins/cfperl/cfperl.xs (file contents):
Revision 1.42 by root, Tue Mar 28 16:48:12 2006 UTC vs.
Revision 1.47 by root, Sun Apr 2 15:57:54 2006 UTC

1/*****************************************************************************/ 1/*****************************************************************************/
2/* CrossFire, A Multiplayer game for the X Window System */ 2/* CrossFire, A Multiplayer game for the X Window System */
3/* */
4/*****************************************************************************/ 3/*****************************************************************************/
5 4
6/* 5/*
7 * This code is placed under the GNU General Public Licence (GPL) 6 * This code is placed under the GNU General Public Licence (GPL)
8 * 7 *
29#include <XSUB.h> 28#include <XSUB.h>
30 29
31#undef save_long // clashes with libproto.h 30#undef save_long // clashes with libproto.h
32 31
33#define PLUGIN_NAME "perl" 32#define PLUGIN_NAME "perl"
34#define PLUGIN_VERSION "cfperl 0.2" 33#define PLUGIN_VERSION "cfperl 0.3"
35 34
36#ifndef __CEXTRACT__ 35#ifndef __CEXTRACT__
37#include <plugin.h> 36#include <plugin.h>
38#endif 37#endif
39 38
65typedef struct 64typedef struct
66{ 65{
67 object* who; 66 object* who;
68 object* activator; 67 object* activator;
69 object* third; 68 object* third;
69 object* event;
70 mapstruct* map; 70 mapstruct* map;
71 char message[1024]; 71 char message[1024];
72 int fix; // seems to be python-only, and should not be part of the API 72 int fix; // seems to be python-only, and should not be part of the API
73 int event_code; 73 int event_code;
74 char extension[1024]; // name field, should invoke specific perl extension 74 char extension[1024]; // name field, should invoke specific perl extension
77} CFPContext; 77} CFPContext;
78 78
79static HV *obj_cache; 79static HV *obj_cache;
80static PerlInterpreter *perl; 80static PerlInterpreter *perl;
81 81
82#define PUSHcfapi(type,value) PUSHs (sv_2mortal (newSVcfapi (CFAPI_ ## type, (value))))
82#define PUSHcfapi(type,ctype) PUSHs (sv_2mortal (newSVcfapi ((type), va_arg (args, ctype)))) 83#define PUSHcfapi_va(type,ctype) PUSHcfapi (type, va_arg (args, ctype))
83#define PUSH_OB PUSHcfapi(CFAPI_POBJECT, object *) 84#define PUSH_OB PUSHcfapi_va(POBJECT, object *)
84#define PUSH_PL PUSHcfapi(CFAPI_PPLAYER, player *) 85#define PUSH_PL PUSHcfapi_va(PPLAYER, player *)
85#define PUSH_MAP PUSHcfapi(CFAPI_PMAP, mapstruct *) 86#define PUSH_MAP PUSHcfapi_va(PMAP, mapstruct *)
86#define PUSH_PV PUSHcfapi(CFAPI_STRING, const char *) 87#define PUSH_PV PUSHcfapi_va(STRING, const char *)
87#define PUSH_IV PUSHcfapi(CFAPI_INT, int) 88#define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int))))
88 89
89////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 90//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90 91
91// garbage collect some perl objects, if possible 92// garbage collect some perl objects, if possible
92// all objects no longer referenced and empty are 93// all objects no longer referenced and empty are
228 case CFAPI_LONG: 229 case CFAPI_LONG:
229 sv = newSVnv ((double)*va_arg (args, sint64 *)); /* oh, the humanity! */ 230 sv = newSVnv ((double)*va_arg (args, sint64 *)); /* oh, the humanity! */
230 break; 231 break;
231 232
232 case CFAPI_DOUBLE: 233 case CFAPI_DOUBLE:
233 sv = newSViv (*va_arg (args, double *)); 234 sv = newSVnv (*va_arg (args, double *));
234 break; 235 break;
235 236
236 case CFAPI_STRING: 237 case CFAPI_STRING:
237 { 238 {
238 char *str = va_arg (args, char *); 239 char *str = va_arg (args, char *);
245 object *obj = va_arg (args, object *); 246 object *obj = va_arg (args, object *);
246 247
247 if (!obj) 248 if (!obj)
248 sv = &PL_sv_undef; 249 sv = &PL_sv_undef;
249 else 250 else
250 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE)) 251 switch (obj->type)
251 { 252 {
252 case MAP: 253 case MAP:
253 sv = newSVptr_cached (obj, "cf::object::map"); 254 sv = newSVptr_cached (obj, "cf::object::map");
254 break; 255 break;
255 256
291 va_end (args); 292 va_end (args);
292 293
293 return sv; 294 return sv;
294} 295}
295 296
296/////////////////////////////////////////////////////////////////////////////
297
298void
299inject_event (const char *func, CFPContext *context)
300{
301 dSP;
302
303 ENTER;
304 SAVETMPS;
305
306 PUSHMARK (SP);
307
308 EXTEND (SP, 10);
309
310 HV *hv = newHV ();
311#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0)
312 hv_context (CFAPI_POBJECT, ,who);
313 hv_context (CFAPI_POBJECT, ,activator);
314 hv_context (CFAPI_POBJECT, ,third);
315 hv_context (CFAPI_PMAP, ,map);
316 hv_context (CFAPI_STRING , ,message);
317 hv_context (CFAPI_INT ,&,fix);
318 hv_context (CFAPI_INT ,&,event_code);
319 hv_context (CFAPI_STRING , ,options);
320 hv_context (CFAPI_STRING , ,extension);
321
322 PUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
323
324 PUTBACK;
325 int count = call_pv (func, G_SCALAR | G_EVAL);
326 SPAGAIN;
327
328 if (SvTRUE (ERRSV))
329 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV));
330
331 context->returnvalue = count > 0 ? POPi : 0;
332
333 PUTBACK;
334 FREETMPS;
335 LEAVE;
336}
337
338///////////////////////////////////////////////////////////////////////////// 297/////////////////////////////////////////////////////////////////////////////
339 298
340int 299int
341initPlugin (const char *iversion, f_plug_api gethooksptr) 300initPlugin (const char *iversion, f_plug_api gethooksptr)
342{ 301{
598 PUTBACK; 557 PUTBACK;
599 int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL); 558 int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL);
600 SPAGAIN; 559 SPAGAIN;
601 560
602 if (SvTRUE (ERRSV)) 561 if (SvTRUE (ERRSV))
603 LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV)); 562 LOG (llevError, "global event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV));
604 563
605 rv = count > 0 ? POPi : 0; 564 rv = count > 0 ? POPi : 0;
606 565
607 PUTBACK; 566 PUTBACK;
608 FREETMPS; 567 FREETMPS;
613} 572}
614 573
615void * 574void *
616eventListener (int *type, ...) 575eventListener (int *type, ...)
617{ 576{
618 static int rv = 0; 577 static int rv;
619 va_list args; 578 va_list args;
620 char *buf; 579 int event_code;
621 CFPContext context; 580 object *who, *activator, *third, *event;
622 object *eob; 581 char *message, *extension, *options;
623 582
624 if (!perl) 583 if (!perl)
625 return; 584 return;
626 585
627 memset (&context, 0, sizeof (context));
628
629 va_start (args, type); 586 va_start (args, type);
630
631 context.who = va_arg (args, object *); 587 who = va_arg (args, object *);
632 context.event_code = va_arg (args, int); 588 event_code = va_arg (args, int);
633 context.activator = va_arg (args, object *); 589 activator = va_arg (args, object *);
634 context.third = va_arg (args, object *); 590 third = va_arg (args, object *);
635
636 buf = va_arg (args, char *); 591 message = va_arg (args, char *);
637 if (buf != 0) 592 va_arg (args, int); // fix yourself
638 strncpy (context.message, buf, sizeof (context.message)); 593 extension = va_arg (args, char *);
639 594 options = va_arg (args, char *);
640 context.fix = va_arg (args, int);
641 strncpy (context.extension, va_arg (args, char *), sizeof (context.extension));
642 strncpy (context.options, va_arg (args, char *), sizeof (context.options));
643 eob = va_arg (args, object *); 595 event = va_arg (args, object *);
644 context.returnvalue = 0;
645 va_end (args); 596 va_end (args);
646 597
647 inject_event ("cf::inject_event", &context); 598 {
599 dSP;
600
601 ENTER;
602 SAVETMPS;
603
604 PUSHMARK (SP);
605 EXTEND (SP, 10);
606
607 PUSHcfapi (STRING, extension);
608 PUSHs (sv_2mortal (newSViv (event_code)));
609
610 PUSHcfapi (POBJECT, event);
611 PUSHcfapi (POBJECT, who);
612
613 switch (event_code)
614 {
615 case EVENT_STOP: // $ob (e.g. arrow)
616 case EVENT_TIME: // $ob
617 case EVENT_TIMER: // $ob
618 break;
619
620 case EVENT_APPLY: // $ob, $who
621 case EVENT_DROP: // $ob, $who
622 case EVENT_CLOSE: // $ob, $who
623 case EVENT_DEATH: // $ob[, $killer]
624 case EVENT_MOVE: // $ob, $enemy
625 case EVENT_THROW: // $ob, $thrower
626 PUSHcfapi (POBJECT, activator);
627 break;
628
629 case EVENT_ATTACK: // $ob, $who, $victim (?? please god enlighten me)
630 PUSHcfapi (POBJECT, activator);
631 PUSHcfapi (POBJECT, third);
632 break;
633
634 case EVENT_TRIGGER: // $ob, $originator, [$victim], [$msg]
635 PUSHcfapi (POBJECT, activator);
636 PUSHcfapi (POBJECT, third);
637 PUSHcfapi (POBJECT, message);
638 break;
639
640 case EVENT_SAY: // $ob, $who, $msg
641 PUSHcfapi (POBJECT, activator);
642 PUSHcfapi (STRING, message);
643 break;
644
645 default:
646 LOG (llevError, "perl plugin called for unsupported event type %d", event_code);
647 break;
648 }
649
650 PUTBACK;
651 int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL);
652 SPAGAIN;
653
654 if (SvTRUE (ERRSV))
655 LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV));
656
657 rv = count > 0 ? POPi : 0;
658
659 PUTBACK;
660 FREETMPS;
661 LEAVE;
662 }
648 663
649 rv = context.returnvalue;
650 return &rv; 664 return &rv;
651} 665}
652 666
653int 667int
654closePlugin () 668closePlugin ()
1509{ 1523{
1510 int unused_type; 1524 int unused_type;
1511 RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y); 1525 RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y);
1512} 1526}
1513 1527
1528# syntatic sugar for easier use in event callbacks.
1529const char *options (object *op)
1530 CODE:
1531 RETVAL = op->name;
1532 OUTPUT:
1533 RETVAL
1534
1514const char *get_ob_key_value (object *op, const char *key) 1535const char *get_ob_key_value (object *op, const char *key)
1515 1536
1516bool set_ob_key_value (object *op, const char *key, const char *value = 0, int add_key = 1) 1537bool set_ob_key_value (object *op, const char *key, const char *value = 0, int add_key = 1)
1517 1538
1518object *get_nearest_player (object *ob) 1539object *get_nearest_player (object *ob)
1554 1575
1555MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_ 1576MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_
1556 1577
1557player *player (object *op) 1578player *player (object *op)
1558 CODE: 1579 CODE:
1559 RETVAL = cf_player_find (cf_query_name (op)); 1580 RETVAL = op->contr;
1560 OUTPUT: RETVAL 1581 OUTPUT: RETVAL
1561 1582
1562void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE) 1583void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE)
1563 1584
1564object *cf_player_send_inventory (object *op) 1585object *cf_player_send_inventory (object *op)
1633{ 1654{
1634 player *pl; 1655 player *pl;
1635 for (pl = first_player; pl; pl = pl->next) 1656 for (pl = first_player; pl; pl = pl->next)
1636 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl)); 1657 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl));
1637} 1658}
1659
1660bool
1661peaceful (player *pl, bool new_setting = 0)
1662 PROTOTYPE: $;$
1663 CODE:
1664 RETVAL = pl->peaceful;
1665 if (items > 1)
1666 pl->peaceful = new_setting;
1667 OUTPUT:
1668 RETVAL
1638 1669
1639living * 1670living *
1640orig_stats (player *pl) 1671orig_stats (player *pl)
1641 CODE: 1672 CODE:
1642 RETVAL = &pl->orig_stats; 1673 RETVAL = &pl->orig_stats;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines