Search in sources :

Example 26 with PermanentCard

use of mage.game.permanent.PermanentCard in project mage by magefree.

the class PlayerImpl method moveCardToCommandWithInfo.

@Override
public boolean moveCardToCommandWithInfo(Card card, Ability source, Game game, Zone fromZone) {
    if (card == null) {
        return false;
    }
    boolean result = false;
    if (card.moveToZone(Zone.COMMAND, source, game, true)) {
        if (!game.isSimulation()) {
            if (card instanceof PermanentCard && game.getCard(card.getId()) != null) {
                card = game.getCard(card.getId());
            }
            StringBuilder sb = new StringBuilder(this.getLogName()).append(" puts ").append(card.getLogName()).append(' ');
            if (fromZone != null) {
                sb.append("from ").append(fromZone.toString().toLowerCase(Locale.ENGLISH)).append(' ');
            }
            if (card.isOwnedBy(getId())) {
                sb.append(" to their command zone");
            } else {
                Player player = game.getPlayer(card.getOwnerId());
                if (player != null) {
                    sb.append(" to ").append(player.getLogName()).append("'s command zone");
                }
            }
            sb.append(CardUtil.getSourceLogName(game, source, card.getId()));
            game.informPlayers(sb.toString());
        }
        result = true;
    }
    return result;
}
Also used : MatchPlayer(mage.game.match.MatchPlayer) PermanentCard(mage.game.permanent.PermanentCard)

Example 27 with PermanentCard

use of mage.game.permanent.PermanentCard in project mage by magefree.

the class Spell method resolve.

@Override
public boolean resolve(Game game) {
    boolean result;
    Player controller = game.getPlayer(getControllerId());
    if (controller == null) {
        return false;
    }
    this.resolving = true;
    if (commandedBy != null && !commandedBy.equals(getControllerId())) {
        Player turnController = game.getPlayer(commandedBy);
        if (turnController != null) {
            turnController.controlPlayersTurn(game, controller.getId());
        }
    }
    if (this.isInstantOrSorcery(game)) {
        int index = 0;
        result = false;
        boolean legalParts = false;
        boolean notTargeted = true;
        // check for legal parts
        for (SpellAbility spellAbility : this.spellAbilities) {
            // if only a spliced spell has targets and all targets ar illegal, the complete spell is countered
            if (hasTargets(spellAbility, game)) {
                notTargeted = false;
                legalParts |= spellAbilityHasLegalParts(spellAbility, game);
            }
        }
        // resolve if legal parts
        if (notTargeted || legalParts) {
            for (SpellAbility spellAbility : this.spellAbilities) {
                // legality of targets is checked only as the spell begins to resolve, not in between modes (spliced spells handeled correctly?)
                if (spellAbilityCheckTargetsAndDeactivateModes(spellAbility, game)) {
                    for (UUID modeId : spellAbility.getModes().getSelectedModes()) {
                        spellAbility.getModes().setActiveMode(modeId);
                        result |= spellAbility.resolve(game);
                    }
                    index++;
                }
            }
            if (game.getState().getZone(card.getMainCard().getId()) == Zone.STACK) {
                if (isCopy()) {
                    // copied spell, only remove from stack
                    game.getStack().remove(this, game);
                } else {
                    controller.moveCards(card, Zone.GRAVEYARD, ability, game);
                }
            }
            return result;
        }
        // 20091005 - 608.2b
        if (!game.isSimulation()) {
            game.informPlayers(getName() + " has been fizzled.");
        }
        counter(null, /*this.getSpellAbility()*/
        game);
        return false;
    } else if (this.isEnchantment(game) && this.hasSubtype(SubType.AURA, game)) {
        if (ability.getTargets().stillLegal(ability, game)) {
            boolean bestow = SpellAbilityCastMode.BESTOW.equals(ability.getSpellAbilityCastMode());
            if (bestow) {
                // before put to play:
                // Must be removed first time, after that will be removed by continous effect
                // Otherwise effects like evolve trigger from creature comes into play event
                card.removeCardType(CardType.CREATURE);
                card.addSubType(game, SubType.AURA);
            }
            UUID permId;
            boolean flag;
            if (isCopy()) {
                EmptyToken token = new EmptyToken();
                CardUtil.copyTo(token).from(card, game, this);
                // The token that a resolving copy of a spell becomes isn’t said to have been “created.” (2020-09-25)
                if (token.putOntoBattlefield(1, game, ability, getControllerId(), false, false, null, false)) {
                    permId = token.getLastAddedToken();
                    flag = true;
                } else {
                    permId = null;
                    flag = false;
                }
            } else {
                permId = card.getId();
                flag = controller.moveCards(card, Zone.BATTLEFIELD, ability, game, false, faceDown, false, null);
            }
            if (flag) {
                if (bestow) {
                    // card will be copied during putOntoBattlefield, so the card of CardPermanent has to be changed
                    // TODO: Find a better way to prevent bestow creatures from being effected by creature affecting abilities
                    Permanent permanent = game.getPermanent(permId);
                    if (permanent instanceof PermanentCard) {
                        // after put to play:
                        // restore removed stats (see "before put to play" above)
                        // otherwise spell ability without bestow will be set
                        permanent.setSpellAbility(ability);
                        card.addCardType(CardType.CREATURE);
                        card.getSubtype().remove(SubType.AURA);
                    }
                }
                if (isCopy()) {
                    Permanent token = game.getPermanent(permId);
                    if (token == null) {
                        return false;
                    }
                    for (Ability ability2 : token.getAbilities()) {
                        if (!bestow || ability2 instanceof BestowAbility) {
                            ability2.getTargets().get(0).add(ability.getFirstTarget(), game);
                            ability2.getEffects().get(0).apply(game, ability2);
                            return ability2.resolve(game);
                        }
                    }
                    return false;
                }
                return ability.resolve(game);
            }
            if (bestow) {
                card.addCardType(game, CardType.CREATURE);
            }
            return false;
        }
        // Aura has no legal target and its a bestow enchantment -> Add it to battlefield as creature
        if (SpellAbilityCastMode.BESTOW.equals(this.getSpellAbility().getSpellAbilityCastMode())) {
            if (controller.moveCards(card, Zone.BATTLEFIELD, ability, game, false, faceDown, false, null)) {
                Permanent permanent = game.getPermanent(card.getId());
                if (permanent instanceof PermanentCard) {
                    ((PermanentCard) permanent).getCard().addCardType(game, CardType.CREATURE);
                    ((PermanentCard) permanent).getCard().removeSubType(game, SubType.AURA);
                    return true;
                }
            }
            return false;
        } else {
            // 20091005 - 608.2b
            if (!game.isSimulation()) {
                game.informPlayers(getName() + " has been fizzled.");
            }
            counter(null, /*this.getSpellAbility()*/
            game);
            return false;
        }
    } else if (isCopy()) {
        EmptyToken token = new EmptyToken();
        CardUtil.copyTo(token).from(card, game, this);
        // The token that a resolving copy of a spell becomes isn’t said to have been “created.” (2020-09-25)
        token.putOntoBattlefield(1, game, ability, getControllerId(), false, false, null, false);
        return true;
    } else {
        return controller.moveCards(card, Zone.BATTLEFIELD, ability, game, false, faceDown, false, null);
    }
}
Also used : TransformAbility(mage.abilities.keyword.TransformAbility) MorphAbility(mage.abilities.keyword.MorphAbility) BestowAbility(mage.abilities.keyword.BestowAbility) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) BestowAbility(mage.abilities.keyword.BestowAbility) EmptyToken(mage.game.permanent.token.EmptyToken) PermanentCard(mage.game.permanent.PermanentCard)

Example 28 with PermanentCard

use of mage.game.permanent.PermanentCard in project mage by magefree.

the class NascentMetamorphEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getFirstTarget());
    if (player == null) {
        return false;
    }
    Cards toReveal = new CardsImpl();
    Card toCopy = null;
    for (Card card : player.getLibrary().getCards(game)) {
        toReveal.add(card);
        if (card == null || !card.isCreature(game)) {
            continue;
        }
        toCopy = card;
        break;
    }
    Permanent permanent = game.getPermanent(source.getSourceId());
    if (toCopy != null && permanent != null) {
        game.copyPermanent(Duration.EndOfTurn, new PermanentCard(toCopy, source.getControllerId(), game), permanent.getId(), source, new EmptyCopyApplier());
    }
    player.revealCards(source, toReveal, game);
    player.putCardsOnBottomOfLibrary(toReveal, game, source, false);
    return true;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) EmptyCopyApplier(mage.util.functions.EmptyCopyApplier) PermanentCard(mage.game.permanent.PermanentCard) PermanentCard(mage.game.permanent.PermanentCard)

Example 29 with PermanentCard

use of mage.game.permanent.PermanentCard in project mage by magefree.

the class YorionSkyNomadEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourceObject = source.getSourceObject(game);
    if (sourceObject == null || controller == null) {
        return false;
    }
    TargetPermanent target = new TargetPermanent(0, Integer.MAX_VALUE, filter, true);
    controller.choose(outcome, target, source.getSourceId(), game);
    Set<Card> toExile = target.getTargets().stream().map(game::getPermanent).collect(Collectors.toSet());
    UUID exileId = CardUtil.getExileZoneId(game, source.getSourceId(), source.getSourceObjectZoneChangeCounter());
    controller.moveCardsToExile(toExile, source, game, true, exileId, sourceObject.getIdName());
    Cards cardsToReturn = new CardsImpl();
    for (Card exiled : toExile) {
        if (exiled instanceof PermanentMeld) {
            MeldCard meldCard = (MeldCard) ((PermanentCard) exiled).getCard();
            Card topCard = meldCard.getTopHalfCard();
            Card bottomCard = meldCard.getBottomHalfCard();
            if (topCard.getZoneChangeCounter(game) == meldCard.getTopLastZoneChangeCounter()) {
                cardsToReturn.add(topCard);
            }
            if (bottomCard.getZoneChangeCounter(game) == meldCard.getBottomLastZoneChangeCounter()) {
                cardsToReturn.add(bottomCard);
            }
        } else if (exiled.getZoneChangeCounter(game) == game.getState().getZoneChangeCounter(exiled.getId()) - 1) {
            cardsToReturn.add(exiled);
        }
    }
    Effect effect = new ReturnToBattlefieldUnderOwnerControlTargetEffect(false, false);
    effect.setTargetPointer(new FixedTargets(cardsToReturn, game));
    AtTheBeginOfNextEndStepDelayedTriggeredAbility delayedAbility = new AtTheBeginOfNextEndStepDelayedTriggeredAbility(effect);
    game.addDelayedTriggeredAbility(delayedAbility, source);
    return true;
}
Also used : Player(mage.players.Player) FixedTargets(mage.target.targetpointer.FixedTargets) MageObject(mage.MageObject) ReturnToBattlefieldUnderOwnerControlTargetEffect(mage.abilities.effects.common.ReturnToBattlefieldUnderOwnerControlTargetEffect) PermanentCard(mage.game.permanent.PermanentCard) AtTheBeginOfNextEndStepDelayedTriggeredAbility(mage.abilities.common.delayed.AtTheBeginOfNextEndStepDelayedTriggeredAbility) PermanentMeld(mage.game.permanent.PermanentMeld) ReturnToBattlefieldUnderOwnerControlTargetEffect(mage.abilities.effects.common.ReturnToBattlefieldUnderOwnerControlTargetEffect) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) TargetPermanent(mage.target.TargetPermanent) UUID(java.util.UUID)

Example 30 with PermanentCard

use of mage.game.permanent.PermanentCard in project mage by magefree.

the class CustomTestCard method addCustomCardWithAbility.

protected void addCustomCardWithAbility(String customName, TestPlayer controllerPlayer, Ability ability, SpellAbility spellAbility, CardType cardType, String spellCost, Zone putAtZone, SubType... additionalSubTypes) {
    CustomTestCard.clearCustomAbilities(customName);
    CustomTestCard.addCustomAbility(customName, spellAbility, ability);
    CustomTestCard.clearAdditionalSubtypes(customName);
    CustomTestCard.addAdditionalSubtypes(customName, additionalSubTypes);
    CardSetInfo testSet = new CardSetInfo(customName, "custom", "123", Rarity.COMMON);
    Card newCard = new CustomTestCard(controllerPlayer.getId(), testSet, cardType, spellCost);
    Card permCard = CardUtil.getDefaultCardSideForBattlefield(currentGame, newCard);
    PermanentCard permanent = new PermanentCard(permCard, controllerPlayer.getId(), currentGame);
    switch(putAtZone) {
        case BATTLEFIELD:
            getBattlefieldCards(controllerPlayer).add(permanent);
            break;
        case GRAVEYARD:
            getGraveCards(controllerPlayer).add(newCard);
            break;
        case HAND:
            getHandCards(controllerPlayer).add(newCard);
            break;
        case LIBRARY:
            getLibraryCards(controllerPlayer).add(newCard);
            break;
        case COMMAND:
            getCommandCards(controllerPlayer).add(newCard);
            break;
        default:
            Assert.fail("Unsupported zone: " + putAtZone);
    }
}
Also used : CardSetInfo(mage.cards.CardSetInfo) Card(mage.cards.Card) PermanentCard(mage.game.permanent.PermanentCard) PermanentCard(mage.game.permanent.PermanentCard)

Aggregations

PermanentCard (mage.game.permanent.PermanentCard)42 Card (mage.cards.Card)22 Permanent (mage.game.permanent.Permanent)16 Player (mage.players.Player)16 Ability (mage.abilities.Ability)11 MageObject (mage.MageObject)9 CardInfo (mage.cards.repository.CardInfo)9 CopyEffect (mage.abilities.effects.common.CopyEffect)8 FilterCard (mage.filter.FilterCard)6 CopyApplier (mage.util.functions.CopyApplier)6 UUID (java.util.UUID)5 PermanentMeld (mage.game.permanent.PermanentMeld)4 Test (org.junit.Test)4 SimpleActivatedAbility (mage.abilities.common.SimpleActivatedAbility)3 SimpleStaticAbility (mage.abilities.common.SimpleStaticAbility)3 CreateTokenCopyTargetEffect (mage.abilities.effects.common.CreateTokenCopyTargetEffect)3 FilterCreatureCard (mage.filter.common.FilterCreatureCard)3 PermanentToken (mage.game.permanent.PermanentToken)3 TargetCard (mage.target.TargetCard)3 Matcher (java.util.regex.Matcher)2