Skip to content

Commit

Permalink
Fix purchase persisting on max built error. (#12670)
Browse files Browse the repository at this point in the history
The fix in #11818 regressed with the changes for saving purchases in saved games. This restores it.

Also fixes the error message to not display a negative max limit.
  • Loading branch information
asvitkine committed Jun 26, 2024
1 parent d7b3134 commit 1374e47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import games.strategy.triplea.attachments.TechAbilityAttachment;
import games.strategy.triplea.attachments.TerritoryAttachment;
import games.strategy.triplea.attachments.TriggerAttachment;
import games.strategy.triplea.attachments.UnitAttachment;
import games.strategy.triplea.attachments.UnitTypeComparator;
import games.strategy.triplea.delegate.remote.IAbstractForumPosterDelegate;
import games.strategy.triplea.delegate.remote.IPurchaseDelegate;
Expand Down Expand Up @@ -174,8 +173,7 @@ protected boolean canAfford(final IntegerMap<Resource> costs, final GamePlayer p
if (!(next instanceof Resource)) {
final UnitType type = (UnitType) next;
final int quantity = results.getInt(type);
final UnitAttachment ua = type.getUnitAttachment();
final int maxBuilt = ua.getMaxBuiltPerPlayer();
final int maxBuilt = type.getUnitAttachment().getMaxBuiltPerPlayer();
if (maxBuilt == 0) {
return "May not build any of this unit right now: " + type.getName();
} else if (maxBuilt > 0) {
Expand All @@ -184,13 +182,13 @@ protected boolean canAfford(final IntegerMap<Resource> costs, final GamePlayer p

final Predicate<Unit> unitTypeOwnedBy =
Matches.unitIsOfType(type).and(Matches.unitIsOwnedBy(player));
final Collection<Territory> allTerrs = getData().getMap().getTerritories();
for (final Territory t : allTerrs) {
for (final Territory t : getData().getMap().getTerritories()) {
currentlyBuilt += t.getUnitCollection().countMatches(unitTypeOwnedBy);
}

final int allowedBuild = maxBuilt - currentlyBuilt;
if (allowedBuild - quantity < 0) {
// Use Math.max(0, ...) to avoid negative if existing count exceeds limit.
final int allowedBuild = Math.max(0, maxBuilt - currentlyBuilt);
if (quantity > allowedBuild) {
return String.format(
"May only build %s of %s this turn, may only build %s total",
allowedBuild, type.getName(), maxBuilt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class PurchasePanel extends ActionPanel {
private static final String BUY = "Buy...";
private static final String CHANGE = "Change...";

private IntegerMap<ProductionRule> purchase;
private IntegerMap<ProductionRule> purchase = new IntegerMap<>();
private boolean bid;
private final SimpleUnitPanel purchasedPreviousRoundsUnits;
private final JLabel purchasedPreviousRoundsLabel;
Expand All @@ -61,10 +61,10 @@ public void actionPerformed(final ActionEvent e) {
// Use the delegate from the step, since it may not actually be named 'purchase'.
final IDelegate delegate = data.getSequence().getStep().getDelegate();
if (delegate instanceof PurchaseDelegate) {
purchase = ((PurchaseDelegate) delegate).getPendingProductionRules();
}
if (purchase == null) {
purchase = new IntegerMap<>();
final var savedPurchase = ((PurchaseDelegate) delegate).getPendingProductionRules();
if (savedPurchase != null) {
purchase = savedPurchase;
}
}

purchase =
Expand Down Expand Up @@ -112,7 +112,7 @@ public void display(final GamePlayer gamePlayer) {
if (keepCurrentPurchase) {
keepCurrentPurchase = false;
} else {
purchase = new IntegerMap<>();
purchase.clear();
}
SwingUtilities.invokeLater(
() -> {
Expand Down

0 comments on commit 1374e47

Please sign in to comment.