--- deliantra/server/common/object.C 2008/05/04 11:12:40 1.231 +++ deliantra/server/common/object.C 2008/05/06 21:04:37 1.237 @@ -356,7 +356,7 @@ // adjust weight per container type ("of holding") static sint32 -weight_adjust (object *op, sint32 weight) +weight_adjust_for (object *op, sint32 weight) { return op->type == CONTAINER ? lerp (weight, 0, 100, 0, 100 - op->stats.Str) @@ -372,7 +372,10 @@ { while (op) { - weight = weight_adjust (op, weight); + // adjust by actual difference to account for rounding errors + // i.e. (w2 - w1) / f != w2 / f - w1 / f and the latter is correct + weight = weight_adjust_for (op, op->carrying) + - weight_adjust_for (op, op->carrying - weight); if (!weight) return; @@ -405,7 +408,7 @@ sum += op->total_weight (); } - sum = weight_adjust (this, sum); + sum = weight_adjust_for (this, sum); if (sum != carrying) { @@ -1056,11 +1059,6 @@ adjust_weight (env, -total_weight ()); - *(above ? &above->below : &env->inv) = below; - - if (below) - below->above = above; - /* we set up values so that it could be inserted into * the map, but we don't actually do that - it is up * to the caller to decide what we want to do. @@ -1068,6 +1066,11 @@ map = env->map; x = env->x; y = env->y; + + // make sure cmov optimisation is applicable + *(above ? &above->below : &env->inv) = below; + *(below ? &below->above : &above ) = above; // &above is just a dummy + above = 0; below = 0; env = 0; @@ -1107,25 +1110,9 @@ } /* link the object above us */ - if (above) - above->below = below; - else - ms.top = below; /* we were top, set new top */ - - /* Relink the object below us, if there is one */ - if (below) - below->above = 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 (map, x, y) != this) - LOG (llevError, "remove_ob: GET_MAP_OB does not return object to be removed even though it appears to be on the bottom? %s\n", debug_desc ()); - - ms.bot = above; /* goes on above it. */ - } + // re-link, make sure compiler can easily use cmove + *(above ? &above->below : &ms.top) = below; + *(below ? &below->above : &ms.bot) = above; above = 0; below = 0; @@ -1282,8 +1269,6 @@ object * insert_ob_in_map (object *op, maptile *m, object *originator, int flag) { - assert (!op->flag [FLAG_FREED]); - op->remove (); /* Ideally, the caller figures this out. However, it complicates a lot @@ -1292,7 +1277,7 @@ */ if (!xy_normalise (m, op->x, op->y)) { - op->destroy (1); + op->head_ ()->destroy (1);// remove head_ once all tail object destroyers found return 0; } @@ -1311,7 +1296,7 @@ for (object *tmp = ms.bot; tmp; tmp = tmp->above) if (object::can_merge (op, tmp)) { - // TODO: we atcually want to update tmp, not op, + // TODO: we actually want to update tmp, not op, // but some caller surely breaks when we return tmp // from here :/ op->nrof += tmp->nrof; @@ -1326,7 +1311,7 @@ if (flag & INS_BELOW_ORIGINATOR) { - if (originator->map != op->map || originator->x != op->x || originator->y != op->y) + if (originator->map != op->map || originator->x != op->x || originator->y != op->y || !originator->is_on_map ()) { LOG (llevError, "insert_ob_in_map called with INS_BELOW_ORIGINATOR when originator not on same space!\n"); abort (); @@ -1334,26 +1319,18 @@ op->above = originator; op->below = originator->below; - - if (op->below) - op->below->above = op; - else - ms.bot = op; - - /* since *below* originator, no need to update top */ originator->below = op; + + *(op->below ? &op->below->above : &ms.bot) = op; } else { - object *top, *floor = NULL; - - top = ms.bot; + object *floor = 0; + object *top = ms.top; /* If there are other objects, then */ if (top) { - object *last = 0; - /* * If there are multiple objects on this space, we do some trickier handling. * We've already dealt with merging if appropriate. @@ -1366,24 +1343,21 @@ * when lots of spells are cast in one area. Currently, it is presumed * that flying non pickable objects are spell objects. */ - for (top = ms.bot; top; top = top->above) + for (object *tmp = ms.bot; tmp; tmp = tmp->above) { - if (QUERY_FLAG (top, FLAG_IS_FLOOR) || QUERY_FLAG (top, FLAG_OVERLAY_FLOOR)) - floor = top; + if (QUERY_FLAG (tmp, FLAG_IS_FLOOR) || QUERY_FLAG (tmp, FLAG_OVERLAY_FLOOR)) + floor = tmp; - if (QUERY_FLAG (top, FLAG_NO_PICK) && (top->move_type & (MOVE_FLY_LOW | MOVE_FLY_HIGH)) && !QUERY_FLAG (top, FLAG_IS_FLOOR)) + if (QUERY_FLAG (tmp, FLAG_NO_PICK) && (tmp->move_type & (MOVE_FLY_LOW | MOVE_FLY_HIGH)) && !QUERY_FLAG (tmp, FLAG_IS_FLOOR)) { /* We insert above top, so we want this object below this */ - top = top->below; + top = tmp->below; break; } - last = top; + top = tmp; } - /* Don't want top to be NULL, so set it to the last valid object */ - top = last; - /* We let update_position deal with figuring out what the space * looks like instead of lots of conditions here. * makes things faster, and effectively the same result. @@ -1399,6 +1373,8 @@ && ms.flags () & P_BLOCKSVIEW && (op->face && !faces [op->face].visibility)) { + object *last; + for (last = top; last != floor; last = last->below) if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT)) break; @@ -1419,30 +1395,23 @@ /* Top is the object that our object (op) is going to get inserted above. */ - /* First object on this space */ + /* no top found, insert on top */ if (!top) { - op->above = ms.bot; - - if (op->above) - op->above->below = op; + op->above = 0; + op->below = ms.top; + ms.top = op; - op->below = 0; - ms.bot = op; + *(op->below ? &op->below->above : &ms.bot) = op; } else { /* get inserted into the stack above top */ op->above = top->above; - - if (op->above) - op->above->below = op; + top->above = op; op->below = top; - top->above = op; + *(op->above ? &op->above->below : &ms.top) = op; } - - if (!op->above) - ms.top = op; } /* else not INS_BELOW_ORIGINATOR */ if (op->type == PLAYER)