Skip to content

Commit f27b56a

Browse files
authored
Add helper method wrapping moveMap.computeIfAbsent() in ProAI code. (#10463)
1 parent f9e4054 commit f27b56a

File tree

4 files changed

+11
-27
lines changed

4 files changed

+11
-27
lines changed

game-app/game-core/src/main/java/games/strategy/triplea/ai/pro/ProCombatMoveAi.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -1878,13 +1878,7 @@ private void checkContestedSeaTerritories() {
18781878
final List<Unit> mySeaUnits =
18791879
t.getUnitCollection()
18801880
.getMatches(ProMatches.unitCanBeMovedAndIsOwnedSea(player, true));
1881-
if (attackMap.containsKey(moveToTerritory)) {
1882-
attackMap.get(moveToTerritory).addUnits(mySeaUnits);
1883-
} else {
1884-
final ProTerritory moveTerritoryData = new ProTerritory(moveToTerritory, proData);
1885-
moveTerritoryData.addUnits(mySeaUnits);
1886-
attackMap.put(moveToTerritory, moveTerritoryData);
1887-
}
1881+
proData.getProTerritory(attackMap, moveToTerritory).addUnits(mySeaUnits);
18881882
ProLogger.info(t + " is a contested territory so moving subs to " + moveToTerritory);
18891883
}
18901884
}

game-app/game-core/src/main/java/games/strategy/triplea/ai/pro/ProData.java

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import games.strategy.triplea.Properties;
1010
import games.strategy.triplea.ai.pro.data.ProPurchaseOption;
1111
import games.strategy.triplea.ai.pro.data.ProPurchaseOptionMap;
12+
import games.strategy.triplea.ai.pro.data.ProTerritory;
1213
import games.strategy.triplea.attachments.TerritoryAttachment;
1314
import games.strategy.triplea.delegate.Matches;
1415
import games.strategy.triplea.util.TuvUtils;
@@ -100,4 +101,8 @@ private double getMinCostPerHitPoint(final List<ProPurchaseOption> landPurchaseO
100101
}
101102
return minCostPerHitPoint;
102103
}
104+
105+
public ProTerritory getProTerritory(Map<Territory, ProTerritory> moveMap, Territory t) {
106+
return moveMap.computeIfAbsent(t, k -> new ProTerritory(t, this));
107+
}
103108
}

game-app/game-core/src/main/java/games/strategy/triplea/ai/pro/ProNonCombatMoveAi.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -2437,13 +2437,7 @@ private Map<Territory, ProTerritory> moveInfraUnits(
24372437
+ " with value="
24382438
+ maxValue);
24392439
moveMap.get(maxValueTerritory).addUnit(u);
2440-
if (factoryMoveMap.containsKey(maxValueTerritory)) {
2441-
factoryMoveMap.get(maxValueTerritory).addUnit(u);
2442-
} else {
2443-
final ProTerritory patd = new ProTerritory(maxValueTerritory, proData);
2444-
patd.addUnit(u);
2445-
factoryMoveMap.put(maxValueTerritory, patd);
2446-
}
2440+
proData.getProTerritory(factoryMoveMap, maxValueTerritory).addUnit(u);
24472441
it.remove();
24482442
}
24492443
}

game-app/game-core/src/main/java/games/strategy/triplea/ai/pro/data/ProTerritoryManager.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -825,10 +825,7 @@ private static void findNavalMoveOptions(
825825
}
826826

827827
// Populate territories with sea unit
828-
moveMap
829-
.computeIfAbsent(
830-
potentialTerritory, k -> new ProTerritory(potentialTerritory, proData))
831-
.addMaxUnit(mySeaUnit);
828+
proData.getProTerritory(moveMap, potentialTerritory).addMaxUnit(mySeaUnit);
832829

833830
// Populate appropriate unit move options map
834831
if (Matches.unitIsTransport().test(mySeaUnit)) {
@@ -932,8 +929,7 @@ private static void findLandMoveOptions(
932929

933930
// Populate territories with land units
934931
final ProTerritory potentialTerritoryMove =
935-
moveMap.computeIfAbsent(
936-
potentialTerritory, k -> new ProTerritory(potentialTerritory, proData));
932+
proData.getProTerritory(moveMap, potentialTerritory);
937933
final List<Unit> unitsToAdd =
938934
ProTransportUtils.findBestUnitsToLandTransport(
939935
myLandUnit, startTerritory, potentialTerritoryMove.getMaxUnits());
@@ -1064,10 +1060,7 @@ private static void findAirMoveOptions(
10641060
}
10651061

10661062
// Populate enemy territories with air unit
1067-
moveMap
1068-
.computeIfAbsent(
1069-
potentialTerritory, k -> new ProTerritory(potentialTerritory, proData))
1070-
.addMaxUnit(myAirUnit);
1063+
proData.getProTerritory(moveMap, potentialTerritory).addMaxUnit(myAirUnit);
10711064

10721065
// Populate unit attack options map
10731066
unitMoveMap.computeIfAbsent(myAirUnit, k -> new HashSet<>()).add(potentialTerritory);
@@ -1254,9 +1247,7 @@ private static void findAmphibMoveOptions(
12541247
player, transport, territoriesCanLoadFrom, alreadyAddedToMaxAmphibUnits);
12551248

12561249
// Add amphib units to attack map
1257-
moveMap
1258-
.computeIfAbsent(moveTerritory, k -> new ProTerritory(moveTerritory, proData))
1259-
.addMaxAmphibUnits(amphibUnits);
1250+
proData.getProTerritory(moveMap, moveTerritory).addMaxAmphibUnits(amphibUnits);
12601251
}
12611252
}
12621253
}

0 commit comments

Comments
 (0)