Added safety margin to AI to avoid numerical precision errors on AP estimate
This commit is contained in:
parent
86ff98707d
commit
b60aa3fd18
|
@ -24,8 +24,13 @@ module SpaceTac.Game.AI {
|
|||
|
||||
// Basic Artificial Intelligence, with a tendency to move forward and shoot the nearest enemy
|
||||
export class BullyAI extends AbstractAI {
|
||||
// Safety margin in moves to account for floating-point rounding errors
|
||||
move_margin: number;
|
||||
|
||||
constructor(fleet: Fleet) {
|
||||
super(fleet);
|
||||
|
||||
this.move_margin = 0.1;
|
||||
}
|
||||
|
||||
protected initWork(): void {
|
||||
|
@ -99,7 +104,7 @@ module SpaceTac.Game.AI {
|
|||
return null;
|
||||
} else {
|
||||
engine = engines[0];
|
||||
var move_distance = distance - weapon.distance;
|
||||
var move_distance = distance - weapon.distance + this.move_margin;
|
||||
var move_ap = engine.ap_usage * move_distance / engine.distance;
|
||||
if (move_ap > remaining_ap) {
|
||||
// Not enough AP to move in range
|
||||
|
|
|
@ -52,6 +52,7 @@ module SpaceTac.Game.AI.Specs {
|
|||
var enemy = new Ship();
|
||||
var ai = new BullyAI(ship.fleet);
|
||||
ai.ship = ship;
|
||||
ai.move_margin = 0;
|
||||
var weapon = new Equipment(SlotType.Weapon);
|
||||
weapon.ap_usage = 2;
|
||||
weapon.distance = 3;
|
||||
|
@ -78,6 +79,17 @@ module SpaceTac.Game.AI.Specs {
|
|||
expect(result.target).toBe(enemy);
|
||||
expect(result.weapon).toBe(weapon);
|
||||
|
||||
// enemy out of range, but moving can bring it in range, except for the safety margin
|
||||
ai.move_margin = 0.1;
|
||||
ship.ap_current.set(8);
|
||||
ship.arena_x = 1;
|
||||
ship.arena_y = 0;
|
||||
enemy.arena_x = 6;
|
||||
enemy.arena_y = 0;
|
||||
result = ai.checkBullyMove(enemy, weapon);
|
||||
expect(result).toBeNull();
|
||||
ai.move_margin = 0;
|
||||
|
||||
// enemy totally out of range
|
||||
ship.ap_current.set(8);
|
||||
ship.arena_x = 1;
|
||||
|
@ -161,6 +173,7 @@ module SpaceTac.Game.AI.Specs {
|
|||
battle.fleets[1].addShip(ship2);
|
||||
|
||||
var ai = new BullyAI(ship1.fleet);
|
||||
ai.move_margin = 0;
|
||||
ai.async = false;
|
||||
ai.ship = ship1;
|
||||
|
||||
|
|
Loading…
Reference in a new issue