Skip to content

Commit

Permalink
Remove special handling of battle logic for edit mode. (#12529)
Browse files Browse the repository at this point in the history
This logic was just adding code complexity and some bugs and it's not clear why it's needed.

(It was making so no casualties would ever happen during battles when edit mode is on.)

Fixes: #12488
(And the duplicate issues.)
  • Loading branch information
asvitkine committed Apr 21, 2024
1 parent 2638089 commit 186c994
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 241 deletions.
Expand Up @@ -25,7 +25,6 @@
import games.strategy.triplea.delegate.DiceRoll;
import games.strategy.triplea.delegate.Die;
import games.strategy.triplea.delegate.Die.DieType;
import games.strategy.triplea.delegate.EditDelegate;
import games.strategy.triplea.delegate.ExecutionStack;
import games.strategy.triplea.delegate.IExecutable;
import games.strategy.triplea.delegate.Matches;
Expand Down Expand Up @@ -454,7 +453,6 @@ class FireAa implements IExecutable {

@Override
public void execute(final ExecutionStack stack, final IDelegateBridge bridge) {
final boolean isEditMode = EditDelegate.getEditMode(bridge.getData().getProperties());
for (final String currentTypeAa : aaTypes) {
final Collection<Unit> currentPossibleAa =
CollectionUtils.getMatches(defendingAa, Matches.unitIsAaOfTypeAa(currentTypeAa));
Expand Down Expand Up @@ -565,9 +563,7 @@ public void execute(final ExecutionStack stack, final IDelegateBridge bridge) {
stack.push(removeHits);
stack.push(notifyCasualties);
stack.push(calculateCasualties);
if (!isEditMode) {
stack.push(roll);
}
stack.push(roll);
}
}
}
Expand Down Expand Up @@ -696,109 +692,47 @@ public void execute(final ExecutionStack stack, final IDelegateBridge bridge) {
}

private void rollDice(final IDelegateBridge bridge) {

final int rollCount = getSbrRolls(attackingUnits, attacker);
if (rollCount == 0) {
dice = null;
return;
}
dice = new int[rollCount];
final boolean isEditMode = EditDelegate.getEditMode(gameData.getProperties());
if (isEditMode) {
final String annotation =
attacker.getName()
+ " fixing dice to allocate cost of strategic bombing raid against "
+ defender.getName()
+ " in "
+ battleSite.getName();
final Player attacker = bridge.getRemotePlayer(StrategicBombingRaidBattle.this.attacker);
// does not take into account bombers with dice sides higher than getDiceSides
dice = attacker.selectFixedDice(rollCount, 0, annotation, gameData.getDiceSides());
} else {
final boolean doNotUseBombingBonus =
!Properties.getUseBombingMaxDiceSidesAndBonus(gameData.getProperties());
final String annotation =
attacker.getName()
+ " rolling to allocate cost of strategic bombing raid against "
+ defender.getName()
+ " in "
+ battleSite.getName();
if (!Properties.getLowLuckDamageOnly(gameData.getProperties())) {
if (doNotUseBombingBonus) {
// no low luck, and no bonus, so just roll based on the map's dice sides
dice =
bridge.getRandom(
gameData.getDiceSides(), rollCount, attacker, DiceType.BOMBING, annotation);
} else {
// we must use bombing bonus
int i = 0;
final int diceSides = gameData.getDiceSides();
for (final Unit u : attackingUnits) {
final int rolls = getSbrRolls(u, attacker);
if (rolls < 1) {
continue;
}
final UnitAttachment ua = u.getUnitAttachment();
int maxDice = ua.getBombingMaxDieSides();
final int bonus = ua.getBombingBonus();
// both could be -1, meaning they were not set. if they were not set, then we use
// default dice sides for
// the map, and zero for the bonus.
if (maxDice < 0) {
maxDice = diceSides;
}
// now we roll, or don't if there is nothing to roll.
if (maxDice > 0) {
final int[] diceRolls =
bridge.getRandom(maxDice, rolls, attacker, DiceType.BOMBING, annotation);
for (final int die : diceRolls) {
// min value is -1 as we add 1 when setting damage since dice are 0 instead of 1
// based
dice[i] = Math.max(-1, die + bonus);
i++;
}
} else {
for (int j = 0; j < rolls; j++) {
// min value is -1 as we add 1 when setting damage since dice are 0 instead of 1
// based
dice[i] = Math.max(-1, bonus);
i++;
}
}
}
}

final boolean doNotUseBombingBonus =
!Properties.getUseBombingMaxDiceSidesAndBonus(gameData.getProperties());
final String annotation =
attacker.getName()
+ " rolling to allocate cost of strategic bombing raid against "
+ defender.getName()
+ " in "
+ battleSite.getName();
if (!Properties.getLowLuckDamageOnly(gameData.getProperties())) {
final int diceSides = gameData.getDiceSides();
if (doNotUseBombingBonus) {
// no low luck, and no bonus, so just roll based on the map's dice sides
dice = bridge.getRandom(diceSides, rollCount, attacker, DiceType.BOMBING, annotation);
} else {
// we must use bombing bonus
int i = 0;
final int diceSides = gameData.getDiceSides();
for (final Unit u : attackingUnits) {
final int rolls = getSbrRolls(u, attacker);
if (rolls < 1) {
continue;
}
final UnitAttachment ua = u.getUnitAttachment();
int maxDice = ua.getBombingMaxDieSides();
int bonus = ua.getBombingBonus();
final int bonus = ua.getBombingBonus();
// both could be -1, meaning they were not set. if they were not set, then we use
// default dice sides for the
// map, and zero for the bonus.
if (maxDice < 0 || doNotUseBombingBonus) {
// default dice sides for the map, and zero for the bonus.
if (maxDice < 0) {
maxDice = diceSides;
}
if (doNotUseBombingBonus) {
bonus = 0;
}
// now, regardless of whether they were set or not, we have to apply "low luck" to them,
// meaning in this
// case that we reduce the luck by 2/3.
if (maxDice >= 5) {
bonus += (maxDice + 1) / 3;
maxDice = (maxDice + 1) / 3;
}
// now we roll, or don't if there is nothing to roll.
if (maxDice > 0) {
final int[] dicerolls =
final int[] diceRolls =
bridge.getRandom(maxDice, rolls, attacker, DiceType.BOMBING, annotation);
for (final int die : dicerolls) {
for (final int die : diceRolls) {
// min value is -1 as we add 1 when setting damage since dice are 0 instead of 1
// based
dice[i] = Math.max(-1, die + bonus);
Expand All @@ -814,6 +748,50 @@ private void rollDice(final IDelegateBridge bridge) {
}
}
}
} else {
int i = 0;
final int diceSides = gameData.getDiceSides();
for (final Unit u : attackingUnits) {
final int rolls = getSbrRolls(u, attacker);
if (rolls < 1) {
continue;
}
final UnitAttachment ua = u.getUnitAttachment();
int maxDice = ua.getBombingMaxDieSides();
int bonus = ua.getBombingBonus();
// both could be -1, meaning they were not set. if they were not set, then we use
// default dice sides for the map, and zero for the bonus.
if (maxDice < 0 || doNotUseBombingBonus) {
maxDice = diceSides;
}
if (doNotUseBombingBonus) {
bonus = 0;
}
// now, regardless of whether they were set or not, we have to apply "low luck" to them,
// meaning in this case that we reduce the luck by 2/3.
if (maxDice >= 5) {
bonus += (maxDice + 1) / 3;
maxDice = (maxDice + 1) / 3;
}
// now we roll, or don't if there is nothing to roll.
if (maxDice > 0) {
final int[] dicerolls =
bridge.getRandom(maxDice, rolls, attacker, DiceType.BOMBING, annotation);
for (final int die : dicerolls) {
// min value is -1 as we add 1 when setting damage since dice are 0 instead of 1
// based
dice[i] = Math.max(-1, die + bonus);
i++;
}
} else {
for (int j = 0; j < rolls; j++) {
// min value is -1 as we add 1 when setting damage since dice are 0 instead of 1
// based
dice[i] = Math.max(-1, bonus);
i++;
}
}
}
}
}

Expand Down
Expand Up @@ -10,7 +10,6 @@
import games.strategy.triplea.Properties;
import games.strategy.triplea.delegate.DiceRoll;
import games.strategy.triplea.delegate.Die.DieType;
import games.strategy.triplea.delegate.EditDelegate;
import games.strategy.triplea.delegate.Matches;
import games.strategy.triplea.delegate.data.CasualtyDetails;
import games.strategy.triplea.delegate.power.calculator.AaPowerStrengthAndRolls;
Expand Down Expand Up @@ -49,8 +48,7 @@ public static CasualtyDetails getAaCasualties(
!defendingAa.isEmpty()
&& defendingAa.stream()
.allMatch(Matches.unitAaShotDamageableInsteadOfKillingInstantly());
if (EditDelegate.getEditMode(data.getProperties())
|| Properties.getChooseAaCasualties(data.getProperties())) {
if (Properties.getChooseAaCasualties(data.getProperties())) {
return CasualtySelector.selectCasualties(
hitPlayer,
planes,
Expand Down
Expand Up @@ -11,7 +11,6 @@
import games.strategy.triplea.ai.weak.WeakAi;
import games.strategy.triplea.attachments.UnitAttachment;
import games.strategy.triplea.delegate.DiceRoll;
import games.strategy.triplea.delegate.EditDelegate;
import games.strategy.triplea.delegate.Matches;
import games.strategy.triplea.delegate.data.CasualtyDetails;
import games.strategy.triplea.delegate.data.CasualtyList;
Expand Down Expand Up @@ -77,24 +76,6 @@ public static CasualtyDetails selectCasualties(
? extraHits
: dice.getHits();

if (EditDelegate.getEditMode(data.getProperties())) {
return tripleaPlayer.selectCasualties(
targetsToPickFrom,
dependents,
hitsRemaining,
text,
dice,
player,
combatValue.getFriendUnits(),
combatValue.getEnemyUnits(),
false,
List.of(),
new CasualtyDetails(),
battleId,
battleSite,
allowMultipleHitsPerUnit);
}

if (dice.getHits() == 0) {
return new CasualtyDetails();
}
Expand Down
Expand Up @@ -7,7 +7,6 @@
import games.strategy.engine.data.Unit;
import games.strategy.engine.delegate.IDelegateBridge;
import games.strategy.triplea.Properties;
import games.strategy.triplea.delegate.EditDelegate;
import games.strategy.triplea.delegate.Matches;
import games.strategy.triplea.delegate.battle.casualty.CasualtySelector;
import games.strategy.triplea.delegate.data.CasualtyDetails;
Expand Down Expand Up @@ -46,12 +45,7 @@ public CasualtyDetails apply(final IDelegateBridge bridge, final SelectCasualtie
final int hitsLeftForRestrictedTransports = hitCount - totalHitPointsAvailable;

final CasualtyDetails casualtyDetails;
if (EditDelegate.getEditMode(step.getBattleState().getGameData().getProperties())) {
final CasualtyDetails message =
selectFunction.apply(bridge, step, step.getFiringGroup().getTargetUnits(), 0);
casualtyDetails = new CasualtyDetails(message, true);

} else if (totalHitPointsAvailable > hitCount) {
if (totalHitPointsAvailable > hitCount) {
// not all units were hit so the player needs to pick which ones are killed
casualtyDetails =
selectFunction.apply(
Expand Down
Expand Up @@ -12,7 +12,6 @@
import games.strategy.engine.data.Unit;
import games.strategy.triplea.delegate.DiceRoll;
import games.strategy.triplea.delegate.Die;
import games.strategy.triplea.delegate.EditDelegate;
import games.strategy.triplea.delegate.Matches;
import games.strategy.triplea.delegate.TerritoryEffectHelper;
import games.strategy.triplea.delegate.battle.BattleState;
Expand Down Expand Up @@ -257,10 +256,7 @@ void casualtyNotification(
final Collection<Unit> damaged,
final Map<Unit, Collection<Unit>> dependents) {
setStep(step);
final boolean isEditMode = (dice == null);
if (!isEditMode) {
dicePanel.setDiceRoll(dice);
}
dicePanel.setDiceRoll(dice);
casualties.setNotification(killed, damaged, dependents);
killed.addAll(updateKilledUnits(killed, player));
if (player.equals(defender)) {
Expand Down Expand Up @@ -477,15 +473,11 @@ CasualtyDetails getCasualties(

SwingUtilities.invokeLater(
() -> {
final boolean isEditMode = EditDelegate.getEditMode(gameData.getProperties());
if (!isEditMode) {
dicePanel.setDiceRoll(dice);
casualties.setVisible(false);
}
final boolean plural = isEditMode || (count > 1);
final String countStr = isEditMode ? "" : "" + count;
dicePanel.setDiceRoll(dice);
casualties.setVisible(false);
final boolean plural = (count > 1);
final String btnText =
hit.getName() + " select " + countStr + (plural ? " casualties" : " casualty");
hit.getName() + " select " + count + (plural ? " casualties" : " casualty");
final var casualtySelection =
new CasualtySelection(
selectFrom,
Expand All @@ -496,8 +488,7 @@ CasualtyDetails getCasualties(
defaultCasualties,
allowMultipleHitsPerUnit,
uiContext,
BattleDisplay.this,
isEditMode);
BattleDisplay.this);

actionButton.setAction(
new AbstractAction(btnText) {
Expand Down
Expand Up @@ -40,7 +40,6 @@ public class CasualtySelection {
private final GamePlayer player;

private final JDialog dialog;
private final boolean isEditMode;

private final JOptionPane optionPane;

Expand All @@ -53,13 +52,10 @@ public CasualtySelection(
final CasualtyList defaultCasualties,
final boolean allowMultipleHitsPerUnit,
final UiContext uiContext,
final Component dialogParent,
final boolean isEditMode) {
final Component dialogParent) {
this.hitsToTake = hitsToTake;
this.player = player;

this.isEditMode = isEditMode;

final boolean movementForAirUnitsOnly =
playerMayChooseToDistributeHitsToUnitsWithDifferentMovement(selectFrom);

Expand All @@ -73,12 +69,7 @@ public CasualtySelection(
allowMultipleHitsPerUnit,
uiContext);
chooser.setTitle(title);

if (isEditMode) {
chooser.disableMax();
} else {
chooser.setMax(hitsToTake);
}
chooser.setMax(hitsToTake);

final JScrollPane chooserScrollPane = new JScrollPane(chooser);
chooserScrollPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
Expand Down Expand Up @@ -120,7 +111,7 @@ public Optional<CasualtyDetails> showModalDialog() {

final List<Unit> killed = chooser.getSelected(false);
final List<Unit> damaged = chooser.getSelectedDamagedMultipleHitPointUnits();
if (!isEditMode && (killed.size() + damaged.size() != hitsToTake)) {
if (killed.size() + damaged.size() != hitsToTake) {
JOptionPane.showMessageDialog(
dialog /*.getParent()*/,
"Wrong number of casualties selected",
Expand Down

0 comments on commit 186c994

Please sign in to comment.