--- deliantra/server/common/object.C 2006/08/31 17:54:14 1.10 +++ deliantra/server/common/object.C 2006/09/04 16:46:32 1.16 @@ -1,6 +1,6 @@ /* * static char *rcsid_object_c = - * "$Id: object.C,v 1.10 2006/08/31 17:54:14 root Exp $"; + * "$Id: object.C,v 1.16 2006/09/04 16:46:32 root Exp $"; */ /* @@ -39,20 +39,10 @@ #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 *active_objects; /* List of active objects that need to be processed */ +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]= {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, @@ -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; - - /* 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. - */ - } - - /* 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; - - /* 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; + 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; + /* 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. + */ } - 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; - } + + /* 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; } - /* Everything passes, must be OK. */ - return 1; -} + 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; +} /* * sum_weight() is a recursive function which calculates the weight * an object is carrying. It goes through in figures out how much @@ -278,7 +286,7 @@ /* * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. - * Some error messages. + * Some error messages. * The result of the dump is stored in the static global errmsg array. */ @@ -381,29 +389,18 @@ */ object *find_object_name(const char *str) { - const char *name=add_string(str); + const char *name = shstr::find (str); object *op; for(op=objects;op!=NULL;op=op->next) - if(op->name==name) + if(&op->name == name) break; - free_string(name); + 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,119 +493,81 @@ } -/* - * 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. */ -static void free_key_values(object * op) { - key_value * i; - key_value * next = NULL; - - if (op->key_values == NULL) return; - - for (i = op->key_values; i != NULL; i = next) { - /* Store next *first*. */ - next = i->next; - - if (i->key) FREE_AND_CLEAR_STR(i->key); - if (i->value) FREE_AND_CLEAR_STR(i->value); - i->next = NULL; - free(i); +static void free_key_values(object * op) +{ + for (key_value *i = op->key_values; i != 0; ) + { + key_value *next = i->next; + delete i; + i = next; } - - op->key_values = NULL; + + op->key_values = 0; } +void object::clear () +{ + attachable_base::clear (); -/* - * clear_object() frees everything allocated by an object, and also - * clears all variables and flags to default settings. - */ - -void clear_object(object *op) { - op->clear (); + free_key_values (this); - event *evt; - event *evt2; + name = 0; + name_pl = 0; + title = 0; + race = 0; + slaying = 0; + skill = 0; + msg = 0; + lore = 0; + custom_name = 0; + materialname = 0; - /* redo this to be simpler/more efficient. Was also seeing - * crashes in the old code. Move this to the top - am - * seeing periodic crashes in this code, and would like to have - * as much info available as possible (eg, object name). - */ - for (evt = op->events; evt; evt=evt2) { - evt2 = evt->next; + memset (static_cast(this), 0, sizeof (object_pod)); - if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook); - if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin); - if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options); - - free(evt); - } - op->events = NULL; - - free_key_values(op); + SET_FLAG (this, FLAG_REMOVED); +} - /* the memset will clear all these values for us, but we need - * to reduce the refcount on them. - */ - if (op->name!=NULL) FREE_AND_CLEAR_STR(op->name); - if (op->name_pl!=NULL) FREE_AND_CLEAR_STR(op->name_pl); - if (op->title != NULL) FREE_AND_CLEAR_STR(op->title); - if (op->race!=NULL) FREE_AND_CLEAR_STR(op->race); - if (op->slaying!=NULL) FREE_AND_CLEAR_STR(op->slaying); - if (op->skill!=NULL) FREE_AND_CLEAR_STR(op->skill); - if (op->msg!=NULL) FREE_AND_CLEAR_STR(op->msg); - if (op->lore!=NULL) FREE_AND_CLEAR_STR(op->lore); - if (op->materialname!= NULL) FREE_AND_CLEAR_STR(op->materialname); +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)); +} - 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); +/* + * clear_object() frees everything allocated by an object, and also + * clears all variables and flags to default settings. + */ +void clear_object (object *op) +{ + op->clear (); - op->contr = NULL; - op->below=NULL; - op->above=NULL; - op->inv=NULL; - op->events=NULL; - op->container=NULL; - op->env=NULL; - op->more=NULL; - op->head=NULL; - op->map=NULL; - op->refcount=0; - op->active_next = NULL; - op->active_prev = NULL; - /* What is not cleared is next, prev, and count */ + op->contr = NULL; + op->below = NULL; + op->above = NULL; + op->inv = NULL; + op->container=NULL; + op->env=NULL; + op->more=NULL; + op->head=NULL; + op->map=NULL; + op->refcount=0; + op->active_next = NULL; + op->active_prev = NULL; + /* What is not cleared is next, prev, and count */ - op->expmul=1.0; - op->face = blank_face; - op->attacked_by_count = (tag_t) -1; - if (settings.casting_time) - op->casting_time = -1; + op->expmul = 1.0; + op->face = blank_face; + op->attacked_by_count = -1; + if (settings.casting_time) + op->casting_time = -1; } /* @@ -622,192 +579,81 @@ * will point at garbage. */ -void copy_object(object *op2, object *op) { - int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED); - event *evt, *evt2, *evt_new; - - op->clear (); - - /* Decrement the refcounts, but don't bother zeroing the fields; - they'll be overwritten by memcpy. */ - if(op->name!=NULL) free_string(op->name); - if(op->name_pl!=NULL) free_string(op->name_pl); - if(op->title!=NULL) free_string(op->title); - if(op->race!=NULL) free_string(op->race); - if(op->slaying!=NULL) free_string(op->slaying); - if(op->skill!=NULL) free_string(op->skill); - if(op->msg!=NULL) free_string(op->msg); - if(op->lore!=NULL) free_string(op->lore); - if(op->materialname != NULL) free_string(op->materialname); - if(op->custom_name != NULL) free_string(op->custom_name); - - /* Basically, same code as from clear_object() */ - for (evt = op->events; evt; evt=evt2) { - evt2 = evt->next; - - if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook); - if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin); - if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options); - - free(evt); - } - op->events = NULL; - - 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(op->name!=NULL) add_refcount(op->name); - if(op->name_pl!=NULL) add_refcount(op->name_pl); - if(op->title!=NULL) add_refcount(op->title); - if(op->race!=NULL) add_refcount(op->race); - if(op->slaying!=NULL) add_refcount(op->slaying); - if(op->skill!=NULL) add_refcount(op->skill); - if(op->lore!=NULL) add_refcount(op->lore); - if(op->msg!=NULL) add_refcount(op->msg); - if(op->custom_name!=NULL) add_refcount(op->custom_name); - if (op->materialname != NULL) add_refcount(op->materialname); - - if((op2->speed<0) && !editor) - op->speed_left=op2->speed_left-RANDOM()%200/100.0; - - /* Copy over event information */ - evt2 = NULL; - for (evt = op2->events; evt; evt=evt->next) { - evt_new = (event *) malloc(sizeof(event)); - memcpy(evt_new, evt, sizeof(event)); - if (evt_new->hook) add_refcount(evt_new->hook); - if (evt_new->plugin) add_refcount(evt_new->plugin); - if (evt_new->options) add_refcount(evt_new->options); - evt_new->next = NULL; - - /* Try to be a little clever here, and store away the - * last event we copied, so that its simpler to update the - * pointer. - */ - if (evt2) - evt2->next = evt_new; - else - op->events = evt_new; +void copy_object (object *op2, object *op) +{ + bool is_freed = QUERY_FLAG (op, FLAG_FREED); + bool is_removed = QUERY_FLAG (op, FLAG_REMOVED); - evt2 = evt_new; - } - /* Copy over key_values, if any. */ - if (op2->key_values != NULL) { - key_value * tail = NULL; - key_value * i; + op2->clone (op); - op->key_values = NULL; + if (is_freed) SET_FLAG (op, FLAG_FREED); + if (is_removed) SET_FLAG (op, FLAG_REMOVED); - for (i = op2->key_values; i != NULL; i = i->next) { - key_value * new_link = (key_value *) malloc(sizeof(key_value)); + if (op2->speed < 0) + op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0; - new_link->next = NULL; - new_link->key = add_refcount(i->key); - if (i->value) - new_link->value = add_refcount(i->value); - else - new_link->value = NULL; + /* Copy over key_values, if any. */ + if (op2->key_values != NULL) + { + key_value *tail = NULL; + key_value *i; + + op->key_values = NULL; - /* Try and be clever here, too. */ - if (op->key_values == NULL) { - op->key_values = new_link; - tail = new_link; - } else { - tail->next = new_link; - tail = new_link; + for (i = op2->key_values; i != NULL; i = i->next) + { + key_value *new_link = new key_value; + + new_link->next = NULL; + new_link->key = i->key; + new_link->value = i->value; + + /* Try and be clever here, too. */ + if (op->key_values == NULL) + { + op->key_values = new_link; + tail = new_link; + } + else + { + tail->next = new_link; + tail = new_link; } } } - update_ob_speed(op); + 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. + * 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. */ -void expand_objects(void) { - int i; - object *obj; - obj = (object *) CALLOC(OBJ_EXPAND,sizeof(object)); +object *get_object () +{ + object *op = new object; - if(obj==NULL) - fatal(OUT_OF_MEMORY); - 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;icount = ++ob_count; - nrofallocobjects += OBJ_EXPAND; - nroffreeobjects += OBJ_EXPAND; -} + op->active_next = 0; + op->active_prev = 0; -/* - * 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. - */ + op->next = objects; + op->prev = 0; -object *get_object(void) { - object *op; + if (objects) + objects->prev = op; - if(free_objects==NULL) { - expand_objects(); - } - op=free_objects; -#ifdef MEMORY_DEBUG - /* The idea is hopefully by doing a realloc, the memory - * debugging program will now use the current stack trace to - * report leaks. - */ - op = realloc(op, sizeof(object)); - SET_FLAG(op, FLAG_REMOVED); - SET_FLAG(op, FLAG_FREED); -#endif + objects = op; + + SET_FLAG (op, FLAG_REMOVED); + + op->expmul = 1.0; + op->face = blank_face; + op->attacked_by_count = -1; - if(!QUERY_FLAG(op,FLAG_FREED)) { - LOG(llevError,"Fatal: Getting busy object.\n"); - } - 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; } @@ -838,7 +684,7 @@ */ if (QUERY_FLAG(op, FLAG_FREED) && op->speed) { - LOG(llevError,"Object %s is freed but has speed.\n", op->name); + LOG(llevError,"Object %s is freed but has speed.\n", &op->name); #ifdef MANY_CORES abort(); #else @@ -932,7 +778,7 @@ void update_object(object *op, int action) { int update_now=0, flags; - MoveType move_on, move_off, move_block, move_slow; + MoveType move_on, move_off, move_block, move_slow; if (op == NULL) { /* this should never happen */ @@ -1025,158 +871,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; + + if (QUERY_FLAG (ob, FLAG_FRIENDLY)) + { + LOG (llevMonster, "Warning: tried to free friendly object.\n"); + remove_friendly_object (ob); } - else { - ob->prev->next=ob->next; - if(ob->next!=NULL) - ob->next->prev=ob->prev; + + if (QUERY_FLAG (ob, FLAG_FREED)) + { + dump_object (ob); + LOG (llevError, "Trying to free freed object.\n%s\n", errmsg); + return; } - - if(ob->name!=NULL) FREE_AND_CLEAR_STR(ob->name); - if(ob->name_pl!=NULL) FREE_AND_CLEAR_STR(ob->name_pl); - if(ob->title!=NULL) FREE_AND_CLEAR_STR(ob->title); - if(ob->race!=NULL) FREE_AND_CLEAR_STR(ob->race); - if(ob->slaying!=NULL) FREE_AND_CLEAR_STR(ob->slaying); - if(ob->skill!=NULL) FREE_AND_CLEAR_STR(ob->skill); - if(ob->lore!=NULL) FREE_AND_CLEAR_STR(ob->lore); - if(ob->msg!=NULL) FREE_AND_CLEAR_STR(ob->msg); - if(ob->materialname!=NULL) FREE_AND_CLEAR_STR(ob->materialname); - - /* Why aren't events freed? */ - free_key_values(ob); + if (ob->more != NULL) + { + free_object2 (ob->more, free_inventory); + ob->more = NULL; + } -#if 0 /* MEMORY_DEBUG*/ - /* This is a nice idea. Unfortunately, a lot of the code in crossfire - * presumes the freed_object will stick around for at least a little - * bit - */ - /* this is necessary so that memory debugging programs will - * be able to accurately report source of malloc. If we recycle - * objects, then some other area may be doing the get_object - * and not freeing it, but the original one that malloc'd the - * object will get the blame. - */ - free(ob); -#else + 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); - /* Now link it with the free_objects list: */ - ob->prev=NULL; - ob->next=free_objects; - if(free_objects!=NULL) - free_objects->prev=ob; - free_objects=ob; - nroffreeobjects++; -#endif -} + 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(void) { - 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(void) { - 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(void) { - int i=0; - object *tmp=active_objects; - while(tmp!=NULL) - tmp=tmp->active_next, i++; - return i; + delete ob; } /* @@ -1264,7 +1072,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; @@ -1349,7 +1156,7 @@ move_apply(tmp, op, NULL); if (was_destroyed (op, tag)) { LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " - "leaving object\n", tmp->name, tmp->arch->name); + "leaving object\n", &tmp->name, &tmp->arch->name); } } @@ -1485,9 +1292,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, @@ -1504,10 +1308,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 @@ -1584,7 +1384,7 @@ */ /* Have object 'fall below' other objects that block view. - * Unless those objects are exits, type 66 + * Unless those objects are exits, type 66 * 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. @@ -1593,7 +1393,7 @@ (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) && (op->face && !op->face->visibility)) { for (last=top; last != floor; last=last->below) - if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break; + 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 @@ -1632,10 +1432,10 @@ * it, so save a few ticks and start from there. */ if (!(flag & INS_MAP_LOAD)) - for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) { + for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) { if (tmp->type == PLAYER) - tmp->contr->socket.update_look=1; - } + tmp->contr->socket.update_look=1; + } /* If this object glows, it may affect lighting conditions that are * visible to others on this map. But update_all_los is really @@ -1717,7 +1517,7 @@ if(orig_ob->nrofnrof?orig_ob->nrof:1, orig_ob->name); + orig_ob->nrof?orig_ob->nrof:1, &orig_ob->name); return NULL; } newob = object_create_clone(orig_ob); @@ -1874,7 +1674,7 @@ } if (op->more) { LOG(llevError, "Tried to insert multipart object %s (%d)\n", - op->name, op->count); + &op->name, op->count); return op; } CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL); @@ -1918,7 +1718,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) @@ -1970,7 +1769,7 @@ tag_t tag; mapstruct *m=op->map; int x=op->x, y=op->y; - MoveType move_on, move_slow, move_block; + MoveType move_on, move_slow, move_block; if(QUERY_FLAG(op,FLAG_NO_APPLY)) return 0; @@ -2298,7 +2097,7 @@ sint16 nx, ny; object *tmp; mapstruct *mp; - MoveType blocked, move_type; + MoveType blocked, move_type; if (exclude && exclude->head) { exclude = exclude->head; @@ -2600,9 +2399,11 @@ op=get_object(); object_thawer thawer (filename); + if (thawer) - load_object(thawer,op,LO_NEWFILE,0); - LOG(llevDebug," load str completed, object=%s\n",op->name); + load_object(thawer,op,0); + + LOG(llevDebug," load str completed, object=%s\n", &op->name); CLEAR_FLAG(op,FLAG_REMOVED); return op; @@ -2651,7 +2452,7 @@ key_value * link; const char * canonical_key; - canonical_key = find_string(key); + canonical_key = shstr::find (key); if (canonical_key == NULL) { /* 1. There being a field named key on any object @@ -2684,7 +2485,7 @@ * * Returns TRUE on success. */ -int set_ob_key_value_s(object * op, const char * canonical_key, const char * value, int add_key) { +int set_ob_key_value_s(object * op, const shstr &canonical_key, const char * value, int add_key) { key_value * field = NULL, *last=NULL; for (field=op->key_values; field != NULL; field=field->next) { @@ -2693,25 +2494,23 @@ continue; } - if (field->value) FREE_AND_CLEAR_STR(field->value); if (value) - field->value = add_string(value); + field->value = value; else { /* Basically, if the archetype has this key set, * we need to store the null value so when we save * it, we save the empty value so that when we load, * we get this value back again. */ - if (get_ob_key_link(&op->arch->clone, canonical_key)) - field->value = NULL; - else { - /* Delete this link */ - if (field->key) FREE_AND_CLEAR_STR(field->key); - if (field->value) FREE_AND_CLEAR_STR(field->value); + if (get_ob_key_link (&op->arch->clone, canonical_key)) + field->value = 0; + else + { if (last) last->next = field->next; else op->key_values = field->next; - free(field); - } + + delete field; + } } return TRUE; } @@ -2730,10 +2529,10 @@ */ if (value == NULL) return TRUE; - field = (key_value *) malloc(sizeof(key_value)); + field = new key_value; - field->key = add_refcount(canonical_key); - field->value = add_string(value); + field->key = canonical_key; + field->value = value; /* Usual prepend-addition. */ field->next = op->key_values; op->key_values = field; @@ -2750,26 +2549,8 @@ * * Returns TRUE on success. */ -int set_ob_key_value(object * op, const char * key, const char * value, int add_key) { - const char * canonical_key = NULL; - int floating_ref = FALSE; - int ret; - - /* HACK This mess is to make sure set_ob_value() passes a shared string - * to get_ob_key_link(), without leaving a leaked refcount. - */ - - canonical_key = find_string(key); - if (canonical_key == NULL) { - canonical_key = add_string(key); - floating_ref = TRUE; - } - - ret = set_ob_key_value_s(op, canonical_key, value, add_key); - - if (floating_ref) { - free_string(canonical_key); - } - - return ret; +int set_ob_key_value(object * op, const char *key, const char * value, int add_key) +{ + shstr key_ (key); + return set_ob_key_value_s (op, key_, value, add_key); }