Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Attackers and defenders collections must be distinct". #11959

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ private void findUnitsThatCantMove(

Map<Territory, ProTerritory> moveMap = territoryManager.getDefendOptions().getTerritoryMap();
Map<Unit, Set<Territory>> unitMoveMap = territoryManager.getDefendOptions().getUnitMoveMap();
List<ProTransport> transportMapList = territoryManager.getDefendOptions().getTransportList();

// Add all units that can't move (to be consumed, allied units, 0 move units, etc)
for (final Territory t : moveMap.keySet()) {
Expand All @@ -252,26 +251,13 @@ private void findUnitsThatCantMove(
// Add all units that only have 1 move option and can't be transported
for (final Iterator<Unit> it = unitMoveMap.keySet().iterator(); it.hasNext(); ) {
final Unit u = it.next();
if (unitMoveMap.get(u).size() == 1) {
final Territory onlyTerritory = CollectionUtils.getAny(unitMoveMap.get(u));
if (onlyTerritory.equals(unitTerritoryMap.get(u))) {
boolean canBeTransported = false;
for (final ProTransport pad : transportMapList) {
for (final Territory t : pad.getTransportMap().keySet()) {
if (pad.getTransportMap().get(t).contains(onlyTerritory)) {
canBeTransported = true;
}
}
for (final Territory t : pad.getSeaTransportMap().keySet()) {
if (pad.getSeaTransportMap().get(t).contains(onlyTerritory)) {
canBeTransported = true;
}
}
}
if (!canBeTransported) {
moveMap.get(onlyTerritory).addCantMoveUnit(u);
it.remove();
}
final Set<Territory> territories = unitMoveMap.get(u);
if (territories.size() == 1) {
final Territory onlyTerritory = CollectionUtils.getAny(territories);
if (onlyTerritory.equals(unitTerritoryMap.get(u))
&& !canPotentiallyBeTransported(onlyTerritory)) {
moveMap.get(onlyTerritory).addCantMoveUnit(u);
it.remove();
}
}
}
Expand Down Expand Up @@ -325,6 +311,24 @@ private Map<Unit, Set<Territory>> findInfraUnitsThatCanMove() {
return infraUnitMoveMap;
}

private boolean canPotentiallyBeTransported(Territory unitTerritory) {
final List<ProTransport> transportMapList =
territoryManager.getDefendOptions().getTransportList();
for (final ProTransport pad : transportMapList) {
for (final Territory t : pad.getTransportMap().keySet()) {
if (pad.getTransportMap().get(t).contains(unitTerritory)) {
return true;
}
}
for (final Territory t : pad.getSeaTransportMap().keySet()) {
if (pad.getSeaTransportMap().get(t).contains(unitTerritory)) {
return true;
}
}
}
return false;
}

private List<Territory> moveOneDefenderToLandTerritoriesBorderingEnemy() {

ProLogger.info("Determine which territories to defend with one land unit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ProTerritory {
private ProBattleResult battleResult;

// Non-combat move variables
private final List<Unit> cantMoveUnits;
private final Set<Unit> cantMoveUnits;
private List<Unit> maxEnemyUnits;
private Set<Unit> maxEnemyBombardUnits;
private ProBattleResult minBattleResult;
Expand Down Expand Up @@ -89,7 +89,7 @@ public ProTerritory(final Territory territory, final ProData proData) {
currentlyWins = false;
battleResult = null;

cantMoveUnits = new ArrayList<>();
cantMoveUnits = new HashSet<>();
maxEnemyUnits = new ArrayList<>();
maxEnemyBombardUnits = new HashSet<>();
minBattleResult = new ProBattleResult();
Expand Down Expand Up @@ -126,7 +126,7 @@ public ProTerritory(final Territory territory, final ProData proData) {
currentlyWins = patd.isCurrentlyWins();
battleResult = patd.getBattleResult();

cantMoveUnits = new ArrayList<>(patd.getCantMoveUnits());
cantMoveUnits = new HashSet<>(patd.getCantMoveUnits());
maxEnemyUnits = new ArrayList<>(patd.getMaxEnemyUnits());
maxEnemyBombardUnits = new HashSet<>(patd.getMaxEnemyBombardUnits());
minBattleResult = patd.getMinBattleResult();
Expand Down Expand Up @@ -332,8 +332,8 @@ public String getResultString() {
return sb.toString();
}

public List<Unit> getCantMoveUnits() {
return Collections.unmodifiableList(cantMoveUnits);
public Collection<Unit> getCantMoveUnits() {
return Collections.unmodifiableCollection(cantMoveUnits);
}

public void addCantMoveUnit(final Unit unit) {
Expand Down