77 lines
2.9 KiB
TypeScript
77 lines
2.9 KiB
TypeScript
|
module TK.SpaceTac {
|
||
|
export const AIProducerTools = <const>{
|
||
|
/**
|
||
|
* Add a random move action
|
||
|
*/
|
||
|
addMove(planning: TurnPlanning, ship: Ship, action: MoveAction, random: RandomGenerator): void {
|
||
|
const distance = random.random() * (action.max_distance - action.min_distance) + action.min_distance;
|
||
|
const angle = random.random() * Math.PI * 2;
|
||
|
planning.addAction(ship, action, distance, angle);
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Add a random active or passive action
|
||
|
*/
|
||
|
addAction(planning: TurnPlanning, ship: Ship, action: BaseAction, random: RandomGenerator): void {
|
||
|
const range = action.getRangeRadius(ship);
|
||
|
if (range) {
|
||
|
const distance = 1 - random.random() * range;
|
||
|
const angle = random.random() * Math.PI * 2;
|
||
|
planning.addAction(ship, action, distance, angle);
|
||
|
} else {
|
||
|
planning.addAction(ship, action);
|
||
|
}
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Get a single random plan
|
||
|
*/
|
||
|
getRandomPlan(battle: Battle, player: Player, random: RandomGenerator): AIPlan {
|
||
|
const planning = new TurnPlanning(battle, player);
|
||
|
|
||
|
for (let ship of battle.ships.iterator()) {
|
||
|
if (ship.isPlayedBy(player)) {
|
||
|
// Passive actions
|
||
|
for (let action of ship.actions.listAll().filter(action => action.getCategory() == ActionCategory.PASSIVE)) {
|
||
|
if (random.bool()) {
|
||
|
this.addAction(planning, ship, action, random);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Move
|
||
|
for (let action of ship.actions.listAll()) {
|
||
|
if (action instanceof MoveAction) {
|
||
|
if (random.bool()) {
|
||
|
this.addMove(planning, ship, action, random);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Active action
|
||
|
for (let action of ship.actions.listAll().filter(action => action.getCategory() == ActionCategory.ACTIVE)) {
|
||
|
if (random.bool()) {
|
||
|
this.addAction(planning, ship, action, random);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return new AIPlan(planning.getTurnPlan(), battle, player);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export const AIPlanProducers: { [name: string]: (...args: any[]) => AIPlanProducer } = {
|
||
|
random: (battle: Battle, player: Player, gen = RandomGenerator.global) => {
|
||
|
const builder = () => AIProducerTools.getRandomPlan(battle, player, gen);
|
||
|
function* producer() {
|
||
|
while (true) {
|
||
|
yield builder();
|
||
|
}
|
||
|
}
|
||
|
return producer();
|
||
|
}
|
||
|
}
|
||
|
}
|