--- deliantra/server/common/object.C 2008/04/15 14:21:04 1.206 +++ deliantra/server/common/object.C 2008/04/20 23:25:09 1.207 @@ -24,6 +24,7 @@ /* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects. sub/add_weight will transcend the environment updating the carrying variable. */ + #include #include #include @@ -328,44 +329,59 @@ return 1; } +static sint32 +weight_adjust (object *op, sint32 weight) +{ + return op->type == CONTAINER + ? lerp (weight, 0, 100, 0, 100 - op->stats.Str) + : weight; +} + /* - * sum_weight() is a recursive function which calculates the weight - * an object is carrying. It goes through in figures out how much - * containers are carrying, and sums it up. + * add_weight(object, weight) adds the specified weight to an object, + * and also updates how much the environment(s) is/are carrying. */ -long -sum_weight (object *op) +static void +add_weight (object *op, sint32 weight) { - long sum; - object *inv; - - for (sum = 0, inv = op->inv; inv; inv = inv->below) + while (op) { - if (inv->inv) - sum_weight (inv); + weight = weight_adjust (op, weight); - sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); + op->carrying += weight; + op = op->env; } - - if (op->type == CONTAINER && op->stats.Str) - sum = (sum * (100 - op->stats.Str)) / 100; - - if (op->carrying != sum) - op->carrying = sum; - - return sum; } -/** - * Return the outermost environment object for a given object. +/* + * sub_weight() recursively (outwards) subtracts a number from the + * weight of an object (and what is carried by it's environment(s)). */ +static void +sub_weight (object *op, long weight) +{ + add_weight (op, -weight); +} -object * -object_get_env_recursive (object *op) +/* + * this is a recursive function which calculates the weight + * an object is carrying. It goes through op and figures out how much + * containers are carrying, and sums it up. + */ +void +object::update_weight () { - while (op->env != NULL) - op = op->env; - return op; + sint32 sum = 0; + + for (object *op = inv; op; op = op->below) + { + if (op->inv) + op->update_weight (); + + sum += op->total_weight (); + } + + carrying = weight_adjust (this, sum); } /* @@ -978,23 +994,6 @@ attachable::destroy (); } -/* - * sub_weight() recursively (outwards) subtracts a number from the - * weight of an object (and what is carried by it's environment(s)). - */ -void -sub_weight (object *op, signed long weight) -{ - while (op != NULL) - { - if (op->type == CONTAINER) - weight = (signed long) (weight * (100 - op->stats.Str) / 100); - - op->carrying -= weight; - op = op->env; - } -} - /* op->remove (): * This function removes the object op from the linked list of objects * which it is currently tied to. When this function is done, the @@ -1614,23 +1613,6 @@ } } -/* - * add_weight(object, weight) adds the specified weight to an object, - * and also updates how much the environment(s) is/are carrying. - */ -void -add_weight (object *op, signed long weight) -{ - while (op != NULL) - { - if (op->type == CONTAINER) - weight = (signed long) (weight * (100 - op->stats.Str) / 100); - - op->carrying += weight; - op = op->env; - } -} - object * insert_ob_in_ob (object *op, object *where) {