Search in sources :

Example 1 with BestowAbility

use of mage.abilities.keyword.BestowAbility 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)

Aggregations

BestowAbility (mage.abilities.keyword.BestowAbility)1 MorphAbility (mage.abilities.keyword.MorphAbility)1 TransformAbility (mage.abilities.keyword.TransformAbility)1 Permanent (mage.game.permanent.Permanent)1 PermanentCard (mage.game.permanent.PermanentCard)1 EmptyToken (mage.game.permanent.token.EmptyToken)1 Player (mage.players.Player)1