1
0
Fork 0

Fixed AI not using AP while moving (cheater...)

This commit is contained in:
Michaël Lemaire 2015-02-19 01:00:00 +01:00
parent ba56824151
commit 53fefc25b8
5 changed files with 76 additions and 18 deletions

View file

@ -235,8 +235,8 @@ module SpaceTac.Game {
// Apply damages to hull and/or shield
addDamage(hull: number, shield: number, log: boolean = true): void {
this.setAttribute(this.hull, -hull, true, log);
this.setAttribute(this.shield, -shield, true, log);
this.setAttribute(this.hull, -hull, true, log);
if (log) {
this.addBattleEvent(new DamageEvent(this, hull, shield));

View file

@ -44,23 +44,26 @@ module SpaceTac.Game.AI {
// Add a work item to the work queue
addWorkItem(item: Function, delay: number = null): void {
if (!this.async) {
if (item) {
item();
}
return;
}
if (!delay) {
delay = 100;
}
var wrapped = () => {
if (item) {
item();
}
this.processNextWorkItem();
};
if (!delay) {
delay = 100;
}
if (this.async && delay) {
this.workqueue.push(() => {
setTimeout(wrapped, delay);
});
} else {
this.workqueue.push(wrapped);
}
this.workqueue.push(() => {
setTimeout(wrapped, delay);
});
}
// Initially fill the work queue.

View file

@ -6,6 +6,9 @@ module SpaceTac.Game.AI {
// Position to move to, before firing
move_to: Target;
// Engine used to move
engine: Equipment;
// Weapon to use
weapon: Equipment;
@ -83,6 +86,7 @@ module SpaceTac.Game.AI {
var target = Target.newFromShip(enemy);
var distance = target.getDistanceTo(Target.newFromShip(this.ship));
var move: Target;
var engine: Equipment;
var remaining_ap = this.ship.ap_current.current;
if (distance <= weapon.distance) {
// No need to move
@ -94,7 +98,7 @@ module SpaceTac.Game.AI {
// No engine available to move
return null;
} else {
var engine = engines[0];
engine = engines[0];
var move_distance = distance - weapon.distance;
var move_ap = engine.ap_usage * move_distance / engine.distance;
if (move_ap > remaining_ap) {
@ -114,6 +118,7 @@ module SpaceTac.Game.AI {
} else {
var result = new BullyMove();
result.move_to = move;
result.engine = engine;
result.target = enemy;
result.weapon = weapon;
return result;
@ -135,11 +140,11 @@ module SpaceTac.Game.AI {
return available[0];
}
// Effectively apply the move
private applyMove(move: BullyMove): void {
// Effectively apply the chosen move
applyMove(move: BullyMove): void {
if (move.move_to) {
this.addWorkItem(() => {
this.ship.moveTo(move.move_to.x, move.move_to.y);
move.engine.action.apply(this.battle, this.ship, move.move_to);
}, 1000);
}

View file

@ -150,5 +150,55 @@ module SpaceTac.Game.AI.Specs {
result = ai.listAllMoves();
expect(result.length).toBe(3);
});
it("applies the chosen move", function () {
var battle = new Battle();
var ship1 = new Ship();
ship1.setArenaPosition(0, 0);
battle.fleets[0].addShip(ship1);
var ship2 = new Ship();
ship2.setArenaPosition(8, 0);
battle.fleets[1].addShip(ship2);
var ai = new BullyAI(ship1.fleet);
ai.async = false;
ai.ship = ship1;
var engine = new Equipment(SlotType.Engine);
engine.distance = 1;
engine.ap_usage = 2;
engine.action = new MoveAction(engine);
ai.ship.addSlot(SlotType.Engine).attach(engine);
var weapon = new Equipment(SlotType.Weapon);
weapon.distance = 6;
weapon.ap_usage = 1;
weapon.target_effects.push(new DamageEffect(20));
weapon.action = new FireWeaponAction(weapon);
ai.ship.addSlot(SlotType.Weapon).attach(weapon);
ai.ship.ap_current.setMaximal(10);
ai.ship.ap_current.set(6);
ship2.hull.set(15);
ship2.shield.set(10);
var move = ai.checkBullyMove(ship2, weapon);
expect(move).not.toBeNull();
battle.log.clear();
ai.applyMove(move);
expect(battle.log.events.length).toBe(6);
expect(battle.log.events[0]).toEqual(new MoveEvent(ship1, 2, 0));
expect(battle.log.events[1]).toEqual(new AttributeChangeEvent(ship1,
new Attribute(AttributeCode.AP, 2, 10)));
expect(battle.log.events[2]).toEqual(new AttributeChangeEvent(ship2,
new Attribute(AttributeCode.Shield, 0)));
expect(battle.log.events[3]).toEqual(new AttributeChangeEvent(ship2,
new Attribute(AttributeCode.Hull, 5)));
expect(battle.log.events[4]).toEqual(new DamageEvent(ship2, 10, 10));
expect(battle.log.events[5]).toEqual(new AttributeChangeEvent(ship1,
new Attribute(AttributeCode.AP, 1, 10)));
});
});
}

View file

@ -111,8 +111,8 @@ module SpaceTac.Game {
expect(ship.hull.current).toEqual(40);
expect(ship.shield.current).toEqual(80);
expect(battle.log.events.length).toBe(3);
expect(battle.log.events[0]).toEqual(new AttributeChangeEvent(ship, ship.hull));
expect(battle.log.events[1]).toEqual(new AttributeChangeEvent(ship, ship.shield));
expect(battle.log.events[0]).toEqual(new AttributeChangeEvent(ship, ship.shield));
expect(battle.log.events[1]).toEqual(new AttributeChangeEvent(ship, ship.hull));
expect(battle.log.events[2]).toEqual(new DamageEvent(ship, 10, 20));
battle.log.clear();