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"
|
|
|
|
|
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);
|
2015-11-18 19:38:29 +01:00
|
|
|
static void evade(void);
|
2015-10-20 13:51:49 +02:00
|
|
|
static void nextAction(void);
|
|
|
|
static void findTarget(void);
|
|
|
|
static int hasClearShot(void);
|
2015-11-18 19:38:29 +01:00
|
|
|
static void fallback(void);
|
2015-10-20 13:51:49 +02:00
|
|
|
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-11-11 07:46:58 +01:00
|
|
|
static int nearExtractionPoint(void);
|
2015-11-18 12:27:05 +01:00
|
|
|
static void moveToExtractionPoint(void);
|
2015-11-11 07:46:58 +01:00
|
|
|
static int nearEnemies(void);
|
2015-11-18 12:27:05 +01:00
|
|
|
static int nearItems(void);
|
|
|
|
static void moveToItem(void);
|
|
|
|
static int nearTowableCraft(void);
|
|
|
|
static void moveToTowableCraft(void);
|
2015-11-11 07:46:58 +01:00
|
|
|
static void lookForPlayer(void);
|
2015-11-13 09:45:50 +01:00
|
|
|
static void fleeEnemies(void);
|
2015-11-21 18:37:23 +01:00
|
|
|
static int isRetreating(void);
|
2015-11-15 14:26:34 +01:00
|
|
|
static int getActionChance(int type);
|
|
|
|
static void doFighterAI(void);
|
2015-10-20 13:51:49 +02:00
|
|
|
|
|
|
|
void doAI(void)
|
2015-11-15 14:26:34 +01:00
|
|
|
{
|
2015-11-21 18:37:23 +01:00
|
|
|
if ((self->aiFlags & AIF_AVOIDS_COMBAT) && nearEnemies())
|
2015-11-15 14:26:34 +01:00
|
|
|
{
|
2015-11-18 12:27:05 +01:00
|
|
|
return;
|
2015-11-15 14:26:34 +01:00
|
|
|
}
|
2015-11-18 12:27:05 +01:00
|
|
|
|
2015-11-22 00:27:43 +01:00
|
|
|
if ((self->aiFlags & AIF_DEFENSIVE) && rand() % 50 && nearEnemies())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-21 18:37:23 +01:00
|
|
|
if ((self->aiFlags & AIF_GOAL_EXTRACTION) && nearExtractionPoint())
|
2015-11-18 12:27:05 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((self->aiFlags & AIF_COLLECTS_ITEMS) && nearItems())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-26 23:48:22 +01:00
|
|
|
if (self->aiFlags & AIF_TOWS)
|
2015-11-18 12:27:05 +01:00
|
|
|
{
|
2015-11-26 23:48:22 +01:00
|
|
|
if (!self->towing && nearTowableCraft())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-11-18 12:27:05 +01:00
|
|
|
}
|
|
|
|
|
2015-11-21 18:37:23 +01:00
|
|
|
if ((self->aiFlags & AIF_RETREATS) && (battle.stats[STAT_TIME] % 60 == 0) && isRetreating())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-18 12:27:05 +01:00
|
|
|
if (!(self->aiFlags & AIF_AVOIDS_COMBAT))
|
2015-11-15 14:26:34 +01:00
|
|
|
{
|
|
|
|
doFighterAI();
|
2015-11-18 12:27:05 +01:00
|
|
|
return;
|
2015-11-15 14:26:34 +01:00
|
|
|
}
|
2015-11-18 12:27:05 +01:00
|
|
|
|
2015-11-28 15:33:05 +01:00
|
|
|
if (self->aiFlags & (AIF_FOLLOWS_PLAYER|AIF_MOVES_TO_PLAYER))
|
2015-11-18 12:27:05 +01:00
|
|
|
{
|
|
|
|
lookForPlayer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no idea - just stay where you are */
|
|
|
|
applyFighterBrakes();
|
2015-11-15 14:26:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void doFighterAI(void)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
int r;
|
2015-11-01 12:37:12 +01:00
|
|
|
|
2015-11-14 14:03:57 +01:00
|
|
|
/* don't hold a grudge against current target */
|
2015-11-20 23:52:48 +01:00
|
|
|
if ((self->target != NULL && self->target->health <= 0) || rand() % 5 == 0)
|
2015-11-01 12:37:12 +01:00
|
|
|
{
|
2015-11-15 14:26:34 +01:00
|
|
|
self->action = doAI;
|
2015-11-01 12:37:12 +01:00
|
|
|
self->target = NULL;
|
|
|
|
}
|
2015-10-29 17:18:41 +01:00
|
|
|
|
2015-11-14 14:03:57 +01:00
|
|
|
if (!self->target || self->target->systemPower <= 0)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
findTarget();
|
|
|
|
|
|
|
|
if (self->target == NULL)
|
|
|
|
{
|
2015-11-28 15:33:05 +01:00
|
|
|
if (self->aiFlags & AIF_MOVES_TO_PLAYER && player != NULL)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
moveToPlayer();
|
|
|
|
}
|
2015-11-18 12:27:05 +01:00
|
|
|
else
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 19:38:29 +01:00
|
|
|
/* don't start dodging, etc., if you're far from your target */
|
|
|
|
if (getDistance(self->x, self->y, self->target->x, self->target->y) > SCREEN_WIDTH)
|
|
|
|
{
|
|
|
|
self->action = huntTarget;
|
|
|
|
self->aiActionTime = FPS * 2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-20 13:51:49 +02:00
|
|
|
r = rand() % 100;
|
|
|
|
|
2015-11-18 19:38:29 +01:00
|
|
|
if (r <= getActionChance(AI_EVADE))
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
2015-11-22 12:42:28 +01:00
|
|
|
self->targetLocation.x = self->target->x + (rand() % 250 - rand() % 250);
|
|
|
|
self->targetLocation.y = self->target->y + (rand() % 250 - rand() % 250);
|
2015-11-18 19:38:29 +01:00
|
|
|
self->action = evade;
|
2015-11-20 23:52:48 +01:00
|
|
|
self->aiActionTime = FPS;
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|
2015-11-18 19:38:29 +01:00
|
|
|
else if (r <= getActionChance(AI_FALLBACK))
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
2015-11-18 19:38:29 +01:00
|
|
|
self->action = fallback;
|
2015-11-19 09:30:45 +01:00
|
|
|
self->aiActionTime = FPS * 2;
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|
2015-11-15 14:26:34 +01:00
|
|
|
else if (r <= getActionChance(AI_HUNT))
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
self->action = huntTarget;
|
|
|
|
self->aiActionTime = FPS * 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self->action = huntAndAttackTarget;
|
2015-11-19 09:30:45 +01:00
|
|
|
self->aiActionTime = FPS * 3;
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-15 14:26:34 +01:00
|
|
|
static int getActionChance(int type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2015-11-18 19:38:29 +01:00
|
|
|
case AI_EVADE:
|
2015-11-22 12:42:28 +01:00
|
|
|
return 25 - (self->aiAggression * 4);
|
2015-11-15 14:26:34 +01:00
|
|
|
|
2015-11-18 19:38:29 +01:00
|
|
|
case AI_FALLBACK:
|
2015-11-22 12:42:28 +01:00
|
|
|
return 55 - (self->aiAggression * 4);
|
2015-11-15 14:26:34 +01:00
|
|
|
|
|
|
|
case AI_HUNT:
|
2015-11-22 12:42:28 +01:00
|
|
|
return 85 - (self->aiAggression * 4);
|
2015-11-15 14:26:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
applyFighterThrust();
|
|
|
|
}
|
|
|
|
|
|
|
|
nextAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void findTarget(void)
|
|
|
|
{
|
2015-11-14 14:03:57 +01:00
|
|
|
int i;
|
|
|
|
Entity *e, **candidates;
|
2015-11-01 11:46:49 +01:00
|
|
|
unsigned int dist, closest;
|
|
|
|
|
2015-11-18 12:27:05 +01:00
|
|
|
dist = closest = (!battle.epic || (!(self->aiFlags & AIF_UNLIMITED_RANGE))) ? 2000 : MAX_TARGET_RANGE;
|
2015-10-20 13:51:49 +02:00
|
|
|
|
2015-11-15 13:28:12 +01:00
|
|
|
candidates = getAllEntsWithin(self->x - (self->w / 2) - (dist / 2), self->y - (self->h / 2) - (dist / 2), self->w + dist, self->h + dist, self);
|
2015-11-14 14:03:57 +01:00
|
|
|
|
2015-10-23 23:44:39 +02:00
|
|
|
self->target = NULL;
|
|
|
|
|
2015-11-14 14:03:57 +01:00
|
|
|
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
2015-11-14 14:03:57 +01:00
|
|
|
if (e->active && e->type == ET_FIGHTER && e->side != self->side && e->health > 0 && canAttack(e))
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
2015-11-14 14:03:57 +01:00
|
|
|
dist = getDistance(self->x, self->y, e->x, e->y);
|
2015-11-13 09:45:50 +01:00
|
|
|
|
2015-10-20 13:51:49 +02:00
|
|
|
if (dist < closest)
|
|
|
|
{
|
2015-11-18 12:27:05 +01:00
|
|
|
if (self->target == NULL || ((self->target->aiFlags & AIF_AVOIDS_COMBAT) == 0) || ((self->target->aiFlags & AIF_AVOIDS_COMBAT) && rand() % 10) == 0)
|
2015-11-13 09:45:50 +01:00
|
|
|
{
|
2015-11-14 14:03:57 +01:00
|
|
|
self->target = e;
|
2015-11-13 09:45:50 +01:00
|
|
|
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-11-16 17:24:57 +01:00
|
|
|
if (f->flags & EF_MUST_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);
|
|
|
|
}
|
|
|
|
|
2015-11-18 19:38:29 +01:00
|
|
|
if (f->shield > 0)
|
|
|
|
{
|
|
|
|
selectWeapon(BT_LASER);
|
|
|
|
}
|
|
|
|
|
2015-10-23 23:44:39 +02:00
|
|
|
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 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-11-22 09:17:29 +01:00
|
|
|
if (!(self->aiFlags & AIF_MISSILE_BOAT))
|
2015-10-30 22:55:01 +01:00
|
|
|
{
|
2015-11-22 09:17:29 +01:00
|
|
|
/* force weapon selection, otherwise we'll keep using lasers / mag */
|
|
|
|
canAttack(self->target);
|
|
|
|
|
|
|
|
if (self->guns[0].type && (self->missiles == 0 || rand() % 50 > 0))
|
|
|
|
{
|
|
|
|
fireGuns(self);
|
|
|
|
}
|
2015-11-28 16:30:07 +01:00
|
|
|
else if (self->missiles && getDistance(self->x, self->y, self->target->x, self->target->y) >= 350)
|
2015-11-22 09:17:29 +01:00
|
|
|
{
|
|
|
|
fireMissile(self);
|
2015-11-28 16:30:07 +01:00
|
|
|
|
|
|
|
self->action = doAI;
|
2015-11-22 09:17:29 +01:00
|
|
|
}
|
2015-10-30 22:55:01 +01:00
|
|
|
}
|
2015-11-22 09:17:29 +01:00
|
|
|
else
|
2015-10-30 22:55:01 +01:00
|
|
|
{
|
2015-11-22 09:17:29 +01:00
|
|
|
fireRocket(self);
|
|
|
|
|
|
|
|
self->reload = FPS;
|
|
|
|
|
|
|
|
/* don't constantly fire rockets like normal guns */
|
|
|
|
if (rand() % 5)
|
|
|
|
{
|
|
|
|
self->action = doAI;
|
|
|
|
}
|
2015-10-30 22:55:01 +01:00
|
|
|
}
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 19:38:29 +01:00
|
|
|
static void turnAndFly(int wantedAngle)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
int dir;
|
|
|
|
|
|
|
|
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-11-18 19:38:29 +01:00
|
|
|
static void evade(void)
|
|
|
|
{
|
2015-11-20 23:52:48 +01:00
|
|
|
int wantedAngle = getAngle(self->x, self->y, self->targetLocation.x, self->targetLocation.y);
|
2015-11-18 19:38:29 +01:00
|
|
|
|
|
|
|
turnAndFly(wantedAngle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fallback(void)
|
|
|
|
{
|
|
|
|
int wantedAngle = 180 + getAngle(self->x, self->y, self->target->x, self->target->y);
|
|
|
|
|
|
|
|
turnAndFly(wantedAngle);
|
|
|
|
}
|
|
|
|
|
2015-11-15 14:26:34 +01:00
|
|
|
static void nextAction(void)
|
|
|
|
{
|
|
|
|
if (--self->aiActionTime <= 0)
|
|
|
|
{
|
|
|
|
self->action = doAI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 18:37:23 +01:00
|
|
|
static int isRetreating(void)
|
|
|
|
{
|
2015-11-22 14:06:19 +01:00
|
|
|
float chance;
|
|
|
|
|
2015-11-21 18:37:23 +01:00
|
|
|
if (!(self->flags & EF_RETREATING))
|
|
|
|
{
|
2015-11-22 14:06:19 +01:00
|
|
|
if (battle.numInitialEnemies > 0)
|
2015-11-21 18:37:23 +01:00
|
|
|
{
|
2015-11-22 14:06:19 +01:00
|
|
|
chance = battle.numEnemies;
|
|
|
|
chance /= battle.numInitialEnemies;
|
|
|
|
chance *= 256;
|
2015-11-21 18:37:23 +01:00
|
|
|
|
2015-11-22 14:06:19 +01:00
|
|
|
if (battle.numEnemies > 0 && rand() % 100 > chance)
|
|
|
|
{
|
|
|
|
self->flags |= EF_RETREATING;
|
|
|
|
|
|
|
|
self->aiFlags |= AIF_AVOIDS_COMBAT;
|
|
|
|
self->aiFlags |= AIF_UNLIMITED_RANGE;
|
|
|
|
self->aiFlags |= AIF_GOAL_EXTRACTION;
|
|
|
|
|
|
|
|
addHudMessage(colors.red, "%s is retreating!", self->name);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2015-11-21 18:37:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self->flags & EF_RETREATING;
|
|
|
|
}
|
|
|
|
|
2015-11-15 14:26:34 +01:00
|
|
|
static int nearEnemies(void)
|
|
|
|
{
|
|
|
|
int i, numEnemies;
|
|
|
|
Entity *e, **candidates;
|
|
|
|
|
2015-11-21 18:37:23 +01:00
|
|
|
candidates = getAllEntsWithin(self->x - (self->w / 2) - 500, self->y - (self->h / 2) - 500, 1000, 1000, self);
|
2015-11-15 14:26:34 +01:00
|
|
|
|
|
|
|
self->target = NULL;
|
|
|
|
self->targetLocation.x = self->targetLocation.y = 0;
|
|
|
|
|
|
|
|
numEnemies = 0;
|
|
|
|
|
|
|
|
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
|
|
|
|
{
|
2015-11-26 22:53:54 +01:00
|
|
|
if (e->type == ET_FIGHTER && e->side != self->side && !(e->flags & EF_DISABLED))
|
2015-11-15 14:26:34 +01:00
|
|
|
{
|
|
|
|
self->targetLocation.x += e->x;
|
|
|
|
self->targetLocation.y += e->y;
|
|
|
|
numEnemies++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numEnemies)
|
|
|
|
{
|
|
|
|
self->targetLocation.x /= numEnemies;
|
|
|
|
self->targetLocation.y /= numEnemies;
|
|
|
|
self->action = fleeEnemies;
|
|
|
|
self->aiActionTime = FPS / 2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fleeEnemies(void)
|
2015-10-29 17:18:41 +01:00
|
|
|
{
|
2015-11-15 14:26:34 +01:00
|
|
|
int wantedAngle = 180 + getAngle(self->x, self->y, self->targetLocation.x, self->targetLocation.y);
|
2015-10-29 17:18:41 +01:00
|
|
|
|
2015-11-18 19:38:29 +01:00
|
|
|
turnAndFly(wantedAngle);
|
2015-11-21 18:37:23 +01:00
|
|
|
|
|
|
|
nextAction();
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-11-18 12:27:05 +01:00
|
|
|
static int nearExtractionPoint(void)
|
|
|
|
{
|
2015-11-20 23:52:48 +01:00
|
|
|
int dist;
|
2015-11-18 12:27:05 +01:00
|
|
|
|
|
|
|
self->target = NULL;
|
|
|
|
|
2015-11-20 23:52:48 +01:00
|
|
|
if (battle.extractionPoint)
|
2015-11-18 12:27:05 +01:00
|
|
|
{
|
2015-11-20 23:52:48 +01:00
|
|
|
dist = getDistance(self->x, self->y, battle.extractionPoint->x, battle.extractionPoint->y);
|
|
|
|
|
|
|
|
if (dist <= 2000 || self->aiFlags & AIF_UNLIMITED_RANGE)
|
2015-11-18 12:27:05 +01:00
|
|
|
{
|
2015-11-20 23:52:48 +01:00
|
|
|
self->target = battle.extractionPoint;
|
|
|
|
self->action = moveToExtractionPoint;
|
2015-11-27 07:36:48 +01:00
|
|
|
self->aiActionTime = (!self->towing) ? FPS / 2 : FPS * 2;
|
2015-11-18 12:27:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self->target != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void moveToExtractionPoint(void)
|
|
|
|
{
|
|
|
|
faceTarget(self->target);
|
|
|
|
|
|
|
|
applyFighterThrust();
|
2015-11-21 18:37:23 +01:00
|
|
|
|
|
|
|
nextAction();
|
2015-11-18 12:27:05 +01:00
|
|
|
}
|
2015-11-11 07:46:58 +01:00
|
|
|
|
2015-11-18 12:27:05 +01:00
|
|
|
static int nearItems(void)
|
2015-11-11 07:46:58 +01:00
|
|
|
{
|
2015-11-18 12:27:05 +01:00
|
|
|
int i;
|
|
|
|
long closest, distance;
|
|
|
|
Entity *e, **candidates;
|
|
|
|
|
|
|
|
closest = MAX_TARGET_RANGE;
|
|
|
|
|
|
|
|
candidates = getAllEntsWithin(self->x - (self->w / 2) - (GRID_CELL_WIDTH / 2), self->y - (self->h / 2) - (GRID_CELL_HEIGHT / 2), GRID_CELL_WIDTH, GRID_CELL_HEIGHT, self);
|
|
|
|
|
|
|
|
self->target = NULL;
|
|
|
|
|
|
|
|
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
|
2015-11-11 07:46:58 +01:00
|
|
|
{
|
2015-11-18 12:27:05 +01:00
|
|
|
if (e->type == ET_ITEM)
|
|
|
|
{
|
|
|
|
distance = getDistance(self->x, self->y, e->x, e->y);
|
|
|
|
|
|
|
|
if (distance < closest)
|
|
|
|
{
|
|
|
|
self->target = e;
|
|
|
|
closest = distance;
|
|
|
|
}
|
|
|
|
}
|
2015-11-11 07:46:58 +01:00
|
|
|
}
|
2015-11-18 12:27:05 +01:00
|
|
|
|
|
|
|
if (self->target != NULL)
|
|
|
|
{
|
|
|
|
self->action = moveToItem;
|
2015-11-21 18:37:23 +01:00
|
|
|
self->aiActionTime = FPS / 2;
|
2015-11-18 12:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self->target != NULL;
|
2015-11-11 07:46:58 +01:00
|
|
|
}
|
|
|
|
|
2015-11-18 12:27:05 +01:00
|
|
|
static void moveToItem(void)
|
|
|
|
{
|
|
|
|
if (self->target->alive == ALIVE_ALIVE)
|
|
|
|
{
|
|
|
|
faceTarget(self->target);
|
|
|
|
applyFighterThrust();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->target = NULL;
|
|
|
|
self->action = doAI;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nearTowableCraft(void)
|
2015-11-11 07:46:58 +01:00
|
|
|
{
|
|
|
|
int i;
|
2015-11-18 12:27:05 +01:00
|
|
|
long closest, distance;
|
2015-11-11 20:15:41 +01:00
|
|
|
Entity *e, **candidates;
|
2015-11-11 07:46:58 +01:00
|
|
|
|
2015-11-18 12:27:05 +01:00
|
|
|
candidates = getAllEntsWithin(self->x - (self->w / 2) - (GRID_CELL_WIDTH / 2), self->y - (self->h / 2) - (GRID_CELL_HEIGHT / 2), GRID_CELL_WIDTH, GRID_CELL_HEIGHT, self);
|
2015-11-11 07:46:58 +01:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2015-11-26 23:48:22 +01:00
|
|
|
if (e->type == ET_FIGHTER && (e->flags & (EF_DISABLED|EF_MISSION_TARGET)) == (EF_DISABLED|EF_MISSION_TARGET))
|
2015-11-11 07:46:58 +01:00
|
|
|
{
|
2015-11-18 12:27:05 +01:00
|
|
|
distance = getDistance(self->x, self->y, e->x, e->y);
|
|
|
|
|
|
|
|
if (distance < closest)
|
|
|
|
{
|
|
|
|
self->target = e;
|
|
|
|
closest = distance;
|
|
|
|
}
|
2015-11-11 07:46:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->target != NULL)
|
|
|
|
{
|
2015-11-18 12:27:05 +01:00
|
|
|
self->action = moveToTowableCraft;
|
2015-11-21 18:37:23 +01:00
|
|
|
self->aiActionTime = FPS / 2;
|
2015-11-11 07:46:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self->target != NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-18 12:27:05 +01:00
|
|
|
static void moveToTowableCraft(void)
|
2015-11-13 09:45:50 +01:00
|
|
|
{
|
|
|
|
faceTarget(self->target);
|
|
|
|
|
|
|
|
applyFighterThrust();
|
2015-11-26 23:48:22 +01:00
|
|
|
|
|
|
|
nextAction();
|
2015-11-13 09:45:50 +01:00
|
|
|
}
|
|
|
|
|
2015-11-11 07:46:58 +01:00
|
|
|
static void lookForPlayer(void)
|
|
|
|
{
|
2015-11-28 15:33:05 +01:00
|
|
|
int range = (self->aiFlags & AIF_MOVES_TO_PLAYER) ? MAX_TARGET_RANGE : 2000;
|
2015-11-18 12:27:05 +01:00
|
|
|
|
|
|
|
if (player != NULL && getDistance(self->x, self->y, player->x, player->y) < range)
|
2015-11-11 07:46:58 +01:00
|
|
|
{
|
|
|
|
moveToPlayer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
applyFighterBrakes();
|
|
|
|
}
|