Skip to content

Commit

Permalink
Battle Calculator: Mimic legacy behaviour for intial setup
Browse files Browse the repository at this point in the history
Mimic the legacy behaviour and force the two parties in the initial
setup to be non-allied.  This legacy behaviour was requested in #9532.
  • Loading branch information
emmanuel-perlow committed Aug 31, 2021
1 parent 9313c67 commit 7d0d44b
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,16 @@ class BattleCalculatorPanel extends JPanel {
}
}

// Mimic the legacy behavior and force the parties to be non-allied modifying either
// attacker or defender like the old code did). This legacy behavior was requested in #9532.
if (data.getRelationshipTracker().isAllied(getDefender(), getAttacker())) {
if (location.isWater() && players.isEmpty()) {
getEnemy(getAttacker()).ifPresent(this::setDefender);
} else {
getEnemy(getDefender()).ifPresent(this::setAttacker);
}
}

setAttackingUnits(
location.getUnitCollection().getMatches(Matches.unitIsOwnedBy(getAttacker())));
setDefendingUnits(
Expand Down Expand Up @@ -1361,6 +1371,27 @@ private boolean isLand() {
return landBattleCheckBox.isSelected();
}

/**
* Get a suitable enemy for {@code player}. First checks for players at war, if none found, checks
* for non-allied players, if still no foe was found, returns an empty Optional.
*
* @param player the player to find a foe for
* @return Optional with not an ally or an empty Optional
*/
private Optional<GamePlayer> getEnemy(final GamePlayer player) {
for (final GamePlayer gamePlayer : data.getPlayerList()) {
if (data.getRelationshipTracker().isAtWar(player, gamePlayer)) {
return Optional.of(gamePlayer);
}
}
for (final GamePlayer gamePlayer : data.getPlayerList()) {
if (!data.getRelationshipTracker().isAllied(player, gamePlayer)) {
return Optional.of(gamePlayer);
}
}
return Optional.empty();
}

private void setResultsToBlank() {
final String blank = "------";
attackerWin.setText(blank);
Expand Down

0 comments on commit 7d0d44b

Please sign in to comment.