--- deliantra/server/common/object.C 2006/09/13 01:09:24 1.38 +++ deliantra/server/common/object.C 2006/12/03 02:46:33 1.57 @@ -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. @@ -34,8 +33,9 @@ #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, @@ -52,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) @@ -299,82 +376,15 @@ * The result of the dump is stored in the static global errmsg array. */ -void -dump_object2 (object *op) -{ - errmsg[0] = 0; - return; - //TODO//D#d# -#if 0 - char *cp; - -/* object *tmp;*/ - - if (op->arch != NULL) - { - strcat (errmsg, "arch "); - strcat (errmsg, op->arch->name ? op->arch->name : "(null)"); - strcat (errmsg, "\n"); - if ((cp = get_ob_diff (op, &empty_archetype->clone)) != NULL) - strcat (errmsg, cp); -# if 0 - /* Don't dump player diffs - they are too long, mostly meaningless, and - * will overflow the buffer. - * Changed so that we don't dump inventory either. This may - * also overflow the buffer. - */ - if (op->type != PLAYER && (cp = get_ob_diff (op, &empty_archetype->clone)) != NULL) - strcat (errmsg, cp); - for (tmp = op->inv; tmp; tmp = tmp->below) - dump_object2 (tmp); -# endif - strcat (errmsg, "end\n"); - } - else - { - strcat (errmsg, "Object "); - if (op->name == NULL) - strcat (errmsg, "(null)"); - else - strcat (errmsg, op->name); - strcat (errmsg, "\n"); -# if 0 - if ((cp = get_ob_diff (op, &empty_archetype->clone)) != NULL) - strcat (errmsg, cp); - for (tmp = op->inv; tmp; tmp = tmp->below) - dump_object2 (tmp); -# endif - strcat (errmsg, "end\n"); - } -#endif -} - -/* - * Dumps an object. Returns output in the static global errmsg array. - */ - -void +char * dump_object (object *op) { - if (op == NULL) - { - strcpy (errmsg, "[NULL pointer]"); - return; - } - errmsg[0] = '\0'; - dump_object2 (op); -} - -void -dump_all_objects (void) -{ - object *op; + if (!op) + return strdup ("[NULLOBJ]"); - for (op = objects; op != NULL; op = op->next) - { - dump_object (op); - fprintf (logfile, "Object %d\n:%s\n", op->count, errmsg); - } + object_freezer freezer; + save_object (freezer, op, 3); + return freezer.as_string (); } /* @@ -404,12 +414,11 @@ object * find_object (tag_t i) { - object *op; - - for (op = objects; op != NULL; op = op->next) + for (object *op = object::first; op; op = op->next) if (op->count == i) - break; - return op; + return op; + + return 0; } /* @@ -424,7 +433,7 @@ shstr_cmp str_ (str); object *op; - for (op = objects; op != NULL; op = op->next) + for (op = object::first; op != NULL; op = op->next) if (op->name == str_) break; @@ -438,27 +447,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. */ @@ -535,7 +523,6 @@ expmul = 1.0; face = blank_face; - attacked_by_count = -1; if (settings.casting_time) casting_time = -1; @@ -840,23 +827,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() && 0)//D - LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D + static int lastmortals = 0;//D + + if (mortals.size() != lastmortals && lastmortals > 100)//D + { + lastmortals = mortals.size ();//D + LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D + } } object::object () @@ -865,7 +857,6 @@ expmul = 1.0; face = blank_face; - attacked_by_count = -1; } object::~object () @@ -876,52 +867,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 == objects) - objects = next; + 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; - } + 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; } @@ -933,10 +905,10 @@ * The object must have been removed by remove_ob() first for * this function to succeed. * - * If free_inventory is set, free inventory as well. Else drop items in + * If destroy_inventory is set, free inventory as well. Else drop items in * inventory to the ground. */ -void object::free (bool free_inventory) +void object::destroy (bool destroy_inventory) { if (QUERY_FLAG (this, FLAG_FREED)) return; @@ -951,7 +923,7 @@ if (more) { - more->free (free_inventory); + more->destroy (destroy_inventory); more = 0; } @@ -961,14 +933,14 @@ * 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 (destroy_inventory || !map || map->in_memory != MAP_IN_MEMORY || GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL) { object *op = inv; while (op) { object *tmp = op->below; - op->free (free_inventory); + op->destroy (destroy_inventory); op = tmp; } } @@ -997,7 +969,33 @@ } } - owner = 0; + // hack to ensure that freed objects still have a valid map + { + static maptile *freed_map; // freed objects are moved here to avoid crashes + + if (!freed_map) + { + freed_map = new maptile; + + freed_map->name = "/internal/freed_objects_map"; + freed_map->width = 3; + freed_map->height = 3; + + freed_map->allocate (); + } + + map = freed_map; + x = 1; + y = 1; + } + + // clear those pointers that likely might have circular references to us + owner = 0; + enemy = 0; + attacked_by = 0; + + // only relevant for players(?), but make sure of it anyways + contr = 0; /* Remove object from the active list */ speed = 0; @@ -1019,9 +1017,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; } @@ -1039,22 +1036,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; @@ -1141,11 +1129,13 @@ */ if (GET_MAP_OB (m, x, y) != op) { - dump_object (op); + char *dump = dump_object (op); LOG (llevError, - "remove_ob: GET_MAP_OB does not return object to be removed even though it appears to be on the bottom?\n%s\n", errmsg); - dump_object (GET_MAP_OB (m, x, y)); - LOG (llevError, "%s\n", errmsg); + "remove_ob: GET_MAP_OB does not return object to be removed even though it appears to be on the bottom?\n%s\n", dump); + free (dump); + dump = dump_object (GET_MAP_OB (m, x, y)); + LOG (llevError, "%s\n", dump); + free (dump); } SET_MAP_OB (m, x, y, op->above); /* goes on above it. */ @@ -1157,7 +1147,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) @@ -1182,14 +1171,14 @@ } /* See if player moving off should effect something */ - if (check_walk_off && ((op->move_type & tmp->move_off) && (op->move_type & ~tmp->move_off & ~tmp->move_block) == 0)) + if (check_walk_off + && ((op->move_type & tmp->move_off) + && (op->move_type & ~tmp->move_off & ~tmp->move_block) == 0)) { 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, destroyed leaving object\n", tmp->debug_desc ()); } /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ @@ -1252,7 +1241,7 @@ } } - return NULL; + return 0; } /* @@ -1260,7 +1249,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; @@ -1298,7 +1287,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; @@ -1311,15 +1300,16 @@ if (m == NULL) { - dump_object (op); - LOG (llevError, "Trying to insert in null-map!\n%s\n", errmsg); + char *dump = dump_object (op); + LOG (llevError, "Trying to insert in null-map!\n%s\n", dump); + free (dump); return op; } if (out_of_map (m, op->x, op->y)) { - dump_object (op); - LOG (llevError, "Trying to insert object outside the map.\n%s\n", errmsg); + char *dump = dump_object (op); + LOG (llevError, "Trying to insert object outside the map.\n%s\n", dump); #ifdef MANY_CORES /* Better to catch this here, as otherwise the next use of this object * is likely to cause a crash. Better to find out where it is getting @@ -1327,13 +1317,15 @@ */ abort (); #endif + free (dump); return op; } if (!QUERY_FLAG (op, FLAG_REMOVED)) { - dump_object (op); - LOG (llevError, "Trying to insert (map) inserted object.\n%s\n", errmsg); + char *dump = dump_object (op); + LOG (llevError, "Trying to insert (map) inserted object.\n%s\n", dump); + free (dump); return op; } @@ -1589,7 +1581,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; @@ -1780,15 +1772,17 @@ if (!QUERY_FLAG (op, FLAG_REMOVED)) { - dump_object (op); - LOG (llevError, "Trying to insert (ob) inserted object.\n%s\n", errmsg); + char *dump = dump_object (op); + LOG (llevError, "Trying to insert (ob) inserted object.\n%s\n", dump); + free (dump); return op; } if (where == NULL) { - dump_object (op); - LOG (llevError, "Trying to put object in NULL.\n%s\n", errmsg); + char *dump = dump_object (op); + LOG (llevError, "Trying to put object in NULL.\n%s\n", dump); + free (dump); return op; } @@ -1898,25 +1892,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); @@ -1986,7 +1970,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 @@ -2008,7 +1992,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; @@ -2031,7 +2015,7 @@ */ object * -present (unsigned char type, mapstruct *m, int x, int y) +present (unsigned char type, maptile *m, int x, int y) { object * tmp; @@ -2181,7 +2165,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, @@ -2212,14 +2196,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; @@ -2293,7 +2277,7 @@ */ int -find_dir (mapstruct *m, int x, int y, object *exclude) +find_dir (maptile *m, int x, int y, object *exclude) { int i, @@ -2302,7 +2286,7 @@ sint16 nx, ny; object * tmp; - mapstruct * + maptile * mp; MoveType blocked, move_type; @@ -2520,7 +2504,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 @@ -2610,54 +2594,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); @@ -2681,8 +2646,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) @@ -2700,16 +2664,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; }