Made the game able to handle entity overload better.

This is a port of yquake2 commit c3b57bc8.
This commit is contained in:
Yamagi 2021-11-04 17:11:41 +01:00
parent fb1ebf960e
commit 8cba5293a0
4 changed files with 72 additions and 27 deletions

View file

@ -1596,8 +1596,15 @@ ai_run(edict_t *self, float dist)
return; return;
} }
tempgoal = G_SpawnOptional();
if (!tempgoal)
{
M_MoveToGoal(self, dist);
return;
}
save = self->goalentity; save = self->goalentity;
tempgoal = G_Spawn();
self->goalentity = tempgoal; self->goalentity = tempgoal;
new = false; new = false;

View file

@ -178,14 +178,19 @@ ThrowGib(edict_t *self, char *gibname, int damage, int type)
return; return;
} }
gibsthisframe++;
if (gibsthisframe > MAX_GIBS) if (gibsthisframe > MAX_GIBS)
{ {
return; return;
} }
gib = G_Spawn(); gib = G_SpawnOptional();
if (!gib)
{
return;
}
gibsthisframe++;
VectorScale(self->size, 0.5, size); VectorScale(self->size, 0.5, size);
VectorAdd(self->absmin, size, origin); VectorAdd(self->absmin, size, origin);
@ -353,14 +358,20 @@ ThrowDebris(edict_t *self, char *modelname, float speed, vec3_t origin)
return; return;
} }
debristhisframe++;
if (debristhisframe > MAX_DEBRIS) if (debristhisframe > MAX_DEBRIS)
{ {
return; return;
} }
chunk = G_Spawn(); chunk = G_SpawnOptional();
if (!chunk)
{
return;
}
debristhisframe++;
VectorCopy(origin, chunk->s.origin); VectorCopy(origin, chunk->s.origin);
gi.setmodel(chunk, modelname); gi.setmodel(chunk, modelname);
v[0] = 100 * crandom(); v[0] = 100 * crandom();

View file

@ -669,40 +669,66 @@ G_InitEdict(edict_t *e)
} }
/* /*
* Either finds a free edict, or allocates a new one. * Either finds a free edict, or allocates a
* Try to avoid reusing an entity that was recently freed, * new one. Try to avoid reusing an entity
* because it can cause the client to think the entity * that was recently freed, because it can
* morphed into something else instead of being removed * cause the client to think the entity
* and recreated, which can cause interpolated angles and * morphed into something else instead of
* bad trails. * being removed and recreated, which can
* cause interpolated angles and bad trails.
*/ */
edict_t * #define POLICY_DEFAULT 0
G_Spawn(void) #define POLICY_DESPERATE 1
static edict_t *
G_FindFreeEdict(int policy)
{ {
int i;
edict_t *e; edict_t *e;
e = &g_edicts[(int)maxclients->value + 1]; for (e = g_edicts + game.maxclients + 1 ; e < &g_edicts[globals.num_edicts] ; e++)
for (i = maxclients->value + 1; i < globals.num_edicts; i++, e++)
{ {
/* the first couple seconds of server time can involve a lot of /* the first couple seconds of server time can involve a lot of
freeing and allocating, so relax the replacement policy */ freeing and allocating, so relax the replacement policy
if (!e->inuse && */
((e->freetime < 2) || (level.time - e->freetime > 0.5))) if (!e->inuse && (policy == POLICY_DESPERATE || e->freetime < 2.0f || (level.time - e->freetime) > 0.5f))
{ {
G_InitEdict(e); G_InitEdict (e);
return e; return e;
} }
} }
if (i == game.maxentities) return NULL;
}
edict_t *
G_SpawnOptional(void)
{
edict_t *e = G_FindFreeEdict (POLICY_DEFAULT);
if (e)
{ {
gi.error("ED_Alloc: no free edicts"); return e;
} }
globals.num_edicts++; if (globals.num_edicts >= game.maxentities)
G_InitEdict(e); {
return G_FindFreeEdict (POLICY_DESPERATE);
}
e = &g_edicts[globals.num_edicts++];
G_InitEdict (e);
return e;
}
edict_t *
G_Spawn(void)
{
edict_t *e = G_SpawnOptional();
if (!e)
gi.error ("ED_Alloc: no free edicts");
return e; return e;
} }

View file

@ -722,6 +722,7 @@ void G_UseTargets(edict_t *ent, edict_t *activator);
void G_SetMovedir(vec3_t angles, vec3_t movedir); void G_SetMovedir(vec3_t angles, vec3_t movedir);
void G_InitEdict(edict_t *e); void G_InitEdict(edict_t *e);
edict_t *G_SpawnOptional(void);
edict_t *G_Spawn(void); edict_t *G_Spawn(void);
void G_FreeEdict(edict_t *e); void G_FreeEdict(edict_t *e);