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.97 by root, Thu Dec 21 22:41:35 2006 UTC vs.
Revision 1.112 by root, Tue Dec 26 03:35:59 2006 UTC

19 * GNU General Public License for more details. 19 * GNU General Public License for more details.
20 * 20 *
21 * You should have received a copy of the GNU General Public License 21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software 22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 23 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24*/ 24 */
25 25
26#define PLUGIN_NAME "perl" 26#define PLUGIN_NAME "perl"
27#define PLUGIN_VERSION "cfperl 0.5" 27#define PLUGIN_VERSION "cfperl 0.5"
28 28
29#include <plugin_common.h> 29#include <plugin_common.h>
36 36
37#include <EXTERN.h> 37#include <EXTERN.h>
38#include <perl.h> 38#include <perl.h>
39#include <XSUB.h> 39#include <XSUB.h>
40 40
41#include "CoroAPI.h"
41#include "perlxsi.c" 42#include "perlxsi.c"
42 43
43extern sint64 *levels; // the experience table 44extern sint64 *levels; // the experience table
44 45
45typedef object object_ornull; 46typedef object object_ornull;
59static f_plug_api object_set_property = cfapi_object_set_property; 60static f_plug_api object_set_property = cfapi_object_set_property;
60static f_plug_api object_insert = cfapi_object_insert; 61static f_plug_api object_insert = cfapi_object_insert;
61 62
62static PerlInterpreter *perl; 63static PerlInterpreter *perl;
63 64
65global gbl_ev;
64static AV *cb_global, *cb_object, *cb_player, *cb_client, *cb_type, *cb_map; 66static AV *cb_global, *cb_attachable, *cb_object, *cb_player, *cb_client, *cb_type, *cb_map;
67
68static HV
69 *stash_cf,
70 *stash_cf_object_wrap,
71 *stash_cf_object_player_wrap,
72 *stash_cf_player_wrap,
73 *stash_cf_map_wrap,
74 *stash_cf_client_wrap,
75 *stash_cf_arch_wrap,
76 *stash_cf_party_wrap,
77 *stash_cf_region_wrap,
78 *stash_cf_living_wrap;
79
80// helper cast function, returns super class * or 0
81template<class super>
82static super *
83is_a (attachable *at)
84{
85 //return dynamic_cast<super *>(at); // slower, safer
86 if (typeid (*at) == typeid (super))
87 return static_cast<super *>(at);
88 else
89 return 0;
90}
65 91
66////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 92//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67 93
94unordered_vector<attachable *> attachable::mortals;
95
96#if 0
97attachable *attachable::rc_first;
98
99attachable::attachable ()
100{
101 refcnt = 0;
102 rc_next = rc_first;
103 rc_first = this;
104}
105#endif
106
107attachable::~attachable ()
108{
109 assert (!(flags & F_BORROWED));//D//TODO//remove when stable
110#if 0
111 assert (!rc_next);
112 assert (!refcnt);
113#endif
114}
115
116// check wether the object really is dead
117void
118attachable::do_check ()
119{
120 if (refcnt > 0)
121 return;
122
123 // try to unborrow the refcnt from perl
124 if (flags & F_BORROWED)
125 {
126 assert (self);//D//TODO//remove when stable
127 flags &= ~F_BORROWED;
128 refcnt_inc ();
129 SvREFCNT_dec (self);
130 }
131
132 if (refcnt > 0 || self)
133 return;
134
135 destroy ();
136}
137
138void
139attachable::do_destroy ()
140{
141 invoke (EVENT_ATTACHABLE_DESTROY, DT_END);
142
143 //TODO: call generic destroy callback
144 mortals.push_back (this);
145}
146
147void
148attachable::destroy ()
149{
150 if (destroyed ())
151 return;
152
153 flags |= F_DESTROYED;
154 do_destroy ();
155}
156
157void attachable::check_mortals ()
158{
159 for (int i = 0; i < mortals.size (); )
160 {
161 attachable *obj = mortals [i];
162
163 obj->refcnt_chk (); // unborrow from perl, if necessary
164
165 if (obj->refcnt || obj->self)
166 {
167#if 0
168 if (mortals.size() > 5)fprintf (stderr, "%d delaying %d:%p:%s %d (self %p:%d)\n", time(0),i, obj, typeid (*obj).name (),
169 obj->refcnt, obj->self, obj->self ? SvREFCNT(obj->self): - 1);//D
170#endif
171
172 ++i; // further delay freeing
173 }//D
174 else
175 {
176 //Dfprintf (stderr, "deleteing %d:%p:%s\n", i, obj,typeid (*obj).name ());//D
177 mortals.erase (i);
178 delete obj;
179 }
180 }
181}
182
183attachable &
184attachable::operator =(const attachable &src)
185{
186 //if (self || cb)
187 //INVOKE_OBJECT (CLONE, this, ARG_OBJECT (dst));
188
189 attach = src.attach;
190 return *this;
191}
192
193//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
194
68static SV * 195static SV *
69newSVptr (void *ptr, const char *klass, HV *hv = newHV ()) 196newSVptr (void *ptr, HV *stash, HV *hv = newHV ())
70{ 197{
71 SV *sv; 198 SV *sv;
72 199
73 if (!ptr) 200 if (!ptr)
74 return &PL_sv_undef; 201 return &PL_sv_undef;
75 202
76 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); 203 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, 0, (char *)ptr, 0);
77 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 204 return sv_bless (newRV_noinc ((SV *)hv), stash);
78} 205}
79 206
80template<class attachable> 207static int
208attachable_free (pTHX_ SV *sv, MAGIC *mg)
209{
210 attachable *at = (attachable *)mg->mg_ptr;
211 assert (!(at->flags & attachable::F_BORROWED));//D//TODO//remove when stable
212 at->self = 0;
213 // next line makes sense, but most objects still have refcnt 0 by default
214 //at->refcnt_chk ();
215 return 0;
216}
217
218static MGVTBL vtbl_attachable = {0, 0, 0, 0, attachable_free};
219
81SV * 220SV *
82newSVattachable (attachable *obj, const char *klass) 221newSVattachable (attachable *obj, HV *stash)
83{ 222{
84 if (!obj) 223 if (!obj)
85 return &PL_sv_undef; 224 return &PL_sv_undef;
86 225
87 if (!obj->self) 226 if (!obj->self)
88 obj->self = newSVptr (obj, klass); 227 {
228 obj->self = newHV ();
229 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &vtbl_attachable, (char *)obj, 0);
89 230
90 return newSVsv (obj->self); 231 // borrow the refcnt from the object
232 obj->flags |= attachable::F_BORROWED;
233 obj->refcnt_dec ();
234 }
235
236 return sv_bless (newRV_inc ((SV *)obj->self), stash);
91} 237}
92 238
93static void 239static void
94clearSVptr (SV *sv) 240clearSVptr (SV *sv)
95{ 241{
136inline SV *to_sv (unsigned long v) { return newSVuv (v); } 282inline SV *to_sv (unsigned long v) { return newSVuv (v); }
137inline SV *to_sv ( signed long long v) { return newSVval64 (v); } 283inline SV *to_sv ( signed long long v) { return newSVval64 (v); }
138inline SV *to_sv (unsigned long long v) { return newSVval64 (v); } 284inline SV *to_sv (unsigned long long v) { return newSVval64 (v); }
139inline SV *to_sv (float v) { return newSVnv (v); } 285inline SV *to_sv (float v) { return newSVnv (v); }
140inline SV *to_sv (double v) { return newSVnv (v); } 286inline SV *to_sv (double v) { return newSVnv (v); }
141inline SV *to_sv (client * v) { return newSVattachable (v, "cf::client::wrap"); } 287inline SV *to_sv (client * v) { return newSVattachable (v, stash_cf_client_wrap); }
142inline SV *to_sv (player * v) { return newSVattachable (v, "cf::player::wrap"); } 288inline SV *to_sv (player * v) { return newSVattachable (v, stash_cf_player_wrap); }
143inline SV *to_sv (object * v) { return newSVattachable (v, v && v->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap"); } 289inline SV *to_sv (object * v) { return newSVattachable (v, v && v->type == PLAYER ? stash_cf_object_player_wrap : stash_cf_object_wrap); }
144inline SV *to_sv (maptile * v) { return newSVattachable (v, "cf::map::wrap"); } 290inline SV *to_sv (maptile * v) { return newSVattachable (v, stash_cf_map_wrap); }
145inline SV *to_sv (archetype * v) { return newSVptr (v, "cf::arch::wrap"); } 291inline SV *to_sv (archetype * v) { return newSVattachable (v, stash_cf_arch_wrap); }
146inline SV *to_sv (partylist * v) { return newSVptr (v, "cf::party::wrap"); } 292inline SV *to_sv (partylist * v) { return newSVptr (v, stash_cf_party_wrap); }
147inline SV *to_sv (region * v) { return newSVptr (v, "cf::region::wrap"); } 293inline SV *to_sv (region * v) { return newSVptr (v, stash_cf_region_wrap); }
148inline SV *to_sv (living * v) { return newSVptr (v, "cf::living::wrap"); } 294inline SV *to_sv (living * v) { return newSVptr (v, stash_cf_living_wrap); }
149 295
150inline SV *to_sv (object & v) { return to_sv (&v); } 296inline SV *to_sv (object & v) { return to_sv (&v); }
151inline SV *to_sv (living & v) { return to_sv (&v); } 297inline SV *to_sv (living & v) { return to_sv (&v); }
152 298
153//TODO: 299//TODO:
192inline void sv_to (SV *sv, refptr<T> &v) { T *tmp; sv_to (sv, tmp); v = tmp; } 338inline void sv_to (SV *sv, refptr<T> &v) { T *tmp; sv_to (sv, tmp); v = tmp; }
193 339
194template<int N> 340template<int N>
195inline void sv_to (SV *sv, char (&v)[N]) { assign (v, SvPV_nolen (sv)); } 341inline void sv_to (SV *sv, char (&v)[N]) { assign (v, SvPV_nolen (sv)); }
196 342
343inline void sv_to (SV *sv, rangetype &v) { v = (rangetype) SvIV (sv); }
344inline void sv_to (SV *sv, bowtype_t &v) { v = (bowtype_t) SvIV (sv); }
345inline void sv_to (SV *sv, petmode_t &v) { v = (petmode_t) SvIV (sv); }
346inline void sv_to (SV *sv, usekeytype &v) { v = (usekeytype) SvIV (sv); }
347inline void sv_to (SV *sv, unapplymode &v) { v = (unapplymode) SvIV (sv); }
348
197inline void sv_to (SV *sv, UUID &v) 349inline void sv_to (SV *sv, UUID &v)
198{ 350{
199 unsigned int version; 351 unsigned int version;
200 352
201 if (2 != sscanf (SvPV_nolen (sv), "<%d.%" SCNx64 ">", &version, &v.seq) || 1 != version) 353 if (2 != sscanf (SvPV_nolen (sv), "<%d.%" SCNx64 ">", &version, &v.seq) || 1 != version)
202 croak ("unparsable uuid: %s", SvPV_nolen (sv)); 354 croak ("unparsable uuid: %s", SvPV_nolen (sv));
203} 355}
356
357inline void sv_to (SV *sv, object::flags_t::reference v) { v = SvTRUE (sv); }
204 358
205static SV * 359static SV *
206newSVdt_va (va_list &ap, data_type type) 360newSVdt_va (va_list &ap, data_type type)
207{ 361{
208 SV *sv; 362 SV *sv;
315} 469}
316 470
317////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 471//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
318 472
319SV * 473SV *
320registry (attachable_base *ext) 474registry (attachable *ext)
321{ 475{
322 if (!ext->cb) 476 if (!ext->cb)
323 ext->cb = newAV (); 477 ext->cb = newAV ();
324 478
325 return newRV_inc ((SV *)ext->cb); 479 return newRV_inc ((SV *)ext->cb);
326} 480}
327 481
482#if 0
328void attachable_base::clear () 483void attachable::clear ()
329{ 484{
330 if (self) 485 if (self)
331 { 486 {
332 if (cb)
333 if (SvROK (*av_fetch (cb, EVENT_OBJECT_DESTROY, 1)))
334 INVOKE_OBJECT (DESTROY, static_cast<object *>(this));
335 else if (SvROK (*av_fetch (cb, EVENT_MAP_DESTROY, 1)))
336 INVOKE_MAP (DESTROY, static_cast<maptile *>(this));
337
338 // disconnect Perl from C, to avoid crashes 487 // disconnect Perl from C, to avoid crashes
339 sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext); 488 sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext);
340 489
341 // clear the perl hash, might or might not be a good idea 490 // clear the perl hash, might or might not be a good idea
342 hv_clear ((HV *)SvRV ((SV *)self)); 491 hv_clear ((HV *)SvRV ((SV *)self));
351 cb = 0; 500 cb = 0;
352 } 501 }
353 502
354 attach = 0; 503 attach = 0;
355} 504}
505#endif
356 506
357void attachable_base::optimise () 507void attachable::optimise ()
358{ 508{
359 if (!self) 509 if (self
360 return;
361
362 HV *hv = (HV *)SvRV ((SV *)self);
363
364 if (SvREFCNT ((SV *)self) == 1
365 && SvREFCNT ((SV *)hv) == 1 510 && SvREFCNT (self) == 1
366 && !HvKEYS (hv)) 511 && !HvTOTALKEYS (self))
367 { 512 {
513 flags &= ~F_BORROWED;
514 refcnt_inc ();
368 SvREFCNT_dec ((SV *)self); 515 SvREFCNT_dec ((SV *)self);
369 self = 0;
370 }
371}
372
373void attachable_base::instantiate (data_type type, void *obj)
374{
375 dSP;
376 ENTER;
377 SAVETMPS;
378 PUSHMARK (SP);
379 EXTEND (SP, 2);
380 PUSHs (sv_2mortal (newSVdt (type, obj)));
381 PUSHs (sv_2mortal (newSVpv (attach, 0)));
382
383 attach = 0;
384
385 PUTBACK;
386 call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL);
387 FREETMPS;
388 LEAVE;
389
390 switch (type)
391 {
392 case DT_OBJECT: INVOKE_OBJECT (INSTANTIATE, obj); break;
393 case DT_MAP: INVOKE_MAP (INSTANTIATE, obj); break;
394 } 516 }
395} 517}
396 518
397///////////////////////////////////////////////////////////////////////////// 519/////////////////////////////////////////////////////////////////////////////
398
399void reattach (data_type type, void *obj)
400{
401 //TODO only do this when the object has _attachment's
402
403 dSP;
404 ENTER;
405 SAVETMPS;
406 PUSHMARK (SP);
407 XPUSHs (sv_2mortal (newSVdt (type, obj)));
408 PUTBACK;
409 call_pv ("cf::reattach", G_DISCARD | G_VOID | G_EVAL);
410 FREETMPS;
411 LEAVE;
412
413 switch (type)
414 {
415 case DT_OBJECT: INVOKE_OBJECT (REATTACH, obj); break;
416 case DT_PLAYER: INVOKE_PLAYER (REATTACH, obj); break;
417 case DT_CLIENT: INVOKE_CLIENT (REATTACH, obj); break;
418 case DT_MAP: INVOKE_MAP (REATTACH, obj); break;
419 }
420}
421
422template<class subclass>
423void reattach (attachable<subclass> *obj)
424{
425 obj->optimise ();
426
427 if (obj->self)
428 reattach ((data_type) cftype<subclass>::dt, (subclass *)obj);
429}
430 520
431#include "kw_hash.h" 521#include "kw_hash.h"
432 522
433object_freezer::object_freezer () 523object_freezer::object_freezer ()
434: dynbuf (128 * 1024, 64 * 1024) 524: dynbuf (128 * 1024, 64 * 1024)
439object_freezer::~object_freezer () 529object_freezer::~object_freezer ()
440{ 530{
441 SvREFCNT_dec (av); 531 SvREFCNT_dec (av);
442} 532}
443 533
444void object_freezer::put (attachable_base *ext) 534void object_freezer::put (attachable *ext)
445{ 535{
446 ext->optimise (); 536 ext->optimise ();
447 537
448 if (ext->self) 538 if (ext->self)
449 { 539 {
450 int idx = AvFILLp ((AV *)av) + 1; 540 int idx = AvFILLp ((AV *)av) + 1;
451 av_store (av, idx, SvREFCNT_inc (ext->self)); 541 av_store (av, idx, newRV_inc ((SV *)ext->self));
452 542
453 add ((void *)"oid ", 4); 543 add ((void *)"oid ", 4);
454 add ((sint32)idx); 544 add ((sint32)idx);
455 add ('\n'); 545 add ('\n');
456 } 546 }
568 text = newSVpv (data, 0); 658 text = newSVpv (data, 0);
569 sv_catpv (text, thawer_eof); 659 sv_catpv (text, thawer_eof);
570 line = SvPVbyte_nolen (text); 660 line = SvPVbyte_nolen (text);
571} 661}
572 662
573void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid) 663void object_thawer::get (attachable *obj, int oid)
574{ 664{
575 if (!av || oid < 0) // this is actually an error of sorts 665 if (!av || oid < 0) // this is actually an error of sorts
576 return; 666 return;
577 667
578 // we have to "re-instantiate"/reattach to an object, so nuke ext->attach 668 // object must be virgin
579 ext->clear (); 669 assert (!obj->self);
580 670
581 SV **svp = av_fetch ((AV *)av, oid, 0); 671 SV **svp = av_fetch ((AV *)av, oid, 0);
582 672
583 if (!svp || !SvROK (*svp)) 673 if (!svp || !SvROK (*svp))
584 { 674 {
585 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); 675 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
586 return; 676 return;
587 } 677 }
588 678
589 ext->self = *svp; *svp = &PL_sv_undef; 679 obj->self = (HV *)SvRV (*svp);
590 sv_magic (SvRV (ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0); 680 SvRV_set (*svp, &PL_sv_undef);
591 681
592 reattach (type, obj); 682 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &vtbl_attachable, (char *)obj, 0);
683 obj->reattach ();
684
685 // borrow a refcount for the perl object
686 obj->flags |= attachable::F_BORROWED;
687 obj->refcnt_dec ();
593} 688}
594 689
595object_thawer::~object_thawer () 690object_thawer::~object_thawer ()
596{ 691{
597 if (text) SvREFCNT_dec (text); 692 if (text) SvREFCNT_dec (text);
835 PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 930 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
836 931
837 char *argv[] = { 932 char *argv[] = {
838 "", 933 "",
839 "-e" 934 "-e"
840 "use Event;" // required for bootstrap 935 "use Event; use Coro;" // required for bootstrap
841 "cf->bootstrap;" // required for datadir :*> 936 "cf->bootstrap;" // required for datadir :*>
842 "unshift @INC, cf::datadir ();" 937 "unshift @INC, cf::datadir ();"
843 "require cf;" 938 "require cf;"
844 }; 939 };
845 940
846 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) 941 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl))
855 dSP; 950 dSP;
856 951
857 PUSHMARK (SP); 952 PUSHMARK (SP);
858 PUTBACK; 953 PUTBACK;
859 call_pv ("cf::main", G_DISCARD | G_VOID); 954 call_pv ("cf::main", G_DISCARD | G_VOID);
955}
956
957void
958attachable::instantiate ()
959{
960 if (attach)
961 {
962 invoke (EVENT_ATTACHABLE_INSTANTIATE, ARG_STRING (attach), DT_END);
963 attach = 0;
964 }
965}
966
967void
968attachable::reattach ()
969{
970 optimise ();
971 //TODO: check for _attachment's, very important for restarts
972 invoke (EVENT_ATTACHABLE_REATTACH, DT_END);
860} 973}
861 974
862static event_klass klass_of[NUM_EVENT_TYPES] = { 975static event_klass klass_of[NUM_EVENT_TYPES] = {
863# define def(type,name) KLASS_ ## type, 976# define def(type,name) KLASS_ ## type,
864# include "eventinc.h" 977# include "eventinc.h"
895 } 1008 }
896 } 1009 }
897 } 1010 }
898} 1011}
899 1012
1013void
1014attachable::gather_callbacks (AV *&callbacks, event_type event) const
1015{
1016 ::gather_callbacks (callbacks, cb_attachable, event);
1017
1018 if (cb)
1019 ::gather_callbacks (callbacks, cb, event);
1020}
1021
1022void
1023global::gather_callbacks (AV *&callbacks, event_type event) const
1024{
1025 ::gather_callbacks (callbacks, cb_object, event);
1026}
1027
1028void
1029object::gather_callbacks (AV *&callbacks, event_type event) const
1030{
1031 if (subtype && type + subtype * NUM_SUBTYPES <= AvFILLp (cb_type))
1032 {
1033 SV *registry = AvARRAY (cb_type)[type + subtype * NUM_SUBTYPES];
1034
1035 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
1036 ::gather_callbacks (callbacks, (AV *)SvRV (registry), event);
1037 }
1038
1039 if (type <= AvFILLp (cb_type))
1040 {
1041 SV *registry = AvARRAY (cb_type)[type];
1042
1043 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
1044 ::gather_callbacks (callbacks, (AV *)SvRV (registry), event);
1045 }
1046
1047 attachable::gather_callbacks (callbacks, event);
1048 ::gather_callbacks (callbacks, cb_object, event);
1049}
1050
1051void
1052archetype::gather_callbacks (AV *&callbacks, event_type event) const
1053{
1054 attachable::gather_callbacks (callbacks, event);
1055 //TODO//::gather_callbacks (callbacks, cb_archetype, event);
1056}
1057
1058void
1059client::gather_callbacks (AV *&callbacks, event_type event) const
1060{
1061 attachable::gather_callbacks (callbacks, event);
1062 ::gather_callbacks (callbacks, cb_client, event);
1063}
1064
1065void
1066player::gather_callbacks (AV *&callbacks, event_type event) const
1067{
1068 attachable::gather_callbacks (callbacks, event);
1069 ::gather_callbacks (callbacks, cb_player, event);
1070}
1071
1072void
1073maptile::gather_callbacks (AV *&callbacks, event_type event) const
1074{
1075 attachable::gather_callbacks (callbacks, event);
1076 ::gather_callbacks (callbacks, cb_map, event);
1077}
1078
1079bool
900bool cfperl_invoke (event_type event, ...) 1080attachable::invoke (event_type event, ...)
901{ 1081{
902 data_type dt; 1082 data_type dt;
903 va_list ap; 1083 va_list ap;
904 1084
905 va_start (ap, event); 1085 va_start (ap, event);
906 1086
907 AV *callbacks = 0;
908
909 object *op;
910 player *pl;
911 maptile *map;
912 client *ns;
913
914 // callback call ordering is: 1087 // callback call ordering should be:
915 // 1. per-object callback 1088 // 1. per-object callback
916 // 2. per-class object 1089 // 2. per-class object
917 // 3. per-type callback 1090 // 3. per-type callback
918 // 4. global callbacks 1091 // 4. global callbacks
919 1092
1093 AV *callbacks = 0;
920 gather_callbacks (callbacks, cb_global, event); 1094 gather_callbacks (callbacks, event);
921
922 switch (KLASS_OF (event))
923 {
924 case KLASS_GLOBAL:
925 break;
926
927 case KLASS_OBJECT:
928 dt = (data_type) va_arg (ap, int);
929 assert (("first argument must be of type object", dt == DT_OBJECT));
930 op = va_arg (ap, object *);
931
932 if (op->cb)
933 gather_callbacks (callbacks, op->cb, event);
934
935 if (op->type)
936 {
937 if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type))
938 {
939 SV *registry = AvARRAY (cb_type)[op->type + op->subtype * NUM_SUBTYPES];
940
941 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
942 gather_callbacks (callbacks, (AV *)SvRV (registry), event);
943 }
944
945 if (op->type <= AvFILLp (cb_type))
946 {
947 SV *registry = AvARRAY (cb_type)[op->type];
948
949 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
950 gather_callbacks (callbacks, (AV *)SvRV (registry), event);
951 }
952 }
953
954 gather_callbacks (callbacks, cb_object, event);
955
956 break;
957
958 case KLASS_PLAYER:
959 dt = (data_type) va_arg (ap, int);
960 assert (("first argument must be of type player", dt == DT_PLAYER));
961 pl = va_arg (ap, player *);
962
963 if (pl->cb)
964 gather_callbacks (callbacks, pl->cb, event);
965
966 gather_callbacks (callbacks, cb_player, event);
967 break;
968
969 case KLASS_MAP:
970 dt = (data_type) va_arg (ap, int);
971 assert (("first argument must be of type object", dt == DT_MAP));
972 map = va_arg (ap, maptile *);
973
974 if (map->cb)
975 gather_callbacks (callbacks, map->cb, event);
976
977 gather_callbacks (callbacks, cb_map, event);
978 break;
979
980 case KLASS_CLIENT:
981 dt = (data_type) va_arg (ap, int);
982 assert (("first argument must be of type client", dt == DT_CLIENT));
983 ns = va_arg (ap, client *);
984
985 if (ns->cb)
986 gather_callbacks (callbacks, ns->cb, event);
987
988 gather_callbacks (callbacks, cb_client, event);
989 break;
990
991 default:
992 assert (("unsupported event klass in cfperl_invoke", 0));
993 }
994 1095
995 // short-circuit processing if no callbacks found/defined 1096 // short-circuit processing if no callbacks found/defined
996 if (!callbacks) 1097 if (!callbacks)
997 return 0; 1098 return 0;
998 1099
1004 EXTEND (SP, 3); 1105 EXTEND (SP, 3);
1005 1106
1006 PUSHs (sv_2mortal (newSViv (event))); // only used for debugging nowadays 1107 PUSHs (sv_2mortal (newSViv (event))); // only used for debugging nowadays
1007 PUSHs (sv_2mortal (newRV_noinc ((SV *)callbacks))); 1108 PUSHs (sv_2mortal (newRV_noinc ((SV *)callbacks)));
1008 1109
1009 switch (KLASS_OF (event)) 1110 //TODO: unhack
1010 { 1111 if (object *op = is_a<object>(this)) PUSHs (sv_2mortal (newSVdt (DT_OBJECT, op)));
1011 case KLASS_OBJECT: PUSHs (sv_2mortal (newSVdt (DT_OBJECT, op))); break; 1112 else if (player *pl = is_a<player>(this)) PUSHs (sv_2mortal (newSVdt (DT_PLAYER, pl)));
1012 case KLASS_PLAYER: PUSHs (sv_2mortal (newSVdt (DT_PLAYER, pl))); break; 1113 else if (client *ns = is_a<client>(this)) PUSHs (sv_2mortal (newSVdt (DT_CLIENT, ns)));
1013 case KLASS_CLIENT: PUSHs (sv_2mortal (newSVdt (DT_CLIENT, pl))); break; 1114 else if (maptile *m = is_a<maptile>(this)) PUSHs (sv_2mortal (newSVdt (DT_MAP, m)));
1014 case KLASS_MAP: PUSHs (sv_2mortal (newSVdt (DT_MAP, map))); break; 1115 else if (global *gl = is_a<global>(this)) /*nop*/;
1015 } 1116 else
1117 abort (); //TODO
1016 1118
1017 for (;;) 1119 for (;;)
1018 { 1120 {
1019 dt = (data_type) va_arg (ap, int); 1121 dt = (data_type) va_arg (ap, int);
1020 1122
1021 if (dt == DT_END) 1123 if (dt == DT_END)
1022 break; 1124 break;
1023
1024 if (dt == DT_AV) 1125 else if (dt == DT_AV)
1025 { 1126 {
1026 AV *av = va_arg (ap, AV *); 1127 AV *av = va_arg (ap, AV *);
1027 1128
1028 for (int i = 0; i <= av_len (av); ++i) 1129 for (int i = 0; i <= av_len (av); ++i)
1029 XPUSHs (*av_fetch (av, i, 1)); 1130 XPUSHs (*av_fetch (av, i, 1));
1130 pe->poll = events; 1231 pe->poll = events;
1131 if (pe->poll) start (); 1232 if (pe->poll) start ();
1132 } 1233 }
1133} 1234}
1134 1235
1236void
1237_connect_to_perl ()
1238{
1239 stash_cf = gv_stashpv ("cf" , 1);
1240
1241 stash_cf_object_wrap = gv_stashpv ("cf::object::wrap", 1);
1242 stash_cf_object_player_wrap = gv_stashpv ("cf::object::player::wrap", 1);
1243 stash_cf_player_wrap = gv_stashpv ("cf::player::wrap", 1);
1244 stash_cf_map_wrap = gv_stashpv ("cf::map::wrap" , 1);
1245 stash_cf_client_wrap = gv_stashpv ("cf::client::wrap", 1);
1246 stash_cf_arch_wrap = gv_stashpv ("cf::arch::wrap" , 1);
1247 stash_cf_party_wrap = gv_stashpv ("cf::party::wrap" , 1);
1248 stash_cf_region_wrap = gv_stashpv ("cf::region::wrap", 1);
1249 stash_cf_living_wrap = gv_stashpv ("cf::living::wrap", 1);
1250
1251 cb_global = get_av ("cf::CB_GLOBAL", 1);
1252 cb_attachable = get_av ("cf::CB_ATTACHABLE", 1);
1253 cb_object = get_av ("cf::CB_OBJECT", 1);
1254 cb_player = get_av ("cf::CB_PLAYER", 1);
1255 cb_client = get_av ("cf::CB_CLIENT", 1);
1256 cb_type = get_av ("cf::CB_TYPE" , 1);
1257 cb_map = get_av ("cf::CB_MAP" , 1);
1258}
1259
1135MODULE = cf PACKAGE = cf PREFIX = cf_ 1260MODULE = cf PACKAGE = cf PREFIX = cf_
1136 1261
1137BOOT: 1262BOOT:
1138{ 1263{
1139 HV *stash = gv_stashpv ("cf", 1); 1264 _connect_to_perl ();
1140 1265
1141 I_EVENT_API (PACKAGE); 1266 I_EVENT_API (PACKAGE);
1142 watcher_base::GEventAPI = GEventAPI; 1267 watcher_base::GEventAPI = GEventAPI;
1268 I_CORO_API (PACKAGE);
1143 1269
1144 newCONSTSUB (stash, "VERSION", newSVpv (VERSION, sizeof (VERSION) - 1)); 1270 newCONSTSUB (stash_cf, "VERSION", newSVpv (VERSION, sizeof (VERSION) - 1));
1145 1271
1146 static const struct { 1272 static const struct {
1147 const char *name; 1273 const char *name;
1148 IV iv; 1274 IV iv;
1149 } *civ, const_iv[] = { 1275 } *civ, const_iv[] = {
1410 const_iv (FLAG_ACTIVATE_ON_RELEASE) 1536 const_iv (FLAG_ACTIVATE_ON_RELEASE)
1411 const_iv (FLAG_IS_WATER) 1537 const_iv (FLAG_IS_WATER)
1412 const_iv (FLAG_CONTENT_ON_GEN) 1538 const_iv (FLAG_CONTENT_ON_GEN)
1413 const_iv (FLAG_IS_A_TEMPLATE) 1539 const_iv (FLAG_IS_A_TEMPLATE)
1414 const_iv (FLAG_IS_BUILDABLE) 1540 const_iv (FLAG_IS_BUILDABLE)
1415 const_iv (FLAG_AFK)
1416 1541
1417 const_iv (NDI_BLACK) 1542 const_iv (NDI_BLACK)
1418 const_iv (NDI_WHITE) 1543 const_iv (NDI_WHITE)
1419 const_iv (NDI_NAVY) 1544 const_iv (NDI_NAVY)
1420 const_iv (NDI_RED) 1545 const_iv (NDI_RED)
1616 const_iv (MAP_IN_MEMORY) 1741 const_iv (MAP_IN_MEMORY)
1617 const_iv (MAP_SWAPPED) 1742 const_iv (MAP_SWAPPED)
1618 const_iv (MAP_LOADING) 1743 const_iv (MAP_LOADING)
1619 const_iv (MAP_SAVING) 1744 const_iv (MAP_SAVING)
1620 1745
1746 const_iv (KLASS_ATTACHABLE)
1621 const_iv (KLASS_GLOBAL) 1747 const_iv (KLASS_GLOBAL)
1622 const_iv (KLASS_OBJECT) 1748 const_iv (KLASS_OBJECT)
1623 const_iv (KLASS_CLIENT) 1749 const_iv (KLASS_CLIENT)
1624 const_iv (KLASS_PLAYER) 1750 const_iv (KLASS_PLAYER)
1625 const_iv (KLASS_MAP) 1751 const_iv (KLASS_MAP)
1752
1753 const_iv (CS_QUERY_YESNO)
1754 const_iv (CS_QUERY_SINGLECHAR)
1755 const_iv (CS_QUERY_HIDEINPUT)
1756
1757 const_iv (ST_DEAD)
1758 const_iv (ST_SETUP)
1759 const_iv (ST_PLAYING)
1760 const_iv (ST_CUSTOM)
1761
1762 const_iv (ST_CHANGE_CLASS)
1763 const_iv (ST_CONFIRM_QUIT)
1764 const_iv (ST_GET_PARTY_PASSWORD)
1626 }; 1765 };
1627 1766
1628 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1767 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
1629 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1768 newCONSTSUB (stash_cf, (char *)civ->name, newSViv (civ->iv));
1630 1769
1631 static const struct { 1770 static const struct {
1632 const char *name; 1771 const char *name;
1633 int skip; 1772 int skip;
1634 IV klass; 1773 IV klass;
1645 { 1784 {
1646 AV *event = newAV (); 1785 AV *event = newAV ();
1647 av_push (event, newSVpv ((char *)eiv->name + eiv->skip, 0)); 1786 av_push (event, newSVpv ((char *)eiv->name + eiv->skip, 0));
1648 av_push (event, newSViv (eiv->klass)); 1787 av_push (event, newSViv (eiv->klass));
1649 av_store (av, eiv->iv, newRV_noinc ((SV *)event)); 1788 av_store (av, eiv->iv, newRV_noinc ((SV *)event));
1650 newCONSTSUB (stash, (char *)eiv->name, newSViv (eiv->iv)); 1789 newCONSTSUB (stash_cf, (char *)eiv->name, newSViv (eiv->iv));
1651 } 1790 }
1652} 1791}
1653 1792
1654void _init_vars () 1793void _connect_to_perl ()
1655 CODE:
1656 cb_global = get_av ("cf::CB_GLOBAL", 1);
1657 cb_object = get_av ("cf::CB_OBJECT", 1);
1658 cb_player = get_av ("cf::CB_PLAYER", 1);
1659 cb_client = get_av ("cf::CB_CLIENT", 1);
1660 cb_type = get_av ("cf::CB_TYPE" , 1);
1661 cb_map = get_av ("cf::CB_MAP" , 1);
1662 1794
1663void _global_reattach () 1795void _global_reattach ()
1664 CODE: 1796 CODE:
1665{ 1797{
1666 // reattach to all attachable objects in the game. 1798 // reattach to all attachable objects in the game.
1667 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) 1799 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
1668 reattach (*i); 1800 (*i)->reattach ();
1669 1801
1670 for (player *pl = first_player; pl; pl = pl->next) 1802 for (player *pl = first_player; pl; pl = pl->next)
1671 reattach (pl); 1803 pl->reattach ();
1672 1804
1673 for (maptile *map = first_map; map; map = map->next) 1805 for (maptile *m = first_map; m; m = m->next)
1674 reattach (map); 1806 m->reattach ();
1675 1807
1676 for (object *op = object::first; op; op = op->next) 1808 for (object *op = object::first; op; op = op->next)
1677 reattach (op); 1809 op->reattach ();
1678} 1810}
1679 1811
1680NV floor (NV x) 1812NV floor (NV x)
1681 1813
1682NV ceil (NV x) 1814NV ceil (NV x)
1738 1870
1739int random_roll (int min, int max, object *op, int goodbad); 1871int random_roll (int min, int max, object *op, int goodbad);
1740 1872
1741const char *cost_string_from_value(uint64 cost, int approx = 0) 1873const char *cost_string_from_value(uint64 cost, int approx = 0)
1742 1874
1743int invoke (int event, ...)
1744 CODE:
1745 if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL");
1746 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1747 for (int i = 1; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1748 RETVAL = INVOKE_((event_type)event, ARG_AV (av));
1749 OUTPUT: RETVAL
1750
1751int 1875int
1752exp_to_level (val64 exp) 1876exp_to_level (val64 exp)
1753 CODE: 1877 CODE:
1754{ 1878{
1755 int i = 0; 1879 int i = 0;
1794 CODE: 1918 CODE:
1795 RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext); 1919 RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext);
1796 OUTPUT: 1920 OUTPUT:
1797 RETVAL 1921 RETVAL
1798 1922
1923MODULE = cf PACKAGE = cf::global
1924
1925int invoke (SV *klass, int event, ...)
1926 CODE:
1927 if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL");
1928 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1929 for (int i = 1; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1930 RETVAL = gbl_ev.invoke ((event_type)event, ARG_AV (av), DT_END);
1931 OUTPUT: RETVAL
1932
1799MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ 1933MODULE = cf PACKAGE = cf::object PREFIX = cf_object_
1800 1934
1801INCLUDE: $PERL genacc object ../include/object.h | 1935INCLUDE: $PERL genacc object ../include/object.h |
1802 1936
1803int invoke (object *op, int event, ...) 1937int invoke (object *op, int event, ...)
1804 CODE: 1938 CODE:
1805 if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT"); 1939 if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT");
1806 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 1940 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1807 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); 1941 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1808 RETVAL = INVOKE_((event_type)event, ARG_OBJECT (op), ARG_AV (av)); 1942 RETVAL = op->invoke ((event_type)event, ARG_AV (av), DT_END);
1809 OUTPUT: RETVAL 1943 OUTPUT: RETVAL
1810 1944
1811SV *registry (object *op) 1945SV *registry (object *op)
1812 1946
1813void mortals () 1947void mortals ()
1821 RETVAL = object::first; 1955 RETVAL = object::first;
1822 OUTPUT: RETVAL 1956 OUTPUT: RETVAL
1823 1957
1824# missing properties 1958# missing properties
1825 1959
1826int flag (object *op, int flag, int value = 1)
1827 PROTOTYPE: $$;$
1828 CODE:
1829 RETVAL = QUERY_FLAG (op, flag);
1830 if (items >= 3)
1831 if (value)
1832 SET_FLAG (op, flag);
1833 else
1834 CLEAR_FLAG (op, flag);
1835 OUTPUT: RETVAL
1836
1837object *head (object *op) 1960object *head (object *op)
1838 PROTOTYPE: $ 1961 PROTOTYPE: $
1839 CODE: 1962 CODE:
1840 RETVAL = op->head ? op->head : op; 1963 RETVAL = op->head ? op->head : op;
1841 OUTPUT: RETVAL 1964 OUTPUT: RETVAL
1851 PROTOTYPE: $ 1974 PROTOTYPE: $
1852 PPCODE: 1975 PPCODE:
1853{ 1976{
1854 object *o; 1977 object *o;
1855 for (o = obj->inv; o; o = o->below) 1978 for (o = obj->inv; o; o = o->below)
1856 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o))); 1979 XPUSHs (sv_2mortal (to_sv (o)));
1857} 1980}
1981
1982void
1983set_animation (object *op, int idx)
1984 CODE:
1985 SET_ANIMATION (op, idx);
1986
1987void
1988object::drain_stat ()
1989
1990void
1991object::drain_specific_stat (int stat)
1992
1993void
1994object::change_luck (int change)
1995
1996void
1997object::add_statbonus ()
1998
1999void
2000object::remove_statbonus ()
1858 2001
1859object *find_best_object_match (object *op, const char *match) 2002object *find_best_object_match (object *op, const char *match)
1860 2003
1861object *find_marked_object (object *op) 2004object *find_marked_object (object *op)
1862 2005
1863int resistance (object *op, int rtype, int newval = 0)
1864 CODE:
1865 if (rtype < 0 || rtype >= NROFATTACKS)
1866 croak ("resistance out of bounds");
1867 RETVAL = op->resist [rtype];
1868 if (items >= 3)
1869 op->resist [rtype] = newval;
1870 OUTPUT: RETVAL
1871
1872void set_resistance (object *op, int rtype, int val)
1873 CODE:
1874 if (rtype < 0 || rtype >= NROFATTACKS)
1875 op->resist[rtype] = val;
1876
1877int need_identify (const object *obj); 2006int need_identify (object *obj);
1878 2007
1879int apply_shop_mat (object *shop_mat, object *op); 2008int apply_shop_mat (object *shop_mat, object *op);
1880 2009
1881int move (object *op, int dir, object *originator = op) 2010int move (object *op, int dir, object *originator = op)
1882 CODE: 2011 CODE:
1895void remove (object *op) 2024void remove (object *op)
1896 CODE: 2025 CODE:
1897 op->remove (); 2026 op->remove ();
1898 2027
1899void 2028void
1900object::destroy (int recursive = 0) 2029object::destroy (bool destroy_inventory = false)
2030
2031void
2032object::destroy_inv (bool drop_to_ground = false)
1901 2033
1902object *cf_object_present_archname_inside (object *op, char *whatstr) 2034object *cf_object_present_archname_inside (object *op, char *whatstr)
1903 2035
1904int cf_object_transfer (object *op, int x, int y, int r = 0, object_ornull *orig = 0) 2036int cf_object_transfer (object *op, int x, int y, int r = 0, object_ornull *orig = 0)
1905 2037
1946 OUTPUT: RETVAL 2078 OUTPUT: RETVAL
1947 2079
1948int query_money (object *op) 2080int query_money (object *op)
1949 ALIAS: money = 0 2081 ALIAS: money = 0
1950 2082
1951int query_cost (object *op, object *who, int flags) 2083val64 query_cost (object *op, object *who, int flags)
1952 ALIAS: cost = 0 2084 ALIAS: cost = 0
1953 2085
1954void spring_trap (object *op, object *victim) 2086void spring_trap (object *op, object *victim)
1955 2087
1956int check_trigger (object *op, object *cause) 2088int check_trigger (object *op, object *cause)
1963 2095
1964object *cf_object_insert_in_ob (object *ob, object *where) 2096object *cf_object_insert_in_ob (object *ob, object *where)
1965 2097
1966int cf_object_teleport (object *op, maptile *map, int x, int y) 2098int cf_object_teleport (object *op, maptile *map, int x, int y)
1967 2099
1968void update (object *op, int action) 2100void update_object (object *op, int action)
1969 CODE:
1970 update_object (op, action);
1971 2101
1972object *cf_create_object_by_name (const char *name) 2102object *cf_create_object_by_name (const char *name)
1973 2103
1974void change_exp (object *op, uint64 exp, const char *skill_name = 0, int flag = 0) 2104void change_exp (object *op, uint64 exp, const char *skill_name = 0, int flag = 0)
1975 2105
1987 2117
1988void remove_button_link (object *op); 2118void remove_button_link (object *op);
1989 2119
1990 2120
1991MODULE = cf PACKAGE = cf::object PREFIX = cf_ 2121MODULE = cf PACKAGE = cf::object PREFIX = cf_
1992
1993void cf_fix_object (object *pl)
1994 ALIAS: fix = 0
1995 2122
1996object *cf_insert_ob_in_ob (object *ob, object *where) 2123object *cf_insert_ob_in_ob (object *ob, object *where)
1997 2124
1998# no clean way to get an object from an archetype - stupid idiotic 2125# no clean way to get an object from an archetype - stupid idiotic
1999# dumb kludgy misdesigned plug-in api slowly gets on my nerves. 2126# dumb kludgy misdesigned plug-in api slowly gets on my nerves.
2022 2149
2023player *contr (object *op) 2150player *contr (object *op)
2024 CODE: 2151 CODE:
2025 RETVAL = op->contr; 2152 RETVAL = op->contr;
2026 OUTPUT: RETVAL 2153 OUTPUT: RETVAL
2154
2155void
2156object::roll_stats ()
2157
2158void
2159object::update_stats ()
2160
2161void
2162object::swap_stats (int a, int b)
2027 2163
2028const char *get_ob_key_value (object *op, const char *key) 2164const char *get_ob_key_value (object *op, const char *key)
2029 2165
2030bool set_ob_key_value (object *op, const char *key, const char *value = 0, int add_key = 1) 2166bool set_ob_key_value (object *op, const char *key, const char *value = 0, int add_key = 1)
2031 2167
2066player *player (object *op) 2202player *player (object *op)
2067 CODE: 2203 CODE:
2068 RETVAL = op->contr; 2204 RETVAL = op->contr;
2069 OUTPUT: RETVAL 2205 OUTPUT: RETVAL
2070 2206
2207void check_score (object *op)
2208
2071void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE) 2209void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE)
2072 2210
2073object *cf_player_send_inventory (object *op) 2211object *cf_player_send_inventory (object *op)
2074 2212
2075char *cf_player_get_ip (object *op) 2213char *cf_player_get_ip (object *op)
2112int invoke (player *pl, int event, ...) 2250int invoke (player *pl, int event, ...)
2113 CODE: 2251 CODE:
2114 if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER"); 2252 if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER");
2115 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 2253 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2116 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); 2254 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
2117 RETVAL = INVOKE_((event_type)event, ARG_PLAYER (pl), ARG_AV (av)); 2255 RETVAL = pl->invoke ((event_type)event, ARG_AV (av), DT_END);
2118 OUTPUT: RETVAL 2256 OUTPUT: RETVAL
2119 2257
2120SV *registry (player *pl) 2258SV *registry (player *pl)
2259
2260player *
2261create ()
2262 CODE:
2263 RETVAL = player::create ();
2264 OUTPUT:
2265 RETVAL
2266
2267player *
2268load (const char *path)
2269 CODE:
2270 RETVAL = player::load (path);
2271 OUTPUT:
2272 RETVAL
2273
2274void
2275player::save (bool final = false)
2276
2277void
2278player::connect (client *ns)
2279
2280void
2281save_stats (player *pl)
2282 CODE:
2283 pl->ob->stats.hp = pl->ob->stats.maxhp;
2284 pl->ob->stats.sp = pl->ob->stats.maxsp;
2285 pl->ob->stats.grace = pl->ob->stats.maxgrace;
2286 pl->orig_stats = pl->ob->stats;
2121 2287
2122player *cf_player_find (char *name) 2288player *cf_player_find (char *name)
2123 PROTOTYPE: $ 2289 PROTOTYPE: $
2124 2290
2125void cf_player_move (player *pl, int dir) 2291void cf_player_move (player *pl, int dir)
2129player *first () 2295player *first ()
2130 CODE: 2296 CODE:
2131 RETVAL = first_player; 2297 RETVAL = first_player;
2132 OUTPUT: RETVAL 2298 OUTPUT: RETVAL
2133 2299
2134player *next (player *pl)
2135 CODE:
2136 RETVAL = pl->next;
2137 OUTPUT: RETVAL
2138
2139bool 2300bool
2140cell_visible (player *pl, int dx, int dy) 2301cell_visible (player *pl, int dx, int dy)
2141 CODE: 2302 CODE:
2142 RETVAL = FABS (dx) <= pl->socket->mapx / 2 && FABS (dy) <= pl->socket->mapy / 2 2303 RETVAL = FABS (dx) <= pl->ns->mapx / 2 && FABS (dy) <= pl->ns->mapy / 2
2143 && !pl->blocked_los [dx + pl->socket->mapx / 2][dy + pl->socket->mapy / 2]; 2304 && !pl->blocked_los [dx + pl->ns->mapx / 2][dy + pl->ns->mapy / 2];
2144 OUTPUT: 2305 OUTPUT:
2145 RETVAL 2306 RETVAL
2146 2307
2147void 2308void
2148send (player *pl, SV *packet) 2309send (player *pl, SV *packet)
2149 CODE: 2310 CODE:
2150{ 2311{
2151 STRLEN len; 2312 STRLEN len;
2152 char *buf = SvPVbyte (packet, len); 2313 char *buf = SvPVbyte (packet, len);
2153 2314
2315 if (pl->ns)
2154 pl->socket->send_packet (buf, len); 2316 pl->ns->send_packet (buf, len);
2155} 2317}
2156 2318
2157int 2319int
2158listening (player *pl, int new_value = -1) 2320listening (player *pl, int new_value = -1)
2159 CODE: 2321 CODE:
2178 if (y) sv_to (y, pl->bed_y); 2340 if (y) sv_to (y, pl->bed_y);
2179 2341
2180void 2342void
2181list () 2343list ()
2182 PPCODE: 2344 PPCODE:
2183{
2184 player *pl;
2185 for (pl = first_player; pl; pl = pl->next) 2345 for (player *pl = first_player; pl; pl = pl->next)
2186 XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl)); 2346 XPUSHs (sv_2mortal (to_sv (pl)));
2187}
2188 2347
2189bool 2348bool
2190peaceful (player *pl, bool new_setting = 0) 2349peaceful (player *pl, bool new_setting = 0)
2191 PROTOTYPE: $;$ 2350 PROTOTYPE: $;$
2192 CODE: 2351 CODE:
2220int invoke (maptile *map, int event, ...) 2379int invoke (maptile *map, int event, ...)
2221 CODE: 2380 CODE:
2222 if (KLASS_OF (event) != KLASS_MAP) croak ("event class must be MAP"); 2381 if (KLASS_OF (event) != KLASS_MAP) croak ("event class must be MAP");
2223 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 2382 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2224 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); 2383 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
2225 RETVAL = INVOKE_((event_type)event, ARG_MAP (map), ARG_AV (av)); 2384 RETVAL = map->invoke ((event_type)event, ARG_AV (av), DT_END);
2226 OUTPUT: RETVAL 2385 OUTPUT: RETVAL
2227 2386
2228SV *registry (maptile *map) 2387SV *registry (maptile *map)
2229 2388
2230INCLUDE: $PERL genacc maptile ../include/map.h | 2389INCLUDE: $PERL genacc maptile ../include/map.h |
2340 move_block_at = 4 2499 move_block_at = 4
2341 move_slow_at = 5 2500 move_slow_at = 5
2342 move_on_at = 6 2501 move_on_at = 6
2343 move_off_at = 7 2502 move_off_at = 7
2344 INIT: 2503 INIT:
2345 if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_UNDEF; 2504 if (x >= obj->width || y >= obj->height) XSRETURN_UNDEF;
2346 CODE: 2505 CODE:
2347 switch (ix) 2506 switch (ix)
2348 { 2507 {
2349 case 0: RETVAL = newSVcfapi (CFAPI_POBJECT, GET_MAP_OB (obj, x, y)); break; 2508 case 0: RETVAL = newSVcfapi (CFAPI_POBJECT, GET_MAP_OB (obj, x, y)); break;
2350 case 1: RETVAL = newSVcfapi (CFAPI_POBJECT, GET_MAP_TOP (obj, x, y)); break; 2509 case 1: RETVAL = newSVcfapi (CFAPI_POBJECT, GET_MAP_TOP (obj, x, y)); break;
2449int invoke (client *ns, int event, ...) 2608int invoke (client *ns, int event, ...)
2450 CODE: 2609 CODE:
2451 if (KLASS_OF (event) != KLASS_CLIENT) croak ("event class must be CLIENT"); 2610 if (KLASS_OF (event) != KLASS_CLIENT) croak ("event class must be CLIENT");
2452 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 2611 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2453 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i))); 2612 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
2454 RETVAL = INVOKE_((event_type)event, ARG_CLIENT (ns), ARG_AV (av)); 2613 RETVAL = ns->invoke ((event_type)event, ARG_AV (av), DT_END);
2455 OUTPUT: RETVAL 2614 OUTPUT: RETVAL
2456 2615
2457SV *registry (client *ns) 2616SV *registry (client *ns)
2617
2618void
2619list ()
2620 PPCODE:
2621 EXTEND (SP, clients.size ());
2622 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
2623 PUSHs (sv_2mortal (to_sv (*i)));
2458 2624
2459client * 2625client *
2460create (int fd, const char *peername) 2626create (int fd, const char *peername)
2461 CODE: 2627 CODE:
2462 RETVAL = client::create (fd, peername); 2628 RETVAL = client::create (fd, peername);
2463 OUTPUT: 2629 OUTPUT:
2464 RETVAL 2630 RETVAL
2465 2631
2466void 2632void
2633client::send_packet (SV *packet)
2634 CODE:
2635{
2636 STRLEN len;
2637 char *buf = SvPVbyte (packet, len);
2638
2639 THIS->send_packet (buf, len);
2640}
2641
2642void
2467client::destroy () 2643client::destroy ()
2468 2644

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines