--- deliantra/server/common/object.C 2006/09/10 16:00:23 1.24 +++ deliantra/server/common/object.C 2006/09/16 22:24:12 1.49 @@ -1,9 +1,3 @@ - -/* - * static char *rcsid_object_c = - * "$Id: object.C,v 1.24 2006/09/10 16:00:23 root Exp $"; - */ - /* CrossFire, A Multiplayer game for X-windows @@ -24,25 +18,24 @@ 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. sub/add_weight will transcend the environment updating the carrying variable. */ #include -#ifndef WIN32 /* ---win32 exclude headers */ -# include -# include -# include -#endif /* win32 */ +#include +#include +#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, @@ -59,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) @@ -216,10 +286,10 @@ switch (ob1->type) { - case SCROLL: - if (ob1->level != ob2->level) - return 0; - break; + case SCROLL: + if (ob1->level != ob2->level) + return 0; + break; } if (ob1->key_values != NULL || ob2->key_values != NULL) @@ -251,10 +321,10 @@ * an object is carrying. It goes through in figures out how much * containers are carrying, and sums it up. */ -signed long +long sum_weight (object *op) { - signed long sum; + long sum; object *inv; for (sum = 0, inv = op->inv; inv != NULL; inv = inv->below) @@ -263,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; } @@ -374,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); } } @@ -410,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; } @@ -425,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; @@ -442,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 */ @@ -496,49 +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 @@ -564,6 +561,7 @@ free_key_values (this); + owner = 0; name = 0; name_pl = 0; title = 0; @@ -574,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 @@ -629,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) { @@ -699,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) { @@ -718,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 */ @@ -731,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 @@ -744,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; } @@ -892,17 +880,11 @@ * that is being removed. */ else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE) - { - update_now = 1; - } + update_now = 1; else if (action == UP_OBJ_FACE) - { - /* Nothing to do for that case */ - } + /* Nothing to do for that case */ ; else - { - LOG (llevError, "update_object called with invalid action: %d\n", action); - } + LOG (llevError, "update_object called with invalid action: %d\n", action); if (update_now) { @@ -914,17 +896,28 @@ update_object (op->more, action); } -static -std::vector < object *> - mortals; +object::vector object::mortals; +object::vector object::objects; // not yet used +object *object::first; void object::free_mortals () { - for (std::vector < object *>::iterator i = mortals.begin (); i != mortals.end (); ++i) - delete * - i; + for (AUTODECL (i, mortals.begin ()); i != mortals.end ();) + if ((*i)->refcnt) + ++i; // further delay freeing + else + { + delete *i; + mortals.erase (i); + } - mortals.clear (); + 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 () @@ -933,7 +926,6 @@ expmul = 1.0; face = blank_face; - attacked_by_count = -1; } object::~object () @@ -944,34 +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; - if (next) - next->prev = prev; - if (this == objects) - objects = next; + if (prev) prev->next = next; + if (next) next->prev = prev; + + prev = 0; + next = 0; } object *object::create () { - object * - op = new object; - + object *op = new object; op->link (); return op; } @@ -988,27 +979,16 @@ */ void object::free (bool free_inventory) { - if (!QUERY_FLAG (this, FLAG_REMOVED)) - { - LOG (llevDebug, "Free object called with non removed object\n"); - dump_object (this); -#ifdef MANY_CORES - abort (); -#endif - } + if (QUERY_FLAG (this, FLAG_FREED)) + return; if (QUERY_FLAG (this, FLAG_FRIENDLY)) - { - LOG (llevMonster, "Warning: tried to free friendly object.\n"); - remove_friendly_object (this); - } + remove_friendly_object (this); - if (QUERY_FLAG (this, FLAG_FREED)) - { - dump_object (this); - LOG (llevError, "Trying to free freed object.\n%s\n", errmsg); - return; - } + if (!QUERY_FLAG (this, FLAG_REMOVED)) + remove_ob (this); + + SET_FLAG (this, FLAG_FREED); if (more) { @@ -1022,30 +1002,24 @@ * 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; + object *op = inv; while (op) { - object * - tmp = op->below; - - remove_ob (op); + object *tmp = op->below; op->free (free_inventory); op = tmp; } } else { /* Put objects in inventory onto this space */ - object * - op = inv; + object *op = inv; while (op) { - object * - tmp = op->below; + object *tmp = op->below; remove_ob (op); @@ -1064,14 +1038,17 @@ } } + // 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; update_ob_speed (this); unlink (); - SET_FLAG (this, FLAG_FREED); - mortals.push_back (this); } @@ -1086,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; } @@ -1106,44 +1082,22 @@ void remove_ob (object *op) { - object * - tmp, * - last = NULL; - object * - otmp; - tag_t - tag; - int - check_walk_off; - mapstruct * - m; - sint16 - x, - y; + object *tmp, *last = 0; + object *otmp; + + int check_walk_off; + maptile *m; + sint16 x, y; if (QUERY_FLAG (op, FLAG_REMOVED)) - { - dump_object (op); - LOG (llevError, "Trying to remove removed object.\n%s\n", errmsg); + return; + + SET_FLAG (op, FLAG_REMOVED); - /* Changed it to always dump core in this case. As has been learned - * in the past, trying to recover from errors almost always - * make things worse, and this is a real error here - something - * that should not happen. - * Yes, if this was a mission critical app, trying to do something - * to recover may make sense, but that is because failure of the app - * may have other disastrous problems. Cf runs out of a script - * so is easily enough restarted without any real problems. - * MSW 2001-07-01 - */ - abort (); - } if (op->more != NULL) remove_ob (op->more); - SET_FLAG (op, FLAG_REMOVED); - /* * In this case, the object to be removed is in someones * inventory. @@ -1178,125 +1132,122 @@ op->map = op->env->map; op->above = NULL, op->below = NULL; op->env = NULL; - return; } + else if (op->map) + { + x = op->x; + y = op->y; + m = get_map_from_coord (op->map, &x, &y); + + if (!m) + { + LOG (llevError, "remove_ob called when object was on map but appears to not be within valid coordinates? %s (%d,%d)\n", + op->map->path, op->x, op->y); + /* in old days, we used to set x and y to 0 and continue. + * it seems if we get into this case, something is probablye + * screwed up and should be fixed. + */ + abort (); + } - /* If we get here, we are removing it from a map */ - if (op->map == NULL) - return; - - x = op->x; - y = op->y; - m = get_map_from_coord (op->map, &x, &y); + if (op->map != m) + LOG (llevDebug, "remove_ob: Object not really on map it claimed to be on? %s != %s, %d,%d != %d,%d\n", + op->map->path, m->path, op->x, op->y, x, y); - if (!m) - { - LOG (llevError, "remove_ob called when object was on map but appears to not be within valid coordinates? %s (%d,%d)\n", - op->map->path, op->x, op->y); - /* in old days, we used to set x and y to 0 and continue. - * it seems if we get into this case, something is probablye - * screwed up and should be fixed. + /* Re did the following section of code - it looks like it had + * lots of logic for things we no longer care about */ - abort (); - } - if (op->map != m) - { - LOG (llevDebug, "remove_ob: Object not really on map it claimed to be on? %s != %s, %d,%d != %d,%d\n", - op->map->path, m->path, op->x, op->y, x, y); - } - /* Re did the following section of code - it looks like it had - * lots of logic for things we no longer care about - */ - - /* link the object above us */ - if (op->above) - op->above->below = op->below; - else - SET_MAP_TOP (m, x, y, op->below); /* we were top, set new top */ + /* link the object above us */ + if (op->above) + op->above->below = op->below; + else + SET_MAP_TOP (m, x, y, op->below); /* we were top, set new top */ - /* Relink the object below us, if there is one */ - if (op->below) - { - op->below->above = op->above; - } - else - { - /* Nothing below, which means we need to relink map object for this space - * use translated coordinates in case some oddness with map tiling is - * evident - */ - if (GET_MAP_OB (m, x, y) != op) + /* Relink the object below us, if there is one */ + if (op->below) + op->below->above = op->above; + else { - 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); + /* Nothing below, which means we need to relink map object for this space + * use translated coordinates in case some oddness with map tiling is + * evident + */ + if (GET_MAP_OB (m, x, y) != op) + { + 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); + } + + SET_MAP_OB (m, x, y, op->above); /* goes on above it. */ } - SET_MAP_OB (m, x, y, op->above); /* goes on above it. */ - } - op->above = NULL; - op->below = NULL; - if (op->map->in_memory == MAP_SAVING) - return; + op->above = 0; + op->below = 0; - 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) - { - /* No point updating the players look faces if he is the object - * being removed. - */ + if (op->map->in_memory == MAP_SAVING) + return; + + check_walk_off = !QUERY_FLAG (op, FLAG_NO_APPLY); - if (tmp->type == PLAYER && tmp != op) + for (tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) { - /* If a container that the player is currently using somehow gets - * removed (most likely destroyed), update the player view - * appropriately. + /* No point updating the players look faces if he is the object + * being removed. */ - if (tmp->container == op) + + if (tmp->type == PLAYER && tmp != op) { - CLEAR_FLAG (op, FLAG_APPLIED); - tmp->container = NULL; + /* If a container that the player is currently using somehow gets + * removed (most likely destroyed), update the player view + * appropriately. + */ + if (tmp->container == op) + { + CLEAR_FLAG (op, FLAG_APPLIED); + tmp->container = NULL; + } + + tmp->contr->socket.update_look = 1; } - tmp->contr->socket.update_look = 1; - } - /* 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)) - { - move_apply (tmp, op, NULL); - if (was_destroyed (op, tag)) + /* 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)) { - LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " "leaving object\n", &tmp->name, &tmp->arch->name); + move_apply (tmp, op, NULL); + + 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 */ + /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ - if (tmp->above == tmp) - tmp->above = NULL; - last = tmp; - } - /* last == NULL of there are no objects on this space */ - if (last == NULL) - { - /* set P_NEED_UPDATE, otherwise update_position will complain. In theory, - * we could preserve the flags (GET_MAP_FLAGS), but update_position figures - * those out anyways, and if there are any flags set right now, they won't - * be correct anyways. - */ - SET_MAP_FLAGS (op->map, op->x, op->y, P_NEED_UPDATE); - update_position (op->map, op->x, op->y); - } - else - update_object (last, UP_OBJ_REMOVE); + if (tmp->above == tmp) + tmp->above = NULL; - if (QUERY_FLAG (op, FLAG_BLOCKSVIEW) || (op->glow_radius != 0)) - update_all_los (op->map, op->x, op->y); + last = tmp; + } + + /* last == NULL of there are no objects on this space */ + if (last == NULL) + { + /* set P_NEED_UPDATE, otherwise update_position will complain. In theory, + * we could preserve the flags (GET_MAP_FLAGS), but update_position figures + * those out anyways, and if there are any flags set right now, they won't + * be correct anyways. + */ + SET_MAP_FLAGS (op->map, op->x, op->y, P_NEED_UPDATE); + update_position (op->map, op->x, op->y); + } + else + update_object (last, UP_OBJ_REMOVE); + if (QUERY_FLAG (op, FLAG_BLOCKSVIEW) || (op->glow_radius != 0)) + update_all_los (op->map, op->x, op->y); + } } /* @@ -1307,14 +1258,15 @@ * 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) { if (!op->nrof) return 0; + if (top == NULL) for (top = op; top != NULL && top->above != NULL; top = top->above); + for (; top != NULL; top = top->below) { if (top == op) @@ -1330,7 +1282,8 @@ return top; } } - return NULL; + + return 0; } /* @@ -1338,18 +1291,19 @@ * 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; + object *tmp; if (op->head) op = op->head; + for (tmp = op; tmp; tmp = tmp->more) { tmp->x = x + tmp->arch->clone.x; tmp->y = y + tmp->arch->clone.y; } + return insert_ob_in_map (op, m, originator, flag); } @@ -1375,27 +1329,24 @@ */ 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; + object *tmp, *top, *floor = NULL; + sint16 x, y; if (QUERY_FLAG (op, FLAG_FREED)) { LOG (llevError, "Trying to insert freed object!\n"); return NULL; } + if (m == NULL) { dump_object (op); LOG (llevError, "Trying to insert in null-map!\n%s\n", errmsg); return op; } + if (out_of_map (m, op->x, op->y)) { dump_object (op); @@ -1409,18 +1360,19 @@ #endif return op; } + if (!QUERY_FLAG (op, FLAG_REMOVED)) { dump_object (op); LOG (llevError, "Trying to insert (map) inserted object.\n%s\n", errmsg); return op; } + if (op->more != NULL) { /* The part may be on a different map. */ - object * - more = op->more; + object *more = op->more; /* We really need the caller to normalize coordinates - if * we set the map, that doesn't work if the location is within @@ -1428,9 +1380,7 @@ * is clear wrong do we normalize it. */ if (OUT_OF_REAL_MAP (more->map, more->x, more->y)) - { - more->map = get_map_from_coord (m, &more->x, &more->y); - } + more->map = get_map_from_coord (m, &more->x, &more->y); else if (!more->map) { /* For backwards compatibility - when not dealing with tiled maps, @@ -1443,9 +1393,11 @@ { if (!op->head) LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n"); + return NULL; } } + CLEAR_FLAG (op, FLAG_REMOVED); /* Ideally, the caller figures this out. However, it complicates a lot @@ -1459,18 +1411,17 @@ /* this has to be done after we translate the coordinates. */ if (op->nrof && !(flag & INS_NO_MERGE)) - { - for (tmp = GET_MAP_OB (op->map, x, y); tmp != NULL; tmp = tmp->above) - if (CAN_MERGE (op, tmp)) - { - op->nrof += tmp->nrof; - remove_ob (tmp); - free_object (tmp); - } - } + for (tmp = GET_MAP_OB (op->map, x, y); tmp != NULL; tmp = tmp->above) + if (CAN_MERGE (op, tmp)) + { + op->nrof += tmp->nrof; + remove_ob (tmp); + free_object (tmp); + } CLEAR_FLAG (op, FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */ CLEAR_FLAG (op, FLAG_INV_LOCKED); + if (!QUERY_FLAG (op, FLAG_ALIVE)) CLEAR_FLAG (op, FLAG_NO_STEAL); @@ -1481,12 +1432,15 @@ LOG (llevError, "insert_ob_in_map called with INS_BELOW_ORIGINATOR when originator not on same space!\n"); abort (); } + op->above = originator; op->below = originator->below; + if (op->below) op->below->above = op; else SET_MAP_OB (op->map, op->x, op->y, op); + /* since *below* originator, no need to update top */ originator->below = op; } @@ -1495,8 +1449,7 @@ /* If there are other objects, then */ if ((!(flag & INS_MAP_LOAD)) && ((top = GET_MAP_OB (op->map, op->x, op->y)) != NULL)) { - object * - last = NULL; + object *last = NULL; /* * If there are multiple objects on this space, we do some trickier handling. @@ -1515,15 +1468,18 @@ { if (QUERY_FLAG (top, FLAG_IS_FLOOR) || QUERY_FLAG (top, FLAG_OVERLAY_FLOOR)) floor = top; + if (QUERY_FLAG (top, FLAG_NO_PICK) && (top->move_type & (MOVE_FLY_LOW | MOVE_FLY_HIGH)) && !QUERY_FLAG (top, FLAG_IS_FLOOR)) { /* We insert above top, so we want this object below this */ top = top->below; break; } + last = top; top = top->above; } + /* Don't want top to be NULL, so set it to the last valid object */ top = last; @@ -1553,8 +1509,10 @@ top = last->below; } } /* If objects on this space */ + if (flag & INS_MAP_LOAD) top = GET_MAP_TOP (op->map, op->x, op->y); + if (flag & INS_ABOVE_FLOOR_ONLY) top = floor; @@ -1565,19 +1523,24 @@ if (!top) { op->above = GET_MAP_OB (op->map, op->x, op->y); + if (op->above) op->above->below = op; + op->below = NULL; SET_MAP_OB (op->map, op->x, op->y, op); } else { /* get inserted into the stack above top */ op->above = top->above; + if (op->above) op->above->below = op; + op->below = top; top->above = op; } + if (op->above == NULL) SET_MAP_TOP (op->map, op->x, op->y, op); } /* else not INS_BELOW_ORIGINATOR */ @@ -1590,10 +1553,8 @@ */ if (!(flag & INS_MAP_LOAD)) for (tmp = floor ? floor : GET_MAP_OB (op->map, op->x, op->y); tmp != NULL; tmp = tmp->above) - { - if (tmp->type == PLAYER) - tmp->contr->socket.update_look = 1; - } + if (tmp->type == PLAYER) + tmp->contr->socket.update_look = 1; /* If this object glows, it may affect lighting conditions that are * visible to others on this map. But update_all_los is really @@ -1607,11 +1568,9 @@ if (MAP_DARKNESS (op->map) && (op->glow_radius != 0)) update_all_los (op->map, op->x, op->y); - /* updates flags (blocked, alive, no magic, etc) for this map space */ update_object (op, UP_OBJ_INSERT); - /* Don't know if moving this to the end will break anything. However, * we want to have update_look set above before calling this. * @@ -1622,7 +1581,6 @@ */ /* if this is not the head or flag has been passed, don't check walk on status */ - if (!(flag & INS_NO_WALK_ON) && !op->head) { if (check_move_on (op, originator)) @@ -1635,6 +1593,7 @@ if (check_move_on (tmp, originator)) return NULL; } + return op; } @@ -1661,8 +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; @@ -1690,7 +1648,9 @@ sprintf (errmsg, "There are only %d %ss.", orig_ob->nrof ? orig_ob->nrof : 1, &orig_ob->name); return NULL; } + newob = object_create_clone (orig_ob); + if ((orig_ob->nrof -= nr) < 1) { if (!is_removed) @@ -1708,6 +1668,7 @@ return NULL; } } + newob->nrof = nr; return newob; @@ -1724,10 +1685,8 @@ object * decrease_ob_nr (object *op, uint32 i) { - object * - tmp; - player * - pl; + object *tmp; + player *pl; if (i == 0) /* objects with op->nrof require this check */ return op; @@ -1736,9 +1695,7 @@ i = op->nrof; if (QUERY_FLAG (op, FLAG_REMOVED)) - { - op->nrof -= i; - } + op->nrof -= i; else if (op->env != NULL) { /* is this object in the players inventory, or sub container @@ -1783,18 +1740,16 @@ } else { - object * - above = op->above; + object *above = op->above; if (i < op->nrof) - { - op->nrof -= i; - } + op->nrof -= i; else { remove_ob (op); op->nrof = 0; } + /* Since we just removed op, op->above is null */ for (tmp = above; tmp != NULL; tmp = tmp->above) if (tmp->type == PLAYER) @@ -1807,9 +1762,7 @@ } if (op->nrof) - { - return op; - } + return op; else { free_object (op); @@ -1828,9 +1781,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; } @@ -1863,22 +1815,26 @@ LOG (llevError, "Trying to insert (ob) inserted object.\n%s\n", errmsg); return op; } + if (where == NULL) { dump_object (op); LOG (llevError, "Trying to put object in NULL.\n%s\n", errmsg); return op; } + if (where->head) { LOG (llevDebug, "Warning: Tried to insert object wrong part of multipart object.\n"); where = where->head; } + if (op->more) { LOG (llevError, "Tried to insert multipart object %s (%d)\n", &op->name, op->count); return op; } + CLEAR_FLAG (op, FLAG_OBJ_ORIGINAL); CLEAR_FLAG (op, FLAG_REMOVED); if (op->nrof) @@ -1973,24 +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; - MoveType - move_on, - move_slow, - move_block; + object *tmp; + maptile *m = op->map; + int x = op->x, y = op->y; + + 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); @@ -2024,7 +1971,8 @@ if ((tmp->move_type & MOVE_FLY_LOW) && QUERY_FLAG (tmp, FLAG_NO_PICK)) break; } - for (; tmp != NULL; tmp = tmp->below) + + for (; tmp; tmp = tmp->below) { if (tmp == op) continue; /* Can't apply yourself */ @@ -2042,17 +1990,13 @@ { float - diff; + diff = tmp->move_slow_penalty * FABS (op->speed); - diff = tmp->move_slow_penalty * FABS (op->speed); if (op->type == PLAYER) - { - if ((QUERY_FLAG (tmp, FLAG_IS_HILLY) && find_skill_by_number (op, SK_CLIMBING)) || - (QUERY_FLAG (tmp, FLAG_IS_WOODED) && find_skill_by_number (op, SK_WOODSMAN))) - { - diff /= 4.0; - } - } + if ((QUERY_FLAG (tmp, FLAG_IS_HILLY) && find_skill_by_number (op, SK_CLIMBING)) || + (QUERY_FLAG (tmp, FLAG_IS_WOODED) && find_skill_by_number (op, SK_WOODSMAN))) + diff /= 4.0; + op->speed_left -= diff; } } @@ -2061,9 +2005,9 @@ if ((!op->move_type && tmp->move_on & MOVE_WALK) || ((op->move_type & tmp->move_on) && (op->move_type & ~tmp->move_on & ~tmp->move_block) == 0)) { - 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 @@ -2074,6 +2018,7 @@ return 0; } } + return 0; } @@ -2084,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; @@ -2107,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; @@ -2257,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, @@ -2288,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; @@ -2369,21 +2314,19 @@ */ int -find_dir (mapstruct *m, int x, int y, object *exclude) +find_dir (maptile *m, int x, int y, object *exclude) { int i, max = SIZEOFFREE, mflags; - sint16 - nx, - ny; + + sint16 nx, ny; object * tmp; - mapstruct * + maptile * mp; - MoveType - blocked, - move_type; + + MoveType blocked, move_type; if (exclude && exclude->head) { @@ -2598,11 +2541,9 @@ 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; + sint16 dx, dy; int mflags; @@ -2690,61 +2631,41 @@ 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); op = get_object (); - object_thawer - thawer (filename); + object_thawer thawer (filename); if (thawer) load_object (thawer, op, 0); @@ -2762,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) @@ -2781,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; } @@ -2805,34 +2720,27 @@ const char * get_ob_key_value (const object *op, const char *const key) { - key_value * - link; - const char * - canonical_key; - - canonical_key = shstr::find (key); + key_value *link; + shstr_cmp canonical_key (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; } @@ -2878,8 +2786,7 @@ else op->key_values = field->next; - delete - field; + delete field; } } return TRUE; @@ -2924,8 +2831,58 @@ int set_ob_key_value (object *op, const char *key, const char *value, int add_key) { - shstr - key_ (key); + shstr key_ (key); 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); +} +