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.7 by root, Fri Feb 3 22:52:51 2006 UTC vs.
Revision 1.27 by root, Sun Mar 5 18:48:50 2006 UTC

28#include <perl.h> 28#include <perl.h>
29#include <XSUB.h> 29#include <XSUB.h>
30 30
31#undef save_long // clashes with libproto.h 31#undef save_long // clashes with libproto.h
32 32
33#define PLUGIN_NAME "cfperl" 33#define PLUGIN_NAME "perl"
34#define PLUGIN_VERSION "cfperl 0.0" 34#define PLUGIN_VERSION "cfperl 0.2"
35 35
36#ifndef __CEXTRACT__ 36#ifndef __CEXTRACT__
37#include <plugin.h> 37#include <plugin.h>
38#endif 38#endif
39 39
47 47
48#include <stdarg.h> 48#include <stdarg.h>
49 49
50#include "perlxsi.c" 50#include "perlxsi.c"
51 51
52typedef object object_ornull;
53typedef mapstruct mapstruct_ornull;
54
52static f_plug_api gethook; 55static f_plug_api gethook;
53static f_plug_api registerGlobalEvent; 56static f_plug_api registerGlobalEvent;
54static f_plug_api unregisterGlobalEvent; 57static f_plug_api unregisterGlobalEvent;
55static f_plug_api systemDirectory; 58static f_plug_api systemDirectory;
59static f_plug_api object_set_property;
60static f_plug_api map_get_map;
61static f_plug_api object_insert;
56 62
57typedef struct 63typedef struct
58{ 64{
59 object* who; 65 object* who;
60 object* activator; 66 object* activator;
61 object* third; 67 object* third;
62 char message[1024]; 68 char message[1024];
63 int fix; 69 int fix; // seems to be python-only, and should not be part of the API
64 int event_code; 70 int event_code;
65 char options[1024]; 71 char extension[1024]; // name field, should invoke specific perl extension
72 char options[1024]; // slaying field of event_connectors
66 int returnvalue; 73 int returnvalue;
67} CFPContext; 74} CFPContext;
68 75
69//static int current_command = -999; 76static HV *obj_cache;
70
71static PerlInterpreter *perl; 77static PerlInterpreter *perl;
72 78
73////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 79//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80
81// garbage collect some perl objects, if possible
82// all objects no longer referenced and empty are
83// eligible for destruction.
84void
85clean_obj_cache ()
86{
87 static int count;
88
89 if (++count & 7)
90 return;
91
92 int todo = 1000;
93 do
94 {
95 I32 klen;
96 char *key;
97 HE *he = hv_iternext (obj_cache);
98
99 if (he)
100 {
101 SV *sv = hv_iterval (obj_cache, he);
102
103 // empty and unreferenced? nuke it
104 if (SvREFCNT (sv) == 1 && SvREFCNT (SvRV (sv)) == 1 && !HvFILL ((HV *)(SvRV (sv))))
105 {
106 hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD);
107 todo++;
108 }
109 }
110 else
111 break;
112 }
113 while (--todo);
114}
74 115
75static SV * 116static SV *
76newSVptr (void *ptr, const char *klass) 117newSVptr (void *ptr, const char *klass)
77{ 118{
119 SV *sv;
120
78 if (!ptr) 121 if (!ptr)
79 return &PL_sv_undef; 122 return &PL_sv_undef;
80 123
124 sv = newSV (0);
125 sv_magic (sv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
126 return sv_bless (newRV_noinc (sv), gv_stashpv (klass, 1));
127}
128
129static SV *
130newSVptr_cached (void *ptr, const char *klass)
131{
132 SV *sv, **he;
133
134 if (!ptr)
135 return &PL_sv_undef;
136
137 he = hv_fetch (obj_cache, (char *)&ptr, sizeof (ptr), 0);
138
139 if (he)
140 sv = *he;
141 else
142 {
81 HV *hv = newHV (); 143 HV *hv = newHV ();
82 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); 144 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
83 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 145 sv = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
146 hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0);
147 }
148
149 return newSVsv (sv);
84} 150}
85 151
86static void 152static void
87clearSVptr (SV *sv) 153clearSVptr (SV *sv)
88{ 154{
105 croak ("perl code used %s object, but C object is already destroyed, caught", klass); 171 croak ("perl code used %s object, but C object is already destroyed, caught", klass);
106 172
107 return (long)mg->mg_ptr; 173 return (long)mg->mg_ptr;
108} 174}
109 175
176static long
177SvPTR_ornull (SV *sv, const char *klass)
178{
179 if (SvOK (sv))
180 return SvPTR (sv, klass);
181 else
182 return 0;
183}
184
110SV * 185SV *
111newSVcfapi (int type, ...) 186newSVcfapi (int type, ...)
112{ 187{
113 SV *sv; 188 SV *sv;
114 189
115 va_list args; 190 va_list args;
116 va_start (args, type); 191 va_start (args, type);
117 192
118 switch (type) 193 switch (type)
119 { 194 {
195#if 0
196 case CFAPI_INT16:
197 sv = newSViv (*va_arg (args, sint16_t *));
198 break;
199#endif
200
120 case CFAPI_INT: 201 case CFAPI_INT:
121 sv = newSViv (*va_arg (args, int *)); 202 sv = newSViv (*va_arg (args, int *));
122 break; 203 break;
123 204
124 case CFAPI_LONG: 205 case CFAPI_LONG:
144 sv = &PL_sv_undef; 225 sv = &PL_sv_undef;
145 else 226 else
146 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE)) 227 switch (*(int *)cf_object_get_property (obj, CFAPI_OBJECT_PROP_TYPE))
147 { 228 {
148 case MAP: 229 case MAP:
149 sv = newSVptr (obj, "cf::object::map"); 230 sv = newSVptr_cached (obj, "cf::object::map");
150 break; 231 break;
151 232
152 case PLAYER: 233 case PLAYER:
153 sv = newSVptr (obj, "cf::object::player"); 234 sv = newSVptr_cached (obj, "cf::object::player");
154 break; 235 break;
155 236
156 default: 237 default:
157 sv = newSVptr (obj, "cf::object"); 238 sv = newSVptr_cached (obj, "cf::object");
158 break; 239 break;
159 } 240 }
160 } 241 }
161 break; 242 break;
162 243
190} 271}
191 272
192///////////////////////////////////////////////////////////////////////////// 273/////////////////////////////////////////////////////////////////////////////
193 274
194void 275void
195inject_event (CFPContext *context) 276inject_event (const char *func, CFPContext *context)
196{ 277{
197 dSP; 278 dSP;
198 279
199 ENTER; 280 ENTER;
200 SAVETMPS; 281 SAVETMPS;
201 282
202 PUSHMARK (SP); 283 PUSHMARK (SP);
203
204 EXTEND (SP, 2);
205 //PUSHs (sv_2mortal (newSViv (type)));
206 284
207 HV *hv = newHV (); 285 HV *hv = newHV ();
208#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0) 286#define hv_context(type,addr,expr) hv_store (hv, #expr, sizeof (#expr) - 1, newSVcfapi (type, addr context->expr), 0)
209 hv_context (CFAPI_POBJECT, ,who); 287 hv_context (CFAPI_POBJECT, ,who);
210 hv_context (CFAPI_POBJECT, ,activator); 288 hv_context (CFAPI_POBJECT, ,activator);
211 hv_context (CFAPI_POBJECT, ,third); 289 hv_context (CFAPI_POBJECT, ,third);
212 hv_context (CFAPI_STRING , ,message); 290 hv_context (CFAPI_STRING , ,message);
213 hv_context (CFAPI_INT ,&,fix); 291 hv_context (CFAPI_INT ,&,fix);
214 hv_context (CFAPI_INT ,&,event_code); 292 hv_context (CFAPI_INT ,&,event_code);
215 hv_context (CFAPI_STRING , ,options); 293 hv_context (CFAPI_STRING , ,options);
294 hv_context (CFAPI_STRING , ,extension);
216 295
217 PUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 296 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
218 297
219 PUTBACK; 298 PUTBACK;
220 int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL); 299 int count = call_pv (func, G_SCALAR | G_EVAL);
221 SPAGAIN; 300 SPAGAIN;
222 301
223 if (SvTRUE (ERRSV)) 302 if (SvTRUE (ERRSV))
224 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV)); 303 LOG (llevError, "event '%d' callback evaluation error: %s", context->event_code, SvPV_nolen (ERRSV));
225 304
334 printf (PLUGIN_VERSION " post init\n"); 413 printf (PLUGIN_VERSION " post init\n");
335 414
336 registerGlobalEvent = gethook (&rtype, hooktype, "cfapi_system_register_global_event"); 415 registerGlobalEvent = gethook (&rtype, hooktype, "cfapi_system_register_global_event");
337 unregisterGlobalEvent = gethook (&rtype, hooktype, "cfapi_system_unregister_global_event"); 416 unregisterGlobalEvent = gethook (&rtype, hooktype, "cfapi_system_unregister_global_event");
338 systemDirectory = gethook (&rtype, hooktype, "cfapi_system_directory"); 417 systemDirectory = gethook (&rtype, hooktype, "cfapi_system_directory");
418 object_set_property = gethook (&rtype, hooktype, "cfapi_object_set_property");
419 map_get_map = gethook (&rtype, hooktype, "cfapi_map_get_map");
420 object_insert = gethook (&rtype, hooktype, "cfapi_object_insert");
421
339 cf_init_plugin (gethook); 422 cf_init_plugin (gethook);
340 423
341 /* Pick the global events you want to monitor from this plugin */ 424 /* Pick the global events you want to monitor from this plugin */
342 registerGlobalEvent (NULL, EVENT_BORN, PLUGIN_NAME, globalEventListener); 425 registerGlobalEvent (NULL, EVENT_BORN, PLUGIN_NAME, globalEventListener);
343 registerGlobalEvent (NULL, EVENT_CLOCK, PLUGIN_NAME, globalEventListener); 426 registerGlobalEvent (NULL, EVENT_CLOCK, PLUGIN_NAME, globalEventListener);
352 registerGlobalEvent (NULL, EVENT_REMOVE, PLUGIN_NAME, globalEventListener); 435 registerGlobalEvent (NULL, EVENT_REMOVE, PLUGIN_NAME, globalEventListener);
353 registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener); 436 registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener);
354 registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener); 437 registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener);
355 registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener); 438 registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener);
356 registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener); 439 registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener);
440 registerGlobalEvent (NULL, EVENT_FREE_OB, PLUGIN_NAME, globalEventListener);
357 441
358 char *argv[] = { 442 char *argv[] = {
359 "", 443 "",
360 "-e" 444 "-e"
361 "BEGIN {" 445 "BEGIN {"
375 459
376 perl_destruct (perl); 460 perl_destruct (perl);
377 perl_free (perl); 461 perl_free (perl);
378 perl = 0; 462 perl = 0;
379 } 463 }
464 else
465 {
466 obj_cache = newHV ();
467 }
380 468
381 return 0; 469 return 0;
382} 470}
383 471
384void * 472void *
403 { 491 {
404 case EVENT_CRASH: 492 case EVENT_CRASH:
405 printf ("Unimplemented for now\n"); 493 printf ("Unimplemented for now\n");
406 break; 494 break;
407 495
496 case EVENT_MAPENTER:
497 case EVENT_MAPLEAVE:
498 case EVENT_FREE_OB:
408 case EVENT_BORN: 499 case EVENT_BORN:
500 case EVENT_REMOVE:
409 context.activator = va_arg (args, object *); 501 context.activator = va_arg (args, object *);
410 break; 502 break;
411 503
412 case EVENT_PLAYER_DEATH: 504 case EVENT_PLAYER_DEATH:
413 context.who = va_arg (args, object *); 505 context.who = va_arg (args, object *);
417 context.who = va_arg (args, object *); 509 context.who = va_arg (args, object *);
418 context.activator = va_arg (args, object *); 510 context.activator = va_arg (args, object *);
419 break; 511 break;
420 512
421 case EVENT_LOGIN: 513 case EVENT_LOGIN:
422 pl = va_arg (args, player *);
423 context.activator = pl->ob;
424 buf = va_arg (args, char *);
425 if (buf != 0)
426 strcpy (context.message, buf);
427 break;
428
429 case EVENT_LOGOUT: 514 case EVENT_LOGOUT:
430 pl = va_arg (args, player *); 515 pl = va_arg (args, player *);
431 context.activator = pl->ob; 516 context.activator = pl->ob;
432 buf = va_arg (args, char *); 517 buf = va_arg (args, char *);
433 if (buf != 0) 518 if (buf != 0)
434 strcpy (context.message, buf); 519 strncpy (context.message, buf, sizeof (context.message));
435 break;
436
437 case EVENT_REMOVE:
438 context.activator = va_arg (args, object *);
439 break; 520 break;
440 521
441 case EVENT_SHOUT: 522 case EVENT_SHOUT:
442 context.activator = va_arg (args, object *);
443 buf = va_arg (args, char *);
444 if (buf != 0)
445 strcpy (context.message, buf);
446 break;
447
448 case EVENT_MUZZLE: 523 case EVENT_MUZZLE:
449 context.activator = va_arg (args, object *);
450 buf = va_arg (args, char *);
451 if (buf != 0)
452 strcpy (context.message, buf);
453 break;
454
455 case EVENT_KICK: 524 case EVENT_KICK:
456 context.activator = va_arg (args, object *); 525 context.activator = va_arg (args, object *);
457 buf = va_arg (args, char *); 526 buf = va_arg (args, char *);
458 if (buf != 0) 527 if (buf != 0)
459 strcpy (context.message, buf); 528 strncpy (context.message, buf, sizeof (context.message));
460 break;
461
462 case EVENT_MAPENTER:
463 context.activator = va_arg (args, object *);
464 break;
465
466 case EVENT_MAPLEAVE:
467 context.activator = va_arg (args, object *);
468 break; 529 break;
469 530
470 case EVENT_CLOCK: 531 case EVENT_CLOCK:
532 clean_obj_cache ();
533 break;
534
535 case EVENT_TELL:
471 break; 536 break;
472 537
473 case EVENT_MAPRESET: 538 case EVENT_MAPRESET:
474 buf = va_arg (args, char *); 539 buf = va_arg (args, char *);
475 if (buf != 0) 540 if (buf != 0)
476 strcpy (context.message, buf); 541 strncpy (context.message, buf, sizeof (context.message));
477 break;
478
479 case EVENT_TELL:
480 break; 542 break;
481 } 543 }
482 544
483 va_end (args); 545 va_end (args);
484 546
485 inject_event (&context); 547 if (context.event_code == EVENT_FREE_OB)
548 {
549 SV *sv = hv_delete (obj_cache, (char *)&context.activator, sizeof (void *), 0);
550
551 if (sv)
552 clearSVptr (sv);
553 }
554 else
555 inject_event ("cf::inject_global_event", &context);
486 556
487 rv = context.returnvalue; 557 rv = context.returnvalue;
488 558
489 return &rv; 559 return &rv;
490} 560}
506 576
507 context.who = va_arg (args, object *); 577 context.who = va_arg (args, object *);
508 context.event_code = va_arg (args, int); 578 context.event_code = va_arg (args, int);
509 context.activator = va_arg (args, object *); 579 context.activator = va_arg (args, object *);
510 context.third = va_arg (args, object *); 580 context.third = va_arg (args, object *);
581
511 buf = va_arg (args, char *); 582 buf = va_arg (args, char *);
512
513 if (buf != 0) 583 if (buf != 0)
514 strcpy (context.message, buf); 584 strncpy (context.message, buf, sizeof (context.message));
515 585
516 context.fix = va_arg (args, int); 586 context.fix = va_arg (args, int);
587 strncpy (context.extension, va_arg (args, char *), sizeof (context.extension));
517 strcpy (context.options, va_arg (args, char *)); 588 strncpy (context.options, va_arg (args, char *), sizeof (context.options));
518 context.returnvalue = 0; 589 context.returnvalue = 0;
519 va_end (args); 590 va_end (args);
520 591
521 inject_event (&context); 592 inject_event ("cf::inject_event", &context);
522 593
523 rv = context.returnvalue; 594 rv = context.returnvalue;
524 return &rv; 595 return &rv;
525} 596}
526 597
543 614
544BOOT: 615BOOT:
545{ 616{
546 HV *stash = gv_stashpv ("cf", 1); 617 HV *stash = gv_stashpv ("cf", 1);
547 618
548 const struct { 619 static const struct {
549 const char *name; 620 const char *name;
550 IV iv; 621 IV iv;
551 } *civ, const_iv[] = { 622 } *civ, const_iv[] = {
552# define const_iv(name) { # name, (IV)name }, 623# define const_iv(name) { # name, (IV)name },
553 const_iv (llevError) 624 const_iv (llevError)
683 const_iv (ST_BD_REMOVE) 754 const_iv (ST_BD_REMOVE)
684 const_iv (ST_MAT_FLOOR) 755 const_iv (ST_MAT_FLOOR)
685 const_iv (ST_MAT_WALL) 756 const_iv (ST_MAT_WALL)
686 const_iv (ST_MAT_ITEM) 757 const_iv (ST_MAT_ITEM)
687 758
759 const_iv (AT_PHYSICAL)
760 const_iv (AT_MAGIC)
761 const_iv (AT_FIRE)
762 const_iv (AT_ELECTRICITY)
763 const_iv (AT_COLD)
764 const_iv (AT_CONFUSION)
765 const_iv (AT_ACID)
766 const_iv (AT_DRAIN)
767 const_iv (AT_WEAPONMAGIC)
768 const_iv (AT_GHOSTHIT)
769 const_iv (AT_POISON)
770 const_iv (AT_SLOW)
771 const_iv (AT_PARALYZE)
772 const_iv (AT_TURN_UNDEAD)
773 const_iv (AT_FEAR)
774 const_iv (AT_CANCELLATION)
775 const_iv (AT_DEPLETE)
776 const_iv (AT_DEATH)
777 const_iv (AT_CHAOS)
778 const_iv (AT_COUNTERSPELL)
779 const_iv (AT_GODPOWER)
780 const_iv (AT_HOLYWORD)
781 const_iv (AT_BLIND)
782 const_iv (AT_INTERNAL)
783 const_iv (AT_LIFE_STEALING)
784 const_iv (AT_DISEASE)
785
688 const_iv (QUEST_IN_PROGRESS) 786 const_iv (QUEST_IN_PROGRESS)
689 const_iv (QUEST_DONE_QUEST) 787 const_iv (QUEST_DONE_QUEST)
690 const_iv (QUEST_DONE_TASK) 788 const_iv (QUEST_DONE_TASK)
691 const_iv (QUEST_START_QUEST) 789 const_iv (QUEST_START_QUEST)
692 const_iv (QUEST_END_QUEST) 790 const_iv (QUEST_END_QUEST)
832 const_iv (F_CURSED) 930 const_iv (F_CURSED)
833 const_iv (F_DAMNED) 931 const_iv (F_DAMNED)
834 const_iv (F_OPEN) 932 const_iv (F_OPEN)
835 const_iv (F_NOPICK) 933 const_iv (F_NOPICK)
836 const_iv (F_LOCKED) 934 const_iv (F_LOCKED)
935
936 const_iv (P_BLOCKSVIEW)
937 const_iv (P_NO_MAGIC)
938 const_iv (P_IS_ALIVE)
939 const_iv (P_NO_CLERIC)
940 const_iv (P_NEED_UPDATE)
941 const_iv (P_NO_ERROR)
942 const_iv (P_OUT_OF_MAP)
943 const_iv (P_NEW_MAP)
944
945 const_iv (UP_OBJ_INSERT)
946 const_iv (UP_OBJ_REMOVE)
947 const_iv (UP_OBJ_CHANGE)
948 const_iv (UP_OBJ_FACE)
949
950 const_iv (INS_NO_MERGE)
951 const_iv (INS_ABOVE_FLOOR_ONLY)
952 const_iv (INS_NO_WALK_ON)
953 const_iv (INS_ON_TOP)
954 const_iv (INS_BELOW_ORIGINATOR)
955 const_iv (INS_MAP_LOAD)
956
957 const_iv (WILL_APPLY_HANDLE)
958 const_iv (WILL_APPLY_TREASURE)
959 const_iv (WILL_APPLY_EARTHWALL)
960 const_iv (WILL_APPLY_DOOR)
961 const_iv (WILL_APPLY_FOOD)
837 }; 962 };
838 963
839 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 964 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
840 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 965 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
841 966
842 const struct { 967 static const struct {
843 const char *name; 968 const char *name;
844 IV iv; 969 IV iv;
845 } *event, event_list[] = { 970 } *event, event_list[] = {
846# define const_event(name) { # name, (IV)EVENT_ ## name }, 971# define const_event(name) { # name, (IV)EVENT_ ## name },
847 const_event (NONE) 972 const_event (NONE)
855 const_event (TIME) 980 const_event (TIME)
856 const_event (THROW) 981 const_event (THROW)
857 const_event (TRIGGER) 982 const_event (TRIGGER)
858 const_event (CLOSE) 983 const_event (CLOSE)
859 const_event (TIMER) 984 const_event (TIMER)
985 const_event (MOVE)
860 986
861 const_event (BORN) 987 const_event (BORN)
862 const_event (CLOCK) 988 const_event (CLOCK)
863 const_event (CRASH) 989 const_event (CRASH)
864 const_event (PLAYER_DEATH) 990 const_event (PLAYER_DEATH)
871 const_event (REMOVE) 997 const_event (REMOVE)
872 const_event (SHOUT) 998 const_event (SHOUT)
873 const_event (TELL) 999 const_event (TELL)
874 const_event (MUZZLE) 1000 const_event (MUZZLE)
875 const_event (KICK) 1001 const_event (KICK)
1002 //const_event (FREE_OB)
876 }; 1003 };
877 1004
878 AV *av = get_av ("cf::EVENT", 1); 1005 AV *av = get_av ("cf::EVENT", 1);
879 1006
880 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; ) 1007 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; )
881 av_store (av, event->iv, newSVpv ((char *)event->name, 0)); 1008 av_store (av, event->iv, newSVpv ((char *)event->name, 0));
882 1009
883 const struct { 1010 static const struct {
884 int dtype; 1011 int dtype;
885 const char *name; 1012 const char *name;
886 IV idx; 1013 IV idx;
887 } *cprop, prop_table[] = { 1014 } *cprop, prop_table[] = {
888# define prop(type, name) { type, # name, (IV) CFAPI_ ## name }, 1015# define prop(type, name) { type, # name, (IV) CFAPI_ ## name },
1067 cf_object_set_int_property (obj, idx, SvIV (newval)); 1194 cf_object_set_int_property (obj, idx, SvIV (newval));
1068 break; 1195 break;
1069 case CFAPI_LONG: 1196 case CFAPI_LONG:
1070 cf_object_set_long_property (obj, idx, SvNV (newval)); 1197 cf_object_set_long_property (obj, idx, SvNV (newval));
1071 break; 1198 break;
1199 case CFAPI_DOUBLE:
1200 {
1201 int unused_type;
1202 object_set_property (&unused_type, obj, idx, (double)SvNV (newval));
1203 }
1204 break;
1072 case CFAPI_STRING: 1205 case CFAPI_STRING:
1073 cf_object_set_string_property (obj, idx, SvPV_nolen (newval)); 1206 cf_object_set_string_property (obj, idx, SvOK (newval) ? SvPV_nolen (newval) : 0);
1207 break;
1208 case CFAPI_POBJECT:
1209 {
1210 int unused_type;
1211 object_set_property (&unused_type, obj, idx, (object *)SvPTR_ornull (newval, "cf::object"));
1212 }
1074 break; 1213 break;
1075 default: 1214 default:
1076 croak ("unhandled type '%d' in set_property '%d'", type, idx); 1215 croak ("unhandled type '%d' in set_property '%d'", type, idx);
1077 } 1216 }
1078 1217
1218# missing properties
1219
1220void
1221set_attacktype (object *obj, U32 attacktype)
1222 CODE:
1223 obj->attacktype = attacktype;
1224
1225U32
1226get_attacktype (object *obj)
1227 ALIAS:
1228 attacktype = 0
1229 CODE:
1230 RETVAL = obj->attacktype;
1231 OUTPUT: RETVAL
1232
1233void
1234set_food (object *obj, int food)
1235 CODE:
1236 obj->stats.food = food;
1237
1238int
1239get_food (object *obj)
1240 ALIAS:
1241 food = 0
1242 CODE:
1243 RETVAL = obj->stats.food;
1244 OUTPUT: RETVAL
1245
1246void
1247inv (object *obj)
1248 PROTOTYPE: $
1249 PPCODE:
1250{
1251 object *o;
1252 for (o = obj->inv; o; o = o->below)
1253 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1254}
1255
1079int cf_object_get_resistance (object *op, int rtype) 1256int cf_object_get_resistance (object *op, int rtype)
1080 ALIAS: resistance = 0 1257 ALIAS: resistance = 0
1081 1258
1082int cf_object_get_flag (object *op, int flag) 1259int cf_object_get_flag (object *op, int flag)
1083 ALIAS: flag = 0 1260 ALIAS: flag = 0
1084 1261
1085void cf_object_set_flag (object *op, int flag, int value) 1262void cf_object_set_flag (object *op, int flag, int value)
1086 1263
1087void cf_object_move (object *op, object *originator, int dir) 1264void cf_object_move (object *op, int dir, object *originator = op)
1088 1265
1089void cf_object_apply (object *op, object *author, int flags) 1266void cf_object_apply (object *op, object *author, int flags = 0)
1090 1267
1091void cf_object_apply_below (object *op) 1268void cf_object_apply_below (object *op)
1092 1269
1093void cf_object_remove (object *op) 1270void cf_object_remove (object *op)
1094 1271
1098 1275
1099int cf_object_transfer (object *op, int x, int y, int r, object *orig) 1276int cf_object_transfer (object *op, int x, int y, int r, object *orig)
1100 1277
1101int cf_object_change_map (object *op, int x, int y, mapstruct *map) 1278int cf_object_change_map (object *op, int x, int y, mapstruct *map)
1102 1279
1103object *cf_object_clone (object *op, int clonetype) 1280object *cf_object_clone (object *op, int clonetype = 0)
1104 1281
1105int cf_object_pay_item (object *op, object *buyer) 1282int cf_object_pay_item (object *op, object *buyer)
1106 1283
1107int cf_object_pay_amount (object *op, double amount) 1284int cf_object_pay_amount (object *op, double amount)
1108 1285
1109int cf_object_cast_spell (object *caster, object *ctoo, int dir, object *sp_, char *flags) 1286int cf_object_cast_spell (object *caster, object *ctoo, int dir, object *spell_ob, char *stringarg = 0)
1110 1287
1111int cf_object_cast_ability (object *caster, object *ctoo, int dir, object *sp_, char *flags) 1288int cf_object_cast_ability (object *caster, object *ctoo, int dir, object *sp_, char *stringarg = 0)
1112 1289
1113void cf_object_learn_spell (object *op, object *sp) 1290void cf_object_learn_spell (object *op, object *sp)
1114 1291
1115void cf_object_forget_spell (object *op, object *sp) 1292void cf_object_forget_spell (object *op, object *sp)
1116 1293
1151 1328
1152char *cf_object_get_key (object *op, char *keyname) 1329char *cf_object_get_key (object *op, char *keyname)
1153 ALIAS: key = 0 1330 ALIAS: key = 0
1154 1331
1155void cf_object_set_key (object *op, char *keyname, char *value) 1332void cf_object_set_key (object *op, char *keyname, char *value)
1333
1334object *cf_create_object_by_name (const char *name)
1335
1336MODULE = cf PACKAGE = cf::object PREFIX = cf_
1337
1338void cf_fix_object (object *pl)
1339 ALIAS: fix = 0
1340
1341object *cf_insert_ob_in_ob (object *ob, object *where)
1342
1343# no clean way to get an object from an archetype - stupid idiotic
1344# dumb kludgy misdesigned plug-in api slowly gets on my nerves.
1345
1346object *new (const char *archetype = 0)
1347 PROTOTYPE: ;$
1348 CODE:
1349 RETVAL = archetype ? get_archetype (archetype) : cf_create_object ();
1350 OUTPUT:
1351 RETVAL
1352
1353object *insert_ob_in_map_at (object *ob, mapstruct *where, object_ornull *orig, int flag, int x, int y)
1354 PROTOTYPE: $$$$$$
1355 CODE:
1356{
1357 int unused_type;
1358 RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y);
1359}
1360
1361object *get_nearest_player (object *ob)
1362 ALIAS: nearest_player = 0
1363 PREINIT:
1364 extern object *get_nearest_player (object *);
1365
1366void rangevector (object *ob, object *other, int flags = 0)
1367 PROTOTYPE: $$;$
1368 PPCODE:
1369{
1370 rv_vector rv;
1371 get_rangevector (ob, other, &rv, flags);
1372 EXTEND (SP, 5);
1373 PUSHs (newSVuv (rv.distance));
1374 PUSHs (newSViv (rv.distance_x));
1375 PUSHs (newSViv (rv.distance_y));
1376 PUSHs (newSViv (rv.direction));
1377 PUSHs (newSVcfapi (CFAPI_POBJECT, rv.part));
1378}
1379
1380bool on_same_map_as (object *ob, object *other)
1381 CODE:
1382 RETVAL = on_same_map (ob, other);
1383 OUTPUT: RETVAL
1156 1384
1157char * 1385char *
1158base_name (object *ob, int plural) 1386base_name (object *ob, int plural)
1159 CODE: 1387 CODE:
1160 RETVAL = cf_query_base_name (ob, plural); 1388 RETVAL = cf_query_base_name (ob, plural);
1161 OUTPUT: RETVAL 1389 OUTPUT: RETVAL
1162 1390
1163MODULE = cf PACKAGE = cf::object PREFIX = cf_object_
1164
1165object *cf_create_object ()
1166 PROTOTYPE:
1167 ALIAS: new = 0
1168
1169object *cf_create_object_by_name (const char *name)
1170 PROTOTYPE: $
1171 ALIAS: new_from_name = 0
1172
1173void cf_free_object (object *ob)
1174
1175void cf_fix_object (object *pl)
1176
1177object *cf_insert_ob_in_ob (object *ob, object *where)
1178
1179 1391
1180MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_ 1392MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_
1181 1393
1182player *player (object *op) 1394player *player (object *op)
1183 CODE: 1395 CODE:
1186 1398
1187void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE) 1399void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE)
1188 1400
1189object *cf_player_send_inventory (object *op) 1401object *cf_player_send_inventory (object *op)
1190 1402
1403player *contr (object *op)
1404 CODE:
1405 RETVAL = op->contr;
1406 OUTPUT: RETVAL
1407
1191char *cf_player_get_ip (object *op) 1408char *cf_player_get_ip (object *op)
1192 ALIAS: ip = 0 1409 ALIAS: ip = 0
1193 1410
1194object *cf_player_get_marked_item (object *op) 1411object *cf_player_get_marked_item (object *op)
1195 ALIAS: marked_item = 0 1412 ALIAS: marked_item = 0
1208 1425
1209player *cf_player_find (char *name) 1426player *cf_player_find (char *name)
1210 PROTOTYPE: $ 1427 PROTOTYPE: $
1211 1428
1212void cf_player_move (player *pl, int dir) 1429void cf_player_move (player *pl, int dir)
1430
1431void MapNewmapCmd (player *pl)
1213 1432
1214# nonstandard 1433# nonstandard
1215object *ob (player *pl) 1434object *ob (player *pl)
1216 CODE: 1435 CODE:
1217 RETVAL = pl->ob; 1436 RETVAL = pl->ob;
1218 OUTPUT: RETVAL 1437 OUTPUT: RETVAL
1438
1439player *first ()
1440 CODE:
1441 RETVAL = first_player;
1442 OUTPUT: RETVAL
1443
1444player *next (player *pl)
1445 CODE:
1446 RETVAL = pl->next;
1447 OUTPUT: RETVAL
1448
1449void
1450list ()
1451 PPCODE:
1452{
1453 player *pl;
1454 for (pl = first_player; pl; pl = pl->next)
1455 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl));
1456}
1219 1457
1220 1458
1221MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ 1459MODULE = cf PACKAGE = cf::map PREFIX = cf_map_
1222 1460
1223SV * 1461SV *
1236 break; 1474 break;
1237 default: 1475 default:
1238 croak ("unhandled type '%d' in set_property '%d'", type, idx); 1476 croak ("unhandled type '%d' in set_property '%d'", type, idx);
1239 } 1477 }
1240 1478
1479mapstruct *new (int width, int height)
1480 PROTOTYPE:
1481 CODE:
1482{
1483 int unused_type;
1484 RETVAL = map_get_map (&unused_type, 0, width, height);
1485}
1486 OUTPUT:
1487 RETVAL
1488
1241mapstruct *cf_map_get_map (char *name) 1489mapstruct *cf_map_get_map (char *name)
1242 PROTOTYPE: $ 1490 PROTOTYPE: $
1243 ALIAS: map = 0 1491 ALIAS: map = 0
1244 1492
1245mapstruct *cf_map_get_first () 1493mapstruct *cf_map_get_first ()
1253object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny) 1501object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny)
1254 C_ARGS: str, map, nx, ny 1502 C_ARGS: str, map, nx, ny
1255 1503
1256#int cf_map_get_flags (mapstruct* map, mapstruct** nmap, I16 x, I16 y, I16 *nx, I16 *ny) 1504#int cf_map_get_flags (mapstruct* map, mapstruct** nmap, I16 x, I16 y, I16 *nx, I16 *ny)
1257 1505
1506void
1507at (mapstruct *obj, unsigned int x, unsigned int y)
1508 PROTOTYPE: $$$
1509 INIT:
1510 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_EMPTY;
1511 PPCODE:
1512{
1513 object *o;
1514 for (o = GET_MAP_OB (obj, x, y); o; o = o->above)
1515 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1516}
1517
1518SV *
1519bot_at (mapstruct *obj, unsigned int x, unsigned int y)
1520 PROTOTYPE: $$$
1521 ALIAS:
1522 top_at = 1
1523 flags_at = 2
1524 light_at = 3
1525 move_block_at = 4
1526 move_slow_at = 5
1527 move_on_at = 6
1528 move_off_at = 7
1529 INIT:
1530 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_UNDEF;
1531 CODE:
1532 switch (ix)
1533 {
1534 case 0: RETVAL = newSVcfapi (CFAPI_POBJECT, GET_MAP_OB (obj, x, y)); break;
1535 case 1: RETVAL = newSVcfapi (CFAPI_POBJECT, GET_MAP_TOP (obj, x, y)); break;
1536 case 2: RETVAL = newSVuv ( GET_MAP_FLAGS (obj, x, y)); break;
1537 case 3: RETVAL = newSViv ( GET_MAP_LIGHT (obj, x, y)); break;
1538 case 4: RETVAL = newSVuv ( GET_MAP_MOVE_BLOCK (obj, x, y)); break;
1539 case 5: RETVAL = newSVuv ( GET_MAP_MOVE_SLOW (obj, x, y)); break;
1540 case 6: RETVAL = newSVuv ( GET_MAP_MOVE_ON (obj, x, y)); break;
1541 case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break;
1542 }
1543 OUTPUT:
1544 RETVAL
1258 1545
1259 1546
1260MODULE = cf PACKAGE = cf::arch PREFIX = cf_archetype_ 1547MODULE = cf PACKAGE = cf::arch PREFIX = cf_archetype_
1261 1548
1262archetype*cf_archetype_get_first() 1549archetype *cf_archetype_get_first()
1263 PROTOTYPE: 1550 PROTOTYPE:
1264 ALIAS: first = 0 1551 ALIAS: first = 0
1265 1552
1266archetype *cf_archetype_get_next (archetype *arch) 1553archetype *cf_archetype_get_next (archetype *arch)
1267 ALIAS: next = 0 1554 ALIAS: next = 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines