--- deliantra/server/include/cfperl.h 2007/02/13 16:23:31 1.55 +++ deliantra/server/include/cfperl.h 2007/05/27 23:14:37 1.67 @@ -4,7 +4,9 @@ #ifndef CFPERL_H__ #define CFPERL_H__ +#include #include +#include using namespace std; @@ -64,6 +66,7 @@ void cfperl_main (); void cfperl_emergency_save (); void cfperl_cleanup (int make_core); +void cfperl_make_book (object *book, int level); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -90,6 +93,10 @@ NUM_EVENT_TYPES }; +// in which global events or per-type events are we interested +extern bitset ev_want_event; +extern bitset ev_want_type; + #define ARG_AV(o) DT_AV , static_cast (o) #define ARG_INT(v) DT_INT , static_cast (v) #define ARG_INT64(v) DT_INT64 , static_cast (v) @@ -105,14 +112,13 @@ #define ARG_REGION(o) DT_REGION, (void *)(static_cast (o)) // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension - -// all these return true when the normal event processing should be skipped (if any) -#define INVOKE_GLOBAL(event, ...) gbl_ev.invoke (EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__, DT_END) -#define INVOKE_ATTACHABLE(event, obj, ...) (obj)->invoke (EVENT_ ## ATTACHABLE ## _ ## event, ## __VA_ARGS__, DT_END) -#define INVOKE_OBJECT(event, obj, ...) (obj)->invoke (EVENT_ ## OBJECT ## _ ## event, ## __VA_ARGS__, DT_END) -#define INVOKE_CLIENT(event, obj, ...) (obj)->invoke (EVENT_ ## CLIENT ## _ ## event, ## __VA_ARGS__, DT_END) -#define INVOKE_PLAYER(event, obj, ...) (obj)->invoke (EVENT_ ## PLAYER ## _ ## event, ## __VA_ARGS__, DT_END) -#define INVOKE_MAP(event, obj, ...) (obj)->invoke (EVENT_ ## MAP ## _ ## event, ## __VA_ARGS__, DT_END) +#define INVOKE(obj,event, ...) (expect_false ((obj)->should_invoke (event)) ? (obj)->invoke (event, ## __VA_ARGS__, DT_END) : 0) +#define INVOKE_GLOBAL(event, ...) INVOKE (&gbl_ev, EVENT_ ## GLOBAL ## _ ## event, ## __VA_ARGS__) +#define INVOKE_ATTACHABLE(event, obj, ...) INVOKE (obj , EVENT_ ## ATTACHABLE ## _ ## event, ## __VA_ARGS__) +#define INVOKE_OBJECT(event, obj, ...) INVOKE (obj , EVENT_ ## OBJECT ## _ ## event, ## __VA_ARGS__) +#define INVOKE_CLIENT(event, obj, ...) INVOKE (obj , EVENT_ ## CLIENT ## _ ## event, ## __VA_ARGS__) +#define INVOKE_PLAYER(event, obj, ...) INVOKE (obj , EVENT_ ## PLAYER ## _ ## event, ## __VA_ARGS__) +#define INVOKE_MAP(event, obj, ...) INVOKE (obj , EVENT_ ## MAP ## _ ## event, ## __VA_ARGS__) //TODO should index into @result #define RESULT(idx,type) cfperl_result_ ## type (idx) @@ -138,8 +144,8 @@ }; // object is delete'd after the refcount reaches 0 - mutable int ACC (RW, refcnt); int ACC (RW, flags); + mutable int ACC (RW, refcnt); MTH void refcnt_inc () const { ++refcnt; } MTH void refcnt_dec () const { --refcnt; } @@ -185,7 +191,14 @@ attachable &operator =(const attachable &src); + // used to _quickly_ device wether to shortcut the evaluation + bool should_invoke (event_type event) + { + return ev_want_event [event] || cb; + } + bool invoke (event_type event, ...); + MTH void instantiate (); void reattach (); @@ -207,7 +220,7 @@ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -struct object_freezer : dynbuf +struct object_freezer : dynbuf_text { AV *av; @@ -221,7 +234,7 @@ { add (k); - if (v) + if (expect_true (v)) add (' '), add (v); add ('\n'); @@ -246,7 +259,7 @@ memcpy (p, keyword_str [k], klen); p += klen; - if (v) + if (expect_true (v)) { *p++ = ' '; vlen--; memcpy (p, v, vlen); p += vlen; @@ -294,7 +307,7 @@ { add (keyword_str [kbeg], keyword_len [kbeg]); add ('\n'); - if (v) + if (expect_true (v)) { add (v); add ('\n'); @@ -318,7 +331,7 @@ template void put (keyword k, const T *v) { - if (v) + if (expect_true (v)) put (k, v->name); else put (k, (const char *)0); @@ -336,17 +349,16 @@ operator bool () { return !!av; } }; -// compatibility support, should be removed when no longer needed -int fprintf (object_freezer &freezer, const char *format, ...); -int fputs (const char *s, object_freezer &freezer); - struct object_thawer { + char *line; // current beginning of line SV *text; // text part AV *av; // perl part int linenum; - char *line; // current beginning of line - char *last_keyword, *last_value; + keyword kw; + char *kw_str; // the keyword parsed, as string + char *value; // the value, or 0 if no value + char *value_nn; // the value, or the empty string if no value const char *name; operator bool () { return !!text; } @@ -357,23 +369,34 @@ void get (attachable *obj, int oid); - keyword get_kv (); // also parse value for later use - void skip_kv (keyword kw); + // parse next line + void next (); + // skip the current key-value (usually fetch next line, for + // multiline-fields, skips untilt he corresponding end-kw + void skip (); - const char *get_str () { return last_value; } // may be 0 + //TODO: remove, deprecated + keyword get_kv () + { + next (); + return kw; + } - void get (shstr &sh) const; - void get_ornull (shstr &sh) const { sh = last_value; } + char *get_str () { return value; } // may be 0 void get_ml (keyword kend, shstr &sh); + void get_ornull (shstr &sh) const { sh = value; } + void get (shstr &sh) const { sh = value; } // might want to check for non-null here + + bool get_bool () const { return *value_nn == '1'; } sint32 get_sint32 () const; - sint64 get_sint64 () const; - double get_double () const; + sint64 get_sint64 () const { return atoll (value_nn); } + double get_double () const { return atof (value_nn); } void get (float &v) { v = get_double (); } void get (double &v) { v = get_double (); } - void get (bool &i) { i = get_sint32 (); } + void get (bool &i) { i = get_bool (); } void get (sint8 &i) { i = get_sint32 (); } void get (uint8 &i) { i = get_sint32 (); } void get (sint16 &i) { i = get_sint32 (); } @@ -383,7 +406,7 @@ void get (uint32 &i) { i = get_sint64 (); } void get (sint64 &i) { i = get_sint64 (); } - bool parse_error (keyword kw, const char *type = 0, const char *name = 0, bool skip = true); + bool parse_error (const char *type = 0, const char *name = 0, bool skip = true); }; //TODO: remove @@ -397,18 +420,34 @@ static int nready () { return CORO_NREADY; } static int cede () { return CORO_CEDE ; } + static double (*time)(); + static double next_cede; static int cede_counter; + static void do_cede_every (); + static void do_cede_to_tick (); + static void do_cede_to_tick_every (); + static void cede_every (int count) { - if (++cede_counter >= count) - { - cede_counter = 0; + if (expect_false (++cede_counter >= count)) + do_cede_every (); + } - if (coroapi::nready ()) - coroapi::cede (); - } + static void cede_to_tick () + { + if (expect_false (time () >= next_cede)) + do_cede_to_tick (); } + + static void cede_to_tick_every (int count) + { + if (expect_false (++cede_counter >= count)) + cede_to_tick (); + } + + static void wait_for_tick (); + static void wait_for_tick_begin (); }; struct watcher_base