Fixed player_noise entity leak and improved stability surrounding player noises

This commit is contained in:
BjossiAlfreds 2021-11-05 15:35:20 +00:00
parent 84746289fa
commit 6df8052f10

View file

@ -27,6 +27,9 @@
#include "../header/local.h" #include "../header/local.h"
#include "../monster/misc/player.h" #include "../monster/misc/player.h"
#define PLAYER_NOISE_SELF 0
#define PLAYER_NOISE_IMPACT 1
#define FRAME_FIRE_FIRST (FRAME_ACTIVATE_LAST + 1) #define FRAME_FIRE_FIRST (FRAME_ACTIVATE_LAST + 1)
#define FRAME_IDLE_FIRST (FRAME_FIRE_LAST + 1) #define FRAME_IDLE_FIRST (FRAME_FIRE_LAST + 1)
#define FRAME_DEACTIVATE_FIRST (FRAME_IDLE_LAST + 1) #define FRAME_DEACTIVATE_FIRST (FRAME_IDLE_LAST + 1)
@ -90,12 +93,111 @@ P_ProjectSource(edict_t *ent, vec3_t distance,
* Monsters that don't directly see the player can move * Monsters that don't directly see the player can move
* to a noise in hopes of seeing the player from there. * to a noise in hopes of seeing the player from there.
*/ */
static edict_t *
PlayerNoise_Spawn(edict_t *who, int type)
{
edict_t *noise;
if (!who)
{
return NULL;
}
noise = G_SpawnOptional();
if (!noise)
{
return NULL;
}
noise->classname = "player_noise";
noise->spawnflags = type;
VectorSet (noise->mins, -8, -8, -8);
VectorSet (noise->maxs, 8, 8, 8);
noise->owner = who;
noise->svflags = SVF_NOCLIENT;
return noise;
}
static void
PlayerNoise_Verify(edict_t *who)
{
edict_t *e;
edict_t *n1;
edict_t *n2;
if (!who)
{
return;
}
n1 = who->mynoise;
n2 = who->mynoise2;
if (n1 && !n1->inuse)
{
n1 = NULL;
}
if (n2 && !n2->inuse)
{
n2 = NULL;
}
if (n1 && n2)
{
return;
}
for (e = g_edicts + 1 + game.maxclients; e < &g_edicts[globals.num_edicts]; e++)
{
if (!e->inuse || strcmp(e->classname, "player_noise") != 0)
{
continue;
}
if (e->owner && e->owner != who)
{
continue;
}
e->owner = who;
if (!n2 && (e->spawnflags == PLAYER_NOISE_IMPACT || n1))
{
n2 = e;
}
else
{
n1 = e;
}
if (n1 && n2)
{
break;
}
}
if (!n1)
{
n1 = PlayerNoise_Spawn(who, PLAYER_NOISE_SELF);
}
if (!n2)
{
n2 = PlayerNoise_Spawn(who, PLAYER_NOISE_IMPACT);
}
who->mynoise = n1;
who->mynoise2 = n2;
}
void void
PlayerNoise(edict_t *who, vec3_t where, int type) PlayerNoise(edict_t *who, vec3_t where, int type)
{ {
edict_t *noise; edict_t *noise;
if (!who) if (!who || !who->client)
{ {
return; return;
} }
@ -119,24 +221,7 @@ PlayerNoise(edict_t *who, vec3_t where, int type)
return; return;
} }
if (!who->mynoise) PlayerNoise_Verify(who);
{
noise = G_Spawn();
noise->classname = "player_noise";
VectorSet(noise->mins, -8, -8, -8);
VectorSet(noise->maxs, 8, 8, 8);
noise->owner = who;
noise->svflags = SVF_NOCLIENT;
who->mynoise = noise;
noise = G_Spawn();
noise->classname = "player_noise";
VectorSet(noise->mins, -8, -8, -8);
VectorSet(noise->maxs, 8, 8, 8);
noise->owner = who;
noise->svflags = SVF_NOCLIENT;
who->mynoise2 = noise;
}
if ((type == PNOISE_SELF) || (type == PNOISE_WEAPON)) if ((type == PNOISE_SELF) || (type == PNOISE_WEAPON))
{ {
@ -145,6 +230,11 @@ PlayerNoise(edict_t *who, vec3_t where, int type)
return; return;
} }
if (!who->mynoise)
{
return;
}
noise = who->mynoise; noise = who->mynoise;
level.sound_entity = noise; level.sound_entity = noise;
level.sound_entity_framenum = level.framenum; level.sound_entity_framenum = level.framenum;
@ -156,6 +246,11 @@ PlayerNoise(edict_t *who, vec3_t where, int type)
return; return;
} }
if (!who->mynoise2)
{
return;
}
noise = who->mynoise2; noise = who->mynoise2;
level.sound2_entity = noise; level.sound2_entity = noise;
level.sound2_entity_framenum = level.framenum; level.sound2_entity_framenum = level.framenum;