2017-09-24 22:23:22 +00:00
|
|
|
module TK.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
|
2018-01-16 00:08:24 +00:00
|
|
|
player: Player
|
|
|
|
|
|
|
|
// Fleet name
|
|
|
|
name: string
|
2014-12-29 00:00:00 +00:00
|
|
|
|
|
|
|
// List of ships
|
2018-01-16 00:08:24 +00:00
|
|
|
ships: Ship[]
|
2014-12-29 00:00:00 +00:00
|
|
|
|
2015-03-24 00:00:00 +00:00
|
|
|
// Current fleet location
|
2018-01-16 00:08:24 +00:00
|
|
|
location: RObjectId | null = null
|
|
|
|
|
|
|
|
// Visited locations (ordered by last visited)
|
|
|
|
visited: RObjectId[] = []
|
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)
|
2018-01-16 00:08:24 +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
|
2018-01-16 00:08:24 +00:00
|
|
|
credits = 0
|
2017-02-27 23:36:12 +00:00
|
|
|
|
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;
|
2018-01-16 00:08:24 +00:00
|
|
|
this.name = player ? player.name : "Fleet";
|
2014-12-30 00:00:00 +00:00
|
|
|
this.ships = [];
|
|
|
|
}
|
|
|
|
|
2017-07-02 18:21:04 +00:00
|
|
|
jasmineToString(): string {
|
2018-01-16 00:08:24 +00:00
|
|
|
return `${this.name} [${this.ships.map(ship => ship.getName()).join(",")}]`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the owner player
|
|
|
|
*/
|
|
|
|
setPlayer(player: Player): void {
|
|
|
|
this.player = player;
|
2017-07-02 18:21:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 22:21:39 +00:00
|
|
|
/**
|
2018-01-16 00:08:24 +00:00
|
|
|
* Set a location as visited
|
|
|
|
*/
|
|
|
|
setVisited(location: StarLocation): void {
|
|
|
|
remove(this.visited, location.id);
|
|
|
|
this.visited.unshift(location.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the fleet to another location, checking that the move is physically possible
|
2017-02-09 22:21:39 +00:00
|
|
|
*
|
|
|
|
* Returns true on success
|
|
|
|
*/
|
2018-01-16 00:08:24 +00:00
|
|
|
move(to: StarLocation): boolean {
|
|
|
|
if (!this.location) {
|
2017-02-09 22:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-16 00:08:24 +00:00
|
|
|
let source = to.universe.locations.get(this.location);
|
|
|
|
if (!source) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-07 00:00:00 +00:00
|
|
|
|
2018-01-16 00:08:24 +00:00
|
|
|
if (source.star != to.star) {
|
|
|
|
// Need to jump, check conditions
|
|
|
|
if (source.type != StarLocationType.WARP || source.jump_dest != to) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-07 00:00:00 +00:00
|
|
|
}
|
2017-02-09 22:21:39 +00:00
|
|
|
|
2018-01-16 00:08:24 +00:00
|
|
|
this.setLocation(to);
|
2017-02-09 22:21:39 +00:00
|
|
|
return true;
|
2015-03-25 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 00:08:24 +00:00
|
|
|
/**
|
|
|
|
* Set the current location of the fleet, without condition
|
|
|
|
*/
|
|
|
|
setLocation(location: StarLocation): void {
|
|
|
|
if (this.location) {
|
|
|
|
let previous = location.universe.locations.get(this.location);
|
|
|
|
if (previous) {
|
|
|
|
previous.removeFleet(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.location = location.id;
|
|
|
|
this.setVisited(location);
|
|
|
|
location.addFleet(this);
|
|
|
|
}
|
|
|
|
|
2017-07-02 18:21:04 +00:00
|
|
|
/**
|
|
|
|
* Add a ship this fleet
|
|
|
|
*/
|
2018-01-16 00:08:24 +00:00
|
|
|
addShip(ship = new Ship(null, `${this.name} ${this.ships.length + 1}`)): 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-10-25 22:45:53 +00:00
|
|
|
if (this.battle) {
|
|
|
|
this.battle.ships.add(ship);
|
|
|
|
}
|
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
|
|
|
}
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
2015-01-07 00:00:00 +00:00
|
|
|
}
|