2017-09-24 22:23:22 +00:00
|
|
|
module TK.SpaceTac {
|
2017-11-14 00:07:06 +00:00
|
|
|
/**
|
|
|
|
* A turn-based battle between fleets
|
|
|
|
*/
|
2017-02-07 18:54:53 +00:00
|
|
|
export class Battle {
|
2017-11-14 00:07:06 +00:00
|
|
|
// Battle outcome, if the battle has ended
|
|
|
|
outcome: BattleOutcome | null = null
|
2017-05-25 23:09:29 +00:00
|
|
|
|
|
|
|
// Statistics
|
|
|
|
stats: BattleStats
|
2015-02-13 00:00:00 +00:00
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Log of all battle events
|
2017-05-25 23:09:29 +00:00
|
|
|
log: BattleLog
|
2014-12-31 00:00:00 +00:00
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
// List of fleets engaged in battle
|
2017-05-25 23:09:29 +00:00
|
|
|
fleets: Fleet[]
|
2014-12-29 00:00:00 +00:00
|
|
|
|
2017-10-25 22:45:53 +00:00
|
|
|
// Container of all engaged ships
|
|
|
|
ships: RObjectContainer<Ship>
|
2014-12-29 00:00:00 +00:00
|
|
|
|
2019-05-19 22:32:15 +00:00
|
|
|
// Current battle turn count
|
2017-11-14 00:07:06 +00:00
|
|
|
cycle = 0
|
2014-12-31 00:00:00 +00:00
|
|
|
|
2017-02-06 21:46:55 +00:00
|
|
|
// List of deployed drones
|
2017-11-14 00:07:06 +00:00
|
|
|
drones = new RObjectContainer<Drone>()
|
2017-02-06 21:46:55 +00:00
|
|
|
|
2017-01-09 00:37:15 +00:00
|
|
|
// Size of the battle area
|
2017-02-19 16:54:19 +00:00
|
|
|
width: number
|
|
|
|
height: number
|
2017-01-09 00:37:15 +00:00
|
|
|
|
2019-05-15 23:04:55 +00:00
|
|
|
constructor(fleet1 = new Fleet(new Player("Attacker")), fleet2 = new Fleet(new Player("Defender")), width = 1920, height = 1080) {
|
2017-03-09 17:11:00 +00:00
|
|
|
this.fleets = [fleet1, fleet2];
|
2017-10-25 22:45:53 +00:00
|
|
|
this.ships = new RObjectContainer(fleet1.ships.concat(fleet2.ships));
|
2017-02-19 16:54:19 +00:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2015-01-29 00:00:00 +00:00
|
|
|
|
2017-05-25 23:09:29 +00:00
|
|
|
this.log = new BattleLog();
|
|
|
|
this.stats = new BattleStats();
|
|
|
|
|
2015-01-29 00:00:00 +00:00
|
|
|
this.fleets.forEach((fleet: Fleet) => {
|
|
|
|
fleet.setBattle(this);
|
|
|
|
});
|
2014-12-30 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 00:07:06 +00:00
|
|
|
/**
|
|
|
|
* Property is true if the battle has ended
|
|
|
|
*/
|
|
|
|
get ended(): boolean {
|
|
|
|
return bool(this.outcome);
|
|
|
|
}
|
|
|
|
|
2019-05-16 17:33:40 +00:00
|
|
|
/**
|
|
|
|
* Apply a turn plan through a resolution
|
|
|
|
*/
|
2019-05-19 22:32:15 +00:00
|
|
|
applyTurnPlan(plan: TurnPlan, random?: RandomGenerator): void {
|
|
|
|
const resolution = new TurnResolution(this, plan, random);
|
2019-05-16 17:33:40 +00:00
|
|
|
resolution.resolve();
|
2019-05-19 22:32:15 +00:00
|
|
|
this.performChecks();
|
2019-05-16 17:33:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 00:07:06 +00:00
|
|
|
/**
|
|
|
|
* Apply a list of diffs to the game state, and add them to the log.
|
|
|
|
*
|
|
|
|
* This should be the main way to modify the game state.
|
|
|
|
*/
|
|
|
|
applyDiffs(diffs: BaseBattleDiff[]): void {
|
|
|
|
let client = new BattleLogClient(this, this.log);
|
|
|
|
diffs.forEach(diff => client.add(diff));
|
|
|
|
}
|
|
|
|
|
2017-10-02 16:02:30 +00:00
|
|
|
/**
|
|
|
|
* Create a quick random battle, for testing purposes, or quick skirmish
|
|
|
|
*/
|
2017-05-02 22:49:35 +00:00
|
|
|
static newQuickRandom(start = true, level = 1, shipcount = 5): Battle {
|
2017-10-02 16:02:30 +00:00
|
|
|
let player1 = Player.newQuickRandom("Player", level, shipcount, true);
|
|
|
|
let player2 = Player.newQuickRandom("Enemy", level, shipcount, true);
|
2015-01-07 00:00:00 +00:00
|
|
|
|
2017-10-02 16:02:30 +00:00
|
|
|
let result = new Battle(player1.fleet, player2.fleet);
|
2017-02-16 22:59:41 +00:00
|
|
|
if (start) {
|
|
|
|
result.start();
|
2015-02-16 00:00:00 +00:00
|
|
|
}
|
2015-01-07 00:00:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-05-14 23:00:36 +00:00
|
|
|
/**
|
2017-10-25 22:45:53 +00:00
|
|
|
* Get a ship by its ID.
|
2017-05-14 23:00:36 +00:00
|
|
|
*/
|
2017-11-14 00:07:06 +00:00
|
|
|
getShip(id: RObjectId | null): Ship | null {
|
|
|
|
if (id === null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return this.ships.get(id);
|
|
|
|
}
|
2017-05-14 23:00:36 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 01:14:14 +00:00
|
|
|
/**
|
|
|
|
* Return an iterator over all ships engaged in the battle
|
|
|
|
*/
|
2019-05-13 21:17:58 +00:00
|
|
|
iships(alive_only = false): Iterable<Ship> {
|
2017-05-17 23:17:07 +00:00
|
|
|
let result = ichainit(imap(iarray(this.fleets), fleet => iarray(fleet.ships)));
|
|
|
|
return alive_only ? ifilter(result, ship => ship.alive) : result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an iterator over ships allies of (or owned by) a player
|
|
|
|
*/
|
2019-05-13 21:17:58 +00:00
|
|
|
iallies(ship: Ship, alive_only = false): Iterable<Ship> {
|
2018-01-16 00:08:24 +00:00
|
|
|
return ifilter(this.iships(alive_only), iship => iship.fleet.player.is(ship.fleet.player));
|
2017-02-22 01:14:14 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
/**
|
|
|
|
* Return an iterator over ships enemy of a player
|
|
|
|
*/
|
2019-05-13 21:17:58 +00:00
|
|
|
ienemies(ship: Ship, alive_only = false): Iterable<Ship> {
|
2018-01-16 00:08:24 +00:00
|
|
|
return ifilter(this.iships(alive_only), iship => !iship.fleet.player.is(ship.fleet.player));
|
2017-02-27 00:42:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 00:08:24 +00:00
|
|
|
/**
|
|
|
|
* Check if a player is able to play
|
|
|
|
*
|
|
|
|
* This can be used by the UI to determine if player interaction is allowed
|
|
|
|
*/
|
2015-02-16 00:00:00 +00:00
|
|
|
canPlay(player: Player): boolean {
|
2019-05-19 22:32:15 +00:00
|
|
|
return !this.ended;
|
2017-11-14 00:07:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-30 00:00:00 +00:00
|
|
|
// Defines the initial ship positions of all engaged fleets
|
2017-05-30 10:33:07 +00:00
|
|
|
placeShips(vertical = true): void {
|
|
|
|
if (vertical) {
|
|
|
|
this.placeFleetShips(this.fleets[0], this.width * 0.25, this.height * 0.5, 0, this.height);
|
|
|
|
this.placeFleetShips(this.fleets[1], this.width * 0.75, this.height * 0.5, Math.PI, this.height);
|
|
|
|
} else {
|
|
|
|
this.placeFleetShips(this.fleets[0], this.width * 0.5, this.height * 0.90, -Math.PI / 2, this.width);
|
|
|
|
this.placeFleetShips(this.fleets[1], this.width * 0.5, this.height * 0.10, Math.PI / 2, this.width);
|
|
|
|
}
|
2014-12-30 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 00:00:00 +00:00
|
|
|
// Collect all ships within a given radius of a target
|
2017-02-15 22:34:27 +00:00
|
|
|
collectShipsInCircle(center: Target, radius: number, alive_only = false): Ship[] {
|
2017-02-27 00:42:12 +00:00
|
|
|
return imaterialize(ifilter(this.iships(), ship => (ship.alive || !alive_only) && Target.newFromShip(ship).getDistanceTo(center) <= radius));
|
2015-03-11 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 17:59:16 +00:00
|
|
|
/**
|
2017-11-14 00:07:06 +00:00
|
|
|
* Ends the battle and sets the outcome
|
2017-04-24 17:59:16 +00:00
|
|
|
*/
|
2017-11-14 00:07:06 +00:00
|
|
|
endBattle(winner: Fleet | null) {
|
|
|
|
this.applyDiffs([new EndBattleDiff(winner, this.cycle)]);
|
2015-02-09 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 00:07:06 +00:00
|
|
|
/**
|
2019-05-19 22:32:15 +00:00
|
|
|
* Make an AI play the current turn
|
2017-02-16 22:59:41 +00:00
|
|
|
*/
|
2017-12-21 19:04:54 +00:00
|
|
|
playAI(debug = false): boolean {
|
2019-05-19 22:32:15 +00:00
|
|
|
// TODO
|
|
|
|
//AIWorker.process(this, debug);
|
|
|
|
return false;
|
2017-02-16 22:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 00:07:06 +00:00
|
|
|
/**
|
|
|
|
* Start the battle
|
|
|
|
*
|
|
|
|
* This will call all necessary initialization steps (initiative, placement...)
|
|
|
|
*
|
|
|
|
* This should not put any diff in the log
|
|
|
|
*/
|
2014-12-30 00:00:00 +00:00
|
|
|
start(): void {
|
2017-11-14 00:07:06 +00:00
|
|
|
this.outcome = null;
|
|
|
|
this.cycle = 1;
|
2014-12-30 00:00:00 +00:00
|
|
|
this.placeShips();
|
2017-11-14 00:07:06 +00:00
|
|
|
iforeach(this.iships(), ship => ship.restoreInitialState());
|
2017-10-25 22:45:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 10:33:07 +00:00
|
|
|
/**
|
|
|
|
* Defines the initial ship positions for one fleet
|
|
|
|
*
|
|
|
|
* *x* and *y* are the center of the fleet formation
|
|
|
|
* *facing_angle* is the forward angle in radians
|
|
|
|
* *width* is the formation width
|
|
|
|
*/
|
|
|
|
private placeFleetShips(fleet: Fleet, x: number, y: number, facing_angle: number, width: number): void {
|
2015-01-07 00:00:00 +00:00
|
|
|
var side_angle = facing_angle + Math.PI * 0.5;
|
2017-05-30 10:33:07 +00:00
|
|
|
var spacing = width * 0.2;
|
2015-01-07 00:00:00 +00:00
|
|
|
var total_length = spacing * (fleet.ships.length - 1);
|
|
|
|
var dx = Math.cos(side_angle);
|
|
|
|
var dy = Math.sin(side_angle);
|
|
|
|
x -= dx * total_length * 0.5;
|
|
|
|
y -= dy * total_length * 0.5;
|
|
|
|
for (var i = 0; i < fleet.ships.length; i++) {
|
|
|
|
fleet.ships[i].setArenaPosition(x + i * dx * spacing, y + i * dy * spacing);
|
|
|
|
fleet.ships[i].setArenaFacingAngle(facing_angle);
|
|
|
|
}
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
2017-02-06 21:46:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a drone to the battle
|
|
|
|
*/
|
2017-11-14 00:07:06 +00:00
|
|
|
addDrone(drone: Drone) {
|
|
|
|
this.drones.add(drone);
|
2017-02-06 21:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a drone from the battle
|
|
|
|
*/
|
2017-11-14 00:07:06 +00:00
|
|
|
removeDrone(drone: Drone) {
|
|
|
|
this.drones.remove(drone);
|
2017-02-06 21:46:55 +00:00
|
|
|
}
|
2017-06-12 22:28:54 +00:00
|
|
|
|
|
|
|
/**
|
2018-03-29 22:57:53 +00:00
|
|
|
* Get the list of area effects that are expected to apply on a given ship
|
2017-06-12 22:28:54 +00:00
|
|
|
*/
|
2018-03-29 22:57:53 +00:00
|
|
|
getAreaEffects(ship: Ship): [Ship | Drone, BaseEffect][] {
|
|
|
|
let drone_effects = this.drones.list().map(drone => {
|
2018-05-15 14:57:45 +00:00
|
|
|
// TODO Should apply filterImpactedShips from drone action
|
2018-03-29 22:57:53 +00:00
|
|
|
if (drone.isInRange(ship.arena_x, ship.arena_y)) {
|
|
|
|
return drone.effects.map((effect): [Ship | Drone, BaseEffect] => [drone, effect]);
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let ships_effects = this.ships.list().map(iship => {
|
|
|
|
return iship.getAreaEffects(ship).map((effect): [Ship | Drone, BaseEffect] => [iship, effect]);
|
|
|
|
});
|
2017-06-12 22:28:54 +00:00
|
|
|
|
2018-03-29 22:57:53 +00:00
|
|
|
return flatten(drone_effects.concat(ships_effects));
|
2017-06-12 22:28:54 +00:00
|
|
|
}
|
2017-11-14 00:07:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform all battle checks to ensure the state is consistent
|
2018-06-13 21:52:36 +00:00
|
|
|
*
|
|
|
|
* Returns all applied diffs
|
2017-11-14 00:07:06 +00:00
|
|
|
*/
|
2018-06-13 21:52:36 +00:00
|
|
|
performChecks(): BaseBattleDiff[] {
|
2017-11-14 00:07:06 +00:00
|
|
|
let checks = new BattleChecks(this);
|
2018-06-13 21:52:36 +00:00
|
|
|
return checks.apply();
|
2017-11-14 00:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply one action to the battle state
|
|
|
|
*
|
|
|
|
* At the end of the action, some checks will be applied to ensure the battle state is consistent
|
|
|
|
*/
|
2017-12-21 19:04:54 +00:00
|
|
|
applyOneAction(action_id: RObjectId, target?: Target): boolean {
|
2019-05-19 22:32:15 +00:00
|
|
|
let ship = first(this.ships.list(), ship => ship.actions.getById(action_id) !== null);
|
2017-11-14 00:07:06 +00:00
|
|
|
if (ship) {
|
2018-02-08 15:16:03 +00:00
|
|
|
let action = ship.actions.getById(action_id);
|
2017-12-21 19:04:54 +00:00
|
|
|
if (action) {
|
|
|
|
if (!target) {
|
|
|
|
target = action.getDefaultTarget(ship);
|
|
|
|
}
|
2017-12-04 23:46:48 +00:00
|
|
|
|
2017-12-21 19:04:54 +00:00
|
|
|
if (action.apply(this, ship, target)) {
|
|
|
|
this.performChecks();
|
2017-12-04 23:46:48 +00:00
|
|
|
|
2017-12-21 19:04:54 +00:00
|
|
|
if (!this.ended) {
|
|
|
|
this.applyDiffs([new ShipActionEndedDiff(ship, action, target)]);
|
2017-12-04 23:46:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 19:04:54 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-14 00:07:06 +00:00
|
|
|
} else {
|
2017-12-21 19:04:54 +00:00
|
|
|
console.error("Action not found on ship", action_id, ship);
|
2017-11-14 00:07:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-21 19:04:54 +00:00
|
|
|
console.error("Cannot apply action - ship not playing", action_id, this);
|
2017-11-14 00:07:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-19 22:32:15 +00:00
|
|
|
* Revert the last turn
|
2017-11-14 00:07:06 +00:00
|
|
|
*
|
|
|
|
* This will remove diffs from the log, so pay attention to other log clients!
|
|
|
|
*/
|
2019-05-19 22:32:15 +00:00
|
|
|
revertOneTurn(): void {
|
2017-11-14 00:07:06 +00:00
|
|
|
let client = new BattleLogClient(this, this.log);
|
2019-05-19 22:32:15 +00:00
|
|
|
while (!client.atStart() && !(client.getCurrent() instanceof TurnStartDiff)) {
|
2017-11-14 00:07:06 +00:00
|
|
|
client.backward();
|
|
|
|
}
|
|
|
|
if (!client.atStart()) {
|
|
|
|
client.backward();
|
|
|
|
}
|
|
|
|
client.truncate();
|
|
|
|
}
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
2015-01-07 00:00:00 +00:00
|
|
|
}
|