From 80fb945cff4a45892469d6cfa073cab92452e25d Mon Sep 17 00:00:00 2001 From: Yamagi Burmeister Date: Sun, 18 Dec 2016 09:59:46 +0100 Subject: [PATCH] Make sure that a monsters enemy is still alive when deciding to attack. When the monster was already killed by another monster or a coop player some references may be NULL and the game was crashed. This was observed by maraakte, who reported it in issue #164. I've just merged his fix from q2dos. --- src/g_ai.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/g_ai.c b/src/g_ai.c index 797fc46..db806ed 100644 --- a/src/g_ai.c +++ b/src/g_ai.c @@ -200,7 +200,11 @@ ai_charge(edict_t *self, float dist) return; } - VectorSubtract(self->enemy->s.origin, self->s.origin, v); + if(self->enemy) + { + VectorSubtract(self->enemy->s.origin, self->s.origin, v); + } + self->ideal_yaw = vectoyaw(v); M_ChangeYaw(self); @@ -382,7 +386,11 @@ HuntTarget(edict_t *self) self->monsterinfo.run(self); } - VectorSubtract(self->enemy->s.origin, self->s.origin, vec); + if(visible(self, self->enemy)) + { + VectorSubtract(self->enemy->s.origin, self->s.origin, vec); + } + self->ideal_yaw = vectoyaw(vec); /* wait a while before first attack */ @@ -395,7 +403,7 @@ HuntTarget(edict_t *self) void FoundTarget(edict_t *self) { - if (!self) + if (!self|| !self->enemy || !self->enemy->inuse) { return; } @@ -697,7 +705,7 @@ M_CheckAttack(edict_t *self) float chance; trace_t tr; - if (!self) + if (!self || !self->enemy || !self->enemy->inuse) { return false; } @@ -899,8 +907,10 @@ ai_checkattack(edict_t *self, float dist) vec3_t temp; qboolean hesDeadJim; - if (!self) + if (!self || !self->enemy || !self->enemy->inuse) { + enemy_vis = false; + return false; } @@ -1029,10 +1039,13 @@ ai_checkattack(edict_t *self, float dist) } } - enemy_infront = infront(self, self->enemy); - enemy_range = range(self, self->enemy); - VectorSubtract(self->enemy->s.origin, self->s.origin, temp); - enemy_yaw = vectoyaw(temp); + if (self->enemy) + { + enemy_infront = infront(self, self->enemy); + enemy_range = range(self, self->enemy); + VectorSubtract(self->enemy->s.origin, self->s.origin, temp); + enemy_yaw = vectoyaw(temp); + } if (self->monsterinfo.attack_state == AS_MISSILE) { @@ -1073,7 +1086,7 @@ ai_run(edict_t *self, float dist) float left, center, right; vec3_t left_target, right_target; - if (!self) + if (!self || !self->enemy || !self->enemy->inuse) { return; }