--- deliantra/server/common/object.C 2006/09/04 13:55:54 1.15 +++ deliantra/server/common/object.C 2006/09/10 14:54:02 1.23 @@ -1,6 +1,6 @@ /* * static char *rcsid_object_c = - * "$Id: object.C,v 1.15 2006/09/04 13:55:54 root Exp $"; + * "$Id: object.C,v 1.23 2006/09/10 14:54:02 root Exp $"; */ /* @@ -57,7 +57,6 @@ 0,1,2,3,4,5,6,7,8,1,2,2,2,3,4,4,4,5,6,6,6,7,8,8,8, 1,2,2,2,2,2,3,4,4,4,4,4,5,6,6,6,6,6,7,8,8,8,8,8}; - /* Returns TRUE if every key_values in wants has a partner with the same value in has. */ static int compare_ob_value_lists_one(const object * wants, const object * has) { key_value * wants_field; @@ -113,116 +112,134 @@ * check weight */ -int CAN_MERGE(object *ob1, object *ob2) { - - /* A couple quicksanity checks */ - if ((ob1 == ob2) || (ob1->type != ob2->type)) return 0; - - if (ob1->speed != ob2->speed) 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) && FABS((ob1)->speed) > MIN_ACTIVE_SPEED) - return 0; - - /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that - * value could not be stored in a sint32 (which unfortunately sometimes is - * used to store nrof). - */ - if (ob1->nrof+ob2->nrof >= 1UL<<31) - return 0; - - /* This is really a spellbook check - really, we should - * check all objects in the inventory. - */ - if (ob1->inv || ob2->inv) { - /* if one object has inventory but the other doesn't, not equiv */ - if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv)) return 0; - - /* Now check to see if the two inventory objects could merge */ - if (!CAN_MERGE(ob1->inv, ob2->inv)) return 0; +bool +object::can_merge (object *ob1, object *ob2) +{ + /* A couple quicksanity checks */ + if ((ob1 == ob2) || (ob1->type != ob2->type)) + return 0; + + if (ob1->speed != ob2->speed) + return 0; + + /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that + * value could not be stored in a sint32 (which unfortunately sometimes is + * used to store nrof). + */ + if (ob1->nrof + ob2->nrof >= 1UL << 31) + return 0; + + /* If the objects have been identified, set the BEEN_APPLIED flag. + * This is to the comparison of the flags below will be OK. We + * just can't ignore the been applied or identified flags, as they + * 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 (QUERY_FLAG (ob2, FLAG_IDENTIFIED)) + SET_FLAG (ob2, FLAG_BEEN_APPLIED); + + + /* the 0x400000 on flags2 is FLAG_INV_LOCK. I don't think something + * being locked in inventory should prevent merging. + * 0x4 in flags3 is CLIENT_SENT + */ + if ((ob1->arch != ob2->arch) || + (ob1->flags[0] != ob2->flags[0]) || + (ob1->flags[1] != ob2->flags[1]) || + ((ob1->flags[2] & ~0x400000) != (ob2->flags[2] & ~0x400000)) || + ((ob1->flags[3] & ~0x4) != (ob2->flags[3] & ~0x4)) || + (ob1->name != ob2->name) || + (ob1->title != ob2->title) || + (ob1->msg != ob2->msg) || + (ob1->weight != ob2->weight) || + (memcmp (&ob1->resist, &ob2->resist, sizeof (ob1->resist)) != 0) || + (memcmp (&ob1->stats, &ob2->stats, sizeof (ob1->stats)) != 0) || + (ob1->attacktype != ob2->attacktype) || + (ob1->magic != ob2->magic) || + (ob1->slaying != ob2->slaying) || + (ob1->skill != ob2->skill) || + (ob1->value != ob2->value) || + (ob1->animation_id != ob2->animation_id) || + (ob1->client_type != ob2->client_type) || + (ob1->materialname != ob2->materialname) || + (ob1->lore != ob2->lore) || + (ob1->subtype != ob2->subtype) || + (ob1->move_type != ob2->move_type) || + (ob1->move_block != ob2->move_block) || + (ob1->move_allow != ob2->move_allow) || + (ob1->move_on != ob2->move_on) || + (ob1->move_off != ob2->move_off) || + (ob1->move_slow != ob2->move_slow) || + (ob1->move_slow_penalty != ob2->move_slow_penalty)) + return 0; + + /* This is really a spellbook check - really, we should + * check all objects in the inventory. + */ + if (ob1->inv || ob2->inv) + { + /* if one object has inventory but the other doesn't, not equiv */ + if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv)) + return 0; + + /* Now check to see if the two inventory objects could merge */ + if (!CAN_MERGE (ob1->inv, ob2->inv)) + return 0; - /* inventory ok - still need to check rest of this object to see - * if it is valid. - */ + /* inventory ok - still need to check rest of this object to see + * if it is valid. + */ } - /* If the objects have been identified, set the BEEN_APPLIED flag. - * This is to the comparison of the flags below will be OK. We - * just can't ignore the been applied or identified flags, as they - * 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); + /* Don't merge objects that are applied. With the new 'body' code, + * 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)) + 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) + && FABS ((ob1)->speed) > MIN_ACTIVE_SPEED) + return 0; - if (QUERY_FLAG(ob2, FLAG_IDENTIFIED)) - SET_FLAG(ob2, FLAG_BEEN_APPLIED); + switch (ob1->type) + { + case SCROLL: + if (ob1->level != ob2->level) + return 0; + break; + } + if (ob1->key_values != NULL || ob2->key_values != NULL) + { + /* At least one of these has key_values. */ + if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) + /* One has fields, but the other one doesn't. */ + return 0; + else if (!compare_ob_value_lists (ob1, ob2)) + return 0; + } - /* the 0x400000 on flags2 is FLAG_INV_LOCK. I don't think something - * being locked in inventory should prevent merging. - * 0x4 in flags3 is CLIENT_SENT - */ - if ((ob1->arch != ob2->arch) || - (ob1->flags[0] != ob2->flags[0]) || - (ob1->flags[1] != ob2->flags[1]) || - ((ob1->flags[2] & ~0x400000) != (ob2->flags[2] & ~ 0x400000)) || - ((ob1->flags[3] & ~0x4) != (ob2->flags[3] & ~0x4)) || - (ob1->name != ob2->name) || - (ob1->title != ob2->title) || - (ob1->msg != ob2->msg) || - (ob1->weight != ob2->weight) || - (memcmp(&ob1->resist, &ob2->resist, sizeof(ob1->resist))!=0) || - (memcmp(&ob1->stats, &ob2->stats, sizeof(ob1->stats))!=0) || - (ob1->attacktype != ob2->attacktype) || - (ob1->magic != ob2->magic) || - (ob1->slaying != ob2->slaying) || - (ob1->skill != ob2->skill) || - (ob1->value != ob2->value) || - (ob1->animation_id != ob2->animation_id) || - (ob1->client_type != ob2->client_type) || - (ob1->materialname != ob2->materialname) || - (ob1->lore != ob2->lore) || - (ob1->subtype != ob2->subtype) || - (ob1->move_type != ob2->move_type) || - (ob1->move_block != ob2->move_block) || - (ob1->move_allow != ob2->move_allow) || - (ob1->move_on != ob2->move_on) || - (ob1->move_off != ob2->move_off) || - (ob1->move_slow != ob2->move_slow) || - (ob1->move_slow_penalty != ob2->move_slow_penalty) - ) - return 0; - - /* Don't merge objects that are applied. With the new 'body' code, - * 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)) - return 0; + //TODO: generate an event or call into perl for additional checks + if (ob1->self || ob2->self) + { + ob1->optimise (); + ob2->optimise (); - switch (ob1->type) { - case SCROLL: - if (ob1->level != ob2->level) return 0; - break; - - } - if (ob1->key_values != NULL || ob2->key_values != NULL) { - /* At least one of these has key_values. */ - if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) { - /* One has fields, but the other one doesn't. */ - return 0; - } else if (!compare_ob_value_lists(ob1, ob2)) { - return 0; - } + if (ob1->self || ob2->self) + return 0; } - /* Everything passes, must be OK. */ - return 1; + /* Everything passes, must be OK. */ + return 1; } - /* * sum_weight() is a recursive function which calculates the weight * an object is carrying. It goes through in figures out how much @@ -514,8 +531,8 @@ void object::clone (object *destination) { - *(object_copy *)destination = *(object_copy *)this; - *(object_pod *)destination = *(object_pod *)this; + *(object_copy *)destination = *this; + *(object_pod *)destination = *this; if (self || cb) INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination)); @@ -575,23 +592,23 @@ op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0; /* Copy over key_values, if any. */ - if (op2->key_values != NULL) + if (op2->key_values) { - key_value *tail = NULL; + key_value *tail = 0; key_value *i; - op->key_values = NULL; + op->key_values = 0; - for (i = op2->key_values; i != NULL; i = i->next) + for (i = op2->key_values; i; i = i->next) { key_value *new_link = new key_value; - new_link->next = NULL; + new_link->next = 0; new_link->key = i->key; new_link->value = i->value; /* Try and be clever here, too. */ - if (op->key_values == NULL) + if (!op->key_values) { op->key_values = new_link; tail = new_link; @@ -608,38 +625,6 @@ } /* - * get_object() grabs an object from the list of unused objects, makes - * sure it is initialised, and returns it. - * If there are no free objects, expand_objects() is called to get more. - */ - -object *get_object () -{ - object *op = new object; - - op->count = ++ob_count; - - op->active_next = 0; - op->active_prev = 0; - - op->next = objects; - op->prev = 0; - - if (objects) - objects->prev = op; - - objects = op; - - SET_FLAG (op, FLAG_REMOVED); - - op->expmul = 1.0; - op->face = blank_face; - op->attacked_by_count = -1; - - return op; -} - -/* * If an object with the IS_TURNABLE() flag needs to be turned due * 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. @@ -810,14 +795,21 @@ if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags & P_IS_ALIVE)) update_now=1; + if (op->type == SAFE_GROUND && !(flags & P_SAFE)) + update_now=1; + if ((move_on | op->move_on) != move_on) update_now=1; + if ((move_off | op->move_off) != move_off) update_now=1; + /* This isn't perfect, but I don't expect a lot of objects to * to have move_allow right now. */ if (((move_block | op->move_block) & ~op->move_allow) != move_block) update_now=1; - if ((move_slow | op->move_slow) != move_slow) update_now=1; + + if ((move_slow | op->move_slow) != move_slow) + update_now=1; } /* if the object is being removed, we can't make intelligent * decisions, because remove_ob can't really pass the object @@ -841,6 +833,61 @@ update_object(op->more, action); } +static std::vector mortals; + +void object::free_mortals () +{ + for (std::vector::iterator i = mortals.begin (); i != mortals.end (); ++i) + delete *i; + + mortals.clear (); +} + +object::object () +{ + SET_FLAG (this, FLAG_REMOVED); + + expmul = 1.0; + face = blank_face; + attacked_by_count = -1; +} + +object::~object () +{ + free_key_values (this); +} + +void +object::link () +{ + count = ++ob_count; + + prev = 0; + next = objects; + + if (objects) + objects->prev = this; + + objects = this; +} + +void +object::unlink () +{ + count = 0; + + /* Remove this object from the list of used objects */ + if (prev) prev->next = next; + if (next) next->prev = prev; + if (this == objects) objects = next; +} + +object *object::create () +{ + object *op = new object; + op->link (); + return op; +} /* * free_object() frees everything allocated by an object, removes @@ -852,73 +899,64 @@ * If free_inventory is set, free inventory as well. Else drop items in * inventory to the ground. */ - -void -free_object (object * ob) -{ - free_object2 (ob, 0); -} - void -free_object2 (object * ob, int free_inventory) +object::free (bool free_inventory) { - object *tmp, *op; - - if (!QUERY_FLAG (ob, FLAG_REMOVED)) + if (!QUERY_FLAG (this, FLAG_REMOVED)) { LOG (llevDebug, "Free object called with non removed object\n"); - dump_object (ob); + dump_object (this); #ifdef MANY_CORES abort (); #endif } - if (QUERY_FLAG (ob, FLAG_FRIENDLY)) + if (QUERY_FLAG (this, FLAG_FRIENDLY)) { LOG (llevMonster, "Warning: tried to free friendly object.\n"); - remove_friendly_object (ob); + remove_friendly_object (this); } - if (QUERY_FLAG (ob, FLAG_FREED)) + if (QUERY_FLAG (this, FLAG_FREED)) { - dump_object (ob); + dump_object (this); LOG (llevError, "Trying to free freed object.\n%s\n", errmsg); return; } - if (ob->more != NULL) + if (more) { - free_object2 (ob->more, free_inventory); - ob->more = NULL; + more->free (free_inventory); + more = 0; } - if (ob->inv) + if (inv) { /* Only if the space blocks everything do we not process - - * if some form of movemnt is allowed, let objects + * if some form of movement is allowed, let objects * drop on that space. */ - if (free_inventory || ob->map == NULL - || ob->map->in_memory != MAP_IN_MEMORY - || (GET_MAP_MOVE_BLOCK (ob->map, ob->x, ob->y) == MOVE_ALL)) + if (free_inventory || !map + || map->in_memory != MAP_IN_MEMORY + || (GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL)) { - op = ob->inv; + object *op = inv; - while (op != NULL) + while (op) { - tmp = op->below; + object *tmp = op->below; remove_ob (op); - free_object2 (op, free_inventory); + op->free (free_inventory); op = tmp; } } else { /* Put objects in inventory onto this space */ - op = ob->inv; + object *op = inv; - while (op != NULL) + while (op) { - tmp = op->below; + object *tmp = op->below; remove_ob (op); if (QUERY_FLAG (op, FLAG_STARTEQUIP) @@ -927,9 +965,9 @@ free_object (op); else { - op->x = ob->x; - op->y = ob->y; - insert_ob_in_map (op, ob->map, NULL, 0); /* Insert in same map as the envir */ + op->x = x; + op->y = y; + insert_ob_in_map (op, map, 0, 0); /* Insert in same map as the envir */ } op = tmp; @@ -938,35 +976,14 @@ } /* Remove object from the active list */ - ob->speed = 0; - update_ob_speed (ob); - - SET_FLAG (ob, FLAG_FREED); - ob->count = 0; - - /* Remove this object from the list of used objects */ - if (ob->prev == NULL) - { - objects = ob->next; - - if (objects != NULL) - objects->prev = NULL; - } - else - { - ob->prev->next = ob->next; - - if (ob->next != NULL) - ob->next->prev = ob->prev; - } + speed = 0; + update_ob_speed (this); - free_key_values (ob); + unlink (); - /* Now link it with the free_objects list: */ - ob->prev = 0; - ob->next = 0; + SET_FLAG (this, FLAG_FREED); - delete ob; + mortals.push_back (this); } /*