ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/cfperl.xs
Revision: 1.419
Committed: Sun Nov 11 02:38:10 2012 UTC (11 years, 6 months ago) by root
Branch: MAIN
Changes since 1.418: +0 -9 lines
Log Message:
-Time::HiRes

File Contents

# User Rev Content
1 root 1.1 /*
2 root 1.264 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.411 *
4 root 1.408 * Copyright (©) 2006,2007,2008,2009,2010,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.411 *
6 root 1.325 * Deliantra is free software: you can redistribute it and/or modify it under
7     * the terms of the Affero GNU General Public License as published by the
8     * Free Software Foundation, either version 3 of the License, or (at your
9     * option) any later version.
10 root 1.411 *
11 root 1.226 * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15 root 1.411 *
16 root 1.325 * You should have received a copy of the Affero GNU General Public License
17     * and the GNU General Public License along with this program. If not, see
18     * <http://www.gnu.org/licenses/>.
19 root 1.411 *
20 root 1.264 * The authors can be reached via e-mail to <support@deliantra.net>
21 root 1.106 */
22 root 1.1
23 root 1.198 #include "autoconf.h"
24    
25     #if HAVE_EXECINFO_H
26     # include <execinfo.h>
27     #endif
28    
29 root 1.6 #include <cstdarg>
30 root 1.292 #include <typeinfo>
31 root 1.1
32 root 1.257 #include "global.h"
33 root 1.392 #include "rmg.h"
34 root 1.400 #include "noise.h"
35 root 1.272 #include "evthread.h"
36     #include "sproto.h"
37 root 1.1
38 root 1.125 #include <unistd.h>
39     #if _POSIX_MEMLOCK
40     # include <sys/mman.h>
41     #endif
42    
43 root 1.259 #if HAVE_MALLOC_H
44     # include <malloc.h>
45     #endif
46    
47 root 1.279 #if !__GLIBC__
48     # define malloc_trim(pad) -1
49     #endif
50    
51 root 1.32 #include <EXTERN.h>
52     #include <perl.h>
53     #include <XSUB.h>
54    
55 root 1.107 #include "CoroAPI.h"
56 root 1.1 #include "perlxsi.c"
57    
58 root 1.247 typedef object_thawer &object_thawer_ref;
59     typedef object_freezer &object_freezer_ref;
60 root 1.183
61 root 1.194 typedef std::string std__string;
62    
63 root 1.1 static PerlInterpreter *perl;
64    
65 root 1.220 tstamp NOW, runtime;
66 root 1.116
67 root 1.272 static int tick_inhibit;
68     static int tick_pending;
69    
70 root 1.109 global gbl_ev;
71     static AV *cb_global, *cb_attachable, *cb_object, *cb_player, *cb_client, *cb_type, *cb_map;
72 root 1.405 static SV *sv_runtime, *sv_tick_start, *sv_next_tick, *sv_now, *sv_server_tick;
73 root 1.321 static AV *av_reflect;
74 root 1.109
75 root 1.210 bitset<NUM_EVENT_TYPES> ev_want_event;
76     bitset<NUM_TYPES> ev_want_type;
77    
78 root 1.109 static HV
79     *stash_cf,
80     *stash_cf_object_wrap,
81     *stash_cf_object_player_wrap,
82     *stash_cf_player_wrap,
83     *stash_cf_map_wrap,
84 root 1.293 *stash_cf_mapspace_wrap,
85 root 1.109 *stash_cf_client_wrap,
86     *stash_cf_arch_wrap,
87     *stash_cf_party_wrap,
88     *stash_cf_region_wrap,
89 root 1.376 *stash_cf_living_wrap,
90 root 1.389 *stash_cf_layout_wrap,
91 root 1.376 *stash_ext_map_world;
92 root 1.109
93 root 1.324 static SV
94     *cv_cf_do_invoke,
95     *cv_cf__can_merge,
96     *cv_cf_client_send_msg,
97     *cv_cf_tick,
98     *cv_cf_match_match;
99    
100 root 1.305 #ifndef newSVpv_utf8
101 root 1.345 static SV *
102 root 1.246 newSVpv_utf8 (const char *s)
103     {
104 root 1.252 if (!s)
105     return newSV (0);
106    
107 root 1.246 SV *sv = newSVpv (s, 0);
108     SvUTF8_on (sv);
109     return sv;
110     }
111 root 1.305 #endif
112 root 1.246
113 root 1.305 #ifndef newSVpvn_utf8
114 root 1.345 static SV *
115 root 1.305 newSVpvn_utf8 (const char *s, STRLEN l, int utf8)
116 root 1.246 {
117 root 1.252 if (!s)
118     return newSV (0);
119    
120 root 1.246 SV *sv = newSVpvn (s, l);
121 root 1.305
122     if (utf8)
123     SvUTF8_on (sv);
124    
125 root 1.246 return sv;
126     }
127 root 1.305 #endif
128 root 1.246
129 root 1.386 noinline utf8_string
130 root 1.345 cfSvPVutf8_nolen (SV *sv)
131     {
132     SvGETMAGIC (sv);
133    
134     if (SvPOK (sv))
135     {
136     if (!SvUTF8 (sv))
137     sv_utf8_upgrade_nomg (sv);
138    
139     return SvPVX (sv);
140     }
141    
142     return SvPV_nolen (sv);
143     }
144    
145 root 1.109 // helper cast function, returns super class * or 0
146     template<class super>
147     static super *
148     is_a (attachable *at)
149     {
150     //return dynamic_cast<super *>(at); // slower, safer
151     if (typeid (*at) == typeid (super))
152     return static_cast<super *>(at);
153     else
154     return 0;
155     }
156    
157     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
158    
159     unordered_vector<attachable *> attachable::mortals;
160    
161 root 1.129 attachable::~attachable ()
162 root 1.109 {
163 root 1.133 assert (!self);
164 root 1.153 assert (!cb);
165 root 1.109 }
166    
167 root 1.130 int
168     attachable::refcnt_cnt () const
169     {
170 root 1.152 return refcnt + (self ? SvREFCNT (self) - 1 : 0);
171 root 1.130 }
172    
173     void
174 root 1.155 attachable::sever_self ()
175 root 1.109 {
176 root 1.164 if (HV *self = this->self)
177 root 1.129 {
178 root 1.155 // keep a refcount because sv_unmagic might call attachable_free,
179     // which might clear self, causing sv_unmagic to crash on a now
180     // invalid object.
181 root 1.154 SvREFCNT_inc (self);
182 root 1.155 hv_clear (self);
183     sv_unmagic ((SV *)self, PERL_MAGIC_ext);
184 root 1.154 SvREFCNT_dec (self);
185 root 1.155
186 pippijn 1.313 // self *must* be null now because that's sv_unmagic's job.
187 root 1.164 assert (!this->self);
188 root 1.129 }
189 root 1.109 }
190    
191 root 1.155 void
192     attachable::optimise ()
193     {
194     if (self
195     && SvREFCNT (self) == 1
196     && !HvTOTALKEYS (self))
197     sever_self ();
198     }
199    
200 root 1.109 // check wether the object really is dead
201     void
202     attachable::do_check ()
203     {
204 root 1.152 if (refcnt_cnt () > 0)
205 root 1.109 return;
206    
207 root 1.133 destroy ();
208 root 1.109 }
209    
210     void
211     attachable::do_destroy ()
212     {
213 root 1.214 INVOKE_ATTACHABLE (DESTROY, this);
214 root 1.109
215 root 1.153 if (cb)
216     {
217     SvREFCNT_dec (cb);
218     cb = 0;
219     }
220    
221 root 1.109 mortals.push_back (this);
222     }
223    
224     void
225     attachable::destroy ()
226     {
227     if (destroyed ())
228     return;
229    
230 root 1.336 attachable_flags |= F_DESTROYED;
231 root 1.109 do_destroy ();
232 root 1.198 sever_self ();
233 root 1.109 }
234    
235 root 1.130 void
236 root 1.367 attachable::do_delete ()
237     {
238     delete this;
239     }
240    
241     void
242 root 1.130 attachable::check_mortals ()
243 root 1.109 {
244 root 1.150 static int i = 0;
245    
246     for (;;)
247 root 1.109 {
248 root 1.150 if (i >= mortals.size ())
249     {
250     i = 0;
251    
252 root 1.221 if (mortals.size () >= 512)
253     {
254     static int last_mortalcount;
255     if (mortals.size () != last_mortalcount)
256     {
257     last_mortalcount = mortals.size ();
258 root 1.406 object *specimen = is_a<object> (mortals [rndm (mortals.size ())]);
259    
260     LOG (llevInfo, "%d mortals (random specimen: %s).\n",
261     (int)mortals.size (), specimen ? specimen->debug_desc () : "(not an object)");
262 root 1.221
263     if (0)
264     {
265     for (int j = 0; j < mortals.size (); ++j)//D
266     fprintf (stderr, "%d:%s %p ", j, &((object *)mortals[j])->name, mortals[j]);//D
267 root 1.367
268 root 1.221 fprintf (stderr, "\n");//D
269     }
270     }
271     }
272 root 1.150
273     break;
274     }
275    
276 root 1.109 attachable *obj = mortals [i];
277    
278 root 1.197 #if 0
279     if (obj->self)//D make this an assert later
280     {
281     LOG (llevError, "check_mortals: object '%s' still has self\n", typeid (obj).name ());
282     obj->sever_self ();
283     }
284     #endif
285 root 1.109
286 root 1.198 if (obj->refcnt)
287 root 1.109 {
288 root 1.150 ++i; // further delay freeing
289 root 1.109
290 root 1.150 if (!(i & 0x3ff))
291     break;
292     }
293 root 1.109 else
294     {
295 root 1.112 mortals.erase (i);
296 root 1.197 obj->sever_self ();
297 root 1.367 obj->do_delete ();
298 root 1.109 }
299     }
300     }
301    
302 root 1.228 void
303 root 1.246 attachable::set_key (const char *key, const char *value, bool is_utf8)
304 root 1.228 {
305     if (!self)
306     self = newHV ();
307    
308     if (value)
309 root 1.246 hv_store (self, key, strlen (key), is_utf8 ? newSVpv_utf8 (value) : newSVpv (value, 0), 0);
310 root 1.228 else
311     hv_delete (self, key, strlen (key), G_DISCARD);
312     }
313    
314 root 1.109 attachable &
315     attachable::operator =(const attachable &src)
316     {
317     //if (self || cb)
318     //INVOKE_OBJECT (CLONE, this, ARG_OBJECT (dst));
319    
320     attach = src.attach;
321     return *this;
322     }
323 root 1.8
324 root 1.333 #if 0
325 root 1.221 template<typename T>
326     static bool
327     find_backref (void *ptr, T *obj)
328     {
329     char *s = (char *)obj;
330     while (s < (char *)obj + sizeof (T))
331     {
332     if (ptr == *(void **)s)
333     return true;
334    
335     s += sizeof (void *); // assume natural alignment
336     }
337    
338     return false;
339     }
340    
341     // for debugging, find "live" objects containing this ptr
342 root 1.333 static void
343 root 1.221 find_backref (void *ptr)
344     {
345     for_all_objects (op)
346     if (find_backref (ptr, op))
347     fprintf (stderr, "O %p %d:'%s'\n", op, op->count, &op->name);
348    
349     for_all_players (pl)
350     if (find_backref (ptr, pl))
351     fprintf (stderr, "P %p\n", pl);
352    
353     for_all_clients (ns)
354     if (find_backref (ptr, ns))
355     fprintf (stderr, "C %p\n", ns);
356     }
357 root 1.333 #endif
358 root 1.221
359 root 1.1 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
360    
361     static SV *
362 elmex 1.379 newSVptr (void *ptr, HV *stash, HV *hv)
363 root 1.1 {
364     if (!ptr)
365 root 1.252 return newSV (0);
366 root 1.1
367 root 1.109 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, 0, (char *)ptr, 0);
368     return sv_bless (newRV_noinc ((SV *)hv), stash);
369     }
370    
371 root 1.388 static SV * noinline
372 elmex 1.379 newSVptr (void *ptr, HV *stash)
373     {
374     return newSVptr (ptr, stash, newHV ());
375     }
376    
377 root 1.109 static int
378     attachable_free (pTHX_ SV *sv, MAGIC *mg)
379     {
380     attachable *at = (attachable *)mg->mg_ptr;
381 root 1.153
382     //TODO: check if transaction behaviour is really required here
383     if (SV *self = (SV *)at->self)
384     {
385     at->self = 0;
386     SvREFCNT_dec (self);
387     }
388    
389 root 1.111 // next line makes sense, but most objects still have refcnt 0 by default
390     //at->refcnt_chk ();
391 root 1.109 return 0;
392 root 1.1 }
393    
394 root 1.116 MGVTBL attachable::vtbl = {0, 0, 0, 0, attachable_free};
395 root 1.109
396 root 1.388 static SV * noinline
397 root 1.109 newSVattachable (attachable *obj, HV *stash)
398 root 1.11 {
399     if (!obj)
400 root 1.252 return newSV (0);
401 root 1.11
402     if (!obj->self)
403 root 1.228 obj->self = newHV ();
404    
405     if (!SvOBJECT (obj->self))
406 root 1.109 {
407 root 1.116 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0);
408 root 1.11
409 root 1.129 // now bless the object _once_
410 root 1.228 //TODO: create a class registry with c++ type<=>perl name<=>stash and use it here and elsewhere
411 root 1.129 return sv_bless (newRV_inc ((SV *)obj->self), stash);
412 root 1.109 }
413 root 1.129 else
414 root 1.140 {
415     SV *sv = newRV_inc ((SV *)obj->self);
416    
417     if (Gv_AMG (stash)) // handle overload correctly, as the perl core does not
418     SvAMAGIC_on (sv);
419    
420     return sv;
421     }
422 root 1.11 }
423    
424 root 1.332 #if 0 // unused
425 root 1.1 static void
426     clearSVptr (SV *sv)
427     {
428     if (SvROK (sv))
429     sv = SvRV (sv);
430    
431     hv_clear ((HV *)sv);
432     sv_unmagic (sv, PERL_MAGIC_ext);
433     }
434 root 1.332 #endif
435 root 1.1
436 root 1.342 static long
437     SvPTR_nc (SV *sv)
438 root 1.337 {
439     sv = SvRV (sv);
440    
441     // very important shortcut
442 root 1.339 if (expect_true (SvMAGIC (sv) && SvMAGIC (sv)->mg_type == PERL_MAGIC_ext))
443 root 1.337 return (long)SvMAGIC (sv)->mg_ptr;
444    
445     if (MAGIC *mg = mg_find (sv, PERL_MAGIC_ext))
446     return (long)mg->mg_ptr;
447    
448     croak ("perl code used object, but C object is already destroyed, caught");
449     }
450    
451 root 1.1 static long
452     SvPTR (SV *sv, const char *klass)
453     {
454     if (!sv_derived_from (sv, klass))
455     croak ("object of type %s expected", klass);
456    
457 root 1.337 return SvPTR_nc (sv);
458     }
459 root 1.1
460 root 1.337 static long noinline
461     SvPTR_ornull (SV *sv, const char *klass)
462     {
463     if (expect_false (!SvOK (sv))) return 0;
464 root 1.1
465 root 1.337 return SvPTR (sv, klass);
466 root 1.1 }
467    
468 root 1.314 static long noinline
469 root 1.337 SvPTR_ornull_client (SV *sv)
470     {
471     if (expect_false (!SvOK (sv))) return 0;
472    
473     if (!SvROK (sv)
474     || (SvSTASH (SvRV (sv)) != stash_cf_client_wrap
475     && !sv_derived_from (sv, "cf::client")))
476     croak ("object of type cf::client expected");
477    
478     return SvPTR_nc (sv);
479     }
480    
481     static long noinline
482     SvPTR_ornull_object (SV *sv)
483     {
484     if (expect_false (!SvOK (sv))) return 0;
485    
486     if (!SvROK (sv)
487     || (SvSTASH (SvRV (sv)) != stash_cf_object_wrap
488     && SvSTASH (SvRV (sv)) != stash_cf_object_player_wrap
489     && SvSTASH (SvRV (sv)) != stash_cf_arch_wrap
490     && !sv_derived_from (sv, "cf::object")))
491     croak ("object of type cf::object expected");
492    
493     return SvPTR_nc (sv);
494     }
495    
496     static long noinline
497 root 1.375 SvPTR_ornull_maptile (SV *sv)
498     {
499     if (expect_false (!SvOK (sv))) return 0;
500    
501     if (!SvROK (sv)
502     || (SvSTASH (SvRV (sv)) != stash_cf_map_wrap
503 root 1.376 && SvSTASH (SvRV (sv)) != stash_ext_map_world
504 root 1.375 && !sv_derived_from (sv, "cf::map")))
505     croak ("object of type cf::map expected");
506    
507     return SvPTR_nc (sv);
508     }
509    
510     static long noinline
511 root 1.337 SvPTR_ornull_player (SV *sv)
512 root 1.1 {
513 root 1.337 if (expect_false (!SvOK (sv))) return 0;
514    
515     if (!SvROK (sv)
516     || (SvSTASH (SvRV (sv)) != stash_cf_player_wrap
517     && !sv_derived_from (sv, "cf::player")))
518     croak ("object of type cf::player expected");
519    
520     return SvPTR_nc (sv);
521 root 1.1 }
522    
523 root 1.333 static inline SV *to_sv (const shstr & v) { return newSVpvn_utf8 ((const char *)v, v.length (), 1); }
524     static inline SV *to_sv (const char * v) { return v ? newSVpv (v, 0) : newSV (0); }
525     static inline SV *to_sv (bool v) { return newSViv (v); }
526     static inline SV *to_sv ( signed char v) { return newSViv (v); }
527     static inline SV *to_sv (unsigned char v) { return newSViv (v); }
528     static inline SV *to_sv ( signed short v) { return newSViv (v); }
529     static inline SV *to_sv (unsigned short v) { return newSVuv (v); }
530     static inline SV *to_sv ( signed int v) { return newSViv (v); }
531     static inline SV *to_sv (unsigned int v) { return newSVuv (v); }
532     static inline SV *to_sv ( signed long v) { return newSViv (v); }
533     static inline SV *to_sv (unsigned long v) { return newSVuv (v); }
534     static inline SV *to_sv ( signed long long v) { return newSVval64 (v); }
535     static inline SV *to_sv (unsigned long long v) { return newSVval64 (v); }
536     static inline SV *to_sv (float v) { return newSVnv (v); }
537     static inline SV *to_sv (double v) { return newSVnv (v); }
538     static inline SV *to_sv (client * v) { return newSVattachable (v, stash_cf_client_wrap); }
539     static inline SV *to_sv (player * v) { return newSVattachable (v, stash_cf_player_wrap); }
540 root 1.388 // gcc dislikes noilnine on to_sv
541     static noinline SV *to_sv_ni (object * v) { return newSVattachable (v, v && v->type == PLAYER ? stash_cf_object_player_wrap : stash_cf_object_wrap); }
542     static inline SV *to_sv (object * v) { return to_sv_ni (v); }
543 root 1.333 static inline SV *to_sv (maptile * v) { return newSVattachable (v, stash_cf_map_wrap); }
544     static inline SV *to_sv (archetype * v) { return newSVattachable (v, stash_cf_arch_wrap); }
545     static inline SV *to_sv (region * v) { return newSVattachable (v, stash_cf_region_wrap); }
546     static inline SV *to_sv (partylist * v) { return newSVptr (v, stash_cf_party_wrap); }
547     static inline SV *to_sv (living * v) { return newSVptr (v, stash_cf_living_wrap); }
548     static inline SV *to_sv (mapspace * v) { return newSVptr (v, stash_cf_mapspace_wrap); }
549 root 1.389 static inline SV *to_sv (layout * v) { return newSVptr (v, stash_cf_layout_wrap); }
550 root 1.333
551     static inline SV *to_sv (object & v) { return to_sv (&v); }
552     static inline SV *to_sv (living & v) { return to_sv (&v); }
553 root 1.45
554 root 1.333 static inline SV *to_sv (const std::string & v) { return newSVpvn (v.data (), v.size ()); }
555     static inline SV *to_sv (const treasurelist *v) { return to_sv (v->name); }
556 root 1.45
557 root 1.333 static inline SV *to_sv (UUID v) { return newSVpv (v.c_str (), 0); }
558 elmex 1.68
559 root 1.333 static inline SV *to_sv (dynbuf * v)
560 root 1.297 {
561     SV *sv = newSV (0);
562    
563     sv_upgrade (sv, SVt_PV);
564     SvGROW (sv, v->size () + 1);
565     SvPOK_only (sv);
566     v->linearise (SvPVX (sv));
567     SvCUR_set (sv, v->size ());
568     *SvEND (sv) = 0;
569    
570     return sv;
571     }
572    
573 root 1.333 static inline SV *to_sv (dynbuf_text * v)
574 root 1.297 {
575     SV *sv = to_sv (static_cast<dynbuf *> (v));
576     SvUTF8_on (sv);
577     return sv;
578     }
579    
580 root 1.345 static inline void sv_to (SV *sv, shstr &v) { v = SvOK (sv) ? cfSvPVutf8_nolen (sv) : 0; }
581 root 1.333 static inline void sv_to (SV *sv, char * &v) { free (v); v = SvOK (sv) ? strdup (SvPV_nolen (sv)) : 0; }
582     static inline void sv_to (SV *sv, bool &v) { v = SvIV (sv); }
583     static inline void sv_to (SV *sv, signed char &v) { v = SvIV (sv); }
584     static inline void sv_to (SV *sv, unsigned char &v) { v = SvIV (sv); }
585     static inline void sv_to (SV *sv, signed short &v) { v = SvIV (sv); }
586     static inline void sv_to (SV *sv, unsigned short &v) { v = SvIV (sv); }
587     static inline void sv_to (SV *sv, signed int &v) { v = SvIV (sv); }
588     static inline void sv_to (SV *sv, unsigned int &v) { v = SvUV (sv); }
589     static inline void sv_to (SV *sv, signed long &v) { v = SvIV (sv); }
590     static inline void sv_to (SV *sv, unsigned long &v) { v = SvUV (sv); }
591     static inline void sv_to (SV *sv, signed long long &v) { v = ( signed long long)SvVAL64 (sv); }
592     static inline void sv_to (SV *sv, unsigned long long &v) { v = (unsigned long long)SvVAL64 (sv); }
593     static inline void sv_to (SV *sv, float &v) { v = SvNV (sv); }
594     static inline void sv_to (SV *sv, double &v) { v = SvNV (sv); }
595 root 1.375 static inline void sv_to (SV *sv, client * &v) { v = (client *) (attachable *)SvPTR_ornull_client (sv); }
596     static inline void sv_to (SV *sv, player * &v) { v = (player *) (attachable *)SvPTR_ornull_player (sv); }
597     static inline void sv_to (SV *sv, object * &v) { v = (object *) (attachable *)SvPTR_ornull_object (sv); }
598     static inline void sv_to (SV *sv, maptile * &v) { v = (maptile *) (attachable *)SvPTR_ornull_maptile (sv); }
599 root 1.333 static inline void sv_to (SV *sv, archetype * &v) { v = (archetype *)(attachable *)SvPTR_ornull (sv, "cf::arch"); }
600 root 1.337 static inline void sv_to (SV *sv, region * &v) { v = (region *) (attachable *)SvPTR_ornull (sv, "cf::region"); }
601     static inline void sv_to (SV *sv, attachable * &v) { v = (attachable *)SvPTR_ornull (sv, "cf::attachable"); }
602     static inline void sv_to (SV *sv, partylist * &v) { v = (partylist *) SvPTR_ornull (sv, "cf::party"); }
603     static inline void sv_to (SV *sv, living * &v) { v = (living *) SvPTR_ornull (sv, "cf::living"); }
604     static inline void sv_to (SV *sv, mapspace * &v) { v = (mapspace *) SvPTR_ornull (sv, "cf::mapspace"); }
605 root 1.389 static inline void sv_to (SV *sv, layout * &v) { v = (layout *) SvPTR_ornull (sv, "cf::layout"); }
606 root 1.337 static inline void sv_to (SV *sv, object_freezer * &v) { v = (object_freezer *) SvPTR_ornull (sv, "cf::object::freezer"); }
607     static inline void sv_to (SV *sv, object_thawer * &v) { v = (object_thawer *) SvPTR_ornull (sv, "cf::object::thawer" ); }
608 root 1.45
609 root 1.333 //static inline void sv_to (SV *sv, faceinfo * &v) { v = &faces [face_find (SvPV_nolen (sv), 0)]; }
610     static inline void sv_to (SV *sv, treasurelist * &v) { v = treasurelist::find (SvPV_nolen (sv)); }
611 root 1.45
612 root 1.52 template<class T>
613 root 1.333 static inline void sv_to (SV *sv, refptr<T> &v) { T *tmp; sv_to (sv, tmp); v = tmp; }
614 root 1.52
615 root 1.45 template<int N>
616 root 1.333 static inline void sv_to (SV *sv, char (&v)[N]) { assign (v, SvPV_nolen (sv)); }
617 root 1.45
618 root 1.333 static inline void sv_to (SV *sv, bowtype_t &v) { v = (bowtype_t) SvIV (sv); }
619     static inline void sv_to (SV *sv, petmode_t &v) { v = (petmode_t) SvIV (sv); }
620     static inline void sv_to (SV *sv, usekeytype &v) { v = (usekeytype) SvIV (sv); }
621     static inline void sv_to (SV *sv, unapplymode &v) { v = (unapplymode) SvIV (sv); }
622 root 1.106
623 root 1.333 static inline void sv_to (SV *sv, std::string &v)
624 root 1.176 {
625     STRLEN len;
626     char *data = SvPVbyte (sv, len);
627     v.assign (data, len);
628     }
629    
630 root 1.408 static inline void sv_to (SV *sv, refcnt_buf &v)
631     {
632     STRLEN len;
633     char *data = SvPVbyte (sv, len);
634     refcnt_buf buf (data, len);
635     v = buf;
636     }
637    
638 root 1.333 static inline void sv_to (SV *sv, UUID &v)
639 root 1.69 {
640 root 1.275 if (!v.parse (SvPV_nolen (sv)))
641 root 1.69 croak ("unparsable uuid: %s", SvPV_nolen (sv));
642 elmex 1.68 }
643    
644 root 1.333 static inline void sv_to (SV *sv, object::flags_t::reference v) { v = SvTRUE (sv); }
645 root 1.106
646 root 1.1 static SV *
647 root 1.6 newSVdt_va (va_list &ap, data_type type)
648 root 1.1 {
649     SV *sv;
650    
651     switch (type)
652     {
653 root 1.10 case DT_INT:
654     sv = newSViv (va_arg (ap, int));
655     break;
656    
657     case DT_INT64:
658     sv = newSVval64 ((val64)va_arg (ap, sint64));
659     break;
660    
661 root 1.6 case DT_DOUBLE:
662 root 1.10 sv = newSVnv (va_arg (ap, double));
663 root 1.1 break;
664    
665 root 1.6 case DT_STRING:
666     {
667 root 1.10 char *str = (char *)va_arg (ap, const char *);
668 root 1.252 sv = str ? newSVpv (str, 0) : newSV (0);
669 root 1.6 }
670 root 1.1 break;
671    
672 root 1.6 case DT_DATA:
673 root 1.1 {
674 root 1.10 char *str = (char *)va_arg (ap, const void *);
675 root 1.6 int len = va_arg (ap, int);
676 root 1.252 sv = str ? newSVpv (str, len) : newSV (0);
677 root 1.1 }
678     break;
679    
680 root 1.6 case DT_OBJECT:
681 root 1.45 sv = to_sv (va_arg (ap, object *));
682 root 1.1 break;
683    
684 root 1.6 case DT_MAP:
685 root 1.11 // va_arg (object *) when void * is passed is an XSI extension
686 root 1.61 sv = to_sv (va_arg (ap, maptile *));
687 root 1.1 break;
688    
689 root 1.88 case DT_CLIENT:
690 root 1.84 sv = to_sv (va_arg (ap, client *));
691 root 1.79 break;
692    
693 root 1.6 case DT_PLAYER:
694 root 1.45 sv = to_sv (va_arg (ap, player *));
695 root 1.1 break;
696    
697 root 1.6 case DT_ARCH:
698 root 1.45 sv = to_sv (va_arg (ap, archetype *));
699 root 1.1 break;
700    
701 root 1.6 case DT_PARTY:
702 root 1.45 sv = to_sv (va_arg (ap, partylist *));
703 root 1.1 break;
704    
705 root 1.6 case DT_REGION:
706 root 1.45 sv = to_sv (va_arg (ap, region *));
707 root 1.1 break;
708    
709     default:
710 root 1.6 assert (("unhandled type in newSVdt_va", 0));
711     }
712    
713     return sv;
714     }
715    
716     static SV *
717     newSVdt (data_type type, ...)
718     {
719     va_list ap;
720    
721     va_start (ap, type);
722     SV *sv = newSVdt_va (ap, type);
723     va_end (ap);
724    
725     return sv;
726     }
727    
728 root 1.314 // typemap support, mostly to avoid excessive inlining
729     template<class type>
730     static void noinline
731     cf_obj_to (SV *arg, type &var)
732     {
733     sv_to (arg, var);
734 root 1.375 if (expect_false (!var))
735 root 1.314 croak ("must not pass invalid/null cf_obj here");
736     }
737    
738     template<class object>
739     static void noinline
740     cf_obj_ornull_to (SV *arg, object *&var)
741     {
742     if (SvOK (arg))
743     {
744     sv_to (arg, var);
745 root 1.375 if (expect_false (!var))
746 root 1.314 croak ("unable to convert perl object to C++ object");
747     }
748     else
749     var = 0;
750     }
751    
752 root 1.11 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
753    
754 root 1.333 static SV *
755 root 1.109 registry (attachable *ext)
756 root 1.12 {
757 root 1.13 if (!ext->cb)
758     ext->cb = newAV ();
759 root 1.12
760 root 1.13 return newRV_inc ((SV *)ext->cb);
761 root 1.12 }
762    
763 root 1.13 /////////////////////////////////////////////////////////////////////////////
764    
765 root 1.7 void
766     cfperl_init ()
767     {
768 root 1.270 extern char **environ;
769    
770     PERL_SYS_INIT3 (&settings.argc, &settings.argv, &environ);
771 root 1.32 perl = perl_alloc ();
772     perl_construct (perl);
773    
774     PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
775    
776 root 1.179 const char *argv[] = {
777 root 1.198 settings.argv [0],
778 root 1.304 "-e0"
779 root 1.7 };
780    
781 root 1.270 if (perl_parse (perl, xs_init, 2, (char **)argv, environ)
782 root 1.179 || perl_run (perl))
783 root 1.7 {
784     printf ("unable to initialize perl-interpreter, aborting.\n");
785     exit (EXIT_FAILURE);
786     }
787 root 1.304
788     eval_pv (
789     "#line 1 'cfperl init'\n"
790 root 1.382 "use EV ();\n" // required by bootstrap
791     "use Coro ();\n" // required by bootstrap
792     "cf->bootstrap;\n" // required for cf::datadir
793     "unshift @INC, cf::datadir ();\n" // required for 'require' :)
794 root 1.304 "require cf;\n",
795     0
796     );
797    
798     if (SvTRUE (ERRSV))
799     {
800     printf ("unable to bootstrap perl, aborting:\n%s", SvPV_nolen (ERRSV));
801     exit (EXIT_FAILURE);
802     }
803 root 1.7 }
804    
805 root 1.314 void
806     cfperl_main ()
807 root 1.2 {
808     dSP;
809    
810     PUSHMARK (SP);
811     PUTBACK;
812 root 1.7 call_pv ("cf::main", G_DISCARD | G_VOID);
813     }
814    
815 root 1.109 void
816     attachable::instantiate ()
817     {
818     if (attach)
819     {
820 root 1.214 INVOKE_ATTACHABLE (INSTANTIATE, this, ARG_STRING (attach));
821 root 1.109 attach = 0;
822     }
823     }
824    
825     void
826     attachable::reattach ()
827     {
828     optimise ();
829     //TODO: check for _attachment's, very important for restarts
830 root 1.214 INVOKE_ATTACHABLE (REATTACH, this);
831 root 1.109 }
832    
833 root 1.8 static event_klass klass_of[NUM_EVENT_TYPES] = {
834     # define def(type,name) KLASS_ ## type,
835     # include "eventinc.h"
836     # undef def
837     };
838    
839 root 1.18 #define KLASS_OF(event) (((unsigned int)event) < NUM_EVENT_TYPES ? klass_of [event] : KLASS_NONE)
840    
841 root 1.314 static void noinline
842 root 1.8 gather_callbacks (AV *&callbacks, AV *registry, event_type event)
843     {
844     // event must be in array
845     if (event >= 0 && event <= AvFILLp (registry))
846     {
847     SV *cbs_ = AvARRAY (registry)[event];
848    
849     // element must be list of callback entries
850     if (cbs_ && SvROK (cbs_) && SvTYPE (SvRV (cbs_)) == SVt_PVAV)
851     {
852     AV *cbs = (AV *)SvRV (cbs_);
853    
854     // no callback entries, no callbacks to call
855     if (AvFILLp (cbs) >= 0)
856     {
857     if (!callbacks)
858     {
859     callbacks = newAV ();
860     av_extend (callbacks, 16);
861     }
862    
863     // never use SvREFCNT_inc to copy values, but its ok here :)
864     for (int i = 0; i <= AvFILLp (cbs); ++i)
865     av_push (callbacks, SvREFCNT_inc (AvARRAY (cbs)[i]));
866     }
867     }
868     }
869     }
870    
871 root 1.109 void
872     attachable::gather_callbacks (AV *&callbacks, event_type event) const
873 root 1.6 {
874 root 1.109 ::gather_callbacks (callbacks, cb_attachable, event);
875 root 1.6
876 root 1.109 if (cb)
877     ::gather_callbacks (callbacks, cb, event);
878     }
879 root 1.8
880 root 1.109 void
881     global::gather_callbacks (AV *&callbacks, event_type event) const
882     {
883 root 1.210 ::gather_callbacks (callbacks, cb_global, event);
884 root 1.109 }
885 root 1.8
886 root 1.109 void
887     object::gather_callbacks (AV *&callbacks, event_type event) const
888     {
889 root 1.210 if (subtype && type + subtype * NUM_TYPES <= AvFILLp (cb_type))
890 root 1.109 {
891 root 1.210 SV *registry = AvARRAY (cb_type)[type + subtype * NUM_TYPES];
892 root 1.8
893 root 1.109 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
894     ::gather_callbacks (callbacks, (AV *)SvRV (registry), event);
895     }
896 root 1.8
897 root 1.109 if (type <= AvFILLp (cb_type))
898 root 1.8 {
899 root 1.109 SV *registry = AvARRAY (cb_type)[type];
900 root 1.8
901 root 1.109 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
902     ::gather_callbacks (callbacks, (AV *)SvRV (registry), event);
903     }
904 root 1.8
905 root 1.109 attachable::gather_callbacks (callbacks, event);
906     ::gather_callbacks (callbacks, cb_object, event);
907     }
908 root 1.8
909 root 1.109 void
910     archetype::gather_callbacks (AV *&callbacks, event_type event) const
911     {
912     attachable::gather_callbacks (callbacks, event);
913     //TODO//::gather_callbacks (callbacks, cb_archetype, event);
914     }
915 root 1.14
916 root 1.109 void
917     client::gather_callbacks (AV *&callbacks, event_type event) const
918     {
919     attachable::gather_callbacks (callbacks, event);
920     ::gather_callbacks (callbacks, cb_client, event);
921     }
922 root 1.8
923 root 1.109 void
924     player::gather_callbacks (AV *&callbacks, event_type event) const
925     {
926     attachable::gather_callbacks (callbacks, event);
927     ::gather_callbacks (callbacks, cb_player, event);
928     }
929 root 1.8
930 root 1.109 void
931     maptile::gather_callbacks (AV *&callbacks, event_type event) const
932     {
933     attachable::gather_callbacks (callbacks, event);
934     ::gather_callbacks (callbacks, cb_map, event);
935     }
936 root 1.8
937 root 1.314 static void noinline
938 root 1.210 _recalc_want (bitset<NUM_EVENT_TYPES> &set, AV *registry)
939     {
940     for (int event = 0; event <= AvFILLp (registry); ++event)
941     {
942     SV *cbs_ = AvARRAY (registry)[event];
943    
944     // element must be list of callback entries
945     if (cbs_ && SvROK (cbs_) && SvTYPE (SvRV (cbs_)) == SVt_PVAV)
946     {
947     AV *cbs = (AV *)SvRV (cbs_);
948    
949     // no callback entries, no callbacks to call
950     if (AvFILLp (cbs) >= 0)
951     set.set (event);
952     }
953     }
954     }
955    
956     // very slow and inefficient way to recalculate the global want bitsets
957 root 1.314 static void
958 root 1.210 _recalc_want ()
959     {
960     ev_want_event.reset ();
961    
962     _recalc_want (ev_want_event, cb_global);
963     _recalc_want (ev_want_event, cb_attachable);
964     _recalc_want (ev_want_event, cb_object);
965     _recalc_want (ev_want_event, cb_client);
966     _recalc_want (ev_want_event, cb_player);
967     _recalc_want (ev_want_event, cb_map);
968    
969     ev_want_type.reset ();
970    
971     for (int type = 0; type <= AvFILLp (cb_type); ++type)
972     {
973     SV *cbs_ = AvARRAY (cb_type)[type];
974    
975     // element must be list of callback entries
976     if (cbs_ && SvROK (cbs_) && SvTYPE (SvRV (cbs_)) == SVt_PVAV)
977     {
978     AV *cbs = (AV *)SvRV (cbs_);
979    
980     // no callback entries, no callbacks to call
981     if (AvFILLp (cbs) >= 0)
982     ev_want_type.set (type % NUM_TYPES);
983     }
984     }
985     }
986    
987 root 1.109 bool
988 root 1.214 attachable::invoke (event_type event, ...)
989 root 1.109 {
990     data_type dt;
991 root 1.8
992 root 1.109 // callback call ordering should be:
993     // 1. per-object callback
994     // 2. per-class object
995     // 3. per-type callback
996     // 4. global callbacks
997 root 1.12
998 root 1.109 AV *callbacks = 0;
999     gather_callbacks (callbacks, event);
1000 root 1.8
1001     // short-circuit processing if no callbacks found/defined
1002     if (!callbacks)
1003     return 0;
1004    
1005 root 1.214 va_list ap;
1006     va_start (ap, event);
1007    
1008 root 1.116 CALL_BEGIN (3);
1009     CALL_ARG_SV (newSViv (event)); // only used for debugging nowadays
1010     CALL_ARG_SV (newRV_noinc ((SV *)callbacks));
1011 root 1.8
1012 root 1.109 //TODO: unhack
1013 root 1.116 if (object *op = is_a<object>(this)) CALL_ARG_SV (newSVdt (DT_OBJECT, op));
1014     else if (player *pl = is_a<player>(this)) CALL_ARG_SV (newSVdt (DT_PLAYER, pl));
1015     else if (client *ns = is_a<client>(this)) CALL_ARG_SV (newSVdt (DT_CLIENT, ns));
1016     else if (maptile *m = is_a<maptile>(this)) CALL_ARG_SV (newSVdt (DT_MAP, m));
1017 root 1.109 else if (global *gl = is_a<global>(this)) /*nop*/;
1018     else
1019     abort (); //TODO
1020 root 1.7
1021 root 1.6 for (;;)
1022     {
1023 root 1.8 dt = (data_type) va_arg (ap, int);
1024 root 1.6
1025     if (dt == DT_END)
1026     break;
1027 root 1.109 else if (dt == DT_AV)
1028 root 1.12 {
1029     AV *av = va_arg (ap, AV *);
1030    
1031     for (int i = 0; i <= av_len (av); ++i)
1032     XPUSHs (*av_fetch (av, i, 1));
1033     }
1034     else
1035     XPUSHs (sv_2mortal (newSVdt_va (ap, dt)));
1036 root 1.6 }
1037    
1038     va_end (ap);
1039    
1040 root 1.324 CALL_CALL (cv_cf_do_invoke, G_SCALAR);
1041 root 1.6 count = count > 0 ? POPi : 0;
1042    
1043 root 1.116 CALL_END;
1044 root 1.6
1045     return count;
1046 root 1.2 }
1047    
1048 root 1.333 static SV *
1049 root 1.12 cfperl_result (int idx)
1050     {
1051 elmex 1.233 AV *av = get_av ("cf::INVOKE_RESULTS", 0);
1052 root 1.12 if (!av)
1053     return &PL_sv_undef;
1054    
1055     SV **sv = av_fetch (av, idx, 0);
1056     if (!sv)
1057     return &PL_sv_undef;
1058    
1059     return *sv;
1060     }
1061    
1062     int
1063     cfperl_result_INT (int idx)
1064     {
1065     return SvIV (cfperl_result (idx));
1066     }
1067    
1068 root 1.74 double
1069 root 1.73 cfperl_result_DOUBLE (int idx)
1070     {
1071     return SvNV (cfperl_result (idx));
1072     }
1073    
1074 root 1.80 /////////////////////////////////////////////////////////////////////////////
1075 root 1.247 // various c++ => perl glue functions
1076 root 1.80
1077 root 1.314 void
1078     cfperl_tick ()
1079 root 1.272 {
1080     tick_pending = 1;
1081    
1082     if (tick_inhibit)
1083     return;
1084    
1085     tick_pending = 0;
1086    
1087     dSP;
1088    
1089     PUSHMARK (SP);
1090     PUTBACK;
1091 root 1.324 call_pvsv (cv_cf_tick, G_DISCARD | G_VOID);
1092 root 1.272
1093     SvNV_set (sv_next_tick, get_next_tick ()); SvNOK_only (sv_next_tick);
1094     }
1095    
1096 root 1.116 void
1097 root 1.134 cfperl_emergency_save ()
1098 root 1.116 {
1099     CALL_BEGIN (0);
1100 root 1.134 CALL_CALL ("cf::emergency_save", G_VOID);
1101 root 1.116 CALL_END;
1102     }
1103    
1104 root 1.165 void
1105     cfperl_cleanup (int make_core)
1106     {
1107     CALL_BEGIN (1);
1108     CALL_ARG (make_core);
1109     CALL_CALL ("cf::post_cleanup", G_VOID);
1110     CALL_END;
1111     }
1112    
1113 root 1.213 void
1114     cfperl_make_book (object *book, int level)
1115     {
1116     CALL_BEGIN (2);
1117     CALL_ARG (book);
1118     CALL_ARG (level);
1119     CALL_CALL ("ext::books::make_book", G_VOID);
1120     CALL_END;
1121     }
1122    
1123 root 1.222 void
1124 root 1.346 cfperl_send_msg (client *ns, int color, const_utf8_string type, const_utf8_string msg)
1125 root 1.222 {
1126     CALL_BEGIN (4);
1127     CALL_ARG (ns);
1128     CALL_ARG (type);
1129 root 1.224 CALL_ARG_SV (newSVpv_utf8 (msg));
1130 root 1.235 CALL_ARG (color);
1131 root 1.324 CALL_CALL (cv_cf_client_send_msg, G_VOID);
1132 root 1.222 CALL_END;
1133     }
1134    
1135 root 1.234 int
1136     cfperl_can_merge (object *ob1, object *ob2)
1137     {
1138     int can;
1139    
1140     CALL_BEGIN (2);
1141     CALL_ARG (ob1);
1142     CALL_ARG (ob2);
1143 root 1.324 CALL_CALL (cv_cf__can_merge, G_SCALAR);
1144 root 1.234 can = count && SvTRUE (TOPs);
1145     CALL_END;
1146    
1147     return can;
1148     }
1149    
1150 root 1.315 void
1151 root 1.317 cfperl_mapscript_activate (object *ob, int state, object *activator, object *originator)
1152 root 1.315 {
1153 root 1.316 CALL_BEGIN (4);
1154 root 1.315 CALL_ARG (ob);
1155 root 1.316 CALL_ARG (state);
1156 root 1.315 CALL_ARG (activator);
1157 root 1.316 CALL_ARG (originator);
1158 root 1.315 CALL_CALL ("cf::mapscript::activate", G_VOID);
1159     CALL_END;
1160     }
1161    
1162 root 1.244 player *
1163 root 1.346 player::find (const_utf8_string name)
1164 root 1.244 {
1165     CALL_BEGIN (1);
1166     CALL_ARG (name);
1167     CALL_CALL ("cf::player::find", G_SCALAR);
1168    
1169 root 1.286 player *retval = 0;
1170     if (count) sv_to (POPs, retval);
1171 root 1.244
1172 root 1.286 CALL_END;
1173    
1174     return retval;
1175     }
1176    
1177     maptile *
1178 root 1.368 find_style (const_utf8_string dirname, const_utf8_string stylename, int difficulty, bool recurse)
1179 root 1.286 {
1180 root 1.368 CALL_BEGIN (4);
1181 root 1.286 CALL_ARG (dirname);
1182     CALL_ARG (stylename);
1183     CALL_ARG (difficulty);
1184 root 1.368 CALL_ARG (recurse);
1185 root 1.286 CALL_CALL ("ext::map_random::find_style", G_SCALAR);
1186    
1187     maptile *retval = 0;
1188     if (count) sv_to (POPs, retval);
1189 root 1.244
1190     CALL_END;
1191    
1192     return retval;
1193     }
1194    
1195 root 1.116 maptile *
1196 root 1.346 maptile::find_sync (const_utf8_string path, maptile *origin)
1197 root 1.116 {
1198     CALL_BEGIN (2);
1199     CALL_ARG (path);
1200     CALL_ARG (origin);
1201 root 1.126 CALL_CALL ("cf::map::find_sync", G_SCALAR);
1202 root 1.116
1203 root 1.286 maptile *retval = 0;
1204     if (count) sv_to (POPs, retval);
1205 root 1.116
1206     CALL_END;
1207    
1208     return retval;
1209     }
1210    
1211 root 1.135 maptile *
1212 root 1.346 maptile::find_async (const_utf8_string path, maptile *origin, bool load)
1213 root 1.135 {
1214 root 1.243 CALL_BEGIN (3);
1215 root 1.135 CALL_ARG (path);
1216     CALL_ARG (origin);
1217 root 1.243 CALL_ARG (load);
1218 root 1.135 CALL_CALL ("cf::map::find_async", G_SCALAR);
1219    
1220 root 1.286 maptile *retval = 0;
1221     if (count) sv_to (POPs, retval);
1222 root 1.135
1223     CALL_END;
1224    
1225     return retval;
1226     }
1227    
1228 root 1.116 void
1229 root 1.126 maptile::do_load_sync ()
1230     {
1231     CALL_BEGIN (1);
1232     CALL_ARG (this);
1233     CALL_CALL ("cf::map::do_load_sync", G_SCALAR);
1234     CALL_END;
1235     }
1236    
1237     void
1238 root 1.116 object::enter_exit (object *exit)
1239     {
1240     if (type != PLAYER)
1241     return;
1242    
1243     CALL_BEGIN (2);
1244     CALL_ARG (this);
1245     CALL_ARG (exit);
1246     CALL_CALL ("cf::object::player::enter_exit", G_VOID);
1247     CALL_END;
1248     }
1249    
1250 root 1.287 void
1251 root 1.346 object::player_goto (const_utf8_string path, int x, int y)
1252 root 1.287 {
1253     if (type != PLAYER)
1254     return;
1255    
1256     CALL_BEGIN (4);
1257     CALL_ARG (this);
1258     CALL_ARG (path);
1259     CALL_ARG (x);
1260     CALL_ARG (y);
1261     CALL_CALL ("cf::object::player::goto", G_VOID);
1262     CALL_END;
1263     }
1264    
1265 root 1.346 const_utf8_string
1266 root 1.247 object::ref () const
1267     {
1268     if (type == PLAYER)
1269     return format ("player/<1.%llx>/%s", (unsigned long long)uuid.seq, &name);
1270     else
1271 root 1.363 // TODO: should be able to save references within the same map, at least
1272 root 1.247 return 0;
1273     }
1274    
1275     object *
1276 root 1.346 object::deref (const_utf8_string ref)
1277 root 1.247 {
1278 root 1.249 object *retval = 0;
1279 root 1.247
1280 root 1.249 if (ref)
1281     {
1282     CALL_BEGIN (1);
1283     CALL_ARG (ref);
1284     CALL_CALL ("cf::object::deref", G_SCALAR);
1285    
1286     if (count)
1287     sv_to (POPs, retval);
1288 root 1.247
1289 root 1.249 CALL_END;
1290     }
1291 root 1.247
1292     return retval;
1293     }
1294    
1295 root 1.198 void
1296 root 1.345 log_backtrace (const_utf8_string msg)
1297 root 1.198 {
1298     #if HAVE_BACKTRACE
1299     void *addr [20];
1300     int size = backtrace (addr, 20);
1301    
1302     CALL_BEGIN (size);
1303     CALL_ARG (msg);
1304     for (int i = 0; i < size; ++i)
1305     CALL_ARG ((IV)addr [i]);
1306     CALL_CALL ("cf::_log_backtrace", G_VOID);
1307     CALL_END;
1308     #endif
1309     }
1310    
1311 root 1.323 bool
1312 root 1.346 is_match_expr (const_utf8_string expr)
1313 root 1.322 {
1314     return !strncmp (expr, "match ", sizeof ("match ") - 1);
1315     }
1316    
1317 root 1.323 bool
1318 root 1.346 match (const_utf8_string expr, object *ob, object *self, object *source, object *originator)
1319 root 1.322 {
1320 root 1.323 if (!strncmp (expr, "match ", sizeof ("match ") - 1))
1321     expr += sizeof ("match ") - 1;
1322 root 1.322
1323     CALL_BEGIN (5);
1324     CALL_ARG (expr);
1325     CALL_ARG (ob);
1326     CALL_ARG (self);
1327     CALL_ARG (source);
1328     CALL_ARG (originator);
1329 root 1.324 CALL_CALL (cv_cf_match_match, G_SCALAR);
1330 root 1.322
1331 root 1.324 bool matched = count && SvTRUE (TOPs);
1332 root 1.322
1333     CALL_END;
1334    
1335     return matched;
1336     }
1337    
1338 root 1.331 object *
1339 root 1.346 match_one (const_utf8_string expr, object *ob, object *self, object *source, object *originator)
1340 root 1.331 {
1341     if (!strncmp (expr, "match ", sizeof ("match ") - 1))
1342     expr += sizeof ("match ") - 1;
1343    
1344     CALL_BEGIN (5);
1345     CALL_ARG (expr);
1346     CALL_ARG (ob);
1347     CALL_ARG (self);
1348     CALL_ARG (source);
1349     CALL_ARG (originator);
1350     CALL_CALL (cv_cf_match_match, G_ARRAY);
1351    
1352     object *one = 0;
1353    
1354     if (count)
1355     sv_to (TOPs, one);
1356    
1357     CALL_END;
1358    
1359     return one;
1360     }
1361    
1362 root 1.116 /////////////////////////////////////////////////////////////////////////////
1363    
1364 root 1.265 struct EVAPI *evapi::GEVAPI;
1365     struct CoroAPI *coroapi::GCoroAPI;
1366 root 1.80
1367 root 1.314 void
1368     coroapi::do_cede_to_tick ()
1369 root 1.189 {
1370 root 1.272 cede_pending = 0;
1371 root 1.189 cede ();
1372     }
1373 root 1.124
1374 root 1.188 void
1375     coroapi::wait_for_tick ()
1376     {
1377     CALL_BEGIN (0);
1378     CALL_CALL ("cf::wait_for_tick", G_DISCARD);
1379     CALL_END;
1380     }
1381    
1382     void
1383     coroapi::wait_for_tick_begin ()
1384     {
1385     CALL_BEGIN (0);
1386     CALL_CALL ("cf::wait_for_tick_begin", G_DISCARD);
1387     CALL_END;
1388     }
1389    
1390 root 1.85 void
1391 root 1.80 iow::poll (int events)
1392     {
1393 root 1.265 if (events != this->events)
1394 root 1.81 {
1395 root 1.265 int active = ev_is_active ((ev_io *)this);
1396     if (active) stop ();
1397     ev_io_set ((ev_io *)this, fd, events);
1398     if (active) start ();
1399 root 1.81 }
1400 root 1.80 }
1401    
1402 root 1.314 static void
1403 root 1.324 _connect_to_perl_1 ()
1404 root 1.109 {
1405 root 1.272 stash_cf = gv_stashpv ("cf", 1);
1406 root 1.109
1407     stash_cf_object_wrap = gv_stashpv ("cf::object::wrap", 1);
1408     stash_cf_object_player_wrap = gv_stashpv ("cf::object::player::wrap", 1);
1409     stash_cf_player_wrap = gv_stashpv ("cf::player::wrap", 1);
1410     stash_cf_map_wrap = gv_stashpv ("cf::map::wrap" , 1);
1411 root 1.293 stash_cf_mapspace_wrap = gv_stashpv ("cf::mapspace::wrap" , 1);
1412 root 1.109 stash_cf_client_wrap = gv_stashpv ("cf::client::wrap", 1);
1413     stash_cf_arch_wrap = gv_stashpv ("cf::arch::wrap" , 1);
1414     stash_cf_party_wrap = gv_stashpv ("cf::party::wrap" , 1);
1415     stash_cf_region_wrap = gv_stashpv ("cf::region::wrap", 1);
1416     stash_cf_living_wrap = gv_stashpv ("cf::living::wrap", 1);
1417 root 1.389 stash_cf_layout_wrap = gv_stashpv ("cf::layout::wrap", 1);
1418 root 1.376 stash_ext_map_world = gv_stashpv ("ext::map_world" , 1);
1419 root 1.109
1420 root 1.405 sv_now = get_sv ("cf::NOW" , 1); SvUPGRADE (sv_now , SVt_NV);
1421     sv_runtime = get_sv ("cf::RUNTIME" , 1); SvUPGRADE (sv_runtime , SVt_NV);
1422     sv_server_tick = get_sv ("cf::SERVER_TICK", 1); SvUPGRADE (sv_server_tick, SVt_IV);
1423     sv_tick_start = get_sv ("cf::TICK_START" , 1); SvUPGRADE (sv_tick_start , SVt_NV);
1424     sv_next_tick = get_sv ("cf::NEXT_TICK" , 1); SvUPGRADE (sv_next_tick , SVt_NV);
1425 root 1.116
1426 root 1.109 cb_global = get_av ("cf::CB_GLOBAL", 1);
1427     cb_attachable = get_av ("cf::CB_ATTACHABLE", 1);
1428     cb_object = get_av ("cf::CB_OBJECT", 1);
1429     cb_player = get_av ("cf::CB_PLAYER", 1);
1430     cb_client = get_av ("cf::CB_CLIENT", 1);
1431     cb_type = get_av ("cf::CB_TYPE" , 1);
1432     cb_map = get_av ("cf::CB_MAP" , 1);
1433 root 1.324 }
1434 root 1.321
1435 root 1.324 static void
1436     _connect_to_perl_2 ()
1437     {
1438     cv_cf_do_invoke = (SV *)get_cv ("cf::do_invoke" , 0); assert (cv_cf_do_invoke);
1439     cv_cf__can_merge = (SV *)get_cv ("cf::_can_merge" , 0); assert (cv_cf__can_merge);
1440     cv_cf_client_send_msg = (SV *)get_cv ("cf::client::send_msg", 0); assert (cv_cf_client_send_msg);
1441     cv_cf_tick = (SV *)get_cv ("cf::tick" , 0); assert (cv_cf_tick);
1442     cv_cf_match_match = (SV *)get_cv ("cf::match::match" , 0); assert (cv_cf_match_match);
1443 root 1.109 }
1444    
1445 root 1.1 MODULE = cf PACKAGE = cf PREFIX = cf_
1446    
1447     BOOT:
1448     {
1449 root 1.265 I_EV_API (PACKAGE); evapi::GEVAPI = GEVAPI;
1450     I_CORO_API (PACKAGE); coroapi::GCoroAPI = GCoroAPI;
1451 root 1.80
1452 root 1.324 _connect_to_perl_1 ();
1453 root 1.189
1454 root 1.109 newCONSTSUB (stash_cf, "VERSION", newSVpv (VERSION, sizeof (VERSION) - 1));
1455 root 1.63
1456 root 1.1 static const struct {
1457     const char *name;
1458     IV iv;
1459     } *civ, const_iv[] = {
1460     # define const_iv(name) { # name, (IV)name },
1461 root 1.358 # include "const_iv.h"
1462 root 1.359 # define def(uc, lc, name, plus, change) const_iv (AT_ ## uc) const_iv (ATNR_ ## uc)
1463     # include "attackinc.h"
1464     # undef def
1465     # define def(uc, flags) const_iv (SK_ ## uc)
1466     # include "skillinc.h"
1467     # undef def
1468 root 1.394 # define def(name, use, nonuse) const_iv (body_ ## name)
1469     # include "slotinc.h"
1470     # undef def
1471 root 1.359
1472 root 1.189 const_iv (MAP_CLIENT_X) const_iv (MAP_CLIENT_Y)
1473 root 1.180
1474 root 1.5 const_iv (MAX_TIME)
1475 root 1.258 const_iv (MAXSOCKBUF)
1476 root 1.189
1477     const_iv (UPD_LOCATION) const_iv (UPD_FLAGS) const_iv (UPD_WEIGHT) const_iv (UPD_FACE)
1478     const_iv (UPD_NAME) const_iv (UPD_ANIM) const_iv (UPD_ANIMSPEED) const_iv (UPD_NROF)
1479    
1480 root 1.350 const_iv (UPD_SP_MANA) const_iv (UPD_SP_GRACE) const_iv (UPD_SP_LEVEL)
1481 root 1.189
1482     const_iv (F_APPLIED) const_iv (F_LOCATION) const_iv (F_UNPAID) const_iv (F_MAGIC)
1483     const_iv (F_CURSED) const_iv (F_DAMNED) const_iv (F_OPEN) const_iv (F_NOPICK)
1484 root 1.1 const_iv (F_LOCKED)
1485    
1486 root 1.293 const_iv (P_BLOCKSVIEW) const_iv (P_NO_MAGIC) const_iv (P_IS_ALIVE)
1487 root 1.189 const_iv (P_NO_CLERIC) const_iv (P_OUT_OF_MAP) const_iv (P_NEW_MAP) const_iv (P_UPTODATE)
1488    
1489     const_iv (SAVE_MODE) const_iv (SAVE_DIR_MODE)
1490    
1491     const_iv (SK_EXP_ADD_SKILL) const_iv (SK_EXP_TOTAL) const_iv (SK_EXP_NONE)
1492     const_iv (SK_SUBTRACT_SKILL_EXP) const_iv (SK_EXP_SKILL_ONLY)
1493    
1494     const_iv (KLASS_ATTACHABLE) const_iv (KLASS_GLOBAL) const_iv (KLASS_OBJECT)
1495     const_iv (KLASS_CLIENT) const_iv (KLASS_PLAYER) const_iv (KLASS_MAP)
1496    
1497     const_iv (CS_QUERY_YESNO) const_iv (CS_QUERY_SINGLECHAR) const_iv (CS_QUERY_HIDEINPUT)
1498    
1499     const_iv (IO_HEADER) const_iv (IO_OBJECTS) const_iv (IO_UNIQUES)
1500 root 1.1 };
1501    
1502 root 1.383 for (civ = const_iv + array_length (const_iv); civ-- > const_iv; )
1503 root 1.109 newCONSTSUB (stash_cf, (char *)civ->name, newSViv (civ->iv));
1504 root 1.1
1505     static const struct {
1506     const char *name;
1507 root 1.14 int skip;
1508 root 1.7 IV klass;
1509 root 1.1 IV iv;
1510 root 1.6 } *eiv, event_iv[] = {
1511 root 1.14 # define def(klass,name) { "EVENT_" # klass "_" # name, sizeof ("EVENT_" # klass), (IV)KLASS_ ## klass, (IV)EVENT_ ## klass ## _ ## name },
1512 root 1.6 # include "eventinc.h"
1513     # undef def
1514     };
1515    
1516     AV *av = get_av ("cf::EVENT", 1);
1517    
1518 root 1.383 for (eiv = event_iv + array_length (event_iv); eiv-- > event_iv; )
1519 root 1.7 {
1520     AV *event = newAV ();
1521 root 1.14 av_push (event, newSVpv ((char *)eiv->name + eiv->skip, 0));
1522 root 1.7 av_push (event, newSViv (eiv->klass));
1523     av_store (av, eiv->iv, newRV_noinc ((SV *)event));
1524 root 1.109 newCONSTSUB (stash_cf, (char *)eiv->name, newSViv (eiv->iv));
1525 root 1.7 }
1526 root 1.324
1527     // used by autogenerated BOOT sections from genacc
1528     av_reflect = get_av ("cf::REFLECT", 1);
1529 root 1.14 }
1530    
1531 root 1.295 void _gv_clear (SV *gv)
1532     CODE:
1533     assert (SvTYPE (gv) == SVt_PVGV);
1534     # define f(sv) { SV *sv_ = (SV *)(sv); sv = 0; SvREFCNT_dec (sv_); }
1535 root 1.410 SvREFCNT_inc (gv);
1536 root 1.295 f (GvGP (gv)->gp_form);
1537     f (GvGP (gv)->gp_io);
1538     f (GvGP (gv)->gp_sv);
1539     f (GvGP (gv)->gp_av);
1540     f (GvGP (gv)->gp_hv);
1541     f (GvGP (gv)->gp_cv);
1542     GvCVGEN (gv) = 0;
1543     GvMULTI_off (gv);
1544 root 1.410 SvREFCNT_dec (gv);
1545 root 1.295 # undef f
1546    
1547 root 1.324 void _connect_to_perl_1 ()
1548    
1549     void _connect_to_perl_2 ()
1550 root 1.14
1551 root 1.210 void _recalc_want ()
1552    
1553 root 1.304 # not used by default anymore
1554 root 1.47 void _global_reattach ()
1555 root 1.14 CODE:
1556     {
1557     // reattach to all attachable objects in the game.
1558 root 1.128 for_all_clients (ns)
1559     ns->reattach ();
1560 root 1.96
1561 root 1.128 for_all_objects (op)
1562 root 1.109 op->reattach ();
1563 root 1.1 }
1564    
1565 root 1.303 void init_anim ()
1566    
1567     void init_globals ()
1568    
1569     void init_attackmess ()
1570    
1571     void init_dynamic ()
1572    
1573     void load_settings ()
1574    
1575 root 1.417 void _reload_exp_table ()
1576 root 1.385
1577 root 1.417 void _reload_materials ()
1578 root 1.303
1579     void init_uuid ()
1580     CODE:
1581     UUID::init ();
1582    
1583     void init_signals ()
1584    
1585     void init_skills ()
1586    
1587     void init_beforeplay ()
1588    
1589 root 1.273 void evthread_start (int aiofd)
1590 root 1.272
1591     void cede_to_tick ()
1592 root 1.236 CODE:
1593 root 1.272 coroapi::cede_to_tick ();
1594 root 1.236
1595 root 1.272 NV till_tick ()
1596 root 1.236 CODE:
1597 root 1.272 RETVAL = SvNVX (sv_next_tick) - now ();
1598 root 1.236 OUTPUT:
1599     RETVAL
1600    
1601 root 1.272 int tick_inhibit ()
1602 root 1.236 CODE:
1603 root 1.272 RETVAL = tick_inhibit;
1604 root 1.236 OUTPUT:
1605     RETVAL
1606    
1607 root 1.272 void tick_inhibit_inc ()
1608     CODE:
1609     ++tick_inhibit;
1610    
1611     void tick_inhibit_dec ()
1612     CODE:
1613     if (!--tick_inhibit)
1614     if (tick_pending)
1615     {
1616     ev_async_send (EV_DEFAULT, &tick_watcher);
1617     coroapi::cede ();
1618     }
1619    
1620 root 1.405 void one_tick ()
1621 root 1.272 CODE:
1622     {
1623 root 1.347 ev_now_update (EV_DEFAULT);
1624 root 1.272 NOW = ev_now (EV_DEFAULT);
1625     SvNV_set (sv_now, NOW); SvNOK_only (sv_now);
1626     SvNV_set (sv_tick_start, NOW); SvNOK_only (sv_tick_start);
1627     runtime = SvNVX (sv_runtime);
1628    
1629 root 1.405 one_tick ();
1630    
1631     ++server_tick;
1632     SvUV_set (sv_server_tick, server_tick); SvIOK_only_UV (sv_server_tick);
1633 root 1.272
1634 root 1.347 ev_now_update (EV_DEFAULT);
1635     NOW = ev_now (EV_DEFAULT);
1636 root 1.272 SvNV_set (sv_now, NOW); SvNOK_only (sv_now);
1637 root 1.405
1638 root 1.272 runtime += TICK;
1639     SvNV_set (sv_runtime, runtime); SvNOK_only (sv_runtime);
1640     }
1641    
1642 root 1.1 NV floor (NV x)
1643    
1644     NV ceil (NV x)
1645    
1646 root 1.143 NV rndm (...)
1647 root 1.286 ALIAS:
1648     rmg_rndm = 1
1649 root 1.143 CODE:
1650 root 1.286 {
1651     rand_gen &gen = ix ? rmg_rndm : rndm;
1652 root 1.143 switch (items)
1653     {
1654 root 1.286 case 0: RETVAL = gen (); break;
1655     case 1: RETVAL = gen (SvUV (ST (0))); break;
1656     case 2: RETVAL = gen (SvIV (ST (0)), SvIV (ST (1))); break;
1657 root 1.368 default: croak ("cf::rndm requires zero, one or two parameters."); break;
1658 root 1.143 }
1659 root 1.286 }
1660 root 1.143 OUTPUT:
1661     RETVAL
1662    
1663 root 1.207 NV clamp (NV value, NV min_value, NV max_value)
1664     CODE:
1665     RETVAL = clamp (value, min_value, max_value);
1666     OUTPUT:
1667     RETVAL
1668    
1669     NV lerp (NV value, NV min_in, NV max_in, NV min_out, NV max_out)
1670     CODE:
1671     RETVAL = lerp (value, min_in, max_in, min_out, max_out);
1672     OUTPUT:
1673     RETVAL
1674    
1675 root 1.360 const char *ordinal (int i)
1676    
1677 root 1.268 void weaken (...)
1678     PROTOTYPE: @
1679     CODE:
1680     while (items > 0)
1681     sv_rvweaken (ST (--items));
1682    
1683 root 1.366 void log_suspend ()
1684    
1685     void log_resume ()
1686    
1687     void log_backtrace (utf8_string msg)
1688 root 1.198
1689 root 1.366 void LOG (int flags, utf8_string msg)
1690 root 1.1 PROTOTYPE: $$
1691 root 1.198 C_ARGS: flags, "%s", msg
1692 root 1.1
1693 root 1.183 octet_string path_combine (octet_string base, octet_string path)
1694 root 1.1 PROTOTYPE: $$
1695    
1696 root 1.183 octet_string path_combine_and_normalize (octet_string base, octet_string path)
1697 root 1.1 PROTOTYPE: $$
1698    
1699     void
1700     sub_generation_inc ()
1701     CODE:
1702     PL_sub_generation++;
1703    
1704 root 1.183 const_octet_string
1705 root 1.1 mapdir ()
1706     PROTOTYPE:
1707     ALIAS:
1708     mapdir = 0
1709     uniquedir = 1
1710     tmpdir = 2
1711     confdir = 3
1712     localdir = 4
1713     playerdir = 5
1714     datadir = 6
1715     CODE:
1716 root 1.19 switch (ix)
1717     {
1718     case 0: RETVAL = settings.mapdir ; break;
1719     case 1: RETVAL = settings.uniquedir; break;
1720     case 2: RETVAL = settings.tmpdir ; break;
1721     case 3: RETVAL = settings.confdir ; break;
1722     case 4: RETVAL = settings.localdir ; break;
1723     case 5: RETVAL = settings.playerdir; break;
1724     case 6: RETVAL = settings.datadir ; break;
1725     }
1726 root 1.1 OUTPUT: RETVAL
1727    
1728 root 1.120 void abort ()
1729    
1730 root 1.199 void reset_signals ()
1731    
1732 root 1.270 void fork_abort (const_octet_string cause = "cf::fork_abort")
1733 root 1.144
1734 root 1.270 void cleanup (const_octet_string cause, bool make_core = false)
1735 root 1.134
1736 root 1.116 void emergency_save ()
1737    
1738 root 1.156 void _exit (int status = EXIT_SUCCESS)
1739    
1740 root 1.125 #if _POSIX_MEMLOCK
1741    
1742     int mlockall (int flags = MCL_CURRENT | MCL_FUTURE)
1743 root 1.271 INIT:
1744 root 1.279 #if __GLIBC__
1745 root 1.300 mallopt (M_TOP_PAD, 1024 * 1024);
1746 root 1.396 mallopt (M_MMAP_THRESHOLD, 1024 * 1024);
1747 root 1.300 mallopt (M_MMAP_MAX, 0); // likely bug-workaround, also frees memory
1748 root 1.397 if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 10))
1749 root 1.396 mallopt (M_PERTURB, 0xee); // bug-workaround for linux glibc+mlockall+calloc
1750 root 1.277 #endif
1751 root 1.125
1752     int munlockall ()
1753    
1754     #endif
1755    
1756 root 1.279 int
1757     malloc_trim (IV pad = 0)
1758    
1759     void
1760     mallinfo ()
1761     PPCODE:
1762     {
1763     #if __GLIBC__
1764     struct mallinfo mai = mallinfo ();
1765     EXTEND (SP, 10*2);
1766     PUSHs (sv_2mortal (newSVpv ("arena" , 0))); PUSHs (sv_2mortal (newSViv (mai.arena)));
1767     PUSHs (sv_2mortal (newSVpv ("ordblks" , 0))); PUSHs (sv_2mortal (newSViv (mai.ordblks)));
1768     PUSHs (sv_2mortal (newSVpv ("smblks" , 0))); PUSHs (sv_2mortal (newSViv (mai.smblks)));
1769     PUSHs (sv_2mortal (newSVpv ("hblks" , 0))); PUSHs (sv_2mortal (newSViv (mai.hblks)));
1770     PUSHs (sv_2mortal (newSVpv ("hblkhd" , 0))); PUSHs (sv_2mortal (newSViv (mai.hblkhd)));
1771     PUSHs (sv_2mortal (newSVpv ("usmblks" , 0))); PUSHs (sv_2mortal (newSViv (mai.usmblks)));
1772     PUSHs (sv_2mortal (newSVpv ("fsmblks" , 0))); PUSHs (sv_2mortal (newSViv (mai.fsmblks)));
1773     PUSHs (sv_2mortal (newSVpv ("uordblks", 0))); PUSHs (sv_2mortal (newSViv (mai.uordblks)));
1774     PUSHs (sv_2mortal (newSVpv ("fordblks", 0))); PUSHs (sv_2mortal (newSViv (mai.fordblks)));
1775     PUSHs (sv_2mortal (newSVpv ("keepcost", 0))); PUSHs (sv_2mortal (newSViv (mai.keepcost)));
1776     #endif
1777 root 1.308 EXTEND (SP, 5*2);
1778 root 1.279 PUSHs (sv_2mortal (newSVpv ("slice_alloc", 0))); PUSHs (sv_2mortal (newSVuv (slice_alloc)));
1779     PUSHs (sv_2mortal (newSVpv ("shstr_alloc", 0))); PUSHs (sv_2mortal (newSVuv (shstr_alloc)));
1780 root 1.282 PUSHs (sv_2mortal (newSVpv ("objects" , 0))); PUSHs (sv_2mortal (newSVuv (objects.size () * sizeof (object))));
1781 root 1.308 PUSHs (sv_2mortal (newSVpv ("sv_count" , 0))); PUSHs (sv_2mortal (newSVuv (PL_sv_count)));
1782     PUSHs (sv_2mortal (newSVpv ("sv_objcount", 0))); PUSHs (sv_2mortal (newSVuv (PL_sv_objcount)));
1783 root 1.279 }
1784    
1785 root 1.183 int find_animation (utf8_string text)
1786 root 1.1 PROTOTYPE: $
1787    
1788 root 1.74 int random_roll (int min, int max, object *op, int goodbad);
1789 root 1.1
1790 root 1.183 const_utf8_string cost_string_from_value(uint64 cost, int approx = 0)
1791 root 1.1
1792 root 1.373 int exp_to_level (val64 exp)
1793 root 1.1
1794 root 1.373 val64 level_to_min_exp (int level)
1795 root 1.1
1796 root 1.417 const_utf8_string
1797     spellpathnames (int idx)
1798     CODE:
1799     RETVAL = spellpathnames [idx];
1800     OUTPUT: RETVAL
1801    
1802 root 1.1 SV *
1803 root 1.394 attacktype_name (int atnr)
1804 root 1.1 CODE:
1805 root 1.394 RETVAL = IN_RANGE_EXC (atnr, 0, NROFATTACKS)
1806     ? newSVpv (attacktype_name [atnr], 0)
1807     : &PL_sv_undef;
1808     OUTPUT: RETVAL
1809    
1810     SV *
1811     attacktype_desc (int atnr)
1812     CODE:
1813     RETVAL = IN_RANGE_EXC (atnr, 0, NROFATTACKS)
1814     ? newSVpv (attacktype_desc [atnr], 0)
1815     : &PL_sv_undef;
1816     OUTPUT: RETVAL
1817    
1818     SV *
1819     resist_plus (int atnr)
1820     CODE:
1821     RETVAL = IN_RANGE_EXC (atnr, 0, NROFATTACKS)
1822     ? newSVpv (resist_plus [atnr], 0)
1823     : &PL_sv_undef;
1824     OUTPUT: RETVAL
1825    
1826     SV *
1827     change_resist_msg (int atnr)
1828     CODE:
1829     RETVAL = IN_RANGE_EXC (atnr, 0, NROFATTACKS)
1830     ? newSVpv (change_resist_msg [atnr], 0)
1831     : &PL_sv_undef;
1832 root 1.1 OUTPUT: RETVAL
1833    
1834 root 1.275 UUID
1835 root 1.274 uuid_cur ()
1836     CODE:
1837 root 1.275 RETVAL = UUID::cur;
1838 root 1.274 OUTPUT:
1839     RETVAL
1840    
1841 root 1.275 UUID
1842 root 1.274 uuid_gen ()
1843     CODE:
1844 root 1.275 RETVAL = UUID::gen ();
1845     OUTPUT:
1846     RETVAL
1847    
1848     val64
1849     uuid_seq (UUID uuid)
1850     CODE:
1851     RETVAL = uuid.seq;
1852     OUTPUT:
1853     RETVAL
1854    
1855     UUID
1856     uuid_str (val64 seq)
1857     CODE:
1858     RETVAL.seq = seq;
1859 root 1.274 OUTPUT:
1860     RETVAL
1861    
1862     void
1863     coin_names ()
1864     PPCODE:
1865     EXTEND (SP, NUM_COINS);
1866     for (int i = 0; i < NUM_COINS; ++i)
1867     PUSHs (sv_2mortal (newSVpv (coins [i], 0)));
1868    
1869     void
1870     coin_archetypes ()
1871     PPCODE:
1872     EXTEND (SP, NUM_COINS);
1873     for (int i = 0; i < NUM_COINS; ++i)
1874     PUSHs (sv_2mortal (to_sv (archetype::find (coins [i]))));
1875    
1876 root 1.162 bool
1877 root 1.278 load_resource_file_ (octet_string filename)
1878 root 1.162
1879 root 1.288 void
1880     fix_weight ()
1881    
1882 root 1.97 MODULE = cf PACKAGE = cf::attachable
1883    
1884 root 1.27 int
1885 root 1.97 valid (SV *obj)
1886 root 1.27 CODE:
1887     RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext);
1888     OUTPUT:
1889     RETVAL
1890    
1891 root 1.377 bool should_invoke (attachable *obj, int event)
1892     CODE:
1893     RETVAL = obj->should_invoke ((event_type)event);
1894     OUTPUT: RETVAL
1895    
1896 root 1.164 void
1897     debug_trace (attachable *obj, bool on = true)
1898     CODE:
1899 root 1.336 obj->attachable_flags &= ~attachable::F_DEBUG_TRACE;
1900 root 1.164 if (on)
1901 root 1.336 obj->attachable_flags |= attachable::F_DEBUG_TRACE;
1902 root 1.164
1903 root 1.153 int mortals_size ()
1904     CODE:
1905     RETVAL = attachable::mortals.size ();
1906     OUTPUT: RETVAL
1907    
1908     #object *mortals (U32 index)
1909     # CODE:
1910     # RETVAL = index < attachable::mortals.size () ? attachable::mortals [index] : 0;
1911     # OUTPUT: RETVAL
1912    
1913 root 1.358 INCLUDE: $PERL $srcdir/genacc attachable $srcdir/../include/util.h $srcdir/../include/cfperl.h |
1914 root 1.115
1915 root 1.101 MODULE = cf PACKAGE = cf::global
1916    
1917     int invoke (SV *klass, int event, ...)
1918     CODE:
1919     if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL");
1920     AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1921     for (int i = 1; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1922 root 1.109 RETVAL = gbl_ev.invoke ((event_type)event, ARG_AV (av), DT_END);
1923 root 1.101 OUTPUT: RETVAL
1924    
1925 root 1.1 MODULE = cf PACKAGE = cf::object PREFIX = cf_object_
1926    
1927 root 1.358 INCLUDE: $PERL $srcdir/genacc object $srcdir/../include/object.h |
1928 root 1.62
1929 root 1.18 int invoke (object *op, int event, ...)
1930     CODE:
1931     if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT");
1932 root 1.24 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1933     for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1934 root 1.109 RETVAL = op->invoke ((event_type)event, ARG_AV (av), DT_END);
1935 root 1.18 OUTPUT: RETVAL
1936    
1937     SV *registry (object *op)
1938    
1939 root 1.134 int objects_size ()
1940     CODE:
1941     RETVAL = objects.size ();
1942     OUTPUT: RETVAL
1943    
1944     object *objects (U32 index)
1945     CODE:
1946     RETVAL = index < objects.size () ? objects [index] : 0;
1947     OUTPUT: RETVAL
1948    
1949     int actives_size ()
1950     CODE:
1951     RETVAL = actives.size ();
1952     OUTPUT: RETVAL
1953    
1954     object *actives (U32 index)
1955 root 1.57 CODE:
1956 root 1.134 RETVAL = index < actives.size () ? actives [index] : 0;
1957 root 1.57 OUTPUT: RETVAL
1958    
1959 root 1.283 int mortals_size ()
1960     CODE:
1961     RETVAL = attachable::mortals.size ();
1962     OUTPUT: RETVAL
1963    
1964 root 1.394 const_utf8_string slot_name (U32 slot)
1965 root 1.205 ALIAS:
1966 root 1.394 slot_name = 0
1967     slot_use_name = 1
1968     slot_nonuse_name = 2
1969 root 1.205 CODE:
1970     {
1971     if (slot >= NUM_BODY_LOCATIONS)
1972     croak ("body slot index out of range");
1973    
1974     switch (ix)
1975     {
1976 root 1.394 case 0: RETVAL = body_locations[slot].name ; break;
1977     case 1: RETVAL = body_locations[slot].use_name ; break;
1978     case 2: RETVAL = body_locations[slot].nonuse_name; break;
1979 root 1.205 }
1980     }
1981     OUTPUT:
1982     RETVAL
1983    
1984 root 1.1 # missing properties
1985    
1986 root 1.54 object *head (object *op)
1987     PROTOTYPE: $
1988     CODE:
1989 root 1.134 RETVAL = op->head_ ();
1990 root 1.54 OUTPUT: RETVAL
1991    
1992 root 1.1 void
1993     inv (object *obj)
1994     PROTOTYPE: $
1995     PPCODE:
1996     {
1997 root 1.254 for (object *o = obj->inv; o; o = o->below)
1998 root 1.100 XPUSHs (sv_2mortal (to_sv (o)));
1999 root 1.1 }
2000    
2001 root 1.102 void
2002     set_animation (object *op, int idx)
2003     CODE:
2004     SET_ANIMATION (op, idx);
2005    
2006 elmex 1.160 int
2007     num_animations (object *op)
2008     CODE:
2009     RETVAL = NUM_ANIMATIONS (op);
2010     OUTPUT: RETVAL
2011    
2012 root 1.205 int slot_info (object *op, UV slot, int value = 0)
2013     ALIAS:
2014     slot_used = 1
2015     CODE:
2016     {
2017     if (slot >= NUM_BODY_LOCATIONS)
2018     croak ("body slot index out of range");
2019    
2020 root 1.208 RETVAL = ix ? op->slot[slot].used : op->slot[slot].info;
2021 root 1.205
2022     if (items > 2)
2023     if (ix)
2024 root 1.208 op->slot[slot].used = value;
2025     else
2026 root 1.205 op->slot[slot].info = value;
2027     }
2028     OUTPUT:
2029     RETVAL
2030    
2031 root 1.183 object *find_best_object_match (object *op, utf8_string match)
2032 root 1.58
2033 root 1.1 int apply_shop_mat (object *shop_mat, object *op);
2034    
2035 root 1.27 int move (object *op, int dir, object *originator = op)
2036     CODE:
2037 root 1.353 RETVAL = op->move (dir, originator);
2038 root 1.27 OUTPUT:
2039     RETVAL
2040 root 1.1
2041 root 1.74 void apply_below (object *op)
2042     CODE:
2043     player_apply_below (op);
2044 root 1.1
2045 root 1.167 int cast_heal (object *op, object *caster, object *spell, int dir = 0)
2046    
2047 root 1.330 int casting_level (object *caster, object *spell)
2048    
2049 root 1.74 int pay_item (object *op, object *buyer)
2050     CODE:
2051     RETVAL = pay_for_item (op, buyer);
2052     OUTPUT: RETVAL
2053 root 1.1
2054 root 1.74 int pay_amount (object *op, uint64 amount)
2055     CODE:
2056     RETVAL = pay_for_amount (amount, op);
2057     OUTPUT: RETVAL
2058 root 1.1
2059     void pay_player (object *op, uint64 amount)
2060    
2061 root 1.183 val64 pay_player_arch (object *op, utf8_string arch, uint64 amount)
2062 root 1.1
2063 root 1.183 int cast_spell (object *op, object *caster, int dir, object *spell_ob, utf8_string stringarg = 0)
2064 root 1.1
2065 root 1.74 void learn_spell (object *op, object *sp, int special_prayer = 0)
2066     CODE:
2067     do_learn_spell (op, sp, special_prayer);
2068 root 1.1
2069 root 1.74 void forget_spell (object *op, object *sp)
2070     CODE:
2071     do_forget_spell (op, query_name (sp));
2072 root 1.1
2073 root 1.183 object *check_for_spell (object *op, utf8_string spellname)
2074 root 1.74 CODE:
2075     RETVAL = check_spell_known (op, spellname);
2076     OUTPUT: RETVAL
2077 root 1.1
2078 root 1.74 int query_money (object *op)
2079 root 1.1 ALIAS: money = 0
2080    
2081 elmex 1.108 val64 query_cost (object *op, object *who, int flags)
2082 root 1.1 ALIAS: cost = 0
2083    
2084 root 1.74 void spring_trap (object *op, object *victim)
2085 root 1.1
2086 root 1.74 int check_trigger (object *op, object *cause)
2087 root 1.1
2088 root 1.74 void drop (object *who, object *op)
2089 root 1.1
2090 root 1.74 void pick_up (object *who, object *op)
2091 root 1.1
2092 root 1.102 void update_object (object *op, int action)
2093 root 1.1
2094 root 1.384 void change_exp (object *op, uint64 exp, shstr_tmp skill_name = shstr_tmp (), int flag = 0)
2095 root 1.1
2096     void player_lvl_adj (object *who, object *skill = 0)
2097    
2098     int kill_object (object *op, int dam = 0, object *hitter = 0, int type = AT_PHYSICAL)
2099    
2100 root 1.334 int calc_skill_exp (object *who, object *op, object *skill)
2101 root 1.1
2102 root 1.334 void push_button (object *op, object *originator)
2103 root 1.1
2104 root 1.334 void use_trigger (object *op, object *originator)
2105 root 1.1
2106 root 1.334 void handle_apply_yield (object *op)
2107 elmex 1.232
2108 root 1.334 int convert_item (object *item, object *converter)
2109 elmex 1.319
2110 elmex 1.352 void fix_generated_item (object *op, object *creator, int difficulty, int max_magic, int flags);
2111 root 1.1
2112     MODULE = cf PACKAGE = cf::object PREFIX = cf_
2113    
2114     # no clean way to get an object from an archetype - stupid idiotic
2115     # dumb kludgy misdesigned plug-in api slowly gets on my nerves.
2116    
2117 root 1.183 object *new (utf8_string archetype = 0)
2118 root 1.1 PROTOTYPE: ;$
2119     CODE:
2120 root 1.403 RETVAL = archetype
2121     ? archetype::get (archetype)
2122     : object::create ();
2123 root 1.1 OUTPUT:
2124     RETVAL
2125    
2126 elmex 1.351 object *generate (utf8_string arch, object *creator)
2127     CODE:
2128 root 1.403 object *obj = archetype::get (arch);
2129 elmex 1.351 fix_generated_item (obj, creator, 0, 0, GT_MINIMAL);
2130     RETVAL = obj;
2131     OUTPUT:
2132     RETVAL
2133    
2134 root 1.225 object *find_object (U32 tag)
2135    
2136 elmex 1.349 object *find_object_uuid (UUID i)
2137    
2138 root 1.218 # TODO: nuke
2139 root 1.61 object *insert_ob_in_map_at (object *ob, maptile *where, object_ornull *orig, int flag, int x, int y)
2140 root 1.1 PROTOTYPE: $$$$$$
2141     CODE:
2142     {
2143 root 1.257 RETVAL = insert_ob_in_map_at (ob, where, orig, flag, x, y);
2144 root 1.329
2145     if (RETVAL->destroyed ())
2146     RETVAL = 0;
2147 root 1.1 }
2148 root 1.407 OUTPUT:
2149     RETVAL
2150 root 1.1
2151 root 1.284 shstr
2152     object::kv_get (shstr key)
2153 root 1.402 CODE:
2154     RETVAL = THIS->kv.get (key);
2155     OUTPUT:
2156     RETVAL
2157 root 1.1
2158 root 1.284 void
2159     object::kv_del (shstr key)
2160 root 1.402 CODE:
2161     THIS->kv.del (key);
2162 root 1.284
2163     void
2164     object::kv_set (shstr key, shstr value)
2165 root 1.402 CODE:
2166     THIS->kv.set (key, value);
2167 root 1.1
2168     object *get_nearest_player (object *ob)
2169     ALIAS: nearest_player = 0
2170     PREINIT:
2171     extern object *get_nearest_player (object *);
2172    
2173     void rangevector (object *ob, object *other, int flags = 0)
2174     PROTOTYPE: $$;$
2175     PPCODE:
2176     {
2177     rv_vector rv;
2178 root 1.291
2179 root 1.1 get_rangevector (ob, other, &rv, flags);
2180 root 1.291
2181 root 1.1 EXTEND (SP, 5);
2182 root 1.364 PUSHs (sv_2mortal (newSVuv (rv.distance)));
2183     PUSHs (sv_2mortal (newSViv (rv.distance_x)));
2184     PUSHs (sv_2mortal (newSViv (rv.distance_y)));
2185     PUSHs (sv_2mortal (newSViv (rv.direction)));
2186 root 1.365 PUSHs (sv_2mortal (to_sv (rv.part)));
2187 root 1.1 }
2188    
2189     bool on_same_map_as (object *ob, object *other)
2190     CODE:
2191     RETVAL = on_same_map (ob, other);
2192     OUTPUT: RETVAL
2193    
2194 root 1.183 const_utf8_string
2195 root 1.58 base_name (object *op, int plural = op->nrof > 1)
2196 root 1.1 CODE:
2197 root 1.58 RETVAL = query_base_name (op, plural);
2198 root 1.1 OUTPUT: RETVAL
2199    
2200 root 1.256 # return the tail of an object, excluding itself
2201     void
2202     tail (object *op)
2203     PPCODE:
2204     while ((op = op->more))
2205     XPUSHs (sv_2mortal (to_sv (op)));
2206    
2207 root 1.1 MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_
2208    
2209     player *player (object *op)
2210     CODE:
2211     RETVAL = op->contr;
2212     OUTPUT: RETVAL
2213    
2214 root 1.257 bool move_player (object *op, int dir)
2215    
2216 root 1.183 void message (object *op, utf8_string txt, int flags = NDI_ORANGE | NDI_UNIQUE)
2217 root 1.120 CODE:
2218     new_draw_info (flags, 0, op, txt);
2219 root 1.1
2220     void kill_player (object *op)
2221    
2222 root 1.257 void esrv_send_item (object *pl, object *item)
2223    
2224     void esrv_update_item (object *pl, int what, object *item)
2225     C_ARGS: what, pl, item
2226    
2227     void esrv_del_item (object *pl, int tag)
2228     C_ARGS: pl->contr, tag
2229 root 1.58
2230 root 1.183 int command_summon (object *op, utf8_string params)
2231 root 1.67
2232 root 1.183 int command_arrest (object *op, utf8_string params)
2233 root 1.67
2234 root 1.66
2235 root 1.12 MODULE = cf PACKAGE = cf::player PREFIX = cf_player_
2236 root 1.1
2237 root 1.358 INCLUDE: $PERL $srcdir/genacc player $srcdir/../include/player.h |
2238 root 1.62
2239 root 1.18 int invoke (player *pl, int event, ...)
2240     CODE:
2241     if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER");
2242 root 1.24 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2243     for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
2244 root 1.109 RETVAL = pl->invoke ((event_type)event, ARG_AV (av), DT_END);
2245 root 1.18 OUTPUT: RETVAL
2246    
2247 root 1.12 SV *registry (player *pl)
2248 root 1.1
2249 root 1.102 void
2250     save_stats (player *pl)
2251     CODE:
2252     pl->ob->stats.hp = pl->ob->stats.maxhp;
2253     pl->ob->stats.sp = pl->ob->stats.maxsp;
2254     pl->ob->stats.grace = pl->ob->stats.maxgrace;
2255     pl->orig_stats = pl->ob->stats;
2256    
2257 root 1.307 # should only be temporary
2258 elmex 1.306 void esrv_new_player (player *pl)
2259    
2260 root 1.310 #d# TODO: replace by blocked_los accessor, fix code using this
2261 root 1.1 bool
2262     cell_visible (player *pl, int dx, int dy)
2263     CODE:
2264 root 1.310 RETVAL = pl->blocked_los (dx, dy) != LOS_BLOCKED;
2265 root 1.1 OUTPUT:
2266     RETVAL
2267    
2268 root 1.4 void
2269 root 1.1 send (player *pl, SV *packet)
2270     CODE:
2271     {
2272     STRLEN len;
2273     char *buf = SvPVbyte (packet, len);
2274    
2275 root 1.258 if (len > MAXSOCKBUF)
2276     pl->failmsg ("[packet too long for client]");
2277     else if (pl->ns)
2278 root 1.100 pl->ns->send_packet (buf, len);
2279 root 1.1 }
2280    
2281 root 1.46 void savebed (player *pl, SV *map_path = 0, SV *x = 0, SV *y = 0)
2282 root 1.45 PROTOTYPE: $;$$$
2283 root 1.1 PPCODE:
2284 root 1.45 if (GIMME_V != G_VOID)
2285     {
2286     EXTEND (SP, 3);
2287     PUSHs (sv_2mortal (newSVpv (pl->savebed_map, 0)));
2288     PUSHs (sv_2mortal (newSViv (pl->bed_x)));
2289     PUSHs (sv_2mortal (newSViv (pl->bed_y)));
2290     }
2291 root 1.46 if (map_path) sv_to (map_path, pl->savebed_map);
2292     if (x) sv_to (x, pl->bed_x);
2293     if (y) sv_to (y, pl->bed_y);
2294 root 1.1
2295     void
2296     list ()
2297     PPCODE:
2298 root 1.128 for_all_players (pl)
2299 root 1.100 XPUSHs (sv_2mortal (to_sv (pl)));
2300 root 1.1
2301    
2302     MODULE = cf PACKAGE = cf::map PREFIX = cf_map_
2303    
2304 root 1.61 int invoke (maptile *map, int event, ...)
2305 root 1.18 CODE:
2306     if (KLASS_OF (event) != KLASS_MAP) croak ("event class must be MAP");
2307 root 1.24 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2308     for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
2309 root 1.109 RETVAL = map->invoke ((event_type)event, ARG_AV (av), DT_END);
2310 root 1.25 OUTPUT: RETVAL
2311 root 1.18
2312 root 1.61 SV *registry (maptile *map)
2313 root 1.12
2314 root 1.255 void
2315     find_tagged_objects (maptile *map, utf8_string tag = 0)
2316     PPCODE:
2317     {
2318     if (!map->spaces)
2319     XSRETURN_EMPTY;
2320    
2321     if (tag)
2322     {
2323     shstr_cmp tag_ (tag);
2324    
2325     for (mapspace *ms = map->spaces + map->size (); ms-- > map->spaces; )
2326     for (object *op = ms->bot; op; op = op->above)
2327     if (op->tag == tag_)
2328     XPUSHs (sv_2mortal (to_sv (op)));
2329     }
2330     else
2331     {
2332     for (mapspace *ms = map->spaces + map->size (); ms-- > map->spaces; )
2333     for (object *op = ms->bot; op; op = op->above)
2334     if (op->tag)
2335     XPUSHs (sv_2mortal (to_sv (op)));
2336     }
2337     }
2338    
2339 root 1.358 INCLUDE: $PERL $srcdir/genacc maptile $srcdir/../include/map.h |
2340 root 1.1
2341 root 1.116 void
2342 root 1.312 adjust_daylight ()
2343     CODE:
2344     maptile::adjust_daylight ();
2345    
2346 root 1.318 int
2347     outdoor_darkness (int darkness = 0)
2348     CODE:
2349     RETVAL = maptile::outdoor_darkness;
2350     if (items)
2351     maptile::outdoor_darkness = darkness;
2352     OUTPUT:
2353     RETVAL
2354    
2355 root 1.312 void
2356 root 1.116 maptile::instantiate ()
2357    
2358     maptile *new ()
2359 root 1.1 PROTOTYPE:
2360     CODE:
2361 root 1.116 RETVAL = new maptile;
2362 root 1.1 OUTPUT:
2363     RETVAL
2364    
2365 root 1.116 void
2366 root 1.117 maptile::players ()
2367     PPCODE:
2368     if (GIMME_V == G_SCALAR)
2369 root 1.118 XPUSHs (sv_2mortal (to_sv (THIS->players)));
2370 root 1.117 else if (GIMME_V == G_ARRAY)
2371     {
2372     EXTEND (SP, THIS->players);
2373     for_all_players (pl)
2374     if (pl->ob && pl->ob->map == THIS)
2375 root 1.118 PUSHs (sv_2mortal (to_sv (pl->ob)));
2376 root 1.117 }
2377    
2378 root 1.156 void
2379 root 1.403 maptile::gen_quadmap (int x, int y, int z)
2380 root 1.400 CODE:
2381 root 1.403 gen_quadmap (THIS, x * 50, y * 50, z);
2382 root 1.401
2383 root 1.400 void
2384 root 1.168 maptile::add_underlay (SV *data, int offset, int stride, SV *palette)
2385 root 1.156 CODE:
2386     {
2387 root 1.168 if (!SvROK (palette) || SvTYPE (SvRV (palette)) != SVt_PVAV)
2388     croak ("maptile::add_underlay: palette must be arrayref");
2389 root 1.156
2390 root 1.168 palette = SvRV (palette);
2391 root 1.156
2392 root 1.168 STRLEN idxlen;
2393     const uint8_t *idx = (const uint8_t *)SvPVbyte (data, idxlen);
2394 root 1.156
2395 root 1.168 for (int x = 0; x < THIS->width; ++x)
2396     for (int y = 0; y < THIS->height; ++y)
2397     {
2398     for (object *op = THIS->at (x, y).bot; op; op = op->above)
2399     if (op->flag [FLAG_IS_FLOOR])
2400 root 1.340 goto skip;
2401 root 1.168
2402     {
2403     int offs = offset + y * stride + x;
2404 root 1.340
2405 root 1.168 if (IN_RANGE_EXC (offs, 0, idxlen))
2406     {
2407     if (SV **elem = av_fetch ((AV *)palette, idx [offs], 0))
2408     {
2409 root 1.403 object *ob = archetype::get (cfSvPVutf8_nolen (*elem));
2410 root 1.168 ob->flag [FLAG_NO_MAP_SAVE] = true;
2411     THIS->insert (ob, x, y, 0, INS_ABOVE_FLOOR_ONLY);
2412 root 1.200
2413 root 1.340 if (ob->randomitems && !ob->above)
2414 root 1.200 {
2415 root 1.340 ob->create_treasure (ob->randomitems);
2416 root 1.203
2417 root 1.340 for (object *op = ob->above; op; op = op->above)
2418     op->flag [FLAG_NO_MAP_SAVE] = true;
2419     // TODO: if this is a pickable object, then the item
2420     // will at a bit weird - saving inside the player
2421     // will clear the flag, but when the player drops
2422     // it without logging out, it keeps the flag.
2423 root 1.400 // nobody has reported this, but this can be rather
2424 root 1.340 // annoying on persistent maps.
2425 root 1.200 }
2426 root 1.168 }
2427     }
2428     }
2429 root 1.340 skip: ;
2430 root 1.168 }
2431     }
2432    
2433     void
2434     maptile::set_regiondata (SV *data, int offset, int stride, SV *palette)
2435     CODE:
2436     {
2437     if (!SvROK (palette) || SvTYPE (SvRV (palette)) != SVt_PVAV)
2438     croak ("maptile::set_regiondata: palette must be arrayref");
2439    
2440     palette = SvRV (palette);
2441    
2442     STRLEN idxlen;
2443     const uint8_t *idx = (const uint8_t *)SvPVbyte (data, idxlen);
2444    
2445 root 1.230 region_ptr *regionmap = new region_ptr [av_len ((AV *)palette) + 1];
2446 root 1.168 uint8_t *regions = salloc<uint8_t> (THIS->size ());
2447    
2448     for (int i = av_len ((AV *)palette) + 1; i--; )
2449 root 1.345 regionmap [i] = region::find (cfSvPVutf8_nolen (*av_fetch ((AV *)palette, i, 1)));
2450 root 1.168
2451     for (int y = 0; y < THIS->height; ++y)
2452     memcpy (regions + y * THIS->width, idx + offset + y * stride, THIS->width);
2453    
2454     sfree (THIS->regions, THIS->size ());
2455 root 1.230 delete [] THIS->regionmap;
2456 root 1.168
2457     THIS->regions = regions;
2458 root 1.156 THIS->regionmap = regionmap;
2459     }
2460    
2461 root 1.193 void
2462     maptile::create_region_treasure ()
2463     CODE:
2464     for (int x = 0; x < THIS->width; ++x)
2465     for (int y = 0; y < THIS->height; ++y)
2466     {
2467     region *rgn = THIS->region (x, y);
2468    
2469 root 1.301 if (object *op = THIS->at (x, y).top)
2470     if (rgn->treasure && rndm () < rgn->treasure_density)
2471 root 1.193 create_treasure (rgn->treasure, op, GT_ENVIRONMENT, THIS->difficulty);
2472     }
2473    
2474 root 1.74 int out_of_map (maptile *map, int x, int y)
2475    
2476 root 1.29 void
2477 root 1.315 find_link (maptile *map, shstr_tmp connection)
2478 root 1.29 PPCODE:
2479 root 1.315 if (oblinkpt *obp = map->find_link (connection))
2480 root 1.29 for (objectlink *ol = obp->link; ol; ol = ol->next)
2481 root 1.257 XPUSHs (sv_2mortal (to_sv ((object *)ol->ob)));
2482 root 1.1
2483     void
2484 root 1.378 xy_normalise (maptile *map, int x, int y, int dir = 0)
2485 root 1.1 PPCODE:
2486     {
2487 root 1.377 mapxy pos (map, x, y);
2488     if (!pos.move (dir).normalise ())
2489     XSRETURN_EMPTY;
2490 root 1.291
2491 root 1.377 EXTEND (SP, 3);
2492     PUSHs (sv_2mortal (to_sv (pos.m)));
2493     PUSHs (sv_2mortal (to_sv (pos.x)));
2494     PUSHs (sv_2mortal (to_sv (pos.y)));
2495 root 1.1 }
2496    
2497 root 1.293 mapspace *
2498 root 1.377 ms (maptile *map, unsigned int x, unsigned int y, int dir = 0)
2499     PROTOTYPE: $$$;$
2500 root 1.293 CODE:
2501     {
2502 root 1.377 mapxy pos (map, x, y);
2503     if (!pos.move (dir).normalise ())
2504 root 1.293 XSRETURN_UNDEF;
2505    
2506 root 1.377 RETVAL = &*pos;
2507 root 1.293 }
2508     OUTPUT:
2509     RETVAL
2510    
2511 root 1.1 void
2512 root 1.377 at (maptile *map, unsigned int x, unsigned int y, int dir = 0)
2513     PROTOTYPE: $$$;$
2514 root 1.1 PPCODE:
2515 root 1.377 mapxy pos (map, x, y);
2516     if (pos.move (dir).normalise ())
2517     for (object *o = pos->bot; o; o = o->above)
2518 root 1.257 XPUSHs (sv_2mortal (to_sv (o)));
2519 root 1.1
2520     SV *
2521 root 1.377 bot_at (maptile *map, unsigned int x, unsigned int y, int dir = 0)
2522     PROTOTYPE: $$$;$
2523 root 1.1 ALIAS:
2524     top_at = 1
2525     flags_at = 2
2526     light_at = 3
2527     move_block_at = 4
2528     move_slow_at = 5
2529     move_on_at = 6
2530     move_off_at = 7
2531     CODE:
2532 root 1.309 {
2533 root 1.377 mapxy pos (map, x, y);
2534     if (!pos.move (dir).normalise ())
2535 root 1.309 XSRETURN_UNDEF;
2536    
2537 root 1.377 mapspace &ms = *pos;
2538 root 1.309
2539     ms.update ();
2540    
2541 root 1.1 switch (ix)
2542     {
2543 root 1.309 case 0: RETVAL = to_sv (ms.bot ); break;
2544     case 1: RETVAL = to_sv (ms.top ); break;
2545     case 2: RETVAL = newSVuv (ms.flags_ ); break;
2546     case 3: RETVAL = newSViv (ms.light ); break;
2547     case 4: RETVAL = newSVuv (ms.move_block); break;
2548     case 5: RETVAL = newSVuv (ms.move_slow ); break;
2549     case 6: RETVAL = newSVuv (ms.move_on ); break;
2550     case 7: RETVAL = newSVuv (ms.move_off ); break;
2551 root 1.1 }
2552 root 1.309 }
2553 root 1.122 OUTPUT: RETVAL
2554 root 1.1
2555 root 1.117 # worst xs function of my life
2556 root 1.140 bool
2557 root 1.391 _create_random_map (maptile *self, SV *options)
2558 root 1.117 CODE:
2559     {
2560 root 1.391 random_map_params rmp ((HV *)SvRV (options));
2561 root 1.140 RETVAL = self->generate_random_map (&rmp);
2562 root 1.117 }
2563     OUTPUT:
2564     RETVAL
2565    
2566 root 1.293 MODULE = cf PACKAGE = cf::mapspace
2567    
2568 root 1.358 INCLUDE: $PERL $srcdir/genacc mapspace $srcdir/../include/map.h |
2569 root 1.293
2570 root 1.19 MODULE = cf PACKAGE = cf::arch
2571 root 1.1
2572 root 1.218 int archetypes_size ()
2573     CODE:
2574     RETVAL = archetypes.size ();
2575     OUTPUT: RETVAL
2576    
2577     archetype *archetypes (U32 index)
2578     CODE:
2579     RETVAL = index < archetypes.size () ? archetypes [index] : 0;
2580     OUTPUT: RETVAL
2581 root 1.1
2582 root 1.417 int skillvec_size ()
2583     CODE:
2584     RETVAL = skillvec.size ();
2585     OUTPUT: RETVAL
2586    
2587     archetype *skillvec (U32 index)
2588     CODE:
2589     RETVAL = index < skillvec.size () ? (archetype *)(object *)skillvec [index] : 0;
2590     OUTPUT: RETVAL
2591    
2592 root 1.358 INCLUDE: $PERL $srcdir/genacc archetype $srcdir/../include/object.h |
2593 root 1.1
2594 root 1.19 MODULE = cf PACKAGE = cf::party
2595 root 1.1
2596 root 1.19 partylist *first ()
2597 root 1.1 PROTOTYPE:
2598 root 1.19 CODE:
2599     RETVAL = get_firstparty ();
2600     OUTPUT: RETVAL
2601 root 1.1
2602 root 1.358 INCLUDE: $PERL $srcdir/genacc partylist $srcdir/../include/player.h |
2603 root 1.1
2604 root 1.19 MODULE = cf PACKAGE = cf::region
2605 root 1.1
2606 root 1.161 void
2607     list ()
2608     PPCODE:
2609     for_all_regions (rgn)
2610     XPUSHs (sv_2mortal (to_sv (rgn)));
2611    
2612 root 1.186 int specificity (region *rgn)
2613     CODE:
2614     RETVAL = 0;
2615     while (rgn = rgn->parent)
2616     RETVAL++;
2617     OUTPUT: RETVAL
2618    
2619 root 1.358 INCLUDE: $PERL $srcdir/genacc region $srcdir/../include/region.h |
2620 root 1.1
2621 root 1.19 MODULE = cf PACKAGE = cf::living
2622 root 1.1
2623 root 1.358 INCLUDE: $PERL $srcdir/genacc living $srcdir/../include/living.h |
2624 root 1.1
2625 root 1.76 MODULE = cf PACKAGE = cf::settings
2626    
2627 root 1.358 INCLUDE: $PERL $srcdir/genacc Settings $srcdir/../include/global.h |
2628 root 1.76
2629 root 1.84 MODULE = cf PACKAGE = cf::client
2630 root 1.79
2631 root 1.358 INCLUDE: $PERL $srcdir/genacc client $srcdir/../include/client.h |
2632 root 1.79
2633 root 1.84 int invoke (client *ns, int event, ...)
2634 root 1.79 CODE:
2635 root 1.88 if (KLASS_OF (event) != KLASS_CLIENT) croak ("event class must be CLIENT");
2636 root 1.79 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2637     for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
2638 root 1.109 RETVAL = ns->invoke ((event_type)event, ARG_AV (av), DT_END);
2639 root 1.79 OUTPUT: RETVAL
2640    
2641 root 1.84 SV *registry (client *ns)
2642 root 1.79
2643 root 1.100 void
2644     list ()
2645     PPCODE:
2646     EXTEND (SP, clients.size ());
2647     for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
2648     PUSHs (sv_2mortal (to_sv (*i)));
2649    
2650 root 1.414 STRLEN
2651     client::inbuf_append (SV *data)
2652     CODE:
2653     {
2654     STRLEN len;
2655     char *ptr = SvPVbyte (data, len);
2656    
2657     min_it (len, sizeof (THIS->inbuf) - THIS->inbuf_len);
2658     memcpy (THIS->inbuf + THIS->inbuf_len, ptr, len);
2659     THIS->inbuf_len += len;
2660    
2661     THIS->inbuf_handle ();
2662    
2663     RETVAL = len;
2664     }
2665     OUTPUT: RETVAL
2666    
2667 root 1.88 void
2668 root 1.100 client::send_packet (SV *packet)
2669     CODE:
2670     {
2671     STRLEN len;
2672     char *buf = SvPVbyte (packet, len);
2673    
2674 root 1.258 if (len > MAXSOCKBUF)
2675     {
2676     // ugly
2677     if (THIS->pl)
2678     THIS->pl->failmsg ("[packet too long for client]");
2679     }
2680     else
2681     THIS->send_packet (buf, len);
2682 root 1.100 }
2683    
2684 root 1.237 faceidx
2685 root 1.238 client::need_face (utf8_string name, int pri = 0)
2686 root 1.237 CODE:
2687 root 1.238 RETVAL = face_find (name, 0);
2688     if (RETVAL)
2689     {
2690     THIS->send_face (RETVAL, pri);
2691     THIS->flush_fx ();
2692     }
2693     OUTPUT:
2694     RETVAL
2695    
2696     int
2697     client::fx_want (int idx, int value = -1)
2698     CODE:
2699     if (0 < idx && idx < FT_NUM)
2700     {
2701     RETVAL = THIS->fx_want [idx];
2702     if (items > 2)
2703     THIS->fx_want [idx] = value;
2704     }
2705     else
2706     RETVAL = 0;
2707 root 1.237 OUTPUT:
2708     RETVAL
2709    
2710 root 1.239 MODULE = cf PACKAGE = cf::sound PREFIX = sound_
2711    
2712     faceidx sound_find (utf8_string name)
2713    
2714 root 1.240 void sound_set (utf8_string str, faceidx face)
2715    
2716     # dire hack
2717     void old_sound_index (int idx, faceidx face)
2718     CODE:
2719     extern faceidx old_sound_index [SOUND_CAST_SPELL_0];
2720     old_sound_index [idx] = face;
2721    
2722 root 1.176 MODULE = cf PACKAGE = cf::face PREFIX = face_
2723    
2724 root 1.358 #INCLUDE: $PERL $srcdir/genacc faceset $srcdir/../include/face.h |
2725 root 1.176
2726 root 1.183 faceidx face_find (utf8_string name, faceidx defidx = 0)
2727 root 1.176
2728 root 1.183 faceidx alloc (utf8_string name)
2729 root 1.176 CODE:
2730     {
2731 root 1.408 RETVAL = face_alloc ();
2732 root 1.176
2733     faces [RETVAL].name = name;
2734     facehash.insert (std::make_pair (faces [RETVAL].name, RETVAL));
2735    
2736 root 1.369 if (!strcmp (name, BLANK_FACE_NAME )) blank_face = RETVAL;
2737     if (!strcmp (name, EMPTY_FACE_NAME )) empty_face = RETVAL;
2738     if (!strcmp (name, MAGICMOUTH_FACE_NAME)) magicmouth_face = RETVAL;
2739 root 1.176 }
2740 root 1.413 OUTPUT: RETVAL
2741 root 1.412
2742     faceidx faces_size ()
2743     CODE:
2744     RETVAL = faces.size ();
2745 root 1.176 OUTPUT: RETVAL
2746    
2747 root 1.227 void set_type (faceidx idx, int value)
2748     ALIAS:
2749     set_type = 0
2750     set_visibility = 1
2751     set_magicmap = 2
2752     set_smooth = 3
2753     set_smoothlevel = 4
2754 root 1.176 CODE:
2755 root 1.229 faceinfo *f = face_info (idx); assert (f);
2756 root 1.227 switch (ix)
2757     {
2758     case 0: f->type = value; break;
2759     case 1: f->visibility = value; break;
2760     case 2: f->magicmap = value; break;
2761     case 3: f->smooth = value; break;
2762     case 4: f->smoothlevel = value; break;
2763     }
2764 root 1.177
2765     void set_data (faceidx idx, int faceset, SV *data, SV *chksum)
2766 root 1.176 CODE:
2767 root 1.182 {
2768 root 1.231 faceinfo *f = face_info (idx); assert (f);
2769 root 1.393 facedata *d = f->face + faceset;
2770 root 1.181 sv_to (data, d->data);
2771     STRLEN clen;
2772     char *cdata = SvPVbyte (chksum, clen);
2773 root 1.182
2774 root 1.416 assert (("cf::face::set_data called with too long checksum", clen <= CHKSUM_MAXLEN));
2775 root 1.344 assert (("cf::face::set_data must be called with a non-empty checksum", clen));
2776    
2777 root 1.338 if (clen != d->chksum_len || memcmp (d->chksum, cdata, clen))
2778 root 1.182 {
2779 root 1.338 d->chksum_len = clen;
2780 root 1.182 memcpy (d->chksum, cdata, clen);
2781    
2782     // invalidate existing client face info
2783     for_all_clients (ns)
2784 root 1.418 ns->invalidate_face (idx);
2785 root 1.182 }
2786     }
2787 root 1.176
2788 root 1.229 int get_data_size (faceidx idx, int faceset = 0)
2789     CODE:
2790 root 1.267 facedata *d = face_data (idx, faceset);
2791     if (!d) XSRETURN_UNDEF;
2792 root 1.229 RETVAL = d->data.size ();
2793     OUTPUT:
2794     RETVAL
2795    
2796 root 1.409 void set_meta (faceidx idx, SV *sv)
2797     CODE:
2798     {
2799     faceinfo *f = face_info (idx); assert (f);
2800     SvREFCNT_dec (f->meta_hv);
2801     f->meta_hv = SvOK (sv) ? (HV *)SvREFCNT_inc (SvRV (sv)) : 0;
2802     }
2803    
2804 root 1.416 SV *mangle_chksum (SV *chksum)
2805     CODE:
2806     STRLEN len;
2807     char *data = SvPVbyte (chksum, len);
2808     RETVAL = newSVpvn (data, min (len, CHKSUM_MAXLEN));
2809     OUTPUT:
2810     RETVAL
2811    
2812 root 1.229 SV *get_chksum (faceidx idx, int faceset = 0)
2813     CODE:
2814 root 1.267 facedata *d = face_data (idx, faceset);
2815     if (!d) XSRETURN_UNDEF;
2816 root 1.338 RETVAL = newSVpvn ((char *)d->chksum, d->chksum_len);
2817 root 1.229 OUTPUT:
2818     RETVAL
2819    
2820 root 1.414 int get_type (faceidx idx)
2821     CODE:
2822     faceinfo *f = face_info (idx);
2823     if (!f) XSRETURN_UNDEF;
2824     RETVAL = f->type;
2825     OUTPUT:
2826     RETVAL
2827    
2828 root 1.415 SV *get_name (faceidx idx)
2829     CODE:
2830     faceinfo *f = face_info (idx);
2831     if (!f) XSRETURN_UNDEF;
2832     RETVAL = to_sv (f->name);
2833     OUTPUT:
2834     RETVAL
2835    
2836 root 1.267 SV *get_data (faceidx idx, int faceset = 0)
2837     CODE:
2838     facedata *d = face_data (idx, faceset);
2839     if (!d) XSRETURN_UNDEF;
2840 root 1.408 RETVAL = newSVpvn (d->data, d->data.size ());
2841 root 1.267 OUTPUT:
2842     RETVAL
2843    
2844 root 1.409 SV *get_meta (faceidx idx)
2845     CODE:
2846     faceinfo *f = face_info (idx); assert (f);
2847     if (!f || !f->meta_hv) XSRETURN_UNDEF;
2848     RETVAL = newRV_inc ((SV *)f->meta_hv);
2849     OUTPUT:
2850     RETVAL
2851    
2852 root 1.177 void invalidate (faceidx idx)
2853     CODE:
2854     for_all_clients (ns)
2855 root 1.408 ns->invalidate_face (idx);
2856 root 1.177
2857     void invalidate_all ()
2858     CODE:
2859     for_all_clients (ns)
2860 root 1.408 ns->invalidate_all_faces ();
2861 root 1.177
2862 root 1.185 MODULE = cf PACKAGE = cf::anim PREFIX = anim_
2863    
2864 root 1.358 #INCLUDE: $PERL $srcdir/genacc faceset $srcdir/../include/anim.h |
2865 root 1.185
2866     animidx anim_find (utf8_string name)
2867     CODE:
2868     RETVAL = animation::find (name).number;
2869     OUTPUT: RETVAL
2870    
2871     animidx set (utf8_string name, SV *frames, int facings = 1)
2872     CODE:
2873     {
2874 root 1.412 if (!SvROK (frames) && SvTYPE (SvRV (frames)) != SVt_PVAV)
2875 root 1.185 croak ("frames must be an arrayref");
2876    
2877     AV *av = (AV *)SvRV (frames);
2878    
2879 root 1.412 animation *anim = &animation::find (name);
2880 root 1.185 if (anim->number)
2881     {
2882     anim->resize (av_len (av) + 1);
2883     anim->facings = facings;
2884     }
2885     else
2886     anim = &animation::create (name, av_len (av) + 1, facings);
2887    
2888     for (int i = 0; i < anim->num_animations; ++i)
2889 root 1.345 anim->faces [i] = face_find (cfSvPVutf8_nolen (*av_fetch (av, i, 1)));
2890 root 1.185 }
2891     OUTPUT: RETVAL
2892    
2893     void invalidate_all ()
2894     CODE:
2895     for_all_clients (ns)
2896     ns->anims_sent.reset ();
2897    
2898 root 1.247 MODULE = cf PACKAGE = cf::object::freezer
2899    
2900 root 1.358 INCLUDE: $PERL $srcdir/genacc object_freezer $srcdir/../include/cfperl.h |
2901 root 1.247
2902     SV *
2903     new (char *klass)
2904     CODE:
2905     RETVAL = newSVptr (new object_freezer, gv_stashpv ("cf::object::freezer", 1));
2906     OUTPUT: RETVAL
2907    
2908     void
2909     DESTROY (SV *sv)
2910     CODE:
2911     object_freezer *self;
2912     sv_to (sv, self);
2913     delete self;
2914    
2915     MODULE = cf PACKAGE = cf::object::thawer
2916    
2917 root 1.372 INCLUDE: $PERL $srcdir/genacc object_thawer $srcdir/../include/freezethaw.h |
2918 root 1.247
2919 root 1.374 bool
2920     errors_are_fatal (bool fatal)
2921     CODE:
2922     RETVAL = object_thawer::errors_are_fatal;
2923     object_thawer::errors_are_fatal = fatal;
2924     OUTPUT:
2925     RETVAL
2926    
2927 root 1.247 SV *
2928     new_from_file (char *klass, octet_string path)
2929     CODE:
2930     object_thawer *f = new object_thawer (path);
2931     if (!*f)
2932     {
2933     delete f;
2934     XSRETURN_UNDEF;
2935     }
2936     RETVAL = newSVptr (f, gv_stashpv ("cf::object::thawer", 1));
2937     OUTPUT: RETVAL
2938    
2939     void
2940     DESTROY (SV *sv)
2941     CODE:
2942     object_thawer *self;
2943     sv_to (sv, self);
2944     delete self;
2945    
2946 root 1.252 void
2947 root 1.253 extract_tags (object_thawer *self)
2948 root 1.254 PPCODE:
2949 root 1.252 while (self->kw != KW_EOF)
2950     {
2951 root 1.254 PUTBACK;
2952 root 1.272 coroapi::cede_to_tick ();
2953 root 1.254 SPAGAIN;
2954 root 1.253
2955 root 1.252 if (self->kw == KW_tag)
2956 root 1.254 XPUSHs (sv_2mortal (newSVpv_utf8 (self->get_str ())));
2957 root 1.252
2958     self->skip ();
2959     }
2960    
2961 root 1.389 MODULE = cf PACKAGE = cf::layout
2962    
2963 root 1.392 INCLUDE: $PERL $srcdir/genacc layout $srcdir/../include/rmg.h |
2964 root 1.389