--- deliantra/server/common/object.C 2006/09/11 12:38:36 1.29 +++ deliantra/server/common/object.C 2006/09/16 22:24:12 1.49 @@ -1,4 +1,3 @@ - /* CrossFire, A Multiplayer game for X-windows @@ -19,7 +18,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - The authors can be reached via e-mail at crossfire-devel@real-time.com + The authors can be reached via e-mail at */ /* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects. @@ -31,12 +30,12 @@ #include #include #include -#include #include int nrofallocobjects = 0; +static UUID uuid; +const uint64 UUID_SKIP = 1<<19; -object *objects; /* Pointer to the list of used objects */ object *active_objects; /* List of active objects that need to be processed */ short freearr_x[SIZEOFFREE] = { 0, 0, 1, 1, 1, 0, -1, -1, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1, @@ -53,6 +52,83 @@ 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8 }; +static void +write_uuid (void) +{ + char filename1[MAX_BUF], filename2[MAX_BUF]; + + sprintf (filename1, "%s/uuid", settings.localdir); + sprintf (filename2, "%s/uuid~", settings.localdir); + + FILE *fp; + + if (!(fp = fopen (filename2, "w"))) + { + LOG (llevError, "ERROR: cannot open %s for writing, unable to write UUID!\n", filename2); + return; + } + + fprintf (fp, "<1,%llx>\n", (unsigned long long)uuid.seq + UUID_SKIP * 2); + fclose (fp); + rename (filename2, filename1); +} + +static void +read_uuid (void) +{ + char filename[MAX_BUF]; + + sprintf (filename, "%s/uuid", settings.localdir); + + FILE *fp; + + if (!(fp = fopen (filename, "r"))) + { + if (errno == ENOENT) + { + LOG (llevInfo, "RESET uid to 1\n"); + uuid.seq = 0; + write_uuid (); + return; + } + + LOG (llevError, "FATAL: cannot open %s for reading!\n", filename); + _exit (1); + } + + int version; + unsigned long long uid; + if (2 != fscanf (fp, "<%d,%llx>\n", &version, &uid) || version != 1) + { + LOG (llevError, "FATAL: error reading uid from %s!\n", filename); + _exit (1); + } + + uuid.seq = uid; + write_uuid (); + LOG (llevDebug, "read UID: %lld\n", uid); + fclose (fp); +} + +UUID +gen_uuid () +{ + UUID uid; + + uid.seq = ++uuid.seq; + + if (!(uuid.seq & (UUID_SKIP - 1))) + write_uuid (); + + return uid; +} + +void +init_uuid () +{ + read_uuid (); +} + /* Returns TRUE if every key_values in wants has a partner with the same value in has. */ static int compare_ob_value_lists_one (const object *wants, const object *has) @@ -257,10 +333,13 @@ sum_weight (inv); sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); } + if (op->type == CONTAINER && op->stats.Str) sum = (sum * (100 - op->stats.Str)) / 100; + if (op->carrying != sum) op->carrying = sum; + return sum; } @@ -368,10 +447,10 @@ { object *op; - for (op = objects; op != NULL; op = op->next) + for (op = object::first; op != NULL; op = op->next) { dump_object (op); - fprintf (logfile, "Object %d\n:%s\n", op->count, errmsg); + fprintf (logfile, "Object %s\n", errmsg); } } @@ -404,9 +483,10 @@ { object *op; - for (op = objects; op != NULL; op = op->next) + for (op = object::first; op != NULL; op = op->next) if (op->count == i) break; + return op; } @@ -419,11 +499,11 @@ object * find_object_name (const char *str) { - const char *name = shstr::find (str); + shstr_cmp str_ (str); object *op; - for (op = objects; op != NULL; op = op->next) - if (&op->name == name) + for (op = object::first; op != NULL; op = op->next) + if (op->name == str_) break; return op; @@ -436,51 +516,13 @@ } /* - * Returns the object which this object marks as being the owner. - * A id-scheme is used to avoid pointing to objects which have been - * freed and are now reused. If this is detected, the owner is - * set to NULL, and NULL is returned. - * Changed 2004-02-12 - if the player is setting at the play again - * prompt, he is removed, and we don't want to treat him as an owner of - * anything, so check removed flag. I don't expect that this should break - * anything - once an object is removed, it is basically dead anyways. - */ - -object * -get_owner (object *op) -{ - if (op->owner == NULL) - return NULL; - - if (!QUERY_FLAG (op->owner, FLAG_FREED) && !QUERY_FLAG (op->owner, FLAG_REMOVED) && op->owner->count == op->ownercount) - return op->owner; - - op->owner = NULL; - op->ownercount = 0; - return NULL; -} - -void -clear_owner (object *op) -{ - if (!op) - return; - - if (op->owner && op->ownercount == op->owner->count) - op->owner->refcount--; - - op->owner = NULL; - op->ownercount = 0; -} - -/* * Sets the owner and sets the skill and exp pointers to owner's current * skill and experience objects. */ void -set_owner (object *op, object *owner) +object::set_owner (object *owner) { - if (owner == NULL || op == NULL) + if (!owner) return; /* next line added to allow objects which own objects */ @@ -490,48 +532,10 @@ * freed and then another object replacing it. Since the ownercounts * didn't match, this check is valid and I believe that cause is valid. */ - while (owner->owner && owner != owner->owner && owner->ownercount == owner->owner->count) + while (owner->owner) owner = owner->owner; - /* IF the owner still has an owner, we did not resolve to a final owner. - * so lets not add to that. - */ - if (owner->owner) - return; - - op->owner = owner; - - op->ownercount = owner->count; - owner->refcount++; -} - -/* Set the owner to clone's current owner and set the skill and experience - * objects to clone's objects (typically those objects that where the owner's - * current skill and experience objects at the time when clone's owner was - * set - not the owner's current skill and experience objects). - * - * Use this function if player created an object (e.g. fire bullet, swarm - * spell), and this object creates further objects whose kills should be - * accounted for the player's original skill, even if player has changed - * skills meanwhile. - */ -void -copy_owner (object *op, object *clone) -{ - object *owner = get_owner (clone); - - if (owner == NULL) - { - /* players don't have owners - they own themselves. Update - * as appropriate. - */ - if (clone->type == PLAYER) - owner = clone; - else - return; - } - - set_owner (op, owner); + this->owner = owner; } /* Zero the key_values on op, decrementing the shared-string @@ -557,8 +561,7 @@ free_key_values (this); - clear_owner (this); - + owner = 0; name = 0; name_pl = 0; title = 0; @@ -569,54 +572,41 @@ lore = 0; custom_name = 0; materialname = 0; + contr = 0; + below = 0; + above = 0; + inv = 0; + container = 0; + env = 0; + more = 0; + head = 0; + map = 0; + active_next = 0; + active_prev = 0; - memset (static_cast < object_pod * >(this), 0, sizeof (object_pod)); + memset (static_cast(this), 0, sizeof (object_pod)); SET_FLAG (this, FLAG_REMOVED); + + /* What is not cleared is next, prev, and count */ + + expmul = 1.0; + face = blank_face; + + if (settings.casting_time) + casting_time = -1; } void object::clone (object *destination) { - *(object_copy *) destination = *this; - *(object_pod *) destination = *this; + *(object_copy *)destination = *this; + *(object_pod *)destination = *this; if (self || cb) INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination)); } /* - * clear_object() frees everything allocated by an object, and also - * clears all variables and flags to default settings. - */ - -void -clear_object (object *op) -{ - op->clear (); - - op->contr = NULL; - op->below = NULL; - op->above = NULL; - op->inv = NULL; - op->container = NULL; - op->env = NULL; - op->more = NULL; - op->head = NULL; - op->map = NULL; - op->refcount = 0; - op->active_next = NULL; - op->active_prev = NULL; - /* What is not cleared is next, prev, and count */ - - op->expmul = 1.0; - op->face = blank_face; - op->attacked_by_count = -1; - - if (settings.casting_time) - op->casting_time = -1; -} - -/* * copy object first frees everything allocated by the second object, * and then copies the contends of the first object into the second * object, allocating what needs to be allocated. Basically, any @@ -624,7 +614,6 @@ * if the first object is freed, the pointers in the new object * will point at garbage. */ - void copy_object (object *op2, object *op) { @@ -694,7 +683,6 @@ * value, or vice versa, then add/remove the object from the active list. * This function needs to be called whenever the speed of an object changes. */ - void update_ob_speed (object *op) { @@ -713,10 +701,10 @@ op->speed = 0; #endif } + if (arch_init) - { - return; - } + return; + if (FABS (op->speed) > MIN_ACTIVE_SPEED) { /* If already on active list, don't do anything */ @@ -726,8 +714,10 @@ /* process_events() expects us to insert the object at the beginning * of the list. */ op->active_next = active_objects; + if (op->active_next != NULL) op->active_next->active_prev = op; + active_objects = op; } else @@ -739,15 +729,18 @@ if (op->active_prev == NULL) { active_objects = op->active_next; + if (op->active_next != NULL) op->active_next->active_prev = NULL; } else { op->active_prev->active_next = op->active_next; + if (op->active_next) op->active_next->active_prev = op->active_prev; } + op->active_next = NULL; op->active_prev = NULL; } @@ -903,21 +896,28 @@ update_object (op->more, action); } -static unordered_vector < object *>mortals; -static -std::vector < object *> - freed; +object::vector object::mortals; +object::vector object::objects; // not yet used +object *object::first; void object::free_mortals () { - for (unordered_vector < object *>::iterator i = mortals.begin (); i != mortals.end ();) - if (!(*i)->refcount) + for (AUTODECL (i, mortals.begin ()); i != mortals.end ();) + if ((*i)->refcnt) + ++i; // further delay freeing + else { - freed.push_back (*i); + delete *i; mortals.erase (i); } - else - ++i; + + static int lastmortals = 0;//D + + if (mortals.size() != lastmortals)//D + { + lastmortals = mortals.size ();//D + LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D + } } object::object () @@ -926,7 +926,6 @@ expmul = 1.0; face = blank_face; - attacked_by_count = -1; } object::~object () @@ -937,54 +936,33 @@ void object::link () { count = ++ob_count; + uuid = gen_uuid (); prev = 0; - next = objects; + next = object::first; - if (objects) - objects->prev = this; + if (object::first) + object::first->prev = this; - objects = this; + object::first = this; } void object::unlink () { - count = 0; + if (this == object::first) + object::first = next; /* Remove this object from the list of used objects */ - if (prev) - { - prev->next = next; - prev = 0; - } + if (prev) prev->next = next; + if (next) next->prev = prev; - if (next) - { - next->prev = prev; - next = 0; - } - - if (this == objects) - objects = next; + prev = 0; + next = 0; } object *object::create () { - object * - op; - - if (freed.empty ()) - op = new object; - - else - { - // highly annoying, but the only way to get it stable right now - op = freed.back (); - freed.pop_back (); - op->~object (); - new ((void *) op) object; - } - + object *op = new object; op->link (); return op; } @@ -1004,12 +982,12 @@ if (QUERY_FLAG (this, FLAG_FREED)) return; - if (!QUERY_FLAG (this, FLAG_REMOVED)) - remove_ob (this); - if (QUERY_FLAG (this, FLAG_FRIENDLY)) remove_friendly_object (this); + if (!QUERY_FLAG (this, FLAG_REMOVED)) + remove_ob (this); + SET_FLAG (this, FLAG_FREED); if (more) @@ -1024,7 +1002,7 @@ * if some form of movement is allowed, let objects * drop on that space. */ - if (free_inventory || !map || map->in_memory != MAP_IN_MEMORY || (GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL)) + if (free_inventory || !map || map->in_memory != MAP_IN_MEMORY || GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL) { object *op = inv; @@ -1060,7 +1038,10 @@ } } - clear_owner (this); + // clear those pointers that likely might have circular references to us + owner = 0; + enemy = 0; + attacked_by = 0; /* Remove object from the active list */ speed = 0; @@ -1082,9 +1063,8 @@ while (op != NULL) { if (op->type == CONTAINER) - { - weight = (signed long) (weight * (100 - op->stats.Str) / 100); - } + weight = (signed long) (weight * (100 - op->stats.Str) / 100); + op->carrying -= weight; op = op->env; } @@ -1102,22 +1082,13 @@ void remove_ob (object *op) { - object * - tmp, * - last = NULL; - object * - otmp; + object *tmp, *last = 0; + object *otmp; - tag_t - tag; - int - check_walk_off; - mapstruct * - m; - - sint16 - x, - y; + int check_walk_off; + maptile *m; + + sint16 x, y; if (QUERY_FLAG (op, FLAG_REMOVED)) return; @@ -1220,7 +1191,6 @@ if (op->map->in_memory == MAP_SAVING) return; - tag = op->count; check_walk_off = !QUERY_FLAG (op, FLAG_NO_APPLY); for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) @@ -1249,10 +1219,8 @@ { move_apply (tmp, op, NULL); - if (was_destroyed (op, tag)) - { - LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " "leaving object\n", &tmp->name, &tmp->arch->name); - } + if (op->destroyed ()); + LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " "leaving object\n", &tmp->name, &tmp->arch->name); } /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ @@ -1290,7 +1258,6 @@ * If top is NULL, it is calculated. * Returns pointer to object if it succeded in the merge, otherwise NULL */ - object * merge_ob (object *op, object *top) { @@ -1316,7 +1283,7 @@ } } - return NULL; + return 0; } /* @@ -1324,7 +1291,7 @@ * job preparing multi-part monsters */ object * -insert_ob_in_map_at (object *op, mapstruct *m, object *originator, int flag, int x, int y) +insert_ob_in_map_at (object *op, maptile *m, object *originator, int flag, int x, int y) { object *tmp; @@ -1362,7 +1329,7 @@ */ object * -insert_ob_in_map (object *op, mapstruct *m, object *originator, int flag) +insert_ob_in_map (object *op, maptile *m, object *originator, int flag) { object *tmp, *top, *floor = NULL; sint16 x, y; @@ -1653,7 +1620,7 @@ } } - tmp1 = arch_to_object (find_archetype (arch_string)); + tmp1 = arch_to_object (archetype::find (arch_string)); tmp1->x = op->x; tmp1->y = op->y; @@ -1962,25 +1929,15 @@ int check_move_on (object *op, object *originator) { - object * - tmp; - tag_t - tag; - mapstruct * - m = op->map; - int - x = op->x, y = op->y; + object *tmp; + maptile *m = op->map; + int x = op->x, y = op->y; - MoveType - move_on, - move_slow, - move_block; + MoveType move_on, move_slow, move_block; if (QUERY_FLAG (op, FLAG_NO_APPLY)) return 0; - tag = op->count; - move_on = GET_MAP_MOVE_ON (op->map, op->x, op->y); move_slow = GET_MAP_MOVE_SLOW (op->map, op->x, op->y); move_block = GET_MAP_MOVE_BLOCK (op->map, op->x, op->y); @@ -2050,7 +2007,7 @@ { move_apply (tmp, op, originator); - if (was_destroyed (op, tag)) + if (op->destroyed ()) return 1; /* what the person/creature stepped onto has moved the object @@ -2072,7 +2029,7 @@ */ object * -present_arch (const archetype *at, mapstruct *m, int x, int y) +present_arch (const archetype *at, maptile *m, int x, int y) { object * tmp; @@ -2095,7 +2052,7 @@ */ object * -present (unsigned char type, mapstruct *m, int x, int y) +present (unsigned char type, maptile *m, int x, int y) { object * tmp; @@ -2245,7 +2202,7 @@ */ int -find_free_spot (const object *ob, mapstruct *m, int x, int y, int start, int stop) +find_free_spot (const object *ob, maptile *m, int x, int y, int start, int stop) { int i, @@ -2276,14 +2233,14 @@ } /* - * find_first_free_spot(archetype, mapstruct, x, y) works like + * find_first_free_spot(archetype, maptile, x, y) works like * find_free_spot(), but it will search max number of squares. * But it will return the first available spot, not a random choice. * Changed 0.93.2: Have it return -1 if there is no free spot available. */ int -find_first_free_spot (const object *ob, mapstruct *m, int x, int y) +find_first_free_spot (const object *ob, maptile *m, int x, int y) { int i; @@ -2357,7 +2314,7 @@ */ int -find_dir (mapstruct *m, int x, int y, object *exclude) +find_dir (maptile *m, int x, int y, object *exclude) { int i, @@ -2366,7 +2323,7 @@ sint16 nx, ny; object * tmp; - mapstruct * + maptile * mp; MoveType blocked, move_type; @@ -2584,7 +2541,7 @@ int -can_see_monsterP (mapstruct *m, int x, int y, int dir) +can_see_monsterP (maptile *m, int x, int y, int dir) { sint16 dx, dy; int @@ -2674,54 +2631,35 @@ prev = tmp; } - /*** copy inventory ***/ for (item = src->inv; item; item = item->below) - { - (void) insert_ob_in_ob (object_create_clone (item), dst); - } + insert_ob_in_ob (object_create_clone (item), dst); return dst; } -/* return true if the object was destroyed, 0 otherwise */ -int -was_destroyed (const object *op, tag_t old_tag) -{ - /* checking for FLAG_FREED isn't necessary, but makes this function more - * robust */ - return op->count != old_tag || QUERY_FLAG (op, FLAG_FREED); -} - /* GROS - Creates an object using a string representing its content. */ - /* Basically, we save the content of the string to a temp file, then call */ - /* load_object on it. I admit it is a highly inefficient way to make things, */ - /* but it was simple to make and allows reusing the load_object function. */ - /* Remember not to use load_object_str in a time-critical situation. */ - /* Also remember that multiparts objects are not supported for now. */ object * load_object_str (const char *obstr) { - object * - op; - char - filename[MAX_BUF]; + object *op; + char filename[MAX_BUF]; sprintf (filename, "%s/cfloadobstr2044", settings.tmpdir); - FILE * - tempfile = fopen (filename, "w"); + FILE *tempfile = fopen (filename, "w"); if (tempfile == NULL) { LOG (llevError, "Error - Unable to access load object temp file\n"); return NULL; - }; + } + fprintf (tempfile, obstr); fclose (tempfile); @@ -2745,8 +2683,7 @@ object * find_obj_by_type_subtype (const object *who, int type, int subtype) { - object * - tmp; + object *tmp; for (tmp = who->inv; tmp; tmp = tmp->below) if (tmp->type == type && tmp->subtype == subtype) @@ -2764,16 +2701,11 @@ key_value * get_ob_key_link (const object *ob, const char *key) { - key_value * - link; + key_value *link; for (link = ob->key_values; link != NULL; link = link->next) - { - if (link->key == key) - { - return link; - } - } + if (link->key == key) + return link; return NULL; } @@ -2788,34 +2720,27 @@ const char * get_ob_key_value (const object *op, const char *const key) { - key_value * - link; - const char * - canonical_key; + key_value *link; + shstr_cmp canonical_key (key); - canonical_key = shstr::find (key); - - if (canonical_key == NULL) + if (!canonical_key) { /* 1. There being a field named key on any object * implies there'd be a shared string to find. * 2. Since there isn't, no object has this field. * 3. Therefore, *this* object doesn't have this field. */ - return NULL; + return 0; } /* This is copied from get_ob_key_link() above - * only 4 lines, and saves the function call overhead. */ - for (link = op->key_values; link != NULL; link = link->next) - { - if (link->key == canonical_key) - { - return link->value; - } - } - return NULL; + for (link = op->key_values; link; link = link->next) + if (link->key == canonical_key) + return link->value; + + return 0; } @@ -2910,3 +2835,54 @@ return set_ob_key_value_s (op, key_, value, add_key); } + +object::depth_iterator::depth_iterator (object *container) +: iterator_base (container) +{ + while (item->inv) + item = item->inv; +} + +void +object::depth_iterator::next () +{ + if (item->below) + { + item = item->below; + + while (item->inv) + item = item->inv; + } + else + item = item->env; +} + +// return a suitable string describing an objetc in enough detail to find it +const char * +object::debug_desc (char *info) const +{ + char info2[256 * 3]; + char *p = info; + + p += snprintf (p, 256, "%d=\"%s%s%s\"", + count, + &name, + title ? " " : "", + title ? (const char *)title : ""); + + if (env) + p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); + + if (map) + p += snprintf (p, 256, "(on %s@%d+%d)", map->path, x, y); + + return info; +} + +const char * +object::debug_desc () const +{ + static char info[256 * 3]; + return debug_desc (info); +} +