This is the entity system I talked about above that should solve our problems (or at least most of them). I have to leave and it is untested, but it should work. If it dosn't work I will revise it when I come back home later tonight or tomorrow.
typedef struct StackNode
{
ENTITY* StoredEnt;
struct StackNode* prev;
} StackNode;
StackNode* StackFirst = NULL;//needed pointer to start of list
StackNode* StackLast = NULL;//needed pointer to end of list
int StackCount=0;//counter of items in list
void StackAdd(ENTITY* myent)
{
StackNode* StackNew;//new pointer
StackNew = malloc(sizeof(StackNode));//reserve memory
StackNew.StoredEnt = myent;//assign values
if(StackCount==0)//if the list is empty
{
StackFirst=StackNew;
StackNew.prev=NULL;
}
else
{
StackNew.prev=StackLast;
}
StackLast=StackNew;
StackCount++;
}
void StackPop()
{
StackNode* StackCurrent;
StackCurrent=StackLast;
if(StackCurrent!=NULL)//if the list is NOT empty
{
StackLast=StackCurrent.prev;
free(StackCurrent);
StackCount--;
}
}
ENTITY* Ent_Create2(STRING model, VECTOR pos, ANGLE angle)
{
ENTITY* myent;
if(StackLast!=NULL)//if there are availabe entities to recycle in our list
{
myent=StackLast.StoredEnt;
ent_morph(myent,model);
vec_set(myent.x,pos);
vec_set(myent.pan,angle);
reset(myent, PASSABLE| INVISIBLE);
c_setminmax(myent);
StackPop();
return(myent);
}
else
{
myent=ent_create(model,pos,NULL);
vec_set(myent.pan,angle);
c_setminmax(myent);
return(myent);
}
}
void Ent_Remove2(ENTITY* myent)
{
set(myent, PASSABLE| INVISIBLE);
vec_set(myent.x,vector(0,0,-1000000));
ent_morph(myent,"DummyEnt.mdl");
StackAdd(myent);
}
The code is slightly commented, but if there are any questions feel free to ask.
I hope this helps us!
If you come up with any problems derived from using a system like this for recycling entities please post and I will think of another solution.
The only thing I see failing with this is that entity actions are not supported for recycled entities, but for trees, buildings, and most static objects it shouldn't be a problem. If you come up with a better version/system to support actions and events for recycled entities feel free to modify, and mabe post your ideas so I can implement also, I am very interested in giving this system as much flexibility as possible to never have to use ent_remove ever again! grrrr (IMHO, if entities leave some unused memory behind when removed... then this is how ent_create and ent_remove should have been made from the start)