--- deliantra/server/common/object.C 2006/09/03 00:18:40 1.11 +++ deliantra/server/common/object.C 2006/09/08 18:26:22 1.20 @@ -1,6 +1,6 @@ /* * static char *rcsid_object_c = - * "$Id: object.C,v 1.11 2006/09/03 00:18:40 root Exp $"; + * "$Id: object.C,v 1.20 2006/09/08 18:26:22 root Exp $"; */ /* @@ -39,19 +39,9 @@ #include #include #include -#ifdef MEMORY_DEBUG -int nroffreeobjects = 0; int nrofallocobjects = 0; -#undef OBJ_EXPAND -#define OBJ_EXPAND 1 -#else -object objarray[STARTMAX]; /* All objects, allocated this way at first */ -int nroffreeobjects = STARTMAX; /* How many OBs allocated and free (free) */ -int nrofallocobjects = STARTMAX; /* How many OBs allocated (free + used) */ -#endif -object *objects; /* Pointer to the list of used objects */ -object *free_objects; /* Pointer to the list of unused objects */ +object *objects; /* Pointer to the list of used objects */ object *active_objects; /* List of active objects that need to be processed */ short freearr_x[SIZEOFFREE]= @@ -123,116 +113,134 @@ * check weight */ -int CAN_MERGE(object *ob1, object *ob2) { +bool +object::can_merge (object *ob1, object *ob2) +{ + /* A couple quicksanity checks */ + if ((ob1 == ob2) || (ob1->type != ob2->type)) + return 0; - /* A couple quicksanity checks */ - if ((ob1 == ob2) || (ob1->type != ob2->type)) return 0; + if (ob1->speed != ob2->speed) + 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; - /* 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); - /* 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; + if (QUERY_FLAG (ob2, FLAG_IDENTIFIED)) + SET_FLAG (ob2, FLAG_BEEN_APPLIED); - /* 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. - */ - } + /* 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; - /* 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); + /* 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; - if (QUERY_FLAG(ob2, FLAG_IDENTIFIED)) - SET_FLAG(ob2, FLAG_BEEN_APPLIED); + /* inventory ok - still need to check rest of this object to see + * if it is valid. + */ + } + /* 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; - /* 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; + /* 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; - switch (ob1->type) { - case SCROLL: - if (ob1->level != ob2->level) return 0; - break; + 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->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; - } + + //TODO: generate an event or call into perl for additional checks + if (ob1->self || ob2->self) + { + ob1->optimise (); + ob2->optimise (); + + 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 @@ -390,20 +398,9 @@ return op; } -void free_all_object_data(void) { -#ifdef MEMORY_DEBUG - object *op, *next; - - for (op=free_objects; op!=NULL; ) { - next=op->next; - free(op); - nrofallocobjects--; - nroffreeobjects--; - op=next; - } -#endif - LOG(llevDebug,"%d allocated objects, %d free objects, STARMAX=%d\n", - nrofallocobjects, nroffreeobjects,STARTMAX); +void free_all_object_data () +{ + LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); } /* @@ -441,8 +438,6 @@ op->ownercount = 0; } - - /* * Sets the owner and sets the skill and exp pointers to owner's current * skill and experience objects. @@ -498,24 +493,6 @@ } -/* - * Resets vital variables in an object - */ - -void reset_object(object *op) { - - op->name = NULL; - op->name_pl = NULL; - op->title = NULL; - op->race = NULL; - op->slaying = NULL; - op->skill = NULL; - op->msg = NULL; - op->materialname = NULL; - op->lore = NULL; - clear_object(op); -} - /* Zero the key_values on op, decrementing the shared-string * refcounts and freeing the links. */ @@ -531,37 +508,46 @@ op->key_values = 0; } +void object::clear () +{ + attachable_base::clear (); + + free_key_values (this); + + name = 0; + name_pl = 0; + title = 0; + race = 0; + slaying = 0; + skill = 0; + msg = 0; + lore = 0; + custom_name = 0; + materialname = 0; + + memset (static_cast(this), 0, sizeof (object_pod)); + + SET_FLAG (this, FLAG_REMOVED); +} + +void object::clone (object *destination) +{ + *(object_copy *)destination = *(object_copy *)this; + *(object_pod *)destination = *(object_pod *)this; + + if (self || cb) + INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination)); +} + /* * clear_object() frees everything allocated by an object, and also * clears all variables and flags to default settings. */ -void clear_object(object *op) +void clear_object (object *op) { op->clear (); - free_key_values (op); - - /* the memset will clear all these values for us, but we need - * to reduce the refcount on them. - */ - op->name = 0; - op->name_pl = 0; - op->title = 0; - op->race = 0; - op->slaying = 0; - op->skill = 0; - op->msg = 0; - op->lore = 0; - op->materialname = 0; - - memset((void*)op, 0, sizeof (object_special)); - /* Below here, we clear things that are not done by the memset, - * or set default values that are not zero. - */ - /* This is more or less true */ - SET_FLAG (op, FLAG_REMOVED); - op->contr = NULL; op->below = NULL; op->above = NULL; @@ -576,9 +562,9 @@ op->active_prev = NULL; /* What is not cleared is next, prev, and count */ - op->expmul=1.0; + op->expmul = 1.0; op->face = blank_face; - op->attacked_by_count = (tag_t) -1; + op->attacked_by_count = -1; if (settings.casting_time) op->casting_time = -1; @@ -593,25 +579,22 @@ * will point at garbage. */ -void copy_object(object *op2, object *op) +void copy_object (object *op2, object *op) { - int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED); - - op->clear (); + bool is_freed = QUERY_FLAG (op, FLAG_FREED); + bool is_removed = QUERY_FLAG (op, FLAG_REMOVED); - free_key_values (op); - - *(object_special *)op = *(object_special *)op2; op2->clone (op); - if (is_freed) SET_FLAG(op,FLAG_FREED); - if (is_removed) SET_FLAG(op,FLAG_REMOVED); + if (is_freed) SET_FLAG (op, FLAG_FREED); + if (is_removed) SET_FLAG (op, FLAG_REMOVED); if (op2->speed < 0) 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 != NULL) + { key_value *tail = NULL; key_value *i; @@ -636,87 +619,32 @@ tail->next = new_link; tail = new_link; } - } - } + } + } update_ob_speed (op); } -/* - * expand_objects() allocates more objects for the list of unused objects. - * It is called from get_object() if the unused list is empty. - */ - -void expand_objects(void) { - int i; - object *obj; - obj = new object [OBJ_EXPAND]; - - free_objects=obj; - obj[0].prev=NULL; - obj[0].next= &obj[1], - SET_FLAG(&(obj[0]), FLAG_REMOVED); - SET_FLAG(&(obj[0]), FLAG_FREED); - - for(i=1;iprev = this; - if(free_objects==NULL) - expand_objects(); + objects = this; - op = free_objects; + SET_FLAG (this, FLAG_REMOVED); - if (!QUERY_FLAG (op, FLAG_FREED)) - LOG(llevError,"Fatal: Getting busy object.\n"); + expmul = 1.0; + face = blank_face; + attacked_by_count = -1; +} - free_objects = op->next; - - if (free_objects != NULL) - free_objects->prev = NULL; - - op->count= ++ob_count; - op->name=NULL; - op->name_pl=NULL; - op->title=NULL; - op->race=NULL; - op->slaying=NULL; - op->skill = NULL; - op->lore=NULL; - op->msg=NULL; - op->materialname=NULL; - op->next=objects; - op->prev=NULL; - op->active_next = NULL; - op->active_prev = NULL; - if(objects!=NULL) - objects->prev=op; - objects=op; - clear_object(op); - SET_FLAG(op,FLAG_REMOVED); - nroffreeobjects--; - return op; +object::~object () +{ } /* @@ -890,14 +818,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 @@ -933,146 +868,120 @@ * inventory to the ground. */ -void free_object(object *ob) { - free_object2(ob, 0); +void +free_object (object * ob) +{ + free_object2 (ob, 0); } -void free_object2(object *ob, int free_inventory) { - object *tmp,*op; - ob->clear (); +void +free_object2 (object * ob, int free_inventory) +{ + object *tmp, *op; - if (!QUERY_FLAG(ob,FLAG_REMOVED)) { - LOG(llevDebug,"Free object called with non removed object\n"); - dump_object(ob); + if (!QUERY_FLAG (ob, FLAG_REMOVED)) + { + LOG (llevDebug, "Free object called with non removed object\n"); + dump_object (ob); #ifdef MANY_CORES - abort(); + abort (); #endif } - if(QUERY_FLAG(ob,FLAG_FRIENDLY)) { - LOG(llevMonster,"Warning: tried to free friendly object.\n"); - remove_friendly_object(ob); - } - if(QUERY_FLAG(ob,FLAG_FREED)) { - dump_object(ob); - LOG(llevError,"Trying to free freed object.\n%s\n",errmsg); - return; - } - if(ob->more!=NULL) { - free_object2(ob->more, free_inventory); - ob->more=NULL; - } - if (ob->inv) { - /* Only if the space blocks everything do we not process - - * if some form of movemnt 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)) - { - op=ob->inv; - while(op!=NULL) { - tmp=op->below; - remove_ob(op); - free_object2(op, free_inventory); - op=tmp; - } - } - else { /* Put objects in inventory onto this space */ - op=ob->inv; - while(op!=NULL) { - tmp=op->below; - remove_ob(op); - if(QUERY_FLAG(op,FLAG_STARTEQUIP)||QUERY_FLAG(op,FLAG_NO_DROP) || - op->type==RUNE || op->type==TRAP || QUERY_FLAG(op,FLAG_IS_A_TEMPLATE)) - 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=tmp; - } - } - } - /* 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; + + if (QUERY_FLAG (ob, FLAG_FRIENDLY)) + { + LOG (llevMonster, "Warning: tried to free friendly object.\n"); + remove_friendly_object (ob); } - - ob->name = 0; - ob->name_pl = 0; - ob->title = 0; - ob->race = 0; - ob->slaying = 0; - ob->skill = 0; - ob->lore = 0; - ob->msg = 0; - ob->materialname = 0; - free_key_values (ob); + if (QUERY_FLAG (ob, FLAG_FREED)) + { + dump_object (ob); + LOG (llevError, "Trying to free freed object.\n%s\n", errmsg); + return; + } - /* Now link it with the free_objects list: */ - ob->prev=NULL; - ob->next=free_objects; + if (ob->more != NULL) + { + free_object2 (ob->more, free_inventory); + ob->more = NULL; + } - if(free_objects!=NULL) - free_objects->prev=ob; + if (ob->inv) + { + /* Only if the space blocks everything do we not process - + * 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)) + { + op = ob->inv; + + while (op != NULL) + { + tmp = op->below; + remove_ob (op); + free_object2 (op, free_inventory); + op = tmp; + } + } + else + { /* Put objects in inventory onto this space */ + op = ob->inv; + + while (op != NULL) + { + tmp = op->below; + remove_ob (op); + + if (QUERY_FLAG (op, FLAG_STARTEQUIP) + || QUERY_FLAG (op, FLAG_NO_DROP) || op->type == RUNE + || op->type == TRAP || QUERY_FLAG (op, FLAG_IS_A_TEMPLATE)) + 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 = tmp; + } + } + } + + /* Remove object from the active list */ + ob->speed = 0; + update_ob_speed (ob); - free_objects=ob; - nroffreeobjects++; -} + SET_FLAG (ob, FLAG_FREED); + ob->count = 0; -/* - * count_free() returns the number of objects on the list of free objects. - */ + /* Remove this object from the list of used objects */ + if (ob->prev == NULL) + { + objects = ob->next; -int count_free () -{ - int i=0; - object *tmp=free_objects; - while(tmp!=NULL) - tmp=tmp->next, i++; - return i; -} + if (objects != NULL) + objects->prev = NULL; + } + else + { + ob->prev->next = ob->next; -/* - * count_used() returns the number of objects on the list of used objects. - */ + if (ob->next != NULL) + ob->next->prev = ob->prev; + } -int count_used() -{ - int i=0; - object *tmp=objects; - while(tmp!=NULL) - tmp=tmp->next, i++; - return i; -} + free_key_values (ob); -/* - * count_active() returns the number of objects on the list of active objects. - */ + /* Now link it with the free_objects list: */ + ob->prev = 0; + ob->next = 0; -int count_active() -{ - int i=0; - object *tmp=active_objects; - while(tmp!=NULL) - tmp=tmp->active_next, i++; - return i; + delete ob; } /* @@ -1160,7 +1069,6 @@ * to the caller to decide what we want to do. */ op->x=op->env->x,op->y=op->env->y; - op->ox=op->x,op->oy=op->y; op->map=op->env->map; op->above=NULL,op->below=NULL; op->env=NULL; @@ -1381,9 +1289,6 @@ * is clear wrong do we normalize it. */ if (OUT_OF_REAL_MAP(more->map, more->x, more->y)) { - /* Debugging information so you can see the last coordinates this object had */ - more->ox = more->x; - more->oy = 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, @@ -1400,10 +1305,6 @@ } CLEAR_FLAG(op,FLAG_REMOVED); - /* Debugging information so you can see the last coordinates this object had */ - op->ox=op->x; - op->oy=op->y; - /* Ideally, the caller figures this out. However, it complicates a lot * of areas of callers (eg, anything that uses find_free_spot would now * need extra work @@ -1814,7 +1715,6 @@ op->above=NULL; op->below=NULL; op->x=0,op->y=0; - op->ox=0,op->oy=0; /* reset the light list and los of the players on the map */ if((op->glow_radius!=0)&&where->map) @@ -2496,8 +2396,10 @@ op=get_object(); object_thawer thawer (filename); + if (thawer) - load_object(thawer,op,LO_NEWFILE,0); + load_object(thawer,op,0); + LOG(llevDebug," load str completed, object=%s\n", &op->name); CLEAR_FLAG(op,FLAG_REMOVED);