mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-15 18:28:00 +00:00
cleanup in CostPayment
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<CostPart> paidCostParts = new ArrayList<CostPart>();
|
||||
private final GameState game;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -54,73 +50,6 @@ public class CostPayment {
|
||||
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>
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* resetUndoList.
|
||||
* </p>
|
||||
*/
|
||||
public final void resetUndoList() {
|
||||
for (final CostPart part : this.paidCostParts) {
|
||||
if (part instanceof CostPartWithList) {
|
||||
((CostPartWithList) part).resetList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* cancelPayment.
|
||||
* </p>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 ) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user