2015-10-20 13:51:49 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2015 Parallel Realities
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ai.h"
|
|
|
|
|
|
|
|
static int aggression[][5] =
|
|
|
|
{
|
|
|
|
{25, 35, 35, 40, 50},
|
|
|
|
{20, 30, 30, 35, 40},
|
|
|
|
{15, 20, 25, 30, 35},
|
|
|
|
{10, 15, 20, 25, 30},
|
|
|
|
{5, 10, 15, 20, 25}
|
|
|
|
};
|
|
|
|
|
2015-10-26 20:16:12 +01:00
|
|
|
static void faceTarget(Entity *f);
|
|
|
|
static int isInFOV(Entity *f, int fov);
|
2015-10-20 13:51:49 +02:00
|
|
|
static void preAttack(void);
|
|
|
|
static void huntTarget(void);
|
|
|
|
static void huntAndAttackTarget(void);
|
|
|
|
static void flyStraight(void);
|
|
|
|
static void dodge(void);
|
|
|
|
static void nextAction(void);
|
|
|
|
static void findTarget(void);
|
|
|
|
static int hasClearShot(void);
|
|
|
|
static void boost(void);
|
|
|
|
static void slow(void);
|
|
|
|
static int targetOutOfRange(void);
|
|
|
|
static void moveToPlayer(void);
|
2015-10-26 20:16:12 +01:00
|
|
|
static int canAttack(Entity *f);
|
2015-10-23 23:44:39 +02:00
|
|
|
static int selectWeapon(int type);
|
2015-10-29 17:18:41 +01:00
|
|
|
static void flee(void);
|
2015-11-11 07:46:58 +01:00
|
|
|
static int nearExtractionPoint(void);
|
|
|
|
static int nearEnemies(void);
|
|
|
|
static void lookForPlayer(void);
|
2015-11-13 09:45:50 +01:00
|
|
|
static void fleeEnemies(void);
|
2015-11-11 07:46:58 +01:00
|
|
|
static void moveToExtractionPoint(void);
|
2015-10-20 13:51:49 +02:00
|
|
|
|
|
|
|
void doAI(void)
|
|
|
|
{
|
|
|
|
int r;
|
2015-11-01 12:37:12 +01:00
|
|
|
|
|
|
|
if (self->target != NULL && self->target->health <= 0)
|
|
|
|
{
|
|
|
|
self->action = self->defaultAction;
|
|
|
|
self->target = NULL;
|
|
|
|
}
|
2015-10-29 17:18:41 +01:00
|
|
|
|
2015-11-13 12:27:11 +01:00
|
|
|
if (!self->target || targetOutOfRange() || self->target->systemPower <= 0 || (self->target->flags & EF_CIVILIAN))
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
findTarget();
|
|
|
|
|
|
|
|
if (self->target == NULL)
|
|
|
|
{
|
2015-10-31 09:03:11 +01:00
|
|
|
if (player != NULL && self->side == SIDE_ALLIES)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
moveToPlayer();
|
|
|
|
}
|
2015-10-29 17:18:41 +01:00
|
|
|
else if (!(self->flags & EF_FLEEING))
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
applyFighterBrakes();
|
|
|
|
}
|
2015-10-29 17:18:41 +01:00
|
|
|
|
2015-10-20 13:51:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = rand() % 100;
|
|
|
|
|
|
|
|
if (r <= aggression[self->aggression][0])
|
|
|
|
{
|
|
|
|
self->action = dodge;
|
|
|
|
self->aiActionTime = FPS * 2;
|
|
|
|
}
|
|
|
|
else if (r <= aggression[self->aggression][1])
|
|
|
|
{
|
|
|
|
self->action = boost;
|
|
|
|
self->aiActionTime = FPS * 1;
|
|
|
|
}
|
|
|
|
else if (r <= aggression[self->aggression][2])
|
|
|
|
{
|
|
|
|
self->action = slow;
|
|
|
|
self->aiActionTime = FPS * 1;
|
|
|
|
}
|
|
|
|
else if (r <= aggression[self->aggression][3])
|
|
|
|
{
|
|
|
|
self->action = flyStraight;
|
|
|
|
self->aiActionTime = FPS * 1;
|
|
|
|
}
|
|
|
|
else if (r <= aggression[self->aggression][4])
|
|
|
|
{
|
|
|
|
self->action = huntTarget;
|
|
|
|
self->aiActionTime = FPS * 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self->action = huntAndAttackTarget;
|
|
|
|
self->aiActionTime = FPS * 1;
|
|
|
|
}
|
2015-10-29 17:18:41 +01:00
|
|
|
|
|
|
|
if (player != NULL && battle.numEnemies <= 2 && self->flags & EF_FLEES)
|
|
|
|
{
|
|
|
|
self->action = flee;
|
|
|
|
self->aiActionTime = FPS * 3;
|
2015-10-31 09:03:11 +01:00
|
|
|
if (!(self->flags & EF_FLEEING) && (self->flags & EF_MISSION_TARGET) && self->side != SIDE_ALLIES)
|
2015-10-29 17:18:41 +01:00
|
|
|
{
|
|
|
|
addHudMessage(colors.cyan, "Mission target is escaping!");
|
|
|
|
self->flags |= EF_FLEEING;
|
|
|
|
}
|
|
|
|
}
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 11:46:49 +01:00
|
|
|
/*
|
|
|
|
* Don't chase your target forever
|
|
|
|
*/
|
2015-10-20 13:51:49 +02:00
|
|
|
static int targetOutOfRange(void)
|
|
|
|
{
|
2015-11-01 11:46:49 +01:00
|
|
|
return getDistance(self->x, self->y, self->target->x, self->target->y) > SCREEN_HEIGHT;
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void huntTarget(void)
|
|
|
|
{
|
|
|
|
faceTarget(self->target);
|
|
|
|
|
|
|
|
applyFighterThrust();
|
|
|
|
|
|
|
|
nextAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void huntAndAttackTarget(void)
|
|
|
|
{
|
|
|
|
int dist = getDistance(self->x, self->y, self->target->x, self->target->y);
|
|
|
|
|
|
|
|
faceTarget(self->target);
|
|
|
|
|
|
|
|
if (dist <= 500 && hasClearShot())
|
|
|
|
{
|
|
|
|
preAttack();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dist <= 250)
|
|
|
|
{
|
|
|
|
applyFighterBrakes();
|
|
|
|
self->aiActionTime = MIN(FPS, self->aiActionTime);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
applyFighterThrust();
|
|
|
|
}
|
|
|
|
|
|
|
|
nextAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void findTarget(void)
|
|
|
|
{
|
2015-10-26 20:16:12 +01:00
|
|
|
Entity *f;
|
2015-11-01 11:46:49 +01:00
|
|
|
unsigned int dist, closest;
|
|
|
|
|
|
|
|
dist = closest = (!battle.epic) ? 2000 : MAX_TARGET_RANGE;
|
2015-10-20 13:51:49 +02:00
|
|
|
|
2015-10-23 23:44:39 +02:00
|
|
|
self->target = NULL;
|
|
|
|
|
2015-10-26 20:16:12 +01:00
|
|
|
for (f = battle.entityHead.next ; f != NULL ; f = f->next)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
2015-10-28 20:12:58 +01:00
|
|
|
if (f->active && f->type == ET_FIGHTER && f->side != self->side && f->health > 0 && canAttack(f))
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
dist = getDistance(self->x, self->y, f->x, f->y);
|
2015-11-13 09:45:50 +01:00
|
|
|
|
2015-10-20 13:51:49 +02:00
|
|
|
if (dist < closest)
|
|
|
|
{
|
2015-11-13 09:45:50 +01:00
|
|
|
if (self->target == NULL || ((self->target->flags & EF_CIVILIAN) == 0) || ((self->target->flags & EF_CIVILIAN) && rand() % 10) == 0)
|
|
|
|
{
|
|
|
|
self->target = f;
|
|
|
|
closest = dist;
|
|
|
|
}
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 20:16:12 +01:00
|
|
|
static int canAttack(Entity *f)
|
2015-10-23 23:44:39 +02:00
|
|
|
{
|
|
|
|
self->selectedGunType = self->guns[0].type;
|
|
|
|
|
2015-10-26 20:16:12 +01:00
|
|
|
if (f->flags & EF_DISABLE)
|
2015-10-23 23:44:39 +02:00
|
|
|
{
|
|
|
|
if (f->systemPower > 0)
|
|
|
|
{
|
|
|
|
return selectWeapon(BT_MAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-26 20:16:12 +01:00
|
|
|
if (f->flags & EF_NO_KILL)
|
2015-10-23 23:44:39 +02:00
|
|
|
{
|
|
|
|
return selectWeapon(BT_LASER) || selectWeapon(BT_MAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int selectWeapon(int type)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0 ; i < MAX_FIGHTER_GUNS ; i++)
|
|
|
|
{
|
|
|
|
if (self->guns[i].type == type)
|
|
|
|
{
|
|
|
|
self->selectedGunType = type;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-26 20:16:12 +01:00
|
|
|
static void faceTarget(Entity *f)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
int dir;
|
|
|
|
int wantedAngle = getAngle(self->x, self->y, f->x, f->y);
|
|
|
|
|
|
|
|
wantedAngle %= 360;
|
|
|
|
|
|
|
|
if (fabs(wantedAngle - self->angle) > TURN_THRESHOLD)
|
|
|
|
{
|
2015-10-26 20:16:12 +01:00
|
|
|
dir = ((int)(wantedAngle - self->angle + 360)) % 360 > 180 ? -1 : 1;
|
2015-10-20 13:51:49 +02:00
|
|
|
|
|
|
|
self->angle += dir * TURN_SPEED;
|
|
|
|
|
|
|
|
self->angle = mod(self->angle, 360);
|
|
|
|
|
|
|
|
applyFighterBrakes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 20:16:12 +01:00
|
|
|
static int isInFOV(Entity *f, int fov)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
int angle, a, b;
|
|
|
|
|
|
|
|
a = mod(self->angle - fov, 360);
|
|
|
|
b = mod(self->angle + fov, 360);
|
|
|
|
angle = getAngle(self->x, self->y, f->x, f->y);
|
|
|
|
|
|
|
|
return (a < b) ? (a <= angle && angle <= b) : (a <= angle || angle <= b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void boost(void)
|
|
|
|
{
|
|
|
|
self->dx *= 1.001;
|
|
|
|
self->dy *= 1.001;
|
|
|
|
|
|
|
|
nextAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void slow(void)
|
|
|
|
{
|
|
|
|
self->dx *= 0.95;
|
|
|
|
self->dy *= 0.95;
|
|
|
|
|
|
|
|
nextAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hasClearShot(void)
|
|
|
|
{
|
|
|
|
int dist;
|
2015-10-26 20:16:12 +01:00
|
|
|
Entity *f;
|
2015-10-20 13:51:49 +02:00
|
|
|
|
|
|
|
if (isInFOV(self->target, 4))
|
|
|
|
{
|
|
|
|
dist = getDistance(self->x, self->y, self->target->x, self->target->y);
|
|
|
|
|
2015-10-26 20:16:12 +01:00
|
|
|
for (f = battle.entityHead.next ; f != NULL ; f = f->next)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
2015-10-28 20:12:58 +01:00
|
|
|
if (f->active && f != self && f != self->target && (getDistance(self->x, self->y, f->x, f->y) < dist))
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
if (isInFOV(f, 8))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void preAttack(void)
|
|
|
|
{
|
2015-10-30 22:55:01 +01:00
|
|
|
if (!self->reload)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
2015-10-30 23:38:01 +01:00
|
|
|
if (self->guns[0].type && (self->missiles.ammo == 0 || (rand() % 50) > 0))
|
2015-10-30 22:55:01 +01:00
|
|
|
{
|
|
|
|
fireGuns(self);
|
|
|
|
}
|
2015-10-30 23:38:01 +01:00
|
|
|
else if (self->missiles.ammo)
|
2015-10-30 22:55:01 +01:00
|
|
|
{
|
|
|
|
fireMissile(self);
|
|
|
|
}
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flyStraight(void)
|
|
|
|
{
|
|
|
|
applyFighterThrust();
|
|
|
|
|
|
|
|
nextAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dodge(void)
|
|
|
|
{
|
|
|
|
int dir;
|
|
|
|
int wantedAngle = 180 + getAngle(self->x, self->y, self->target->x, self->target->y);
|
|
|
|
|
|
|
|
wantedAngle %= 360;
|
|
|
|
|
|
|
|
if (fabs(wantedAngle - self->angle) > TURN_THRESHOLD)
|
|
|
|
{
|
2015-10-26 20:16:12 +01:00
|
|
|
dir = ((int)(wantedAngle - self->angle + 360)) % 360 > 180 ? -1 : 1;
|
2015-10-20 13:51:49 +02:00
|
|
|
|
|
|
|
self->angle += dir * TURN_SPEED;
|
|
|
|
|
|
|
|
self->angle = mod(self->angle, 360);
|
|
|
|
}
|
|
|
|
|
|
|
|
applyFighterThrust();
|
|
|
|
|
|
|
|
nextAction();
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:18:41 +01:00
|
|
|
static void flee(void)
|
|
|
|
{
|
|
|
|
int dir;
|
|
|
|
int wantedAngle = 180 + getAngle(self->x, self->y, player->x, player->y);
|
|
|
|
|
|
|
|
wantedAngle %= 360;
|
|
|
|
|
|
|
|
if (fabs(wantedAngle - self->angle) > TURN_THRESHOLD)
|
|
|
|
{
|
|
|
|
dir = ((int)(wantedAngle - self->angle + 360)) % 360 > 180 ? -1 : 1;
|
|
|
|
|
|
|
|
self->angle += dir * TURN_SPEED;
|
|
|
|
|
|
|
|
self->angle = mod(self->angle, 360);
|
|
|
|
}
|
|
|
|
|
|
|
|
applyFighterThrust();
|
|
|
|
|
|
|
|
nextAction();
|
|
|
|
}
|
|
|
|
|
2015-10-20 13:51:49 +02:00
|
|
|
static void nextAction(void)
|
|
|
|
{
|
|
|
|
if (--self->aiActionTime <= 0)
|
|
|
|
{
|
|
|
|
self->action = doAI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void moveToPlayer(void)
|
|
|
|
{
|
|
|
|
int dist = getDistance(self->x, self->y, player->x, player->y);
|
|
|
|
|
|
|
|
if (dist <= 250)
|
|
|
|
{
|
|
|
|
applyFighterBrakes();
|
|
|
|
|
|
|
|
self->aiActionTime = MIN(FPS, self->aiActionTime);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
faceTarget(player);
|
|
|
|
|
|
|
|
applyFighterThrust();
|
|
|
|
}
|
|
|
|
}
|
2015-11-11 07:46:58 +01:00
|
|
|
|
|
|
|
/* ====== Civilian AI ======= */
|
|
|
|
|
|
|
|
void doCivilianAI(void)
|
|
|
|
{
|
|
|
|
if (!nearExtractionPoint() && !nearEnemies())
|
|
|
|
{
|
|
|
|
lookForPlayer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nearExtractionPoint(void)
|
|
|
|
{
|
|
|
|
int i;
|
2015-11-11 20:15:41 +01:00
|
|
|
Entity *e, **candidates;
|
2015-11-11 07:46:58 +01:00
|
|
|
|
|
|
|
candidates = getAllEntsWithin(self->x - 500, self->y - 500, 1000, 1000, self);
|
|
|
|
|
|
|
|
self->target = NULL;
|
|
|
|
|
2015-11-14 00:35:51 +01:00
|
|
|
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
|
2015-11-11 07:46:58 +01:00
|
|
|
{
|
|
|
|
if (e->type == ET_EXTRACTION_POINT)
|
|
|
|
{
|
|
|
|
self->target = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->target != NULL)
|
|
|
|
{
|
|
|
|
self->action = moveToExtractionPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self->target != NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-13 09:45:50 +01:00
|
|
|
static void moveToExtractionPoint(void)
|
|
|
|
{
|
|
|
|
faceTarget(self->target);
|
|
|
|
|
|
|
|
applyFighterThrust();
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:46:58 +01:00
|
|
|
static int nearEnemies(void)
|
|
|
|
{
|
2015-11-13 09:45:50 +01:00
|
|
|
int i, numEnemies;
|
|
|
|
Entity *e, **candidates;
|
|
|
|
|
2015-11-13 12:27:11 +01:00
|
|
|
candidates = getAllEntsWithin(self->x - 1000, self->y - 1000, 2000, 2000, self);
|
2015-11-13 09:45:50 +01:00
|
|
|
|
|
|
|
self->target = NULL;
|
|
|
|
self->targetLocation.x = self->targetLocation.y = 0;
|
|
|
|
|
|
|
|
numEnemies = 0;
|
|
|
|
|
2015-11-14 00:35:51 +01:00
|
|
|
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
|
2015-11-13 09:45:50 +01:00
|
|
|
{
|
|
|
|
if (e->type == ET_FIGHTER && e->side != SIDE_ALLIES)
|
|
|
|
{
|
|
|
|
self->targetLocation.x += e->x;
|
|
|
|
self->targetLocation.y += e->y;
|
|
|
|
numEnemies++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numEnemies)
|
|
|
|
{
|
|
|
|
self->targetLocation.x /= numEnemies;
|
|
|
|
self->targetLocation.y /= numEnemies;
|
|
|
|
self->action = fleeEnemies;
|
2015-11-13 17:54:38 +01:00
|
|
|
self->aiActionTime = FPS / 2;
|
2015-11-13 09:45:50 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:46:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-13 09:45:50 +01:00
|
|
|
static void fleeEnemies(void)
|
2015-11-11 07:46:58 +01:00
|
|
|
{
|
2015-11-13 09:45:50 +01:00
|
|
|
int dir;
|
|
|
|
int wantedAngle = 180 + getAngle(self->x, self->y, self->targetLocation.x, self->targetLocation.y);
|
|
|
|
|
|
|
|
wantedAngle %= 360;
|
|
|
|
|
|
|
|
if (fabs(wantedAngle - self->angle) > TURN_THRESHOLD)
|
|
|
|
{
|
|
|
|
dir = ((int)(wantedAngle - self->angle + 360)) % 360 > 180 ? -1 : 1;
|
|
|
|
|
|
|
|
self->angle += dir * TURN_SPEED;
|
2015-11-11 07:46:58 +01:00
|
|
|
|
2015-11-13 09:45:50 +01:00
|
|
|
self->angle = mod(self->angle, 360);
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:46:58 +01:00
|
|
|
applyFighterThrust();
|
2015-11-13 09:45:50 +01:00
|
|
|
|
|
|
|
if (--self->aiActionTime <= 0)
|
|
|
|
{
|
|
|
|
self->action = doCivilianAI;
|
|
|
|
}
|
2015-11-11 07:46:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void lookForPlayer(void)
|
|
|
|
{
|
2015-11-13 09:45:50 +01:00
|
|
|
if (player != NULL && getDistance(self->x, self->y, player->x, player->y) < 1000)
|
2015-11-11 07:46:58 +01:00
|
|
|
{
|
|
|
|
moveToPlayer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
applyFighterBrakes();
|
|
|
|
}
|