2017-02-09 00:00:35 +00:00
|
|
|
module TS.SpaceTac {
|
2015-02-27 00:00:00 +00:00
|
|
|
// unit testing utilities
|
|
|
|
export class TestTools {
|
2015-03-12 00:00:00 +00:00
|
|
|
|
|
|
|
// Create a battle between two fleets, with a fixed play order (owned ships, then enemy ships)
|
|
|
|
static createBattle(own_ships: number, enemy_ships: number): Battle {
|
|
|
|
var fleet1 = new Fleet();
|
|
|
|
var fleet2 = new Fleet();
|
|
|
|
|
|
|
|
while (own_ships--) {
|
|
|
|
fleet1.addShip(new Ship());
|
|
|
|
}
|
|
|
|
while (enemy_ships--) {
|
|
|
|
fleet2.addShip(new Ship());
|
|
|
|
}
|
|
|
|
|
|
|
|
var battle = new Battle(fleet1, fleet2);
|
|
|
|
battle.play_order = fleet1.ships.concat(fleet2.ships);
|
|
|
|
battle.advanceToNextShip();
|
|
|
|
return battle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get or add an equipment of a given slot type
|
2017-02-05 18:01:00 +00:00
|
|
|
static getOrGenEquipment(ship: Ship, slot: SlotType, template: LootTemplate, force_generate = false): Equipment {
|
2015-03-12 00:00:00 +00:00
|
|
|
var equipped = ship.listEquipment(slot);
|
2015-02-27 00:00:00 +00:00
|
|
|
var equipment: Equipment;
|
2017-02-05 18:01:00 +00:00
|
|
|
if (force_generate || equipped.length === 0) {
|
2017-04-18 19:51:23 +00:00
|
|
|
equipment = template.generate(1);
|
|
|
|
equipment.requirements = {};
|
2015-03-12 00:00:00 +00:00
|
|
|
ship.addSlot(slot).attach(equipment);
|
2015-02-27 00:00:00 +00:00
|
|
|
} else {
|
2015-03-12 00:00:00 +00:00
|
|
|
equipment = equipped[0];
|
2015-02-27 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 00:00:00 +00:00
|
|
|
return equipment;
|
|
|
|
}
|
|
|
|
|
2015-04-15 00:00:00 +00:00
|
|
|
// Add an engine, allowing a ship to move *distance*, for each action points
|
2015-04-21 20:14:17 +00:00
|
|
|
static addEngine(ship: Ship, distance: number): Equipment {
|
2017-04-18 22:55:59 +00:00
|
|
|
var equipment = this.getOrGenEquipment(ship, SlotType.Engine, new Equipments.RocketEngine(), true);
|
2017-04-18 19:51:23 +00:00
|
|
|
(<MoveAction>equipment.action).distance_per_power = distance;
|
2015-04-21 20:14:17 +00:00
|
|
|
return equipment;
|
2015-04-15 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 00:42:12 +00:00
|
|
|
/**
|
|
|
|
* Add a weapon to a ship
|
|
|
|
*/
|
|
|
|
static addWeapon(ship: Ship, damage = 100, power_usage = 1, max_distance = 100, blast = 0): Equipment {
|
|
|
|
var equipment = ship.addSlot(SlotType.Weapon).attach(new Equipment(SlotType.Weapon));
|
2017-04-18 19:51:23 +00:00
|
|
|
equipment.action = new FireWeaponAction(equipment, power_usage, max_distance, blast, [new DamageEffect(damage)], "Test Weapon");
|
2017-02-27 00:42:12 +00:00
|
|
|
return equipment;
|
|
|
|
}
|
|
|
|
|
2015-03-12 00:00:00 +00:00
|
|
|
// Set a ship action points, adding/updating an equipment if needed
|
|
|
|
static setShipAP(ship: Ship, points: number, recovery: number = 0): void {
|
2017-04-18 22:55:59 +00:00
|
|
|
var equipment = this.getOrGenEquipment(ship, SlotType.Power, new Equipments.NuclearReactor());
|
2015-03-12 00:00:00 +00:00
|
|
|
|
2017-04-18 19:51:23 +00:00
|
|
|
equipment.effects.forEach(effect => {
|
2017-02-07 19:15:21 +00:00
|
|
|
if (effect instanceof AttributeEffect) {
|
|
|
|
if (effect.attrcode === "power_capacity") {
|
|
|
|
effect.value = points;
|
|
|
|
} else if (effect.attrcode === "power_recovery") {
|
|
|
|
effect.value = recovery;
|
2017-04-18 19:51:23 +00:00
|
|
|
} else if (effect.attrcode === "power_initial") {
|
|
|
|
effect.value = points;
|
2015-02-27 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-07 19:15:21 +00:00
|
|
|
ship.updateAttributes();
|
|
|
|
ship.setValue("power", points);
|
2015-02-27 00:00:00 +00:00
|
|
|
}
|
2015-03-12 00:00:00 +00:00
|
|
|
|
|
|
|
// Set a ship hull and shield points, adding/updating an equipment if needed
|
|
|
|
static setShipHP(ship: Ship, hull_points: number, shield_points: number): void {
|
2017-03-05 14:12:08 +00:00
|
|
|
var armor = TestTools.getOrGenEquipment(ship, SlotType.Hull, new Equipments.IronHull());
|
2017-04-18 22:55:59 +00:00
|
|
|
var shield = TestTools.getOrGenEquipment(ship, SlotType.Shield, new Equipments.ForceField());
|
2015-03-12 00:00:00 +00:00
|
|
|
|
2017-04-18 19:51:23 +00:00
|
|
|
armor.effects.forEach(effect => {
|
2017-02-07 19:15:21 +00:00
|
|
|
if (effect instanceof AttributeEffect) {
|
|
|
|
if (effect.attrcode === "hull_capacity") {
|
|
|
|
effect.value = hull_points;
|
2015-03-12 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-04-18 19:51:23 +00:00
|
|
|
shield.effects.forEach((effect: BaseEffect) => {
|
2017-02-07 19:15:21 +00:00
|
|
|
if (effect instanceof AttributeEffect) {
|
|
|
|
if (effect.attrcode === "shield_capacity") {
|
|
|
|
effect.value = shield_points;
|
2015-03-12 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ship.updateAttributes();
|
|
|
|
ship.restoreHealth();
|
|
|
|
}
|
2015-02-27 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|