Modal Double Faces cards fixes:

* Fixed that mdf card can duplicate triggers (example: Skyclave Cleric, see #7187);
 * Fixed that mdf card can raise triggers from another side (example: Kazandu Mammoth, see #7180);
This commit is contained in:
Oleg Agafonov 2020-11-18 02:04:32 +04:00
parent 6dcf7a2e53
commit 656653f38b
5 changed files with 132 additions and 31 deletions

View file

@ -549,4 +549,48 @@ public class ModalDoubleFacesCardsTest extends CardTestPlayerBase {
assertPermanentCount(playerA, "Balduvian Bears", 2); assertPermanentCount(playerA, "Balduvian Bears", 2);
} }
@Test
public void test_ETB_OnlySideCardsCanAddAbilitiesToGame() {
// possible bug: double triggers (loadCard adds abilities from main + side cards instead side card only)
// https://github.com/magefree/mage/issues/7187
// Skyclave Cleric
// creature 1/3
// When Skyclave Cleric enters the battlefield, you gain 2 life.
addCard(Zone.HAND, playerA, "Skyclave Cleric"); // {1}{W}
addCard(Zone.BATTLEFIELD, playerA, "Plains", 2);
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Skyclave Cleric");
setStrictChooseMode(true);
setStopAt(1, PhaseStep.END_TURN);
execute();
assertAllCommandsUsed();
assertLife(playerA, 20 + 2); // +2 from 1 etb trigger
}
@Test
public void test_ETB_OnlyActualSideCardCanRaiseTriggers() {
// possible bug: you play one card but game raise triggers from another side too
// https://github.com/magefree/mage/issues/7180
// Kazandu Mammoth
// creature 3/3
// Landfall Whenever a land enters the battlefield under your control, Kazandu Mammoth gets +2/+2 until end of turn.
//
// Kazandu Valley
// land
addCard(Zone.HAND, playerA, "Kazandu Mammoth"); // {1}{G}{G}
// play land, but no landfall triggers from other side
playLand(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Kazandu Valley");
checkStackSize("no triggers", 1, PhaseStep.PRECOMBAT_MAIN, playerA, 0);
setStrictChooseMode(true);
setStopAt(1, PhaseStep.END_TURN);
execute();
assertAllCommandsUsed();
}
} }

View file

@ -1149,7 +1149,11 @@ public class TestPlayer implements Player {
} }
private void printAbilities(Game game, List<? extends Ability> abilities) { private void printAbilities(Game game, List<? extends Ability> abilities) {
System.out.println("Total abilities: " + (abilities != null ? abilities.size() : 0)); printAbilities("Total abilities", game, abilities);
}
private void printAbilities(String info, Game game, List<? extends Ability> abilities) {
System.out.println(info + ": " + (abilities != null ? abilities.size() : 0));
if (abilities == null) { if (abilities == null) {
return; return;
} }
@ -1484,6 +1488,9 @@ public class TestPlayer implements Player {
} }
private void assertStackSize(PlayerAction action, Game game, int needStackSize) { private void assertStackSize(PlayerAction action, Game game, int needStackSize) {
if (game.getStack().size() != needStackSize) {
printAbilities("Current stack", game, game.getStack().stream().map(StackObject::getStackAbility).collect(Collectors.toList()));
}
Assert.assertEquals(action.getActionName() + " - stack size must be " + needStackSize + " but is " + game.getStack().size(), needStackSize, game.getStack().size()); Assert.assertEquals(action.getActionName() + " - stack size must be " + needStackSize + " but is " + game.getStack().size(), needStackSize, game.getStack().size());
} }

View file

@ -241,40 +241,43 @@ public abstract class GameImpl implements Game, Serializable {
if (card instanceof PermanentCard) { if (card instanceof PermanentCard) {
card = ((PermanentCard) card).getCard(); card = ((PermanentCard) card).getCard();
} }
// main card
card.setOwnerId(ownerId); card.setOwnerId(ownerId);
gameCards.put(card.getId(), card); addCardToState(card);
state.addCard(card);
// parts
if (card instanceof SplitCard) { if (card instanceof SplitCard) {
// left // left
Card leftCard = ((SplitCard) card).getLeftHalfCard(); Card leftCard = ((SplitCard) card).getLeftHalfCard();
leftCard.setOwnerId(ownerId); leftCard.setOwnerId(ownerId);
gameCards.put(leftCard.getId(), leftCard); addCardToState(leftCard);
state.addCard(leftCard);
// right // right
Card rightCard = ((SplitCard) card).getRightHalfCard(); Card rightCard = ((SplitCard) card).getRightHalfCard();
rightCard.setOwnerId(ownerId); rightCard.setOwnerId(ownerId);
gameCards.put(rightCard.getId(), rightCard); addCardToState(rightCard);
state.addCard(rightCard);
} else if (card instanceof ModalDoubleFacesCard) { } else if (card instanceof ModalDoubleFacesCard) {
// left // left
Card leftCard = ((ModalDoubleFacesCard) card).getLeftHalfCard(); Card leftCard = ((ModalDoubleFacesCard) card).getLeftHalfCard();
leftCard.setOwnerId(ownerId); leftCard.setOwnerId(ownerId);
gameCards.put(leftCard.getId(), leftCard); addCardToState(leftCard);
state.addCard(leftCard);
// right // right
Card rightCard = ((ModalDoubleFacesCard) card).getRightHalfCard(); Card rightCard = ((ModalDoubleFacesCard) card).getRightHalfCard();
rightCard.setOwnerId(ownerId); rightCard.setOwnerId(ownerId);
gameCards.put(rightCard.getId(), rightCard); addCardToState(rightCard);
state.addCard(rightCard);
} else if (card instanceof AdventureCard) { } else if (card instanceof AdventureCard) {
Card spellCard = ((AdventureCard) card).getSpellCard(); Card spellCard = ((AdventureCard) card).getSpellCard();
spellCard.setOwnerId(ownerId); spellCard.setOwnerId(ownerId);
gameCards.put(spellCard.getId(), spellCard); addCardToState(spellCard);
state.addCard(spellCard);
} }
} }
} }
private void addCardToState(Card card) {
gameCards.put(card.getId(), card);
state.addCard(card);
}
@Override @Override
public Collection<Card> getCards() { public Collection<Card> getCards() {
return gameCards.values(); return gameCards.values();

View file

@ -614,7 +614,7 @@ public class GameState implements Serializable, Copyable<GameState> {
} }
public void addEffect(ContinuousEffect effect, Ability source) { public void addEffect(ContinuousEffect effect, Ability source) {
effects.addEffect(effect, source); addEffect(effect, null, source);
} }
public void addEffect(ContinuousEffect effect, UUID sourceId, Ability source) { public void addEffect(ContinuousEffect effect, UUID sourceId, Ability source) {
@ -625,9 +625,17 @@ public class GameState implements Serializable, Copyable<GameState> {
} }
} }
// public void addMessage(String message) { private void addTrigger(TriggeredAbility ability, MageObject attachedTo) {
// this.messages.add(message); addTrigger(ability, null, attachedTo);
// } }
private void addTrigger(TriggeredAbility ability, UUID sourceId, MageObject attachedTo) {
if (sourceId == null) {
triggers.add(ability, attachedTo);
} else {
triggers.add(ability, sourceId, attachedTo);
}
}
/** /**
* Returns a list of all players of the game ignoring range or if a player * Returns a list of all players of the game ignoring range or if a player
@ -839,6 +847,14 @@ public class GameState implements Serializable, Copyable<GameState> {
public void addCard(Card card) { public void addCard(Card card) {
setZone(card.getId(), Zone.OUTSIDE); setZone(card.getId(), Zone.OUTSIDE);
// dirty hack to fix double triggers, see https://github.com/magefree/mage/issues/7187
// main mdf card don't have attached abilities, only parts contains it
if (card instanceof ModalDoubleFacesCard) {
return;
}
// add card's abilities to game
for (Ability ability : card.getAbilities()) { for (Ability ability : card.getAbilities()) {
addAbility(ability, null, card); addAbility(ability, null, card);
} }
@ -888,7 +904,7 @@ public class GameState implements Serializable, Copyable<GameState> {
} }
} }
} else if (ability instanceof TriggeredAbility) { } else if (ability instanceof TriggeredAbility) {
this.triggers.add((TriggeredAbility) ability, attachedTo); addTrigger((TriggeredAbility) ability, null, attachedTo);
} }
} }
@ -911,8 +927,7 @@ public class GameState implements Serializable, Copyable<GameState> {
} }
} }
} else if (ability instanceof TriggeredAbility) { } else if (ability instanceof TriggeredAbility) {
// TODO: add sources for triggers - the same way as in addEffect: sources addTrigger((TriggeredAbility) ability, sourceId, attachedTo);
this.triggers.add((TriggeredAbility) ability, sourceId, attachedTo);
} }
List<Watcher> watcherList = new ArrayList<>(ability.getWatchers()); // Workaround to prevent ConcurrentModificationException, not clear to me why this is happening now List<Watcher> watcherList = new ArrayList<>(ability.getWatchers()); // Workaround to prevent ConcurrentModificationException, not clear to me why this is happening now

View file

@ -117,25 +117,57 @@ public final class ZonesHandler {
Card targetCard = getTargetCard(game, event.getTargetId()); Card targetCard = getTargetCard(game, event.getTargetId());
Cards cardsToMove = null; // moving real cards Cards cardsToMove = null; // moving real cards
Cards cardsToUpdate = null; // updating all card's parts Map<Zone, Cards> cardsToUpdate = new LinkedHashMap<>(); // updating all card's parts (must be ordered LinkedHashMap)
cardsToUpdate.put(toZone, new CardsImpl());
cardsToUpdate.put(Zone.OUTSIDE, new CardsImpl());
// if we're moving a token it shouldn't be put into any zone as an object. // if we're moving a token it shouldn't be put into any zone as an object.
if (!(targetCard instanceof Permanent) && targetCard != null) { if (!(targetCard instanceof Permanent) && targetCard != null) {
if (targetCard instanceof MeldCard) { if (targetCard instanceof MeldCard) {
// meld/group cards must be independent (use can choose order) // meld/group cards must be independent (use can choose order)
cardsToMove = ((MeldCard) targetCard).getHalves(); cardsToMove = ((MeldCard) targetCard).getHalves();
cardsToUpdate = cardsToMove; cardsToUpdate.get(toZone).addAll(cardsToMove);
} else if (targetCard instanceof ModalDoubleFacesCard } else if (targetCard instanceof ModalDoubleFacesCard
|| targetCard instanceof ModalDoubleFacesCardHalf) { || targetCard instanceof ModalDoubleFacesCardHalf) {
// mdf cards must be moved as single object, but each half must be updated separetly // mdf cards must be moved as single object, but each half must be updated separately
ModalDoubleFacesCard mdfCard = (ModalDoubleFacesCard) targetCard.getMainCard(); ModalDoubleFacesCard mdfCard = (ModalDoubleFacesCard) targetCard.getMainCard();
cardsToMove = new CardsImpl(mdfCard); cardsToMove = new CardsImpl(mdfCard);
cardsToUpdate = new CardsImpl(mdfCard); cardsToUpdate.get(toZone).add(mdfCard);
cardsToUpdate.add(mdfCard.getLeftHalfCard()); // example: cast left side
cardsToUpdate.add(mdfCard.getRightHalfCard()); // result:
// * main to battlefield
// * left to battlefield
// * right to outside (it helps to ignore all triggers and other effects from that card)
switch (toZone) {
case STACK:
case BATTLEFIELD:
if (targetCard.getId().equals(mdfCard.getLeftHalfCard().getId())) {
// play left
cardsToUpdate.get(toZone).add(mdfCard.getLeftHalfCard());
cardsToUpdate.get(Zone.OUTSIDE).add(mdfCard.getRightHalfCard());
} else if (targetCard.getId().equals(mdfCard.getRightHalfCard().getId())) {
// play right
cardsToUpdate.get(toZone).add(mdfCard.getRightHalfCard());
cardsToUpdate.get(Zone.OUTSIDE).add(mdfCard.getLeftHalfCard());
} else {
// cast mdf (only on stack)
if (!toZone.equals(Zone.STACK)) {
throw new IllegalStateException("Wrong mdf card move to " + toZone + " in placeInDestinationZone");
}
cardsToUpdate.get(toZone).add(mdfCard.getLeftHalfCard());
cardsToUpdate.get(toZone).add(mdfCard.getRightHalfCard());
}
break;
default:
// move all parts
cardsToUpdate.get(toZone).add(mdfCard.getLeftHalfCard());
cardsToUpdate.get(toZone).add(mdfCard.getRightHalfCard());
break;
}
} else { } else {
cardsToMove = new CardsImpl(targetCard); cardsToMove = new CardsImpl(targetCard);
cardsToUpdate = cardsToMove; cardsToUpdate.get(toZone).addAll(cardsToMove);
} }
Player owner = game.getPlayer(targetCard.getOwnerId()); Player owner = game.getPlayer(targetCard.getOwnerId());
switch (toZone) { switch (toZone) {
case HAND: case HAND:
@ -208,13 +240,13 @@ public final class ZonesHandler {
game.setZone(event.getTargetId(), event.getToZone()); game.setZone(event.getTargetId(), event.getToZone());
// update zone in other parts (meld cards, mdf half cards) // update zone in other parts (meld cards, mdf half cards)
if (cardsToUpdate != null) { cardsToUpdate.entrySet().forEach(entry -> {
for (Card card : cardsToUpdate.getCards(game)) { for (Card card : entry.getValue().getCards(game)) {
if (!card.getId().equals(event.getTargetId())) { if (!card.getId().equals(event.getTargetId())) {
game.setZone(card.getId(), event.getToZone()); game.setZone(card.getId(), entry.getKey());
} }
} }
} });
// reset meld status // reset meld status
if (targetCard instanceof MeldCard) { if (targetCard instanceof MeldCard) {