Fixed the suicide case
This commit is contained in:
parent
0c429bd305
commit
8e3339d6f4
1
TODO.md
1
TODO.md
|
@ -37,7 +37,6 @@ Character sheet
|
|||
Battle
|
||||
------
|
||||
|
||||
* Fix the suicide case when the playing ship dies (it is removed from play_order but no ShipChangeDiff is emitted)
|
||||
* Fix arena's ship hovering happening even when the character sheet (or a dialog) is open on top
|
||||
* Add a voluntary retreat option
|
||||
* Add scroll buttons when there are too many actions
|
||||
|
|
|
@ -104,6 +104,28 @@ module TK.SpaceTac {
|
|||
check.same(battle.playing_ship, ship2);
|
||||
});
|
||||
|
||||
test.case("handles the suicide case (playing ship dies because of its action)", check => {
|
||||
let battle = TestTools.createBattle(3, 1);
|
||||
let [ship1, ship2, ship3, ship4] = battle.play_order;
|
||||
ship1.setArenaPosition(0, 0);
|
||||
ship2.setArenaPosition(0, 0);
|
||||
ship3.setArenaPosition(1000, 1000);
|
||||
ship4.setArenaPosition(1000, 1000);
|
||||
let weapon = TestTools.addWeapon(ship1, 8000, 0, 50, 100);
|
||||
|
||||
check.in("initially", check => {
|
||||
check.same(battle.playing_ship, ship1, "playing ship");
|
||||
check.equals(battle.ships.list().filter(ship => ship.alive), [ship1, ship2, ship3, ship4], "alive ships");
|
||||
});
|
||||
|
||||
let result = battle.applyOneAction(nn(weapon.action), Target.newFromLocation(0, 0));
|
||||
check.equals(result, true, "action applied successfully");
|
||||
check.in("after weapon", check => {
|
||||
check.same(battle.playing_ship, ship3, "playing ship");
|
||||
check.equals(battle.ships.list().filter(ship => ship.alive), [ship3, ship4], "alive ships");
|
||||
});
|
||||
});
|
||||
|
||||
test.case("detects victory condition and logs a final EndBattleEvent", check => {
|
||||
var fleet1 = new Fleet();
|
||||
var fleet2 = new Fleet();
|
||||
|
|
|
@ -362,9 +362,16 @@ module TK.SpaceTac {
|
|||
}
|
||||
if (action.apply(this, ship, target)) {
|
||||
this.performChecks();
|
||||
|
||||
if (!this.ended) {
|
||||
this.applyDiffs([new ShipActionEndedDiff(ship, action, target)]);
|
||||
|
||||
if (ship.playing && ship.getValue("hull") <= 0) {
|
||||
// Playing ship died during its action, force a turn end
|
||||
this.applyOneAction(EndTurnAction.SINGLETON);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -28,21 +28,24 @@ module TK.SpaceTac.Specs {
|
|||
], "fixed values");
|
||||
})
|
||||
|
||||
test.case("marks ships as dead", check => {
|
||||
let battle = new Battle();
|
||||
let ship1 = battle.fleets[0].addShip();
|
||||
let ship2 = battle.fleets[1].addShip();
|
||||
let ship3 = battle.fleets[1].addShip();
|
||||
battle.ships.list().forEach(ship => TestTools.setShipHP(ship, 10, 0));
|
||||
test.case("marks ships as dead, except the playing one", check => {
|
||||
let battle = TestTools.createBattle(1, 2);
|
||||
let [ship1, ship2, ship3] = battle.play_order;
|
||||
let checks = new BattleChecks(battle);
|
||||
check.equals(checks.checkDeadShips(), [], "no ship to mark as dead");
|
||||
|
||||
ship1.setValue("hull", 0);
|
||||
ship3.setValue("hull", 0);
|
||||
check.equals(checks.checkDeadShips(), [
|
||||
new ShipDeathDiff(battle, ship1),
|
||||
new ShipDeathDiff(battle, ship3),
|
||||
], "2 ships to mark as dead");
|
||||
battle.ships.list().forEach(ship => ship.setValue("hull", 0));
|
||||
|
||||
let result = checks.checkDeadShips();
|
||||
check.equals(result, [new ShipDeathDiff(battle, ship2)], "ship2 marked as dead");
|
||||
battle.applyDiffs(result);
|
||||
|
||||
result = checks.checkDeadShips();
|
||||
check.equals(result, [new ShipDeathDiff(battle, ship3)], "ship3 marked as dead");
|
||||
battle.applyDiffs(result);
|
||||
|
||||
result = checks.checkDeadShips();
|
||||
check.equals(result, [], "ship1 left playing");
|
||||
})
|
||||
|
||||
test.case("fixes area effects", check => {
|
||||
|
|
|
@ -110,27 +110,14 @@ module TK.SpaceTac {
|
|||
}
|
||||
|
||||
/**
|
||||
* Check that ship with no more hull are dead
|
||||
* Check that not-playing ships with no more hull are dead
|
||||
*/
|
||||
checkDeadShips(): BaseBattleDiff[] {
|
||||
let result: BaseBattleDiff[] = [];
|
||||
// We do one ship at a time, because the state of one ship may depend on another
|
||||
let dying = ifirst(this.battle.iships(true), ship => !ship.playing && ship.getValue("hull") <= 0);
|
||||
|
||||
iforeach(this.battle.iships(true), ship => {
|
||||
if (ship.getValue("hull") <= 0) {
|
||||
result = result.concat(ship.getDeathDiffs(this.battle));
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the playing ship is not playing
|
||||
*/
|
||||
checkDeadShipPlaying(): BaseBattleDiff[] {
|
||||
if (this.battle.playing_ship && !this.battle.playing_ship.alive) {
|
||||
let ship = this.battle.playing_ship;
|
||||
return EndTurnAction.SINGLETON.getDiffs(ship, this.battle);
|
||||
if (dying) {
|
||||
return dying.getDeathDiffs(this.battle);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
|
|
@ -4,14 +4,14 @@ module TK.SpaceTac {
|
|||
|
||||
// Create a battle between two fleets, with a fixed play order (owned ships, then enemy ships)
|
||||
static createBattle(own_ships = 1, enemy_ships = 1): Battle {
|
||||
var fleet1 = new Fleet();
|
||||
var fleet2 = new Fleet();
|
||||
var fleet1 = new Fleet(new Player(undefined, "Attacker"));
|
||||
var fleet2 = new Fleet(new Player(undefined, "Defender"));
|
||||
|
||||
while (own_ships--) {
|
||||
fleet1.addShip(new Ship());
|
||||
fleet1.addShip();
|
||||
}
|
||||
while (enemy_ships--) {
|
||||
fleet2.addShip(new Ship());
|
||||
fleet2.addShip();
|
||||
}
|
||||
|
||||
var battle = new Battle(fleet1, fleet2);
|
||||
|
|
|
@ -28,7 +28,7 @@ module TK.SpaceTac {
|
|||
return -power_diff;
|
||||
}
|
||||
|
||||
protected getSpecificDiffs(ship: Ship, battle: Battle, target: Target): BaseBattleDiff[] {
|
||||
getSpecificDiffs(ship: Ship, battle: Battle, target: Target): BaseBattleDiff[] {
|
||||
if (ship.is(battle.playing_ship)) {
|
||||
let result: BaseBattleDiff[] = [];
|
||||
let new_ship = battle.getNextShip();
|
||||
|
|
Loading…
Reference in a new issue