Test framework: added support to put mdf cards on battlefield by cheats or unit tests;

This commit is contained in:
Oleg Agafonov 2021-02-03 14:23:50 +04:00
parent be77ecea84
commit 4b9de96a6b
7 changed files with 111 additions and 49 deletions

View file

@ -8,15 +8,20 @@ import mage.abilities.costs.mana.ManaCosts;
import mage.abilities.keyword.TransformAbility;
import mage.cards.Card;
import mage.cards.LevelerCard;
import mage.cards.ModalDoubleFacesCard;
import mage.cards.SplitCard;
import mage.constants.SpellAbilityType;
import mage.game.Game;
import mage.game.events.ZoneChangeEvent;
import mage.util.CardUtil;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import java.util.UUID;
/**
* Static permanent on the battlefield. There are possible multiple permanents per one card,
* so be carefull for targets (ids are different) and ZCC (zcc is static for permanent).
*
* @author BetaSteward_at_googlemail.com
*/
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@ -31,6 +36,22 @@ public class PermanentCard extends PermanentImpl {
public PermanentCard(Card card, UUID controllerId, Game game) {
super(card.getId(), card.getOwnerId(), controllerId, card.getName());
// usage check: you must put to play only real card's part
// if you use it in test code then call CardUtil.getDefaultCardSideForBattlefield for default side
// it's a basic check and still allows to create permanent from instant or sorcery
boolean goodForBattlefield = true;
if (card instanceof ModalDoubleFacesCard) {
goodForBattlefield = false;
} else if (card instanceof SplitCard) {
// fused spells allowed (it uses main card)
if (card.getSpellAbility() != null && !card.getSpellAbility().getSpellAbilityType().equals(SpellAbilityType.SPLIT_FUSED)) {
goodForBattlefield = false;
}
}
if (!goodForBattlefield) {
throw new IllegalArgumentException("ERROR, can't create permanent card from split or mdf: " + card.getName());
}
this.card = card;
this.zoneChangeCounter = card.getZoneChangeCounter(game); // local value already set to the raised number
init(card, game);

View file

@ -15,10 +15,7 @@ import mage.abilities.effects.common.asthought.CanPlayCardControllerEffect;
import mage.abilities.effects.common.asthought.YouMaySpendManaAsAnyColorToCastTargetEffect;
import mage.abilities.hint.Hint;
import mage.abilities.hint.HintUtils;
import mage.cards.Card;
import mage.cards.MeldCard;
import mage.cards.ModalDoubleFacesCard;
import mage.cards.SplitCard;
import mage.cards.*;
import mage.constants.*;
import mage.filter.Filter;
import mage.filter.predicate.mageobject.NamePredicate;
@ -896,33 +893,36 @@ public final class CardUtil {
/**
* Put card to battlefield without resolve (for cheats and tests only)
*
* @param source must be non null (if you need it empty then use fakeSourceAbility)
* @param source must be non null (if you need it empty then use fakeSourceAbility)
* @param game
* @param card
* @param newCard
* @param player
*/
public static void putCardOntoBattlefieldWithEffects(Ability source, Game game, Card card, Player player) {
public static void putCardOntoBattlefieldWithEffects(Ability source, Game game, Card newCard, Player player) {
// same logic as ZonesHandler->maybeRemoveFromSourceZone
// workaround to put real permanent from one side (example: you call mdf card by cheats)
Card permCard = getDefaultCardSideForBattlefield(newCard);
// prepare card and permanent
card.setZone(Zone.BATTLEFIELD, game);
card.setOwnerId(player.getId());
permCard.setZone(Zone.BATTLEFIELD, game);
permCard.setOwnerId(player.getId());
PermanentCard permanent;
if (card instanceof MeldCard) {
permanent = new PermanentMeld(card, player.getId(), game);
if (permCard instanceof MeldCard) {
permanent = new PermanentMeld(permCard, player.getId(), game);
} else {
permanent = new PermanentCard(card, player.getId(), game);
permanent = new PermanentCard(permCard, player.getId(), game);
}
// put onto battlefield with possible counters
game.getPermanentsEntering().put(permanent.getId(), permanent);
card.checkForCountersToAdd(permanent, source, game);
permCard.checkForCountersToAdd(permanent, source, game);
permanent.entersBattlefield(source, game, Zone.OUTSIDE, false);
game.addPermanent(permanent, game.getState().getNextPermanentOrderNumber());
game.getPermanentsEntering().remove(permanent.getId());
// workaround for special tapped status from test framework's command (addCard)
if (card instanceof PermanentCard && ((PermanentCard) card).isTapped()) {
if (permCard instanceof PermanentCard && ((PermanentCard) permCard).isTapped()) {
permanent.setTapped(true);
}
@ -938,6 +938,27 @@ public final class CardUtil {
}
}
/**
* Choose default card's part to put on battlefield (for cheats and tests only)
*
* @param card
* @return
*/
public static Card getDefaultCardSideForBattlefield(Card card) {
// chose left side all time
Card permCard;
if (card instanceof SplitCard) {
permCard = card;
} else if (card instanceof AdventureCard) {
permCard = card;
} else if (card instanceof ModalDoubleFacesCard) {
permCard = ((ModalDoubleFacesCard) card).getLeftHalfCard();
} else {
permCard = card;
}
return permCard;
}
/**
* Return card name for same name searching
*