2017-02-09 00:00:35 +00:00
|
|
|
module TS.SpaceTac {
|
2017-02-09 22:21:39 +00:00
|
|
|
/**
|
|
|
|
* A fleet of ships, all belonging to the same player
|
|
|
|
*/
|
2017-02-07 18:54:53 +00:00
|
|
|
export class Fleet {
|
2014-12-29 00:00:00 +00:00
|
|
|
// Fleet owner
|
|
|
|
player: Player;
|
|
|
|
|
|
|
|
// List of ships
|
|
|
|
ships: Ship[];
|
|
|
|
|
2015-03-24 00:00:00 +00:00
|
|
|
// Current fleet location
|
2017-03-14 22:28:07 +00:00
|
|
|
location: StarLocation | null = null;
|
|
|
|
previous_location: StarLocation | null = null;
|
2015-03-24 00:00:00 +00:00
|
|
|
|
2015-01-19 00:00:00 +00:00
|
|
|
// Current battle in which the fleet is engaged (null if not fighting)
|
2017-03-14 22:28:07 +00:00
|
|
|
battle: Battle | null = null;
|
2015-01-19 00:00:00 +00:00
|
|
|
|
2017-02-27 23:36:12 +00:00
|
|
|
// Amount of credits available
|
|
|
|
credits = 0;
|
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
// Create a fleet, bound to a player
|
2017-03-09 17:11:00 +00:00
|
|
|
constructor(player = new Player()) {
|
|
|
|
this.player = player;
|
2014-12-30 00:00:00 +00:00
|
|
|
this.ships = [];
|
|
|
|
}
|
|
|
|
|
2017-07-02 18:21:04 +00:00
|
|
|
jasmineToString(): string {
|
2017-07-31 18:17:43 +00:00
|
|
|
return `${this.player.name}'s fleet [${this.ships.map(ship => ship.getFullName()).join(",")}]`;
|
2017-07-02 18:21:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 22:21:39 +00:00
|
|
|
/**
|
|
|
|
* Set the current location of the fleet
|
|
|
|
*
|
|
|
|
* Returns true on success
|
|
|
|
*/
|
|
|
|
setLocation(location: StarLocation, force = false): boolean {
|
|
|
|
if (!force && this.location && location.star != this.location.star && (this.location.type != StarLocationType.WARP || this.location.jump_dest != location)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:28:07 +00:00
|
|
|
this.previous_location = this.location;
|
2015-03-25 00:00:00 +00:00
|
|
|
this.location = location;
|
2017-01-29 18:34:38 +00:00
|
|
|
this.player.setVisited(this.location);
|
2015-04-07 00:00:00 +00:00
|
|
|
|
|
|
|
// Check encounter
|
|
|
|
var battle = this.location.enterLocation(this.player.fleet);
|
|
|
|
if (battle) {
|
|
|
|
this.player.setBattle(battle);
|
|
|
|
}
|
2017-02-09 22:21:39 +00:00
|
|
|
|
|
|
|
return true;
|
2015-03-25 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 18:21:04 +00:00
|
|
|
/**
|
|
|
|
* Add a ship this fleet
|
|
|
|
*/
|
2017-02-27 00:42:12 +00:00
|
|
|
addShip(ship = new Ship()): Ship {
|
2017-02-22 01:14:14 +00:00
|
|
|
if (ship.fleet && ship.fleet != this) {
|
|
|
|
remove(ship.fleet.ships, ship);
|
2015-02-13 00:00:00 +00:00
|
|
|
}
|
2017-02-22 01:14:14 +00:00
|
|
|
add(this.ships, ship);
|
2014-12-30 00:00:00 +00:00
|
|
|
ship.fleet = this;
|
2017-02-22 01:14:14 +00:00
|
|
|
return ship;
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
2015-01-19 00:00:00 +00:00
|
|
|
|
2017-07-02 18:21:04 +00:00
|
|
|
/**
|
|
|
|
* Remove the ship from this fleet, transferring it to another fleet
|
|
|
|
*/
|
|
|
|
removeShip(ship: Ship, fleet = new Fleet()): void {
|
|
|
|
if (ship.fleet === this) {
|
|
|
|
fleet.addShip(ship);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 00:00:00 +00:00
|
|
|
// Set the current battle
|
2017-03-09 17:11:00 +00:00
|
|
|
setBattle(battle: Battle | null): void {
|
2015-01-19 00:00:00 +00:00
|
|
|
this.battle = battle;
|
|
|
|
}
|
2015-02-09 00:00:00 +00:00
|
|
|
|
2015-03-12 00:00:00 +00:00
|
|
|
// Get the average level of this fleet
|
|
|
|
getLevel(): number {
|
|
|
|
if (this.ships.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sum = 0;
|
|
|
|
this.ships.forEach((ship: Ship) => {
|
2017-03-17 00:07:00 +00:00
|
|
|
sum += ship.level.get();
|
2015-03-12 00:00:00 +00:00
|
|
|
});
|
|
|
|
var avg = sum / this.ships.length;
|
2017-03-17 00:07:00 +00:00
|
|
|
return Math.floor(avg);
|
2015-03-12 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 18:21:04 +00:00
|
|
|
/**
|
|
|
|
* Check if the fleet is considered alive (at least one ship alive, and no critical ship dead)
|
|
|
|
*/
|
2015-02-09 00:00:00 +00:00
|
|
|
isAlive(): boolean {
|
2017-07-02 18:21:04 +00:00
|
|
|
if (any(this.ships, ship => ship.critical && !ship.alive)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return any(this.ships, ship => ship.alive);
|
|
|
|
}
|
2015-02-09 00:00:00 +00:00
|
|
|
}
|
2017-07-10 22:50:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an equipment to the first available cargo slot
|
|
|
|
*
|
|
|
|
* Returns true on success, false if no empty cargo slot was available.
|
|
|
|
*/
|
|
|
|
addCargo(equipment: Equipment): boolean {
|
|
|
|
let ship = first(this.ships, ship => ship.getFreeCargoSpace() > 0);
|
|
|
|
if (ship) {
|
|
|
|
return ship.addCargo(equipment);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
2015-01-07 00:00:00 +00:00
|
|
|
}
|