From 2a725471c780b976611a746ff7a24a2a59f694c0 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 22 May 2016 16:50:11 +0100 Subject: [PATCH] If at edge of battle area when fleeing, turn around. --- src/battle/ai.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/battle/ai.c b/src/battle/ai.c index 60c61f9..4e3013b 100644 --- a/src/battle/ai.c +++ b/src/battle/ai.c @@ -55,6 +55,7 @@ static int selectWeaponForTarget(Entity *e); static void deployMine(void); static int isSurrendering(void); static void doSurrender(void); +static void fleeWithinBattleArea(void); void doAI(void) { @@ -680,15 +681,35 @@ static int nearEnemies(void) self->targetLocation.x += (rand() % 100 - rand() % 100); self->targetLocation.y += (rand() % 100 - rand() % 100); - self->action = fleeEnemies; self->aiActionTime = FPS * 2; + fleeWithinBattleArea(); + + self->action = fleeEnemies; + return 1; } return 0; } +static void fleeWithinBattleArea(void) +{ + /* at the limit of the battle area, try somewhere else */ + if (self->targetLocation.x < SCREEN_WIDTH || self->targetLocation.x >= BATTLE_AREA_WIDTH - SCREEN_WIDTH) + { + self->targetLocation.x = -self->targetLocation.x; + self->aiActionTime = FPS * 5; + } + + /* at the limit of the battle area, try somewhere else */ + if (self->targetLocation.y < SCREEN_HEIGHT || self->targetLocation.y >= BATTLE_AREA_HEIGHT - SCREEN_HEIGHT) + { + self->targetLocation.y = -self->targetLocation.y; + self->aiActionTime = FPS * 5; + } +} + static void deployMine(void) { Entity *mine; @@ -739,8 +760,11 @@ static int nearMines(void) self->targetLocation.y += (rand() % 100 - rand() % 100); self->action = fleeEnemies; + self->aiActionTime = FPS * 2; + fleeWithinBattleArea(); + return 1; }