cleanup in CostPayment

This commit is contained in:
Maxmtg
2013-03-27 20:49:43 +00:00
parent 0d143f055b
commit c29b4d02d7
9 changed files with 57 additions and 161 deletions

View File

@@ -80,11 +80,10 @@ public abstract class CostPartWithList extends CostPart {
* @param hash * @param hash
* the hash * the hash
*/ */
public final void addListToHash(final SpellAbility sa) { public final void reportPaidCardsTo(final SpellAbility sa) {
final String hash = getHashForList(); final String paymentMethod = getHashForList();
for (final Card card : this.getList()) { for (final Card card : this.list) {
Card copy = CardUtil.getLKICopy(card); sa.addCostToHashList(CardUtil.getLKICopy(card), paymentMethod);
sa.addCostToHashList(copy, hash);
} }
} }

View File

@@ -22,7 +22,6 @@ import java.util.List;
import forge.Card; import forge.Card;
import forge.card.spellability.SpellAbility; import forge.card.spellability.SpellAbility;
import forge.card.spellability.SpellAbilityRequirements;
import forge.game.GameState; import forge.game.GameState;
import forge.game.player.AIPlayer; import forge.game.player.AIPlayer;
import forge.game.player.Player; import forge.game.player.Player;
@@ -36,12 +35,9 @@ import forge.game.player.Player;
* @version $Id$ * @version $Id$
*/ */
public class CostPayment { public class CostPayment {
private Cost cost = null; private final Cost cost;
private SpellAbility ability = null; private final SpellAbility ability;
private SpellAbilityRequirements req = null;
private boolean bCancel = false;
private final ArrayList<CostPart> paidCostParts = new ArrayList<CostPart>(); private final ArrayList<CostPart> paidCostParts = new ArrayList<CostPart>();
private final GameState game;
/** /**
* <p> * <p>
@@ -54,73 +50,6 @@ public class CostPayment {
return this.cost; return this.cost;
} }
/**
* <p>
* Getter for the field <code>ability</code>.
* </p>
*
* @return a {@link forge.card.spellability.SpellAbility} object.
*/
public final SpellAbility getAbility() {
return this.ability;
}
/**
* <p>
* Getter for the field <code>card</code>.
* </p>
*
* @return a {@link forge.Card} object.
*/
public final Card getCard() {
return this.ability.getSourceCard();
}
/**
* <p>
* setRequirements.
* </p>
*
* @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;
}
/**
* <p>
* setCancel.
* </p>
*
* @param cancel
* a boolean.
*/
public final void setCancel(final boolean cancel) {
this.bCancel = cancel;
}
/**
* <p>
* isCanceled.
* </p>
*
* @return a boolean.
*/
public final boolean isCanceled() {
return this.bCancel;
}
/** /**
* <p> * <p>
* Constructor for Cost_Payment. * Constructor for Cost_Payment.
@@ -131,10 +60,9 @@ public class CostPayment {
* @param abil * @param abil
* a {@link forge.card.spellability.SpellAbility} object. * 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.cost = cost;
this.ability = abil; this.ability = abil;
this.game = game;
} }
/** /**
@@ -169,33 +97,6 @@ public class CostPayment {
return true; 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);
}
/** /**
* <p> * <p>
* payCost. * payCost.
@@ -203,22 +104,26 @@ public class CostPayment {
* *
* @return a boolean. * @return a boolean.
*/ */
public void payCost() { public boolean payCost(final GameState game) {
for (final CostPart part : this.cost.getCostParts()) { for (final CostPart part : this.cost.getCostParts()) {
// This portion of the cost is already paid for, keep moving if ( false == part.payHuman(this.ability, game) ) {
if (this.paidCostParts.contains(part)) { return false;
continue;
} }
if ( false == part.payHuman(getAbility(), game) ) { // abilities care what was used to pay for them
this.setCancel(true);
return;
}
if( part instanceof CostPartWithList ) if( part instanceof CostPartWithList )
((CostPartWithList) part).addListToHash(ability); ((CostPartWithList) part).reportPaidCardsTo(ability);
setPaidPart(part);
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. * @return a boolean.
*/ */
public final boolean isAllPaid() { public final boolean isFullyPaid() {
for (final CostPart part : this.cost.getCostParts()) { for (final CostPart part : this.cost.getCostParts()) {
if (!this.paidCostParts.contains(part)) { if (!this.paidCostParts.contains(part)) {
return false; return false;
@@ -238,28 +143,16 @@ public class CostPayment {
return true; return true;
} }
/**
* <p>
* resetUndoList.
* </p>
*/
public final void resetUndoList() {
for (final CostPart part : this.paidCostParts) {
if (part instanceof CostPartWithList) {
((CostPartWithList) part).resetList();
}
}
}
/** /**
* <p> * <p>
* cancelPayment. * cancelPayment.
* </p> * </p>
*/ */
public final void cancelPayment() { public final void refundPayment() {
Card sourceCard = this.ability.getSourceCard();
for (final CostPart part : this.paidCostParts) { for (final CostPart part : this.paidCostParts) {
if (part.isUndoable()) { if (part.isUndoable()) {
part.refund(this.getCard()); part.refund(sourceCard);
} }
} }

View File

@@ -192,7 +192,7 @@ public class CostSacrifice extends CostPartWithList {
*/ */
@Override @Override
public final void payAI(final AIPlayer ai, final SpellAbility ability, final Card source, final CostPayment payment, final GameState game) { 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()) { for (final Card c : this.getList()) {
executePayment(ability, c); executePayment(ability, c);
} }

View File

@@ -103,7 +103,7 @@ public class CostUnattach extends CostPartWithList {
Card equippingCard = cardToUnattach.getEquipping().get(0); Card equippingCard = cardToUnattach.getEquipping().get(0);
cardToUnattach.unEquipCard(equippingCard); cardToUnattach.unEquipCard(equippingCard);
this.addToList(cardToUnattach); this.addToList(cardToUnattach);
this.addListToHash(ability); this.reportPaidCardsTo(ability);
} }
/* /*

View File

@@ -26,6 +26,7 @@ import forge.CardCharacteristicName;
import forge.Singletons; import forge.Singletons;
import forge.card.ability.AbilityUtils; import forge.card.ability.AbilityUtils;
import forge.card.cost.CostPayment; import forge.card.cost.CostPayment;
import forge.game.GameState;
import forge.game.zone.Zone; import forge.game.zone.Zone;
/** /**
@@ -67,20 +68,22 @@ public class SpellAbilityRequirements {
} }
public final void fillRequirements(final boolean skipTargeting) { public final void fillRequirements(final boolean skipTargeting) {
final GameState game = Singletons.getModel().getGame();
if ((this.ability instanceof Spell) && !this.bCasting) { if ((this.ability instanceof Spell) && !this.bCasting) {
// remove from hand // remove from hand
this.bCasting = true; this.bCasting = true;
if (!this.ability.getSourceCard().isCopiedSpell()) { if (!this.ability.getSourceCard().isCopiedSpell()) {
final Card c = this.ability.getSourceCard(); 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.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. // 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 // Announce things like how many times you want to Multikick or the value of X
if (!this.announceRequirements()) { if (!this.announceRequirements()) {
@@ -103,30 +106,31 @@ public class SpellAbilityRequirements {
} }
// Payment // Payment
if (!this.isFree) { boolean paymentMade = this.isFree;
this.payment.setRequirements(this);
if (!paymentMade) {
this.payment.changeCost(); this.payment.changeCost();
this.payment.payCost(); paymentMade = this.payment.payCost(game);
} }
if (this.payment.isCanceled()) { if (!paymentMade) {
rollbackAbility(); rollbackAbility();
return; return;
} }
else if (this.isFree || this.payment.isAllPaid()) { else if (this.isFree || this.payment.isFullyPaid()) {
if (this.skipStack) { if (this.skipStack) {
AbilityUtils.resolve(this.ability, false); AbilityUtils.resolve(this.ability, false);
} else { } else {
this.enusureAbilityHasDescription(this.ability); this.enusureAbilityHasDescription(this.ability);
this.ability.getActivatingPlayer().getManaPool().clearManaPaid(this.ability, false); 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 // Warning about this - resolution may come in another thread, and it would still need its targets
this.select.clearTargets(); this.select.clearTargets();
Singletons.getModel().getGame().getAction().checkStateEffects(); game.getAction().checkStateEffects();
} }
} }
@@ -149,7 +153,7 @@ public class SpellAbilityRequirements {
} }
this.ability.resetOnceResolved(); this.ability.resetOnceResolved();
this.payment.cancelPayment(); this.payment.refundPayment();
Singletons.getModel().getGame().getStack().clearFrozen(); Singletons.getModel().getGame().getStack().clearFrozen();
// Singletons.getModel().getGame().getStack().removeFromFrozenStack(this.ability); // Singletons.getModel().getGame().getStack().removeFromFrozenStack(this.ability);
} }

View File

@@ -74,7 +74,7 @@ public class GameActionPlay {
CharmEffect.makeChoices(sa); CharmEffect.makeChoices(sa);
} }
final TargetSelection ts = new TargetSelection(sa.getTarget(), 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); final SpellAbilityRequirements req = new SpellAbilityRequirements(sa, ts, payment);
req.setFree(true); req.setFree(true);
@@ -386,9 +386,9 @@ public class GameActionPlay {
final TargetSelection ts = new TargetSelection(sa.getTarget(), sa); final TargetSelection ts = new TargetSelection(sa.getTarget(), sa);
CostPayment payment = null; CostPayment payment = null;
if (sa.getPayCosts() == 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 { } else {
payment = new CostPayment(sa.getPayCosts(), sa, game); payment = new CostPayment(sa.getPayCosts(), sa);
} }
final SpellAbilityRequirements req = new SpellAbilityRequirements(sa, ts, payment); final SpellAbilityRequirements req = new SpellAbilityRequirements(sa, ts, payment);
@@ -431,7 +431,7 @@ public class GameActionPlay {
if (sa.getPayCosts() != null) { if (sa.getPayCosts() != null) {
final TargetSelection ts = new TargetSelection(sa.getTarget(), 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);
if (!sa.isTrigger()) { if (!sa.isTrigger()) {
payment.changeCost(); payment.changeCost();

View File

@@ -522,7 +522,7 @@ public final class GameActionUtil {
for(Card c : toSac) { for(Card c : toSac) {
cpl.executePayment(sourceAbility, c); cpl.executePayment(sourceAbility, c);
} }
cpl.addListToHash(sourceAbility); cpl.reportPaidCardsTo(sourceAbility);
} }
else if (part instanceof CostReturn) { else if (part instanceof CostReturn) {
@@ -541,7 +541,7 @@ public final class GameActionUtil {
for(Card c : inp.getSelected()) { for(Card c : inp.getSelected()) {
cpl.executePayment(sourceAbility, c); cpl.executePayment(sourceAbility, c);
} }
cpl.addListToHash(sourceAbility); cpl.reportPaidCardsTo(sourceAbility);
} }
else if (part instanceof CostDiscard) { else if (part instanceof CostDiscard) {
@@ -560,7 +560,7 @@ public final class GameActionUtil {
for(Card c : inp.getSelected()) { for(Card c : inp.getSelected()) {
cpl.executePayment(sourceAbility, c); cpl.executePayment(sourceAbility, c);
} }
cpl.addListToHash(sourceAbility); cpl.reportPaidCardsTo(sourceAbility);
} }
else if (part instanceof CostPartMana ) { else if (part instanceof CostPartMana ) {

View File

@@ -81,7 +81,7 @@ public class ComputerUtil {
if (cost == null && ComputerUtilMana.payManaCost(ai, sa)) { if (cost == null && ComputerUtilMana.payManaCost(ai, sa)) {
sa.resolve(); sa.resolve();
} else { } else {
final CostPayment pay = new CostPayment(cost, sa, game); final CostPayment pay = new CostPayment(cost, sa);
if (pay.payComputerCosts(ai, game)) { if (pay.payComputerCosts(ai, game)) {
sa.resolve(); sa.resolve();
} }
@@ -108,7 +108,7 @@ public class ComputerUtil {
game.getStack().addAndUnfreeze(sa); game.getStack().addAndUnfreeze(sa);
return true; return true;
} else { } else {
final CostPayment pay = new CostPayment(cost, sa, game); final CostPayment pay = new CostPayment(cost, sa);
if (pay.payComputerCosts(ai, game)) { if (pay.payComputerCosts(ai, game)) {
game.getStack().addAndUnfreeze(sa); game.getStack().addAndUnfreeze(sa);
if (sa.getSplicedCards() != null && !sa.getSplicedCards().isEmpty()) { if (sa.getSplicedCards() != null && !sa.getSplicedCards().isEmpty()) {
@@ -216,7 +216,7 @@ public class ComputerUtil {
ComputerUtilMana.payManaCost(ai, sa); ComputerUtilMana.payManaCost(ai, sa);
game.getStack().add(sa); game.getStack().add(sa);
} else { } else {
final CostPayment pay = new CostPayment(cost, sa, game); final CostPayment pay = new CostPayment(cost, sa);
if (pay.payComputerCosts(ai, game)) { if (pay.payComputerCosts(ai, game)) {
game.getStack().add(sa); game.getStack().add(sa);
} }
@@ -263,7 +263,7 @@ public class ComputerUtil {
newSA.setSourceCard(game.getAction().moveToStack(source)); 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); pay.payComputerCosts(ai, game);
game.getStack().add(newSA); game.getStack().add(newSA);
@@ -291,7 +291,7 @@ public class ComputerUtil {
if (cost == null) { if (cost == null) {
ComputerUtilMana.payManaCost(ai, sa); ComputerUtilMana.payManaCost(ai, sa);
} else { } else {
final CostPayment pay = new CostPayment(cost, sa, game); final CostPayment pay = new CostPayment(cost, sa);
pay.payComputerCosts((AIPlayer)ai, game); pay.payComputerCosts((AIPlayer)ai, game);
} }

View File

@@ -185,7 +185,7 @@ public class ComputerUtilMana {
if (!test) { if (!test) {
// Pay additional costs // Pay additional costs
if (ma.getPayCosts() != null) { 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)) { if (!pay.payComputerCosts((AIPlayer)ai, game)) {
continue; continue;
} }