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

Move a preconditions check earlier. #12695

Merged
merged 2 commits into from
Jul 5, 2024
Merged
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
@@ -1,10 +1,12 @@
package games.strategy.triplea.delegate.battle.casualty;

import com.google.common.base.Preconditions;
import games.strategy.engine.data.GameData;
import games.strategy.engine.data.GamePlayer;
import games.strategy.engine.data.Territory;
import games.strategy.engine.data.Unit;
import games.strategy.engine.data.UnitType;
import games.strategy.engine.data.properties.GameProperties;
import games.strategy.engine.delegate.IDelegateBridge;
import games.strategy.engine.player.Player;
import games.strategy.triplea.Properties;
Expand Down Expand Up @@ -123,29 +125,38 @@ public static CasualtyDetails selectCasualties(
? CasualtyUtil.getTotalHitpointsLeft(sortedTargetsToPickFrom)
: sortedTargetsToPickFrom.size());

final CasualtyDetails casualtyDetails =
final boolean autoChooseCasualties =
hitsRemaining >= totalHitpoints
|| sortedTargetsToPickFrom.size() == 1
|| allTargetsOneTypeOneHitPoint(
sortedTargetsToPickFrom,
dependents,
Properties.getPartialAmphibiousRetreat(data.getProperties()))
? new CasualtyDetails(defaultCasualties, true)
: tripleaPlayer.selectCasualties(
sortedTargetsToPickFrom,
dependents,
hitsRemaining,
text,
dice,
player,
combatValue.getFriendUnits(),
combatValue.getEnemyUnits(),
false,
List.of(),
defaultCasualties,
battleId,
battleSite,
allowMultipleHitsPerUnit);
|| sortedTargetsToPickFrom.size() == 1
|| allTargetsOneTypeOneHitPoint(
sortedTargetsToPickFrom, dependents, data.getProperties());
final CasualtyDetails casualtyDetails;
if (autoChooseCasualties) {
casualtyDetails = new CasualtyDetails(defaultCasualties, true);
} else {
Preconditions.checkState(
defaultCasualties.size() == hitsRemaining,
String.format(
"Select Casualties showing different numbers for number of hits to take (%s) vs "
+ "total size of default casualty selections (%s) in %s (player = %s)",
hitsRemaining, defaultCasualties.size(), battleSite, player.getName()));
casualtyDetails =
tripleaPlayer.selectCasualties(
sortedTargetsToPickFrom,
dependents,
hitsRemaining,
text,
dice,
player,
combatValue.getFriendUnits(),
combatValue.getEnemyUnits(),
false,
List.of(),
defaultCasualties,
battleId,
battleSite,
allowMultipleHitsPerUnit);
}

if (!Properties.getPartialAmphibiousRetreat(data.getProperties())) {
final boolean unitsWithMarineBonusAndWasAmphibiousKilled =
Expand Down Expand Up @@ -304,11 +315,13 @@ public static List<Unit> getCasualtyOrderOfLoss(
*
* @param targets a collection of target units
* @param dependents map of dependent units for target units
* @param properties game properties
*/
private static boolean allTargetsOneTypeOneHitPoint(
final Collection<Unit> targets,
final Map<Unit, Collection<Unit>> dependents,
final boolean separateByRetreatPossibility) {
final GameProperties properties) {
final boolean separateByRetreatPossibility = Properties.getPartialAmphibiousRetreat(properties);
final Set<UnitCategory> categorized =
UnitSeparator.categorize(
targets,
Expand Down