Further AI tweaks. Allowed AI to select laser cannons.

This commit is contained in:
Steve 2015-11-18 18:38:29 +00:00
parent 881464282d
commit 5fda0b1ad8
2 changed files with 46 additions and 43 deletions

View File

@ -26,12 +26,12 @@ static void preAttack(void);
static void huntTarget(void); static void huntTarget(void);
static void huntAndAttackTarget(void); static void huntAndAttackTarget(void);
static void flyStraight(void); static void flyStraight(void);
static void dodge(void); static void evade(void);
static void nextAction(void); static void nextAction(void);
static void findTarget(void); static void findTarget(void);
static int hasClearShot(void); static int hasClearShot(void);
static void boost(void); static void boost(void);
static void slow(void); static void fallback(void);
static void moveToPlayer(void); static void moveToPlayer(void);
static int canAttack(Entity *f); static int canAttack(Entity *f);
static int selectWeapon(int type); static int selectWeapon(int type);
@ -115,11 +115,21 @@ static void doFighterAI(void)
} }
} }
/* 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;
}
r = rand() % 100; r = rand() % 100;
if (r <= getActionChance(AI_DODGE)) if (r <= getActionChance(AI_EVADE))
{ {
self->action = dodge; self->targetLocation.x = self->target->x + rand() % 500 - rand() % 500;
self->targetLocation.y = self->target->y + rand() % 500 - rand() % 500;
self->action = evade;
self->aiActionTime = FPS; self->aiActionTime = FPS;
} }
else if (r <= getActionChance(AI_BOOST)) else if (r <= getActionChance(AI_BOOST))
@ -127,9 +137,9 @@ static void doFighterAI(void)
self->action = boost; self->action = boost;
self->aiActionTime = FPS / 2; self->aiActionTime = FPS / 2;
} }
else if (r <= getActionChance(AI_SLOW)) else if (r <= getActionChance(AI_FALLBACK))
{ {
self->action = slow; self->action = fallback;
self->aiActionTime = FPS / 2; self->aiActionTime = FPS / 2;
} }
else if (r <= getActionChance(AI_STRAIGHT)) else if (r <= getActionChance(AI_STRAIGHT))
@ -153,20 +163,20 @@ static int getActionChance(int type)
{ {
switch (type) switch (type)
{ {
case AI_DODGE: case AI_EVADE:
return 40 - (self->aiAggression * 3); return 20 - (self->aiAggression * 3);
case AI_BOOST: case AI_BOOST:
return 50 - (self->aiAggression * 4); return 30 - (self->aiAggression * 4);
case AI_SLOW: case AI_FALLBACK:
return 60 - (self->aiAggression * 5); return 40 - (self->aiAggression * 5);
case AI_STRAIGHT: case AI_STRAIGHT:
return 70 - (self->aiAggression * 6); return 50 - (self->aiAggression * 6);
case AI_HUNT: case AI_HUNT:
return 80 - (self->aiAggression * 7); return 60 - (self->aiAggression * 7);
} }
return 100; return 100;
@ -253,6 +263,11 @@ static int canAttack(Entity *f)
return selectWeapon(BT_LASER) || selectWeapon(BT_MAG); return selectWeapon(BT_LASER) || selectWeapon(BT_MAG);
} }
if (f->shield > 0)
{
selectWeapon(BT_LASER);
}
return 1; return 1;
} }
@ -310,14 +325,6 @@ static void boost(void)
nextAction(); nextAction();
} }
static void slow(void)
{
self->dx *= 0.95;
self->dy *= 0.95;
nextAction();
}
static int hasClearShot(void) static int hasClearShot(void)
{ {
int dist; int dist;
@ -366,10 +373,9 @@ static void flyStraight(void)
nextAction(); nextAction();
} }
static void dodge(void) static void turnAndFly(int wantedAngle)
{ {
int dir; int dir;
int wantedAngle = 180 + getAngle(self->x, self->y, self->target->x, self->target->y);
wantedAngle %= 360; wantedAngle %= 360;
@ -387,6 +393,20 @@ static void dodge(void)
nextAction(); nextAction();
} }
static void evade(void)
{
int wantedAngle = 180 + getAngle(self->x, self->y, self->targetLocation.x, self->targetLocation.y);
turnAndFly(wantedAngle);
}
static void fallback(void)
{
int wantedAngle = 180 + getAngle(self->x, self->y, self->target->x, self->target->y);
turnAndFly(wantedAngle);
}
static void nextAction(void) static void nextAction(void)
{ {
if (--self->aiActionTime <= 0) if (--self->aiActionTime <= 0)
@ -431,26 +451,9 @@ static int nearEnemies(void)
static void fleeEnemies(void) static void fleeEnemies(void)
{ {
int dir;
int wantedAngle = 180 + getAngle(self->x, self->y, self->targetLocation.x, self->targetLocation.y); int wantedAngle = 180 + getAngle(self->x, self->y, self->targetLocation.x, self->targetLocation.y);
wantedAngle %= 360; turnAndFly(wantedAngle);
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();
if (--self->aiActionTime <= 0)
{
self->action = doAI;
}
} }
static void moveToPlayer(void) static void moveToPlayer(void)

View File

@ -23,9 +23,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../defs.h" #include "../defs.h"
#include "../structs.h" #include "../structs.h"
#define AI_DODGE 0 #define AI_EVADE 0
#define AI_BOOST 1 #define AI_BOOST 1
#define AI_SLOW 2 #define AI_FALLBACK 2
#define AI_STRAIGHT 3 #define AI_STRAIGHT 3
#define AI_HUNT 4 #define AI_HUNT 4