2017-02-09 00:26:04 +00:00
|
|
|
module TS.SpaceTac {
|
2017-02-27 00:42:12 +00:00
|
|
|
/**
|
|
|
|
* Ship maneuver for an artifical intelligence
|
|
|
|
*
|
2017-05-17 22:07:16 +00:00
|
|
|
* A maneuver is like a human player action, choosing an action and using it
|
2017-02-27 00:42:12 +00:00
|
|
|
*/
|
2015-03-12 00:00:00 +00:00
|
|
|
export class Maneuver {
|
2015-03-12 00:00:00 +00:00
|
|
|
// Concerned ship
|
2017-05-18 21:10:16 +00:00
|
|
|
ship: Ship
|
|
|
|
|
|
|
|
// Reference to battle
|
|
|
|
battle: Battle
|
2015-03-12 00:00:00 +00:00
|
|
|
|
2017-05-17 22:07:16 +00:00
|
|
|
// Action to use
|
2017-05-18 21:10:16 +00:00
|
|
|
action: BaseAction
|
2015-03-12 00:00:00 +00:00
|
|
|
|
2017-05-18 21:10:16 +00:00
|
|
|
// Target for the action
|
|
|
|
target: Target
|
2015-03-12 00:00:00 +00:00
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
// Result of move-fire simulation
|
2017-05-18 21:10:16 +00:00
|
|
|
simulation: MoveFireResult
|
|
|
|
|
|
|
|
// List of guessed effects of this maneuver
|
|
|
|
effects: [Ship, BaseEffect][]
|
2017-02-27 00:42:12 +00:00
|
|
|
|
2017-05-17 22:07:16 +00:00
|
|
|
constructor(ship: Ship, action: BaseAction, target: Target, move_margin = 0.1) {
|
2015-03-12 00:00:00 +00:00
|
|
|
this.ship = ship;
|
2017-05-18 21:10:16 +00:00
|
|
|
this.battle = nn(ship.getBattle());
|
2017-05-17 22:07:16 +00:00
|
|
|
this.action = action;
|
2015-03-12 00:00:00 +00:00
|
|
|
this.target = target;
|
2017-02-27 00:42:12 +00:00
|
|
|
|
|
|
|
let simulator = new MoveFireSimulator(this.ship);
|
2017-05-17 22:07:16 +00:00
|
|
|
this.simulation = simulator.simulateAction(this.action, this.target, move_margin);
|
2017-05-18 21:10:16 +00:00
|
|
|
|
|
|
|
this.effects = this.guessEffects();
|
2015-03-12 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:20:05 +00:00
|
|
|
jasmineToString() {
|
2017-05-17 22:07:16 +00:00
|
|
|
return `Use ${this.action.code} on ${this.target.jasmineToString()}`;
|
2017-05-09 23:20:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
/**
|
|
|
|
* Apply the maneuver in current battle
|
|
|
|
*/
|
2015-03-12 00:00:00 +00:00
|
|
|
apply(): void {
|
2017-02-27 00:42:12 +00:00
|
|
|
if (this.simulation.success) {
|
2017-03-07 22:16:47 +00:00
|
|
|
this.simulation.parts.filter(part => part.possible).forEach(part => {
|
2017-03-07 19:27:46 +00:00
|
|
|
if (!part.action.apply(this.ship, part.target)) {
|
|
|
|
console.error("AI cannot apply maneuver", this, part);
|
2017-02-27 00:42:12 +00:00
|
|
|
}
|
|
|
|
});
|
2017-02-22 01:14:14 +00:00
|
|
|
}
|
2015-03-12 00:00:00 +00:00
|
|
|
}
|
2017-05-09 21:09:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the location of the ship after the action
|
|
|
|
*/
|
|
|
|
getFinalLocation(): { x: number, y: number } {
|
|
|
|
if (this.simulation.need_move) {
|
|
|
|
return this.simulation.move_location;
|
|
|
|
} else {
|
|
|
|
return { x: this.ship.arena_x, y: this.ship.arena_y };
|
|
|
|
}
|
|
|
|
}
|
2017-05-09 23:20:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total power usage of this maneuver
|
|
|
|
*/
|
|
|
|
getPowerUsage(): number {
|
|
|
|
return this.simulation.total_move_ap + this.simulation.total_fire_ap;
|
|
|
|
}
|
2017-05-18 21:10:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Guess what will be the effects applied on any ship by this maneuver
|
|
|
|
*/
|
|
|
|
guessEffects(): [Ship, BaseEffect][] {
|
|
|
|
let result: [Ship, BaseEffect][] = [];
|
|
|
|
|
|
|
|
if (this.action instanceof FireWeaponAction) {
|
|
|
|
result = result.concat(this.action.getEffects(this.ship, this.target));
|
|
|
|
} else if (this.action instanceof DeployDroneAction) {
|
|
|
|
let ships = this.battle.collectShipsInCircle(this.target, this.action.effect_radius, true);
|
|
|
|
this.action.effects.forEach(effect => {
|
|
|
|
result = result.concat(ships.map(ship => <[Ship, BaseEffect]>[ship, effect]));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2015-03-12 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|