--- deliantra/server/common/object.C 2010/04/14 21:36:31 1.325 +++ deliantra/server/common/object.C 2010/05/06 21:45:49 1.337 @@ -428,37 +428,35 @@ } // adjust weight per container type ("of holding") -static sint32 -weight_adjust_for (object *op, sint32 weight) +static uint32 +weight_adjust_for (object *op, uint32 weight) { return op->type == CONTAINER - ? lerp (weight, 0, 100, 0, 100 - op->stats.Str) + ? weight - weight * op->stats.Str / 100 : weight; } /* - * adjust_weight(object, weight) adds the specified weight to an object, + * subtracts, then adds, the specified weight to an object, * and also updates how much the environment(s) is/are carrying. */ static void -adjust_weight (object *op, sint32 weight) +adjust_weight (object *op, sint32 sub, sint32 add) { while (op) { - // 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); + sint32 ocarrying = op->carrying; - if (!weight) - return; - - op->carrying += weight; + op->carrying -= weight_adjust_for (op, sub); + op->carrying += weight_adjust_for (op, add); if (object *pl = op->visible_to ()) if (pl != op) // player is handled lazily esrv_update_item (UPD_WEIGHT, pl, op); + sub = ocarrying; + add = op->carrying; + op = op->env; } } @@ -475,16 +473,17 @@ for (object *op = inv; op; op = op->below) { - if (op->inv) - op->update_weight (); + op->update_weight (); - sum += op->total_weight (); + sum += weight_adjust_for (this, op->total_weight ()); } - sum = weight_adjust_for (this, sum); - if (sum != carrying) { + if (carrying != sum && carrying)//D + LOG (llevDebug, "updating carrying got %ld, expected %ld (%s)\n", + (long long)sum, (long long)carrying, debug_desc ()); + carrying = sum; if (object *pl = visible_to ()) @@ -1033,15 +1032,13 @@ static struct freed_map : maptile { freed_map () + : maptile (3, 3) { path = ""; name = "/internal/freed_objects_map"; - width = 3; - height = 3; no_drop = 1; no_reset = 1; - alloc (); in_memory = MAP_ACTIVE; } @@ -1144,7 +1141,7 @@ esrv_del_item (pl->contr, count); flag [FLAG_REMOVED] = true; // hack around the issue of visible_to checking flag_removed - adjust_weight (env, -total_weight ()); + adjust_weight (env, total_weight (), 0); object *pl = in_player (); @@ -1572,14 +1569,14 @@ /* if this is not the head or flag has been passed, don't check walk on status */ if (!(flag & INS_NO_WALK_ON) && op->is_head ()) { - if (check_move_on (op, originator)) + if (check_move_on (op, originator, flag)) return 0; - /* If we are a multi part object, lets work our way through the check + /* If we are a multi part object, let's work our way through the check * walk on's. */ for (object *tmp = op->more; tmp; tmp = tmp->more) - if (check_move_on (tmp, originator)) + if (check_move_on (tmp, originator, flag)) return 0; } @@ -1655,12 +1652,15 @@ if (nrof > nr) { + sint64 oweight = total_weight (); + nrof -= nr; - adjust_weight (env, -weight * max (1, nr)); // carrying == 0 if (object *pl = visible_to ()) esrv_update_item (UPD_NROF, pl, this); + adjust_weight (env, oweight, total_weight ()); + return true; } else @@ -1744,13 +1744,17 @@ if (object::can_merge (tmp, op)) { /* return the original object and remove inserted object - (client needs the original object) */ + (client prefers the original object) */ + + // carring must be 0 for mergable objects + sint64 oweight = tmp->weight * tmp->nrof; + tmp->nrof += op->nrof; if (object *pl = tmp->visible_to ()) esrv_update_item (UPD_NROF, pl, tmp); - adjust_weight (this, op->total_weight ()); + adjust_weight (this, oweight, tmp->weight * tmp->nrof); op->destroy (); op = tmp; @@ -1776,7 +1780,7 @@ if (object *pl = op->visible_to ()) esrv_send_item (pl, op); - adjust_weight (this, op->total_weight ()); + adjust_weight (this, 0, op->total_weight ()); inserted: /* reset the light list and los of the players on the map */ @@ -1815,7 +1819,7 @@ * on top. */ int -check_move_on (object *op, object *originator) +check_move_on (object *op, object *originator, int flags) { if (op->flag [FLAG_NO_APPLY]) return 0; @@ -1884,6 +1888,11 @@ 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)) { + if ((flags & INS_NO_AUTO_EXIT) + && (tmp->type == EXIT || tmp->type == TELEPORTER + || tmp->type == HOLE || tmp->type == TRAPDOOR)) //TODO: temporary, fix exits instead + continue; + move_apply (tmp, op, originator); if (op->destroyed ()) @@ -2218,45 +2227,84 @@ } /* - * find_dir_2(delta-x,delta-y) will return a direction in which - * an object which has subtracted the x and y coordinates of another - * object, needs to travel toward it. + * find_dir_2(delta-x,delta-y) will return a direction value + * for running into direct [dx, dy]. + * (the opposite of crossfire's find_dir_2!) */ int find_dir_2 (int x, int y) { +#if 1 // new algorithm + // this works by putting x, y into 16 sectors, which + // are not equal sized, but are a better approximation + // then the old algorithm, and then using a mapping + // table to map it into a direction value. + // basically, it maps these comparisons to each bit + // bit #3: x < 0 + // bit #2: y < 0 + // bit #1: x > y + // bit #0: x > 2y + + static const uint8 dir[16] = { + 4, 5, 4, 3, + 2, 1, 2, 3, + 6, 5, 6, 7, + 8, 1, 8, 7, + }; + int sector = 0; + + // this is a bit ugly, but more likely to result in branchless code + sector |= x < 0 ? 8 : 0; + x = x < 0 ? -x : x; // abs + + sector |= y < 0 ? 4 : 0; + y = y < 0 ? -y : y; // abs + + if (x > y) + { + sector |= 2; + + if (x > y * 2) + sector |= 1; + } + else + { + if (y > x * 2) + sector |= 1; + else if (!y) + return 0; // x == 0 here + } + + return dir [sector]; +#else // old algorithm int q; if (y) - q = x * 100 / y; + q = 128 * x / y; else if (x) - q = -300 * x; + q = -512 * x; // to make it > 309 else return 0; if (y > 0) { - if (q < -242) - return 3; - if (q < -41) - return 2; - if (q < 41) - return 1; - if (q < 242) - return 8; - return 7; - } + if (q < -309) return 7; + if (q < -52) return 6; + if (q < 52) return 5; + if (q < 309) return 4; - if (q < -242) - return 7; - if (q < -41) - return 6; - if (q < 41) - return 5; - if (q < 242) - return 4; + return 3; + } + else + { + if (q < -309) return 3; + if (q < -52) return 2; + if (q < 52) return 1; + if (q < 309) return 8; - return 3; + return 7; + } +#endif } /* @@ -2266,13 +2314,9 @@ int dirdiff (int dir1, int dir2) { - int d; - - d = abs (dir1 - dir2); - if (d > 4) - d = 8 - d; + int d = abs (dir1 - dir2); - return d; + return d > 4 ? 8 - d : d; } /* peterm: