ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/cfperl.xs
Revision: 1.41
Committed: Sun Sep 3 22:45:57 2006 UTC (17 years, 9 months ago) by root
Branch: MAIN
Changes since 1.40: +160 -43 lines
Log Message:
string scanning (e.g. for patch) is not implemented ATM but should be easy
to add with an alternative constructor for object_thawer.

Rewrote flex scanner to be simpler, faster and more modularised.

Initial speedup: 16%

(ah well)

File Contents

# User Rev Content
1 root 1.1 /*****************************************************************************/
2     /* CrossFire, A Multiplayer game for the X Window System */
3     /*****************************************************************************/
4    
5     /*
6     * This code is placed under the GNU General Public Licence (GPL)
7     *
8     * Copyright (C) 2001-2005 by Chachkoff Yann
9     * Copyright (C) 2006 by Marc Lehmann <cf@schmorpd.e>
10     *
11     * This program is free software; you can redistribute it and/or modify
12     * it under the terms of the GNU General Public License as published by
13     * the Free Software Foundation; either version 2 of the License, or
14     * (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU General Public License
22     * along with this program; if not, write to the Free Software
23     * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24     */
25    
26     #define PLUGIN_NAME "perl"
27 root 1.13 #define PLUGIN_VERSION "cfperl 0.5"
28 root 1.1
29     #include <plugin_common.h>
30     #include <sounds.h>
31 root 1.6 #include <cstdarg>
32     #include <sproto.h>
33 root 1.1
34 root 1.6 #include "cfperl.h"
35 root 1.12 #include "shstr.h"
36 root 1.1
37 root 1.32 #include <EXTERN.h>
38     #include <perl.h>
39     #include <XSUB.h>
40    
41 root 1.1 #include "perlxsi.c"
42    
43     extern sint64 *levels; // the experience table
44    
45     typedef object object_ornull;
46     typedef mapstruct mapstruct_ornull;
47    
48     typedef double val64;
49     #define newSVval64 newSVnv
50     #define SvVAL64 SvNV
51    
52 root 1.19 static f_plug_api gethook = cfapi_get_hooks;
53     static f_plug_api object_set_property = cfapi_object_set_property;
54     static f_plug_api object_insert = cfapi_object_insert;
55 root 1.1
56     /* this is a stupid way to do things, and awkward to use for plug-in authors */
57     typedef struct
58     {
59     object* who;
60     object* activator;
61     object* third;
62     object* event;
63     mapstruct* map;
64     char message[1024];
65     int fix; // seems to be python-only, and should not be part of the API
66     int event_code;
67     char extension[1024]; // name field, should invoke specific perl extension
68     char options[1024]; // slaying field of event_connectors
69     int returnvalue;
70     } CFPContext;
71    
72     static HV *obj_cache;
73     static PerlInterpreter *perl;
74    
75 root 1.8 static AV *cb_global, *cb_object, *cb_player, *cb_type, *cb_map;
76    
77 root 1.1 #define PUSHcfapi(type,value) PUSHs (sv_2mortal (newSVcfapi (CFAPI_ ## type, (value))))
78     #define PUSHcfapi_va(type,ctype) PUSHcfapi (type, va_arg (args, ctype))
79     #define PUSH_OB PUSHcfapi_va(POBJECT, object *)
80     #define PUSH_PL PUSHcfapi_va(PPLAYER, player *)
81     #define PUSH_MAP PUSHcfapi_va(PMAP, mapstruct *)
82     #define PUSH_PV PUSHcfapi_va(STRING, const char *)
83     #define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int))))
84    
85     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
86    
87     static SV *
88 root 1.13 newSVptr (void *ptr, const char *klass, HV *hv = newHV ())
89 root 1.1 {
90     SV *sv;
91    
92     if (!ptr)
93     return &PL_sv_undef;
94    
95 root 1.13 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
96     return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
97 root 1.1 }
98    
99 root 1.17 template<class attachable>
100 root 1.11 SV *
101 root 1.17 newSVattachable (attachable *obj, const char *klass)
102 root 1.11 {
103     if (!obj)
104     return &PL_sv_undef;
105    
106     if (!obj->self)
107     obj->self = newSVptr (obj, klass);
108    
109 root 1.32 return newSVsv (obj->self);
110 root 1.11 }
111    
112 root 1.1 static void
113     SVptr_cache_set (void *ptr, SV *sv)
114     {
115     hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0);
116     }
117    
118     static SV *
119     SVptr_cache_get (void *ptr)
120     {
121     SV **he = hv_fetch (obj_cache, (char *)&ptr, sizeof (ptr), 0);
122    
123     return he ? *he : 0;
124     }
125    
126     static SV *
127     newSVptr_cached (void *ptr, const char *klass)
128     {
129     SV *sv;
130    
131     if (!ptr)
132     return &PL_sv_undef;
133    
134     sv = SVptr_cache_get (ptr);
135    
136     if (!sv)
137     {
138     HV *hv = newHV ();
139     sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
140     sv = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
141    
142     SVptr_cache_set (ptr, sv);
143     }
144    
145     return newSVsv (sv);
146     }
147    
148     static void
149     clearSVptr (SV *sv)
150     {
151     if (SvROK (sv))
152     sv = SvRV (sv);
153    
154     hv_clear ((HV *)sv);
155     sv_unmagic (sv, PERL_MAGIC_ext);
156     }
157    
158     static long
159     SvPTR (SV *sv, const char *klass)
160     {
161     if (!sv_derived_from (sv, klass))
162     croak ("object of type %s expected", klass);
163    
164     MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
165    
166     if (!mg)
167     croak ("perl code used %s object, but C object is already destroyed, caught", klass);
168    
169     return (long)mg->mg_ptr;
170     }
171    
172     static long
173     SvPTR_ornull (SV *sv, const char *klass)
174     {
175     if (SvOK (sv))
176     return SvPTR (sv, klass);
177     else
178     return 0;
179     }
180    
181     static SV *
182 root 1.6 newSVdt_va (va_list &ap, data_type type)
183 root 1.1 {
184     SV *sv;
185    
186     switch (type)
187     {
188 root 1.10 case DT_INT:
189     sv = newSViv (va_arg (ap, int));
190     break;
191    
192     case DT_INT64:
193     sv = newSVval64 ((val64)va_arg (ap, sint64));
194     break;
195    
196 root 1.6 case DT_DOUBLE:
197 root 1.10 sv = newSVnv (va_arg (ap, double));
198 root 1.1 break;
199    
200 root 1.6 case DT_STRING:
201     {
202 root 1.10 char *str = (char *)va_arg (ap, const char *);
203 root 1.6 sv = str ? newSVpv (str, 0) : &PL_sv_undef;
204     }
205 root 1.1 break;
206    
207 root 1.6 case DT_DATA:
208 root 1.1 {
209 root 1.10 char *str = (char *)va_arg (ap, const void *);
210 root 1.6 int len = va_arg (ap, int);
211     sv = str ? newSVpv (str, len) : &PL_sv_undef;
212 root 1.1 }
213     break;
214    
215 root 1.6 case DT_OBJECT:
216 root 1.1 {
217 root 1.6 object *obj = va_arg (ap, object *);
218 root 1.17 sv = newSVattachable (obj, obj && obj->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap");
219 root 1.1 }
220     break;
221    
222 root 1.6 case DT_MAP:
223 root 1.11 // va_arg (object *) when void * is passed is an XSI extension
224 root 1.17 sv = newSVattachable (va_arg (ap, mapstruct *), "cf::map::wrap");
225 root 1.1 break;
226    
227 root 1.6 case DT_PLAYER:
228 root 1.17 sv = newSVattachable (va_arg (ap, player *), "cf::player::wrap");
229 root 1.1 break;
230    
231 root 1.6 case DT_ARCH:
232     sv = newSVptr (va_arg (ap, archetype *), "cf::arch::wrap");
233 root 1.1 break;
234    
235 root 1.6 case DT_PARTY:
236     sv = newSVptr (va_arg (ap, partylist *), "cf::party::wrap");
237 root 1.1 break;
238    
239 root 1.6 case DT_REGION:
240     sv = newSVptr (va_arg (ap, region *), "cf::region::wrap");
241 root 1.1 break;
242    
243     default:
244 root 1.6 assert (("unhandled type in newSVdt_va", 0));
245     }
246    
247     return sv;
248     }
249    
250     static SV *
251     newSVdt (data_type type, ...)
252     {
253     va_list ap;
254    
255     va_start (ap, type);
256     SV *sv = newSVdt_va (ap, type);
257     va_end (ap);
258    
259     return sv;
260     }
261    
262     static SV *
263     newSVcfapi (int type, ...)
264     {
265     SV *sv;
266    
267     va_list ap;
268     va_start (ap, type);
269    
270     switch (type)
271     {
272 root 1.12 case CFAPI_INT: sv = newSViv (*va_arg (ap, int * )); break;
273     case CFAPI_LONG: sv = newSVval64 (*va_arg (ap, sint64 *)); break;
274     case CFAPI_DOUBLE: sv = newSVnv (*va_arg (ap, double *)); break;
275 root 1.6 case CFAPI_STRING: sv = newSVdt_va (ap, DT_STRING); break;
276     case CFAPI_POBJECT: sv = newSVdt_va (ap, DT_OBJECT); break;
277     case CFAPI_PMAP: sv = newSVdt_va (ap, DT_MAP ); break;
278     case CFAPI_PPLAYER: sv = newSVdt_va (ap, DT_PLAYER); break;
279     case CFAPI_PARCH: sv = newSVdt_va (ap, DT_ARCH ); break;
280     case CFAPI_PPARTY: sv = newSVdt_va (ap, DT_PARTY ); break;
281     case CFAPI_PREGION: sv = newSVdt_va (ap, DT_REGION); break;
282    
283     default:
284 root 1.1 assert (("unhandled type in newSVcfapi", 0));
285     }
286    
287 root 1.6 va_end (ap);
288 root 1.1
289     return sv;
290     }
291    
292 root 1.11 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
293    
294 root 1.12 SV *
295 root 1.17 registry_of (attachable_base *ext)
296 root 1.12 {
297 root 1.13 if (!ext->cb)
298     ext->cb = newAV ();
299 root 1.12
300 root 1.13 return newRV_inc ((SV *)ext->cb);
301 root 1.12 }
302    
303 root 1.17 void attachable_base::clear ()
304 root 1.11 {
305 root 1.21 if (self)
306     {
307 root 1.22 if (cb)
308 root 1.32 if (SvROK (*av_fetch (cb, EVENT_OBJECT_DESTROY, 1)))
309 root 1.22 INVOKE_OBJECT (DESTROY, static_cast<object *>(this));
310 root 1.32 else if (SvROK (*av_fetch (cb, EVENT_MAP_DESTROY, 1)))
311 root 1.22 INVOKE_MAP (DESTROY, static_cast<mapstruct *>(this));
312    
313     // disconnect Perl from C, to avoid crashes
314     sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext);
315    
316     // clear the perl hash, might or might not be a good idea
317 root 1.21 hv_clear ((HV *)SvRV ((SV *)self));
318 root 1.22
319 root 1.21 SvREFCNT_dec (self);
320 root 1.22 self = 0;
321 root 1.21 }
322 root 1.12
323 root 1.21 if (cb)
324     {
325     SvREFCNT_dec (cb);
326     cb = 0;
327     }
328 root 1.12
329 root 1.40 attach = 0;
330 root 1.11 }
331    
332 root 1.17 void attachable_base::optimise ()
333 root 1.11 {
334 root 1.13 if (!self)
335     return;
336 root 1.11
337 root 1.13 HV *hv = (HV *)SvRV ((SV *)self);
338    
339     if (SvREFCNT ((SV *)self) == 1
340     && SvREFCNT ((SV *)hv) == 1
341     && !HvKEYS (hv))
342     {
343     SvREFCNT_dec ((SV *)self);
344     self = 0;
345     }
346     }
347    
348 root 1.23 void attachable_base::instantiate (data_type type, void *obj)
349 root 1.13 {
350     dSP;
351     ENTER;
352     SAVETMPS;
353     PUSHMARK (SP);
354     EXTEND (SP, 2);
355 root 1.23 PUSHs (sv_2mortal (newSVdt (type, obj)));
356 root 1.13 PUSHs (sv_2mortal (newSVpv (attach, 0)));
357 root 1.23
358     attach = 0;
359    
360 root 1.13 PUTBACK;
361     call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL);
362     FREETMPS;
363     LEAVE;
364 root 1.16
365 root 1.23 switch (type)
366     {
367     case DT_OBJECT: INVOKE_OBJECT (INSTANTIATE, obj); break;
368     case DT_MAP: INVOKE_MAP (INSTANTIATE, obj); break;
369     }
370 root 1.13 }
371    
372     /////////////////////////////////////////////////////////////////////////////
373    
374 root 1.18 void reattach (data_type type, void *obj)
375     {
376 root 1.22 //TODO only do this when the object has _attachment's
377    
378 root 1.18 dSP;
379     ENTER;
380     SAVETMPS;
381     PUSHMARK (SP);
382     XPUSHs (sv_2mortal (newSVdt (type, obj)));
383     PUTBACK;
384     call_pv ("cf::reattach", G_DISCARD | G_VOID | G_EVAL);
385     FREETMPS;
386     LEAVE;
387    
388     switch (type)
389     {
390     case DT_OBJECT: INVOKE_OBJECT (REATTACH, obj); break;
391     case DT_PLAYER: INVOKE_PLAYER (REATTACH, obj); break;
392     case DT_MAP: INVOKE_MAP (REATTACH, obj); break;
393     }
394     }
395    
396     template<class subclass>
397     void reattach (attachable<subclass> *obj)
398     {
399     obj->optimise ();
400    
401     if (obj->self)
402 root 1.35 reattach ((data_type) cftype<subclass>::dt, (subclass *)obj);
403 root 1.18 }
404    
405 root 1.35 #include "kw_hash.h"
406    
407 root 1.34 object_freezer::object_freezer ()
408 root 1.37 : dynbuf (128 * 1024, 64 * 1024)
409 root 1.13 {
410 root 1.32 av = newAV ();
411 root 1.11 }
412    
413 root 1.13 object_freezer::~object_freezer ()
414 root 1.11 {
415 root 1.34 SvREFCNT_dec (av);
416 root 1.13 }
417    
418 root 1.20 void object_freezer::put (attachable_base *ext)
419 root 1.13 {
420     ext->optimise ();
421 root 1.12
422 root 1.13 if (ext->self)
423 root 1.18 {
424     int idx = AvFILLp ((AV *)av) + 1;
425 root 1.32 av_store (av, idx, SvREFCNT_inc (ext->self));
426    
427 root 1.37 add ((void *)"oid ", 4);
428     add ((sint32)idx);
429     add ('\n');
430 root 1.18 }
431 root 1.11 }
432    
433 root 1.34 bool object_freezer::save (const char *filename)
434     {
435     dSP;
436     ENTER;
437     SAVETMPS;
438     PUSHMARK (SP);
439     EXTEND (SP, 3);
440     PUSHs (sv_2mortal (newSVpv (filename, 0)));
441 root 1.37 PUSHs (sv_2mortal (newRV_noinc (newSVpvn ((char *)linearise (), size ()))));
442 root 1.34 PUSHs (sv_2mortal (newRV_inc ((SV *)av)));
443     PUTBACK;
444     call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL);
445     FREETMPS;
446     LEAVE;
447     }
448    
449 root 1.32 int fprintf (object_freezer &freezer, const char *format, ...)
450     {
451     va_list ap;
452    
453     va_start (ap, format);
454 root 1.37
455     int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap);
456    
457     if (len >= 0)
458     freezer.alloc (len);
459    
460 root 1.32 va_end (ap);
461     }
462    
463     int fputs (const char *s, object_freezer &freezer)
464     {
465 root 1.37 freezer.add (s);
466 root 1.32 }
467    
468 root 1.28 object_thawer::object_thawer (const char *filename)
469 root 1.11 {
470 root 1.41 static const char eof[] = "\n\n\n\0\0\0";
471 root 1.13
472 root 1.41 av = 0;
473     text = 0;
474 root 1.13
475 root 1.41 if (filename)
476 root 1.13 {
477 root 1.28 dSP;
478     ENTER;
479     SAVETMPS;
480     PUSHMARK (SP);
481     XPUSHs (sv_2mortal (newSVpv (filename, 0)));
482     PUTBACK;
483    
484 root 1.41 if (2 == call_pv ("cf::object_thawer_load", G_ARRAY | G_EVAL))
485 root 1.28 {
486     SPAGAIN;
487 root 1.41
488     // second value - perl objects
489     {
490     SV *sv = POPs;
491     if (SvROK (sv))
492     av = (AV *)SvREFCNT_inc (SvRV (sv));
493     }
494    
495     // first value - text part, pad with 3 zeroes
496     {
497     SV *sv = POPs;
498     STRLEN len;
499     char *sv_ = SvPVbyte (sv, len);
500     text = newSV (len + sizeof (eof));
501     SvCUR_set (text, len);
502     memcpy (SvPVX (text), sv_, len);
503     memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure
504     }
505 root 1.28 }
506    
507 root 1.41 PUTBACK;
508 root 1.28 FREETMPS;
509     LEAVE;
510 root 1.13 }
511 root 1.41
512     if (!text)
513     text = newSVpvn (eof, sizeof (eof));
514    
515     line = SvPVX (text);
516 root 1.11 }
517    
518 root 1.18 void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid)
519 root 1.13 {
520 root 1.18 if (!av || oid < 0) // this is actually an error of sorts
521 root 1.13 return;
522    
523     // we have to "re-instantiate"/reattach to an object, so nuke ext->attach
524     ext->clear ();
525    
526 root 1.18 SV **svp = av_fetch ((AV *)av, oid, 0);
527 root 1.13
528 root 1.18 if (!svp || !SvROK (*svp))
529     {
530     printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
531     return;
532     }
533 root 1.13
534 root 1.18 ext->self = *svp; *svp = &PL_sv_undef;
535 root 1.32 sv_magic (SvRV (ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0);
536 root 1.13
537 root 1.18 reattach (type, obj);
538 root 1.13 }
539    
540     object_thawer::~object_thawer ()
541     {
542 root 1.41 if (text) SvREFCNT_dec (text);
543     if (av) SvREFCNT_dec (av);
544     }
545    
546     char *fgets (char *s, int n, object_thawer &thawer)
547     {
548     char *p = thawer.line;
549     char *q = s;
550    
551     while (--n)
552     {
553     if (!*p)
554     break;
555    
556     *q++ = *p;
557    
558     if (*p++ == '\n')
559     break;
560     }
561    
562     *q = 0;
563     thawer.line = p;
564    
565     return s == q ? 0 : s;
566 root 1.13 }
567    
568 root 1.41 keyword object_thawer::get_kv ()
569 root 1.34 {
570     for (;;)
571     {
572 root 1.41 char *p = line;
573 root 1.34
574 root 1.41 if (!*p)
575     return KW_EOF;
576 root 1.34
577 root 1.41 // parse keyword
578 root 1.34 while (*p > ' ')
579     p++;
580    
581 root 1.41 int klen = p - line;
582 root 1.34
583 root 1.41 if (*p++ != '\n')
584     {
585     // parse value
586     while (*p <= ' ' && *p != '\n') // skip 0x01 .. 0x20
587     ++p;
588 root 1.34
589 root 1.41 last_value = p;
590 root 1.34
591 root 1.41 while (*p != '\n')
592 root 1.34 p++;
593    
594 root 1.41 *p++ = 0;
595     }
596     else
597     last_value = 0;
598    
599     line [klen] = 0;
600     keyword_idx *kw = kw_lex::match (line, klen);
601 root 1.34
602 root 1.41 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D
603    
604     if (kw)
605     {
606     line = p;
607     return kw->index;
608     }
609     else if (!*line || *line == '#')
610     {
611     // empty/comment line
612     line = p;
613 root 1.34 }
614     else
615 root 1.41 return KW_ERROR;
616 root 1.34 }
617     }
618    
619 root 1.41 void object_thawer::get (shstr &sh) const
620     {
621     if (last_value)
622     sh = last_value;
623     else
624     {
625     sh = "<value missing>";
626     LOG (llevError, "keyword requires value: <%s>\n", line);//TODO: add filename
627     }
628     }
629    
630     void object_thawer::get_ml (keyword kend, shstr &sh)
631     {
632     char kw[128];
633    
634     // multi-line strings are delimited by "\nendXXX\n"
635     kw [0] = '\n';
636     strcpy (kw + 1, keyword_str [kend]);
637    
638     char *end = strstr (line, kw);
639    
640     if (!end)
641     {
642     sh = 0;
643     return;
644     }
645    
646     *end = 0;
647     sh = line;
648    
649     line = end + keyword_len [kend] + 1;
650    
651     while (*line++ != '\n')
652     ;
653     }
654    
655     sint32 object_thawer::get_sint32 () const
656     {
657     char *p = last_value;
658    
659     if (!p)
660     return 0;
661    
662     sint32 val = 0;
663     bool negate;
664    
665     if (*p == '-')
666     {
667     negate = true;
668     ++p;
669     }
670     else
671     negate = false;
672    
673     do
674     {
675     val *= 10;
676     val += *p++ - '0';
677     }
678     while (*p);
679    
680     return negate ? -val : val;
681     }
682    
683     sint64 object_thawer::get_sint64 () const
684     {
685     return last_value ? atoll (last_value) : 0;
686     }
687    
688     double object_thawer::get_double () const
689     {
690     return last_value ? atof (last_value) : 0;
691     }
692    
693 root 1.1 /////////////////////////////////////////////////////////////////////////////
694    
695     extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr)
696     {
697     return 0;
698     }
699    
700     static CommArray_s rtn_cmd;
701    
702     static int
703     runPluginCommand (object *obj, char *params)
704     {
705     dSP;
706    
707     ENTER;
708     SAVETMPS;
709    
710     PUSHMARK (SP);
711    
712     EXTEND (SP, 3);
713     PUSHs (sv_2mortal (newSVpv (rtn_cmd.name, 0)));
714     PUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, obj)));
715    
716     if (params)
717     PUSHs (sv_2mortal (newSVpv (params, 0)));
718    
719     PUTBACK;
720     int count = call_pv ("cf::inject_command", G_SCALAR | G_EVAL);
721     SPAGAIN;
722    
723     if (SvTRUE (ERRSV))
724     LOG (llevError, "command '%s' callback evaluation error: %s", rtn_cmd.name, SvPV_nolen (ERRSV));
725    
726     int returnvalue = count > 0 ? POPi : -1;
727    
728     PUTBACK;
729     FREETMPS;
730     LEAVE;
731    
732     return returnvalue;
733     }
734    
735     extern "C" void *cfperl_getPluginProperty (int *type, ...)
736     {
737     va_list args;
738     char *propname;
739     int i;
740     va_start (args, type);
741     propname = va_arg (args, char *);
742     //printf ("Property name: %s\n", propname);
743    
744     if (!strcmp (propname, "command?"))
745     {
746     if (!perl)
747     return NULL;
748    
749     const char *cmdname = va_arg (args, const char *);
750     HV *hv = get_hv ("cf::COMMAND", 1);
751     SV **svp = hv_fetch (hv, cmdname, strlen (cmdname) + 1, 0);
752    
753     va_end (args);
754    
755     if (svp)
756     {
757     // this is totaly broken, should stash it into %COMMAND
758     rtn_cmd.name = cmdname;
759     rtn_cmd.time = SvNV (*svp);
760     rtn_cmd.func = runPluginCommand;
761    
762     return &rtn_cmd;
763     }
764     }
765     else if (!strcmp (propname, "Identification"))
766     {
767     va_end (args);
768     return (void*) PLUGIN_NAME;
769     }
770     else if (!strcmp (propname, "FullName"))
771     {
772     va_end (args);
773     return (void*) PLUGIN_VERSION;
774     }
775     else
776     va_end (args);
777    
778     return NULL;
779     }
780    
781     extern "C" int cfperl_postInitPlugin ()
782     {
783     int hooktype = 1;
784     int rtype = 0;
785    
786     cf_init_plugin (gethook);
787    
788     return 0;
789     }
790    
791     extern "C" int cfperl_closePlugin ()
792     {
793     return 0;
794     }
795    
796 root 1.7 void
797     cfperl_init ()
798     {
799 root 1.32 PERL_SYS_INIT3 (&settings.argc, &settings.argv, 0);
800     perl = perl_alloc ();
801     perl_construct (perl);
802    
803     PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
804    
805 root 1.7 char *argv[] = {
806     "",
807     "-e"
808 root 1.32 "cf->bootstrap;"
809     "unshift @INC, cf::datadir ();"
810 root 1.41 "require cf;"
811 root 1.7 };
812    
813     if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl))
814     {
815     printf ("unable to initialize perl-interpreter, aborting.\n");
816     exit (EXIT_FAILURE);
817     }
818 root 1.8
819     obj_cache = newHV ();
820 root 1.7 }
821    
822 root 1.5 void cfperl_main ()
823 root 1.2 {
824     dSP;
825    
826     PUSHMARK (SP);
827     PUTBACK;
828 root 1.7 call_pv ("cf::main", G_DISCARD | G_VOID);
829     }
830    
831 root 1.8 static event_klass klass_of[NUM_EVENT_TYPES] = {
832     # define def(type,name) KLASS_ ## type,
833     # include "eventinc.h"
834     # undef def
835     };
836    
837 root 1.18 #define KLASS_OF(event) (((unsigned int)event) < NUM_EVENT_TYPES ? klass_of [event] : KLASS_NONE)
838    
839 root 1.8 static void
840     gather_callbacks (AV *&callbacks, AV *registry, event_type event)
841     {
842     // event must be in array
843     if (event >= 0 && event <= AvFILLp (registry))
844     {
845     SV *cbs_ = AvARRAY (registry)[event];
846    
847     // element must be list of callback entries
848     if (cbs_ && SvROK (cbs_) && SvTYPE (SvRV (cbs_)) == SVt_PVAV)
849     {
850     AV *cbs = (AV *)SvRV (cbs_);
851    
852     // no callback entries, no callbacks to call
853     if (AvFILLp (cbs) >= 0)
854     {
855     if (!callbacks)
856     {
857     callbacks = newAV ();
858     av_extend (callbacks, 16);
859     }
860    
861     // never use SvREFCNT_inc to copy values, but its ok here :)
862     for (int i = 0; i <= AvFILLp (cbs); ++i)
863     av_push (callbacks, SvREFCNT_inc (AvARRAY (cbs)[i]));
864     }
865     }
866     }
867     }
868    
869     bool cfperl_invoke (event_type event, ...)
870 root 1.6 {
871 root 1.8 data_type dt;
872 root 1.6 va_list ap;
873    
874 root 1.8 va_start (ap, event);
875    
876     AV *callbacks = 0;
877    
878     object *op;
879     player *pl;
880     mapstruct *map;
881    
882     // callback call ordering is:
883     // 1. per-object callback (NYI)
884     // 2. per-class object
885     // 2a. per-type callback
886     // 4. global callbacks
887    
888     gather_callbacks (callbacks, cb_global, event);
889    
890 root 1.18 switch (KLASS_OF (event))
891 root 1.8 {
892     case KLASS_GLOBAL:
893     break;
894    
895     case KLASS_OBJECT:
896     dt = (data_type) va_arg (ap, int);
897     assert (("first argument must be of type object", dt == DT_OBJECT));
898 root 1.12 op = va_arg (ap, object *);
899 root 1.8
900 root 1.12 if (op->cb)
901 root 1.32 gather_callbacks (callbacks, op->cb, event);
902 root 1.8
903 root 1.14 if (op->type)
904     {
905     if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type))
906     {
907     SV *registry = AvARRAY (cb_type)[op->type + op->subtype * NUM_SUBTYPES];
908    
909     if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
910     gather_callbacks (callbacks, (AV *)SvRV (registry), event);
911     }
912 root 1.8
913 root 1.14 if (op->type <= AvFILLp (cb_type))
914     {
915     SV *registry = AvARRAY (cb_type)[op->type];
916 root 1.8
917 root 1.14 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
918     gather_callbacks (callbacks, (AV *)SvRV (registry), event);
919     }
920 root 1.8 }
921    
922 root 1.14 gather_callbacks (callbacks, cb_object, event);
923    
924 root 1.8 break;
925    
926     case KLASS_PLAYER:
927     dt = (data_type) va_arg (ap, int);
928     assert (("first argument must be of type player", dt == DT_PLAYER));
929 root 1.12 pl = va_arg (ap, player *);
930    
931 root 1.13 if (pl->cb)
932 root 1.32 gather_callbacks (callbacks, pl->cb, event);
933 root 1.8
934     gather_callbacks (callbacks, cb_player, event);
935     break;
936    
937     case KLASS_MAP:
938     dt = (data_type) va_arg (ap, int);
939     assert (("first argument must be of type object", dt == DT_MAP));
940 root 1.12 map = va_arg (ap, mapstruct *);
941    
942 root 1.13 if (map->cb)
943 root 1.32 gather_callbacks (callbacks, map->cb, event);
944 root 1.8
945     gather_callbacks (callbacks, cb_map, event);
946     break;
947    
948     default:
949     assert (("unsupported event klass in cfperl_invoke", 0));
950     }
951    
952     // short-circuit processing if no callbacks found/defined
953     if (!callbacks)
954     return 0;
955    
956     dSP;
957 root 1.6 ENTER;
958     SAVETMPS;
959    
960     PUSHMARK (SP);
961 root 1.8 EXTEND (SP, 3);
962 root 1.6
963 root 1.8 PUSHs (sv_2mortal (newSViv (event))); // only used for debugging nowadays
964     PUSHs (sv_2mortal (newRV_noinc ((SV *)callbacks)));
965    
966 root 1.18 switch (KLASS_OF (event))
967 root 1.8 {
968     case KLASS_OBJECT: PUSHs (sv_2mortal (newSVdt (DT_OBJECT, op))); break;
969     case KLASS_PLAYER: PUSHs (sv_2mortal (newSVdt (DT_PLAYER, pl))); break;
970     case KLASS_MAP: PUSHs (sv_2mortal (newSVdt (DT_MAP, map))); break;
971     }
972 root 1.7
973 root 1.6 for (;;)
974     {
975 root 1.8 dt = (data_type) va_arg (ap, int);
976 root 1.6
977     if (dt == DT_END)
978     break;
979    
980 root 1.12 if (dt == DT_AV)
981     {
982     AV *av = va_arg (ap, AV *);
983    
984     for (int i = 0; i <= av_len (av); ++i)
985     XPUSHs (*av_fetch (av, i, 1));
986     }
987     else
988     XPUSHs (sv_2mortal (newSVdt_va (ap, dt)));
989 root 1.6 }
990    
991     va_end (ap);
992    
993     PUTBACK;
994 root 1.12 int count = call_pv ("cf::do_invoke", G_SCALAR | G_EVAL);
995 root 1.2 SPAGAIN;
996 root 1.6
997     count = count > 0 ? POPi : 0;
998    
999     FREETMPS;
1000     LEAVE;
1001    
1002     return count;
1003 root 1.2 }
1004    
1005 root 1.12 SV *
1006     cfperl_result (int idx)
1007     {
1008     AV *av = get_av ("cfperl::invoke_results", 0);
1009     if (!av)
1010     return &PL_sv_undef;
1011    
1012     SV **sv = av_fetch (av, idx, 0);
1013     if (!sv)
1014     return &PL_sv_undef;
1015    
1016     return *sv;
1017     }
1018    
1019     int
1020     cfperl_result_INT (int idx)
1021     {
1022     return SvIV (cfperl_result (idx));
1023     }
1024    
1025 root 1.1 MODULE = cf PACKAGE = cf PREFIX = cf_
1026    
1027     BOOT:
1028     {
1029     HV *stash = gv_stashpv ("cf", 1);
1030    
1031     static const struct {
1032     const char *name;
1033     IV iv;
1034     } *civ, const_iv[] = {
1035     # define const_iv(name) { # name, (IV)name },
1036     const_iv (llevError)
1037     const_iv (llevInfo)
1038     const_iv (llevDebug)
1039     const_iv (llevMonster)
1040    
1041 root 1.5 const_iv (MAX_TIME)
1042 root 1.1 const_iv (PLAYER)
1043     const_iv (TRANSPORT)
1044     const_iv (ROD)
1045     const_iv (TREASURE)
1046     const_iv (POTION)
1047     const_iv (FOOD)
1048     const_iv (POISON)
1049     const_iv (BOOK)
1050     const_iv (CLOCK)
1051     const_iv (LIGHTNING)
1052     const_iv (ARROW)
1053     const_iv (BOW)
1054     const_iv (WEAPON)
1055     const_iv (ARMOUR)
1056     const_iv (PEDESTAL)
1057     const_iv (ALTAR)
1058     const_iv (LOCKED_DOOR)
1059     const_iv (SPECIAL_KEY)
1060     const_iv (MAP)
1061     const_iv (DOOR)
1062     const_iv (KEY)
1063     const_iv (TIMED_GATE)
1064     const_iv (TRIGGER)
1065     const_iv (GRIMREAPER)
1066     const_iv (MAGIC_EAR)
1067     const_iv (TRIGGER_BUTTON)
1068     const_iv (TRIGGER_ALTAR)
1069     const_iv (TRIGGER_PEDESTAL)
1070     const_iv (SHIELD)
1071     const_iv (HELMET)
1072     const_iv (HORN)
1073     const_iv (MONEY)
1074     const_iv (CLASS)
1075     const_iv (GRAVESTONE)
1076     const_iv (AMULET)
1077     const_iv (PLAYERMOVER)
1078     const_iv (TELEPORTER)
1079     const_iv (CREATOR)
1080     const_iv (SKILL)
1081     const_iv (EXPERIENCE)
1082     const_iv (EARTHWALL)
1083     const_iv (GOLEM)
1084     const_iv (THROWN_OBJ)
1085     const_iv (BLINDNESS)
1086     const_iv (GOD)
1087     const_iv (DETECTOR)
1088     const_iv (TRIGGER_MARKER)
1089     const_iv (DEAD_OBJECT)
1090     const_iv (DRINK)
1091     const_iv (MARKER)
1092     const_iv (HOLY_ALTAR)
1093     const_iv (PLAYER_CHANGER)
1094     const_iv (BATTLEGROUND)
1095     const_iv (PEACEMAKER)
1096     const_iv (GEM)
1097     const_iv (FIREWALL)
1098     const_iv (ANVIL)
1099     const_iv (CHECK_INV)
1100     const_iv (MOOD_FLOOR)
1101     const_iv (EXIT)
1102     const_iv (ENCOUNTER)
1103     const_iv (SHOP_FLOOR)
1104     const_iv (SHOP_MAT)
1105     const_iv (RING)
1106     const_iv (FLOOR)
1107     const_iv (FLESH)
1108     const_iv (INORGANIC)
1109     const_iv (SKILL_TOOL)
1110     const_iv (LIGHTER)
1111     const_iv (TRAP_PART)
1112     const_iv (WALL)
1113     const_iv (LIGHT_SOURCE)
1114     const_iv (MISC_OBJECT)
1115     const_iv (MONSTER)
1116     const_iv (SPAWN_GENERATOR)
1117     const_iv (LAMP)
1118     const_iv (DUPLICATOR)
1119     const_iv (TOOL)
1120     const_iv (SPELLBOOK)
1121     const_iv (BUILDFAC)
1122     const_iv (CLOAK)
1123     const_iv (SPINNER)
1124     const_iv (GATE)
1125     const_iv (BUTTON)
1126     const_iv (CF_HANDLE)
1127     const_iv (HOLE)
1128     const_iv (TRAPDOOR)
1129     const_iv (SIGN)
1130     const_iv (BOOTS)
1131     const_iv (GLOVES)
1132     const_iv (SPELL)
1133     const_iv (SPELL_EFFECT)
1134     const_iv (CONVERTER)
1135     const_iv (BRACERS)
1136     const_iv (POISONING)
1137     const_iv (SAVEBED)
1138     const_iv (POISONCLOUD)
1139     const_iv (FIREHOLES)
1140     const_iv (WAND)
1141     const_iv (SCROLL)
1142     const_iv (DIRECTOR)
1143     const_iv (GIRDLE)
1144     const_iv (FORCE)
1145     const_iv (POTION_EFFECT)
1146     const_iv (EVENT_CONNECTOR)
1147     const_iv (CLOSE_CON)
1148     const_iv (CONTAINER)
1149     const_iv (ARMOUR_IMPROVER)
1150     const_iv (WEAPON_IMPROVER)
1151     const_iv (SKILLSCROLL)
1152     const_iv (DEEP_SWAMP)
1153     const_iv (IDENTIFY_ALTAR)
1154     const_iv (MENU)
1155     const_iv (RUNE)
1156     const_iv (TRAP)
1157     const_iv (POWER_CRYSTAL)
1158     const_iv (CORPSE)
1159     const_iv (DISEASE)
1160     const_iv (SYMPTOM)
1161     const_iv (BUILDER)
1162     const_iv (MATERIAL)
1163     const_iv (ITEM_TRANSFORMER)
1164     const_iv (QUEST)
1165    
1166 root 1.14 const_iv (NUM_SUBTYPES)
1167    
1168 root 1.1 const_iv (ST_BD_BUILD)
1169     const_iv (ST_BD_REMOVE)
1170    
1171     const_iv (ST_MAT_FLOOR)
1172     const_iv (ST_MAT_WALL)
1173     const_iv (ST_MAT_ITEM)
1174    
1175     const_iv (AT_PHYSICAL)
1176     const_iv (AT_MAGIC)
1177     const_iv (AT_FIRE)
1178     const_iv (AT_ELECTRICITY)
1179     const_iv (AT_COLD)
1180     const_iv (AT_CONFUSION)
1181     const_iv (AT_ACID)
1182     const_iv (AT_DRAIN)
1183     const_iv (AT_WEAPONMAGIC)
1184     const_iv (AT_GHOSTHIT)
1185     const_iv (AT_POISON)
1186     const_iv (AT_SLOW)
1187     const_iv (AT_PARALYZE)
1188     const_iv (AT_TURN_UNDEAD)
1189     const_iv (AT_FEAR)
1190     const_iv (AT_CANCELLATION)
1191     const_iv (AT_DEPLETE)
1192     const_iv (AT_DEATH)
1193     const_iv (AT_CHAOS)
1194     const_iv (AT_COUNTERSPELL)
1195     const_iv (AT_GODPOWER)
1196     const_iv (AT_HOLYWORD)
1197     const_iv (AT_BLIND)
1198     const_iv (AT_INTERNAL)
1199     const_iv (AT_LIFE_STEALING)
1200     const_iv (AT_DISEASE)
1201    
1202     const_iv (QUEST_IN_PROGRESS)
1203     const_iv (QUEST_DONE_QUEST)
1204     const_iv (QUEST_DONE_TASK)
1205     const_iv (QUEST_START_QUEST)
1206     const_iv (QUEST_END_QUEST)
1207     const_iv (QUEST_START_TASK)
1208     const_iv (QUEST_END_TASK)
1209     const_iv (QUEST_OVERRIDE)
1210     const_iv (QUEST_ON_ACTIVATE)
1211    
1212     const_iv (WEAP_HIT)
1213     const_iv (WEAP_SLASH)
1214     const_iv (WEAP_PIERCE)
1215     const_iv (WEAP_CLEAVE)
1216     const_iv (WEAP_SLICE)
1217     const_iv (WEAP_STAB)
1218     const_iv (WEAP_WHIP)
1219     const_iv (WEAP_CRUSH)
1220     const_iv (WEAP_BLUD)
1221    
1222     const_iv (FLAG_ALIVE)
1223     const_iv (FLAG_WIZ)
1224     const_iv (FLAG_REMOVED)
1225     const_iv (FLAG_FREED)
1226     const_iv (FLAG_WAS_WIZ)
1227     const_iv (FLAG_APPLIED)
1228     const_iv (FLAG_UNPAID)
1229     const_iv (FLAG_USE_SHIELD)
1230     const_iv (FLAG_NO_PICK)
1231     const_iv (FLAG_ANIMATE)
1232     const_iv (FLAG_MONSTER)
1233     const_iv (FLAG_FRIENDLY)
1234     const_iv (FLAG_GENERATOR)
1235     const_iv (FLAG_IS_THROWN)
1236     const_iv (FLAG_AUTO_APPLY)
1237     const_iv (FLAG_TREASURE)
1238     const_iv (FLAG_PLAYER_SOLD)
1239     const_iv (FLAG_SEE_INVISIBLE)
1240     const_iv (FLAG_CAN_ROLL)
1241     const_iv (FLAG_OVERLAY_FLOOR)
1242     const_iv (FLAG_IS_TURNABLE)
1243     const_iv (FLAG_IS_USED_UP)
1244     const_iv (FLAG_IDENTIFIED)
1245     const_iv (FLAG_REFLECTING)
1246     const_iv (FLAG_CHANGING)
1247     const_iv (FLAG_SPLITTING)
1248     const_iv (FLAG_HITBACK)
1249     const_iv (FLAG_STARTEQUIP)
1250     const_iv (FLAG_BLOCKSVIEW)
1251     const_iv (FLAG_UNDEAD)
1252     const_iv (FLAG_SCARED)
1253     const_iv (FLAG_UNAGGRESSIVE)
1254     const_iv (FLAG_REFL_MISSILE)
1255     const_iv (FLAG_REFL_SPELL)
1256     const_iv (FLAG_NO_MAGIC)
1257     const_iv (FLAG_NO_FIX_PLAYER)
1258     const_iv (FLAG_IS_LIGHTABLE)
1259     const_iv (FLAG_TEAR_DOWN)
1260     const_iv (FLAG_RUN_AWAY)
1261     const_iv (FLAG_PICK_UP)
1262     const_iv (FLAG_UNIQUE)
1263     const_iv (FLAG_NO_DROP)
1264     const_iv (FLAG_WIZCAST)
1265     const_iv (FLAG_CAST_SPELL)
1266     const_iv (FLAG_USE_SCROLL)
1267     const_iv (FLAG_USE_RANGE)
1268     const_iv (FLAG_USE_BOW)
1269     const_iv (FLAG_USE_ARMOUR)
1270     const_iv (FLAG_USE_WEAPON)
1271     const_iv (FLAG_USE_RING)
1272     const_iv (FLAG_READY_RANGE)
1273     const_iv (FLAG_READY_BOW)
1274     const_iv (FLAG_XRAYS)
1275     const_iv (FLAG_NO_APPLY)
1276     const_iv (FLAG_IS_FLOOR)
1277     const_iv (FLAG_LIFESAVE)
1278     const_iv (FLAG_NO_STRENGTH)
1279     const_iv (FLAG_SLEEP)
1280     const_iv (FLAG_STAND_STILL)
1281     const_iv (FLAG_RANDOM_MOVE)
1282     const_iv (FLAG_ONLY_ATTACK)
1283     const_iv (FLAG_CONFUSED)
1284     const_iv (FLAG_STEALTH)
1285     const_iv (FLAG_WIZPASS)
1286     const_iv (FLAG_IS_LINKED)
1287     const_iv (FLAG_CURSED)
1288     const_iv (FLAG_DAMNED)
1289     const_iv (FLAG_SEE_ANYWHERE)
1290     const_iv (FLAG_KNOWN_MAGICAL)
1291     const_iv (FLAG_KNOWN_CURSED)
1292     const_iv (FLAG_CAN_USE_SKILL)
1293     const_iv (FLAG_BEEN_APPLIED)
1294     const_iv (FLAG_READY_SCROLL)
1295     const_iv (FLAG_USE_ROD)
1296     const_iv (FLAG_USE_HORN)
1297     const_iv (FLAG_MAKE_INVIS)
1298     const_iv (FLAG_INV_LOCKED)
1299     const_iv (FLAG_IS_WOODED)
1300     const_iv (FLAG_IS_HILLY)
1301     const_iv (FLAG_READY_SKILL)
1302     const_iv (FLAG_READY_WEAPON)
1303     const_iv (FLAG_NO_SKILL_IDENT)
1304     const_iv (FLAG_BLIND)
1305     const_iv (FLAG_SEE_IN_DARK)
1306     const_iv (FLAG_IS_CAULDRON)
1307     const_iv (FLAG_NO_STEAL)
1308     const_iv (FLAG_ONE_HIT)
1309     const_iv (FLAG_CLIENT_SENT)
1310     const_iv (FLAG_BERSERK)
1311     const_iv (FLAG_NEUTRAL)
1312     const_iv (FLAG_NO_ATTACK)
1313     const_iv (FLAG_NO_DAMAGE)
1314     const_iv (FLAG_OBJ_ORIGINAL)
1315     const_iv (FLAG_OBJ_SAVE_ON_OVL)
1316     const_iv (FLAG_ACTIVATE_ON_PUSH)
1317     const_iv (FLAG_ACTIVATE_ON_RELEASE)
1318     const_iv (FLAG_IS_WATER)
1319     const_iv (FLAG_CONTENT_ON_GEN)
1320     const_iv (FLAG_IS_A_TEMPLATE)
1321     const_iv (FLAG_IS_BUILDABLE)
1322     const_iv (FLAG_AFK)
1323    
1324     const_iv (NDI_BLACK)
1325     const_iv (NDI_WHITE)
1326     const_iv (NDI_NAVY)
1327     const_iv (NDI_RED)
1328     const_iv (NDI_ORANGE)
1329     const_iv (NDI_BLUE)
1330     const_iv (NDI_DK_ORANGE)
1331     const_iv (NDI_GREEN)
1332     const_iv (NDI_LT_GREEN)
1333     const_iv (NDI_GREY)
1334     const_iv (NDI_BROWN)
1335     const_iv (NDI_GOLD)
1336     const_iv (NDI_TAN)
1337     const_iv (NDI_MAX_COLOR)
1338     const_iv (NDI_COLOR_MASK)
1339     const_iv (NDI_UNIQUE)
1340     const_iv (NDI_ALL)
1341    
1342     const_iv (F_APPLIED)
1343     const_iv (F_LOCATION)
1344     const_iv (F_UNPAID)
1345     const_iv (F_MAGIC)
1346     const_iv (F_CURSED)
1347     const_iv (F_DAMNED)
1348     const_iv (F_OPEN)
1349     const_iv (F_NOPICK)
1350     const_iv (F_LOCKED)
1351    
1352     const_iv (F_BUY)
1353     const_iv (F_SHOP)
1354     const_iv (F_SELL)
1355    
1356     const_iv (P_BLOCKSVIEW)
1357     const_iv (P_NO_MAGIC)
1358     const_iv (P_IS_ALIVE)
1359     const_iv (P_NO_CLERIC)
1360     const_iv (P_NEED_UPDATE)
1361     const_iv (P_NO_ERROR)
1362     const_iv (P_OUT_OF_MAP)
1363     const_iv (P_NEW_MAP)
1364    
1365     const_iv (UP_OBJ_INSERT)
1366     const_iv (UP_OBJ_REMOVE)
1367     const_iv (UP_OBJ_CHANGE)
1368     const_iv (UP_OBJ_FACE)
1369    
1370     const_iv (INS_NO_MERGE)
1371     const_iv (INS_ABOVE_FLOOR_ONLY)
1372     const_iv (INS_NO_WALK_ON)
1373     const_iv (INS_ON_TOP)
1374     const_iv (INS_BELOW_ORIGINATOR)
1375     const_iv (INS_MAP_LOAD)
1376    
1377     const_iv (WILL_APPLY_HANDLE)
1378     const_iv (WILL_APPLY_TREASURE)
1379     const_iv (WILL_APPLY_EARTHWALL)
1380     const_iv (WILL_APPLY_DOOR)
1381     const_iv (WILL_APPLY_FOOD)
1382    
1383     const_iv (SAVE_MODE)
1384     const_iv (SAVE_DIR_MODE)
1385    
1386     const_iv (M_PAPER)
1387     const_iv (M_IRON)
1388     const_iv (M_GLASS)
1389     const_iv (M_LEATHER)
1390     const_iv (M_WOOD)
1391     const_iv (M_ORGANIC)
1392     const_iv (M_STONE)
1393     const_iv (M_CLOTH)
1394     const_iv (M_ADAMANT)
1395     const_iv (M_LIQUID)
1396     const_iv (M_SOFT_METAL)
1397     const_iv (M_BONE)
1398     const_iv (M_ICE)
1399     const_iv (M_SPECIAL)
1400    
1401     const_iv (SK_EXP_ADD_SKILL)
1402     const_iv (SK_EXP_TOTAL)
1403     const_iv (SK_EXP_NONE)
1404     const_iv (SK_SUBTRACT_SKILL_EXP)
1405 elmex 1.39 const_iv (SK_EXP_SKILL_ONLY)
1406 root 1.1
1407     const_iv (SK_LOCKPICKING)
1408     const_iv (SK_HIDING)
1409     const_iv (SK_SMITHERY)
1410     const_iv (SK_BOWYER)
1411     const_iv (SK_JEWELER)
1412     const_iv (SK_ALCHEMY)
1413     const_iv (SK_STEALING)
1414     const_iv (SK_LITERACY)
1415     const_iv (SK_BARGAINING)
1416     const_iv (SK_JUMPING)
1417     const_iv (SK_DET_MAGIC)
1418     const_iv (SK_ORATORY)
1419     const_iv (SK_SINGING)
1420     const_iv (SK_DET_CURSE)
1421     const_iv (SK_FIND_TRAPS)
1422     const_iv (SK_MEDITATION)
1423     const_iv (SK_PUNCHING)
1424     const_iv (SK_FLAME_TOUCH)
1425     const_iv (SK_KARATE)
1426     const_iv (SK_CLIMBING)
1427     const_iv (SK_WOODSMAN)
1428     const_iv (SK_INSCRIPTION)
1429     const_iv (SK_ONE_HANDED_WEAPON)
1430     const_iv (SK_MISSILE_WEAPON)
1431     const_iv (SK_THROWING)
1432     const_iv (SK_USE_MAGIC_ITEM)
1433     const_iv (SK_DISARM_TRAPS)
1434     const_iv (SK_SET_TRAP)
1435     const_iv (SK_THAUMATURGY)
1436     const_iv (SK_PRAYING)
1437     const_iv (SK_CLAWING)
1438     const_iv (SK_LEVITATION)
1439     const_iv (SK_SUMMONING)
1440     const_iv (SK_PYROMANCY)
1441     const_iv (SK_EVOCATION)
1442     const_iv (SK_SORCERY)
1443     const_iv (SK_TWO_HANDED_WEAPON)
1444     const_iv (SK_SPARK_TOUCH)
1445     const_iv (SK_SHIVER)
1446     const_iv (SK_ACID_SPLASH)
1447     const_iv (SK_POISON_NAIL)
1448    
1449     const_iv (SOUND_NEW_PLAYER)
1450     const_iv (SOUND_FIRE_ARROW)
1451     const_iv (SOUND_LEARN_SPELL)
1452     const_iv (SOUND_FUMBLE_SPELL)
1453     const_iv (SOUND_WAND_POOF)
1454     const_iv (SOUND_OPEN_DOOR)
1455     const_iv (SOUND_PUSH_PLAYER)
1456     const_iv (SOUND_PLAYER_HITS1)
1457     const_iv (SOUND_PLAYER_HITS2)
1458     const_iv (SOUND_PLAYER_HITS3)
1459     const_iv (SOUND_PLAYER_HITS4)
1460     const_iv (SOUND_PLAYER_IS_HIT1)
1461     const_iv (SOUND_PLAYER_IS_HIT2)
1462     const_iv (SOUND_PLAYER_IS_HIT3)
1463     const_iv (SOUND_PLAYER_KILLS)
1464     const_iv (SOUND_PET_IS_KILLED)
1465     const_iv (SOUND_PLAYER_DIES)
1466     const_iv (SOUND_OB_EVAPORATE)
1467     const_iv (SOUND_OB_EXPLODE)
1468     const_iv (SOUND_CLOCK)
1469     const_iv (SOUND_TURN_HANDLE)
1470     const_iv (SOUND_FALL_HOLE)
1471     const_iv (SOUND_DRINK_POISON)
1472     const_iv (SOUND_CAST_SPELL_0)
1473    
1474     const_iv (PREFER_LOW)
1475     const_iv (PREFER_HIGH)
1476    
1477     const_iv (ATNR_PHYSICAL)
1478     const_iv (ATNR_MAGIC)
1479     const_iv (ATNR_FIRE)
1480     const_iv (ATNR_ELECTRICITY)
1481     const_iv (ATNR_COLD)
1482     const_iv (ATNR_CONFUSION)
1483     const_iv (ATNR_ACID)
1484     const_iv (ATNR_DRAIN)
1485     const_iv (ATNR_WEAPONMAGIC)
1486     const_iv (ATNR_GHOSTHIT)
1487     const_iv (ATNR_POISON)
1488     const_iv (ATNR_SLOW)
1489     const_iv (ATNR_PARALYZE)
1490     const_iv (ATNR_TURN_UNDEAD)
1491     const_iv (ATNR_FEAR)
1492     const_iv (ATNR_CANCELLATION)
1493     const_iv (ATNR_DEPLETE)
1494     const_iv (ATNR_DEATH)
1495     const_iv (ATNR_CHAOS)
1496     const_iv (ATNR_COUNTERSPELL)
1497     const_iv (ATNR_GODPOWER)
1498     const_iv (ATNR_HOLYWORD)
1499     const_iv (ATNR_BLIND)
1500     const_iv (ATNR_INTERNAL)
1501     const_iv (ATNR_LIFE_STEALING)
1502     const_iv (ATNR_DISEASE)
1503    
1504     const_iv (MAP_FLUSH)
1505     const_iv (MAP_PLAYER_UNIQUE)
1506     const_iv (MAP_BLOCK)
1507     const_iv (MAP_STYLE)
1508     const_iv (MAP_OVERLAY)
1509    
1510     const_iv (MAP_IN_MEMORY)
1511     const_iv (MAP_SWAPPED)
1512     const_iv (MAP_LOADING)
1513     const_iv (MAP_SAVING)
1514 root 1.7
1515     const_iv (KLASS_GLOBAL)
1516     const_iv (KLASS_OBJECT)
1517     const_iv (KLASS_PLAYER)
1518     const_iv (KLASS_MAP)
1519 root 1.1 };
1520    
1521     for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
1522     newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
1523    
1524     static const struct {
1525     const char *name;
1526 root 1.14 int skip;
1527 root 1.7 IV klass;
1528 root 1.1 IV iv;
1529 root 1.6 } *eiv, event_iv[] = {
1530 root 1.14 # define def(klass,name) { "EVENT_" # klass "_" # name, sizeof ("EVENT_" # klass), (IV)KLASS_ ## klass, (IV)EVENT_ ## klass ## _ ## name },
1531 root 1.6 # include "eventinc.h"
1532     # undef def
1533     };
1534    
1535     AV *av = get_av ("cf::EVENT", 1);
1536    
1537     for (eiv = event_iv + sizeof (event_iv) / sizeof (event_iv [0]); eiv-- > event_iv; )
1538 root 1.7 {
1539     AV *event = newAV ();
1540 root 1.14 av_push (event, newSVpv ((char *)eiv->name + eiv->skip, 0));
1541 root 1.7 av_push (event, newSViv (eiv->klass));
1542     av_store (av, eiv->iv, newRV_noinc ((SV *)event));
1543 root 1.14 newCONSTSUB (stash, (char *)eiv->name, newSViv (eiv->iv));
1544 root 1.7 }
1545 root 1.6
1546     static const struct {
1547 root 1.1 int dtype;
1548     const char *name;
1549     IV idx;
1550     } *cprop, prop_table[] = {
1551     # define prop(type, name) { type, # name, (IV) CFAPI_ ## name },
1552     prop (CFAPI_INT, MAP_PROP_FLAGS)
1553     prop (CFAPI_INT, MAP_PROP_DIFFICULTY)
1554     prop (CFAPI_STRING, MAP_PROP_PATH)
1555     prop (CFAPI_STRING, MAP_PROP_TMPNAME)
1556     prop (CFAPI_STRING, MAP_PROP_NAME)
1557     prop (CFAPI_INT, MAP_PROP_RESET_TIME)
1558     prop (CFAPI_INT, MAP_PROP_RESET_TIMEOUT)
1559     prop (CFAPI_INT, MAP_PROP_PLAYERS)
1560     prop (CFAPI_INT, MAP_PROP_DARKNESS)
1561     prop (CFAPI_INT, MAP_PROP_WIDTH)
1562     prop (CFAPI_INT, MAP_PROP_HEIGHT)
1563     prop (CFAPI_INT, MAP_PROP_ENTER_X)
1564     prop (CFAPI_INT, MAP_PROP_ENTER_Y)
1565     prop (CFAPI_INT, MAP_PROP_TEMPERATURE)
1566     prop (CFAPI_INT, MAP_PROP_PRESSURE)
1567     prop (CFAPI_INT, MAP_PROP_HUMIDITY)
1568     prop (CFAPI_INT, MAP_PROP_WINDSPEED)
1569     prop (CFAPI_INT, MAP_PROP_WINDDIR)
1570     prop (CFAPI_INT, MAP_PROP_SKY)
1571     prop (CFAPI_INT, MAP_PROP_WPARTX)
1572     prop (CFAPI_INT, MAP_PROP_WPARTY)
1573     prop (CFAPI_STRING, MAP_PROP_MESSAGE)
1574     prop (CFAPI_PREGION, MAP_PROP_REGION)
1575     prop (CFAPI_POBJECT, OBJECT_PROP_NEXT_ACTIVE_OB)
1576     prop (CFAPI_POBJECT, OBJECT_PROP_PREV_ACTIVE_OB)
1577     prop (CFAPI_POBJECT, OBJECT_PROP_INVENTORY)
1578     prop (CFAPI_POBJECT, OBJECT_PROP_ENVIRONMENT)
1579     prop (CFAPI_POBJECT, OBJECT_PROP_CONTAINER)
1580     prop (CFAPI_PMAP, OBJECT_PROP_MAP)
1581     prop (CFAPI_INT, OBJECT_PROP_COUNT)
1582     prop (CFAPI_INT, OBJECT_PROP_REFCOUNT)
1583     prop (CFAPI_STRING, OBJECT_PROP_NAME)
1584     prop (CFAPI_STRING, OBJECT_PROP_NAME_PLURAL)
1585     prop (CFAPI_STRING, OBJECT_PROP_TITLE)
1586     prop (CFAPI_STRING, OBJECT_PROP_RACE)
1587     prop (CFAPI_STRING, OBJECT_PROP_SLAYING)
1588     prop (CFAPI_STRING, OBJECT_PROP_SKILL)
1589     prop (CFAPI_STRING, OBJECT_PROP_MESSAGE)
1590     prop (CFAPI_STRING, OBJECT_PROP_LORE)
1591     prop (CFAPI_INT, OBJECT_PROP_X)
1592     prop (CFAPI_INT, OBJECT_PROP_Y)
1593     prop (CFAPI_DOUBLE, OBJECT_PROP_SPEED)
1594     prop (CFAPI_DOUBLE, OBJECT_PROP_SPEED_LEFT)
1595     prop (CFAPI_INT, OBJECT_PROP_NROF)
1596     prop (CFAPI_INT, OBJECT_PROP_DIRECTION)
1597     prop (CFAPI_INT, OBJECT_PROP_FACING)
1598     prop (CFAPI_INT, OBJECT_PROP_TYPE)
1599     prop (CFAPI_INT, OBJECT_PROP_SUBTYPE)
1600     prop (CFAPI_INT, OBJECT_PROP_CLIENT_TYPE)
1601     prop (CFAPI_INT, OBJECT_PROP_ATTACK_TYPE)
1602     prop (CFAPI_INT, OBJECT_PROP_PATH_ATTUNED)
1603     prop (CFAPI_INT, OBJECT_PROP_PATH_REPELLED)
1604     prop (CFAPI_INT, OBJECT_PROP_PATH_DENIED)
1605     prop (CFAPI_INT, OBJECT_PROP_MATERIAL)
1606     prop (CFAPI_STRING, OBJECT_PROP_MATERIAL_NAME)
1607     prop (CFAPI_INT, OBJECT_PROP_MAGIC)
1608     prop (CFAPI_INT, OBJECT_PROP_VALUE)
1609     prop (CFAPI_INT, OBJECT_PROP_LEVEL)
1610     prop (CFAPI_INT, OBJECT_PROP_LAST_HEAL)
1611     prop (CFAPI_INT, OBJECT_PROP_LAST_SP)
1612     prop (CFAPI_INT, OBJECT_PROP_LAST_GRACE)
1613     prop (CFAPI_INT, OBJECT_PROP_LAST_EAT)
1614     prop (CFAPI_INT, OBJECT_PROP_INVISIBLE_TIME)
1615     prop (CFAPI_INT, OBJECT_PROP_PICK_UP)
1616     prop (CFAPI_INT, OBJECT_PROP_ITEM_POWER)
1617     prop (CFAPI_INT, OBJECT_PROP_GEN_SP_ARMOUR)
1618     prop (CFAPI_INT, OBJECT_PROP_WEIGHT)
1619     prop (CFAPI_INT, OBJECT_PROP_WEIGHT_LIMIT)
1620     prop (CFAPI_INT, OBJECT_PROP_CARRYING)
1621     prop (CFAPI_INT, OBJECT_PROP_GLOW_RADIUS)
1622     prop (CFAPI_LONG, OBJECT_PROP_PERM_EXP)
1623     prop (CFAPI_POBJECT, OBJECT_PROP_CURRENT_WEAPON)
1624     prop (CFAPI_POBJECT, OBJECT_PROP_ENEMY)
1625     prop (CFAPI_POBJECT, OBJECT_PROP_ATTACKED_BY)
1626     prop (CFAPI_INT, OBJECT_PROP_RUN_AWAY)
1627     prop (CFAPI_POBJECT, OBJECT_PROP_CHOSEN_SKILL)
1628     prop (CFAPI_INT, OBJECT_PROP_HIDDEN)
1629     prop (CFAPI_INT, OBJECT_PROP_MOVE_STATUS)
1630     prop (CFAPI_INT, OBJECT_PROP_MOVE_TYPE)
1631     prop (CFAPI_POBJECT, OBJECT_PROP_SPELL_ITEM)
1632     prop (CFAPI_DOUBLE, OBJECT_PROP_EXP_MULTIPLIER)
1633     prop (CFAPI_PARCH, OBJECT_PROP_ARCHETYPE)
1634     prop (CFAPI_PARCH, OBJECT_PROP_OTHER_ARCH)
1635     prop (CFAPI_STRING, OBJECT_PROP_CUSTOM_NAME)
1636     prop (CFAPI_INT, OBJECT_PROP_ANIM_SPEED)
1637     prop (CFAPI_INT, OBJECT_PROP_FRIENDLY)
1638     prop (CFAPI_STRING, OBJECT_PROP_SHORT_NAME)
1639     prop (CFAPI_INT, OBJECT_PROP_MAGICAL)
1640     prop (CFAPI_INT, OBJECT_PROP_LUCK)
1641     prop (CFAPI_POBJECT, OBJECT_PROP_OWNER)
1642     prop (CFAPI_POBJECT, OBJECT_PROP_PRESENT)
1643     prop (CFAPI_INT, OBJECT_PROP_CHEATER)
1644     prop (CFAPI_INT, OBJECT_PROP_MERGEABLE)
1645     prop (CFAPI_INT, OBJECT_PROP_PICKABLE)
1646     prop (CFAPI_INT, OBJECT_PROP_STR)
1647     prop (CFAPI_INT, OBJECT_PROP_DEX)
1648     prop (CFAPI_INT, OBJECT_PROP_CON)
1649     prop (CFAPI_INT, OBJECT_PROP_WIS)
1650     prop (CFAPI_INT, OBJECT_PROP_INT)
1651     prop (CFAPI_INT, OBJECT_PROP_POW)
1652     prop (CFAPI_INT, OBJECT_PROP_CHA)
1653     prop (CFAPI_INT, OBJECT_PROP_WC)
1654     prop (CFAPI_INT, OBJECT_PROP_AC)
1655     prop (CFAPI_INT, OBJECT_PROP_HP)
1656     prop (CFAPI_INT, OBJECT_PROP_SP)
1657     prop (CFAPI_INT, OBJECT_PROP_GP)
1658     prop (CFAPI_INT, OBJECT_PROP_FP)
1659     prop (CFAPI_INT, OBJECT_PROP_MAXHP)
1660     prop (CFAPI_INT, OBJECT_PROP_MAXSP)
1661     prop (CFAPI_INT, OBJECT_PROP_MAXGP)
1662     prop (CFAPI_INT, OBJECT_PROP_DAM)
1663     prop (CFAPI_STRING, OBJECT_PROP_GOD)
1664     prop (CFAPI_STRING, OBJECT_PROP_ARCH_NAME)
1665     prop (CFAPI_INT, OBJECT_PROP_INVISIBLE)
1666     prop (CFAPI_INT, OBJECT_PROP_FACE)
1667     };
1668    
1669     HV *prop_type = get_hv ("cf::PROP_TYPE", 1);
1670     HV *prop_idx = get_hv ("cf::PROP_IDX", 1);
1671    
1672     for (cprop = prop_table + sizeof (prop_table) / sizeof (prop_table [0]); cprop-- > prop_table; )
1673     {
1674     hv_store (prop_type, cprop->name, strlen (cprop->name), newSViv (cprop->dtype), 0);
1675     hv_store (prop_idx, cprop->name, strlen (cprop->name), newSViv (cprop->idx ), 0);
1676     }
1677    
1678 root 1.14 //I_EVENT_API (PACKAGE);
1679     }
1680    
1681     void _reload_1 ()
1682     CODE:
1683     cb_global = get_av ("cf::CB_GLOBAL", 1);
1684     cb_object = get_av ("cf::CB_OBJECT", 1);
1685     cb_player = get_av ("cf::CB_PLAYER", 1);
1686     cb_type = get_av ("cf::CB_TYPE" , 1);
1687     cb_map = get_av ("cf::CB_MAP" , 1);
1688    
1689     void _reload_2 ()
1690     CODE:
1691     {
1692     // reattach to all attachable objects in the game.
1693     for (player *pl = first_player; pl; pl = pl->next)
1694     reattach (pl);
1695    
1696     for (mapstruct *map = first_map; map; map = map->next)
1697     reattach (map);
1698 root 1.8
1699 root 1.14 for (object *op = objects; op; op = op->next)
1700     reattach (op);
1701 root 1.1 }
1702    
1703     NV floor (NV x)
1704    
1705     NV ceil (NV x)
1706    
1707 root 1.5 void server_tick ()
1708    
1709 root 1.1 void
1710     LOG (int level, char *msg)
1711     PROTOTYPE: $$
1712     C_ARGS: (LogLevel)level, "%s", msg
1713    
1714     char *path_combine (char *base, char *path)
1715     PROTOTYPE: $$
1716    
1717     char *path_combine_and_normalize (char *base, char *path)
1718     PROTOTYPE: $$
1719    
1720 root 1.19 const char *
1721     get_maps_directory (char *path)
1722 root 1.1 PROTOTYPE: $
1723     ALIAS: maps_directory = 0
1724 root 1.19 CODE:
1725     RETVAL = create_pathname (path);
1726     OUTPUT: RETVAL
1727 root 1.1
1728     void
1729     sub_generation_inc ()
1730     CODE:
1731     PL_sub_generation++;
1732    
1733     char *
1734     mapdir ()
1735     PROTOTYPE:
1736     ALIAS:
1737     mapdir = 0
1738     uniquedir = 1
1739     tmpdir = 2
1740     confdir = 3
1741     localdir = 4
1742     playerdir = 5
1743     datadir = 6
1744     CODE:
1745 root 1.19 switch (ix)
1746     {
1747     case 0: RETVAL = settings.mapdir ; break;
1748     case 1: RETVAL = settings.uniquedir; break;
1749     case 2: RETVAL = settings.tmpdir ; break;
1750     case 3: RETVAL = settings.confdir ; break;
1751     case 4: RETVAL = settings.localdir ; break;
1752     case 5: RETVAL = settings.playerdir; break;
1753     case 6: RETVAL = settings.datadir ; break;
1754     }
1755 root 1.1 OUTPUT: RETVAL
1756    
1757     int
1758     cf_find_animation (char *text)
1759     PROTOTYPE: $
1760    
1761     int random_roll(int min, int max, object *op, int goodbad);
1762    
1763     const char *cost_string_from_value(uint64 cost, int approx = 0)
1764    
1765 root 1.18 int invoke (int event, ...)
1766 root 1.12 CODE:
1767 root 1.18 if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL");
1768 root 1.24 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1769     for (int i = 1; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1770 root 1.18 RETVAL = INVOKE_((event_type)event, ARG_AV (av));
1771     OUTPUT: RETVAL
1772 root 1.12
1773 root 1.1 int
1774     exp_to_level (val64 exp)
1775     CODE:
1776     {
1777     int i = 0;
1778    
1779     RETVAL = settings.max_level;
1780    
1781     for (i = 1; i <= settings.max_level; i++)
1782     {
1783     if (levels[i] > exp)
1784     {
1785     RETVAL = i - 1;
1786     break;
1787     }
1788     }
1789     }
1790     OUTPUT: RETVAL
1791    
1792     val64
1793     level_to_min_exp (int level)
1794     CODE:
1795     if (level > settings.max_level)
1796     RETVAL = levels[settings.max_level];
1797     else if (level < 1)
1798     RETVAL = 0;
1799     else
1800     RETVAL = levels[level];
1801     OUTPUT: RETVAL
1802    
1803     SV *
1804     resistance_to_string (int atnr)
1805     CODE:
1806     if (atnr >= 0 && atnr < NROFATTACKS)
1807     RETVAL = newSVpv (resist_plus[atnr], 0);
1808     else
1809     XSRETURN_UNDEF;
1810     OUTPUT: RETVAL
1811    
1812 root 1.27 int
1813     _valid (SV *obj)
1814     CODE:
1815     RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext);
1816     OUTPUT:
1817     RETVAL
1818    
1819 root 1.1 MODULE = cf PACKAGE = cf::object PREFIX = cf_object_
1820    
1821 root 1.18 int invoke (object *op, int event, ...)
1822     CODE:
1823     if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT");
1824 root 1.24 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1825     for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1826 root 1.18 RETVAL = INVOKE_((event_type)event, ARG_OBJECT (op), ARG_AV (av));
1827     OUTPUT: RETVAL
1828    
1829     SV *registry (object *op)
1830     CODE:
1831     RETVAL = registry_of (op);
1832     OUTPUT:
1833     RETVAL
1834    
1835 root 1.27 object *head (object *op)
1836     PROTOTYPE: $
1837     ALIAS:
1838     more = 1
1839     above = 2
1840     below = 3
1841     CODE:
1842     switch (ix)
1843     {
1844     case 0: RETVAL = op->head ? op->head : op; break; // DOH!
1845     case 1: RETVAL = op->more; break;
1846     case 2: RETVAL = op->above; break;
1847     case 3: RETVAL = op->below; break;
1848     }
1849     OUTPUT:
1850     RETVAL
1851    
1852 root 1.1 SV *
1853     get_property (object *obj, int type, int idx)
1854     CODE:
1855     RETVAL = newSVcfapi (type, cf_object_get_property (obj, idx));
1856     OUTPUT: RETVAL
1857    
1858     SV *
1859     set_property (object *obj, int type, int idx, SV *newval)
1860     CODE:
1861     switch (type)
1862     {
1863     case CFAPI_INT:
1864     cf_object_set_int_property (obj, idx, SvIV (newval));
1865     break;
1866     case CFAPI_LONG:
1867     cf_object_set_long_property (obj, idx, (long) SvVAL64 (newval));
1868     break;
1869     case CFAPI_DOUBLE:
1870     {
1871     int unused_type;
1872     object_set_property (&unused_type, obj, idx, (double)SvNV (newval));
1873     }
1874     break;
1875     case CFAPI_STRING:
1876     cf_object_set_string_property (obj, idx, SvOK (newval) ? SvPV_nolen (newval) : 0);
1877     break;
1878     case CFAPI_POBJECT:
1879     {
1880     int unused_type;
1881     object_set_property (&unused_type, obj, idx, (object *)SvPTR_ornull (newval, "cf::object"));
1882     }
1883     break;
1884     default:
1885     croak ("unhandled type '%d' in set_property '%d'", type, idx);
1886     }
1887    
1888     # missing properties
1889    
1890     void
1891     set_attacktype (object *obj, U32 attacktype)
1892     CODE:
1893     obj->attacktype = attacktype;
1894    
1895     U32
1896     get_attacktype (object *obj)
1897     ALIAS:
1898     attacktype = 0
1899     CODE:
1900     RETVAL = obj->attacktype;
1901     OUTPUT: RETVAL
1902    
1903     # missing in plug-in api, of course
1904     void
1905     set_food (object *obj, int food)
1906     CODE:
1907     obj->stats.food = food;
1908    
1909     int
1910     get_food (object *obj)
1911     ALIAS:
1912     food = 0
1913     CODE:
1914     RETVAL = obj->stats.food;
1915     OUTPUT: RETVAL
1916    
1917     void
1918     inv (object *obj)
1919     PROTOTYPE: $
1920     PPCODE:
1921     {
1922     object *o;
1923     for (o = obj->inv; o; o = o->below)
1924     XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
1925     }
1926    
1927     int cf_object_get_resistance (object *op, int rtype)
1928     ALIAS: resistance = 0
1929    
1930     int cf_object_get_flag (object *op, int flag)
1931     ALIAS: flag = 0
1932    
1933     void cf_object_set_flag (object *op, int flag, int value)
1934    
1935     int need_identify (const object *obj);
1936    
1937     int apply_shop_mat (object *shop_mat, object *op);
1938    
1939 root 1.27 int move (object *op, int dir, object *originator = op)
1940     CODE:
1941     RETVAL = move_ob (op, dir, originator);
1942     OUTPUT:
1943     RETVAL
1944 root 1.1
1945     void cf_object_apply (object *op, object *author, int flags = 0)
1946    
1947     void cf_object_apply_below (object *op)
1948    
1949     void cf_object_remove (object *op)
1950    
1951     void cf_object_free (object *op)
1952    
1953     object *cf_object_present_archname_inside (object *op, char *whatstr)
1954    
1955     int cf_object_transfer (object *op, int x, int y, int r = 0, object_ornull *orig = 0)
1956    
1957     int cf_object_change_map (object *op, int x, int y, mapstruct *map)
1958    
1959     object *cf_object_clone (object *op, int clonetype = 0)
1960    
1961     int cf_object_pay_item (object *op, object *buyer)
1962    
1963     int cf_object_pay_amount (object *op, uint64 amount)
1964    
1965     void pay_player (object *op, uint64 amount)
1966    
1967     val64 pay_player_arch (object *op, const char *arch, uint64 amount)
1968    
1969     int cf_object_cast_spell (object *caster, object *ctoo, int dir, object *spell_ob, char *stringarg = 0)
1970    
1971     int cf_object_cast_ability (object *caster, object *ctoo, int dir, object *sp_, char *stringarg = 0)
1972    
1973     void cf_object_learn_spell (object *op, object *sp)
1974    
1975     void cf_object_forget_spell (object *op, object *sp)
1976    
1977     object *cf_object_check_for_spell (object *op, char *spellname)
1978    
1979     int cf_object_query_money (object *op)
1980     ALIAS: money = 0
1981    
1982     int cf_object_query_cost (object *op, object *who, int flags)
1983     ALIAS: cost = 0
1984    
1985     void cf_object_activate_rune (object *op , object *victim)
1986    
1987     int cf_object_check_trigger (object *op, object *cause)
1988    
1989     int cf_object_out_of_map (object *op, int x, int y)
1990    
1991     void cf_object_drop (object *op, object *author)
1992    
1993     void cf_object_take (object *op, object *author)
1994    
1995     object *cf_object_insert_object (object *op, object *container)
1996    
1997     const char *cf_object_get_msg (object *ob)
1998     ALIAS: msg = 0
1999    
2000     object *cf_object_insert_in_ob (object *ob, object *where)
2001    
2002     int cf_object_teleport (object *op, mapstruct *map, int x, int y)
2003    
2004     void cf_object_update (object *op, int flags)
2005    
2006     void cf_object_pickup (object *op, object *what)
2007    
2008     object *cf_create_object_by_name (const char *name)
2009    
2010     void change_exp (object *op, uint64 exp, const char *skill_name = 0, int flag = 0)
2011    
2012     void player_lvl_adj (object *who, object *skill = 0)
2013    
2014     int kill_object (object *op, int dam = 0, object *hitter = 0, int type = AT_PHYSICAL)
2015    
2016     int calc_skill_exp (object *who, object *op, object *skill);
2017    
2018     void push_button (object *op);
2019    
2020     void use_trigger (object *op);
2021    
2022     void add_button_link (object *button, mapstruct *map, int connected);
2023    
2024     void remove_button_link (object *op);
2025    
2026     void
2027     cf_object_set_resistance (object *op, int rtype, int val)
2028     CODE:
2029     if (rtype >= 0 && rtype < NROFATTACKS)
2030     op->resist[rtype] = val;
2031    
2032    
2033     MODULE = cf PACKAGE = cf::object PREFIX = cf_
2034    
2035     void cf_fix_object (object *pl)
2036     ALIAS: fix = 0
2037    
2038     object *cf_insert_ob_in_ob (object *ob, object *where)
2039    
2040     # no clean way to get an object from an archetype - stupid idiotic
2041     # dumb kludgy misdesigned plug-in api slowly gets on my nerves.
2042    
2043     object *new (const char *archetype = 0)
2044     PROTOTYPE: ;$
2045     CODE:
2046     RETVAL = archetype ? get_archetype (archetype) : cf_create_object ();
2047     OUTPUT:
2048     RETVAL
2049    
2050     object *insert_ob_in_map_at (object *ob, mapstruct *where, object_ornull *orig, int flag, int x, int y)
2051     PROTOTYPE: $$$$$$
2052     CODE:
2053     {
2054     int unused_type;
2055     RETVAL = (object *)object_insert (&unused_type, ob, 0, where, orig, flag, x, y);
2056     }
2057    
2058     # syntatic sugar for easier use in event callbacks.
2059     const char *options (object *op)
2060     CODE:
2061     RETVAL = op->name;
2062     OUTPUT:
2063     RETVAL
2064    
2065     player *contr (object *op)
2066     CODE:
2067     RETVAL = op->contr;
2068     OUTPUT: RETVAL
2069    
2070     const char *get_ob_key_value (object *op, const char *key)
2071    
2072     bool set_ob_key_value (object *op, const char *key, const char *value = 0, int add_key = 1)
2073    
2074     object *get_nearest_player (object *ob)
2075     ALIAS: nearest_player = 0
2076     PREINIT:
2077     extern object *get_nearest_player (object *);
2078    
2079     void rangevector (object *ob, object *other, int flags = 0)
2080     PROTOTYPE: $$;$
2081     PPCODE:
2082     {
2083     rv_vector rv;
2084     get_rangevector (ob, other, &rv, flags);
2085     EXTEND (SP, 5);
2086     PUSHs (newSVuv (rv.distance));
2087     PUSHs (newSViv (rv.distance_x));
2088     PUSHs (newSViv (rv.distance_y));
2089     PUSHs (newSViv (rv.direction));
2090     PUSHs (newSVcfapi (CFAPI_POBJECT, rv.part));
2091     }
2092    
2093     bool on_same_map_as (object *ob, object *other)
2094     CODE:
2095     RETVAL = on_same_map (ob, other);
2096     OUTPUT: RETVAL
2097    
2098     char *
2099     base_name (object *ob, int plural)
2100     CODE:
2101     RETVAL = cf_query_base_name (ob, plural);
2102     OUTPUT: RETVAL
2103    
2104     living *
2105     stats (object *ob)
2106     CODE:
2107     RETVAL = &ob->stats;
2108     OUTPUT: RETVAL
2109    
2110    
2111     MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_
2112    
2113     player *player (object *op)
2114     CODE:
2115     RETVAL = op->contr;
2116     OUTPUT: RETVAL
2117    
2118     void cf_player_message (object *obj, char *txt, int flags = NDI_ORANGE | NDI_UNIQUE)
2119    
2120     object *cf_player_send_inventory (object *op)
2121    
2122     char *cf_player_get_ip (object *op)
2123     ALIAS: ip = 0
2124    
2125     object *cf_player_get_marked_item (object *op)
2126     ALIAS: marked_item = 0
2127    
2128     void cf_player_set_marked_item (object *op, object *ob)
2129    
2130     partylist *cf_player_get_party (object *op)
2131     ALIAS: party = 0
2132    
2133     void cf_player_set_party (object *op, partylist *party)
2134    
2135     void kill_player (object *op)
2136    
2137 root 1.12 MODULE = cf PACKAGE = cf::player PREFIX = cf_player_
2138 root 1.1
2139 root 1.18 int invoke (player *pl, int event, ...)
2140     CODE:
2141     if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER");
2142 root 1.24 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2143     for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
2144 root 1.18 RETVAL = INVOKE_((event_type)event, ARG_PLAYER (pl), ARG_AV (av));
2145     OUTPUT: RETVAL
2146    
2147 root 1.12 SV *registry (player *pl)
2148     CODE:
2149     RETVAL = registry_of (pl);
2150     OUTPUT:
2151     RETVAL
2152 root 1.1
2153     player *cf_player_find (char *name)
2154     PROTOTYPE: $
2155    
2156     void cf_player_move (player *pl, int dir)
2157    
2158     void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0);
2159    
2160     # nonstandard
2161     object *ob (player *pl)
2162     CODE:
2163     RETVAL = pl->ob;
2164     OUTPUT: RETVAL
2165    
2166     player *first ()
2167     CODE:
2168     RETVAL = first_player;
2169     OUTPUT: RETVAL
2170    
2171     player *next (player *pl)
2172     CODE:
2173     RETVAL = pl->next;
2174     OUTPUT: RETVAL
2175    
2176     bool
2177     cell_visible (player *pl, int dx, int dy)
2178     CODE:
2179     RETVAL = FABS (dx) <= pl->socket.mapx / 2 && FABS (dy) <= pl->socket.mapy / 2
2180     && !pl->blocked_los [dx + pl->socket.mapx / 2][dy + pl->socket.mapy / 2];
2181     OUTPUT:
2182     RETVAL
2183    
2184 root 1.4 char *
2185     client (player *pl)
2186     CODE:
2187     RETVAL = pl->socket.client;
2188     OUTPUT:
2189     RETVAL
2190    
2191 root 1.7 char *
2192     host (player *pl)
2193     CODE:
2194     RETVAL = pl->socket.host;
2195     OUTPUT:
2196     RETVAL
2197    
2198     char *
2199     killer (player *pl, char *killer = 0)
2200     CODE:
2201     if (killer)
2202     snprintf (pl->killer, sizeof (pl->killer), "%s", killer);
2203     RETVAL = pl->killer;
2204     OUTPUT:
2205     RETVAL
2206    
2207 root 1.4 void
2208     buggy_mapscroll (player *pl, int value = 1)
2209     CODE:
2210     pl->socket.buggy_mapscroll = value;
2211    
2212 root 1.1 void
2213     send (player *pl, SV *packet)
2214     CODE:
2215     {
2216     STRLEN len;
2217     char *buf = SvPVbyte (packet, len);
2218    
2219     Write_String_To_Socket (&pl->socket, buf, len);
2220     }
2221    
2222     int
2223     listening (player *pl, int new_value = -1)
2224     CODE:
2225     RETVAL = pl->listening;
2226     if (new_value >= 0)
2227     pl->listening = new_value;
2228     OUTPUT:
2229     RETVAL
2230    
2231     void get_savebed (player *pl)
2232     ALIAS:
2233     savebed = 0
2234     PPCODE:
2235     EXTEND (SP, 3);
2236     PUSHs (sv_2mortal (newSVpv (pl->savebed_map, 0)));
2237     PUSHs (sv_2mortal (newSViv (pl->bed_x)));
2238     PUSHs (sv_2mortal (newSViv (pl->bed_y)));
2239    
2240     void set_savebed (player *pl, char *map_path, int x, int y)
2241     CODE:
2242     strcpy (pl->savebed_map, map_path);
2243     pl->bed_x = x;
2244     pl->bed_y = y;
2245    
2246     void
2247     list ()
2248     PPCODE:
2249     {
2250     player *pl;
2251     for (pl = first_player; pl; pl = pl->next)
2252     XPUSHs (newSVcfapi (CFAPI_PPLAYER, pl));
2253     }
2254    
2255     bool
2256     peaceful (player *pl, bool new_setting = 0)
2257     PROTOTYPE: $;$
2258     CODE:
2259     RETVAL = pl->peaceful;
2260     if (items > 1)
2261     pl->peaceful = new_setting;
2262     OUTPUT:
2263     RETVAL
2264    
2265     living *
2266     orig_stats (player *pl)
2267     CODE:
2268     RETVAL = &pl->orig_stats;
2269     OUTPUT: RETVAL
2270    
2271     living *
2272     last_stats (player *pl)
2273     CODE:
2274     RETVAL = &pl->last_stats;
2275     OUTPUT: RETVAL
2276    
2277    
2278     MODULE = cf PACKAGE = cf::map PREFIX = cf_map_
2279    
2280 root 1.29 mapstruct *first ()
2281     PROTOTYPE:
2282 elmex 1.26 CODE:
2283 root 1.29 RETVAL = first_map;
2284     OUTPUT: RETVAL
2285 elmex 1.26
2286 root 1.29 mapstruct *next (mapstruct *map)
2287     PROTOTYPE:
2288     CODE:
2289     RETVAL = map->next;
2290     OUTPUT: RETVAL
2291 elmex 1.26
2292 root 1.18 int invoke (mapstruct *map, int event, ...)
2293     CODE:
2294     if (KLASS_OF (event) != KLASS_MAP) croak ("event class must be MAP");
2295 root 1.24 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2296     for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
2297 root 1.18 RETVAL = INVOKE_((event_type)event, ARG_MAP (map), ARG_AV (av));
2298 root 1.25 OUTPUT: RETVAL
2299 root 1.18
2300 root 1.12 SV *registry (mapstruct *map)
2301     CODE:
2302     RETVAL = registry_of (map);
2303     OUTPUT:
2304     RETVAL
2305    
2306 root 1.1 SV *
2307     get_property (mapstruct *obj, int type, int idx)
2308     CODE:
2309     RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx));
2310     OUTPUT: RETVAL
2311    
2312     SV *
2313     set_property (mapstruct *obj, int type, int idx, SV *newval)
2314     CODE:
2315     switch (type)
2316     {
2317     case CFAPI_INT:
2318     cf_map_set_int_property (obj, idx, SvIV (newval));
2319     break;
2320     default:
2321     croak ("unhandled type '%d' in set_property '%d'", type, idx);
2322     }
2323    
2324     mapstruct *new (int width, int height)
2325     PROTOTYPE:
2326     CODE:
2327     {
2328 root 1.29 RETVAL = get_empty_map (width, height);
2329 root 1.1 }
2330     OUTPUT:
2331     RETVAL
2332    
2333     void delete_map (mapstruct *map)
2334    
2335     void clean_tmp_map (mapstruct *map)
2336    
2337     void play_sound_map (mapstruct *map, int x, int y, int sound_num)
2338    
2339     mapstruct *tile_map (mapstruct *map, unsigned int dir)
2340     CODE:
2341     RETVAL = dir < 4 ? map->tile_map [dir] : 0;
2342     OUTPUT:
2343     RETVAL
2344    
2345     char *tile_path (mapstruct *map, unsigned int dir)
2346     CODE:
2347     if (dir >= 4)
2348     XSRETURN_UNDEF;
2349     RETVAL = map->tile_path [dir];
2350     OUTPUT:
2351     RETVAL
2352    
2353 root 1.29 mapstruct *ready_map_name (char *name, int flags = 0)
2354     PROTOTYPE: $;$
2355     ALIAS:
2356     find = 0
2357     get_map = 1
2358 root 1.1
2359     mapstruct *has_been_loaded (char *name)
2360     PROTOTYPE: $
2361    
2362     # whoever "designed" the plug-in api should have wasted
2363     # his/her time with staying away from the project - would have
2364     # saved others a lot of time, without doubt.
2365     void set_path (mapstruct *where, char *path)
2366     CODE:
2367     strcpy (where->path, path);
2368    
2369     int in_memory (mapstruct *map)
2370     CODE:
2371     RETVAL = map->in_memory;
2372     OUTPUT:
2373     RETVAL
2374    
2375     bool unique (mapstruct *map)
2376     CODE:
2377     RETVAL = map->unique;
2378     OUTPUT:
2379     RETVAL
2380    
2381     void set_unique (mapstruct *map, bool unique)
2382     CODE:
2383     map->unique = unique;
2384    
2385 root 1.29 void
2386     trigger (mapstruct *map, long connection, bool state = true)
2387     CODE:
2388     activate_connection (map, connection, state);
2389    
2390     void
2391     get_connection (mapstruct *map, long connection)
2392     PPCODE:
2393     oblinkpt *obp = get_connection_links (map, connection);
2394     if (obp)
2395     for (objectlink *ol = obp->link; ol; ol = ol->next)
2396     XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, ol->ob)));
2397    
2398 root 1.1 object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags)
2399    
2400     object *cf_map_insert_object (mapstruct *where, object* op, int x, int y)
2401    
2402     object* cf_map_present_arch_by_name (mapstruct *map, const char* str, int nx, int ny)
2403     C_ARGS: str, map, nx, ny
2404    
2405     void
2406     cf_map_normalise (mapstruct *map, int x, int y)
2407     PPCODE:
2408     {
2409     mapstruct *nmap = 0;
2410     I16 nx = 0, ny = 0;
2411 root 1.19 int flags = get_map_flags (map, &nmap, x, y, &nx, &ny);
2412 root 1.1
2413     EXTEND (SP, 4);
2414     PUSHs (sv_2mortal (newSViv (flags)));
2415    
2416     if (GIMME_V == G_ARRAY)
2417     {
2418     PUSHs (sv_2mortal (newSVcfapi (CFAPI_PMAP, nmap)));
2419     PUSHs (sv_2mortal (newSViv (nx)));
2420     PUSHs (sv_2mortal (newSViv (ny)));
2421     }
2422     }
2423    
2424     void
2425     at (mapstruct *map, unsigned int x, unsigned int y)
2426     PROTOTYPE: $$$
2427     PPCODE:
2428     {
2429     object *o;
2430     mapstruct *nmap = 0;
2431     I16 nx, ny;
2432    
2433 root 1.19 get_map_flags (map, &nmap, x, y, &nx, &ny);
2434 root 1.1
2435     if (nmap)
2436     for (o = GET_MAP_OB (nmap, nx, ny); o; o = o->above)
2437     XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
2438     }
2439    
2440     SV *
2441     bot_at (mapstruct *obj, unsigned int x, unsigned int y)
2442     PROTOTYPE: $$$
2443     ALIAS:
2444     top_at = 1
2445     flags_at = 2
2446     light_at = 3
2447     move_block_at = 4
2448     move_slow_at = 5
2449     move_on_at = 6
2450     move_off_at = 7
2451     INIT:
2452     if (x >= MAP_WIDTH (obj) || y >= MAP_HEIGHT (obj)) XSRETURN_UNDEF;
2453     CODE:
2454     switch (ix)
2455     {
2456     case 0: RETVAL = newSVcfapi (CFAPI_POBJECT, GET_MAP_OB (obj, x, y)); break;
2457     case 1: RETVAL = newSVcfapi (CFAPI_POBJECT, GET_MAP_TOP (obj, x, y)); break;
2458     case 2: RETVAL = newSVuv ( GET_MAP_FLAGS (obj, x, y)); break;
2459     case 3: RETVAL = newSViv ( GET_MAP_LIGHT (obj, x, y)); break;
2460     case 4: RETVAL = newSVuv ( GET_MAP_MOVE_BLOCK (obj, x, y)); break;
2461     case 5: RETVAL = newSVuv ( GET_MAP_MOVE_SLOW (obj, x, y)); break;
2462     case 6: RETVAL = newSVuv ( GET_MAP_MOVE_ON (obj, x, y)); break;
2463     case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break;
2464     }
2465     OUTPUT:
2466     RETVAL
2467    
2468    
2469 root 1.19 MODULE = cf PACKAGE = cf::arch
2470 root 1.1
2471 elmex 1.36 archetype *find (const char *name)
2472     CODE:
2473     RETVAL = find_archetype (name);
2474     OUTPUT:
2475     RETVAL
2476    
2477 root 1.19 archetype *first()
2478 root 1.1 PROTOTYPE:
2479 root 1.19 CODE:
2480     RETVAL = first_archetype;
2481     OUTPUT: RETVAL
2482 root 1.1
2483 root 1.19 archetype *next (archetype *arch)
2484     CODE:
2485     RETVAL = arch->next;
2486     OUTPUT: RETVAL
2487 root 1.1
2488 root 1.19 archetype *head (archetype *arch)
2489     CODE:
2490     RETVAL = arch->head;
2491     OUTPUT: RETVAL
2492 root 1.1
2493 root 1.19 archetype *more (archetype *arch)
2494     CODE:
2495     RETVAL = arch->more;
2496     OUTPUT: RETVAL
2497 root 1.1
2498 root 1.19 const char *name (archetype *arch)
2499     CODE:
2500     RETVAL = arch->name;
2501     OUTPUT: RETVAL
2502 root 1.1
2503 root 1.19 object *clone (archetype *arch)
2504     CODE:
2505     RETVAL = &arch->clone;
2506     OUTPUT: RETVAL
2507 root 1.1
2508 root 1.19 MODULE = cf PACKAGE = cf::party
2509 root 1.1
2510 root 1.19 partylist *first ()
2511 root 1.1 PROTOTYPE:
2512 root 1.19 CODE:
2513     RETVAL = get_firstparty ();
2514     OUTPUT: RETVAL
2515 root 1.1
2516 root 1.19 partylist *next (partylist *party)
2517     CODE:
2518     RETVAL = party->next;
2519     OUTPUT: RETVAL
2520 root 1.1
2521 root 1.19 const char *name (partylist *party)
2522     CODE:
2523     RETVAL = party->partyname;
2524     OUTPUT: RETVAL
2525 root 1.1
2526 root 1.19 const char *password (partylist *party)
2527     CODE:
2528     RETVAL = party->passwd;
2529     OUTPUT: RETVAL
2530 root 1.1
2531 root 1.19 MODULE = cf PACKAGE = cf::region
2532 root 1.1
2533 root 1.19 region *first ()
2534 root 1.1 PROTOTYPE:
2535 root 1.19 CODE:
2536     RETVAL = first_region;
2537     OUTPUT: RETVAL
2538 root 1.1
2539 root 1.19 region *next (region *reg)
2540     CODE:
2541     RETVAL = reg->next;
2542     OUTPUT: RETVAL
2543 root 1.1
2544 root 1.19 const char *name (region *reg)
2545     CODE:
2546     RETVAL = reg->name;
2547     OUTPUT: RETVAL
2548 root 1.1
2549 root 1.19 region *parent (region *reg)
2550     CODE:
2551     RETVAL = reg->parent;
2552     OUTPUT: RETVAL
2553 root 1.1
2554 root 1.19 const char *longname (region *reg)
2555     CODE:
2556     RETVAL = reg->longname;
2557     OUTPUT: RETVAL
2558 root 1.1
2559 root 1.19 const char *msg (region *reg)
2560     CODE:
2561     RETVAL = reg->msg;
2562     OUTPUT: RETVAL
2563 root 1.1
2564 root 1.19 MODULE = cf PACKAGE = cf::living
2565 root 1.1
2566     val64
2567     exp (living *liv, val64 new_val = 0)
2568     PROTOTYPE: $;$
2569     ALIAS:
2570     Str = 1
2571     Dex = 2
2572     Con = 3
2573     Wis = 4
2574     Cha = 5
2575     Int = 6
2576     Pow = 7
2577     wc = 8
2578     ac = 9
2579     hp = 10
2580     maxhp = 11
2581     sp = 12
2582     maxsp = 13
2583     grace = 14
2584     maxgrace = 15
2585     food = 16
2586     dam = 17
2587     luck = 18
2588     CODE:
2589     # define LIVING_ACC(acc,idx) case idx: RETVAL = liv->acc; if (items > 1) liv->acc = (sint64)new_val; break
2590     switch (ix)
2591     {
2592     LIVING_ACC (exp , 0);
2593     LIVING_ACC (Str , 1);
2594     LIVING_ACC (Dex , 2);
2595     LIVING_ACC (Con , 3);
2596     LIVING_ACC (Wis , 4);
2597     LIVING_ACC (Cha , 5);
2598     LIVING_ACC (Int , 6);
2599     LIVING_ACC (Pow , 7);
2600     LIVING_ACC (wc , 8);
2601     LIVING_ACC (ac , 9);
2602     LIVING_ACC (hp , 10);
2603     LIVING_ACC (maxhp , 11);
2604     LIVING_ACC (sp , 12);
2605     LIVING_ACC (maxsp , 13);
2606     LIVING_ACC (grace , 14);
2607     LIVING_ACC (maxgrace, 15);
2608     LIVING_ACC (food , 16);
2609     LIVING_ACC (dam , 17);
2610     LIVING_ACC (luck , 18);
2611     }
2612     # undef LIVING_ACC
2613     OUTPUT:
2614     RETVAL
2615