--- deliantra/server/common/object.C 2010/03/26 21:43:43 1.315 +++ deliantra/server/common/object.C 2010/04/18 14:04:50 1.331 @@ -39,6 +39,12 @@ objectvec objects; activevec actives; +freelist_item *object::freelist; +uint32_t object::object_count; +uint32_t object::free_count; +uint32_t object::create_count; +uint32_t object::destroy_count; + //+GPL short freearr_x[SIZEOFFREE] = { @@ -268,11 +274,11 @@ * are not equal - just if it has been identified, the been_applied * flags lose any meaning. */ - if (QUERY_FLAG (ob1, FLAG_IDENTIFIED)) - SET_FLAG (ob1, FLAG_BEEN_APPLIED); + if (ob1->flag [FLAG_IDENTIFIED]) + ob1->set_flag (FLAG_BEEN_APPLIED); - if (QUERY_FLAG (ob2, FLAG_IDENTIFIED)) - SET_FLAG (ob2, FLAG_BEEN_APPLIED); + if (ob2->flag [FLAG_IDENTIFIED]) + ob2->set_flag (FLAG_BEEN_APPLIED); if (ob1->arch->archname != ob2->arch->archname || ob1->name != ob2->name @@ -331,14 +337,14 @@ * it is possible for most any character to have more than one of * some items equipped, and we don't want those to merge. */ - if (QUERY_FLAG (ob1, FLAG_APPLIED) || QUERY_FLAG (ob2, FLAG_APPLIED)) + if (ob1->flag [FLAG_APPLIED] || ob2->flag [FLAG_APPLIED]) return 0; /* Note sure why the following is the case - either the object has to * be animated or have a very low speed. Is this an attempted monster * check? */ - if (!QUERY_FLAG (ob1, FLAG_ANIMATE) && ob1->has_active_speed ()) + if (!ob1->flag [FLAG_ANIMATE] && ob1->has_active_speed ()) return 0; switch (ob1->type) @@ -479,6 +485,10 @@ if (sum != carrying) { + if (carrying != sum)//D + LOG (llevDebug, "updating weight got %ld, expected %ld (%s)\n", + (long long)sum, (long long)carrying, debug_desc ()); + carrying = sum; if (object *pl = visible_to ()) @@ -576,73 +586,6 @@ this->owner = owner; } -int -object::slottype () const -{ - if (type == SKILL) - { - if (IS_COMBAT_SKILL (subtype)) return slot_combat; - if (IS_RANGED_SKILL (subtype)) return slot_ranged; - } - else - { - if (slot [body_combat].info) return slot_combat; - if (slot [body_range ].info) return slot_ranged; - } - - return slot_none; -} - -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 all your items at once, " - "but the %s is just too much for your body. " - "[You need to unapply some items first - use the 'body' command to see " - "how many items you can wear on a specific body part.]", &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."); - - if (ob && !ob->flag [FLAG_APPLIED] && ob->type != SPELL) - { - LOG (llevError | logBacktrace, "%s changed to unapplied weapon %s", - &name, ob->debug_desc ()); - return false; - } - - return true; -} - /* Zero the key_values on op, decrementing the shared-string * refcounts and freeing the links. */ @@ -752,7 +695,7 @@ void update_turn_face (object *op) { - if (!QUERY_FLAG (op, FLAG_IS_TURNABLE) || !op->arch) + if (!op->flag [FLAG_IS_TURNABLE] || !op->arch) return; SET_ANIMATION (op, op->direction); @@ -828,12 +771,12 @@ { #if 0 // this is likely overkill, TODO: revisit (schmorp) - if ((QUERY_FLAG (op, FLAG_BLOCKSVIEW) && !(m.flags_ & P_BLOCKSVIEW)) - || (QUERY_FLAG (op, FLAG_NO_MAGIC) && !(m.flags_ & P_NO_MAGIC)) + if ((op->flag [FLAG_BLOCKSVIEW] && !(m.flags_ & P_BLOCKSVIEW)) + || (op->flag [FLAG_NO_MAGIC] && !(m.flags_ & P_NO_MAGIC)) || (op->is_player () && !(m.flags_ & P_PLAYER)) || (op->type == SAFE_GROUND && !(m.flags_ & P_SAFE)) - || (QUERY_FLAG (op, FLAG_ALIVE) && !(m.flags_ & P_IS_ALIVE)) - || (QUERY_FLAG (op, FLAG_DAMNED) && !(m.flags_ & P_NO_CLERIC)) + || (op->flag [FLAG_ALIVE] && !(m.flags_ & P_IS_ALIVE)) + || (op->flag [FLAG_DAMNED] && !(m.flags_ & P_NO_CLERIC)) || (m.move_on | op->move_on ) != m.move_on || (m.move_off | op->move_off ) != m.move_off || (m.move_slow | op->move_slow) != m.move_slow @@ -864,7 +807,7 @@ object::object () { - SET_FLAG (this, FLAG_REMOVED); + this->set_flag (FLAG_REMOVED); //expmul = 1.0; declared const for the time being face = blank_face; @@ -878,16 +821,16 @@ free_key_values (this); } -static int object_count; - void object::link () { assert (!index);//D uuid = UUID::gen (); - count = ++object_count; refcnt_inc (); objects.insert (this); + + ++create_count; + } void object::unlink () @@ -895,6 +838,8 @@ if (!index) return; + ++destroy_count; + objects.erase (this); refcnt_dec (); } @@ -1008,25 +953,97 @@ } } -object *object::create () +/* + * Remove and free all objects in the inventory of the given object. + * Unlike destroy_inv, this assumes the *this is destroyed as well + * well, so we can (and have to!) take shortcuts. + */ +void +object::destroy_inv_fast () +{ + while (object *op = inv) + { + // remove from object the fast way + op->flag [FLAG_REMOVED] = true; + op->env = 0; + if ((inv = inv->below)) + inv->above = 0; + + // then destroy + op->destroy (); + } +} + +void +object::freelist_free (int count) +{ + while (count-- && freelist) + { + freelist_item *next = freelist->next; + // count is being "destroyed" + + sfree ((char *)freelist, sizeof (object)); + + freelist = next; + --free_count; + } +} + +object * +object::create () { - object *op = new object; + object *op; + + if (freelist) + { + freelist_item li = *freelist; + memset (freelist, 0, sizeof (object)); + + op = new (freelist) object; + op->count = li.count; + + freelist = li.next; + --free_count; + } + else + { + void *ni = salloc0 (sizeof (object)); + + op = new(ni) object; + + op->count = ++object_count; + } + op->link (); + return op; } +void +object::do_delete () +{ + uint32_t count = this->count; + + this->~object (); + + freelist_item *li = (freelist_item *)this; + li->next = freelist; + li->count = count; + + freelist = li; + ++free_count; +} + 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; } @@ -1087,7 +1104,7 @@ return; } - destroy_inv (false); + destroy_inv_fast (); if (is_head ()) if (sound_destroy) @@ -1151,9 +1168,23 @@ if (pl && pl->is_player ()) { + if (expect_false (pl->contr->combat_ob == this)) + { + pl->apply (pl->contr->combat_ob, AP_UNAPPLY); + pl->contr->combat_ob = 0; + if (pl->contr->ranged_ob) pl->apply (pl->contr->ranged_ob); + } + + if (expect_false (pl->contr->ranged_ob == this)) + { + pl->apply (pl->contr->ranged_ob, AP_UNAPPLY); + pl->contr->ranged_ob = 0; + if (pl->contr->combat_ob) pl->apply (pl->contr->combat_ob); + } + pl->contr->queue_stats_update (); - if (glow_radius && pl->is_on_map ()) + if (expect_false (glow_radius) && pl->is_on_map ()) update_all_los (pl->map, pl->x, pl->y); } } @@ -1183,8 +1214,8 @@ // removing a container should close it close_container (); } - - esrv_del_item (pl->contr, count); + else + esrv_del_item (pl->contr, count); } /* link the object above us */ @@ -1380,11 +1411,11 @@ tmp->destroy (); } - CLEAR_FLAG (op, FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */ - CLEAR_FLAG (op, FLAG_INV_LOCKED); + op->clr_flag (FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */ + op->clr_flag (FLAG_INV_LOCKED); - if (!QUERY_FLAG (op, FLAG_ALIVE)) - CLEAR_FLAG (op, FLAG_NO_STEAL); + if (!op->flag [FLAG_ALIVE]) + op->clr_flag (FLAG_NO_STEAL); if (flag & INS_BELOW_ORIGINATOR) { @@ -1429,10 +1460,10 @@ */ for (object *tmp = ms.bot; tmp; tmp = tmp->above) { - if (QUERY_FLAG (tmp, FLAG_IS_FLOOR) || QUERY_FLAG (tmp, FLAG_OVERLAY_FLOOR)) + if (tmp->flag [FLAG_IS_FLOOR] || tmp->flag [FLAG_OVERLAY_FLOOR]) floor = tmp; - if (QUERY_FLAG (tmp, FLAG_NO_PICK) && (tmp->move_type & (MOVE_FLY_LOW | MOVE_FLY_HIGH)) && !QUERY_FLAG (tmp, FLAG_IS_FLOOR)) + if (tmp->flag [FLAG_NO_PICK] && (tmp->move_type & (MOVE_FLY_LOW | MOVE_FLY_HIGH)) && !tmp->flag [FLAG_IS_FLOOR]) { /* We insert above top, so we want this object below this */ top = tmp->below; @@ -1460,7 +1491,7 @@ object *last; for (last = top; last != floor; last = last->below) - if (QUERY_FLAG (last, FLAG_BLOCKSVIEW) && (last->type != EXIT)) + if (last->flag [FLAG_BLOCKSVIEW] && (last->type != EXIT)) break; /* Check to see if we found the object that blocks view, @@ -1601,10 +1632,10 @@ return true; if (originator && originator->is_player ()) - originator->contr->failmsg (format ( + originator->contr->failmsgf ( "No matter how hard you try, you just cannot put the %s here H", query_name () - )); + ); return false; } @@ -1788,7 +1819,7 @@ int check_move_on (object *op, object *originator) { - if (QUERY_FLAG (op, FLAG_NO_APPLY)) + if (op->flag [FLAG_NO_APPLY]) return 0; object *tmp; @@ -1835,7 +1866,7 @@ * space doesn't slow down swimming (for example), if you can't actually * swim on that space, can't use it to avoid the penalty. */ - if (!QUERY_FLAG (op, FLAG_WIZPASS)) + if (!op->flag [FLAG_WIZPASS]) { if ((!op->move_type && tmp->move_slow & MOVE_WALK) || ((op->move_type & tmp->move_slow) && (op->move_type & ~tmp->move_slow & ~tmp->move_block) == 0)) @@ -1976,7 +2007,7 @@ { for (object *tmp = op->inv; tmp; tmp = tmp->below) { - SET_FLAG (tmp, flag); + tmp->set_flag (flag); flag_inv (tmp, flag); } } @@ -1989,7 +2020,7 @@ { for (object *tmp = op->inv; tmp; tmp = tmp->below) { - CLEAR_FLAG (tmp, flag); + tmp->clr_flag (flag); unflag_inv (tmp, flag); } } @@ -2189,45 +2220,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 } /* @@ -2237,13 +2307,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: @@ -2359,9 +2425,9 @@ int can_pick (const object *who, const object *item) { - return /*QUERY_FLAG(who,FLAG_WIZ)|| */ - (item->weight > 0 && !QUERY_FLAG (item, FLAG_NO_PICK) && - !QUERY_FLAG (item, FLAG_ALIVE) && !item->invisible && (who->is_player () || item->weight < who->weight / 3)); + return /*who->flag [FLAG_WIZ]|| */ + (item->weight > 0 && !item->flag [FLAG_NO_PICK] && + !item->flag [FLAG_ALIVE] && !item->invisible && (who->is_player () || item->weight < who->weight / 3)); } /* @@ -2711,3 +2777,18 @@ move_type = mt; } +/* object should be a player. + * we return the object the player has marked with the 'mark' command + * below. If no match is found (or object has changed), we return + * NULL. We leave it up to the calling function to print messages if + * nothing is found. + */ +object * +object::mark () const +{ + if (contr && contr->mark && contr->mark->env == this) + return contr->mark; + else + return 0; +} +