From d57e228f33682528331055a6e59c45260dfb3dae Mon Sep 17 00:00:00 2001 From: asvitkine Date: Fri, 5 Jul 2024 22:50:11 -0400 Subject: [PATCH] Small clean up to sortUnitsForCasualtiesWithSupport(). Since we'll need to debug this code for https://github.com/triplea-game/triplea/issues/12689, this is a prelim cleanup to make the code easier to understand. It splits out the core logic into an impl function, with the caching logic being a wrapper around it. Also improves some comments and uses some local variables to reduce wrapping. --- .../casualty/CasualtyOrderOfLosses.java | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/battle/casualty/CasualtyOrderOfLosses.java b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/battle/casualty/CasualtyOrderOfLosses.java index b8153714b77..23e4eb29f11 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/delegate/battle/casualty/CasualtyOrderOfLosses.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/delegate/battle/casualty/CasualtyOrderOfLosses.java @@ -49,12 +49,12 @@ static class Parameters { /** * The purpose of this is to return a list in the PERFECT order of which units should be selected * to die first, And that means that certain units MUST BE INTERLEAVED. This list assumes that you - * have already taken any extra hit points away from any 2 hitpoint units. Example: You have a 1 + * have already taken any extra hit points away from any 2 hit point units. Example: You have a 1 * attack Artillery unit that supports, and a 1 attack infantry unit that can receive support. The * best selection of units to die is first to take whichever unit has excess, then cut that down * til they are both the same size, then to take 1 artillery followed by 1 infantry, followed by 1 - * artillery, then 1 inf, etc, until everyone is dead. If you just return all infantry followed by - * all artillery, or the other way around, you will be missing out on some important support + * artillery, then 1 inf, etc., until everyone is dead. If you just return all infantry followed + * by all artillery, or the other way around, you will be missing out on some important support * provided. (Veqryn) */ List sortUnitsForCasualtiesWithSupport(final Parameters parameters) { @@ -81,6 +81,24 @@ List sortUnitsForCasualtiesWithSupport(final Parameters parameters) { } return result; } + + final List sortedWellEnoughUnitsList = sortUnitsForCasualtiesWithSupportImpl(parameters); + // Cache result and all subsets of the result + final List unitTypes = new ArrayList<>(); + for (final Unit u : sortedWellEnoughUnitsList) { + unitTypes.add(AmphibType.of(u)); + } + for (final Iterator it = unitTypes.iterator(); it.hasNext(); ) { + oolCache.put(key, new ArrayList<>(unitTypes)); + final AmphibType unitTypeToRemove = it.next(); + targetTypes.remove(unitTypeToRemove); + key = computeOolCacheKey(parameters, targetTypes); + it.remove(); + } + return sortedWellEnoughUnitsList; + } + + private List sortUnitsForCasualtiesWithSupportImpl(final Parameters parameters) { // Sort enough units to kill off final List sortedUnitsList = new ArrayList<>(parameters.targetsToPickFrom); sortedUnitsList.sort( @@ -91,7 +109,7 @@ List sortUnitsForCasualtiesWithSupport(final Parameters parameters) { true, false) .reversed()); - // Sort units starting with strongest so that support gets added to them first + // Sort units starting with the strongest so that support gets added to them first final UnitBattleComparator unitComparatorWithoutPrimaryPower = new UnitBattleComparator( parameters.costs, @@ -135,11 +153,11 @@ List sortUnitsForCasualtiesWithSupport(final Parameters parameters) { if (strengthAndRolls == null) { continue; } - // Remove any rolls provided by this support so they aren't counted twice + // Remove any rolls provided by this support, so they aren't counted twice final IntegerMap unitSupportRollsMapForUnit = unitSupportRollsMap.get(u); if (unitSupportRollsMapForUnit != null) { - strengthAndRolls = - strengthAndRolls.subtractRolls(unitSupportRollsMapForUnit.getInt(supportedUnit)); + final int rolls = unitSupportRollsMapForUnit.getInt(supportedUnit); + strengthAndRolls = strengthAndRolls.subtractRolls(rolls); } // If one roll then just add the power if (strengthAndRolls.getRolls() == 1) { @@ -150,10 +168,8 @@ List sortUnitsForCasualtiesWithSupport(final Parameters parameters) { final int powerWithSupport = strengthAndRolls.getPower(); // Find supported unit power without support - final int powerWithoutSupport = - strengthAndRolls - .subtractStrength(unitSupportPowerMapForUnit.getInt(supportedUnit)) - .getPower(); + final int strength = unitSupportPowerMapForUnit.getInt(supportedUnit); + final int powerWithoutSupport = strengthAndRolls.subtractStrength(strength).getPower(); // Add the actual power provided by the support final int addedPower = powerWithSupport - powerWithoutSupport; power += addedPower; @@ -171,10 +187,8 @@ List sortUnitsForCasualtiesWithSupport(final Parameters parameters) { // Find supported unit power with support final int powerWithSupport = strengthAndRolls.getPower(); // Find supported unit power without support - final int powerWithoutSupport = - strengthAndRolls - .subtractRolls(unitSupportRollsMap.get(u).getInt(supportedUnit)) - .getPower(); + final int rolls = unitSupportRollsMapForUnit.getInt(supportedUnit); + final int powerWithoutSupport = strengthAndRolls.subtractRolls(rolls).getPower(); // Add the actual power provided by the support final int addedPower = powerWithSupport - powerWithoutSupport; power += addedPower; @@ -187,7 +201,7 @@ List sortUnitsForCasualtiesWithSupport(final Parameters parameters) { minPower = power; } } - // Add worst unit to sorted list, update any units it supported, and remove from other + // Add the worst unit to sorted list, update any units it supported, and remove from other // collections final IntegerMap unitSupportPowerMapForUnit = unitSupportPowerMap.get(worstUnit); if (unitSupportPowerMapForUnit != null) { @@ -226,18 +240,6 @@ List sortUnitsForCasualtiesWithSupport(final Parameters parameters) { unitSupportRollsMap.remove(worstUnit); } sortedWellEnoughUnitsList.addAll(sortedUnitsList); - // Cache result and all subsets of the result - final List unitTypes = new ArrayList<>(); - for (final Unit u : sortedWellEnoughUnitsList) { - unitTypes.add(AmphibType.of(u)); - } - for (final Iterator it = unitTypes.iterator(); it.hasNext(); ) { - oolCache.put(key, new ArrayList<>(unitTypes)); - final AmphibType unitTypeToRemove = it.next(); - targetTypes.remove(unitTypeToRemove); - key = computeOolCacheKey(parameters, targetTypes); - it.remove(); - } return sortedWellEnoughUnitsList; }