--- deliantra/server/common/object.C 2006/12/26 10:51:52 1.91 +++ deliantra/server/common/object.C 2007/01/03 00:21:35 1.105 @@ -192,7 +192,6 @@ * Improvements made with merge: Better checking on potion, and also * check weight */ - bool object::can_merge_slow (object *ob1, object *ob2) { /* A couple quicksanity checks */ @@ -279,7 +278,7 @@ * be animated or have a very low speed. Is this an attempted monster * check? */ - if (!QUERY_FLAG (ob1, FLAG_ANIMATE) && FABS ((ob1)->speed) > MIN_ACTIVE_SPEED) + if (!QUERY_FLAG (ob1, FLAG_ANIMATE) && ob1->has_active_speed ()) return 0; switch (ob1->type) @@ -540,12 +539,12 @@ * to the closest player being on the other side, this function can * be called to update the face variable, _and_ how it looks on the map. */ - void update_turn_face (object *op) { - if (!QUERY_FLAG (op, FLAG_IS_TURNABLE) || op->arch == NULL) + if (!QUERY_FLAG (op, FLAG_IS_TURNABLE) || !op->arch) return; + SET_ANIMATION (op, op->direction); update_object (op, UP_OBJ_FACE); } @@ -558,11 +557,6 @@ void object::set_speed (float speed) { - extern int arch_init; - - /* No reason putting the archetypes objects on the speed list, - * since they never really need to be updated. - */ if (flag [FLAG_FREED] && speed) { LOG (llevError, "Object %s is freed but has speed.\n", &name); @@ -571,79 +565,10 @@ this->speed = speed; - if (arch_init) - return; - - if (FABS (speed) > MIN_ACTIVE_SPEED) - { - /* If already on active list, don't do anything */ - if (active_next || active_prev || this == active_objects) - return; - - /* process_events() expects us to insert the object at the beginning - * of the list. */ - active_next = active_objects; - - if (active_next) - active_next->active_prev = this; - - active_objects = this; - } - else - { - /* If not on the active list, nothing needs to be done */ - if (!active_next && !active_prev && this != active_objects) - return; - - if (!active_prev) - { - active_objects = active_next; - - if (active_next) - active_next->active_prev = 0; - } - else - { - active_prev->active_next = active_next; - - if (active_next) - active_next->active_prev = active_prev; - } - - active_next = 0; - active_prev = 0; - } -} - -/* This function removes object 'op' from the list of active - * objects. - * This should only be used for style maps or other such - * reference maps where you don't want an object that isn't - * in play chewing up cpu time getting processed. - * The reverse of this is to call update_ob_speed, which - * will do the right thing based on the speed of the object. - */ -void -remove_from_active_list (object *op) -{ - /* If not on the active list, nothing needs to be done */ - if (!op->active_next && !op->active_prev && op != active_objects) - return; - - if (op->active_prev == NULL) - { - active_objects = op->active_next; - if (op->active_next != NULL) - op->active_next->active_prev = NULL; - } + if (has_active_speed ()) + activate (); 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; + deactivate (); } /* @@ -701,7 +626,7 @@ mapspace &m = op->ms (); - if (m.flags_ & P_NEED_UPDATE) + if (!(m.flags_ & P_UPTODATE)) /* nop */; else if (action == UP_OBJ_INSERT) { @@ -720,14 +645,14 @@ */ || ((m.move_block | op->move_block) & ~op->move_allow) != m.move_block || 1) // the above is not strong enough a test to skip updating. los maybe? TODO (Schmorp) - m.flags_ = P_NEED_UPDATE; + m.flags_ = 0; } /* if the object is being removed, we can't make intelligent * decisions, because remove_ob can't really pass the object * that is being removed. */ else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE) - m.flags_ = P_NEED_UPDATE; + m.flags_ = 0; else if (action == UP_OBJ_FACE) /* Nothing to do for that case */ ; else @@ -737,7 +662,6 @@ update_object (op->more, action); } -object::vector object::objects; // not yet used object *object::first; object::object () @@ -780,6 +704,82 @@ next = 0; } +bool +object::active () const +{ + return active_next || active_prev || this == active_objects; +} + +void +object::activate () +{ + /* If already on active list, don't do anything */ + if (active ()) + return; + + if (has_active_speed ()) + { + /* process_events() expects us to insert the object at the beginning + * of the list. */ + active_next = active_objects; + + if (active_next) + active_next->active_prev = this; + + active_objects = this; + } +} + +void +object::activate_recursive () +{ + activate (); + + for (object *op = inv; op; op = op->below) + op->activate_recursive (); +} + +/* This function removes object 'op' from the list of active + * objects. + * This should only be used for style maps or other such + * reference maps where you don't want an object that isn't + * in play chewing up cpu time getting processed. + * The reverse of this is to call update_ob_speed, which + * will do the right thing based on the speed of the object. + */ +void +object::deactivate () +{ + /* If not on the active list, nothing needs to be done */ + if (!active ()) + return; + + if (active_prev == 0) + { + active_objects = active_next; + if (active_next) + active_next->active_prev = 0; + } + else + { + active_prev->active_next = active_next; + if (active_next) + active_next->active_prev = active_prev; + } + + active_next = 0; + active_prev = 0; +} + +void +object::deactivate_recursive () +{ + for (object *op = inv; op; op = op->below) + op->deactivate_recursive (); + + deactivate (); +} + /* * Remove and free all objects in the inventory of the given object. * object.c ? @@ -787,14 +787,28 @@ void object::destroy_inv (bool drop_to_ground) { + // 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, + // cf will crash below with off-map x and y + if (!inv) + return; + /* Only if the space blocks everything do we not process - * if some form of movement is allowed, let objects * drop on that space. */ - if (!drop_to_ground || !map || map->in_memory != MAP_IN_MEMORY || GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL) + if (!drop_to_ground + || !map + || map->in_memory != MAP_IN_MEMORY + || ms ().move_block == MOVE_ALL) { while (inv) - inv->destroy (); + { + inv->destroy_inv (drop_to_ground); + inv->destroy (); + } } else { /* Put objects in inventory onto this space */ @@ -809,12 +823,7 @@ || op->flag [FLAG_IS_A_TEMPLATE]) op->destroy (); else - { - op->remove (); - op->x = x; - op->y = y; - insert_ob_in_map (op, map, 0, 0); /* Insert in same map as the envir */ - } + map->insert (op, x, y); } } } @@ -841,12 +850,13 @@ if (flag [FLAG_FREED]) return; + set_speed (0); + flag [FLAG_FREED] = 1; attachable::do_destroy (); destroy_inv (true); - set_speed (0); unlink (); // hack to ensure that freed objects still have a valid map @@ -861,7 +871,8 @@ freed_map->width = 3; freed_map->height = 3; - freed_map->allocate (); + freed_map->alloc (); + freed_map->in_memory = MAP_IN_MEMORY; } map = freed_map; @@ -956,12 +967,12 @@ if ((otmp = in_player ()) && otmp->contr && !QUERY_FLAG (otmp, FLAG_NO_FIX_PLAYER)) otmp->update_stats (); - if (above != NULL) + if (above) above->below = below; else env->inv = below; - if (below != NULL) + if (below) below->above = above; /* we set up values so that it could be inserted into @@ -975,9 +986,13 @@ } else if (map) { - /* Re did the following section of code - it looks like it had - * lots of logic for things we no longer care about - */ + if (type == PLAYER) + { + --map->players; + map->touch (); + } + + map->dirty = true; /* link the object above us */ if (above) @@ -1038,7 +1053,7 @@ tmp->contr->ns->floorbox_update (); } - /* See if player moving off should effect something */ + /* See if object moving off should effect something */ if (check_walk_off && ((move_type & tmp->move_off) && (move_type & ~tmp->move_off & ~tmp->move_block) == 0)) @@ -1050,16 +1065,17 @@ } /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */ - + //TODO: why is this horrible hacka fix? get rid of this code=bug! (schmorp) if (tmp->above == tmp) tmp->above = 0; last = tmp; } - /* last == NULL of there are no objects on this space */ + /* last == NULL if there are no objects on this space */ + //TODO: this makes little sense, why only update the topmost object? if (!last) - map->at (x, y).flags_ = P_NEED_UPDATE; + map->at (x, y).flags_ = 0; else update_object (last, UP_OBJ_REMOVE); @@ -1112,12 +1128,7 @@ object * insert_ob_in_map_at (object *op, maptile *m, object *originator, int flag, int x, int y) { - object *tmp; - - if (op->head) - op = op->head; - - for (tmp = op; tmp; tmp = tmp->more) + for (object *tmp = op->head_ (); tmp; tmp = tmp->more) { tmp->x = x + tmp->arch->clone.x; tmp->y = y + tmp->arch->clone.y; @@ -1158,7 +1169,7 @@ return NULL; } - if (m == NULL) + if (!m) { char *dump = dump_object (op); LOG (llevError, "Trying to insert in null-map!\n%s\n", dump); @@ -1195,10 +1206,10 @@ object *more = op->more; - /* We really need the caller to normalize coordinates - if + /* We really need the caller to normalise coordinates - if * we set the map, that doesn't work if the location is within * a map and this is straddling an edge. So only if coordinate - * is clear wrong do we normalize it. + * is clear wrong do we normalise it. */ if (OUT_OF_REAL_MAP (more->map, more->x, more->y)) more->map = get_map_from_coord (m, &more->x, &more->y); @@ -1232,7 +1243,7 @@ /* 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) + for (tmp = GET_MAP_OB (op->map, x, y); tmp; tmp = tmp->above) if (object::can_merge (op, tmp)) { op->nrof += tmp->nrof; @@ -1269,7 +1280,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 = 0; /* * If there are multiple objects on this space, we do some trickier handling. @@ -1283,8 +1294,7 @@ * when lots of spells are cast in one area. Currently, it is presumed * that flying non pickable objects are spell objects. */ - - while (top != NULL) + while (top) { if (QUERY_FLAG (top, FLAG_IS_FLOOR) || QUERY_FLAG (top, FLAG_OVERLAY_FLOOR)) floor = top; @@ -1347,7 +1357,7 @@ if (op->above) op->above->below = op; - op->below = NULL; + op->below = 0; op->ms ().bot = op; } else @@ -1361,12 +1371,18 @@ top->above = op; } - if (op->above == NULL) + if (!op->above) op->ms ().top = op; } /* else not INS_BELOW_ORIGINATOR */ if (op->type == PLAYER) - op->contr->do_los = 1; + { + op->contr->do_los = 1; + ++op->map->players; + op->map->touch (); + } + + op->map->dirty = true; /* If we have a floor, we know the player, if any, will be above * it, so save a few ticks and start from there. @@ -1382,7 +1398,7 @@ * on the map will get recalculated. The players could very well * be far away from this change and not affected in any way - * this should get redone to only look for players within range, - * or just updating the P_NEED_UPDATE for spaces within this area + * or just updating the P_UPTODATE for spaces within this area * of effect may be sufficient. */ if (op->map->darkness && (op->glow_radius != 0)) @@ -1430,7 +1446,7 @@ /* first search for itself and remove any old instances */ - for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp != NULL; tmp = tmp->above) + for (tmp = op->ms ().bot; tmp; tmp = tmp->above) if (!strcmp (tmp->arch->name, arch_string)) /* same archetype */ tmp->destroy (); @@ -1441,6 +1457,12 @@ insert_ob_in_map (tmp1, op->map, op, 0); } +object * +object::insert_at (object *where, object *originator, int flags) +{ + where->map->insert (this, where->x, where->y, originator, flags); +} + /* * get_split_ob(ob,nr) splits up ob into two parts. The part which * is returned contains nr objects, and the remaining parts contains @@ -1448,7 +1470,6 @@ * On failure, NULL is returned, and the reason put into the * global static errmsg array. */ - object * get_split_ob (object *orig_ob, uint32 nr) { @@ -1722,7 +1743,6 @@ * object being inserted. insert_ob_in_map may not put new objects * on top. */ - int check_move_on (object *op, object *originator) { @@ -1759,7 +1779,7 @@ * Hence, we first go to the top: */ - for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp && tmp->above; tmp = tmp->above) + for (tmp = op->ms ().bot; tmp && tmp->above; tmp = tmp->above) { /* Trim the search when we find the first other spell effect * this helps performance so that if a space has 50 spell objects, @@ -1827,13 +1847,13 @@ object * present_arch (const archetype *at, maptile *m, int x, int y) { - if (m == NULL || out_of_map (m, x, y)) + if (!m || out_of_map (m, x, y)) { LOG (llevError, "Present_arch called outside map.\n"); return NULL; } - for (object *tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) + for (object *tmp = m->at (x, y).bot; tmp; tmp = tmp->above) if (tmp->arch == at) return tmp; @@ -1854,7 +1874,7 @@ return NULL; } - for (object *tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) + for (object *tmp = m->at (x, y).bot; tmp; tmp = tmp->above) if (tmp->type == type) return tmp; @@ -2589,24 +2609,55 @@ item = item->env; } -// return a suitable string describing an objetc in enough detail to find it + +const char * +object::flag_desc (char *desc, int len) const +{ + char *p = desc; + bool first = true; + + *p = 0; + + for (int i = 0; i < NUM_FLAGS; i++) + { + if (len <= 10) // magic constant! + { + snprintf (p, len, ",..."); + break; + } + + if (flag [i]) + { + int cnt = snprintf (p, len, "%s%d", first ? "" : ",", i); + len -= cnt; + p += cnt; + first = false; + } + } + + return desc; +} + +// return a suitable string describing an object in enough detail to find it const char * object::debug_desc (char *info) const { - char info2[256 * 3]; + char flagdesc[512]; + char info2[256 * 4]; char *p = info; - p += snprintf (p, 256, "%d=\"%s%s%s\"", - count, + p += snprintf (p, 512, "{cnt:%d,uuid:<1,%" PRIx64 ">,name:\"%s%s%s\",flags:[%s],type:%d}", + count, uuid.seq, &name, - title ? " " : "", - title ? (const char *)title : ""); + title ? "\",title:" : "", + title ? (const char *)title : "", + flag_desc (flagdesc, 512), type); 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); + p += snprintf (p, 256, "(on %s@%d+%d)", &map->path, x, y); return info; } @@ -2614,7 +2665,7 @@ const char * object::debug_desc () const { - static char info[256 * 3]; + static char info[256 * 4]; return debug_desc (info); }