--- deliantra/server/common/object.C 2006/09/11 23:33:27 1.31 +++ deliantra/server/common/object.C 2006/09/14 22:33:59 1.47 @@ -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,7 +447,7 @@ { 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); @@ -404,7 +483,7 @@ { 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 +498,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,27 +515,6 @@ } /* - * 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 * -object::get_owner () -{ - if (!owner - || QUERY_FLAG (owner, FLAG_FREED) - || QUERY_FLAG (owner, FLAG_REMOVED)) - owner = 0; - - return owner; -} - -/* * Sets the owner and sets the skill and exp pointers to owner's current * skill and experience objects. */ @@ -533,7 +591,6 @@ expmul = 1.0; face = blank_face; - attacked_by_count = -1; if (settings.casting_time) casting_time = -1; @@ -838,23 +895,28 @@ update_object (op->more, action); } -static unordered_vector mortals; -static std::vector freed; +object::vector object::mortals; +object::vector object::objects; // not yet used +object *object::first; void object::free_mortals () { - for (unordered_vector::iterator i = mortals.begin (); i != mortals.end ();) + for (AUTODECL (i, mortals.begin ()); i != mortals.end ();) if ((*i)->refcnt) ++i; // further delay freeing else { - freed.push_back (*i);//D - //delete *i; + delete *i; mortals.erase (i); } - if (mortals.size())//D - LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D + 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 () @@ -863,7 +925,6 @@ expmul = 1.0; face = blank_face; - attacked_by_count = -1; } object::~object () @@ -874,53 +935,36 @@ 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; + //count = 0;//D + if (!prev && !next) return;//D - /* Remove this object from the list of used objects */ - if (prev) - { - prev->next = next; - prev = 0; - } + if (this == object::first) + object::first = next; - if (next) - { - next->prev = prev; - next = 0; - } + /* Remove this object from the list of used objects */ + if (prev) prev->next = next; + if (next) next->prev = prev; - 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; } @@ -940,12 +984,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) @@ -960,7 +1004,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; @@ -996,7 +1040,10 @@ } } - owner = 0; + // 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; @@ -1018,9 +1065,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; } @@ -1038,22 +1084,14 @@ 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; + tag_t tag; + int check_walk_off; + mapstruct *m; - sint16 - x, - y; + sint16 x, y; if (QUERY_FLAG (op, FLAG_REMOVED)) return; @@ -1226,7 +1264,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) { @@ -1252,7 +1289,7 @@ } } - return NULL; + return 0; } /* @@ -1589,7 +1626,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; @@ -2657,7 +2694,8 @@ { LOG (llevError, "Error - Unable to access load object temp file\n"); return NULL; - }; + } + fprintf (tempfile, obstr); fclose (tempfile); @@ -2724,34 +2762,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; } @@ -2847,13 +2878,53 @@ return set_ob_key_value_s (op, key_, value, add_key); } -void -object::deep_iterator::next () +object::depth_iterator::depth_iterator (object *container) +: iterator_base (container) { - if (item->inv) + while (item->inv) item = item->inv; - else if (item->below) - item = item->below; +} + +void +object::depth_iterator::next () +{ + if (item->below) + { + item = item->below; + + while (item->inv) + item = item->inv; + } else - item = item->env->below; + 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); +} +