From e6ea4bff06a75608361e0e7a6e0d95a0efc1d51f Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 29 Mar 2016 07:09:26 +0100 Subject: [PATCH] Reworked canAttack logic. --- src/battle/ai.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/battle/ai.c b/src/battle/ai.c index 145b48a..11d5e55 100644 --- a/src/battle/ai.c +++ b/src/battle/ai.c @@ -50,6 +50,7 @@ static void doGunAI(void); static void moveToLeader(void); static void wander(void); static void doWander(void); +static int selectWeaponForTarget(Entity *e); void doAI(void) { @@ -302,7 +303,7 @@ static void findTarget(void) for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) { - if (e->active && (e->flags & EF_TAKES_DAMAGE) && (!(e->flags & EF_DISABLED)) && e->side != self->side && e->health > 0 && canAttack(e)) + if (canAttack(e)) { dist = getDistance(self->x, self->y, e->x, e->y); @@ -317,7 +318,15 @@ static void findTarget(void) static int canAttack(Entity *e) { - self->selectedGunType = self->guns[0].type; + if (!e->active || e->side == self->side || e->health <= 0) + { + return 0; + } + + if (!(e->flags & EF_TAKES_DAMAGE)) + { + return 0; + } if (!(e->flags & EF_AI_TARGET)) { @@ -330,6 +339,13 @@ static int canAttack(Entity *e) } } + return selectWeaponForTarget(e); +} + +static int selectWeaponForTarget(Entity *e) +{ + self->selectedGunType = self->guns[0].type; + if (e->flags & EF_MUST_DISABLE) { if (e->systemPower > 0) @@ -437,7 +453,7 @@ static void preAttack(void) if (!(self->aiFlags & AIF_MISSILE_BOAT)) { /* force weapon selection, otherwise we'll keep using lasers / mag */ - canAttack(self->target); + selectWeaponForTarget(self->target); if (self->guns[0].type && (self->missiles == 0 || rand() % 50 > 0)) {