--- deliantra/server/common/object.C 2008/04/21 23:35:24 1.212 +++ deliantra/server/common/object.C 2008/04/30 08:29:31 1.223 @@ -332,6 +332,42 @@ return 1; } +// find player who can see this object +object * +object::visible_to () const +{ + if (client_visible () && !flag [FLAG_REMOVED]) + { + // see if we are in a container of sorts + if (env) + { + // the player inventory itself is always visible + if (env->type == PLAYER) + return env; + + // else a player could have our env open + object *envest = env->outer_env (); + + // the player itself is always on a map, so we will find him here + // even if our inv is in a player. + if (envest->is_on_map ()) + if (object *pl = envest->ms ().player ()) + if (pl->container == env) + return pl; + } + else + { + // maybe there is a player standing on the same mapspace + // this will catch the case where "this" is a player + if (object *pl = ms ().player ()) + if (!pl->container || this == pl->container) + return pl; + } + } + + return 0; +} + // adjust weight per container type ("of holding") static sint32 weight_adjust (object *op, sint32 weight) @@ -358,7 +394,8 @@ op->carrying += weight; if (object *pl = op->visible_to ()) - esrv_update_item (UPD_WEIGHT, pl, op); + if (pl != op) // player is handled lazily + esrv_update_item (UPD_WEIGHT, pl, op); op = op->env; } @@ -389,7 +426,8 @@ carrying = sum; if (object *pl = visible_to ()) - esrv_update_item (UPD_WEIGHT, pl, this); + if (pl != this) // player is handled lazily + esrv_update_item (UPD_WEIGHT, pl, this); } } @@ -710,14 +748,14 @@ void update_object (object *op, int action) { - if (op == NULL) + if (!op) { /* this should never happen */ - LOG (llevDebug, "update_object() called for NULL object.\n"); + LOG (llevError | logBacktrace, "update_object() called for NULL object.\n"); return; } - if (op->env) + if (!op->is_on_map ()) { /* Animation is currently handled by client, so nothing * to do in this case. @@ -725,12 +763,6 @@ return; } - /* If the map is saving, don't do anything as everything is - * going to get freed anyways. - */ - if (!op->map || op->map->in_memory == MAP_SAVING) - return; - /* make sure the object is within map boundaries */ if (op->x < 0 || op->x >= op->map->width || op->y < 0 || op->y >= op->map->height) { @@ -882,7 +914,7 @@ // need to check first, because the checks below might segfault // as we might be on an invalid mapspace and crossfire code // is too buggy to ensure that the inventory is empty. - // corollary: if you create arrows etc. with stuff in tis inventory, + // corollary: if you create arrows etc. with stuff in its inventory, // cf will crash below with off-map x and y if (!inv) return; @@ -899,7 +931,7 @@ { while (inv) { - inv->destroy_inv (drop_to_ground); + inv->destroy_inv (false); inv->destroy (); } } @@ -915,7 +947,7 @@ || op->type == TRAP || op->flag [FLAG_IS_A_TEMPLATE] || op->flag [FLAG_DESTROY_ON_DEATH]) - op->destroy (); + op->destroy (true); else map->insert (op, x, y); } @@ -929,24 +961,33 @@ return op; } -void -object::do_destroy () +static struct freed_map : maptile { - if (object *pl = visible_to ()) - esrv_del_item (pl->contr, count); + freed_map () + { + path = ""; + name = "/internal/freed_objects_map"; + width = 3; + height = 3; + nodrop = 1; - attachable::do_destroy (); + alloc (); + in_memory = MAP_ACTIVE; + } +} freed_map; // freed objects are moved here to avoid crashes +void +object::do_destroy () +{ if (flag [FLAG_IS_LINKED]) remove_button_link (this); if (flag [FLAG_FRIENDLY]) remove_friendly_object (this); - if (!flag [FLAG_REMOVED]) - remove (); + remove (); - destroy_inv (true); + attachable::do_destroy (); deactivate (); unlink (); @@ -954,27 +995,9 @@ flag [FLAG_FREED] = 1; // 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->path = ""; - freed_map->name = "/internal/freed_objects_map"; - freed_map->width = 3; - freed_map->height = 3; - freed_map->nodrop = 1; - - freed_map->alloc (); - freed_map->in_memory = MAP_ACTIVE; - } - - map = freed_map; - x = 1; - y = 1; - } + map = &freed_map; + x = 1; + y = 1; if (more) { @@ -997,8 +1020,14 @@ if (destroyed ()) return; - if (destroy_inventory) - destroy_inv (false); + if (!is_head () && !head->destroyed ()) + { + LOG (llevError | logBacktrace, "tried to destroy the tail of an object"); + head->destroy (destroy_inventory); + return; + } + + destroy_inv (!destroy_inventory); if (is_head ()) if (sound_destroy) @@ -1022,12 +1051,13 @@ object *tmp, *last = 0; object *otmp; - if (QUERY_FLAG (this, FLAG_REMOVED)) + if (flag [FLAG_REMOVED]) return; - SET_FLAG (this, FLAG_REMOVED); INVOKE_OBJECT (REMOVE, this); + flag [FLAG_REMOVED] = true; + if (more) more->remove (); @@ -1037,6 +1067,11 @@ */ if (env) { + flag [FLAG_REMOVED] = false; // hack around the issue of visible_to checking flag_removed + if (object *pl = visible_to ()) + 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 ()); *(above ? &above->below : &env->inv) = below; @@ -1064,20 +1099,30 @@ } else if (map) { - if (type == PLAYER) + map->dirty = true; + mapspace &ms = this->ms (); + + if (object *pl = ms.player ()) { - // leaving a spot always closes any open container on the ground - if (container && !container->env) - // this causes spurious floorbox updates, but it ensures - // that the CLOSE event is being sent. - close_container (); + if (type == PLAYER) // this == pl(!) + { + // leaving a spot always closes any open container on the ground + if (container && !container->env) + // this causes spurious floorbox updates, but it ensures + // that the CLOSE event is being sent. + close_container (); - --map->players; - map->touch (); - } + --map->players; + map->touch (); + } + else if (pl->container == this) + { + // removing a container should close it + close_container (); + } - map->dirty = true; - mapspace &ms = this->ms (); + esrv_del_item (pl->contr, count); + } /* link the object above us */ if (above) @@ -1117,7 +1162,8 @@ */ pl->close_container (); - pl->contr->ns->floorbox_update (); + //TODO: the floorbox prev/next might need updating + esrv_del_item (pl->contr, count); } for (tmp = ms.bot; tmp; tmp = tmp->above) @@ -1171,20 +1217,20 @@ ; for (; top; top = top->below) - { - if (top == op) - continue; + if (object::can_merge (op, top)) + { + top->nrof += op->nrof; - if (object::can_merge (op, top)) - { - top->nrof += op->nrof; + if (object *pl = top->visible_to ()) + esrv_update_item (UPD_NROF, pl, top); -/* CLEAR_FLAG(top,FLAG_STARTEQUIP);*/ - op->weight = 0; /* Don't want any adjustements now */ - op->destroy (); - return top; - } - } + op->weight = 0; // cancel the addition above + op->carrying = 0; // must be 0 already + + op->destroy (1); + + return top; + } return 0; } @@ -1253,8 +1299,6 @@ { assert (!op->flag [FLAG_FREED]); - object *top, *floor = NULL; - op->remove (); /* Ideally, the caller figures this out. However, it complicates a lot @@ -1263,7 +1307,7 @@ */ if (!xy_normalise (m, op->x, op->y)) { - op->destroy (); + op->destroy (1); return 0; } @@ -1282,6 +1326,9 @@ for (object *tmp = ms.bot; tmp; tmp = tmp->above) if (object::can_merge (op, tmp)) { + // TODO: we atcually want to update tmp, not op, + // but some caller surely breaks when we return tmp + // from here :/ op->nrof += tmp->nrof; tmp->destroy (1); } @@ -1313,6 +1360,8 @@ } else { + object *top, *floor = NULL; + top = ms.bot; /* If there are other objects, then */ @@ -1421,7 +1470,8 @@ op->map->dirty = true; if (object *pl = ms.player ()) - pl->contr->ns->floorbox_update (); + //TODO: the floorbox prev/next might need updating + esrv_send_item (pl, op); /* If this object glows, it may affect lighting conditions that are * visible to others on this map. But update_all_los is really @@ -1496,41 +1546,6 @@ return where->map->insert (this, where->x, where->y, originator, flags); } -// find player who can see this object -object * -object::visible_to () const -{ - if (!flag [FLAG_REMOVED]) - { - // see if we are in a container of sorts - if (env) - { - // the player inventory itself is always visible - if (env->type == PLAYER) - return env; - - // else a player could have our env open - object *envest = env->outer_env (); - - // the player itself is always on a map, so we will find him here - // even if our inv is in a player. - if (envest->is_on_map ()) - if (object *pl = envest->ms ().player ()) - if (pl->container == env) - return pl; - } - else - { - // maybe there is a player standing on the same mapspace - // this will catch the case where "this" is a player - if (object *pl = ms ().player ()) - return pl; - } - } - - return 0; -} - /* * decrease(object, number) decreases a specified number from * the amount of an object. If the amount reaches 0, the object @@ -1640,6 +1655,10 @@ /* return the original object and remove inserted object (client needs the original object) */ tmp->nrof += op->nrof; + + if (object *pl = tmp->visible_to ()) + esrv_update_item (UPD_NROF, pl, tmp); + adjust_weight (this, op->total_weight ()); op->destroy (1); @@ -1663,6 +1682,9 @@ op->flag [FLAG_REMOVED] = 0; + if (object *pl = op->visible_to ()) + esrv_send_item (pl, op); + adjust_weight (this, op->total_weight ()); inserted: @@ -1670,9 +1692,9 @@ if (op->glow_radius && map && map->darkness) update_all_los (map, x, y); - if (object *otmp = in_player ()) - if (otmp->contr && !QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER)) - otmp->update_stats (); + // if this is a player's inventory, update stats + if (type == PLAYER && !flag [FLAG_NO_FIX_PLAYER]) + update_stats (); INVOKE_OBJECT (INSERT, this); @@ -2560,7 +2582,7 @@ title ? (const char *)title : "", flag_desc (flagdesc, 512), type); - if (!this->flag[FLAG_REMOVED] && env) + if (!flag[FLAG_REMOVED] && env) p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); if (map) @@ -2600,7 +2622,9 @@ if (container == new_container) return; - if (object *old_container = container) + object *old_container = container; + + if (old_container) { if (INVOKE_OBJECT (CLOSE, old_container, ARG_OBJECT (this))) return; @@ -2612,10 +2636,15 @@ closer->destroy (); #endif - old_container->flag [FLAG_APPLIED] = 0; + // make sure the container is available + esrv_send_item (this, old_container); + + old_container->flag [FLAG_APPLIED] = false; container = 0; + // client needs item update to make it work, client bug requires this to be separate esrv_update_item (UPD_FLAGS, this, old_container); + new_draw_info_format (NDI_UNIQUE, 0, this, "You close %s.", query_name (old_container)); play_sound (sound_find ("chest_close")); } @@ -2638,13 +2667,19 @@ new_draw_info_format (NDI_UNIQUE, 0, this, "You open %s.", query_name (new_container)); - new_container->flag [FLAG_APPLIED] = 1; + // make sure the container is available, client bug requires this to be separate + esrv_send_item (this, new_container); + + new_container->flag [FLAG_APPLIED] = true; container = new_container; + // client needs flag change esrv_update_item (UPD_FLAGS, this, new_container); esrv_send_inventory (this, new_container); play_sound (sound_find ("chest_open")); } +// else if (!old_container->env && contr && contr->ns) +// contr->ns->floorbox_reset (); } object *