--- deliantra/server/common/object.C 2006/12/30 21:07:46 1.97 +++ deliantra/server/common/object.C 2007/05/12 16:06:42 1.147 @@ -1,25 +1,26 @@ /* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2001 Mark Wedel & Crossfire Development Team - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The authors can be reached via e-mail at -*/ + * CrossFire, A Multiplayer game + * + * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team + * Copyright (C) 2001 Mark Wedel & Crossfire Development Team + * Copyright (C) 1992 Frank Tore Johansen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * The authors can be reached via e-mail at + */ /* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects. sub/add_weight will transcend the environment updating the carrying @@ -30,6 +31,7 @@ #include #include #include +#include #include #include @@ -38,7 +40,8 @@ static UUID uuid; const uint64 UUID_SKIP = 1<<19; -object *active_objects; /* List of active objects that need to be processed */ +objectvec objects; +activevec actives; short freearr_x[SIZEOFFREE] = { 0, 0, 1, 1, 1, 0, -1, -1, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -3, -3, -3, -3, -2, -1 @@ -324,10 +327,11 @@ long sum; object *inv; - for (sum = 0, inv = op->inv; inv != NULL; inv = inv->below) + for (sum = 0, inv = op->inv; inv; inv = inv->below) { if (inv->inv) sum_weight (inv); + sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); } @@ -357,7 +361,6 @@ * Some error messages. * The result of the dump is stored in the static global errmsg array. */ - char * dump_object (object *op) { @@ -365,7 +368,7 @@ return strdup ("[NULLOBJ]"); object_freezer freezer; - save_object (freezer, op, 1); + op->write (freezer); return freezer.as_string (); } @@ -392,13 +395,12 @@ /* * Returns the object which has the count-variable equal to the argument. */ - object * find_object (tag_t i) { - for (object *op = object::first; op; op = op->next) - if (op->count == i) - return op; + for_all_objects (op) + if (op->count == i) + return op; return 0; } @@ -408,14 +410,13 @@ * Used only by the patch command, but not all that useful. * Enables features like "patch food 999" */ - object * find_object_name (const char *str) { shstr_cmp str_ (str); object *op; - for (op = object::first; op != NULL; op = op->next) + for_all_objects (op) if (op->name == str_) break; @@ -451,13 +452,54 @@ this->owner = owner; } +bool +object::change_weapon (object *ob) +{ + if (current_weapon == ob) + return true; + + if (chosen_skill) + chosen_skill->flag [FLAG_APPLIED] = false; + + current_weapon = ob; + chosen_skill = !ob || ob->type == SKILL ? ob : find_skill_by_name (this, ob->skill); + + if (chosen_skill) + chosen_skill->flag [FLAG_APPLIED] = true; + + update_stats (); + + if (ob) + { + // now check wether any body locations became invalid, in which case + // we cannot apply the weapon at the moment. + for (int i = 0; i < NUM_BODY_LOCATIONS; ++i) + if (slot[i].used < 0) + { + current_weapon = chosen_skill = 0; + update_stats (); + + new_draw_info_format (NDI_UNIQUE, 0, this, + "You try to balance your applied items all at once, but the %s is too much. " + "You need to unapply some items first.", &ob->name); + return false; + } + + new_draw_info_format (NDI_UNIQUE, 0, this, "You switch to your %s.", &ob->name); + } + else + new_draw_info_format (NDI_UNIQUE, 0, this, "You unwield your weapons."); + + return true; +} + /* Zero the key_values on op, decrementing the shared-string * refcounts and freeing the links. */ static void free_key_values (object *op) { - for (key_value *i = op->key_values; i != 0;) + for (key_value *i = op->key_values; i; ) { key_value *next = i->next; delete i; @@ -468,40 +510,24 @@ op->key_values = 0; } -/* - * copy_to first frees everything allocated by the dst object, - * and then copies the contents of itself into the second - * object, allocating what needs to be allocated. Basically, any - * data that is malloc'd needs to be re-malloc/copied. Otherwise, - * if the first object is freed, the pointers in the new object - * will point at garbage. - */ -void -object::copy_to (object *dst) +object & +object::operator =(const object &src) { - bool is_freed = QUERY_FLAG (dst, FLAG_FREED); - bool is_removed = QUERY_FLAG (dst, FLAG_REMOVED); + bool is_freed = flag [FLAG_FREED]; + bool is_removed = flag [FLAG_REMOVED]; - *(object_copy *)dst = *this; + *(object_copy *)this = src; - if (is_freed) - SET_FLAG (dst, FLAG_FREED); - - if (is_removed) - SET_FLAG (dst, FLAG_REMOVED); - - if (speed < 0) - dst->speed_left = speed_left - RANDOM () % 200 / 100.0; + flag [FLAG_FREED] = is_freed; + flag [FLAG_REMOVED] = is_removed; /* Copy over key_values, if any. */ - if (key_values) + if (src.key_values) { key_value *tail = 0; - key_value *i; - - dst->key_values = 0; + key_values = 0; - for (i = key_values; i; i = i->next) + for (key_value *i = src.key_values; i; i = i->next) { key_value *new_link = new key_value; @@ -510,9 +536,9 @@ new_link->value = i->value; /* Try and be clever here, too. */ - if (!dst->key_values) + if (!key_values) { - dst->key_values = new_link; + key_values = new_link; tail = new_link; } else @@ -522,10 +548,46 @@ } } } +} + +/* + * copy_to first frees everything allocated by the dst object, + * and then copies the contents of itself into the second + * object, allocating what needs to be allocated. Basically, any + * data that is malloc'd needs to be re-malloc/copied. Otherwise, + * if the first object is freed, the pointers in the new object + * will point at garbage. + */ +void +object::copy_to (object *dst) +{ + *dst = *this; + + if (speed < 0) + dst->speed_left = speed_left - rndm (); dst->set_speed (dst->speed); } +void +object::instantiate () +{ + if (!uuid.seq) // HACK + uuid = gen_uuid (); + + speed_left = -0.1f; + /* copy the body_info to the body_used - this is only really + * need for monsters, but doesn't hurt to do it for everything. + * by doing so, when a monster is created, it has good starting + * values for the body_used info, so when items are created + * for it, they can be properly equipped. + */ + for (int i = NUM_BODY_LOCATIONS; i--; ) + slot[i].used = slot[i].info; + + attachable::instantiate (); +} + object * object::clone () { @@ -566,9 +628,9 @@ this->speed = speed; if (has_active_speed ()) - activate (false); + activate (); else - deactivate (false); + deactivate (); } /* @@ -591,8 +653,6 @@ void update_object (object *op, int action) { - MoveType move_on, move_off, move_block, move_slow; - if (op == NULL) { /* this should never happen */ @@ -626,7 +686,7 @@ mapspace &m = op->ms (); - if (m.flags_ & P_NEED_UPDATE) + if (!(m.flags_ & P_UPTODATE)) /* nop */; else if (action == UP_OBJ_INSERT) { @@ -645,14 +705,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 @@ -662,9 +722,6 @@ update_object (op->more, action); } -object::vector object::objects; // not yet used -object *object::first; - object::object () { SET_FLAG (this, FLAG_REMOVED); @@ -675,58 +732,50 @@ object::~object () { + unlink (); + free_key_values (this); } +static int object_count; + void object::link () { - count = ++ob_count; + assert (!index);//D uuid = gen_uuid (); + count = ++object_count; - prev = 0; - next = object::first; - - if (object::first) - object::first->prev = this; - - object::first = this; + refcnt_inc (); + objects.insert (this); } void object::unlink () { - if (this == object::first) - object::first = next; - - /* Remove this object from the list of used objects */ - if (prev) prev->next = next; - if (next) next->prev = prev; + if (!index) + return; - prev = 0; - next = 0; + objects.erase (this); + refcnt_dec (); } void -object::activate (bool recursive) +object::activate () { - if (has_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 already on active list, don't do anything */ + if (active) + return; - if (active_next) - active_next->active_prev = this; + if (has_active_speed ()) + actives.insert (this); +} - active_objects = this; - } +void +object::activate_recursive () +{ + activate (); - if (recursive) - for (object *op = inv; op; op = op->above) - op->activate (1); + for (object *op = inv; op; op = op->below) + op->activate_recursive (); } /* This function removes object 'op' from the list of active @@ -738,31 +787,32 @@ * will do the right thing based on the speed of the object. */ void -object::deactivate (bool recursive) +object::deactivate () { /* If not on the active list, nothing needs to be done */ - if (!active_next && !active_prev && this != active_objects) + 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; - } + actives.erase (this); +} + +void +object::deactivate_recursive () +{ + for (object *op = inv; op; op = op->below) + op->deactivate_recursive (); - active_next = 0; - active_prev = 0; + deactivate (); +} - if (recursive) - for (object *op = inv; op; op = op->above) - op->deactivate (1); +void +object::set_flag_inv (int flag, int value) +{ + for (object *op = inv; op; op = op->below) + { + op->flag [flag] = value; + op->set_flag_inv (flag, value); + } } /* @@ -787,6 +837,7 @@ if (!drop_to_ground || !map || map->in_memory != MAP_IN_MEMORY + || map->nodrop || ms ().move_block == MOVE_ALL) { while (inv) @@ -805,7 +856,8 @@ || op->flag [FLAG_NO_DROP] || op->type == RUNE || op->type == TRAP - || op->flag [FLAG_IS_A_TEMPLATE]) + || op->flag [FLAG_IS_A_TEMPLATE] + || op->flag [FLAG_DESTROY_ON_DEATH]) op->destroy (); else map->insert (op, x, y); @@ -823,6 +875,8 @@ void object::do_destroy () { + attachable::do_destroy (); + if (flag [FLAG_IS_LINKED]) remove_button_link (this); @@ -832,18 +886,13 @@ if (!flag [FLAG_REMOVED]) remove (); - if (flag [FLAG_FREED]) - return; + destroy_inv (true); - set_speed (0); + deactivate (); + unlink (); flag [FLAG_FREED] = 1; - attachable::do_destroy (); - - destroy_inv (true); - unlink (); - // hack to ensure that freed objects still have a valid map { static maptile *freed_map; // freed objects are moved here to avoid crashes @@ -857,6 +906,7 @@ freed_map->height = 3; freed_map->alloc (); + freed_map->in_memory = MAP_IN_MEMORY; } map = freed_map; @@ -876,9 +926,6 @@ owner = 0; enemy = 0; attacked_by = 0; - - // only relevant for players(?), but make sure of it anyways - contr = 0; } void @@ -916,10 +963,9 @@ * object will have no environment. If the object previously had an * environment, the x and y coordinates will be updated to * the previous environment. - * Beware: This function is called from the editor as well! */ void -object::remove () +object::do_remove () { object *tmp, *last = 0; object *otmp; @@ -972,16 +1018,24 @@ { if (type == 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 (); + --map->players; - map->last_access = runtime; + map->touch (); } + map->dirty = true; + mapspace &ms = this->ms (); /* link the object above us */ if (above) above->below = below; else - map->at (x, y).top = below; /* we were top, set new top */ + ms.top = below; /* we were top, set new top */ /* Relink the object below us, if there is one */ if (below) @@ -993,17 +1047,9 @@ * evident */ if (GET_MAP_OB (map, x, y) != this) - { - char *dump = dump_object (this); - LOG (llevError, - "remove_ob: GET_MAP_OB does not return object to be removed even though it appears to be on the bottom?\n%s\n", dump); - free (dump); - dump = dump_object (GET_MAP_OB (map, x, y)); - LOG (llevError, "%s\n", dump); - free (dump); - } + 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 ()); - map->at (x, y).bot = above; /* goes on above it. */ + ms.bot = above; /* goes on above it. */ } above = 0; @@ -1014,7 +1060,7 @@ int check_walk_off = !flag [FLAG_NO_APPLY]; - for (tmp = map->at (x, y).bot; tmp; tmp = tmp->above) + for (tmp = ms.bot; tmp; tmp = tmp->above) { /* No point updating the players look faces if he is the object * being removed. @@ -1058,7 +1104,7 @@ /* 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); @@ -1104,9 +1150,32 @@ return 0; } +void +object::expand_tail () +{ + if (more) + return; + + object *prev = this; + + for (archetype *at = arch->more; at; at = at->more) + { + object *op = arch_to_object (at); + + op->name = name; + op->name_pl = name_pl; + op->title = title; + + op->head = this; + prev->more = op; + + prev = op; + } +} + /* - * same as insert_ob_in_map except it handle separate coordinates and do a clean - * job preparing multi-part monsters + * same as insert_ob_in_map except it handles separate coordinates and does a clean + * job preparing multi-part monsters. */ object * insert_ob_in_map_at (object *op, maptile *m, object *originator, int flag, int x, int y) @@ -1143,27 +1212,23 @@ object * insert_ob_in_map (object *op, maptile *m, object *originator, int flag) { + assert (!op->flag [FLAG_FREED]); + object *tmp, *top, *floor = NULL; - sint16 x, y; - if (QUERY_FLAG (op, FLAG_FREED)) - { - LOG (llevError, "Trying to insert freed object!\n"); - return NULL; - } + op->remove (); - if (!m) - { - char *dump = dump_object (op); - LOG (llevError, "Trying to insert in null-map!\n%s\n", dump); - free (dump); - return op; - } +#if 0 + if (!m->active != !op->active) + if (m->active) + op->activate_recursive (); + else + op->deactivate_recursive (); +#endif if (out_of_map (m, op->x, op->y)) { - char *dump = dump_object (op); - LOG (llevError, "Trying to insert object outside the map.\n%s\n", dump); + LOG (llevError, "Trying to insert object outside the map.\n%s\n", op->debug_desc ()); #ifdef MANY_CORES /* Better to catch this here, as otherwise the next use of this object * is likely to cause a crash. Better to find out where it is getting @@ -1171,40 +1236,12 @@ */ abort (); #endif - free (dump); return op; } - if (!QUERY_FLAG (op, FLAG_REMOVED)) + if (object *more = op->more) { - char *dump = dump_object (op); - LOG (llevError, "Trying to insert (map) inserted object.\n%s\n", dump); - free (dump); - return op; - } - - if (op->more) - { - /* The part may be on a different map. */ - - object *more = op->more; - - /* We really need the caller to normalize 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. - */ - if (OUT_OF_REAL_MAP (more->map, more->x, more->y)) - more->map = get_map_from_coord (m, &more->x, &more->y); - else if (!more->map) - { - /* For backwards compatibility - when not dealing with tiled maps, - * more->map should always point to the parent. - */ - more->map = m; - } - - if (insert_ob_in_map (more, more->map, originator, flag) == NULL) + if (!insert_ob_in_map (more, m, originator, flag)) { if (!op->head) LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n"); @@ -1219,14 +1256,16 @@ * of areas of callers (eg, anything that uses find_free_spot would now * need extra work */ - op->map = get_map_from_coord (m, &op->x, &op->y); - x = op->x; - y = op->y; + if (!xy_normalise (m, op->x, op->y)) + return 0; + + op->map = m; + mapspace &ms = op->ms (); /* 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; tmp = tmp->above) + for (tmp = ms.bot; tmp; tmp = tmp->above) if (object::can_merge (op, tmp)) { op->nrof += tmp->nrof; @@ -1253,15 +1292,17 @@ if (op->below) op->below->above = op; else - op->ms ().bot = op; + ms.bot = op; /* since *below* originator, no need to update top */ originator->below = op; } else { + top = ms.bot; + /* If there are other objects, then */ - if ((!(flag & INS_MAP_LOAD)) && ((top = GET_MAP_OB (op->map, op->x, op->y)) != NULL)) + if ((!(flag & INS_MAP_LOAD)) && top) { object *last = 0; @@ -1277,7 +1318,7 @@ * when lots of spells are cast in one area. Currently, it is presumed * that flying non pickable objects are spell objects. */ - while (top) + for (top = ms.bot; top; top = top->above) { if (QUERY_FLAG (top, FLAG_IS_FLOOR) || QUERY_FLAG (top, FLAG_OVERLAY_FLOOR)) floor = top; @@ -1290,7 +1331,6 @@ } last = top; - top = top->above; } /* Don't want top to be NULL, so set it to the last valid object */ @@ -1302,17 +1342,19 @@ */ /* Have object 'fall below' other objects that block view. - * Unless those objects are exits, type 66 + * Unless those objects are exits. * If INS_ON_TOP is used, don't do this processing * Need to find the object that in fact blocks view, otherwise * stacking is a bit odd. */ - if (!(flag & INS_ON_TOP) && - (get_map_flags (op->map, 0, op->x, op->y, 0, 0) & P_BLOCKSVIEW) && (op->face && !op->face->visibility)) + if (!(flag & INS_ON_TOP) + && ms.flags () & P_BLOCKSVIEW + && (op->face && !faces [op->face].visibility)) { for (last = top; last != floor; last = last->below) if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT)) break; + /* Check to see if we found the object that blocks view, * and make sure we have a below pointer for it so that * we can get inserted below this one, which requires we @@ -1324,7 +1366,7 @@ } /* If objects on this space */ if (flag & INS_MAP_LOAD) - top = GET_MAP_TOP (op->map, op->x, op->y); + top = ms.top; if (flag & INS_ABOVE_FLOOR_ONLY) top = floor; @@ -1335,13 +1377,13 @@ /* First object on this space */ if (!top) { - op->above = GET_MAP_OB (op->map, op->x, op->y); + op->above = ms.bot; if (op->above) op->above->below = op; op->below = 0; - op->ms ().bot = op; + ms.bot = op; } else { /* get inserted into the stack above top */ @@ -1355,21 +1397,23 @@ } if (!op->above) - op->ms ().top = op; + ms.top = op; } /* else not INS_BELOW_ORIGINATOR */ if (op->type == PLAYER) { op->contr->do_los = 1; ++op->map->players; - op->map->last_access = runtime; + 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. */ if (!(flag & INS_MAP_LOAD)) - if (object *pl = op->ms ().player ()) + if (object *pl = ms.player ()) if (pl->contr->ns) pl->contr->ns->floorbox_update (); @@ -1379,7 +1423,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)) @@ -1427,7 +1471,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,7 +1485,7 @@ object * object::insert_at (object *where, object *originator, int flags) { - where->map->insert (this, where->x, where->y, originator, flags); + return where->map->insert (this, where->x, where->y, originator, flags); } /* @@ -1491,7 +1535,6 @@ * * Return value: 'op' if something is left, NULL if the amount reached 0 */ - object * decrease_ob_nr (object *op, uint32 i) { @@ -1576,7 +1619,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) { @@ -1618,7 +1660,6 @@ * The function returns now pointer to inserted item, and return value can * be != op, if items are merged. -Tero */ - object * object::insert (object *op) { @@ -1760,7 +1801,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, @@ -1788,7 +1829,7 @@ { float - diff = tmp->move_slow_penalty * FABS (op->speed); + diff = tmp->move_slow_penalty * fabs (op->speed); if (op->type == PLAYER) if ((QUERY_FLAG (tmp, FLAG_IS_HILLY) && find_skill_by_number (op, SK_CLIMBING)) || @@ -1828,13 +1869,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; @@ -1855,7 +1896,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; @@ -2005,7 +2046,7 @@ if (!index) return -1; - return altern[RANDOM () % index]; + return altern [rndm (index)]; } /* @@ -2036,7 +2077,7 @@ end -= begin; while (--end) - swap (arr [end], arr [RANDOM () % (end + 1)]); + swap (arr [end], arr [rndm (end + 1)]); } /* new function to make monster searching more efficient, and effective! @@ -2180,28 +2221,9 @@ } /* - * absdir(int): Returns a number between 1 and 8, which represent - * the "absolute" direction of a number (it actually takes care of - * "overflow" in previous calculations of a direction). - */ - -int -absdir (int d) -{ - while (d < 1) - d += 8; - - while (d > 8) - d -= 8; - - return d; -} - -/* * dirdiff(dir1, dir2) returns how many 45-degrees differences there is * between two directions (which are expected to be absolute (see absdir()) */ - int dirdiff (int dir1, int dir2) { @@ -2377,44 +2399,6 @@ return dst; } -/* GROS - Creates an object using a string representing its content. */ -/* Basically, we save the content of the string to a temp file, then call */ -/* load_object on it. I admit it is a highly inefficient way to make things, */ -/* but it was simple to make and allows reusing the load_object function. */ -/* Remember not to use load_object_str in a time-critical situation. */ -/* Also remember that multiparts objects are not supported for now. */ -object * -load_object_str (const char *obstr) -{ - object *op; - char filename[MAX_BUF]; - - sprintf (filename, "%s/cfloadobstr2044", settings.tmpdir); - - FILE *tempfile = fopen (filename, "w"); - - if (tempfile == NULL) - { - LOG (llevError, "Error - Unable to access load object temp file\n"); - return NULL; - } - - fprintf (tempfile, obstr); - fclose (tempfile); - - op = object::create (); - - object_thawer thawer (filename); - - if (thawer) - load_object (thawer, op, 0); - - LOG (llevDebug, " load str completed, object=%s\n", &op->name); - CLEAR_FLAG (op, FLAG_REMOVED); - - return op; -} - /* This returns the first object in who's inventory that * has the same type and subtype match. * returns NULL if no match. @@ -2478,7 +2462,6 @@ return 0; } - /* * Updates the canonical_key in op to value. * @@ -2597,6 +2580,8 @@ char *p = desc; bool first = true; + *p = 0; + for (int i = 0; i < NUM_FLAGS; i++) { if (len <= 10) // magic constant! @@ -2605,7 +2590,7 @@ break; } - if (flag[i]) + if (flag [i]) { int cnt = snprintf (p, len, "%s%d", first ? "" : ",", i); len -= cnt; @@ -2617,7 +2602,7 @@ return desc; } -// return a suitable string describing an objetc in enough detail to find it +// return a suitable string describing an object in enough detail to find it const char * object::debug_desc (char *info) const { @@ -2625,10 +2610,10 @@ char info2[256 * 4]; char *p = info; - p += snprintf (p, 512, "{cnt:%d,uuid:<1,%" PRIx64 ">,name:\"%s%s%s\",flags:[%s],type:%d}", + p += snprintf (p, 512, "{cnt:%d,uuid:<1.%" PRIx64 ">,name:\"%s\"%s%s,flags:[%s],type:%d}", count, uuid.seq, &name, - title ? "\",title:" : "", + title ? "\",title:\"" : "", title ? (const char *)title : "", flag_desc (flagdesc, 512), type); @@ -2644,7 +2629,80 @@ const char * object::debug_desc () const { - static char info[256 * 3]; - return debug_desc (info); + static char info[3][256 * 4]; + static int info_idx; + + return debug_desc (info [++info_idx % 3]); +} + +struct region * +object::region () const +{ + return map ? map->region (x, y) + : region::default_region (); +} + +const materialtype_t * +object::dominant_material () const +{ + if (materialtype_t *mat = name_to_material (materialname)) + return mat; + + // omfg this is slow, this has to be temporary :) + shstr unknown ("unknown"); + + return name_to_material (unknown); +} + +void +object::open_container (object *new_container) +{ + if (container == new_container) + return; + + if (object *old_container = container) + { + if (INVOKE_OBJECT (CLOSE, old_container, ARG_OBJECT (this))) + return; + +#if 0 + // remove the "Close old_container" object. + if (object *closer = old_container->inv) + if (closer->type == CLOSE_CON) + closer->destroy (); +#endif + + old_container->flag [FLAG_APPLIED] = 0; + container = 0; + + esrv_update_item (UPD_FLAGS, this, old_container); + new_draw_info_format (NDI_UNIQUE, 0, this, "You close %s.", query_name (old_container)); + } + + if (new_container) + { + if (INVOKE_OBJECT (OPEN, new_container, ARG_OBJECT (this))) + return; + + // TODO: this does not seem to serve any purpose anymore? +#if 0 + // insert the "Close Container" object. + if (archetype *closer = new_container->other_arch) + { + object *closer = arch_to_object (new_container->other_arch); + closer->flag [FLAG_NO_MAP_SAVE] = 1; + new_container->insert (closer); + } +#endif + + new_draw_info_format (NDI_UNIQUE, 0, this, "You open %s.", query_name (new_container)); + + new_container->flag [FLAG_APPLIED] = 1; + container = new_container; + + esrv_update_item (UPD_FLAGS, this, new_container); + esrv_send_inventory (this, new_container); + } } +