[SHM] reworked implementation of Conspire ability

This commit is contained in:
Evan Kranzler 2022-02-23 09:18:25 -05:00
parent 15a6d939b7
commit 86b20185f5
14 changed files with 87 additions and 190 deletions

View file

@ -3,13 +3,11 @@ package mage.abilities.keyword;
import mage.abilities.Ability;
import mage.abilities.SpellAbility;
import mage.abilities.StaticAbility;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.costs.*;
import mage.abilities.costs.common.TapTargetCost;
import mage.abilities.costs.mana.ManaCostsImpl;
import mage.abilities.effects.Effect;
import mage.abilities.effects.OneShotEffect;
import mage.cards.Card;
import mage.abilities.effects.common.CastSourceTriggeredAbility;
import mage.abilities.effects.common.CopySourceSpellEffect;
import mage.constants.CardType;
import mage.constants.Outcome;
import mage.constants.Zone;
@ -18,14 +16,15 @@ import mage.filter.predicate.mageobject.SharesColorWithSourcePredicate;
import mage.filter.predicate.permanent.TappedPredicate;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.game.stack.Spell;
import mage.players.Player;
import mage.target.common.TargetControlledPermanent;
import java.util.HashSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import java.util.List;
import java.util.Objects;
/*
* 702.77. Conspire
@ -45,62 +44,54 @@ import java.util.UUID;
public class ConspireAbility extends StaticAbility implements OptionalAdditionalSourceCosts {
private static final String keywordText = "Conspire";
private static final FilterControlledPermanent filter = new FilterControlledPermanent("untapped creatures you control that share a color with it");
protected static final String CONSPIRE_ACTIVATION_KEY = "ConspireActivation";
private static final FilterControlledPermanent filter
= new FilterControlledPermanent("untapped creatures you control that share a color with it");
static {
filter.add(TappedPredicate.UNTAPPED);
filter.add(new SharesColorWithSourcePredicate());
filter.add(SharesColorWithSourcePredicate.instance);
filter.add(CardType.CREATURE.getPredicate());
}
public enum ConspireTargets {
NONE,
ONE,
MORE
NONE(""),
ONE(" and you may choose a new target for the copy"),
MORE(" and you may choose new targets for the copy");
private final String message;
ConspireTargets(String message) {
this.message = message;
}
public String getReminder() {
return "as you cast this spell, you may tap two untapped creatures you control " +
"that share a color with it. When you do, copy it" + message;
}
}
private final UUID conspireId;
private String reminderText;
private OptionalAdditionalCost conspireCost;
private final String reminderText;
private final OptionalAdditionalCost conspireCost;
/**
* Unique Id for a ConspireAbility but may not change while a continuous
* effect gives Conspire
*
* @param conspireId
* @param conspireTargets controls the content of the reminder text
*/
public ConspireAbility(UUID conspireId, ConspireTargets conspireTargets) {
public ConspireAbility(ConspireTargets conspireTargets) {
super(Zone.STACK, null);
this.conspireId = conspireId;
switch (conspireTargets) {
case NONE:
reminderText = "As you cast this spell, you may tap two untapped creatures you control that share a color with it. When you do, copy it.";
break;
case ONE:
reminderText = "As you cast this spell, you may tap two untapped creatures you control that share a color with it. When you do, copy it and you may choose a new target for the copy.";
break;
case MORE:
reminderText = "As you cast this spell, you may tap two untapped creatures you control that share a color with it. When you do, copy it and you may choose new targets for the copy.";
break;
}
Cost cost = new TapTargetCost(new TargetControlledPermanent(2, 2, filter, true));
cost.setText("");
addConspireCostAndSetup(new OptionalAdditionalCostImpl(keywordText, " ", reminderText, cost));
addSubAbility(new ConspireTriggeredAbility(conspireId));
}
private void addConspireCostAndSetup(OptionalAdditionalCost newCost) {
this.conspireCost = newCost;
reminderText = conspireTargets.getReminder();
this.conspireCost = new OptionalAdditionalCostImpl(
keywordText, " ", reminderText,
new TapTargetCost(new TargetControlledPermanent(2, filter))
);
this.conspireCost.setCostType(VariableCostType.ADDITIONAL);
addSubAbility(new ConspireTriggeredAbility());
}
public ConspireAbility(final ConspireAbility ability) {
super(ability);
this.conspireId = ability.conspireId;
this.conspireCost = ability.conspireCost.copy();
this.reminderText = ability.reminderText;
}
@ -113,66 +104,39 @@ public class ConspireAbility extends StaticAbility implements OptionalAdditional
@Override
public void addCost(Cost cost) {
if (conspireCost != null) {
((Costs) conspireCost).add(cost);
((Costs<Cost>) conspireCost).add(cost);
}
}
public UUID getConspireId() {
return conspireId;
}
@Override
public boolean isActivated() {
throw new UnsupportedOperationException("Use ConspireAbility.isActivated(Ability ability, Game game) method instead!");
}
public boolean isActivated(Ability ability, Game game) {
Set<UUID> activations = (Set<UUID>) game.getState().getValue(CONSPIRE_ACTIVATION_KEY + ability.getId());
if (activations != null) {
return activations.contains(getConspireId());
}
return false;
}
@Override
public void addOptionalAdditionalCosts(Ability ability, Game game) {
if (ability instanceof SpellAbility) {
Player player = game.getPlayer(getControllerId());
if (player != null) {
resetConspire(ability, game);
// AI supports conspire
if (conspireCost.canPay(ability, this, getControllerId(), game)
&& player.chooseUse(Outcome.Benefit, "Pay " + conspireCost.getText(false) + " ?", ability, game)) {
activateConspire(ability, game);
for (Iterator it = ((Costs) conspireCost).iterator(); it.hasNext(); ) {
Cost cost = (Cost) it.next();
if (cost instanceof ManaCostsImpl) {
ability.getManaCostsToPay().add((ManaCostsImpl) cost.copy());
} else {
ability.getCosts().add(cost.copy());
}
}
}
if (!(ability instanceof SpellAbility)) {
return;
}
Player player = game.getPlayer(getControllerId());
if (player == null) {
return;
}
// AI supports conspire
if (!conspireCost.canPay(ability, this, getControllerId(), game)
|| !player.chooseUse(Outcome.Benefit, "Pay " + conspireCost.getText(false) + " ?", ability, game)) {
return;
}
for (Iterator<Cost> it = ((Costs<Cost>) conspireCost).iterator(); it.hasNext(); ) {
Cost cost = (Cost) it.next();
if (cost instanceof ManaCostsImpl) {
ability.getManaCostsToPay().add((ManaCostsImpl<?>) cost.copy());
} else {
ability.getCosts().add(cost.copy());
}
}
}
private void activateConspire(Ability ability, Game game) {
Set<UUID> activations = (Set<UUID>) game.getState().getValue(CONSPIRE_ACTIVATION_KEY + ability.getId());
if (activations == null) {
activations = new HashSet<>();
game.getState().setValue(CONSPIRE_ACTIVATION_KEY + ability.getId(), activations);
}
activations.add(getConspireId());
}
private void resetConspire(Ability ability, Game game) {
Set<UUID> activations = (Set<UUID>) game.getState().getValue(CONSPIRE_ACTIVATION_KEY + ability.getId());
if (activations != null) {
activations.remove(getConspireId());
}
}
@Override
public String getRule() {
StringBuilder sb = new StringBuilder();
@ -185,35 +149,19 @@ public class ConspireAbility extends StaticAbility implements OptionalAdditional
@Override
public String getCastMessageSuffix() {
if (conspireCost != null) {
return conspireCost.getCastSuffixMessage(0);
} else {
return "";
}
}
public String getReminderText() {
if (conspireCost != null) {
return conspireCost.getReminderText();
} else {
return "";
}
return conspireCost != null ? conspireCost.getCastSuffixMessage(0) : "";
}
}
class ConspireTriggeredAbility extends TriggeredAbilityImpl {
class ConspireTriggeredAbility extends CastSourceTriggeredAbility {
private final UUID conspireId;
public ConspireTriggeredAbility(UUID conspireId) {
super(Zone.STACK, new ConspireEffect());
this.conspireId = conspireId;
public ConspireTriggeredAbility() {
super(new CopySourceSpellEffect(), false);
this.setRuleVisible(false);
}
private ConspireTriggeredAbility(final ConspireTriggeredAbility ability) {
super(ability);
this.conspireId = ability.conspireId;
}
@Override
@ -221,34 +169,21 @@ class ConspireTriggeredAbility extends TriggeredAbilityImpl {
return new ConspireTriggeredAbility(this);
}
@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.SPELL_CAST;
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (event.getSourceId().equals(getSourceId())) {
Spell spell = game.getStack().getSpell(event.getSourceId());
for (Ability ability : spell.getAbilities(game)) {
if (ability instanceof ConspireAbility
&& ((ConspireAbility) ability).getConspireId().equals(getConspireId())) {
if (((ConspireAbility) ability).isActivated(spell.getSpellAbility(), game)) {
for (Effect effect : this.getEffects()) {
if (effect instanceof ConspireEffect) {
((ConspireEffect) effect).setConspiredSpell(spell);
}
}
return true;
}
}
}
if (!super.checkTrigger(event, game)) {
return false;
}
return false;
}
public UUID getConspireId() {
return conspireId;
Spell spell = game.getStack().getSpell(event.getSourceId());
return spell != null && spell
.getSpellAbility()
.getEffects()
.stream()
.map(effect -> effect.getValue("tappedPermanents"))
.filter(Objects::nonNull)
.map(x -> (List<Permanent>) x)
.flatMap(Collection::stream)
.count() >= 2;
}
@Override
@ -256,39 +191,3 @@ class ConspireTriggeredAbility extends TriggeredAbilityImpl {
return "When you pay the conspire costs, copy it and you may choose a new target for the copy.";
}
}
class ConspireEffect extends OneShotEffect {
private Spell conspiredSpell;
public ConspireEffect() {
super(Outcome.Copy);
}
public ConspireEffect(final ConspireEffect effect) {
super(effect);
this.conspiredSpell = effect.conspiredSpell;
}
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null && conspiredSpell != null) {
Card card = game.getCard(conspiredSpell.getSourceId());
if (card != null) {
conspiredSpell.createCopyOnStack(game, source, source.getControllerId(), true);
return true;
}
}
return false;
}
public void setConspiredSpell(Spell conspiredSpell) {
this.conspiredSpell = conspiredSpell;
}
@Override
public ConspireEffect copy() {
return new ConspireEffect(this);
}
}

View file

@ -6,11 +6,11 @@ import mage.filter.predicate.ObjectSourcePlayerPredicate;
import mage.game.Game;
/**
*
* @author LevelX2
*/
public class SharesColorWithSourcePredicate implements ObjectSourcePlayerPredicate<MageObject> {
public enum SharesColorWithSourcePredicate implements ObjectSourcePlayerPredicate<MageObject> {
instance;
@Override
public boolean apply(ObjectSourcePlayer<MageObject> input, Game game) {
@ -26,4 +26,4 @@ public class SharesColorWithSourcePredicate implements ObjectSourcePlayerPredica
public String toString() {
return "shares a color";
}
}
}