From 48f7eace00e7b94c009909fef579dae9076564c8 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Sun, 9 Dec 2012 19:22:27 +0100 Subject: [PATCH] Changes to buyback, added random discard to DiscardCost. --- .../costs/OptionalAdditionalCostImpl.java | 4 +- .../costs/common/DiscardCardCost.java | 7 +- .../costs/common/DiscardTargetCost.java | 35 +++++++--- .../abilities/keyword/BuybackAbility.java | 64 +++++++++++++------ 4 files changed, 81 insertions(+), 29 deletions(-) diff --git a/Mage/src/mage/abilities/costs/OptionalAdditionalCostImpl.java b/Mage/src/mage/abilities/costs/OptionalAdditionalCostImpl.java index 2573509c94f..8d41b9cf3c7 100644 --- a/Mage/src/mage/abilities/costs/OptionalAdditionalCostImpl.java +++ b/Mage/src/mage/abilities/costs/OptionalAdditionalCostImpl.java @@ -28,6 +28,8 @@ package mage.abilities.costs; +import mage.abilities.costs.mana.ManaCostsImpl; + /** * * @author LevelX2 @@ -103,7 +105,7 @@ public class OptionalAdditionalCostImpl */ @Override public String getCastSuffixMessage(int position) { - return ""; + return " with " + name; } diff --git a/Mage/src/mage/abilities/costs/common/DiscardCardCost.java b/Mage/src/mage/abilities/costs/common/DiscardCardCost.java index 1917d47143e..f8c330582a1 100644 --- a/Mage/src/mage/abilities/costs/common/DiscardCardCost.java +++ b/Mage/src/mage/abilities/costs/common/DiscardCardCost.java @@ -27,6 +27,7 @@ */ package mage.abilities.costs.common; +import mage.filter.FilterCard; import mage.target.common.TargetCardInHand; /** @@ -36,7 +37,11 @@ import mage.target.common.TargetCardInHand; public class DiscardCardCost extends DiscardTargetCost { public DiscardCardCost() { - super(new TargetCardInHand()); + this(false); + } + + public DiscardCardCost(boolean randomDiscard) { + super(new TargetCardInHand(new FilterCard("a Card at random")), randomDiscard); } public DiscardCardCost(DiscardCardCost cost) { diff --git a/Mage/src/mage/abilities/costs/common/DiscardTargetCost.java b/Mage/src/mage/abilities/costs/common/DiscardTargetCost.java index f23342b59e5..a9d7358e2f6 100644 --- a/Mage/src/mage/abilities/costs/common/DiscardTargetCost.java +++ b/Mage/src/mage/abilities/costs/common/DiscardTargetCost.java @@ -46,9 +46,15 @@ import mage.target.common.TargetCardInHand; public class DiscardTargetCost extends CostImpl { List cards = new ArrayList(); + protected boolean randomDiscard; public DiscardTargetCost(TargetCardInHand target) { + this(target, false); + } + + public DiscardTargetCost(TargetCardInHand target, boolean randomDiscard) { this.addTarget(target); + this.randomDiscard = randomDiscard; this.text = "Discard " + target.getTargetName(); } @@ -57,19 +63,32 @@ public class DiscardTargetCost extends CostImpl { for (Card card: cost.cards) { this.cards.add(card.copy()); } + this.randomDiscard = cost.randomDiscard; } @Override public boolean pay(Ability ability, Game game, UUID sourceId, UUID controllerId, boolean noMana) { - if (targets.choose(Outcome.Discard, controllerId, sourceId, game)) { - Player player = game.getPlayer(controllerId); - for (UUID targetId: targets.get(0).getTargets()) { - Card card = player.getHand().get(targetId, game); - if (card == null) { - return false; + Player player = game.getPlayer(controllerId); + if (randomDiscard) { + int amount = this.getTargets().get(0).getMaxNumberOfTargets(); + int maxAmount = Math.min(amount, player.getHand().size()); + for (int i = 0; i < maxAmount; i++) { + Card card = player.getHand().getRandom(game); + if (card != null) { + paid |= player.discard(card, null, game); + } + } + } else { + if (targets.choose(Outcome.Discard, controllerId, sourceId, game)) { + + for (UUID targetId: targets.get(0).getTargets()) { + Card card = player.getHand().get(targetId, game); + if (card == null) { + return false; + } + this.cards.add(card.copy()); + paid |= player.discard(card, null, game); } - this.cards.add(card.copy()); - paid |= player.discard(card, null, game); } } return paid; diff --git a/Mage/src/mage/abilities/keyword/BuybackAbility.java b/Mage/src/mage/abilities/keyword/BuybackAbility.java index e5ec6bef6c6..fa874cd09d3 100644 --- a/Mage/src/mage/abilities/keyword/BuybackAbility.java +++ b/Mage/src/mage/abilities/keyword/BuybackAbility.java @@ -40,7 +40,6 @@ import mage.abilities.costs.Costs; import mage.abilities.costs.OptionalAdditionalCost; import mage.abilities.costs.OptionalAdditionalCostImpl; import mage.abilities.costs.OptionalAdditionalSourceCosts; -import mage.abilities.costs.mana.BuybackManaCost; import mage.abilities.costs.mana.ManaCostsImpl; import mage.abilities.effects.ReplacementEffectImpl; import mage.cards.Card; @@ -56,24 +55,29 @@ import mage.players.Player; public class BuybackAbility extends StaticAbility implements OptionalAdditionalSourceCosts { + private static final String keywordTextMana = "Buyback "; + private static final String keywordTextCost = "Buyback-"; + private static final String reminderTextCost = "(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.)"; + private static final String reminderTextMana = "(You may pay an additional {cost} as you cast this spell. If you do, put this card into your hand as it resolves.)"; protected List buybackCosts = new LinkedList(); public BuybackAbility(String manaString) { super(Zone.STACK, new BuybackEffect()); - this.buybackCosts.add(new BuybackManaCost(manaString)); + OptionalAdditionalCostImpl buybackCost = new OptionalAdditionalCostImpl(keywordTextMana, reminderTextMana, new ManaCostsImpl(manaString)); + buybackCosts.add(buybackCost); setRuleAtTheTop(true); } public BuybackAbility(Cost cost) { super(Zone.STACK, new BuybackEffect()); - OptionalAdditionalCostImpl buybackCost = new OptionalAdditionalCostImpl("Buyback", "(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.)", cost); - this.buybackCosts.add(buybackCost); + OptionalAdditionalCostImpl buybackCost = new OptionalAdditionalCostImpl(keywordTextCost, reminderTextCost, cost); + buybackCosts.add(buybackCost); setRuleAtTheTop(true); } public BuybackAbility(final BuybackAbility ability) { super(ability); - this.buybackCosts = ability.buybackCosts; + buybackCosts = ability.buybackCosts; } @Override @@ -81,6 +85,16 @@ public class BuybackAbility extends StaticAbility implements Opt return new BuybackAbility(this); } + @Override + public void addCost(Cost cost) { + if (buybackCosts.size() > 0) { + ((Costs) buybackCosts.get(0)).add(cost); + } else { + OptionalAdditionalCostImpl buybackCost = new OptionalAdditionalCostImpl(keywordTextCost, reminderTextCost, cost); + buybackCosts.add(buybackCost); + } + } + public boolean isActivated() { for (OptionalAdditionalCost cost: buybackCosts) { if(cost.isActivated()) { @@ -143,35 +157,47 @@ public class BuybackAbility extends StaticAbility implements Opt @Override public String getRule() { StringBuilder sb = new StringBuilder(); + String reminderText = ""; int numberBuyback = 0; - String remarkText = ""; for (OptionalAdditionalCost buybackCost: buybackCosts) { if (numberBuyback == 0) { sb.append(buybackCost.getText(false)); - remarkText = buybackCost.getReminderText(); + reminderText = buybackCost.getReminderText(); } else { - sb.append(" and/or ").append(buybackCost.getText(true)); + sb.append(", ").append(buybackCost.getText(true)); } ++numberBuyback; } - if (numberBuyback == 1) { - sb.append(" ").append(remarkText); - } + sb.append(" ").append(reminderText); return sb.toString(); } @Override public String getCastMessageSuffix() { - StringBuilder sb = new StringBuilder(); - int position = 0; - for (OptionalAdditionalCost cost : buybackCosts) { - if (cost.isActivated()) { - sb.append(cost.getCastSuffixMessage(position)); - ++position; - } + String message = ""; + if (isActivated()) { + message = " with " + keywordTextMana; } - return sb.toString(); + return message; + } + + public String getReminderText() { + String costsReminderText = reminderTextCost; + StringBuilder costsText = new StringBuilder(); + int costCounter = 0; + for (OptionalAdditionalCost cost : buybackCosts) { + if (costCounter > 0) { + costsText.append(" and "); + } else { + if (((Costs)cost).get(0) instanceof ManaCostsImpl) { + costsReminderText = reminderTextMana; + } + } + costsText.append(cost.getText(true)); + ++costCounter; + } + return costsReminderText.replace("{cost}", costsText); } }