module TK.SpaceTac.Specs { testing("BattlePlanning", test => { test.case("initializes from a battle state", check => { let battle = new Battle(); let planning = new BattlePlanning(battle); check.equals(planning.getBattlePlan(), { fleets: [ { fleet: battle.fleets[0].id, ships: [] }, { fleet: battle.fleets[1].id, ships: [] }, ] }); battle.fleets[0].addShip(); planning = new BattlePlanning(battle); check.equals(planning.getBattlePlan(), { fleets: [ { fleet: battle.fleets[0].id, ships: [ { ship: battle.fleets[0].ships[0].id, actions: [] }, ] }, { fleet: battle.fleets[1].id, ships: [] }, ] }); }); test.case("gets child fleet and ship plans", check => { const battle = new Battle(); battle.fleets[0].addShip(); battle.fleets[1].addShip(); battle.fleets[1].addShip(); const planning = new BattlePlanning(battle); check.equals(planning.getFleetPlan(battle.fleets[0]).fleet, battle.fleets[0].id); check.equals(planning.getFleetPlan(battle.fleets[1]).fleet, battle.fleets[1].id); check.equals(planning.getFleetPlan(new Fleet()).fleet, -1); check.equals(planning.getShipPlan(battle.fleets[0].ships[0]).ship, battle.fleets[0].ships[0].id); check.equals(planning.getShipPlan(battle.fleets[1].ships[0]).ship, battle.fleets[1].ships[0].id); check.equals(planning.getShipPlan(battle.fleets[1].ships[1]).ship, battle.fleets[1].ships[1].id); check.equals(planning.getShipPlan(new Ship()).ship, -1); }); test.case("adds an action and target to a ship plan", check => { const battle = new Battle(); const ship = battle.fleets[0].addShip(); const action1 = ship.actions.addCustom(new BaseAction()); const planning = new BattlePlanning(battle); check.equals(planning.getShipPlan(ship).actions, []); check.equals(planning.collectAllActions(), []); planning.addAction(ship, action1, Target.newFromShip(ship)); check.equals(planning.getShipPlan(ship).actions, [ { action: action1.id, category: action1.getCategory(), target: Target.newFromShip(ship) } ]); check.equals(planning.collectAllActions(), [ { action: action1.id, category: action1.getCategory(), target: Target.newFromShip(ship) } ]); }); test.case("replaces existing actions", check => { const battle = new Battle(); const ship = battle.fleets[0].addShip(); const action1 = ship.actions.addCustom(new BaseAction()); const action2 = ship.actions.addCustom(new BaseAction()); const planning = new BattlePlanning(battle); check.equals(planning.getShipPlan(ship).actions, []); planning.addAction(ship, action1, Target.newFromShip(ship)); check.equals(planning.getShipPlan(ship).actions, [ { action: action1.id, category: action1.getCategory(), target: Target.newFromShip(ship) } ]); planning.addAction(ship, action2, Target.newFromShip(ship)); check.equals(planning.getShipPlan(ship).actions, [ { action: action1.id, category: action1.getCategory(), target: Target.newFromShip(ship) }, { action: action2.id, category: action2.getCategory(), target: Target.newFromShip(ship) } ]); planning.addAction(ship, action1, Target.newFromLocation(5, 1)); check.equals(planning.getShipPlan(ship).actions, [ { action: action2.id, category: action2.getCategory(), target: Target.newFromShip(ship) }, { action: action1.id, category: action1.getCategory(), target: Target.newFromLocation(5, 1) } ]); }); test.case("replaces other similar actions", check => { const battle = new Battle(); const ship = battle.fleets[0].addShip(); const active1 = ship.actions.addCustom(new TriggerAction()); const active2 = ship.actions.addCustom(new TriggerAction()); const move1 = ship.actions.addCustom(new MoveAction()); const move2 = ship.actions.addCustom(new MoveAction()); const planning = new BattlePlanning(battle); planning.addAction(ship, active1); check.equals(planning.getShipPlan(ship).actions, [ { action: active1.id, category: active1.getCategory(), target: undefined } ]); planning.addAction(ship, active2); check.equals(planning.getShipPlan(ship).actions, [ { action: active2.id, category: active2.getCategory(), target: undefined } ]); planning.addAction(ship, move1); check.equals(planning.getShipPlan(ship).actions, [ { action: active2.id, category: active2.getCategory(), target: undefined }, { action: move1.id, category: move1.getCategory(), target: undefined } ]); planning.addAction(ship, move2); check.equals(planning.getShipPlan(ship).actions, [ { action: active2.id, category: active2.getCategory(), target: undefined }, { action: move2.id, category: move2.getCategory(), target: undefined } ]); }); }); }