2017-02-21 21:16:18 +00:00
|
|
|
/// <reference path="AbstractAI.ts"/>
|
|
|
|
/// <reference path="Maneuver.ts"/>
|
2017-09-24 22:23:22 +00:00
|
|
|
module TK.SpaceTac {
|
2017-02-21 21:16:18 +00:00
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
export type TacticalProducer = Iterator<Maneuver>;
|
2017-03-08 23:18:40 +00:00
|
|
|
export type TacticalEvaluator = (maneuver: Maneuver) => number;
|
2017-02-21 21:16:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AI that applies a set of tactical rules
|
|
|
|
*
|
|
|
|
* It uses a set of producers (to propose new maneuvers), and evaluators (to choose the best maneuver).
|
2017-02-22 01:14:14 +00:00
|
|
|
*
|
|
|
|
* As much work as possible is done using iterators, without materializing every possibilities.
|
2017-02-21 21:16:18 +00:00
|
|
|
*/
|
|
|
|
export class TacticalAI extends AbstractAI {
|
2017-05-17 23:03:33 +00:00
|
|
|
private producers: TacticalProducer[] = []
|
|
|
|
private evaluators: TacticalEvaluator[] = []
|
2017-02-21 21:16:18 +00:00
|
|
|
|
2017-05-17 23:03:33 +00:00
|
|
|
private best: Maneuver | null
|
|
|
|
private best_score: number
|
2017-02-21 21:16:18 +00:00
|
|
|
|
2017-05-18 23:19:05 +00:00
|
|
|
protected initWork(): void {
|
2017-03-08 23:18:40 +00:00
|
|
|
this.best = null;
|
|
|
|
this.best_score = -Infinity;
|
|
|
|
|
2017-05-17 23:03:33 +00:00
|
|
|
this.producers = this.getDefaultProducers();
|
|
|
|
this.evaluators = this.getDefaultEvaluators();
|
2017-02-22 01:14:14 +00:00
|
|
|
|
2017-08-23 17:59:22 +00:00
|
|
|
if (this.debug) {
|
|
|
|
console.log("AI started", this.name, this.ship.name);
|
|
|
|
}
|
2017-02-21 21:16:18 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 17:59:22 +00:00
|
|
|
protected doWorkUnit(): boolean {
|
|
|
|
if (this.producers.length > 0 && this.getDuration() < 8000) {
|
2017-02-27 00:42:12 +00:00
|
|
|
// Produce a maneuver
|
2017-03-09 17:11:00 +00:00
|
|
|
let maneuver: Maneuver | null = null;
|
2017-02-27 00:42:12 +00:00
|
|
|
let producer = this.producers.shift();
|
2017-03-09 17:11:00 +00:00
|
|
|
if (producer) {
|
|
|
|
[maneuver, producer] = producer();
|
|
|
|
}
|
2017-02-21 21:16:18 +00:00
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
if (maneuver) {
|
2017-03-09 17:11:00 +00:00
|
|
|
if (producer) {
|
|
|
|
this.producers.push(producer);
|
|
|
|
}
|
2017-02-21 21:16:18 +00:00
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
// Evaluate the maneuver
|
|
|
|
let score = this.evaluate(maneuver);
|
2017-05-17 23:03:33 +00:00
|
|
|
//console.debug("AI evaluation", maneuver, score);
|
2017-02-27 00:42:12 +00:00
|
|
|
if ((Math.abs(score - this.best_score) < 0.0001 && this.random.bool()) || score > this.best_score) {
|
|
|
|
this.best = maneuver;
|
|
|
|
this.best_score = score;
|
|
|
|
}
|
2017-02-21 21:16:18 +00:00
|
|
|
}
|
2017-02-27 00:42:12 +00:00
|
|
|
|
2017-08-23 17:59:22 +00:00
|
|
|
return true;
|
2017-02-21 21:16:18 +00:00
|
|
|
} else if (this.best) {
|
2017-08-23 17:59:22 +00:00
|
|
|
// Choose the best maneuver so far
|
2017-05-18 23:19:05 +00:00
|
|
|
let best_maneuver = this.best;
|
2017-08-23 17:59:22 +00:00
|
|
|
if (this.debug) {
|
|
|
|
console.log("AI maneuver", this.name, this.ship.name, best_maneuver, this.best_score);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.best.action instanceof EndTurnAction) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let success = this.applyManeuver(best_maneuver);
|
|
|
|
if (success && best_maneuver.mayContinue()) {
|
|
|
|
// Try to play another maneuver
|
|
|
|
this.initWork();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No maneuver produced
|
|
|
|
return false;
|
2017-02-21 21:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 01:14:14 +00:00
|
|
|
|
2017-08-23 17:59:22 +00:00
|
|
|
/**
|
|
|
|
* Evaluate a single maneuver
|
|
|
|
*/
|
|
|
|
evaluate(maneuver: Maneuver) {
|
|
|
|
return sum(this.evaluators.map(evaluator => evaluator(maneuver)));
|
|
|
|
}
|
|
|
|
|
2017-02-22 01:14:14 +00:00
|
|
|
/**
|
2017-05-17 23:03:33 +00:00
|
|
|
* Get the default set of maneuver producers
|
2017-02-22 01:14:14 +00:00
|
|
|
*/
|
2017-05-17 23:03:33 +00:00
|
|
|
getDefaultProducers() {
|
2017-02-27 00:42:12 +00:00
|
|
|
let producers = [
|
2017-05-17 23:03:33 +00:00
|
|
|
TacticalAIHelpers.produceEndTurn,
|
2017-02-27 00:42:12 +00:00
|
|
|
TacticalAIHelpers.produceDirectShots,
|
|
|
|
TacticalAIHelpers.produceBlastShots,
|
2017-05-09 23:20:05 +00:00
|
|
|
TacticalAIHelpers.produceDroneDeployments,
|
2017-02-27 00:42:12 +00:00
|
|
|
TacticalAIHelpers.produceRandomMoves,
|
|
|
|
]
|
2017-05-17 23:03:33 +00:00
|
|
|
return producers.map(producer => producer(this.ship, this.ship.getBattle() || new Battle()));
|
2017-02-22 01:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-17 23:03:33 +00:00
|
|
|
* Get the default set of maneuver evaluators
|
2017-02-22 01:14:14 +00:00
|
|
|
*/
|
2017-05-17 23:03:33 +00:00
|
|
|
getDefaultEvaluators() {
|
2017-08-23 17:59:22 +00:00
|
|
|
type EvaluatorHelper = (ship: Ship, battle: Battle, maneuver: Maneuver) => number;
|
|
|
|
|
|
|
|
function scaled(evaluator: EvaluatorHelper, factor: number): EvaluatorHelper {
|
|
|
|
return (ship: Ship, battle: Battle, maneuver: Maneuver) => factor * evaluator(ship, battle, maneuver);
|
|
|
|
}
|
|
|
|
|
|
|
|
let evaluators: EvaluatorHelper[] = [
|
2017-05-18 21:10:16 +00:00
|
|
|
scaled(TacticalAIHelpers.evaluateTurnCost, 1),
|
|
|
|
scaled(TacticalAIHelpers.evaluateOverheat, 10),
|
2017-05-18 23:19:05 +00:00
|
|
|
scaled(TacticalAIHelpers.evaluateEnemyHealth, 500),
|
|
|
|
scaled(TacticalAIHelpers.evaluateAllyHealth, 800),
|
2017-05-18 21:10:16 +00:00
|
|
|
scaled(TacticalAIHelpers.evaluateClustering, 3),
|
2017-05-09 21:09:44 +00:00
|
|
|
scaled(TacticalAIHelpers.evaluatePosition, 1),
|
2017-05-18 21:10:16 +00:00
|
|
|
scaled(TacticalAIHelpers.evaluateIdling, 1),
|
2017-02-27 00:42:12 +00:00
|
|
|
]
|
2017-05-17 23:03:33 +00:00
|
|
|
|
2017-08-23 17:59:22 +00:00
|
|
|
let battle = nn(this.ship.getBattle());
|
|
|
|
return evaluators.map(evaluator => ((maneuver: Maneuver) => evaluator(this.ship, battle, maneuver)));
|
2017-02-22 01:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-21 21:16:18 +00:00
|
|
|
}
|