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

Comparing deliantra/server/include/cfperl.h (file contents):
Revision 1.65 by root, Thu May 24 03:33:29 2007 UTC vs.
Revision 1.80 by root, Sun Jun 24 04:09:29 2007 UTC

1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Crossfire TRT is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * The authors can be reached via e-mail to <crossfire@schmorp.de>
23 */
24
1// 25//
2// cfperl.h perl interface 26// cfperl.h perl interface
3// 27//
4#ifndef CFPERL_H__ 28#ifndef CFPERL_H__
5#define CFPERL_H__ 29#define CFPERL_H__
65void cfperl_init (); 89void cfperl_init ();
66void cfperl_main (); 90void cfperl_main ();
67void cfperl_emergency_save (); 91void cfperl_emergency_save ();
68void cfperl_cleanup (int make_core); 92void cfperl_cleanup (int make_core);
69void cfperl_make_book (object *book, int level); 93void cfperl_make_book (object *book, int level);
94void cfperl_send_msg (client *ns, int color, const char *type, const char *msg);
70 95
71////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 96//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72 97
73// virtual server time, excluding time jumps and lag 98extern tstamp runtime; // virtual server time, excluding time jumps and lag
74extern double runtime; 99extern tstamp NOW; // real time of current server tick
75 100
76enum event_klass 101enum event_klass
77{ 102{
78 KLASS_NONE, 103 KLASS_NONE,
79 KLASS_GLOBAL, 104 KLASS_GLOBAL,
110#define ARG_CLIENT(o) DT_CLIENT, (void *)(static_cast<client *> (o)) 135#define ARG_CLIENT(o) DT_CLIENT, (void *)(static_cast<client *> (o))
111#define ARG_PARTY(o) DT_PARTY , (void *)(static_cast<party *> (o)) 136#define ARG_PARTY(o) DT_PARTY , (void *)(static_cast<party *> (o))
112#define ARG_REGION(o) DT_REGION, (void *)(static_cast<region *> (o)) 137#define ARG_REGION(o) DT_REGION, (void *)(static_cast<region *> (o))
113 138
114// the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension 139// the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension
115 140#define INVOKE(obj,event, ...) (expect_false ((obj)->should_invoke (event)) ? (obj)->invoke (event, ## __VA_ARGS__, DT_END) : 0)
116// all these return true when the normal event processing should be skipped (if any)
117#define INVOKE_GLOBAL(event, ...) gbl_ev.invoke (EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__, DT_END) 141#define INVOKE_GLOBAL(event, ...) INVOKE (&gbl_ev, EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__)
118#define INVOKE_ATTACHABLE(event, obj, ...) (obj)->invoke (EVENT_ ## ATTACHABLE ## _ ## event, ## __VA_ARGS__, DT_END) 142#define INVOKE_ATTACHABLE(event, obj, ...) INVOKE (obj , EVENT_ ## ATTACHABLE ## _ ## event, ## __VA_ARGS__)
119#define INVOKE_OBJECT(event, obj, ...) (obj)->invoke (EVENT_ ## OBJECT ## _ ## event, ## __VA_ARGS__, DT_END) 143#define INVOKE_OBJECT(event, obj, ...) INVOKE (obj , EVENT_ ## OBJECT ## _ ## event, ## __VA_ARGS__)
120#define INVOKE_CLIENT(event, obj, ...) (obj)->invoke (EVENT_ ## CLIENT ## _ ## event, ## __VA_ARGS__, DT_END) 144#define INVOKE_CLIENT(event, obj, ...) INVOKE (obj , EVENT_ ## CLIENT ## _ ## event, ## __VA_ARGS__)
121#define INVOKE_PLAYER(event, obj, ...) (obj)->invoke (EVENT_ ## PLAYER ## _ ## event, ## __VA_ARGS__, DT_END) 145#define INVOKE_PLAYER(event, obj, ...) INVOKE (obj , EVENT_ ## PLAYER ## _ ## event, ## __VA_ARGS__)
122#define INVOKE_MAP(event, obj, ...) (obj)->invoke (EVENT_ ## MAP ## _ ## event, ## __VA_ARGS__, DT_END) 146#define INVOKE_MAP(event, obj, ...) INVOKE (obj , EVENT_ ## MAP ## _ ## event, ## __VA_ARGS__)
123 147
124//TODO should index into @result 148//TODO should index into @result
125#define RESULT(idx,type) cfperl_result_ ## type (idx) 149#define RESULT(idx,type) cfperl_result_ ## type (idx)
126#define RESULT_DOUBLE(idx) RESULT(idx, DOUBLE) 150#define RESULT_DOUBLE(idx) RESULT(idx, DOUBLE)
127#define RESULT_INT(idx) RESULT(idx, INT) 151#define RESULT_INT(idx) RESULT(idx, INT)
151 MTH void refcnt_inc () const { ++refcnt; } 175 MTH void refcnt_inc () const { ++refcnt; }
152 MTH void refcnt_dec () const { --refcnt; } 176 MTH void refcnt_dec () const { --refcnt; }
153 177
154 MTH int refcnt_cnt () const; 178 MTH int refcnt_cnt () const;
155 // check wether the object has died and destroy 179 // check wether the object has died and destroy
156 MTH void refcnt_chk () { if (refcnt <= 0) do_check (); } 180 MTH void refcnt_chk () { if (expect_false (refcnt <= 0)) do_check (); }
157 181
158 // destroy the object unless it was already destroyed 182 // destroy the object unless it was already destroyed
159 // this politely asks everybody interested the reduce 183 // this politely asks everybody interested the reduce
160 // the refcount to 0 as soon as possible. 184 // the refcount to 0 as soon as possible.
161 MTH void destroy (); 185 MTH void destroy ();
190 214
191 virtual ~attachable (); 215 virtual ~attachable ();
192 216
193 attachable &operator =(const attachable &src); 217 attachable &operator =(const attachable &src);
194 218
195 bool vinvoke (event_type event, va_list &ap); 219 // used to _quickly_ device wether to shortcut the evaluation
220 bool should_invoke (event_type event)
221 {
222 return ev_want_event [event] || cb;
223 }
224
196 bool invoke (event_type event, ...) 225 bool invoke (event_type event, ...);
197 {
198 if (ev_want_event [event] || cb)
199 {
200 va_list ap;
201 va_start (ap, event);
202 vinvoke (event, ap);
203 va_end (ap);
204 }
205 }
206 226
207 MTH void instantiate (); 227 MTH void instantiate ();
208 void reattach (); 228 void reattach ();
209 229
210protected: 230protected:
237 // used only for user-defined key-value pairs 257 // used only for user-defined key-value pairs
238 void put (const shstr &k, const shstr &v) 258 void put (const shstr &k, const shstr &v)
239 { 259 {
240 add (k); 260 add (k);
241 261
242 if (v) 262 if (expect_true (v))
243 add (' '), add (v); 263 add (' '), add (v);
244 264
245 add ('\n'); 265 add ('\n');
246 } 266 }
247 267
248 //TODO//temporary, used only for saving body locations
249 void put (const char *k, int v)
250 {
251 add (k);
252 add (' ');
253 add (sint32 (v));
254 add ('\n');
255 }
256
257 template<typename T> 268 template<typename T>
258 void put_string (keyword k, const T &v) 269 void put_kw_string (keyword k, const T &v)
259 { 270 {
260 int klen = keyword_len [k]; 271 int klen = keyword_len [k];
261 int vlen = v ? strlen (v) + 1 : 0; 272 int vlen = v ? strlen (v) + 1 : 0;
262 273
263 char *p = (char *)alloc (klen + vlen + 1); 274 char *p = (char *)alloc (klen + vlen + 1);
264 275
265 memcpy (p, keyword_str [k], klen); p += klen; 276 memcpy (p, keyword_str [k], klen); p += klen;
266 277
267 if (v) 278 if (expect_true (v))
268 { 279 {
269 *p++ = ' '; vlen--; 280 *p++ = ' '; vlen--;
270 memcpy (p, v, vlen); p += vlen; 281 memcpy (p, v, vlen); p += vlen;
271 } 282 }
272 283
273 *p = '\n'; 284 *p = '\n';
274 } 285 }
275 286
276 void put (keyword k, const char *v = 0) 287 void put (keyword k, const char *v = 0)
277 { 288 {
278 put_string (k, v); 289 put_kw_string (k, v);
279 } 290 }
280 291
281 void put (keyword k, const shstr &v) 292 void put (keyword k, const shstr &v)
282 { 293 {
283 put_string (k, v); 294 put_kw_string (k, v);
284 } 295 }
285 296
286 void put (keyword k, double v) 297 void put (keyword k, double v)
287 { 298 {
288 char buf [128]; 299 force (MAX_KEYWORD_LEN + 2 + 32);
289 300 fadd (keyword_str [k], keyword_len [k]);
290 snprintf (buf, 128, "%.7g", v); 301 fadd (' ');
291 302 falloc (sprintf (ptr, "%.7g", v));
292 put (k, (const char *)buf); 303 fadd ('\n');
293 } 304 }
294 305
295 void put_(keyword k, sint64 v) 306 void put_(keyword k, sint64 v)
296 { 307 {
308 force (MAX_KEYWORD_LEN + 2 + sint64_digits);
297 add (keyword_str [k], keyword_len [k]); 309 fadd (keyword_str [k], keyword_len [k]);
298 add (' '); 310 fadd (' ');
299 add (v); 311 add (v);
300 add ('\n'); 312 fadd ('\n');
301 } 313 }
302 314
303 void put_(keyword k, sint32 v) 315 void put_(keyword k, sint32 v)
304 { 316 {
317 force (MAX_KEYWORD_LEN + 2 + sint32_digits);
305 add (keyword_str [k], keyword_len [k]); 318 fadd (keyword_str [k], keyword_len [k]);
306 add (' '); 319 fadd (' ');
307 add (v); 320 add (v);
308 add ('\n'); 321 fadd ('\n');
309 }
310
311 void put (keyword kbeg, keyword kend, const shstr &v)
312 {
313 add (keyword_str [kbeg], keyword_len [kbeg]); add ('\n');
314
315 if (v)
316 {
317 add (v);
318 add ('\n');
319 }
320
321 add (keyword_str [kend], keyword_len [kend]); add ('\n');
322 } 322 }
323 323
324 void put (keyword k, float v) { put (k, (double)v); } 324 void put (keyword k, float v) { put (k, (double)v); }
325 void put (keyword k, signed char v) { put_(k, (sint32)v); } 325 void put (keyword k, signed char v) { put_(k, (sint32)v); }
326 void put (keyword k, unsigned char v) { put_(k, (sint32)v); } 326 void put (keyword k, unsigned char v) { put_(k, (sint32)v); }
331 void put (keyword k, signed long v) { put_(k, (sint64)v); } 331 void put (keyword k, signed long v) { put_(k, (sint64)v); }
332 void put (keyword k, unsigned long v) { put_(k, (sint64)v); } 332 void put (keyword k, unsigned long v) { put_(k, (sint64)v); }
333 void put (keyword k, signed long long v) { put_(k, (sint64)v); } 333 void put (keyword k, signed long long v) { put_(k, (sint64)v); }
334 void put (keyword k, unsigned long long v) { put_(k, (sint64)v); } 334 void put (keyword k, unsigned long long v) { put_(k, (sint64)v); }
335 335
336 template<typename T> 336 void put (keyword kbeg, keyword kend, const shstr &v)
337 {
338 force (MAX_KEYWORD_LEN + 1);
339 fadd (keyword_str [kbeg], keyword_len [kbeg]); fadd ('\n');
340
341 if (expect_true (v))
342 {
343 add (v);
344 add ('\n');
345 }
346
347 force (MAX_KEYWORD_LEN + 1);
348 fadd (keyword_str [kend], keyword_len [kend]); fadd ('\n');
349 }
350
351 void put (keyword k, archetype *v);
352 void put (keyword k, treasurelist *v);
337 void put (keyword k, const T *v) 353 void put (keyword k, faceinfo *v);
338 {
339 if (v)
340 put (k, v->name);
341 else
342 put (k, (const char *)0);
343 }
344 354
345 template<typename T> 355 template<typename T>
346 void put (keyword k, const refptr<T> &v) 356 void put (keyword k, const refptr<T> &v)
347 { 357 {
348 put (k, (T *)v); 358 put (k, (T *)v);
352 char *as_string (); // like strdup 362 char *as_string (); // like strdup
353 363
354 operator bool () { return !!av; } 364 operator bool () { return !!av; }
355}; 365};
356 366
357// compatibility support, should be removed when no longer needed
358void fprintf (object_freezer &freezer, const char *format, ...);
359void fputs (const char *s, object_freezer &freezer);
360
361struct object_thawer 367struct object_thawer
362{ 368{
369 char *line; // current beginning of line
363 SV *text; // text part 370 SV *text; // text part
364 AV *av; // perl part 371 AV *av; // perl part
365 int linenum; 372 int linenum;
366 char *line; // current beginning of line
367 keyword kw; 373 keyword kw;
368 char *kw_str, *value; 374 char *kw_str; // the keyword parsed, as string
375 char *value; // the value, or 0 if no value
376 char *value_nn; // the value, or the empty string if no value
369 const char *name; 377 const char *name;
370 378
371 operator bool () { return !!text; } 379 operator bool () { return !!text; }
372 380
373 object_thawer (const char *path = 0); 381 object_thawer (const char *path = 0);
377 void get (attachable *obj, int oid); 385 void get (attachable *obj, int oid);
378 386
379 // parse next line 387 // parse next line
380 void next (); 388 void next ();
381 // skip the current key-value (usually fetch next line, for 389 // skip the current key-value (usually fetch next line, for
382 // multiline-fields, skips untilt he corresponding end-kw 390 // multiline-fields, skips till the corresponding end-kw
383 void skip (); 391 void skip ();
384 392
385 //TODO: remove, deprecated
386 keyword get_kv ()
387 {
388 next ();
389 return kw;
390 }
391
392 char *get_str () { return value; } // may be 0 393 char *get_str () { return value; } // may be 0
394 void get_ml (keyword kend, shstr &sh);
393 395
394 void get (shstr &sh) const;
395 void get_ornull (shstr &sh) const { sh = value; } 396 void get_ornull (shstr &sh) const { sh = value; }
396 void get_ml (keyword kend, shstr &sh); 397 void get (shstr &sh) const { sh = value; } // might want to check for non-null here
397 398
399 bool get_bool () const { return *value_nn == '1'; }
398 sint32 get_sint32 () const; 400 sint32 get_sint32 () const;
399 sint64 get_sint64 () const; 401 sint64 get_sint64 () const { return strtoll (value_nn, 0, 10); }
400 double get_double () const; 402 double get_double () const { return strtod (value_nn, 0); }
401 403
402 void get (float &v) { v = get_double (); } 404 void get (float &v) { v = get_double (); }
403 void get (double &v) { v = get_double (); } 405 void get (double &v) { v = get_double (); }
404 406
405 void get (bool &i) { i = get_sint32 (); } 407 void get (bool &i) { i = get_bool (); }
406 void get (sint8 &i) { i = get_sint32 (); } 408 void get (sint8 &i) { i = get_sint32 (); }
407 void get (uint8 &i) { i = get_sint32 (); } 409 void get (uint8 &i) { i = get_sint32 (); }
408 void get (sint16 &i) { i = get_sint32 (); } 410 void get (sint16 &i) { i = get_sint32 (); }
409 void get (uint16 &i) { i = get_sint32 (); } 411 void get (uint16 &i) { i = get_sint32 (); }
410 void get (sint32 &i) { i = get_sint32 (); } 412 void get (sint32 &i) { i = get_sint32 (); }
424 static struct CoroAPI *GCoroAPI; 426 static struct CoroAPI *GCoroAPI;
425 427
426 static int nready () { return CORO_NREADY; } 428 static int nready () { return CORO_NREADY; }
427 static int cede () { return CORO_CEDE ; } 429 static int cede () { return CORO_CEDE ; }
428 430
429 static double (*time)();
430 static double next_cede; 431 static tstamp next_cede;
431 static int cede_counter; 432 static int cede_counter;
432 433
433 static void do_cede_every (); 434 static void do_cede_every ();
434 static void do_cede_to_tick (); 435 static void do_cede_to_tick ();
435 static void do_cede_to_tick_every (); 436 static void do_cede_to_tick_every ();
436 437
437 static void cede_every (int count) 438 static void cede_every (int count)
438 { 439 {
439 if (++cede_counter >= count) 440 if (expect_false (++cede_counter >= count))
440 do_cede_every (); 441 do_cede_every ();
441 } 442 }
442 443
443 static void cede_to_tick () 444 static bool cede_to_tick ()
444 { 445 {
445 if (time () >= next_cede) 446 if (expect_true (now () < next_cede))
447 return false;
448
446 do_cede_to_tick (); 449 do_cede_to_tick ();
450 return true;
447 } 451 }
448 452
449 static void cede_to_tick_every (int count) 453 static bool cede_to_tick_every (int count)
450 { 454 {
451 if (++cede_counter >= count) 455 if (expect_true (++cede_counter < count))
456 return false;
457
452 cede_to_tick (); 458 return cede_to_tick ();
453 } 459 }
454 460
455 static void wait_for_tick (); 461 static void wait_for_tick ();
456 static void wait_for_tick_begin (); 462 static void wait_for_tick_begin ();
457}; 463};

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines