From c29b4d02d7dfb7a865721a22ef1ae9c8921ad5e6 Mon Sep 17 00:00:00 2001 From: Maxmtg Date: Wed, 27 Mar 2013 20:49:43 +0000 Subject: [PATCH] cleanup in CostPayment --- .../forge/card/cost/CostPartWithList.java | 9 +- .../java/forge/card/cost/CostPayment.java | 153 +++--------------- .../java/forge/card/cost/CostSacrifice.java | 2 +- .../java/forge/card/cost/CostUnattach.java | 2 +- .../SpellAbilityRequirements.java | 26 +-- src/main/java/forge/game/GameActionPlay.java | 8 +- src/main/java/forge/game/GameActionUtil.java | 6 +- src/main/java/forge/game/ai/ComputerUtil.java | 10 +- .../java/forge/game/ai/ComputerUtilMana.java | 2 +- 9 files changed, 57 insertions(+), 161 deletions(-) diff --git a/src/main/java/forge/card/cost/CostPartWithList.java b/src/main/java/forge/card/cost/CostPartWithList.java index b73cc179ff1..e9682e08ce8 100644 --- a/src/main/java/forge/card/cost/CostPartWithList.java +++ b/src/main/java/forge/card/cost/CostPartWithList.java @@ -80,11 +80,10 @@ public abstract class CostPartWithList extends CostPart { * @param hash * the hash */ - public final void addListToHash(final SpellAbility sa) { - final String hash = getHashForList(); - for (final Card card : this.getList()) { - Card copy = CardUtil.getLKICopy(card); - sa.addCostToHashList(copy, hash); + public final void reportPaidCardsTo(final SpellAbility sa) { + final String paymentMethod = getHashForList(); + for (final Card card : this.list) { + sa.addCostToHashList(CardUtil.getLKICopy(card), paymentMethod); } } diff --git a/src/main/java/forge/card/cost/CostPayment.java b/src/main/java/forge/card/cost/CostPayment.java index 17a2e7fb7e8..6017dbde1f1 100644 --- a/src/main/java/forge/card/cost/CostPayment.java +++ b/src/main/java/forge/card/cost/CostPayment.java @@ -22,7 +22,6 @@ import java.util.List; import forge.Card; import forge.card.spellability.SpellAbility; -import forge.card.spellability.SpellAbilityRequirements; import forge.game.GameState; import forge.game.player.AIPlayer; import forge.game.player.Player; @@ -36,12 +35,9 @@ import forge.game.player.Player; * @version $Id$ */ public class CostPayment { - private Cost cost = null; - private SpellAbility ability = null; - private SpellAbilityRequirements req = null; - private boolean bCancel = false; + private final Cost cost; + private final SpellAbility ability; private final ArrayList paidCostParts = new ArrayList(); - private final GameState game; /** *

@@ -54,73 +50,6 @@ public class CostPayment { return this.cost; } - /** - *

- * Getter for the field ability. - *

- * - * @return a {@link forge.card.spellability.SpellAbility} object. - */ - public final SpellAbility getAbility() { - return this.ability; - } - - /** - *

- * Getter for the field card. - *

- * - * @return a {@link forge.Card} object. - */ - public final Card getCard() { - return this.ability.getSourceCard(); - } - - /** - *

- * setRequirements. - *

- * - * @param reqs - * a {@link forge.card.spellability.SpellAbilityRequirements} - * object. - */ - public final void setRequirements(final SpellAbilityRequirements reqs) { - this.req = reqs; - } - - /** - * Gets the requirements. - * - * @return the requirements - */ - public final SpellAbilityRequirements getRequirements() { - return this.req; - } - - /** - *

- * setCancel. - *

- * - * @param cancel - * a boolean. - */ - public final void setCancel(final boolean cancel) { - this.bCancel = cancel; - } - - /** - *

- * isCanceled. - *

- * - * @return a boolean. - */ - public final boolean isCanceled() { - return this.bCancel; - } - /** *

* Constructor for Cost_Payment. @@ -131,10 +60,9 @@ public class CostPayment { * @param abil * a {@link forge.card.spellability.SpellAbility} object. */ - public CostPayment(final Cost cost, final SpellAbility abil, final GameState game) { + public CostPayment(final Cost cost, final SpellAbility abil) { this.cost = cost; this.ability = abil; - this.game = game; } /** @@ -169,33 +97,6 @@ public class CostPayment { return true; } - /** - * Sets the paid mana part. - * - * @param part - * the part - * @param bPaid - * the b paid - */ - public final void setPaidPart(final CostPart part) { - this.paidCostParts.add(part); - } - - /** - * Cancel cost (including CostPart for refunding). - */ - public final void cancelCost(final CostPart part) { - this.setPaidPart(part); - this.cancelCost(); - } - - /** - * Cancel cost. - */ - public final void cancelCost() { - this.setCancel(true); - } - /** *

* payCost. @@ -203,22 +104,26 @@ public class CostPayment { * * @return a boolean. */ - public void payCost() { + public boolean payCost(final GameState game) { for (final CostPart part : this.cost.getCostParts()) { - // This portion of the cost is already paid for, keep moving - if (this.paidCostParts.contains(part)) { - continue; - } - - if ( false == part.payHuman(getAbility(), game) ) { - this.setCancel(true); - return; + if ( false == part.payHuman(this.ability, game) ) { + return false; } + + // abilities care what was used to pay for them if( part instanceof CostPartWithList ) - ((CostPartWithList) part).addListToHash(ability); - setPaidPart(part); + ((CostPartWithList) part).reportPaidCardsTo(ability); + + this.paidCostParts.add(part); } - this.resetUndoList(); + + // this clears lists used for undo. + for (final CostPart part1 : this.paidCostParts) { + if (part1 instanceof CostPartWithList) { + ((CostPartWithList) part1).resetList(); + } + } + return true; } /** @@ -228,7 +133,7 @@ public class CostPayment { * * @return a boolean. */ - public final boolean isAllPaid() { + public final boolean isFullyPaid() { for (final CostPart part : this.cost.getCostParts()) { if (!this.paidCostParts.contains(part)) { return false; @@ -238,28 +143,16 @@ public class CostPayment { return true; } - /** - *

- * resetUndoList. - *

- */ - public final void resetUndoList() { - for (final CostPart part : this.paidCostParts) { - if (part instanceof CostPartWithList) { - ((CostPartWithList) part).resetList(); - } - } - } - /** *

* cancelPayment. *

*/ - public final void cancelPayment() { + public final void refundPayment() { + Card sourceCard = this.ability.getSourceCard(); for (final CostPart part : this.paidCostParts) { if (part.isUndoable()) { - part.refund(this.getCard()); + part.refund(sourceCard); } } diff --git a/src/main/java/forge/card/cost/CostSacrifice.java b/src/main/java/forge/card/cost/CostSacrifice.java index 89a0d61c3af..321ce2c2c99 100644 --- a/src/main/java/forge/card/cost/CostSacrifice.java +++ b/src/main/java/forge/card/cost/CostSacrifice.java @@ -192,7 +192,7 @@ public class CostSacrifice extends CostPartWithList { */ @Override public final void payAI(final AIPlayer ai, final SpellAbility ability, final Card source, final CostPayment payment, final GameState game) { - this.addListToHash(ability); + this.reportPaidCardsTo(ability); for (final Card c : this.getList()) { executePayment(ability, c); } diff --git a/src/main/java/forge/card/cost/CostUnattach.java b/src/main/java/forge/card/cost/CostUnattach.java index e1da452e539..a3b232df141 100644 --- a/src/main/java/forge/card/cost/CostUnattach.java +++ b/src/main/java/forge/card/cost/CostUnattach.java @@ -103,7 +103,7 @@ public class CostUnattach extends CostPartWithList { Card equippingCard = cardToUnattach.getEquipping().get(0); cardToUnattach.unEquipCard(equippingCard); this.addToList(cardToUnattach); - this.addListToHash(ability); + this.reportPaidCardsTo(ability); } /* diff --git a/src/main/java/forge/card/spellability/SpellAbilityRequirements.java b/src/main/java/forge/card/spellability/SpellAbilityRequirements.java index 15f4c28b9ea..3513267975a 100644 --- a/src/main/java/forge/card/spellability/SpellAbilityRequirements.java +++ b/src/main/java/forge/card/spellability/SpellAbilityRequirements.java @@ -26,6 +26,7 @@ import forge.CardCharacteristicName; import forge.Singletons; import forge.card.ability.AbilityUtils; import forge.card.cost.CostPayment; +import forge.game.GameState; import forge.game.zone.Zone; /** @@ -67,20 +68,22 @@ public class SpellAbilityRequirements { } public final void fillRequirements(final boolean skipTargeting) { + final GameState game = Singletons.getModel().getGame(); + if ((this.ability instanceof Spell) && !this.bCasting) { // remove from hand this.bCasting = true; if (!this.ability.getSourceCard().isCopiedSpell()) { final Card c = this.ability.getSourceCard(); - this.fromZone = Singletons.getModel().getGame().getZoneOf(c); + this.fromZone = game.getZoneOf(c); this.zonePosition = this.fromZone.getPosition(c); - this.ability.setSourceCard(Singletons.getModel().getGame().getAction().moveToStack(c)); + this.ability.setSourceCard(game.getAction().moveToStack(c)); } } // freeze Stack. No abilities should go onto the stack while I'm filling requirements. - Singletons.getModel().getGame().getStack().freezeStack(); + game.getStack().freezeStack(); // Announce things like how many times you want to Multikick or the value of X if (!this.announceRequirements()) { @@ -103,30 +106,31 @@ public class SpellAbilityRequirements { } // Payment - if (!this.isFree) { - this.payment.setRequirements(this); + boolean paymentMade = this.isFree; + + if (!paymentMade) { this.payment.changeCost(); - this.payment.payCost(); + paymentMade = this.payment.payCost(game); } - if (this.payment.isCanceled()) { + if (!paymentMade) { rollbackAbility(); return; } - else if (this.isFree || this.payment.isAllPaid()) { + else if (this.isFree || this.payment.isFullyPaid()) { if (this.skipStack) { AbilityUtils.resolve(this.ability, false); } else { this.enusureAbilityHasDescription(this.ability); this.ability.getActivatingPlayer().getManaPool().clearManaPaid(this.ability, false); - Singletons.getModel().getGame().getStack().addAndUnfreeze(this.ability); + game.getStack().addAndUnfreeze(this.ability); } // Warning about this - resolution may come in another thread, and it would still need its targets this.select.clearTargets(); - Singletons.getModel().getGame().getAction().checkStateEffects(); + game.getAction().checkStateEffects(); } } @@ -149,7 +153,7 @@ public class SpellAbilityRequirements { } this.ability.resetOnceResolved(); - this.payment.cancelPayment(); + this.payment.refundPayment(); Singletons.getModel().getGame().getStack().clearFrozen(); // Singletons.getModel().getGame().getStack().removeFromFrozenStack(this.ability); } diff --git a/src/main/java/forge/game/GameActionPlay.java b/src/main/java/forge/game/GameActionPlay.java index 174c721db0a..f44b02dff81 100644 --- a/src/main/java/forge/game/GameActionPlay.java +++ b/src/main/java/forge/game/GameActionPlay.java @@ -74,7 +74,7 @@ public class GameActionPlay { CharmEffect.makeChoices(sa); } final TargetSelection ts = new TargetSelection(sa.getTarget(), sa); - final CostPayment payment = new CostPayment(sa.getPayCosts(), sa, game); + final CostPayment payment = new CostPayment(sa.getPayCosts(), sa); final SpellAbilityRequirements req = new SpellAbilityRequirements(sa, ts, payment); req.setFree(true); @@ -386,9 +386,9 @@ public class GameActionPlay { final TargetSelection ts = new TargetSelection(sa.getTarget(), sa); CostPayment payment = null; if (sa.getPayCosts() == null) { - payment = new CostPayment(new Cost(sa.getSourceCard(), "0", sa.isAbility()), sa, game); + payment = new CostPayment(new Cost(sa.getSourceCard(), "0", sa.isAbility()), sa); } else { - payment = new CostPayment(sa.getPayCosts(), sa, game); + payment = new CostPayment(sa.getPayCosts(), sa); } final SpellAbilityRequirements req = new SpellAbilityRequirements(sa, ts, payment); @@ -431,7 +431,7 @@ public class GameActionPlay { if (sa.getPayCosts() != null) { final TargetSelection ts = new TargetSelection(sa.getTarget(), sa); - final CostPayment payment = new CostPayment(sa.getPayCosts(), sa, game); + final CostPayment payment = new CostPayment(sa.getPayCosts(), sa); if (!sa.isTrigger()) { payment.changeCost(); diff --git a/src/main/java/forge/game/GameActionUtil.java b/src/main/java/forge/game/GameActionUtil.java index 518834dc2a2..74f8f90c632 100644 --- a/src/main/java/forge/game/GameActionUtil.java +++ b/src/main/java/forge/game/GameActionUtil.java @@ -522,7 +522,7 @@ public final class GameActionUtil { for(Card c : toSac) { cpl.executePayment(sourceAbility, c); } - cpl.addListToHash(sourceAbility); + cpl.reportPaidCardsTo(sourceAbility); } else if (part instanceof CostReturn) { @@ -541,7 +541,7 @@ public final class GameActionUtil { for(Card c : inp.getSelected()) { cpl.executePayment(sourceAbility, c); } - cpl.addListToHash(sourceAbility); + cpl.reportPaidCardsTo(sourceAbility); } else if (part instanceof CostDiscard) { @@ -560,7 +560,7 @@ public final class GameActionUtil { for(Card c : inp.getSelected()) { cpl.executePayment(sourceAbility, c); } - cpl.addListToHash(sourceAbility); + cpl.reportPaidCardsTo(sourceAbility); } else if (part instanceof CostPartMana ) { diff --git a/src/main/java/forge/game/ai/ComputerUtil.java b/src/main/java/forge/game/ai/ComputerUtil.java index 1f87304e148..5b218e2d817 100644 --- a/src/main/java/forge/game/ai/ComputerUtil.java +++ b/src/main/java/forge/game/ai/ComputerUtil.java @@ -81,7 +81,7 @@ public class ComputerUtil { if (cost == null && ComputerUtilMana.payManaCost(ai, sa)) { sa.resolve(); } else { - final CostPayment pay = new CostPayment(cost, sa, game); + final CostPayment pay = new CostPayment(cost, sa); if (pay.payComputerCosts(ai, game)) { sa.resolve(); } @@ -108,7 +108,7 @@ public class ComputerUtil { game.getStack().addAndUnfreeze(sa); return true; } else { - final CostPayment pay = new CostPayment(cost, sa, game); + final CostPayment pay = new CostPayment(cost, sa); if (pay.payComputerCosts(ai, game)) { game.getStack().addAndUnfreeze(sa); if (sa.getSplicedCards() != null && !sa.getSplicedCards().isEmpty()) { @@ -216,7 +216,7 @@ public class ComputerUtil { ComputerUtilMana.payManaCost(ai, sa); game.getStack().add(sa); } else { - final CostPayment pay = new CostPayment(cost, sa, game); + final CostPayment pay = new CostPayment(cost, sa); if (pay.payComputerCosts(ai, game)) { game.getStack().add(sa); } @@ -263,7 +263,7 @@ public class ComputerUtil { newSA.setSourceCard(game.getAction().moveToStack(source)); } - final CostPayment pay = new CostPayment(newSA.getPayCosts(), newSA, game); + final CostPayment pay = new CostPayment(newSA.getPayCosts(), newSA); pay.payComputerCosts(ai, game); game.getStack().add(newSA); @@ -291,7 +291,7 @@ public class ComputerUtil { if (cost == null) { ComputerUtilMana.payManaCost(ai, sa); } else { - final CostPayment pay = new CostPayment(cost, sa, game); + final CostPayment pay = new CostPayment(cost, sa); pay.payComputerCosts((AIPlayer)ai, game); } diff --git a/src/main/java/forge/game/ai/ComputerUtilMana.java b/src/main/java/forge/game/ai/ComputerUtilMana.java index 6d36605b3d2..b85b2e725b2 100644 --- a/src/main/java/forge/game/ai/ComputerUtilMana.java +++ b/src/main/java/forge/game/ai/ComputerUtilMana.java @@ -185,7 +185,7 @@ public class ComputerUtilMana { if (!test) { // Pay additional costs if (ma.getPayCosts() != null) { - final CostPayment pay = new CostPayment(ma.getPayCosts(), ma, game); + final CostPayment pay = new CostPayment(ma.getPayCosts(), ma); if (!pay.payComputerCosts((AIPlayer)ai, game)) { continue; }