2015-02-13 00:00:00 +00:00
|
|
|
module SpaceTac.Game {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// Result of an ended battle
|
|
|
|
export class BattleOutcome {
|
|
|
|
// Indicates if the battle is a draw (no winner)
|
|
|
|
draw: boolean;
|
|
|
|
|
|
|
|
// Victorious fleet
|
|
|
|
winner: Fleet;
|
|
|
|
|
|
|
|
// Retrievable loot
|
|
|
|
loot: Equipment[];
|
|
|
|
|
|
|
|
constructor(winner: Fleet) {
|
|
|
|
this.winner = winner;
|
|
|
|
this.draw = winner ? false : true;
|
|
|
|
this.loot = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create loot from dead ships
|
|
|
|
createLoot(battle: Battle, random: RandomGenerator = new RandomGenerator()): void {
|
|
|
|
this.loot = [];
|
|
|
|
battle.fleets.forEach((fleet: Fleet) => {
|
|
|
|
fleet.ships.forEach((ship: Ship) => {
|
|
|
|
if (!ship.alive) {
|
|
|
|
if (ship.fleet === this.winner) {
|
|
|
|
// Member of the winner fleet, salvage a number of equipments
|
|
|
|
var count = random.throwInt(0, ship.getEquipmentCount());
|
|
|
|
while (count > 0) {
|
|
|
|
var salvaged = ship.getRandomEquipment(random);
|
|
|
|
salvaged.detach();
|
|
|
|
this.loot.push(salvaged);
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
var luck = random.throw();
|
|
|
|
if (luck > 0.9) {
|
2015-02-13 00:00:00 +00:00
|
|
|
// Salvage a supposedly transported item
|
|
|
|
var transported = this.generateLootItem(random);
|
|
|
|
if (transported) {
|
|
|
|
this.loot.push(transported);
|
|
|
|
}
|
2015-02-13 00:00:00 +00:00
|
|
|
} else if (luck > 0.5) {
|
2015-02-13 00:00:00 +00:00
|
|
|
// Salvage one equipped item
|
|
|
|
var token = ship.getRandomEquipment(random);
|
|
|
|
if (token) {
|
|
|
|
token.detach();
|
|
|
|
this.loot.push(token);
|
|
|
|
}
|
2015-02-13 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-02-13 00:00:00 +00:00
|
|
|
|
|
|
|
// Create a loot generator for lucky loots
|
|
|
|
getLootGenerator(random: RandomGenerator): LootGenerator {
|
|
|
|
return new LootGenerator(random);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a special loot item for the winner fleet
|
|
|
|
// The equipment will be in the winner range (because its a lucky loot!)
|
|
|
|
generateLootItem(random: RandomGenerator): Equipment {
|
|
|
|
var generator = this.getLootGenerator(random);
|
|
|
|
var level = new IntegerRange(this.winner.level - 1, this.winner.level + 1);
|
|
|
|
return generator.generate(level);
|
|
|
|
}
|
2015-02-13 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|