Some changes to BuybackAbility.

This commit is contained in:
LevelX2 2012-12-15 19:32:24 +01:00
parent e11fed3d03
commit 4a498fa256

View file

@ -28,8 +28,6 @@
package mage.abilities.keyword; package mage.abilities.keyword;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import mage.Constants; import mage.Constants;
import mage.Constants.Zone; import mage.Constants.Zone;
import mage.abilities.Ability; import mage.abilities.Ability;
@ -49,35 +47,40 @@ import mage.game.events.ZoneChangeEvent;
import mage.players.Player; import mage.players.Player;
/** /**
* 702.25. Buyback
*
* 702.25a Buyback appears on some instants and sorceries. It represents two static
* abilities that function while the spell is on the stack. "Buyback [cost]" means
* "You may pay an additional [cost] as you cast this spell" and "If the buyback
* cost was paid, put this spell into its owner's hand instead of into that player's
* graveyard as it resolves." Paying a spell's buyback cost follows the rules for
* paying additional costs in rules 601.2b and 601.2e-g.
* *
* @author LevelX2 * @author LevelX2
*/ */
public class BuybackAbility extends StaticAbility<BuybackAbility> implements OptionalAdditionalSourceCosts { public class BuybackAbility extends StaticAbility<BuybackAbility> implements OptionalAdditionalSourceCosts {
private static final String keywordTextMana = "Buyback "; private static final String keywordText = "Buyback";
private static final String keywordTextCost = "Buyback-";
private static final String reminderTextCost = "<i>(You may {cost} in addition to any other costs as you cast this spell. If you do, put this card into your hand as it resolves.)</i>"; private static final String reminderTextCost = "<i>(You may {cost} in addition to any other costs as you cast this spell. If you do, put this card into your hand as it resolves.)</i>";
private static final String reminderTextMana = "<i>(You may pay an additional {cost} as you cast this spell. If you do, put this card into your hand as it resolves.)</i>"; private static final String reminderTextMana = "<i>(You may pay an additional {cost} as you cast this spell. If you do, put this card into your hand as it resolves.)</i>";
protected List<OptionalAdditionalCost> buybackCosts = new LinkedList<OptionalAdditionalCost>(); protected OptionalAdditionalCost buybackCost;
public BuybackAbility(String manaString) { public BuybackAbility(String manaString) {
super(Zone.STACK, new BuybackEffect()); super(Zone.STACK, new BuybackEffect());
OptionalAdditionalCostImpl buybackCost = new OptionalAdditionalCostImpl(keywordTextMana, reminderTextMana, new ManaCostsImpl(manaString)); this.buybackCost = new OptionalAdditionalCostImpl(keywordText, reminderTextMana, new ManaCostsImpl(manaString));
buybackCosts.add(buybackCost);
setRuleAtTheTop(true); setRuleAtTheTop(true);
} }
public BuybackAbility(Cost cost) { public BuybackAbility(Cost cost) {
super(Zone.STACK, new BuybackEffect()); super(Zone.STACK, new BuybackEffect());
OptionalAdditionalCostImpl buybackCost = new OptionalAdditionalCostImpl(keywordTextCost, reminderTextCost, cost); this.buybackCost = new OptionalAdditionalCostImpl(keywordText, "-", reminderTextCost, cost);
buybackCosts.add(buybackCost);
setRuleAtTheTop(true); setRuleAtTheTop(true);
} }
public BuybackAbility(final BuybackAbility ability) { public BuybackAbility(final BuybackAbility ability) {
super(ability); super(ability);
buybackCosts = ability.buybackCosts; buybackCost = ability.buybackCost;
} }
@Override @Override
@ -87,52 +90,32 @@ public class BuybackAbility extends StaticAbility<BuybackAbility> implements Opt
@Override @Override
public void addCost(Cost cost) { public void addCost(Cost cost) {
if (buybackCosts.size() > 0) { if (buybackCost != null) {
((Costs) buybackCosts.get(0)).add(cost); ((Costs) buybackCost).add(cost);
} else {
OptionalAdditionalCostImpl buybackCost = new OptionalAdditionalCostImpl(keywordTextCost, reminderTextCost, cost);
buybackCosts.add(buybackCost);
} }
} }
public boolean isActivated() { public boolean isActivated() {
for (OptionalAdditionalCost cost: buybackCosts) { if (buybackCost != null) {
if(cost.isActivated()) { return buybackCost.isActivated();
return true;
}
} }
return false; return false;
} }
public void resetBuyback() { public void resetBuyback() {
for (OptionalAdditionalCost cost: buybackCosts) { if (buybackCost != null) {
cost.reset(); buybackCost.reset();
} }
} }
public List<OptionalAdditionalCost> getBuybackCosts () {
return buybackCosts;
}
public void addKickerManaCost(OptionalAdditionalCost kickerCost) {
buybackCosts.add(kickerCost);
}
@Override @Override
public void addOptionalAdditionalCosts(Ability ability, Game game) { public void addOptionalAdditionalCosts(Ability ability, Game game) {
if (ability instanceof SpellAbility) { if (ability instanceof SpellAbility) {
Player player = game.getPlayer(controllerId); Player player = game.getPlayer(controllerId);
if (player != null) { if (player != null) {
this.resetBuyback(); this.resetBuyback();
for (OptionalAdditionalCost buybackCost: buybackCosts) { if (buybackCost != null) {
boolean again = true; if (player.chooseUse(Constants.Outcome.Benefit,new StringBuilder("Pay ").append(buybackCost.getText(false)).append(" ?").toString(), game)) {
while (again) {
String times = "";
if (buybackCost.isRepeatable()) {
int activated = buybackCost.getActivateCount();
times = Integer.toString(activated + 1) + (activated == 0 ? " time ":" times ");
}
if (player.chooseUse(Constants.Outcome.Benefit, "Pay " + times + buybackCost.getText(false) + " ?", game)) {
buybackCost.activate(); buybackCost.activate();
for (Iterator it = ((Costs) buybackCost).iterator(); it.hasNext();) { for (Iterator it = ((Costs) buybackCost).iterator(); it.hasNext();) {
Cost cost = (Cost) it.next(); Cost cost = (Cost) it.next();
@ -142,11 +125,6 @@ public class BuybackAbility extends StaticAbility<BuybackAbility> implements Opt
ability.getCosts().add(cost.copy()); ability.getCosts().add(cost.copy());
} }
} }
again = buybackCost.isRepeatable();
} else {
again = false;
}
} }
} }
} }
@ -157,48 +135,29 @@ public class BuybackAbility extends StaticAbility<BuybackAbility> implements Opt
@Override @Override
public String getRule() { public String getRule() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String reminderText = ""; if (buybackCost != null) {
int numberBuyback = 0;
for (OptionalAdditionalCost buybackCost: buybackCosts) {
if (numberBuyback == 0) {
sb.append(buybackCost.getText(false)); sb.append(buybackCost.getText(false));
reminderText = buybackCost.getReminderText(); sb.append(" ").append(buybackCost.getReminderText());
} else {
sb.append(", ").append(buybackCost.getText(true));
} }
++numberBuyback;
}
sb.append(" ").append(reminderText);
return sb.toString(); return sb.toString();
} }
@Override @Override
public String getCastMessageSuffix() { public String getCastMessageSuffix() {
String message = ""; if (buybackCost != null) {
if (isActivated()) { return buybackCost.getCastSuffixMessage(0);
message = " with " + keywordTextMana; } else {
return "";
} }
return message;
} }
public String getReminderText() { public String getReminderText() {
String costsReminderText = reminderTextCost; if (buybackCost != null) {
StringBuilder costsText = new StringBuilder(); return buybackCost.getReminderText();
int costCounter = 0;
for (OptionalAdditionalCost cost : buybackCosts) {
if (costCounter > 0) {
costsText.append(" and ");
} else { } else {
if (((Costs)cost).get(0) instanceof ManaCostsImpl) { return "";
costsReminderText = reminderTextMana;
} }
} }
costsText.append(cost.getText(true));
++costCounter;
}
return costsReminderText.replace("{cost}", costsText);
}
} }
class BuybackEffect extends ReplacementEffectImpl<BuybackEffect> { class BuybackEffect extends ReplacementEffectImpl<BuybackEffect> {