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.40 by root, Sun Mar 26 15:52:03 2006 UTC vs.
Revision 1.52 by root, Tue May 9 17:48:28 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
76 int returnvalue; 76 int returnvalue;
77} CFPContext; 77} CFPContext;
78 78
79static HV *obj_cache; 79static HV *obj_cache;
80static PerlInterpreter *perl; 80static PerlInterpreter *perl;
81
82#define PUSHcfapi(type,value) PUSHs (sv_2mortal (newSVcfapi (CFAPI_ ## type, (value))))
83#define PUSHcfapi_va(type,ctype) PUSHcfapi (type, va_arg (args, ctype))
84#define PUSH_OB PUSHcfapi_va(POBJECT, object *)
85#define PUSH_PL PUSHcfapi_va(PPLAYER, player *)
86#define PUSH_MAP PUSHcfapi_va(PMAP, mapstruct *)
87#define PUSH_PV PUSHcfapi_va(STRING, const char *)
88#define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int))))
81 89
82////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 90//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
83 91
84// garbage collect some perl objects, if possible 92// garbage collect some perl objects, if possible
85// all objects no longer referenced and empty are 93// all objects no longer referenced and empty are
221 case CFAPI_LONG: 229 case CFAPI_LONG:
222 sv = newSVnv ((double)*va_arg (args, sint64 *)); /* oh, the humanity! */ 230 sv = newSVnv ((double)*va_arg (args, sint64 *)); /* oh, the humanity! */
223 break; 231 break;
224 232
225 case CFAPI_DOUBLE: 233 case CFAPI_DOUBLE:
226 sv = newSViv (*va_arg (args, double *)); 234 sv = newSVnv (*va_arg (args, double *));
227 break; 235 break;
228 236
229 case CFAPI_STRING: 237 case CFAPI_STRING:
230 { 238 {
231 char *str = va_arg (args, char *); 239 char *str = va_arg (args, char *);
238 object *obj = va_arg (args, object *); 246 object *obj = va_arg (args, object *);
239 247
240 if (!obj) 248 if (!obj)
241 sv = &PL_sv_undef; 249 sv = &PL_sv_undef;
242 else 250 else
243 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE)) 251 switch (obj->type)
244 { 252 {
245 case MAP: 253 case MAP:
246 sv = newSVptr_cached (obj, "cf::object::map"); 254 sv = newSVptr_cached (obj, "cf::object::map");
247 break; 255 break;
248 256
284 va_end (args); 292 va_end (args);
285 293
286 return sv; 294 return sv;
287} 295}
288 296
289/////////////////////////////////////////////////////////////////////////////
290
291void
292inject_event (const char *func, CFPContext *context)
293{
294 dSP;
295
296 ENTER;
297 SAVETMPS;
298
299 PUSHMARK (SP);
300
301 HV *hv = newHV ();
302#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0)
303 hv_context (CFAPI_POBJECT, ,who);
304 hv_context (CFAPI_POBJECT, ,activator);
305 hv_context (CFAPI_POBJECT, ,third);
306 hv_context (CFAPI_PMAP, ,map);
307 hv_context (CFAPI_STRING , ,message);
308 hv_context (CFAPI_INT ,&,fix);
309 hv_context (CFAPI_INT ,&,event_code);
310 hv_context (CFAPI_STRING , ,options);
311 hv_context (CFAPI_STRING , ,extension);
312
313 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
314
315 PUTBACK;
316 int count = call_pv (func, G_SCALAR | G_EVAL);
317 SPAGAIN;
318
319 if (SvTRUE (ERRSV))
320 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV));
321
322 context->returnvalue = count > 0 ? POPi : 0;
323
324 PUTBACK;
325 FREETMPS;
326 LEAVE;
327}
328
329///////////////////////////////////////////////////////////////////////////// 297/////////////////////////////////////////////////////////////////////////////
330 298
331int 299int
332initPlugin (const char *iversion, f_plug_api gethooksptr) 300initPlugin (const char *iversion, f_plug_api gethooksptr)
333{ 301{
494 462
495void * 463void *
496globalEventListener (int *type, ...) 464globalEventListener (int *type, ...)
497{ 465{
498 va_list args; 466 va_list args;
499 static int rv = 0; 467 static int rv;
500 CFPContext context; 468 int event_code;
501 char *buf;
502 player *pl;
503 object *op;
504 469
505 if (!perl) 470 if (!perl)
506 return; 471 return;
507 472
508 memset (&context, 0, sizeof (context));
509
510 va_start (args, type); 473 va_start (args, type);
511 context.event_code = va_arg (args, int); 474 event_code = va_arg (args, int);
512 475
513 switch (context.event_code) 476 if (event_code == EVENT_FREE_OB)
514 { 477 {
515 case EVENT_CRASH: 478 player *pl;
516 printf ("Unimplemented for now\n"); 479 object *op;
517 break; 480 SV *sv;
518 481
519 case EVENT_PLAYER_LOAD:
520 case EVENT_PLAYER_SAVE:
521 context.who = va_arg (args, object *); 482 op = va_arg (args, object *);
522 buf = va_arg (args, char *);
523 if (buf != 0)
524 strncpy (context.message, buf, sizeof (context.message));
525 break;
526
527 case EVENT_MAPLOAD:
528 case EVENT_MAPOUT:
529 case EVENT_MAPIN:
530 case EVENT_MAPCLEAN:
531 context.map = va_arg (args, mapstruct *);
532 break;
533
534 case EVENT_MAPENTER:
535 case EVENT_MAPLEAVE:
536 case EVENT_FREE_OB:
537 case EVENT_BORN:
538 case EVENT_REMOVE:
539 context.activator = va_arg (args, object *);
540 break;
541
542 case EVENT_PLAYER_DEATH:
543 context.who = va_arg (args, object *);
544 break;
545
546 case EVENT_GKILL:
547 context.who = va_arg (args, object *);
548 context.activator = va_arg (args, object *);
549 break;
550
551 case EVENT_LOGIN:
552 case EVENT_LOGOUT:
553 pl = va_arg (args, player *);
554 context.activator = pl->ob;
555 buf = va_arg (args, char *);
556 if (buf != 0)
557 strncpy (context.message, buf, sizeof (context.message));
558 break;
559
560 case EVENT_SHOUT:
561 case EVENT_MUZZLE:
562 case EVENT_KICK:
563 context.activator = va_arg (args, object *);
564 buf = va_arg (args, char *);
565 if (buf != 0)
566 strncpy (context.message, buf, sizeof (context.message));
567 break;
568
569 case EVENT_CLOCK:
570 clean_obj_cache ();
571 break;
572
573 case EVENT_TELL:
574 break;
575
576 case EVENT_MAPRESET:
577 /* stupid, should be the map itself, not "message"??? */
578 buf = va_arg (args, char *);
579 if (buf != 0)
580 strncpy (context.message, buf, sizeof (context.message));
581 break;
582 }
583
584 va_end (args);
585
586 if (context.event_code == EVENT_FREE_OB)
587 {
588 SV *sv = hv_delete (obj_cache, (char *)&context.activator, sizeof (void *), 0); 483 sv = hv_delete (obj_cache, (char *)&op, sizeof (void *), 0);
589 484
590 if (sv) 485 if (sv)
591 clearSVptr (sv); 486 clearSVptr (sv);
487
488 rv = 0;
592 } 489 }
593 else 490 else
594 inject_event ("cf::inject_global_event", &context); 491 {
595 492 dSP;
596 rv = context.returnvalue; 493
494 ENTER;
495 SAVETMPS;
496
497 PUSHMARK (SP);
498
499 EXTEND (SP, 10);
500 PUSHs (sv_2mortal (newSViv (event_code)));
501
502 switch (event_code)
503 {
504 case EVENT_CRASH:
505 break;
506
507 case EVENT_PLAYER_LOAD:
508 case EVENT_PLAYER_SAVE:
509 PUSH_OB;
510 PUSH_PV;
511 break;
512
513 case EVENT_MAPLOAD:
514 case EVENT_MAPOUT:
515 case EVENT_MAPIN:
516 case EVENT_MAPCLEAN:
517 case EVENT_MAPRESET:
518 PUSH_MAP;
519 break;
520
521 case EVENT_MAPENTER:
522 case EVENT_MAPLEAVE:
523 case EVENT_BORN:
524 case EVENT_REMOVE:
525 case EVENT_PLAYER_DEATH:
526 PUSH_OB;
527 break;
528
529 case EVENT_GKILL:
530 PUSH_OB;
531 PUSH_OB;
532 break;
533
534 case EVENT_LOGIN:
535 case EVENT_LOGOUT:
536 PUSH_PL;
537 PUSH_PV;
538 break;
539
540 case EVENT_SHOUT:
541 case EVENT_MUZZLE:
542 case EVENT_KICK:
543 PUSH_OB;
544 PUSH_PV;
545 break;
546
547 case EVENT_CLOCK:
548 clean_obj_cache ();
549 break;
550
551 case EVENT_TELL:
552 break;
553 }
554
555 va_end (args);
556
557 PUTBACK;
558 int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL);
559 SPAGAIN;
560
561 if (SvTRUE (ERRSV))
562 LOG (llevError, "global event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV));
563
564 rv = count > 0 ? POPi : 0;
565
566 PUTBACK;
567 FREETMPS;
568 LEAVE;
569 }
597 570
598 return &rv; 571 return &rv;
599} 572}
600 573
601void * 574void *
602eventListener (int *type, ...) 575eventListener (int *type, ...)
603{ 576{
604 static int rv = 0; 577 static int rv;
605 va_list args; 578 va_list args;
606 char *buf; 579 int event_code;
607 CFPContext context; 580 object *who, *activator, *third, *event;
581 char *message, *extension, *options;
608 582
609 if (!perl) 583 if (!perl)
610 return; 584 return;
611 585
612 memset (&context, 0, sizeof (context));
613
614 va_start (args, type); 586 va_start (args, type);
615
616 context.who = va_arg (args, object *); 587 who = va_arg (args, object *);
617 context.event_code = va_arg (args, int); 588 event_code = va_arg (args, int);
618 context.activator = va_arg (args, object *); 589 activator = va_arg (args, object *);
619 context.third = va_arg (args, object *); 590 third = va_arg (args, object *);
620
621 buf = va_arg (args, char *); 591 message = va_arg (args, char *);
622 if (buf != 0) 592 va_arg (args, int); // fix yourself
623 strncpy (context.message, buf, sizeof (context.message)); 593 extension = va_arg (args, char *);
624 594 options = va_arg (args, char *);
625 context.fix = va_arg (args, int); 595 event = va_arg (args, object *);
626 strncpy (context.extension, va_arg (args, char *), sizeof (context.extension));
627 strncpy (context.options, va_arg (args, char *), sizeof (context.options));
628 context.returnvalue = 0;
629 va_end (args); 596 va_end (args);
630 597
631 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 }
632 663
633 rv = context.returnvalue;
634 return &rv; 664 return &rv;
635} 665}
636 666
637int 667int
638closePlugin () 668closePlugin ()
1058 const_iv (SK_SUMMONING) 1088 const_iv (SK_SUMMONING)
1059 const_iv (SK_PYROMANCY) 1089 const_iv (SK_PYROMANCY)
1060 const_iv (SK_EVOCATION) 1090 const_iv (SK_EVOCATION)
1061 const_iv (SK_SORCERY) 1091 const_iv (SK_SORCERY)
1062 const_iv (SK_TWO_HANDED_WEAPON) 1092 const_iv (SK_TWO_HANDED_WEAPON)
1093 const_iv (SK_SPARK_TOUCH)
1094 const_iv (SK_SHIVER)
1095 const_iv (SK_ACID_SPLASH)
1096 const_iv (SK_POISON_NAIL)
1063 1097
1064 const_iv (SOUND_NEW_PLAYER) 1098 const_iv (SOUND_NEW_PLAYER)
1065 const_iv (SOUND_FIRE_ARROW) 1099 const_iv (SOUND_FIRE_ARROW)
1066 const_iv (SOUND_LEARN_SPELL) 1100 const_iv (SOUND_LEARN_SPELL)
1067 const_iv (SOUND_FUMBLE_SPELL) 1101 const_iv (SOUND_FUMBLE_SPELL)
1456 1490
1457void cf_object_update (object *op, int flags) 1491void cf_object_update (object *op, int flags)
1458 1492
1459void cf_object_pickup (object *op, object *what) 1493void cf_object_pickup (object *op, object *what)
1460 1494
1461char *cf_object_get_key (object *op, char *keyname)
1462 ALIAS: key = 0
1463
1464void cf_object_set_key (object *op, char *keyname, char *value)
1465
1466object *cf_create_object_by_name (const char *name) 1495object *cf_create_object_by_name (const char *name)
1467 1496
1468void change_exp (object *op, double exp, const char *skill_name = 0, int flag = 0) 1497void change_exp (object *op, double exp, const char *skill_name = 0, int flag = 0)
1469 1498
1470void player_lvl_adj (object *who, object *skill = 0) 1499void player_lvl_adj (object *who, object *skill = 0)
1471 1500
1501int kill_object (object *op, int dam = 0, object *hitter = 0, int type = AT_PHYSICAL)
1472 1502
1473MODULE = cf PACKAGE = cf::object PREFIX = cf_ 1503MODULE = cf PACKAGE = cf::object PREFIX = cf_
1474 1504
1475void cf_fix_object (object *pl) 1505void cf_fix_object (object *pl)
1476 ALIAS: fix = 0 1506 ALIAS: fix = 0
1492 CODE: 1522 CODE:
1493{ 1523{
1494 int unused_type; 1524 int unused_type;
1495 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);
1496} 1526}
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
1497 1534
1498const char *get_ob_key_value (object *op, const char *key) 1535const char *get_ob_key_value (object *op, const char *key)
1499 1536
1500bool 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)
1501 1538
1538 1575
1539MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_ 1576MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_
1540 1577
1541player *player (object *op) 1578player *player (object *op)
1542 CODE: 1579 CODE:
1543 RETVAL = cf_player_find (cf_query_name (op)); 1580 RETVAL = op->contr;
1544 OUTPUT: RETVAL 1581 OUTPUT: RETVAL
1545 1582
1546void 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)
1547 1584
1548object *cf_player_send_inventory (object *op) 1585object *cf_player_send_inventory (object *op)
1565 1602
1566void cf_player_set_party (object *op, partylist *party) 1603void cf_player_set_party (object *op, partylist *party)
1567 1604
1568void change_skill (object *op, double exp, char *skill_name = 0, int flag = 0) 1605void change_skill (object *op, double exp, char *skill_name = 0, int flag = 0)
1569 1606
1607void kill_player (object *op)
1608
1570MODULE = cf PACKAGE = cf::object::map PREFIX = cf_ 1609MODULE = cf PACKAGE = cf::object::map PREFIX = cf_
1571 1610
1572MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ 1611MODULE = cf PACKAGE = cf::player PREFIX = cf_player_
1573 1612
1574player *cf_player_find (char *name) 1613player *cf_player_find (char *name)
1593 1632
1594player *next (player *pl) 1633player *next (player *pl)
1595 CODE: 1634 CODE:
1596 RETVAL = pl->next; 1635 RETVAL = pl->next;
1597 OUTPUT: RETVAL 1636 OUTPUT: RETVAL
1637
1638int
1639listening (player *pl, int new_value = -1)
1640 CODE:
1641 RETVAL = pl->listening;
1642 if (new_value >= 0)
1643 pl->listening = new_value;
1644 OUTPUT:
1645 RETVAL
1598 1646
1599void get_savebed (player *pl) 1647void get_savebed (player *pl)
1600 ALIAS: 1648 ALIAS:
1601 savebed = 0 1649 savebed = 0
1602 PPCODE: 1650 PPCODE:
1617{ 1665{
1618 player *pl; 1666 player *pl;
1619 for (pl = first_player; pl; pl = pl->next) 1667 for (pl = first_player; pl; pl = pl->next)
1620 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl)); 1668 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl));
1621} 1669}
1670
1671bool
1672peaceful (player *pl, bool new_setting = 0)
1673 PROTOTYPE: $;$
1674 CODE:
1675 RETVAL = pl->peaceful;
1676 if (items > 1)
1677 pl->peaceful = new_setting;
1678 OUTPUT:
1679 RETVAL
1622 1680
1623living * 1681living *
1624orig_stats (player *pl) 1682orig_stats (player *pl)
1625 CODE: 1683 CODE:
1626 RETVAL = &pl->orig_stats; 1684 RETVAL = &pl->orig_stats;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines