CostPutCounter - input is based on select cards

CostRemoveCounter removed a variable (using a field from super class instead)
This commit is contained in:
Maxmtg
2013-05-19 20:50:30 +00:00
parent 0936352729
commit b78cc19e1c
2 changed files with 79 additions and 63 deletions

View File

@@ -17,21 +17,21 @@
*/ */
package forge.card.cost; package forge.card.cost;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import forge.Card; import forge.Card;
import forge.CardLists; import forge.CardLists;
import forge.CounterType; import forge.CounterType;
import forge.FThreads; import forge.FThreads;
import forge.Singletons;
import forge.card.ability.AbilityUtils; import forge.card.ability.AbilityUtils;
import forge.card.spellability.SpellAbility; import forge.card.spellability.SpellAbility;
import forge.control.input.InputPayment; import forge.control.input.InputSelectCards;
import forge.control.input.InputSyncronizedBase;
import forge.game.GameState; import forge.game.GameState;
import forge.game.ai.ComputerUtilCard; import forge.game.ai.ComputerUtilCard;
import forge.game.player.Player; import forge.game.player.Player;
import forge.game.zone.ZoneType; import forge.game.zone.ZoneType;
import forge.view.ButtonUtil;
/** /**
* The Class CostPutCounter. * The Class CostPutCounter.
@@ -41,14 +41,11 @@ public class CostPutCounter extends CostPartWithList {
* TODO: Write javadoc for this type. * TODO: Write javadoc for this type.
* *
*/ */
public static final class InputPayCostPutCounter extends InputSyncronizedBase implements InputPayment { public static final class InputPayCostPutCounter extends InputSelectCards {
private final String type;
private final CostPutCounter costPutCounter;
private final int nNeeded;
private final SpellAbility sa;
private static final long serialVersionUID = 2685832214519141903L; private static final long serialVersionUID = 2685832214519141903L;
private List<Card> typeList; private List<Card> validChoices;
private int nPut = 0; private final Map<Card,Integer> cardsChosen;
/** /**
* TODO: Write javadoc for Constructor. * TODO: Write javadoc for Constructor.
@@ -58,61 +55,63 @@ public class CostPutCounter extends CostPartWithList {
* @param payment * @param payment
* @param sa * @param sa
*/ */
public InputPayCostPutCounter(String type, CostPutCounter costPutCounter, int nNeeded, SpellAbility sa) { public InputPayCostPutCounter(int cntCounters, List<Card> targets) {
super(Singletons.getControl().getPlayer()); super(cntCounters, cntCounters);
this.type = type; validChoices = targets;
this.costPutCounter = costPutCounter; cardsChosen = cntCounters > 1 ? new HashMap<Card, Integer>() : null;
this.nNeeded = nNeeded; }
this.sa = sa;
protected String getMessage() {
return max == Integer.MAX_VALUE
? String.format(message, getDistibutedCounters())
: String.format(message, max - getDistibutedCounters());
}
private int getDistibutedCounters() {
int sum = 0;
for(Card c : selected) {
sum += max == 1 || cardsChosen.get(c) == null ? 1 : cardsChosen.get(c).intValue();
}
return sum;
}
@Override
protected boolean selectEntity(Card c) {
if (!isValidChoice(c)) {
return false;
}
int tc = getTimesSelected(c);
if( tc == 0)
selected.add(c);
else
cardsChosen.put(c, tc+1);
onSelectStateChanged(c, true);
return true;
} }
@Override @Override
public void showMessage() { protected boolean hasEnoughTargets() {
if ((nNeeded == 0) || (nNeeded == this.nPut)) { return hasAllTargets();
this.done();
}
final StringBuilder msg = new StringBuilder("Put ");
final int nLeft = nNeeded - this.nPut;
msg.append(nLeft).append(" ");
msg.append(costPutCounter.getCounter()).append(" on ");
msg.append(costPutCounter.getDescriptiveType());
if (nLeft > 1) {
msg.append("s");
}
this.typeList = CardLists.getValidCards(sa.getActivatingPlayer().getCardsIn(ZoneType.Battlefield), type.split(";"), sa.getActivatingPlayer(), sa.getSourceCard());
showMessage(msg.toString());
ButtonUtil.enableOnlyCancel();
} }
@Override @Override
protected void onCardSelected(Card card) { protected boolean hasAllTargets() {
if (this.typeList.contains(card)) { int sum = getDistibutedCounters();
this.nPut++; return sum >= max;
costPutCounter.executePayment(sa, card);
if (nNeeded == this.nPut) {
this.done();
} else {
this.showMessage();
}
}
}
boolean bPaid = false;
final protected void done() {
bPaid = true;
this.stop();
} }
@Override @Override
final protected void onCancel() { protected final boolean isValidChoice(Card choice) {
this.stop(); return validChoices.contains(choice);
}
public int getTimesSelected(Card c) {
return selected.contains(c) ? max == 1 || cardsChosen.get(c) == null ? 1 : cardsChosen.get(c).intValue() : 0;
} }
final public boolean isPaid() { return bPaid; }
} }
// Put Counter doesn't really have a "Valid" portion of the cost // Put Counter doesn't really have a "Valid" portion of the cost
@@ -255,11 +254,30 @@ public class CostPutCounter extends CostPartWithList {
if (this.payCostFromSource()) { if (this.payCostFromSource()) {
executePayment(ability, source, c); executePayment(ability, source, c);
lastPaidAmount = c;
return true; return true;
} else { } else {
InputPayment inp = new InputPayCostPutCounter(this.getType(), this, c, ability); // Cards to use this branch: Scarscale Ritual, Wandering Mage - each adds only one counter
final Player actor = ability.getActivatingPlayer();
List<Card> typeList = CardLists.getValidCards(actor.getCardsIn(ZoneType.Battlefield), getType().split(";"), actor, ability.getSourceCard());
InputPayCostPutCounter inp = new InputPayCostPutCounter(c, typeList);
inp.setMessage("Put %d " + getCounter().getName() + " counter on " + getDescriptiveType());
inp.setCancelAllowed(true);
FThreads.setInputAndWait(inp); FThreads.setInputAndWait(inp);
return inp.isPaid();
if(inp.hasCancelled())
return false;
int sum = 0;
for(Card crd : inp.getSelected()) {
int added = inp.getTimesSelected(crd);
sum += added;
executePayment(ability, crd, added);
}
source.setSVar("CostCountersAdded", Integer.toString(sum));
lastPaidAmount = sum;
return true;
} }
} }

View File

@@ -54,14 +54,12 @@ public class CostRemoveCounter extends CostPartWithList {
private final Map<Card,Integer> cardsChosen; private final Map<Card,Integer> cardsChosen;
private final CounterType counterType; private final CounterType counterType;
private final int nNeeded;
private final List<Card> validChoices; private final List<Card> validChoices;
public InputSelectCardToRemoveCounter(int cntCounters, CounterType cType, List<Card> validCards) { public InputSelectCardToRemoveCounter(int cntCounters, CounterType cType, List<Card> validCards) {
super(cntCounters, cntCounters); super(cntCounters, cntCounters);
this.validChoices = validCards; this.validChoices = validCards;
counterType = cType; counterType = cType;
nNeeded = cntCounters;
cardsChosen = cntCounters > 1 ? new HashMap<Card, Integer>() : null; cardsChosen = cntCounters > 1 ? new HashMap<Card, Integer>() : null;
} }
@@ -89,19 +87,19 @@ public class CostRemoveCounter extends CostPartWithList {
@Override @Override
protected boolean hasAllTargets() { protected boolean hasAllTargets() {
int sum = getDistibutedCounters(); int sum = getDistibutedCounters();
return sum >= nNeeded; return sum >= max;
} }
protected String getMessage() { protected String getMessage() {
return max == Integer.MAX_VALUE return max == Integer.MAX_VALUE
? String.format(message, getDistibutedCounters()) ? String.format(message, getDistibutedCounters())
: String.format(message, nNeeded - getDistibutedCounters()); : String.format(message, max - getDistibutedCounters());
} }
private int getDistibutedCounters() { private int getDistibutedCounters() {
int sum = 0; int sum = 0;
for(Card c : selected) { for(Card c : selected) {
sum += nNeeded == 1 || cardsChosen.get(c) == null ? 1 : cardsChosen.get(c).intValue(); sum += max == 1 || cardsChosen.get(c) == null ? 1 : cardsChosen.get(c).intValue();
} }
return sum; return sum;
} }
@@ -112,7 +110,7 @@ public class CostRemoveCounter extends CostPartWithList {
} }
public int getTimesSelected(Card c) { public int getTimesSelected(Card c) {
return selected.contains(c) ? nNeeded == 1 || cardsChosen.get(c) == null ? 1 : cardsChosen.get(c).intValue() : 0; return selected.contains(c) ? max == 1 || cardsChosen.get(c) == null ? 1 : cardsChosen.get(c).intValue() : 0;
} }
} }