refactor effects "you may cast... from... graveyard... exile it instead" (#10926)

* cleanup exiling cast spells

* common class MayCastTargetThenExileEffect

* fix zcc check

* add test suite
This commit is contained in:
xenohedron 2023-08-21 00:26:09 -04:00 committed by GitHub
parent 570b47705a
commit 4af977289e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 461 additions and 1252 deletions

View file

@ -1,115 +0,0 @@
package mage.abilities.effects;
import mage.abilities.Ability;
import mage.cards.Card;
import mage.constants.AsThoughEffectType;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeEvent;
import mage.players.Player;
import mage.target.targetpointer.FixedTarget;
import java.util.UUID;
public class CastCardFromGraveyardThenExileItEffect extends OneShotEffect {
public CastCardFromGraveyardThenExileItEffect() {
super(Outcome.Benefit);
}
protected CastCardFromGraveyardThenExileItEffect(final CastCardFromGraveyardThenExileItEffect effect) {
super(effect);
}
@Override
public CastCardFromGraveyardThenExileItEffect copy() {
return new CastCardFromGraveyardThenExileItEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
Card card = game.getCard(this.getTargetPointer().getFirst(game, source));
if (card == null) {
return false;
}
ContinuousEffect effect = new CastCardFromGraveyardEffect();
effect.setTargetPointer(new FixedTarget(card, game));
game.addEffect(effect, source);
effect = new ExileReplacementEffect(card.getId());
game.addEffect(effect, source);
return true;
}
}
class CastCardFromGraveyardEffect extends AsThoughEffectImpl {
CastCardFromGraveyardEffect() {
super(AsThoughEffectType.PLAY_FROM_NOT_OWN_HAND_ZONE, Duration.EndOfTurn, Outcome.Benefit);
this.staticText = "You may cast target card from your graveyard";
}
private CastCardFromGraveyardEffect(final CastCardFromGraveyardEffect effect) {
super(effect);
}
@Override
public boolean apply(Game game, Ability source) {
return true;
}
@Override
public CastCardFromGraveyardEffect copy() {
return new CastCardFromGraveyardEffect(this);
}
@Override
public boolean applies(UUID objectId, Ability source, UUID affectedControllerId, Game game) {
return objectId.equals(this.getTargetPointer().getFirst(game, source))
&& affectedControllerId.equals(source.getControllerId());
}
}
class ExileReplacementEffect extends ReplacementEffectImpl {
private final UUID cardId;
ExileReplacementEffect(UUID cardId) {
super(Duration.EndOfTurn, Outcome.Exile);
this.cardId = cardId;
this.staticText = "If that card would be put into your graveyard this turn, exile it instead";
}
private ExileReplacementEffect(final ExileReplacementEffect effect) {
super(effect);
this.cardId = effect.cardId;
}
@Override
public ExileReplacementEffect copy() {
return new ExileReplacementEffect(this);
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
Player controller = game.getPlayer(source.getControllerId());
Card card = game.getCard(this.cardId);
return controller != null
&& card != null
&& controller.moveCards(card, Zone.EXILED, source, game);
}
@Override
public boolean checksEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.ZONE_CHANGE;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
return zEvent.getToZone() == Zone.GRAVEYARD
&& zEvent.getTargetId().equals(this.cardId);
}
}

View file

@ -1,50 +0,0 @@
package mage.abilities.effects.common;
import mage.abilities.Ability;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeEvent;
import java.util.UUID;
public class ExileCardEnteringGraveyardReplacementEffect extends ReplacementEffectImpl {
private final UUID cardId;
public ExileCardEnteringGraveyardReplacementEffect(UUID cardId) {
super(Duration.EndOfTurn, Outcome.Exile);
this.cardId = cardId;
}
ExileCardEnteringGraveyardReplacementEffect(final ExileCardEnteringGraveyardReplacementEffect effect) {
super(effect);
this.cardId = effect.cardId;
}
@Override
public ExileCardEnteringGraveyardReplacementEffect copy() {
return new ExileCardEnteringGraveyardReplacementEffect(this);
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
((ZoneChangeEvent) event).setToZone(Zone.EXILED);
return false;
}
@Override
public boolean checksEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.ZONE_CHANGE;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
return zEvent.getToZone() == Zone.GRAVEYARD
&& zEvent.getTargetId().equals(this.cardId);
}
}

View file

@ -0,0 +1,98 @@
package mage.abilities.effects.common;
import mage.ApprovingObject;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.ContinuousEffect;
import mage.abilities.effects.OneShotEffect;
import mage.abilities.effects.common.replacement.ThatSpellGraveyardExileReplacementEffect;
import mage.cards.Card;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.game.Game;
import mage.players.Player;
import mage.target.targetpointer.FixedTarget;
import mage.util.CardUtil;
/**
* @author xenohedron
*/
public class MayCastTargetThenExileEffect extends OneShotEffect {
private final Duration duration;
private final boolean noMana;
/**
* Allows to cast the target card immediately, either for its cost or for free.
* If resulting spell would be put into graveyard, exiles it instead.
*/
public MayCastTargetThenExileEffect(boolean noMana) {
super(Outcome.Benefit);
this.duration = Duration.OneUse;
this.noMana = noMana;
}
/**
* Makes the target card playable for the specified duration as long as it remains in that zone.
* If resulting spell would be put into graveyard, exiles it instead.
*/
public MayCastTargetThenExileEffect(Duration duration) {
super(Outcome.Benefit);
this.duration = duration;
this.noMana = false;
}
protected MayCastTargetThenExileEffect(final MayCastTargetThenExileEffect effect) {
super(effect);
this.duration = effect.duration;
this.noMana = effect.noMana;
}
@Override
public MayCastTargetThenExileEffect copy() {
return new MayCastTargetThenExileEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
Card card = game.getCard(getTargetPointer().getFirst(game, source));
if (card == null) {
return false;
}
FixedTarget fixedTarget = new FixedTarget(card, game);
if (duration == Duration.OneUse) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null || !controller.chooseUse(outcome, "Cast " + card.getLogName() + '?', source, game)) {
return false;
}
game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), Boolean.TRUE);
controller.cast(controller.chooseAbilityForCast(card, game, noMana),
game, noMana, new ApprovingObject(source, game));
game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), null);
} else {
CardUtil.makeCardPlayable(game, source, card, duration, false);
}
ContinuousEffect effect = new ThatSpellGraveyardExileReplacementEffect(true);
effect.setTargetPointer(fixedTarget);
game.addEffect(effect, source);
return true;
}
@Override
public String getText(Mode mode) {
if (staticText != null && !staticText.isEmpty()) {
return staticText;
}
String text = "you may cast " + getTargetPointer().describeTargets(mode.getTargets(), "it");
if (duration == Duration.EndOfTurn) {
text += " this turn";
} else if (!duration.toString().isEmpty()) {
text += duration.toString();
}
if (noMana) {
text += " without paying its mana cost";
}
return text + ". " + ThatSpellGraveyardExileReplacementEffect.RULE_YOUR;
}
}

View file

@ -0,0 +1,58 @@
package mage.abilities.effects.common.replacement;
import mage.abilities.Ability;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeEvent;
import mage.target.targetpointer.FixedTarget;
/**
* @author xenohedron
*/
public class ThatSpellGraveyardExileReplacementEffect extends ReplacementEffectImpl {
public static final String RULE_A = "If that spell would be put into a graveyard, exile it instead.";
public static final String RULE_YOUR = "If that spell would be put into your graveyard, exile it instead.";
/**
* If that spell would be put into a graveyard, exiles it instead.
* Must set target pointer to fixed target.
*/
public ThatSpellGraveyardExileReplacementEffect(boolean yourGraveyard) {
super(Duration.EndOfTurn, Outcome.Exile);
staticText = yourGraveyard ? RULE_YOUR : RULE_A;
}
protected ThatSpellGraveyardExileReplacementEffect(final ThatSpellGraveyardExileReplacementEffect effect) {
super(effect);
}
@Override
public ThatSpellGraveyardExileReplacementEffect copy() {
return new ThatSpellGraveyardExileReplacementEffect(this);
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
((ZoneChangeEvent) event).setToZone(Zone.EXILED);
return false;
}
@Override
public boolean checksEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.ZONE_CHANGE;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
return zEvent.getToZone() == Zone.GRAVEYARD
&& zEvent.getTargetId().equals(((FixedTarget) getTargetPointer()).getTarget())
&& ((FixedTarget) getTargetPointer()).getZoneChangeCounter() + 1
== game.getState().getZoneChangeCounter(zEvent.getTargetId());
}
}