Fix the Divine Visitation replacement effect.

This also gives the CREATE_TOKEN game events access to the Token being created and updates the GatherSpecimens replacement effect to use that access.
This commit is contained in:
dilnu 2020-02-01 15:14:15 -05:00
parent b708494647
commit cc9629ed51
4 changed files with 81 additions and 60 deletions

View file

@ -27,7 +27,6 @@ public final class DivineVisitation extends CardImpl {
public DivineVisitation(UUID ownerId, CardSetInfo setInfo) {
super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, "{3}{W}{W}");
// TODO: This implementation is not entirely correct, see https://twitter.com/EliShffrn/status/1042145606582591490
// If one or more creature tokens would be created under your control, that many 4/4 white Angel creature tokens with flying and vigilance are created instead.
this.addAbility(new SimpleStaticAbility(
Zone.BATTLEFIELD, new DivineVisitationEffect()
@ -59,21 +58,17 @@ class DivineVisitationEffect extends ReplacementEffectImpl {
@Override
public boolean checksEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.ENTERS_THE_BATTLEFIELD;
return event.getType() == GameEvent.EventType.CREATE_TOKEN;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
Permanent perm = ((EntersTheBattlefieldEvent) event).getTarget();
return perm != null
&& perm.isCreature()
&& perm instanceof PermanentToken
&& perm.isControlledBy(source.getControllerId());
return event.getPlayerId().equals(source.getControllerId());
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
game.addEffect(new CopyEffect(Duration.Custom, new AngelVigilanceToken(), event.getTargetId()), source);
event.setToken(new AngelVigilanceToken());
return false;
}

View file

@ -76,7 +76,7 @@ class GatherSpecimensReplacementEffect extends ReplacementEffectImpl {
}
}
}
if (event.getType() == GameEvent.EventType.CREATE_TOKEN && event.getFlag()) { // flag indicates if it's a creature token
if (event.getType() == GameEvent.EventType.CREATE_TOKEN && event.getToken().isCreature()) {
Player controller = game.getPlayer(source.getControllerId());
return controller != null && controller.hasOpponent(event.getPlayerId(), game);
}

View file

@ -2,6 +2,7 @@ package mage.game.events;
import mage.MageObjectReference;
import mage.constants.Zone;
import mage.game.permanent.token.Token;
import java.io.Serializable;
import java.util.ArrayList;
@ -13,6 +14,7 @@ import java.util.UUID;
*/
public class GameEvent implements Serializable {
protected Token token;
protected EventType type;
protected UUID targetId;
protected UUID sourceId;
@ -339,13 +341,17 @@ public class GameEvent implements Serializable {
}
public GameEvent(EventType type, UUID targetId, UUID sourceId, UUID playerId, MageObjectReference reference) {
this(type, null, targetId, sourceId, playerId, 0, false, reference);
this(type, null, targetId, sourceId, playerId, 0, false, reference, null);
}
public GameEvent(EventType type, UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag) {
this(type, null, targetId, sourceId, playerId, amount, flag);
}
public GameEvent(EventType type, UUID sourceId, UUID playerId, int amount, Token token) {
this(type, null, null, sourceId, playerId, amount, false, null, token);
}
public GameEvent(UUID customEventType, UUID targetId, UUID sourceId, UUID playerId) {
this(EventType.CUSTOM_EVENT, customEventType, targetId, sourceId, playerId, 0, false);
}
@ -398,11 +404,11 @@ public class GameEvent implements Serializable {
private GameEvent(EventType type, UUID customEventType,
UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag) {
this(type, customEventType, targetId, sourceId, playerId, amount, flag, null);
this(type, customEventType, targetId, sourceId, playerId, amount, flag, null, null);
}
private GameEvent(EventType type, UUID customEventType,
UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag, MageObjectReference reference) {
UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag, MageObjectReference reference, Token token) {
this.type = type;
this.customEventType = customEventType;
this.targetId = targetId;
@ -411,6 +417,7 @@ public class GameEvent implements Serializable {
this.playerId = playerId;
this.flag = flag;
this.reference = reference;
this.token = token;
}
public EventType getType() {
@ -445,6 +452,10 @@ public class GameEvent implements Serializable {
this.amount = amount;
}
public Token getToken() { return token; }
public void setToken(Token token) { this.token = token; }
public void setAmountForCounters(int amount, boolean isEffect) {
this.amount = amount;

View file

@ -2,6 +2,7 @@ package mage.game.permanent.token;
import mage.MageObject;
import mage.MageObjectImpl;
import mage.MageObjectReference;
import mage.abilities.Ability;
import mage.cards.Card;
import mage.constants.Zone;
@ -31,7 +32,7 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
private boolean expansionSetCodeChecked;
private Card copySourceCard; // the card the Token is a copy from
// list of set codes tokene images are available for
// list of set codes token images are available for
protected List<String> availableImageSetCodes = new ArrayList<>();
public enum Type {
@ -131,14 +132,7 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
return putOntoBattlefield(amount, game, sourceId, controllerId, tapped, attacking, null);
}
@Override
public boolean putOntoBattlefield(int amount, Game game, UUID sourceId, UUID controllerId, boolean tapped, boolean attacking, UUID attackedPlayer) {
Player controller = game.getPlayer(controllerId);
if (controller == null) {
return false;
}
lastAddedTokenIds.clear();
private String getSetCode(Game game, UUID sourceId) {
// moved here from CreateTokenEffect because not all cards that create tokens use CreateTokenEffect
// they use putOntoBattlefield directly
// TODO: Check this setCode handling because it makes no sense if token put into play with e.g. "Feldon of the third Path"
@ -156,55 +150,76 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
}
}
}
if (!expansionSetCodeChecked) {
expansionSetCodeChecked = this.updateExpansionSetCode(setCode);
}
return setCode;
}
GameEvent event = new GameEvent(EventType.CREATE_TOKEN, null, sourceId, controllerId, amount, this.isCreature());
@Override
public boolean putOntoBattlefield(int amount, Game game, UUID sourceId, UUID controllerId, boolean tapped, boolean attacking, UUID attackedPlayer) {
Player controller = game.getPlayer(controllerId);
if (controller == null) {
return false;
}
lastAddedTokenIds.clear();
GameEvent event = new GameEvent(EventType.CREATE_TOKEN, sourceId, controllerId, amount, this);
if (!game.replaceEvent(event)) {
amount = event.getAmount();
List<Permanent> permanents = new ArrayList<>();
List<Permanent> permanentsEntered = new ArrayList<>();
for (int i = 0; i < amount; i++) {
PermanentToken newToken = new PermanentToken(this, event.getPlayerId(), setCode, game); // use event.getPlayerId() because it can be replaced by replacement effect
game.getState().addCard(newToken);
permanents.add(newToken);
game.getPermanentsEntering().put(newToken.getId(), newToken);
newToken.setTapped(tapped);
}
game.setScopeRelevant(true);
for (Permanent permanent : permanents) {
if (permanent.entersBattlefield(sourceId, game, Zone.OUTSIDE, true)) {
permanentsEntered.add(permanent);
} else {
game.getPermanentsEntering().remove(permanent.getId());
}
}
game.setScopeRelevant(false);
for (Permanent permanent : permanentsEntered) {
game.addPermanent(permanent);
permanent.setZone(Zone.BATTLEFIELD, game);
game.getPermanentsEntering().remove(permanent.getId());
this.lastAddedTokenIds.add(permanent.getId());
this.lastAddedTokenId = permanent.getId();
game.addSimultaneousEvent(new ZoneChangeEvent(permanent, permanent.getControllerId(), Zone.OUTSIDE, Zone.BATTLEFIELD));
if (attacking && game.getCombat() != null && game.getActivePlayerId().equals(permanent.getControllerId())) {
game.getCombat().addAttackingCreature(permanent.getId(), game, attackedPlayer);
}
if (!game.isSimulation()) {
game.informPlayers(controller.getLogName() + " creates a " + permanent.getLogName() + " token");
}
}
game.getState().applyEffects(game); // Needed to do it here without LKIReset i.e. do get SwordOfTheMeekTest running correctly.
putOntoBattlefieldHelper(event, game, tapped, attacking, attackedPlayer);
return true;
}
return false;
}
private static void putOntoBattlefieldHelper(GameEvent event, Game game, boolean tapped, boolean attacking, UUID attackedPlayer) {
Player controller = game.getPlayer(event.getPlayerId());
Token token = event.getToken();
int amount = event.getAmount();
List<Permanent> permanents = new ArrayList<>();
List<Permanent> permanentsEntered = new ArrayList<>();
String setCode = token instanceof TokenImpl ? ((TokenImpl) token).getSetCode(game, event.getSourceId()) : null;
for (int i = 0; i < amount; i++) {
PermanentToken newToken = new PermanentToken(token, event.getPlayerId(), setCode, game); // use event.getPlayerId() because it can be replaced by replacement effect
game.getState().addCard(newToken);
permanents.add(newToken);
game.getPermanentsEntering().put(newToken.getId(), newToken);
newToken.setTapped(tapped);
}
game.setScopeRelevant(true);
for (Permanent permanent : permanents) {
if (permanent.entersBattlefield(event.getSourceId(), game, Zone.OUTSIDE, true)) {
permanentsEntered.add(permanent);
} else {
game.getPermanentsEntering().remove(permanent.getId());
}
}
game.setScopeRelevant(false);
for (Permanent permanent : permanentsEntered) {
game.addPermanent(permanent);
permanent.setZone(Zone.BATTLEFIELD, game);
game.getPermanentsEntering().remove(permanent.getId());
if (token instanceof TokenImpl) {
((TokenImpl) token).lastAddedTokenIds.add(permanent.getId());
((TokenImpl) token).lastAddedTokenId = permanent.getId();
}
game.addSimultaneousEvent(new ZoneChangeEvent(permanent, permanent.getControllerId(), Zone.OUTSIDE, Zone.BATTLEFIELD));
if (attacking && game.getCombat() != null && game.getActivePlayerId().equals(permanent.getControllerId())) {
game.getCombat().addAttackingCreature(permanent.getId(), game, attackedPlayer);
}
if (!game.isSimulation()) {
game.informPlayers(controller.getLogName() + " creates a " + permanent.getLogName() + " token");
}
}
game.getState().applyEffects(game); // Needed to do it here without LKIReset i.e. do get SwordOfTheMeekTest running correctly.
}
@Override
public void setPower(int power) {
this.power.setValue(power);