ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/cfperl.xs
(Generate patch)

Comparing deliantra/server/server/cfperl.xs (file contents):
Revision 1.1 by root, Tue Aug 15 17:10:46 2006 UTC vs.
Revision 1.55 by root, Tue Sep 12 18:15:34 2006 UTC

4 4
5/* 5/*
6 * This code is placed under the GNU General Public Licence (GPL) 6 * This code is placed under the GNU General Public Licence (GPL)
7 * 7 *
8 * Copyright (C) 2001-2005 by Chachkoff Yann 8 * Copyright (C) 2001-2005 by Chachkoff Yann
9 * Copyright (C) 2006 by Marc Lehmann <cf@schmorpd.e> 9 * Copyright (C) 2006 by Marc Lehmann <cf@schmorp.de>
10 * 10 *
11 * This program is free software; you can redistribute it and/or modify 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 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 13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version. 14 * (at your option) any later version.
21 * You should have received a copy of the GNU General Public License 21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software 22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 23 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24*/ 24*/
25 25
26#define PLUGIN_NAME "perl"
27#define PLUGIN_VERSION "cfperl 0.5"
28
29#include <plugin_common.h>
30#include <sounds.h>
31#include <cstdarg>
32#include <sproto.h>
33
34#include "cfperl.h"
35#include "shstr.h"
36
26#include <EXTERN.h> 37#include <EXTERN.h>
27#include <perl.h> 38#include <perl.h>
28#include <XSUB.h> 39#include <XSUB.h>
29 40
30#undef save_long // clashes with libproto.h
31
32#define PLUGIN_NAME "perl"
33#define PLUGIN_VERSION "cfperl 0.3"
34
35#ifndef __CEXTRACT__
36#include <plugin.h>
37#endif
38
39#undef MODULEAPI
40#ifdef WIN32
41#else
42#define MODULEAPI
43#endif
44
45#include <plugin_common.h>
46#include <sounds.h>
47
48#include <stdarg.h>
49
50#include <sproto.h>
51
52//#include "EventAPI.h"
53#include "perlxsi.c" 41#include "perlxsi.c"
54
55static void *globalEventListener (int *type, ...);
56 42
57extern sint64 *levels; // the experience table 43extern sint64 *levels; // the experience table
58 44
59typedef object object_ornull; 45typedef object object_ornull;
60typedef mapstruct mapstruct_ornull; 46typedef mapstruct mapstruct_ornull;
61 47
62typedef double val64; 48typedef double val64;
63#define newSVval64 newSVnv 49#define newSVval64 newSVnv
64#define SvVAL64 SvNV 50#define SvVAL64 SvNV
65 51
66static f_plug_api gethook; 52static f_plug_api gethook = cfapi_get_hooks;
67static f_plug_api registerGlobalEvent; 53static f_plug_api object_set_property = cfapi_object_set_property;
68static f_plug_api unregisterGlobalEvent; 54static f_plug_api object_insert = cfapi_object_insert;
69static f_plug_api systemDirectory;
70static f_plug_api object_set_property;
71static f_plug_api map_get_map;
72static f_plug_api object_insert;
73
74/* this is a stupid way to do things, and awkward to use for plug-in authors */
75typedef struct
76{
77 object* who;
78 object* activator;
79 object* third;
80 object* event;
81 mapstruct* map;
82 char message[1024];
83 int fix; // seems to be python-only, and should not be part of the API
84 int event_code;
85 char extension[1024]; // name field, should invoke specific perl extension
86 char options[1024]; // slaying field of event_connectors
87 int returnvalue;
88} CFPContext;
89 55
90static HV *obj_cache; 56static HV *obj_cache;
91static PerlInterpreter *perl; 57static PerlInterpreter *perl;
58
59static AV *cb_global, *cb_object, *cb_player, *cb_type, *cb_map;
92 60
93#define PUSHcfapi(type,value) PUSHs (sv_2mortal (newSVcfapi (CFAPI_ ## type, (value)))) 61#define PUSHcfapi(type,value) PUSHs (sv_2mortal (newSVcfapi (CFAPI_ ## type, (value))))
94#define PUSHcfapi_va(type,ctype) PUSHcfapi (type, va_arg (args, ctype)) 62#define PUSHcfapi_va(type,ctype) PUSHcfapi (type, va_arg (args, ctype))
95#define PUSH_OB PUSHcfapi_va(POBJECT, object *) 63#define PUSH_OB PUSHcfapi_va(POBJECT, object *)
96#define PUSH_PL PUSHcfapi_va(PPLAYER, player *) 64#define PUSH_PL PUSHcfapi_va(PPLAYER, player *)
98#define PUSH_PV PUSHcfapi_va(STRING, const char *) 66#define PUSH_PV PUSHcfapi_va(STRING, const char *)
99#define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int)))) 67#define PUSH_IV PUSHs (sv_2mortal (newSViv (va_arg (args, int))))
100 68
101////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 69//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
102 70
103// garbage collect some perl objects, if possible
104// all objects no longer referenced and empty are
105// eligible for destruction.
106static void
107clean_obj_cache ()
108{
109 static int count;
110
111 if (++count & 7)
112 return;
113
114 int todo = 1000;
115 do
116 {
117 I32 klen;
118 char *key;
119 HE *he = hv_iternext (obj_cache);
120
121 if (he)
122 {
123 SV *sv = hv_iterval (obj_cache, he);
124
125 // empty and unreferenced? nuke it
126 if (SvREFCNT (sv) == 1 && SvREFCNT (SvRV (sv)) == 1 && !HvFILL ((HV *)(SvRV (sv))))
127 {
128 hv_delete (obj_cache, HeKEY (he), HeKLEN (he), G_DISCARD);
129 todo++;
130 }
131 }
132 else
133 break;
134 }
135 while (--todo);
136}
137
138static SV * 71static SV *
139newSVptr (void *ptr, const char *klass) 72newSVptr (void *ptr, const char *klass, HV *hv = newHV ())
140{ 73{
141 SV *sv; 74 SV *sv;
142 75
143 if (!ptr) 76 if (!ptr)
144 return &PL_sv_undef; 77 return &PL_sv_undef;
145 78
146 sv = newSV (0);
147 sv_magic (sv, 0, PERL_MAGIC_ext, (char *)ptr, 0); 79 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
148 return sv_bless (newRV_noinc (sv), gv_stashpv (klass, 1)); 80 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
81}
82
83template<class attachable>
84SV *
85newSVattachable (attachable *obj, const char *klass)
86{
87 if (!obj)
88 return &PL_sv_undef;
89
90 if (!obj->self)
91 obj->self = newSVptr (obj, klass);
92
93 return newSVsv (obj->self);
149} 94}
150 95
151static void 96static void
152SVptr_cache_set (void *ptr, SV *sv) 97SVptr_cache_set (void *ptr, SV *sv)
153{ 98{
215 return SvPTR (sv, klass); 160 return SvPTR (sv, klass);
216 else 161 else
217 return 0; 162 return 0;
218} 163}
219 164
165inline SV *to_sv (const shstr & v) { return v ? newSVpvn ((const char *)v, v.length ()) : &PL_sv_undef; }
166inline SV *to_sv (const char * v) { return newSVpv (v, 0); }
167inline SV *to_sv (bool v) { return newSViv (v); }
168inline SV *to_sv ( signed char v) { return newSViv (v); }
169inline SV *to_sv (unsigned char v) { return newSViv (v); }
170inline SV *to_sv ( signed short v) { return newSViv (v); }
171inline SV *to_sv (unsigned short v) { return newSVuv (v); }
172inline SV *to_sv ( signed int v) { return newSViv (v); }
173inline SV *to_sv (unsigned int v) { return newSVuv (v); }
174inline SV *to_sv ( signed long v) { return newSViv (v); }
175inline SV *to_sv (unsigned long v) { return newSVuv (v); }
176inline SV *to_sv ( signed long long v) { return newSVval64 (v); }
177inline SV *to_sv (unsigned long long v) { return newSVval64 (v); }
178inline SV *to_sv (float v) { return newSVnv (v); }
179inline SV *to_sv (double v) { return newSVnv (v); }
180inline SV *to_sv (player * v) { return newSVattachable (v, "cf::player::wrap"); }
181inline SV *to_sv (object * v) { return newSVattachable (v, v && v->type == PLAYER ? "cf::object::player::wrap" : "cf::object::wrap"); }
182inline SV *to_sv (mapstruct * v) { return newSVattachable (v, "cf::map::wrap"); }
183inline SV *to_sv (archetype * v) { return newSVptr (v, "cf::arch::wrap"); }
184inline SV *to_sv (partylist * v) { return newSVptr (v, "cf::party::wrap"); }
185inline SV *to_sv (region * v) { return newSVptr (v, "cf::region::wrap"); }
186inline SV *to_sv (living * v) { return newSVptr (v, "cf::living::wrap"); }
187
188inline SV *to_sv (object & v) { return to_sv (&v); }
189inline SV *to_sv (living & v) { return to_sv (&v); }
190
191//TODO:
192inline SV *to_sv (New_Face * v) { return to_sv (v->name); }
193inline SV *to_sv (treasurelist * v) { return to_sv (v->name); }
194
195inline void sv_to (SV *sv, shstr &v) { v = SvPV_nolen (sv); }
196inline void sv_to (SV *sv, char * &v) { free (v); v = strdup (SvPV_nolen (sv)); } //TODO: verify that all simple pointers are strdup-managed
197inline void sv_to (SV *sv, bool &v) { v = SvIV (sv); }
198inline void sv_to (SV *sv, signed char &v) { v = SvIV (sv); }
199inline void sv_to (SV *sv, unsigned char &v) { v = SvIV (sv); }
200inline void sv_to (SV *sv, signed short &v) { v = SvIV (sv); }
201inline void sv_to (SV *sv, unsigned short &v) { v = SvIV (sv); }
202inline void sv_to (SV *sv, signed int &v) { v = SvIV (sv); }
203inline void sv_to (SV *sv, unsigned int &v) { v = SvUV (sv); }
204inline void sv_to (SV *sv, signed long &v) { v = SvIV (sv); }
205inline void sv_to (SV *sv, unsigned long &v) { v = SvUV (sv); }
206inline void sv_to (SV *sv, signed long long &v) { v = ( signed long long)SvVAL64 (sv); }
207inline void sv_to (SV *sv, unsigned long long &v) { v = (unsigned long long)SvVAL64 (sv); }
208inline void sv_to (SV *sv, float &v) { v = SvNV (sv); }
209inline void sv_to (SV *sv, double &v) { v = SvNV (sv); }
210inline void sv_to (SV *sv, player * &v) { v = (player *)SvPTR_ornull (sv, "cf::player"); }
211inline void sv_to (SV *sv, object * &v) { v = (object *)SvPTR_ornull (sv, "cf::object"); }
212inline void sv_to (SV *sv, archetype * &v) { v = (archetype *)SvPTR_ornull (sv, "cf::arch"); }
213inline void sv_to (SV *sv, mapstruct * &v) { v = (mapstruct *)SvPTR_ornull (sv, "cf::map"); }
214inline void sv_to (SV *sv, partylist * &v) { v = (partylist *)SvPTR_ornull (sv, "cf::party"); }
215inline void sv_to (SV *sv, region * &v) { v = (region *)SvPTR_ornull (sv, "cf::region"); }
216inline void sv_to (SV *sv, living * &v) { v = (living *)SvPTR_ornull (sv, "cf::living"); }
217
218inline void sv_to (SV *sv, New_Face * &v) { v = &new_faces[FindFace (SvPV_nolen (sv), 0)]; } //TODO
219inline void sv_to (SV *sv, treasurelist * &v) { v = find_treasurelist (SvPV_nolen (sv)); } // TODO
220
221template<class T>
222inline void sv_to (SV *sv, refptr<T> &v) { T *tmp; sv_to (sv, tmp); v = tmp; }
223
224template<int N>
225inline void sv_to (SV *sv, char (&v)[N]) { assign (v, SvPV_nolen (sv)); }
226
220static SV * 227static SV *
221newSVcfapi (int type, ...) 228newSVdt_va (va_list &ap, data_type type)
222{ 229{
223 SV *sv; 230 SV *sv;
224 231
225 va_list args;
226 va_start (args, type);
227
228 switch (type) 232 switch (type)
229 { 233 {
230#if 0
231 case CFAPI_INT16: 234 case DT_INT:
232 sv = newSViv (*va_arg (args, sint16_t *)); 235 sv = newSViv (va_arg (ap, int));
233 break; 236 break;
234#endif
235 237
236 case CFAPI_INT: 238 case DT_INT64:
237 sv = newSViv (*va_arg (args, int *)); 239 sv = newSVval64 ((val64)va_arg (ap, sint64));
238 break; 240 break;
239 241
240 case CFAPI_LONG: 242 case DT_DOUBLE:
241 sv = newSVval64 ((val64)*va_arg (args, sint64 *)); 243 sv = newSVnv (va_arg (ap, double));
242 break; 244 break;
243 245
244 case CFAPI_DOUBLE:
245 sv = newSVnv (*va_arg (args, double *));
246 break;
247
248 case CFAPI_STRING: 246 case DT_STRING:
249 { 247 {
250 char *str = va_arg (args, char *); 248 char *str = (char *)va_arg (ap, const char *);
251 sv = str ? newSVpv (str, 0) : &PL_sv_undef; 249 sv = str ? newSVpv (str, 0) : &PL_sv_undef;
252 } 250 }
253 break; 251 break;
254 252
255 case CFAPI_POBJECT: 253 case DT_DATA:
256 { 254 {
257 object *obj = va_arg (args, object *); 255 char *str = (char *)va_arg (ap, const void *);
258 256 int len = va_arg (ap, int);
259 if (!obj) 257 sv = str ? newSVpv (str, len) : &PL_sv_undef;
260 sv = &PL_sv_undef;
261 else
262 switch (obj->type)
263 {
264 case MAP:
265 sv = newSVptr_cached (obj, "cf::object::map::wrap");
266 break;
267
268 case PLAYER:
269 sv = newSVptr_cached (obj, "cf::object::player::wrap");
270 break;
271
272 default:
273 sv = newSVptr_cached (obj, "cf::object::wrap");
274 break;
275 }
276 } 258 }
277 break; 259 break;
278 260
279 case CFAPI_PMAP: 261 case DT_OBJECT:
280 sv = newSVptr (va_arg (args, mapstruct *), "cf::map::wrap"); 262 sv = to_sv (va_arg (ap, object *));
281 break; 263 break;
282 264
283 case CFAPI_PPLAYER: 265 case DT_MAP:
284 sv = newSVptr (va_arg (args, player *), "cf::player::wrap"); 266 // va_arg (object *) when void * is passed is an XSI extension
267 sv = to_sv (va_arg (ap, mapstruct *));
285 break; 268 break;
286 269
287 case CFAPI_PARCH: 270 case DT_PLAYER:
288 sv = newSVptr (va_arg (args, archetype *), "cf::arch::wrap"); 271 sv = to_sv (va_arg (ap, player *));
289 break; 272 break;
290 273
291 case CFAPI_PPARTY: 274 case DT_ARCH:
292 sv = newSVptr (va_arg (args, partylist *), "cf::party::wrap"); 275 sv = to_sv (va_arg (ap, archetype *));
293 break; 276 break;
294 277
295 case CFAPI_PREGION: 278 case DT_PARTY:
296 sv = newSVptr (va_arg (args, region *), "cf::region::wrap"); 279 sv = to_sv (va_arg (ap, partylist *));
297 break; 280 break;
281
282 case DT_REGION:
283 sv = to_sv (va_arg (ap, region *));
284 break;
285
286 default:
287 assert (("unhandled type in newSVdt_va", 0));
288 }
289
290 return sv;
291}
292
293static SV *
294newSVdt (data_type type, ...)
295{
296 va_list ap;
297
298 va_start (ap, type);
299 SV *sv = newSVdt_va (ap, type);
300 va_end (ap);
301
302 return sv;
303}
304
305static SV *
306newSVcfapi (int type, ...)
307{
308 SV *sv;
309
310 va_list ap;
311 va_start (ap, type);
312
313 switch (type)
314 {
315 case CFAPI_INT: sv = newSViv (*va_arg (ap, int * )); break;
316 case CFAPI_LONG: sv = newSVval64 (*va_arg (ap, sint64 *)); break;
317 case CFAPI_DOUBLE: sv = newSVnv (*va_arg (ap, double *)); break;
318 case CFAPI_STRING: sv = newSVdt_va (ap, DT_STRING); break;
319 case CFAPI_POBJECT: sv = newSVdt_va (ap, DT_OBJECT); break;
320 case CFAPI_PMAP: sv = newSVdt_va (ap, DT_MAP ); break;
321 case CFAPI_PPLAYER: sv = newSVdt_va (ap, DT_PLAYER); break;
322 case CFAPI_PARCH: sv = newSVdt_va (ap, DT_ARCH ); break;
323 case CFAPI_PPARTY: sv = newSVdt_va (ap, DT_PARTY ); break;
324 case CFAPI_PREGION: sv = newSVdt_va (ap, DT_REGION); break;
298 325
299 default: 326 default:
300 assert (("unhandled type in newSVcfapi", 0)); 327 assert (("unhandled type in newSVcfapi", 0));
301 } 328 }
302 329
303 va_end (args); 330 va_end (ap);
304 331
305 return sv; 332 return sv;
306} 333}
307 334
335//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
336
337SV *
338registry_of (attachable_base *ext)
339{
340 if (!ext->cb)
341 ext->cb = newAV ();
342
343 return newRV_inc ((SV *)ext->cb);
344}
345
346void attachable_base::clear ()
347{
348 if (self)
349 {
350 if (cb)
351 if (SvROK (*av_fetch (cb, EVENT_OBJECT_DESTROY, 1)))
352 INVOKE_OBJECT (DESTROY, static_cast<object *>(this));
353 else if (SvROK (*av_fetch (cb, EVENT_MAP_DESTROY, 1)))
354 INVOKE_MAP (DESTROY, static_cast<mapstruct *>(this));
355
356 // disconnect Perl from C, to avoid crashes
357 sv_unmagic (SvRV ((SV *)self), PERL_MAGIC_ext);
358
359 // clear the perl hash, might or might not be a good idea
360 hv_clear ((HV *)SvRV ((SV *)self));
361
362 SvREFCNT_dec (self);
363 self = 0;
364 }
365
366 if (cb)
367 {
368 SvREFCNT_dec (cb);
369 cb = 0;
370 }
371
372 attach = 0;
373}
374
375void attachable_base::optimise ()
376{
377 if (!self)
378 return;
379
380 HV *hv = (HV *)SvRV ((SV *)self);
381
382 if (SvREFCNT ((SV *)self) == 1
383 && SvREFCNT ((SV *)hv) == 1
384 && !HvKEYS (hv))
385 {
386 SvREFCNT_dec ((SV *)self);
387 self = 0;
388 }
389}
390
391void attachable_base::instantiate (data_type type, void *obj)
392{
393 dSP;
394 ENTER;
395 SAVETMPS;
396 PUSHMARK (SP);
397 EXTEND (SP, 2);
398 PUSHs (sv_2mortal (newSVdt (type, obj)));
399 PUSHs (sv_2mortal (newSVpv (attach, 0)));
400
401 attach = 0;
402
403 PUTBACK;
404 call_pv ("cf::instantiate", G_DISCARD | G_VOID | G_EVAL);
405 FREETMPS;
406 LEAVE;
407
408 switch (type)
409 {
410 case DT_OBJECT: INVOKE_OBJECT (INSTANTIATE, obj); break;
411 case DT_MAP: INVOKE_MAP (INSTANTIATE, obj); break;
412 }
413}
414
308///////////////////////////////////////////////////////////////////////////// 415/////////////////////////////////////////////////////////////////////////////
309 416
417void reattach (data_type type, void *obj)
418{
419 //TODO only do this when the object has _attachment's
420
421 dSP;
422 ENTER;
423 SAVETMPS;
424 PUSHMARK (SP);
425 XPUSHs (sv_2mortal (newSVdt (type, obj)));
426 PUTBACK;
427 call_pv ("cf::reattach", G_DISCARD | G_VOID | G_EVAL);
428 FREETMPS;
429 LEAVE;
430
431 switch (type)
432 {
433 case DT_OBJECT: INVOKE_OBJECT (REATTACH, obj); break;
434 case DT_PLAYER: INVOKE_PLAYER (REATTACH, obj); break;
435 case DT_MAP: INVOKE_MAP (REATTACH, obj); break;
436 }
437}
438
439template<class subclass>
440void reattach (attachable<subclass> *obj)
441{
442 obj->optimise ();
443
444 if (obj->self)
445 reattach ((data_type) cftype<subclass>::dt, (subclass *)obj);
446}
447
448#include "kw_hash.h"
449
450object_freezer::object_freezer ()
451: dynbuf (128 * 1024, 64 * 1024)
452{
453 av = newAV ();
454}
455
456object_freezer::~object_freezer ()
457{
458 SvREFCNT_dec (av);
459}
460
461void object_freezer::put (attachable_base *ext)
462{
463 ext->optimise ();
464
465 if (ext->self)
466 {
467 int idx = AvFILLp ((AV *)av) + 1;
468 av_store (av, idx, SvREFCNT_inc (ext->self));
469
470 add ((void *)"oid ", 4);
471 add ((sint32)idx);
472 add ('\n');
473 }
474}
475
476bool object_freezer::save (const char *filename)
477{
478 dSP;
479 ENTER;
480 SAVETMPS;
481 PUSHMARK (SP);
482 EXTEND (SP, 3);
483 PUSHs (sv_2mortal (newSVpv (filename, 0)));
484 PUSHs (sv_2mortal (newRV_noinc (newSVpvn ((char *)linearise (), size ()))));
485 PUSHs (sv_2mortal (newRV_inc ((SV *)av)));
486 PUTBACK;
487 call_pv ("cf::object_freezer_save", G_VOID | G_DISCARD | G_EVAL);
488 FREETMPS;
489 LEAVE;
490}
491
492int fprintf (object_freezer &freezer, const char *format, ...)
493{
494 va_list ap;
495
496 va_start (ap, format);
497
498 int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap);
499
500 if (len >= 0)
501 freezer.alloc (len);
502
503 va_end (ap);
504}
505
506int fputs (const char *s, object_freezer &freezer)
507{
508 freezer.add (s);
509}
510
511object_thawer::object_thawer (const char *filename)
512{
513 static const char eof[] = "\n\n\n\0\0\0";
514
515 av = 0;
516 text = 0;
517 line = 0;
518
519 if (filename)
520 {
521 dSP;
522 ENTER;
523 SAVETMPS;
524 PUSHMARK (SP);
525 XPUSHs (sv_2mortal (newSVpv (filename, 0)));
526 PUTBACK;
527
528 if (2 == call_pv ("cf::object_thawer_load", G_ARRAY | G_EVAL))
529 {
530 SPAGAIN;
531
532 // second value - perl objects
533 {
534 SV *sv = POPs;
535 if (SvROK (sv))
536 av = (AV *)SvREFCNT_inc (SvRV (sv));
537 }
538
539 // first value - text part, pad with 3 zeroes
540 {
541 SV *sv = POPs;
542 STRLEN len;
543 char *sv_ = SvPVbyte (sv, len);
544 text = newSV (len + sizeof (eof));
545 SvCUR_set (text, len);
546 memcpy (SvPVX (text), sv_, len);
547 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure
548
549 line = SvPVX (text);
550 }
551 }
552
553 PUTBACK;
554 FREETMPS;
555 LEAVE;
556 }
557}
558
559void object_thawer::get (data_type type, void *obj, attachable_base *ext, int oid)
560{
561 if (!av || oid < 0) // this is actually an error of sorts
562 return;
563
564 // we have to "re-instantiate"/reattach to an object, so nuke ext->attach
565 ext->clear ();
566
567 SV **svp = av_fetch ((AV *)av, oid, 0);
568
569 if (!svp || !SvROK (*svp))
570 {
571 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
572 return;
573 }
574
575 ext->self = *svp; *svp = &PL_sv_undef;
576 sv_magic (SvRV (ext->self), 0, PERL_MAGIC_ext, (char *)obj, 0);
577
578 reattach (type, obj);
579}
580
581object_thawer::~object_thawer ()
582{
583 if (text) SvREFCNT_dec (text);
584 if (av) SvREFCNT_dec (av);
585}
586
587char *fgets (char *s, int n, object_thawer &thawer)
588{
589 char *p = thawer.line;
590 char *q = s;
591
592 if (!p)
593 return 0;
594
595 while (--n)
596 {
597 if (!*p)
598 break;
599
600 *q++ = *p;
601
602 if (*p++ == '\n')
603 break;
604 }
605
606 *q = 0;
607 thawer.line = p;
608
609 return s == q ? 0 : s;
610}
611
612keyword object_thawer::get_kv ()
613{
614 if (!line)
615 return KW_EOF;
616
617 for (;;)
618 {
619 char *p = line;
620
621 if (!*p)
622 return KW_EOF;
623
624 // parse keyword
625 while (*p > ' ')
626 p++;
627
628 int klen = p - line;
629
630 if (*p++ != '\n')
631 {
632 // parse value
633 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20
634 ++p;
635
636 last_value = p;
637
638 while (*p != '\n')
639 p++;
640
641 *p++ = 0;
642 }
643 else
644 last_value = 0;
645
646 line [klen] = 0;
647 keyword_idx *kw = kw_lex::match (line, klen);
648
649 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D
650
651 if (kw)
652 {
653 line = p;
654 return kw->index;
655 }
656 else if (!*line || *line == '#')
657 {
658 // empty/comment line
659 line = p;
660 }
661 else
662 return KW_ERROR;
663 }
664}
665
666void object_thawer::get (shstr &sh) const
667{
668 if (last_value)
669 sh = last_value;
670 else
671 {
672 sh = "<value missing>";
673 LOG (llevError, "keyword requires value: <%s>\n", line);//TODO: add filename
674 }
675}
676
677void object_thawer::get_ml (keyword kend, shstr &sh)
678{
679 char kw[128];
680
681 int klen = keyword_len [kend];
682
683 kw [0] = '\n';
684 memcpy (kw + 1, keyword_str [kend], klen);
685 kw [klen + 1] = '\n';
686 kw [klen + 2] = 0;
687
688 // first test for completely empty msg... "endXXX\n"
689 if (!strncmp (line, kw + 1, klen + 1))
690 {
691 sh = 0;
692
693 line += klen + 1;
694
695 return;
696 }
697 else
698 {
699 // multi-line strings are delimited by "\nendXXX\n" or "endXXX\n" (NULL)
700
701 char *end = strstr (line, kw);
702
703 if (!end)
704 {
705 sh = 0;
706 return;
707 }
708
709 *end = 0;
710 sh = line;
711
712 line = end + keyword_len [kend] + 1;
713
714 while (*line++ != '\n')
715 ;
716 }
717}
718
719sint32 object_thawer::get_sint32 () const
720{
721 char *p = last_value;
722
723 if (!p)
724 return 0;
725
726 sint32 val = 0;
727 bool negate;
728
729 if (*p == '-')
730 {
731 negate = true;
732 ++p;
733 }
734 else
735 negate = false;
736
737 do
738 {
739 val *= 10;
740 val += *p++ - '0';
741 }
742 while (*p);
743
744 return negate ? -val : val;
745}
746
747sint64 object_thawer::get_sint64 () const
748{
749 return last_value ? atoll (last_value) : 0;
750}
751
752double object_thawer::get_double () const
753{
754 return last_value ? atof (last_value) : 0;
755}
756
757/////////////////////////////////////////////////////////////////////////////
758
310extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr) 759extern "C" int cfperl_initPlugin (const char *iversion, f_plug_api gethooksptr)
311{ 760{
312 gethook = gethooksptr;
313 printf (PLUGIN_VERSION " init\n");
314
315 return 0; 761 return 0;
316} 762}
317 763
318static CommArray_s rtn_cmd; 764static CommArray_s rtn_cmd;
319 765
399extern "C" int cfperl_postInitPlugin () 845extern "C" int cfperl_postInitPlugin ()
400{ 846{
401 int hooktype = 1; 847 int hooktype = 1;
402 int rtype = 0; 848 int rtype = 0;
403 849
404 printf (PLUGIN_VERSION " post init\n");
405
406 registerGlobalEvent = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_register_global_event");
407 unregisterGlobalEvent = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_unregister_global_event");
408 systemDirectory = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_system_directory");
409 object_set_property = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_set_property");
410 map_get_map = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_map_get_map");
411 object_insert = (void* (*)(int*, ...)) gethook (&rtype, hooktype, "cfapi_object_insert");
412
413 cf_init_plugin (gethook); 850 cf_init_plugin (gethook);
414 851
415 /* Pick the global events you want to monitor from this plugin */ 852 return 0;
416 registerGlobalEvent (NULL, EVENT_BORN, PLUGIN_NAME, globalEventListener); 853}
417 registerGlobalEvent (NULL, EVENT_CLOCK, PLUGIN_NAME, globalEventListener); 854
418 //registerGlobalEvent (NULL, EVENT_CRASH, PLUGIN_NAME, globalEventListener); 855extern "C" int cfperl_closePlugin ()
419 registerGlobalEvent (NULL, EVENT_FIND_UNARMED_SKILL, PLUGIN_NAME, globalEventListener); 856{
420 registerGlobalEvent (NULL, EVENT_PLAYER_USE_SKILL, PLUGIN_NAME, globalEventListener); 857 return 0;
421 registerGlobalEvent (NULL, EVENT_CAST_SPELL, PLUGIN_NAME, globalEventListener); 858}
422 registerGlobalEvent (NULL, EVENT_MONSTER_USE_SKILL, PLUGIN_NAME, globalEventListener); 859
423 registerGlobalEvent (NULL, EVENT_PLAYER_DEATH, PLUGIN_NAME, globalEventListener); 860void
424 registerGlobalEvent (NULL, EVENT_GKILL, PLUGIN_NAME, globalEventListener); 861cfperl_init ()
425 registerGlobalEvent (NULL, EVENT_LOGIN, PLUGIN_NAME, globalEventListener); 862{
426 registerGlobalEvent (NULL, EVENT_LOGOUT, PLUGIN_NAME, globalEventListener); 863 PERL_SYS_INIT3 (&settings.argc, &settings.argv, 0);
427 registerGlobalEvent (NULL, EVENT_MAPENTER, PLUGIN_NAME, globalEventListener); 864 perl = perl_alloc ();
428 registerGlobalEvent (NULL, EVENT_MAPLEAVE, PLUGIN_NAME, globalEventListener); 865 perl_construct (perl);
429 registerGlobalEvent (NULL, EVENT_MAPRESET, PLUGIN_NAME, globalEventListener); 866
430 registerGlobalEvent (NULL, EVENT_MAPLOAD, PLUGIN_NAME, globalEventListener); 867 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
431 registerGlobalEvent (NULL, EVENT_MAPOUT, PLUGIN_NAME, globalEventListener);
432 registerGlobalEvent (NULL, EVENT_MAPIN, PLUGIN_NAME, globalEventListener);
433 registerGlobalEvent (NULL, EVENT_MAPCLEAN, PLUGIN_NAME, globalEventListener);
434 registerGlobalEvent (NULL, EVENT_REMOVE, PLUGIN_NAME, globalEventListener);
435 registerGlobalEvent (NULL, EVENT_SHOUT, PLUGIN_NAME, globalEventListener);
436 registerGlobalEvent (NULL, EVENT_TELL, PLUGIN_NAME, globalEventListener);
437 registerGlobalEvent (NULL, EVENT_MUZZLE, PLUGIN_NAME, globalEventListener);
438 registerGlobalEvent (NULL, EVENT_KICK, PLUGIN_NAME, globalEventListener);
439 registerGlobalEvent (NULL, EVENT_FREE_OB, PLUGIN_NAME, globalEventListener);
440 registerGlobalEvent (NULL, EVENT_PLAYER_LOAD, PLUGIN_NAME, globalEventListener);
441 registerGlobalEvent (NULL, EVENT_PLAYER_SAVE, PLUGIN_NAME, globalEventListener);
442 registerGlobalEvent (NULL, EVENT_EXTCMD, PLUGIN_NAME, globalEventListener);
443 868
444 char *argv[] = { 869 char *argv[] = {
445 "", 870 "",
446 "-e" 871 "-e"
447 "BEGIN {"
448 " cf->bootstrap;" 872 "cf->bootstrap;"
449 " unshift @INC, cf::datadir ();" 873 "unshift @INC, cf::datadir ();"
450 "}"
451 ""
452 "use cf;" 874 "require cf;"
453 }; 875 };
454 876
455 perl = perl_alloc ();
456 perl_construct (perl);
457
458 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
459
460 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) 877 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl))
461 { 878 {
462 printf ("unable to initialize perl-interpreter, continuing without.\n"); 879 printf ("unable to initialize perl-interpreter, aborting.\n");
463 880 exit (EXIT_FAILURE);
464 perl_destruct (perl);
465 perl_free (perl);
466 perl = 0;
467 }
468 else
469 { 881 }
882
470 obj_cache = newHV (); 883 obj_cache = newHV ();
471 }
472
473 return 0;
474} 884}
475 885
886void cfperl_main ()
887{
888 dSP;
889
890 PUSHMARK (SP);
891 PUTBACK;
892 call_pv ("cf::main", G_DISCARD | G_VOID);
893}
894
895static event_klass klass_of[NUM_EVENT_TYPES] = {
896# define def(type,name) KLASS_ ## type,
897# include "eventinc.h"
898# undef def
899};
900
901#define KLASS_OF(event) (((unsigned int)event) < NUM_EVENT_TYPES ? klass_of [event] : KLASS_NONE)
902
476static void * 903static void
477globalEventListener (int *type, ...) 904gather_callbacks (AV *&callbacks, AV *registry, event_type event)
478{ 905{
479 va_list args; 906 // event must be in array
480 static int rv; 907 if (event >= 0 && event <= AvFILLp (registry))
481 int event_code;
482
483 if (!perl)
484 return NULL;
485
486 va_start (args, type);
487 event_code = va_arg (args, int);
488
489 if (event_code == EVENT_FREE_OB)
490 {
491 player *pl;
492 object *op;
493 SV *sv;
494
495 op = va_arg (args, object *);
496 sv = hv_delete (obj_cache, (char *)&op, sizeof (void *), 0);
497
498 if (sv)
499 clearSVptr (sv);
500
501 rv = 0;
502 } 908 {
503 else if (event_code == EVENT_CLOCK) 909 SV *cbs_ = AvARRAY (registry)[event];
504 {
505 dSP;
506 int i, count;
507 910
508 clean_obj_cache (); 911 // element must be list of callback entries
509 912 if (cbs_ && SvROK (cbs_) && SvTYPE (SvRV (cbs_)) == SVt_PVAV)
510 ENTER;
511 SAVETMPS;
512
513 // service up to 8 events per tick better would be
514 // to check for elapsed time and stop processing after
515 // 0.25 * server_tick or so
516 for (i = 9; --i; )
517 { 913 {
518 PUSHMARK (SP); 914 AV *cbs = (AV *)SvRV (cbs_);
519 XPUSHs (sv_2mortal (newSViv (0)));
520 PUTBACK;
521 count = call_pv ("Event::one_event", G_SCALAR | G_EVAL);
522 SPAGAIN;
523 915
524 if (!count || !POPi) 916 // no callback entries, no callbacks to call
917 if (AvFILLp (cbs) >= 0)
525 break; 918 {
919 if (!callbacks)
920 {
921 callbacks = newAV ();
922 av_extend (callbacks, 16);
923 }
924
925 // never use SvREFCNT_inc to copy values, but its ok here :)
926 for (int i = 0; i <= AvFILLp (cbs); ++i)
927 av_push (callbacks, SvREFCNT_inc (AvARRAY (cbs)[i]));
928 }
526 } 929 }
527
528 FREETMPS;
529 LEAVE;
530 }
531 else
532 { 930 }
931}
932
933bool cfperl_invoke (event_type event, ...)
934{
935 data_type dt;
936 va_list ap;
937
938 va_start (ap, event);
939
940 AV *callbacks = 0;
941
942 object *op;
943 player *pl;
944 mapstruct *map;
945
946 // callback call ordering is:
947 // 1. per-object callback (NYI)
948 // 2. per-class object
949 // 2a. per-type callback
950 // 4. global callbacks
951
952 gather_callbacks (callbacks, cb_global, event);
953
954 switch (KLASS_OF (event))
955 {
956 case KLASS_GLOBAL:
957 break;
958
959 case KLASS_OBJECT:
960 dt = (data_type) va_arg (ap, int);
961 assert (("first argument must be of type object", dt == DT_OBJECT));
962 op = va_arg (ap, object *);
963
964 if (op->cb)
965 gather_callbacks (callbacks, op->cb, event);
966
967 if (op->type)
968 {
969 if (op->subtype && op->type + op->subtype * NUM_SUBTYPES <= AvFILLp (cb_type))
970 {
971 SV *registry = AvARRAY (cb_type)[op->type + op->subtype * NUM_SUBTYPES];
972
973 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
974 gather_callbacks (callbacks, (AV *)SvRV (registry), event);
975 }
976
977 if (op->type <= AvFILLp (cb_type))
978 {
979 SV *registry = AvARRAY (cb_type)[op->type];
980
981 if (registry && SvROK (registry) && SvTYPE (SvRV (registry)) == SVt_PVAV)
982 gather_callbacks (callbacks, (AV *)SvRV (registry), event);
983 }
984 }
985
986 gather_callbacks (callbacks, cb_object, event);
987
988 break;
989
990 case KLASS_PLAYER:
991 dt = (data_type) va_arg (ap, int);
992 assert (("first argument must be of type player", dt == DT_PLAYER));
993 pl = va_arg (ap, player *);
994
995 if (pl->cb)
996 gather_callbacks (callbacks, pl->cb, event);
997
998 gather_callbacks (callbacks, cb_player, event);
999 break;
1000
1001 case KLASS_MAP:
1002 dt = (data_type) va_arg (ap, int);
1003 assert (("first argument must be of type object", dt == DT_MAP));
1004 map = va_arg (ap, mapstruct *);
1005
1006 if (map->cb)
1007 gather_callbacks (callbacks, map->cb, event);
1008
1009 gather_callbacks (callbacks, cb_map, event);
1010 break;
1011
1012 default:
1013 assert (("unsupported event klass in cfperl_invoke", 0));
1014 }
1015
1016 // short-circuit processing if no callbacks found/defined
1017 if (!callbacks)
1018 return 0;
1019
533 dSP; 1020 dSP;
534
535 ENTER; 1021 ENTER;
536 SAVETMPS; 1022 SAVETMPS;
537 1023
538 PUSHMARK (SP); 1024 PUSHMARK (SP);
539
540 EXTEND (SP, 10); 1025 EXTEND (SP, 3);
541 PUSHs (sv_2mortal (newSViv (event_code)));
542 1026
543 switch (event_code) 1027 PUSHs (sv_2mortal (newSViv (event))); // only used for debugging nowadays
1028 PUSHs (sv_2mortal (newRV_noinc ((SV *)callbacks)));
1029
1030 switch (KLASS_OF (event))
1031 {
1032 case KLASS_OBJECT: PUSHs (sv_2mortal (newSVdt (DT_OBJECT, op))); break;
1033 case KLASS_PLAYER: PUSHs (sv_2mortal (newSVdt (DT_PLAYER, pl))); break;
1034 case KLASS_MAP: PUSHs (sv_2mortal (newSVdt (DT_MAP, map))); break;
1035 }
1036
1037 for (;;)
1038 {
1039 dt = (data_type) va_arg (ap, int);
1040
1041 if (dt == DT_END)
1042 break;
1043
1044 if (dt == DT_AV)
544 { 1045 {
545 case EVENT_CRASH: 1046 AV *av = va_arg (ap, AV *);
546 break;
547 1047
548 case EVENT_PLAYER_LOAD: 1048 for (int i = 0; i <= av_len (av); ++i)
549 case EVENT_PLAYER_SAVE: 1049 XPUSHs (*av_fetch (av, i, 1));
550 PUSH_OB;
551 PUSH_PV;
552 break;
553
554 case EVENT_MAPLOAD:
555 case EVENT_MAPOUT:
556 case EVENT_MAPIN:
557 case EVENT_MAPCLEAN:
558 case EVENT_MAPRESET:
559 PUSH_MAP;
560 break;
561
562 case EVENT_MAPENTER:
563 case EVENT_MAPLEAVE:
564 case EVENT_BORN:
565 case EVENT_REMOVE:
566 case EVENT_PLAYER_DEATH:
567 PUSH_OB;
568 break;
569
570 case EVENT_GKILL:
571 PUSH_OB;
572 PUSH_OB;
573 break;
574
575 case EVENT_LOGIN:
576 case EVENT_LOGOUT:
577 PUSH_PL;
578 PUSH_PV;
579 break;
580
581 case EVENT_SHOUT:
582 case EVENT_MUZZLE:
583 case EVENT_KICK:
584 PUSH_OB;
585 PUSH_PV;
586 break;
587
588 case EVENT_FIND_UNARMED_SKILL:
589 PUSH_OB;
590 break;
591
592 case EVENT_PLAYER_USE_SKILL:
593 case EVENT_MONSTER_USE_SKILL:
594 case EVENT_CAST_SPELL:
595 PUSH_OB;
596 PUSH_OB;
597 PUSH_OB;
598 PUSH_IV;
599 PUSH_PV;
600 break;
601
602 case EVENT_EXTCMD:
603 PUSH_PL;
604 {
605 char *buf = va_arg (args, char *);
606 int len = va_arg (args, int);
607 PUSHs (sv_2mortal (newSVpvn (buf, len)));
608 }
609 break;
610
611 case EVENT_TELL:
612 break;
613 } 1050 }
614 1051 else
615 va_end (args); 1052 XPUSHs (sv_2mortal (newSVdt_va (ap, dt)));
616
617 PUTBACK;
618 int count = call_pv ("cf::inject_global_event", G_SCALAR | G_EVAL);
619 SPAGAIN;
620
621 if (SvTRUE (ERRSV))
622 LOG (llevError, "global event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV));
623
624 rv = count > 0 ? POPi : 0;
625
626 PUTBACK;
627 FREETMPS;
628 LEAVE;
629 } 1053 }
630 1054
631 return &rv;
632}
633
634extern "C" void *cfperl_eventListener (int *type, ...)
635{
636 static int rv;
637 va_list args;
638 int event_code;
639 object *who, *activator, *third, *event;
640 char *message, *extension, *options;
641
642 if (!perl)
643 return NULL;
644
645 va_start (args, type);
646 who = va_arg (args, object *);
647 event_code = va_arg (args, int);
648 activator = va_arg (args, object *);
649 third = va_arg (args, object *);
650 message = va_arg (args, char *);
651 va_arg (args, int); // fix yourself
652 extension = va_arg (args, char *);
653 options = va_arg (args, char *);
654 event = va_arg (args, object *);
655 va_end (args); 1055 va_end (ap);
656 1056
657 {
658 dSP;
659
660 ENTER;
661 SAVETMPS;
662
663 PUSHMARK (SP);
664 EXTEND (SP, 10);
665
666 PUSHcfapi (STRING, extension);
667 PUSHs (sv_2mortal (newSViv (event_code)));
668
669 PUSHcfapi (POBJECT, event);
670 PUSHcfapi (POBJECT, who);
671
672 switch (event_code)
673 {
674 case EVENT_STOP: // $ob (e.g. arrow)
675 case EVENT_TIME: // $ob
676 case EVENT_TIMER: // $ob
677 break;
678
679 case EVENT_APPLY: // $ob, $who
680 case EVENT_DROP: // $ob, $who
681 case EVENT_CLOSE: // $ob, $who
682 case EVENT_DEATH: // $ob[, $killer]
683 case EVENT_MOVE: // $ob, $enemy
684 case EVENT_THROW: // $ob, $thrower
685 PUSHcfapi (POBJECT, activator);
686 break;
687
688 case EVENT_DROP_ON: // $ob, $who, $what
689 case EVENT_ATTACK: // $ob, $who, $victim (?? please god enlighten me)
690 PUSHcfapi (POBJECT, activator);
691 PUSHcfapi (POBJECT, third);
692 break;
693
694 case EVENT_TRIGGER: // $ob, $originator, [$victim], [$msg]
695 PUSHcfapi (POBJECT, activator);
696 PUSHcfapi (POBJECT, third);
697 PUSHcfapi (POBJECT, message);
698 break;
699
700 case EVENT_SAY: // $ob, $who, $msg
701 PUSHcfapi (POBJECT, activator);
702 PUSHcfapi (STRING, message);
703 break;
704
705 default:
706 LOG (llevError, "perl plugin called for unsupported event type %d", event_code);
707 break;
708 }
709
710 PUTBACK; 1057 PUTBACK;
711 int count = call_pv ("cf::inject_event", G_SCALAR | G_EVAL); 1058 int count = call_pv ("cf::do_invoke", G_SCALAR | G_EVAL);
712 SPAGAIN; 1059 SPAGAIN;
713 1060
714 if (SvTRUE (ERRSV))
715 LOG (llevError, "event '%d' callback evaluation error: %s", event_code, SvPV_nolen (ERRSV));
716
717 rv = count > 0 ? POPi : 0; 1061 count = count > 0 ? POPi : 0;
718 1062
719 PUTBACK;
720 FREETMPS; 1063 FREETMPS;
721 LEAVE; 1064 LEAVE;
722 } 1065
723 1066 return count;
1067}
1068
1069SV *
1070cfperl_result (int idx)
1071{
1072 AV *av = get_av ("cfperl::invoke_results", 0);
1073 if (!av)
1074 return &PL_sv_undef;
1075
1076 SV **sv = av_fetch (av, idx, 0);
1077 if (!sv)
1078 return &PL_sv_undef;
1079
724 return &rv; 1080 return *sv;
725} 1081}
726 1082
727extern "C" int cfperl_closePlugin () 1083int
1084cfperl_result_INT (int idx)
728{ 1085{
729 printf (PLUGIN_VERSION " closing\n"); 1086 return SvIV (cfperl_result (idx));
730
731 if (perl)
732 {
733 perl_destruct (perl);
734 perl_free (perl);
735 perl = 0;
736 }
737
738 return 0;
739} 1087}
740 1088
741MODULE = cf PACKAGE = cf PREFIX = cf_ 1089MODULE = cf PACKAGE = cf PREFIX = cf_
742 1090
743BOOT: 1091BOOT:
752 const_iv (llevError) 1100 const_iv (llevError)
753 const_iv (llevInfo) 1101 const_iv (llevInfo)
754 const_iv (llevDebug) 1102 const_iv (llevDebug)
755 const_iv (llevMonster) 1103 const_iv (llevMonster)
756 1104
1105 const_iv (MAX_TIME)
757 const_iv (PLAYER) 1106 const_iv (PLAYER)
758 const_iv (TRANSPORT) 1107 const_iv (TRANSPORT)
759 const_iv (ROD) 1108 const_iv (ROD)
760 const_iv (TREASURE) 1109 const_iv (TREASURE)
761 const_iv (POTION) 1110 const_iv (POTION)
876 const_iv (BUILDER) 1225 const_iv (BUILDER)
877 const_iv (MATERIAL) 1226 const_iv (MATERIAL)
878 const_iv (ITEM_TRANSFORMER) 1227 const_iv (ITEM_TRANSFORMER)
879 const_iv (QUEST) 1228 const_iv (QUEST)
880 1229
1230 const_iv (NUM_SUBTYPES)
1231
881 const_iv (ST_BD_BUILD) 1232 const_iv (ST_BD_BUILD)
882 const_iv (ST_BD_REMOVE) 1233 const_iv (ST_BD_REMOVE)
883 1234
884 const_iv (ST_MAT_FLOOR) 1235 const_iv (ST_MAT_FLOOR)
885 const_iv (ST_MAT_WALL) 1236 const_iv (ST_MAT_WALL)
909 const_iv (AT_HOLYWORD) 1260 const_iv (AT_HOLYWORD)
910 const_iv (AT_BLIND) 1261 const_iv (AT_BLIND)
911 const_iv (AT_INTERNAL) 1262 const_iv (AT_INTERNAL)
912 const_iv (AT_LIFE_STEALING) 1263 const_iv (AT_LIFE_STEALING)
913 const_iv (AT_DISEASE) 1264 const_iv (AT_DISEASE)
914
915 const_iv (QUEST_IN_PROGRESS)
916 const_iv (QUEST_DONE_QUEST)
917 const_iv (QUEST_DONE_TASK)
918 const_iv (QUEST_START_QUEST)
919 const_iv (QUEST_END_QUEST)
920 const_iv (QUEST_START_TASK)
921 const_iv (QUEST_END_TASK)
922 const_iv (QUEST_OVERRIDE)
923 const_iv (QUEST_ON_ACTIVATE)
924 1265
925 const_iv (WEAP_HIT) 1266 const_iv (WEAP_HIT)
926 const_iv (WEAP_SLASH) 1267 const_iv (WEAP_SLASH)
927 const_iv (WEAP_PIERCE) 1268 const_iv (WEAP_PIERCE)
928 const_iv (WEAP_CLEAVE) 1269 const_iv (WEAP_CLEAVE)
1113 1454
1114 const_iv (SK_EXP_ADD_SKILL) 1455 const_iv (SK_EXP_ADD_SKILL)
1115 const_iv (SK_EXP_TOTAL) 1456 const_iv (SK_EXP_TOTAL)
1116 const_iv (SK_EXP_NONE) 1457 const_iv (SK_EXP_NONE)
1117 const_iv (SK_SUBTRACT_SKILL_EXP) 1458 const_iv (SK_SUBTRACT_SKILL_EXP)
1459 const_iv (SK_EXP_SKILL_ONLY)
1118 1460
1119 const_iv (SK_LOCKPICKING) 1461 const_iv (SK_LOCKPICKING)
1120 const_iv (SK_HIDING) 1462 const_iv (SK_HIDING)
1121 const_iv (SK_SMITHERY) 1463 const_iv (SK_SMITHERY)
1122 const_iv (SK_BOWYER) 1464 const_iv (SK_BOWYER)
1221 1563
1222 const_iv (MAP_IN_MEMORY) 1564 const_iv (MAP_IN_MEMORY)
1223 const_iv (MAP_SWAPPED) 1565 const_iv (MAP_SWAPPED)
1224 const_iv (MAP_LOADING) 1566 const_iv (MAP_LOADING)
1225 const_iv (MAP_SAVING) 1567 const_iv (MAP_SAVING)
1568
1569 const_iv (KLASS_GLOBAL)
1570 const_iv (KLASS_OBJECT)
1571 const_iv (KLASS_PLAYER)
1572 const_iv (KLASS_MAP)
1226 }; 1573 };
1227 1574
1228 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1575 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
1229 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1576 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
1230 1577
1231 static const struct { 1578 static const struct {
1232 const char *name; 1579 const char *name;
1580 int skip;
1581 IV klass;
1233 IV iv; 1582 IV iv;
1234 } *event, event_list[] = { 1583 } *eiv, event_iv[] = {
1235# define const_event(name) { # name, (IV)EVENT_ ## name }, 1584# define def(klass,name) { "EVENT_" # klass "_" # name, sizeof ("EVENT_" # klass), (IV)KLASS_ ## klass, (IV)EVENT_ ## klass ## _ ## name },
1236 const_event (NONE) 1585# include "eventinc.h"
1237 const_event (APPLY) 1586# undef def
1238 const_event (ATTACK)
1239 const_event (DEATH)
1240 const_event (DROP)
1241 const_event (DROP_ON)
1242 const_event (PICKUP)
1243 const_event (SAY)
1244 const_event (STOP)
1245 const_event (TIME)
1246 const_event (THROW)
1247 const_event (TRIGGER)
1248 const_event (CLOSE)
1249 const_event (TIMER)
1250 const_event (MOVE)
1251
1252 const_event (BORN)
1253 //const_event (CLOCK)
1254 const_event (CRASH)
1255 const_event (PLAYER_DEATH)
1256 const_event (PLAYER_LOAD)
1257 const_event (PLAYER_SAVE)
1258 const_event (GKILL)
1259 const_event (LOGIN)
1260 const_event (LOGOUT)
1261 const_event (MAPENTER)
1262 const_event (MAPLEAVE)
1263 const_event (MAPRESET)
1264 const_event (MAPLOAD)
1265 const_event (MAPOUT)
1266 const_event (MAPIN)
1267 const_event (MAPCLEAN)
1268 const_event (REMOVE)
1269 const_event (SHOUT)
1270 const_event (TELL)
1271 const_event (MUZZLE)
1272 const_event (KICK)
1273 const_event (PLAYER_USE_SKILL)
1274 const_event (MONSTER_USE_SKILL)
1275 const_event (CAST_SPELL)
1276 const_event (FIND_UNARMED_SKILL)
1277 const_event (EXTCMD)
1278 //const_event (FREE_OB)
1279 }; 1587 };
1280 1588
1281 AV *av = get_av ("cf::EVENT", 1); 1589 AV *av = get_av ("cf::EVENT", 1);
1282 1590
1283 for (event = event_list + sizeof (event_list) / sizeof (event_list [0]); event-- > event_list; ) 1591 for (eiv = event_iv + sizeof (event_iv) / sizeof (event_iv [0]); eiv-- > event_iv; )
1284 av_store (av, event->iv, newSVpv ((char *)event->name, 0));
1285
1286 static const struct {
1287 int dtype;
1288 const char *name;
1289 IV idx;
1290 } *cprop, prop_table[] = {
1291# define prop(type, name) { type, # name, (IV) CFAPI_ ## name },
1292 prop (CFAPI_INT, MAP_PROP_FLAGS)
1293 prop (CFAPI_INT, MAP_PROP_DIFFICULTY)
1294 prop (CFAPI_STRING, MAP_PROP_PATH)
1295 prop (CFAPI_STRING, MAP_PROP_TMPNAME)
1296 prop (CFAPI_STRING, MAP_PROP_NAME)
1297 prop (CFAPI_INT, MAP_PROP_RESET_TIME)
1298 prop (CFAPI_INT, MAP_PROP_RESET_TIMEOUT)
1299 prop (CFAPI_INT, MAP_PROP_PLAYERS)
1300 prop (CFAPI_INT, MAP_PROP_DARKNESS)
1301 prop (CFAPI_INT, MAP_PROP_WIDTH)
1302 prop (CFAPI_INT, MAP_PROP_HEIGHT)
1303 prop (CFAPI_INT, MAP_PROP_ENTER_X)
1304 prop (CFAPI_INT, MAP_PROP_ENTER_Y)
1305 prop (CFAPI_INT, MAP_PROP_TEMPERATURE)
1306 prop (CFAPI_INT, MAP_PROP_PRESSURE)
1307 prop (CFAPI_INT, MAP_PROP_HUMIDITY)
1308 prop (CFAPI_INT, MAP_PROP_WINDSPEED)
1309 prop (CFAPI_INT, MAP_PROP_WINDDIR)
1310 prop (CFAPI_INT, MAP_PROP_SKY)
1311 prop (CFAPI_INT, MAP_PROP_WPARTX)
1312 prop (CFAPI_INT, MAP_PROP_WPARTY)
1313 prop (CFAPI_STRING, MAP_PROP_MESSAGE)
1314 prop (CFAPI_PMAP, MAP_PROP_NEXT)
1315 prop (CFAPI_PREGION, MAP_PROP_REGION)
1316 prop (CFAPI_POBJECT, OBJECT_PROP_OB_ABOVE)
1317 prop (CFAPI_POBJECT, OBJECT_PROP_OB_BELOW)
1318 prop (CFAPI_POBJECT, OBJECT_PROP_NEXT_ACTIVE_OB)
1319 prop (CFAPI_POBJECT, OBJECT_PROP_PREV_ACTIVE_OB)
1320 prop (CFAPI_POBJECT, OBJECT_PROP_INVENTORY)
1321 prop (CFAPI_POBJECT, OBJECT_PROP_ENVIRONMENT)
1322 prop (CFAPI_POBJECT, OBJECT_PROP_HEAD)
1323 prop (CFAPI_POBJECT, OBJECT_PROP_CONTAINER)
1324 prop (CFAPI_PMAP, OBJECT_PROP_MAP)
1325 prop (CFAPI_INT, OBJECT_PROP_COUNT)
1326 prop (CFAPI_INT, OBJECT_PROP_REFCOUNT)
1327 prop (CFAPI_STRING, OBJECT_PROP_NAME)
1328 prop (CFAPI_STRING, OBJECT_PROP_NAME_PLURAL)
1329 prop (CFAPI_STRING, OBJECT_PROP_TITLE)
1330 prop (CFAPI_STRING, OBJECT_PROP_RACE)
1331 prop (CFAPI_STRING, OBJECT_PROP_SLAYING)
1332 prop (CFAPI_STRING, OBJECT_PROP_SKILL)
1333 prop (CFAPI_STRING, OBJECT_PROP_MESSAGE)
1334 prop (CFAPI_STRING, OBJECT_PROP_LORE)
1335 prop (CFAPI_INT, OBJECT_PROP_X)
1336 prop (CFAPI_INT, OBJECT_PROP_Y)
1337 prop (CFAPI_DOUBLE, OBJECT_PROP_SPEED)
1338 prop (CFAPI_DOUBLE, OBJECT_PROP_SPEED_LEFT)
1339 prop (CFAPI_INT, OBJECT_PROP_NROF)
1340 prop (CFAPI_INT, OBJECT_PROP_DIRECTION)
1341 prop (CFAPI_INT, OBJECT_PROP_FACING)
1342 prop (CFAPI_INT, OBJECT_PROP_TYPE)
1343 prop (CFAPI_INT, OBJECT_PROP_SUBTYPE)
1344 prop (CFAPI_INT, OBJECT_PROP_CLIENT_TYPE)
1345 prop (CFAPI_INT, OBJECT_PROP_ATTACK_TYPE)
1346 prop (CFAPI_INT, OBJECT_PROP_PATH_ATTUNED)
1347 prop (CFAPI_INT, OBJECT_PROP_PATH_REPELLED)
1348 prop (CFAPI_INT, OBJECT_PROP_PATH_DENIED)
1349 prop (CFAPI_INT, OBJECT_PROP_MATERIAL)
1350 prop (CFAPI_STRING, OBJECT_PROP_MATERIAL_NAME)
1351 prop (CFAPI_INT, OBJECT_PROP_MAGIC)
1352 prop (CFAPI_INT, OBJECT_PROP_VALUE)
1353 prop (CFAPI_INT, OBJECT_PROP_LEVEL)
1354 prop (CFAPI_INT, OBJECT_PROP_LAST_HEAL)
1355 prop (CFAPI_INT, OBJECT_PROP_LAST_SP)
1356 prop (CFAPI_INT, OBJECT_PROP_LAST_GRACE)
1357 prop (CFAPI_INT, OBJECT_PROP_LAST_EAT)
1358 prop (CFAPI_INT, OBJECT_PROP_INVISIBLE_TIME)
1359 prop (CFAPI_INT, OBJECT_PROP_PICK_UP)
1360 prop (CFAPI_INT, OBJECT_PROP_ITEM_POWER)
1361 prop (CFAPI_INT, OBJECT_PROP_GEN_SP_ARMOUR)
1362 prop (CFAPI_INT, OBJECT_PROP_WEIGHT)
1363 prop (CFAPI_INT, OBJECT_PROP_WEIGHT_LIMIT)
1364 prop (CFAPI_INT, OBJECT_PROP_CARRYING)
1365 prop (CFAPI_INT, OBJECT_PROP_GLOW_RADIUS)
1366 prop (CFAPI_LONG, OBJECT_PROP_PERM_EXP)
1367 prop (CFAPI_POBJECT, OBJECT_PROP_CURRENT_WEAPON)
1368 prop (CFAPI_POBJECT, OBJECT_PROP_ENEMY)
1369 prop (CFAPI_POBJECT, OBJECT_PROP_ATTACKED_BY)
1370 prop (CFAPI_INT, OBJECT_PROP_RUN_AWAY)
1371 prop (CFAPI_POBJECT, OBJECT_PROP_CHOSEN_SKILL)
1372 prop (CFAPI_INT, OBJECT_PROP_HIDDEN)
1373 prop (CFAPI_INT, OBJECT_PROP_MOVE_STATUS)
1374 prop (CFAPI_INT, OBJECT_PROP_MOVE_TYPE)
1375 prop (CFAPI_POBJECT, OBJECT_PROP_SPELL_ITEM)
1376 prop (CFAPI_DOUBLE, OBJECT_PROP_EXP_MULTIPLIER)
1377 prop (CFAPI_PARCH, OBJECT_PROP_ARCHETYPE)
1378 prop (CFAPI_PARCH, OBJECT_PROP_OTHER_ARCH)
1379 prop (CFAPI_STRING, OBJECT_PROP_CUSTOM_NAME)
1380 prop (CFAPI_INT, OBJECT_PROP_ANIM_SPEED)
1381 prop (CFAPI_INT, OBJECT_PROP_FRIENDLY)
1382 prop (CFAPI_STRING, OBJECT_PROP_SHORT_NAME)
1383 prop (CFAPI_INT, OBJECT_PROP_MAGICAL)
1384 prop (CFAPI_INT, OBJECT_PROP_LUCK)
1385 prop (CFAPI_POBJECT, OBJECT_PROP_OWNER)
1386 prop (CFAPI_POBJECT, OBJECT_PROP_PRESENT)
1387 prop (CFAPI_INT, OBJECT_PROP_CHEATER)
1388 prop (CFAPI_INT, OBJECT_PROP_MERGEABLE)
1389 prop (CFAPI_INT, OBJECT_PROP_PICKABLE)
1390 prop (CFAPI_INT, OBJECT_PROP_STR)
1391 prop (CFAPI_INT, OBJECT_PROP_DEX)
1392 prop (CFAPI_INT, OBJECT_PROP_CON)
1393 prop (CFAPI_INT, OBJECT_PROP_WIS)
1394 prop (CFAPI_INT, OBJECT_PROP_INT)
1395 prop (CFAPI_INT, OBJECT_PROP_POW)
1396 prop (CFAPI_INT, OBJECT_PROP_CHA)
1397 prop (CFAPI_INT, OBJECT_PROP_WC)
1398 prop (CFAPI_INT, OBJECT_PROP_AC)
1399 prop (CFAPI_INT, OBJECT_PROP_HP)
1400 prop (CFAPI_INT, OBJECT_PROP_SP)
1401 prop (CFAPI_INT, OBJECT_PROP_GP)
1402 prop (CFAPI_INT, OBJECT_PROP_FP)
1403 prop (CFAPI_INT, OBJECT_PROP_MAXHP)
1404 prop (CFAPI_INT, OBJECT_PROP_MAXSP)
1405 prop (CFAPI_INT, OBJECT_PROP_MAXGP)
1406 prop (CFAPI_INT, OBJECT_PROP_DAM)
1407 prop (CFAPI_STRING, OBJECT_PROP_GOD)
1408 prop (CFAPI_STRING, OBJECT_PROP_ARCH_NAME)
1409 prop (CFAPI_INT, OBJECT_PROP_INVISIBLE)
1410 prop (CFAPI_INT, OBJECT_PROP_FACE)
1411 };
1412
1413 HV *prop_type = get_hv ("cf::PROP_TYPE", 1);
1414 HV *prop_idx = get_hv ("cf::PROP_IDX", 1);
1415
1416 for (cprop = prop_table + sizeof (prop_table) / sizeof (prop_table [0]); cprop-- > prop_table; )
1417 { 1592 {
1418 hv_store (prop_type, cprop->name, strlen (cprop->name), newSViv (cprop->dtype), 0); 1593 AV *event = newAV ();
1419 hv_store (prop_idx, cprop->name, strlen (cprop->name), newSViv (cprop->idx ), 0); 1594 av_push (event, newSVpv ((char *)eiv->name + eiv->skip, 0));
1595 av_push (event, newSViv (eiv->klass));
1596 av_store (av, eiv->iv, newRV_noinc ((SV *)event));
1597 newCONSTSUB (stash, (char *)eiv->name, newSViv (eiv->iv));
1420 } 1598 }
1421 1599
1422 //I_EVENT_API (PACKAGE); 1600 //I_EVENT_API (PACKAGE);
1423} 1601}
1424 1602
1603void _init_vars ()
1604 CODE:
1605 cb_global = get_av ("cf::CB_GLOBAL", 1);
1606 cb_object = get_av ("cf::CB_OBJECT", 1);
1607 cb_player = get_av ("cf::CB_PLAYER", 1);
1608 cb_type = get_av ("cf::CB_TYPE" , 1);
1609 cb_map = get_av ("cf::CB_MAP" , 1);
1610
1611void _global_reattach ()
1612 CODE:
1613{
1614 // reattach to all attachable objects in the game.
1615 for (player *pl = first_player; pl; pl = pl->next)
1616 reattach (pl);
1617
1618 for (mapstruct *map = first_map; map; map = map->next)
1619 reattach (map);
1620
1621 for (object *op = objects; op; op = op->next)
1622 reattach (op);
1623}
1624
1425NV floor (NV x) 1625NV floor (NV x)
1426 1626
1427NV ceil (NV x) 1627NV ceil (NV x)
1628
1629void server_tick ()
1428 1630
1429void 1631void
1430LOG (int level, char *msg) 1632LOG (int level, char *msg)
1431 PROTOTYPE: $$ 1633 PROTOTYPE: $$
1432 C_ARGS: (LogLevel)level, "%s", msg 1634 C_ARGS: (LogLevel)level, "%s", msg
1435 PROTOTYPE: $$ 1637 PROTOTYPE: $$
1436 1638
1437char *path_combine_and_normalize (char *base, char *path) 1639char *path_combine_and_normalize (char *base, char *path)
1438 PROTOTYPE: $$ 1640 PROTOTYPE: $$
1439 1641
1440char * 1642const char *
1441cf_get_maps_directory (char *path) 1643get_maps_directory (char *path)
1442 PROTOTYPE: $ 1644 PROTOTYPE: $
1443 ALIAS: maps_directory = 0 1645 ALIAS: maps_directory = 0
1646 CODE:
1647 RETVAL = create_pathname (path);
1648 OUTPUT: RETVAL
1444 1649
1445void 1650void
1446sub_generation_inc () 1651sub_generation_inc ()
1447 CODE: 1652 CODE:
1448 PL_sub_generation++; 1653 PL_sub_generation++;
1457 confdir = 3 1662 confdir = 3
1458 localdir = 4 1663 localdir = 4
1459 playerdir = 5 1664 playerdir = 5
1460 datadir = 6 1665 datadir = 6
1461 CODE: 1666 CODE:
1462{ 1667 switch (ix)
1463 int unused_type; 1668 {
1464 RETVAL = (char *)systemDirectory (&unused_type, ix); 1669 case 0: RETVAL = settings.mapdir ; break;
1465} 1670 case 1: RETVAL = settings.uniquedir; break;
1671 case 2: RETVAL = settings.tmpdir ; break;
1672 case 3: RETVAL = settings.confdir ; break;
1673 case 4: RETVAL = settings.localdir ; break;
1674 case 5: RETVAL = settings.playerdir; break;
1675 case 6: RETVAL = settings.datadir ; break;
1676 }
1466 OUTPUT: RETVAL 1677 OUTPUT: RETVAL
1467 1678
1468int 1679int
1469cf_find_animation (char *text) 1680cf_find_animation (char *text)
1470 PROTOTYPE: $ 1681 PROTOTYPE: $
1471 1682
1472int random_roll(int min, int max, object *op, int goodbad); 1683int random_roll(int min, int max, object *op, int goodbad);
1473 1684
1474const char *cost_string_from_value(uint64 cost, int approx = 0) 1685const char *cost_string_from_value(uint64 cost, int approx = 0)
1686
1687int invoke (int event, ...)
1688 CODE:
1689 if (KLASS_OF (event) != KLASS_GLOBAL) croak ("event class must be GLOBAL");
1690 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1691 for (int i = 1; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1692 RETVAL = INVOKE_((event_type)event, ARG_AV (av));
1693 OUTPUT: RETVAL
1475 1694
1476int 1695int
1477exp_to_level (val64 exp) 1696exp_to_level (val64 exp)
1478 CODE: 1697 CODE:
1479{ 1698{
1510 RETVAL = newSVpv (resist_plus[atnr], 0); 1729 RETVAL = newSVpv (resist_plus[atnr], 0);
1511 else 1730 else
1512 XSRETURN_UNDEF; 1731 XSRETURN_UNDEF;
1513 OUTPUT: RETVAL 1732 OUTPUT: RETVAL
1514 1733
1734int
1735_valid (SV *obj)
1736 CODE:
1737 RETVAL = SvROK (obj) && mg_find (SvRV (obj), PERL_MAGIC_ext);
1738 OUTPUT:
1739 RETVAL
1740
1515MODULE = cf PACKAGE = cf::object PREFIX = cf_object_ 1741MODULE = cf PACKAGE = cf::object PREFIX = cf_object_
1516 1742
1517SV * 1743int invoke (object *op, int event, ...)
1518get_property (object *obj, int type, int idx)
1519 CODE: 1744 CODE:
1520 RETVAL = newSVcfapi (type, cf_object_get_property (obj, idx)); 1745 if (KLASS_OF (event) != KLASS_OBJECT) croak ("event class must be OBJECT");
1746 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1747 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1748 RETVAL = INVOKE_((event_type)event, ARG_OBJECT (op), ARG_AV (av));
1521 OUTPUT: RETVAL 1749 OUTPUT: RETVAL
1522 1750
1523SV * 1751SV *registry (object *op)
1524set_property (object *obj, int type, int idx, SV *newval)
1525 CODE: 1752 CODE:
1526 switch (type) 1753 RETVAL = registry_of (op);
1527 { 1754 OUTPUT:
1528 case CFAPI_INT: 1755 RETVAL
1529 cf_object_set_int_property (obj, idx, SvIV (newval)); 1756
1530 break; 1757INCLUDE: $PERL genacc object ../include/object.h |
1531 case CFAPI_LONG:
1532 cf_object_set_long_property (obj, idx, (long) SvVAL64 (newval));
1533 break;
1534 case CFAPI_DOUBLE:
1535 {
1536 int unused_type;
1537 object_set_property (&unused_type, obj, idx, (double)SvNV (newval));
1538 }
1539 break;
1540 case CFAPI_STRING:
1541 cf_object_set_string_property (obj, idx, SvOK (newval) ? SvPV_nolen (newval) : 0);
1542 break;
1543 case CFAPI_POBJECT:
1544 {
1545 int unused_type;
1546 object_set_property (&unused_type, obj, idx, (object *)SvPTR_ornull (newval, "cf::object"));
1547 }
1548 break;
1549 default:
1550 croak ("unhandled type '%d' in set_property '%d'", type, idx);
1551 }
1552 1758
1553# missing properties 1759# missing properties
1554 1760
1555void 1761int flag (object *op, int flag, int value = 1)
1556set_attacktype (object *obj, U32 attacktype) 1762 PROTOTYPE: $$;$
1557 CODE: 1763 CODE:
1558 obj->attacktype = attacktype; 1764 RETVAL = QUERY_FLAG (op, flag);
1559 1765 if (items >= 3)
1560U32 1766 if (value)
1561get_attacktype (object *obj) 1767 SET_FLAG (op, flag);
1562 ALIAS: 1768 else
1563 attacktype = 0 1769 CLEAR_FLAG (op, flag);
1564 CODE:
1565 RETVAL = obj->attacktype;
1566 OUTPUT: RETVAL 1770 OUTPUT: RETVAL
1567 1771
1568# missing in plug-in api, of course 1772object *head (object *op)
1569void 1773 PROTOTYPE: $
1570set_food (object *obj, int food)
1571 CODE: 1774 CODE:
1572 obj->stats.food = food; 1775 RETVAL = op->head ? op->head : op;
1776 OUTPUT: RETVAL
1573 1777
1574int 1778int is_head (object *op)
1575get_food (object *obj) 1779 PROTOTYPE: $
1576 ALIAS: 1780 CODE:
1577 food = 0
1578 CODE:
1579 RETVAL = obj->stats.food; 1781 RETVAL = !op->head;
1580 OUTPUT: RETVAL 1782 OUTPUT: RETVAL
1581 1783
1582void 1784void
1583inv (object *obj) 1785inv (object *obj)
1584 PROTOTYPE: $ 1786 PROTOTYPE: $
1585 PPCODE: 1787 PPCODE:
1590} 1792}
1591 1793
1592int cf_object_get_resistance (object *op, int rtype) 1794int cf_object_get_resistance (object *op, int rtype)
1593 ALIAS: resistance = 0 1795 ALIAS: resistance = 0
1594 1796
1595int cf_object_get_flag (object *op, int flag)
1596 ALIAS: flag = 0
1597
1598void cf_object_set_flag (object *op, int flag, int value)
1599
1600int need_identify (const object *obj); 1797int need_identify (const object *obj);
1601 1798
1602int apply_shop_mat (object *shop_mat, object *op); 1799int apply_shop_mat (object *shop_mat, object *op);
1603 1800
1604void cf_object_move (object *op, int dir, object *originator = op) 1801int move (object *op, int dir, object *originator = op)
1802 CODE:
1803 RETVAL = move_ob (op, dir, originator);
1804 OUTPUT:
1805 RETVAL
1605 1806
1606void cf_object_apply (object *op, object *author, int flags = 0) 1807void cf_object_apply (object *op, object *author, int flags = 0)
1607 1808
1608void cf_object_apply_below (object *op) 1809void cf_object_apply_below (object *op)
1609 1810
1651 1852
1652void cf_object_drop (object *op, object *author) 1853void cf_object_drop (object *op, object *author)
1653 1854
1654void cf_object_take (object *op, object *author) 1855void cf_object_take (object *op, object *author)
1655 1856
1656void cf_object_say (object *op, char *msg)
1657
1658void cf_object_speak (object *op, char *msg)
1659
1660object *cf_object_insert_object (object *op, object *container) 1857object *cf_object_insert_object (object *op, object *container)
1661
1662const char *cf_object_get_msg (object *ob)
1663 ALIAS: msg = 0
1664 1858
1665object *cf_object_insert_in_ob (object *ob, object *where) 1859object *cf_object_insert_in_ob (object *ob, object *where)
1666 1860
1667int cf_object_teleport (object *op, mapstruct *map, int x, int y) 1861int cf_object_teleport (object *op, mapstruct *map, int x, int y)
1668 1862
1764base_name (object *ob, int plural) 1958base_name (object *ob, int plural)
1765 CODE: 1959 CODE:
1766 RETVAL = cf_query_base_name (ob, plural); 1960 RETVAL = cf_query_base_name (ob, plural);
1767 OUTPUT: RETVAL 1961 OUTPUT: RETVAL
1768 1962
1769living *
1770stats (object *ob)
1771 CODE:
1772 RETVAL = &ob->stats;
1773 OUTPUT: RETVAL
1774
1775
1776MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_ 1963MODULE = cf PACKAGE = cf::object::player PREFIX = cf_player_
1777 1964
1778player *player (object *op) 1965player *player (object *op)
1779 CODE: 1966 CODE:
1780 RETVAL = op->contr; 1967 RETVAL = op->contr;
1797 1984
1798void cf_player_set_party (object *op, partylist *party) 1985void cf_player_set_party (object *op, partylist *party)
1799 1986
1800void kill_player (object *op) 1987void kill_player (object *op)
1801 1988
1802MODULE = cf PACKAGE = cf::object::map PREFIX = cf_
1803
1804MODULE = cf PACKAGE = cf::player PREFIX = cf_player_ 1989MODULE = cf PACKAGE = cf::player PREFIX = cf_player_
1990
1991int invoke (player *pl, int event, ...)
1992 CODE:
1993 if (KLASS_OF (event) != KLASS_PLAYER) croak ("event class must be PLAYER");
1994 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
1995 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
1996 RETVAL = INVOKE_((event_type)event, ARG_PLAYER (pl), ARG_AV (av));
1997 OUTPUT: RETVAL
1998
1999SV *registry (player *pl)
2000 CODE:
2001 RETVAL = registry_of (pl);
2002 OUTPUT:
2003 RETVAL
1805 2004
1806player *cf_player_find (char *name) 2005player *cf_player_find (char *name)
1807 PROTOTYPE: $ 2006 PROTOTYPE: $
1808 2007
1809void cf_player_move (player *pl, int dir) 2008void cf_player_move (player *pl, int dir)
1810
1811void MapNewmapCmd (player *pl)
1812 2009
1813void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0); 2010void play_sound_player_only (player *pl, int soundnum, int x = 0, int y = 0);
1814 2011
1815# nonstandard 2012# nonstandard
1816object *ob (player *pl) 2013object *ob (player *pl)
1833 CODE: 2030 CODE:
1834 RETVAL = FABS (dx) <= pl->socket.mapx / 2 && FABS (dy) <= pl->socket.mapy / 2 2031 RETVAL = FABS (dx) <= pl->socket.mapx / 2 && FABS (dy) <= pl->socket.mapy / 2
1835 && !pl->blocked_los [dx + pl->socket.mapx / 2][dy + pl->socket.mapy / 2]; 2032 && !pl->blocked_los [dx + pl->socket.mapx / 2][dy + pl->socket.mapy / 2];
1836 OUTPUT: 2033 OUTPUT:
1837 RETVAL 2034 RETVAL
2035
2036char *
2037client (player *pl)
2038 CODE:
2039 RETVAL = pl->socket.client;
2040 OUTPUT:
2041 RETVAL
2042
2043char *
2044host (player *pl)
2045 CODE:
2046 RETVAL = pl->socket.host;
2047 OUTPUT:
2048 RETVAL
2049
2050char *
2051killer (player *pl, char *killer = 0)
2052 CODE:
2053 if (killer)
2054 snprintf (pl->killer, sizeof (pl->killer), "%s", killer);
2055 RETVAL = pl->killer;
2056 OUTPUT:
2057 RETVAL
2058
2059void
2060buggy_mapscroll (player *pl, int value = 1)
2061 CODE:
2062 pl->socket.buggy_mapscroll = value;
1838 2063
1839void 2064void
1840send (player *pl, SV *packet) 2065send (player *pl, SV *packet)
1841 CODE: 2066 CODE:
1842{ 2067{
1853 if (new_value >= 0) 2078 if (new_value >= 0)
1854 pl->listening = new_value; 2079 pl->listening = new_value;
1855 OUTPUT: 2080 OUTPUT:
1856 RETVAL 2081 RETVAL
1857 2082
1858void get_savebed (player *pl) 2083void savebed (player *pl, SV *map_path = 0, SV *x = 0, SV *y = 0)
1859 ALIAS: 2084 PROTOTYPE: $;$$$
1860 savebed = 0
1861 PPCODE: 2085 PPCODE:
2086 if (GIMME_V != G_VOID)
2087 {
1862 EXTEND (SP, 3); 2088 EXTEND (SP, 3);
1863 PUSHs (sv_2mortal (newSVpv (pl->savebed_map, 0))); 2089 PUSHs (sv_2mortal (newSVpv (pl->savebed_map, 0)));
1864 PUSHs (sv_2mortal (newSViv (pl->bed_x))); 2090 PUSHs (sv_2mortal (newSViv (pl->bed_x)));
1865 PUSHs (sv_2mortal (newSViv (pl->bed_y))); 2091 PUSHs (sv_2mortal (newSViv (pl->bed_y)));
1866 2092 }
1867void set_savebed (player *pl, char *map_path, int x, int y) 2093 if (map_path) sv_to (map_path, pl->savebed_map);
1868 CODE: 2094 if (x) sv_to (x, pl->bed_x);
1869 strcpy (pl->savebed_map, map_path); 2095 if (y) sv_to (y, pl->bed_y);
1870 pl->bed_x = x;
1871 pl->bed_y = y;
1872 2096
1873void 2097void
1874list () 2098list ()
1875 PPCODE: 2099 PPCODE:
1876{ 2100{
1902 OUTPUT: RETVAL 2126 OUTPUT: RETVAL
1903 2127
1904 2128
1905MODULE = cf PACKAGE = cf::map PREFIX = cf_map_ 2129MODULE = cf PACKAGE = cf::map PREFIX = cf_map_
1906 2130
1907SV * 2131mapstruct *first ()
1908get_property (mapstruct *obj, int type, int idx) 2132 PROTOTYPE:
2133 CODE:
2134 RETVAL = first_map;
2135 OUTPUT: RETVAL
2136
2137int invoke (mapstruct *map, int event, ...)
1909 CODE: 2138 CODE:
1910 RETVAL = newSVcfapi (type, cf_map_get_property (obj, idx)); 2139 if (KLASS_OF (event) != KLASS_MAP) croak ("event class must be MAP");
2140 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
2141 for (int i = 2; i < items; i++) av_push (av, SvREFCNT_inc (ST (i)));
2142 RETVAL = INVOKE_((event_type)event, ARG_MAP (map), ARG_AV (av));
1911 OUTPUT: RETVAL 2143 OUTPUT: RETVAL
1912 2144
1913SV * 2145SV *registry (mapstruct *map)
1914set_property (mapstruct *obj, int type, int idx, SV *newval)
1915 CODE: 2146 CODE:
1916 switch (type) 2147 RETVAL = registry_of (map);
1917 { 2148 OUTPUT:
1918 case CFAPI_INT: 2149 RETVAL
1919 cf_map_set_int_property (obj, idx, SvIV (newval)); 2150
1920 break; 2151INCLUDE: $PERL genacc mapstruct ../include/map.h |
1921 default:
1922 croak ("unhandled type '%d' in set_property '%d'", type, idx);
1923 }
1924 2152
1925mapstruct *new (int width, int height) 2153mapstruct *new (int width, int height)
1926 PROTOTYPE: 2154 PROTOTYPE:
1927 CODE: 2155 CODE:
1928{ 2156{
1929 int unused_type; 2157 RETVAL = get_empty_map (width, height);
1930 RETVAL = (mapstruct*) map_get_map (&unused_type, 0, width, height);
1931} 2158}
1932 OUTPUT: 2159 OUTPUT:
1933 RETVAL 2160 RETVAL
1934 2161
1935void delete_map (mapstruct *map) 2162void delete_map (mapstruct *map)
1950 XSRETURN_UNDEF; 2177 XSRETURN_UNDEF;
1951 RETVAL = map->tile_path [dir]; 2178 RETVAL = map->tile_path [dir];
1952 OUTPUT: 2179 OUTPUT:
1953 RETVAL 2180 RETVAL
1954 2181
1955mapstruct *cf_map_get_map (char *name) 2182mapstruct *ready_map_name (char *name, int flags = 0)
1956 PROTOTYPE: $ 2183 PROTOTYPE: $;$
1957 ALIAS: map = 0 2184 ALIAS:
2185 find = 0
2186 get_map = 1
1958 2187
1959mapstruct *has_been_loaded (char *name) 2188mapstruct *has_been_loaded (char *name)
1960 PROTOTYPE: $ 2189 PROTOTYPE: $
1961
1962mapstruct *cf_map_get_first ()
1963 PROTOTYPE:
1964 ALIAS: first = 0
1965
1966# whoever "designed" the plug-in api should have wasted
1967# his/her time with staying away from the project - would have
1968# saved others a lot of time, without doubt.
1969void set_path (mapstruct *where, char *path)
1970 CODE:
1971 strcpy (where->path, path);
1972 2190
1973int in_memory (mapstruct *map) 2191int in_memory (mapstruct *map)
1974 CODE: 2192 CODE:
1975 RETVAL = map->in_memory; 2193 RETVAL = map->in_memory;
1976 OUTPUT: 2194 OUTPUT:
1977 RETVAL 2195 RETVAL
1978 2196
1979bool unique (mapstruct *map) 2197void
1980 CODE: 2198trigger (mapstruct *map, long connection, bool state = true)
1981 RETVAL = map->unique; 2199 CODE:
1982 OUTPUT: 2200 activate_connection (map, connection, state);
1983 RETVAL
1984 2201
1985void set_unique (mapstruct *map, bool unique) 2202void
1986 CODE: 2203get_connection (mapstruct *map, long connection)
1987 map->unique = unique; 2204 PPCODE:
2205 oblinkpt *obp = get_connection_links (map, connection);
2206 if (obp)
2207 for (objectlink *ol = obp->link; ol; ol = ol->next)
2208 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, ol->ob)));
1988 2209
1989object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags) 2210object *cf_map_insert_object_there (mapstruct *where, object *op, object *originator, int flags)
1990 2211
1991object *cf_map_insert_object (mapstruct *where, object* op, int x, int y) 2212object *cf_map_insert_object (mapstruct *where, object* op, int x, int y)
1992 2213
1997cf_map_normalise (mapstruct *map, int x, int y) 2218cf_map_normalise (mapstruct *map, int x, int y)
1998 PPCODE: 2219 PPCODE:
1999{ 2220{
2000 mapstruct *nmap = 0; 2221 mapstruct *nmap = 0;
2001 I16 nx = 0, ny = 0; 2222 I16 nx = 0, ny = 0;
2002 int flags = cf_map_get_flags (map, &nmap, x, y, &nx, &ny); 2223 int flags = get_map_flags (map, &nmap, x, y, &nx, &ny);
2003 2224
2004 EXTEND (SP, 4); 2225 EXTEND (SP, 4);
2005 PUSHs (sv_2mortal (newSViv (flags))); 2226 PUSHs (sv_2mortal (newSViv (flags)));
2006 2227
2007 if (GIMME_V == G_ARRAY) 2228 if (GIMME_V == G_ARRAY)
2019{ 2240{
2020 object *o; 2241 object *o;
2021 mapstruct *nmap = 0; 2242 mapstruct *nmap = 0;
2022 I16 nx, ny; 2243 I16 nx, ny;
2023 2244
2024 cf_map_get_flags (map, &nmap, x, y, &nx, &ny); 2245 get_map_flags (map, &nmap, x, y, &nx, &ny);
2025 2246
2026 if (nmap) 2247 if (nmap)
2027 for (o = GET_MAP_OB (nmap, nx, ny); o; o = o->above) 2248 for (o = GET_MAP_OB (nmap, nx, ny); o; o = o->above)
2028 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o))); 2249 XPUSHs (sv_2mortal (newSVcfapi (CFAPI_POBJECT, o)));
2029} 2250}
2054 case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break; 2275 case 7: RETVAL = newSVuv ( GET_MAP_MOVE_OFF (obj, x, y)); break;
2055 } 2276 }
2056 OUTPUT: 2277 OUTPUT:
2057 RETVAL 2278 RETVAL
2058 2279
2059# "serialise" map perl data into a ref
2060void
2061_get_obs (mapstruct *map)
2062 PPCODE:
2063{
2064 object *o;
2065 int x, y;
2066 AV *obs = newAV ();
2067 int nonnull = 0;
2068 2280
2069 for (y = 0; y < MAP_HEIGHT (map); y++) 2281MODULE = cf PACKAGE = cf::arch
2070 for (x = 0; x < MAP_WIDTH (map); x++)
2071 {
2072 AV *av = newAV ();
2073 2282
2074 for (o = GET_MAP_OB (map, x, y); o; o = o->above) 2283archetype *find (const char *name)
2075 { 2284 CODE:
2076 SV *sv = SVptr_cache_get (o); 2285 RETVAL = find_archetype (name);
2286 OUTPUT:
2287 RETVAL
2077 2288
2078 if (sv && HvFILL (SvRV (sv))) 2289archetype *first()
2079 {
2080 nonnull = 1;
2081 sv = newSVsv (sv);
2082 }
2083 else
2084 sv = &PL_sv_undef;
2085
2086 av_push (av, sv);
2087 }
2088
2089 av_store (obs, x + y * MAP_HEIGHT (map), newRV_noinc ((SV *)av));
2090 }
2091
2092 if (nonnull)
2093 XPUSHs (sv_2mortal (newRV_noinc ((SV *)obs)));
2094 else
2095 SvREFCNT_dec (obs);
2096}
2097
2098# "deserialise" perl map data into the map
2099void
2100_set_obs (mapstruct *map, SV *sv)
2101 CODE:
2102{
2103 object *o;
2104 AV *av;
2105 int x, y;
2106 AV *obs = (AV *)SvRV (sv);
2107
2108 for (y = 0; y < MAP_HEIGHT (map); y++)
2109 for (x = 0; x < MAP_WIDTH (map); x++)
2110 {
2111 sv = *av_fetch (obs, x + y * MAP_HEIGHT (map), 1);
2112
2113 if (!SvROK (sv))
2114 continue;
2115
2116 av = (AV *)SvRV (sv);
2117
2118 for (o = GET_MAP_OB (map, x, y); o; o = o->above)
2119 {
2120 sv = av_shift (av);
2121
2122 if (SvROK (sv))
2123 {
2124 sv_magic ((SV *)SvRV (sv), 0, PERL_MAGIC_ext, (char *)o, 0);
2125 SVptr_cache_set (o, sv);
2126 }
2127 }
2128 }
2129}
2130
2131
2132MODULE = cf PACKAGE = cf::arch PREFIX = cf_archetype_
2133
2134archetype *cf_archetype_get_first()
2135 PROTOTYPE: 2290 PROTOTYPE:
2136 ALIAS: first = 0 2291 CODE:
2292 RETVAL = first_archetype;
2293 OUTPUT: RETVAL
2137 2294
2138archetype *cf_archetype_get_next (archetype *arch) 2295INCLUDE: $PERL genacc archetype ../include/object.h |
2139 ALIAS: next = 0
2140 2296
2141archetype *cf_archetype_get_head (archetype *arch) 2297MODULE = cf PACKAGE = cf::party
2142 ALIAS: head = 0
2143 2298
2144archetype *cf_archetype_get_more (archetype *arch) 2299partylist *first ()
2145 ALIAS: more = 0
2146
2147const char *cf_archetype_get_name (archetype *arch)
2148 ALIAS: name = 0
2149
2150object *cf_archetype_get_clone (archetype *arch)
2151 ALIAS: clone = 0
2152
2153MODULE = cf PACKAGE = cf::party PREFIX = cf_party_
2154
2155partylist *cf_party_get_first ()
2156 PROTOTYPE: 2300 PROTOTYPE:
2157 ALIAS: first = 0 2301 CODE:
2302 RETVAL = get_firstparty ();
2303 OUTPUT: RETVAL
2158 2304
2159partylist *cf_party_get_next (partylist *party) 2305partylist *next (partylist *party)
2160 ALIAS: next = 0 2306 CODE:
2307 RETVAL = party->next;
2308 OUTPUT: RETVAL
2161 2309
2162const char *cf_party_get_name (partylist *party) 2310const char *name (partylist *party)
2311 CODE:
2312 RETVAL = party->partyname;
2313 OUTPUT: RETVAL
2163 2314
2164const char *cf_party_get_password (partylist *party) 2315const char *password (partylist *party)
2165 ALIAS: password = 0 2316 CODE:
2317 RETVAL = party->passwd;
2318 OUTPUT: RETVAL
2166 2319
2167player *cf_party_get_first_player (partylist *party) 2320MODULE = cf PACKAGE = cf::region
2168 ALIAS: first_player = 0
2169 2321
2170player *cf_party_get_next_player (partylist *party, player *op) 2322region *first ()
2171 ALIAS: next_player = 0
2172
2173
2174MODULE = cf PACKAGE = cf::region PREFIX = cf_region_
2175
2176region *cf_region_get_first ()
2177 PROTOTYPE: 2323 PROTOTYPE:
2178 ALIAS: first = 0
2179
2180const char *cf_region_get_name (region *reg)
2181 ALIAS: name = 0
2182
2183region *cf_region_get_next (region *reg)
2184 ALIAS: next = 0
2185
2186region *cf_region_get_parent (region *reg)
2187 ALIAS: parent = 0
2188
2189const char *cf_region_get_longname (region *reg)
2190 ALIAS: longname = 0
2191
2192const char *cf_region_get_message (region *reg)
2193 ALIAS: message = 0
2194
2195
2196MODULE = cf PACKAGE = cf::living PREFIX = cf_living_
2197
2198val64
2199exp (living *liv, val64 new_val = 0)
2200 PROTOTYPE: $;$
2201 ALIAS:
2202 Str = 1
2203 Dex = 2
2204 Con = 3
2205 Wis = 4
2206 Cha = 5
2207 Int = 6
2208 Pow = 7
2209 wc = 8
2210 ac = 9
2211 hp = 10
2212 maxhp = 11
2213 sp = 12
2214 maxsp = 13
2215 grace = 14
2216 maxgrace = 15
2217 food = 16
2218 dam = 17
2219 luck = 18
2220 CODE: 2324 CODE:
2221# define LIVING_ACC(acc,idx) case idx: RETVAL = liv->acc; if (items > 1) liv->acc = (sint64)new_val; break 2325 RETVAL = first_region;
2222 switch (ix)
2223 {
2224 LIVING_ACC (exp , 0);
2225 LIVING_ACC (Str , 1);
2226 LIVING_ACC (Dex , 2);
2227 LIVING_ACC (Con , 3);
2228 LIVING_ACC (Wis , 4);
2229 LIVING_ACC (Cha , 5);
2230 LIVING_ACC (Int , 6);
2231 LIVING_ACC (Pow , 7);
2232 LIVING_ACC (wc , 8);
2233 LIVING_ACC (ac , 9);
2234 LIVING_ACC (hp , 10);
2235 LIVING_ACC (maxhp , 11);
2236 LIVING_ACC (sp , 12);
2237 LIVING_ACC (maxsp , 13);
2238 LIVING_ACC (grace , 14);
2239 LIVING_ACC (maxgrace, 15);
2240 LIVING_ACC (food , 16);
2241 LIVING_ACC (dam , 17);
2242 LIVING_ACC (luck , 18);
2243 }
2244# undef LIVING_ACC
2245 OUTPUT:
2246 RETVAL 2326 OUTPUT: RETVAL
2247 2327
2328region *next (region *reg)
2329 CODE:
2330 RETVAL = reg->next;
2331 OUTPUT: RETVAL
2332
2333const char *name (region *reg)
2334 CODE:
2335 RETVAL = reg->name;
2336 OUTPUT: RETVAL
2337
2338region *parent (region *reg)
2339 CODE:
2340 RETVAL = reg->parent;
2341 OUTPUT: RETVAL
2342
2343const char *longname (region *reg)
2344 CODE:
2345 RETVAL = reg->longname;
2346 OUTPUT: RETVAL
2347
2348const char *msg (region *reg)
2349 CODE:
2350 RETVAL = reg->msg;
2351 OUTPUT: RETVAL
2352
2353MODULE = cf PACKAGE = cf::living
2354
2355INCLUDE: $PERL genacc living ../include/living.h |
2356

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines