Search in sources :

Example 36 with SpellAbility

use of mage.abilities.SpellAbility in project mage by magefree.

the class EvokeAbility method askToActivateAlternativeCosts.

@Override
public boolean askToActivateAlternativeCosts(Ability ability, Game game) {
    if (ability instanceof SpellAbility) {
        // we must use the controller of the ability here IE: Hedonist's Trove (play from not own hand when you aren't the owner)
        Player player = game.getPlayer(ability.getControllerId());
        if (player != null) {
            this.resetEvoke();
            for (AlternativeCost2 evokeCost : evokeCosts) {
                if (evokeCost.canPay(ability, this, player.getId(), game) && player.chooseUse(Outcome.Benefit, new StringBuilder(EVOKE_KEYWORD).append(" the creature for ").append(evokeCost.getText(true)).append(" ?").toString(), ability, game)) {
                    activateEvoke(evokeCost, game);
                    ability.getManaCostsToPay().clear();
                    ability.getCosts().clear();
                    for (Iterator it = ((Costs) evokeCost).iterator(); it.hasNext(); ) {
                        Cost cost = (Cost) it.next();
                        if (cost instanceof ManaCostsImpl) {
                            ability.getManaCostsToPay().add((ManaCostsImpl) cost.copy());
                        } else {
                            ability.getCosts().add(cost.copy());
                        }
                    }
                }
            }
        }
    }
    return isActivated(ability, game);
}
Also used : Player(mage.players.Player) Iterator(java.util.Iterator) SpellAbility(mage.abilities.SpellAbility) ManaCostsImpl(mage.abilities.costs.mana.ManaCostsImpl)

Example 37 with SpellAbility

use of mage.abilities.SpellAbility in project mage by magefree.

the class DashAddDelayedTriggeredAbilityEffect method askToActivateAlternativeCosts.

@Override
public boolean askToActivateAlternativeCosts(Ability ability, Game game) {
    if (ability instanceof SpellAbility) {
        // we must use the controller of the ability here IE: Hedonist's Trove (play from not own hand when you aren't the owner)
        Player player = game.getPlayer(ability.getControllerId());
        if (player != null) {
            this.resetDash();
            for (AlternativeCost2 dashCost : alternativeSourceCosts) {
                if (dashCost.canPay(ability, this, player.getId(), game) && player.chooseUse(Outcome.Benefit, KEYWORD + " the creature for " + dashCost.getText(true) + " ?", ability, game)) {
                    activateDash(dashCost, game);
                    ability.getManaCostsToPay().clear();
                    ability.getCosts().clear();
                    for (Iterator it = ((Costs) dashCost).iterator(); it.hasNext(); ) {
                        Cost cost = (Cost) it.next();
                        if (cost instanceof ManaCostsImpl) {
                            ability.getManaCostsToPay().add((ManaCostsImpl) cost.copy());
                        } else {
                            ability.getCosts().add(cost.copy());
                        }
                    }
                }
            }
        }
    }
    return isActivated(ability, game);
}
Also used : Player(mage.players.Player) Iterator(java.util.Iterator) SpellAbility(mage.abilities.SpellAbility) ManaCostsImpl(mage.abilities.costs.mana.ManaCostsImpl)

Example 38 with SpellAbility

use of mage.abilities.SpellAbility in project mage by magefree.

the class TokenImpl method putOntoBattlefieldHelper.

private static void putOntoBattlefieldHelper(CreateTokenEvent event, Game game, Ability source, boolean tapped, boolean attacking, UUID attackedPlayer, boolean created) {
    Player controller = game.getPlayer(event.getPlayerId());
    if (controller == null) {
        return;
    }
    for (Map.Entry<Token, Integer> entry : event.getTokens().entrySet()) {
        Token token = entry.getKey();
        int amount = entry.getValue();
        String setCode = token instanceof TokenImpl ? ((TokenImpl) token).getSetCode(game, event.getSourceId()) : null;
        List<Permanent> needTokens = new ArrayList<>();
        List<Permanent> allowedTokens = new ArrayList<>();
        // prepare tokens to enter
        for (int i = 0; i < amount; i++) {
            // use event.getPlayerId() as controller cause it can be replaced by replacement effect
            PermanentToken newPermanent = new PermanentToken(token, event.getPlayerId(), setCode, game);
            game.getState().addCard(newPermanent);
            needTokens.add(newPermanent);
            game.getPermanentsEntering().put(newPermanent.getId(), newPermanent);
            newPermanent.setTapped(tapped);
            ZoneChangeEvent emptyEvent = new ZoneChangeEvent(newPermanent, newPermanent.getControllerId(), Zone.OUTSIDE, Zone.BATTLEFIELD);
            // tokens zcc must simulate card's zcc too keep copied card/spell settings
            // (example: etb's kicker ability of copied creature spell, see tests with Deathforge Shaman)
            newPermanent.updateZoneChangeCounter(game, emptyEvent);
        }
        // check ETB effects
        game.setScopeRelevant(true);
        for (Permanent permanent : needTokens) {
            if (permanent.entersBattlefield(source, game, Zone.OUTSIDE, true)) {
                allowedTokens.add(permanent);
            } else {
                game.getPermanentsEntering().remove(permanent.getId());
            }
        }
        game.setScopeRelevant(false);
        // put allowed tokens to play
        int createOrder = game.getState().getNextPermanentOrderNumber();
        for (Permanent permanent : allowedTokens) {
            game.addPermanent(permanent, createOrder);
            permanent.setZone(Zone.BATTLEFIELD, game);
            game.getPermanentsEntering().remove(permanent.getId());
            // keep tokens ids
            if (token instanceof TokenImpl) {
                ((TokenImpl) token).lastAddedTokenIds.add(permanent.getId());
                ((TokenImpl) token).lastAddedTokenId = permanent.getId();
            }
            // created token events
            ZoneChangeEvent zccEvent = new ZoneChangeEvent(permanent, permanent.getControllerId(), Zone.OUTSIDE, Zone.BATTLEFIELD);
            game.addSimultaneousEvent(zccEvent);
            if (permanent instanceof PermanentToken && created) {
                game.addSimultaneousEvent(new CreatedTokenEvent(source, (PermanentToken) permanent));
            }
            // code refactored from CopyPermanentEffect
            if (permanent.getSubtype().contains(SubType.AURA)) {
                Outcome auraOutcome = Outcome.BoostCreature;
                Target auraTarget = null;
                // attach - search effect in spell ability (example: cast Utopia Sprawl, cast Estrid's Invocation on it)
                for (Ability ability : permanent.getAbilities()) {
                    if (!(ability instanceof SpellAbility)) {
                        continue;
                    }
                    auraOutcome = ability.getEffects().getOutcome(ability);
                    for (Effect effect : ability.getEffects()) {
                        if (!(effect instanceof AttachEffect)) {
                            continue;
                        }
                        if (permanent.getSpellAbility().getTargets().size() > 0) {
                            auraTarget = permanent.getSpellAbility().getTargets().get(0);
                        }
                    }
                }
                // enchant - search in all abilities (example: cast Estrid's Invocation on enchanted creature by Estrid, the Masked second ability, cast Estrid's Invocation on it)
                if (auraTarget == null) {
                    for (Ability ability : permanent.getAbilities()) {
                        if (!(ability instanceof EnchantAbility)) {
                            continue;
                        }
                        auraOutcome = ability.getEffects().getOutcome(ability);
                        if (ability.getTargets().size() > 0) {
                            // Animate Dead don't have targets
                            auraTarget = ability.getTargets().get(0);
                        }
                    }
                }
                // if this is a copy of a copy, the copy's target has been copied and needs to be cleared
                if (auraTarget == null) {
                    break;
                }
                // clear selected target
                if (auraTarget.getFirstTarget() != null) {
                    auraTarget.remove(auraTarget.getFirstTarget());
                }
                // select new target
                auraTarget.setNotTarget(true);
                if (!controller.choose(auraOutcome, auraTarget, source.getSourceId(), game)) {
                    break;
                }
                UUID targetId = auraTarget.getFirstTarget();
                Permanent targetPermanent = game.getPermanent(targetId);
                Player targetPlayer = game.getPlayer(targetId);
                if (targetPermanent != null) {
                    targetPermanent.addAttachment(permanent.getId(), source, game);
                } else if (targetPlayer != null) {
                    targetPlayer.addAttachment(permanent.getId(), source, game);
                }
            }
            // must attack
            if (attacking && game.getCombat() != null && game.getActivePlayerId().equals(permanent.getControllerId())) {
                game.getCombat().addAttackingCreature(permanent.getId(), game, attackedPlayer);
            }
            // game logs
            if (created) {
                game.informPlayers(controller.getLogName() + " creates a " + permanent.getLogName() + " token");
            } else {
                game.informPlayers(permanent.getLogName() + " enters the battlefield as a token under " + controller.getLogName() + "'s control'");
            }
        }
    }
    // Needed to do it here without LKIReset i.e. do get SwordOfTheMeekTest running correctly.
    game.getState().applyEffects(game);
}
Also used : EnchantAbility(mage.abilities.keyword.EnchantAbility) SpellAbility(mage.abilities.SpellAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) CreatedTokenEvent(mage.game.events.CreatedTokenEvent) PermanentToken(mage.game.permanent.PermanentToken) PermanentToken(mage.game.permanent.PermanentToken) SpellAbility(mage.abilities.SpellAbility) EnchantAbility(mage.abilities.keyword.EnchantAbility) AttachEffect(mage.abilities.effects.common.AttachEffect) ZoneChangeEvent(mage.game.events.ZoneChangeEvent) Target(mage.target.Target) Outcome(mage.constants.Outcome) Effect(mage.abilities.effects.Effect) AttachEffect(mage.abilities.effects.common.AttachEffect)

Example 39 with SpellAbility

use of mage.abilities.SpellAbility in project mage by magefree.

the class FeatherTheRedeemedEffect method checkSpell.

private boolean checkSpell(Spell spell, Game game) {
    if (spell == null) {
        return false;
    }
    SpellAbility sa = spell.getSpellAbility();
    for (UUID modeId : sa.getModes().getSelectedModes()) {
        Mode mode = sa.getModes().get(modeId);
        for (Target target : mode.getTargets()) {
            for (UUID targetId : target.getTargets()) {
                Permanent permanent = game.getPermanent(targetId);
                if (permanent != null && permanent.isCreature(game) && permanent.isControlledBy(getControllerId())) {
                    this.getEffects().clear();
                    this.addEffect(new FeatherTheRedeemedEffect(new MageObjectReference(spell, game)));
                    return true;
                }
            }
        }
        for (Effect effect : mode.getEffects()) {
            for (UUID targetId : effect.getTargetPointer().getTargets(game, sa)) {
                Permanent permanent = game.getPermanent(targetId);
                if (permanent != null && permanent.isCreature(game) && permanent.isControlledBy(getControllerId())) {
                    this.getEffects().clear();
                    this.addEffect(new FeatherTheRedeemedEffect(new MageObjectReference(spell, game)));
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Target(mage.target.Target) FixedTarget(mage.target.targetpointer.FixedTarget) Permanent(mage.game.permanent.Permanent) Mode(mage.abilities.Mode) SpellAbility(mage.abilities.SpellAbility) Effect(mage.abilities.effects.Effect) ReturnToHandTargetEffect(mage.abilities.effects.common.ReturnToHandTargetEffect) UUID(java.util.UUID) MageObjectReference(mage.MageObjectReference)

Example 40 with SpellAbility

use of mage.abilities.SpellAbility in project mage by magefree.

the class HydradoodleEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent permanent = game.getPermanentEntering(source.getSourceId());
    Player controller = game.getPlayer(source.getControllerId());
    if (permanent != null && controller != null) {
        SpellAbility spellAbility = (SpellAbility) getValue(EntersBattlefieldEffect.SOURCE_CAST_SPELL_ABILITY);
        if (spellAbility != null && spellAbility.getSourceId().equals(source.getSourceId()) && permanent.getZoneChangeCounter(game) == spellAbility.getSourceObjectZoneChangeCounter()) {
            int amount = spellAbility.getManaCostsToPay().getX();
            if (amount > 0) {
                int total = controller.rollDice(outcome, source, game, 6, amount, 0).stream().mapToInt(x -> x).sum();
                permanent.addCounters(CounterType.P1P1.createInstance(total), source.getControllerId(), source, game);
            }
        }
        return true;
    }
    return false;
}
Also used : EntersBattlefieldEffect(mage.abilities.effects.EntersBattlefieldEffect) CounterAnyPredicate(mage.filter.predicate.permanent.CounterAnyPredicate) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) UUID(java.util.UUID) MageInt(mage.MageInt) ReachAbility(mage.abilities.keyword.ReachAbility) SubType(mage.constants.SubType) EntersBattlefieldAbility(mage.abilities.common.EntersBattlefieldAbility) FilterPermanent(mage.filter.FilterPermanent) Player(mage.players.Player) CardSetInfo(mage.cards.CardSetInfo) SpellAbility(mage.abilities.SpellAbility) Game(mage.game.Game) CardImpl(mage.cards.CardImpl) Permanent(mage.game.permanent.Permanent) CardType(mage.constants.CardType) TrampleAbility(mage.abilities.keyword.TrampleAbility) CounterType(mage.counters.CounterType) Ability(mage.abilities.Ability) Player(mage.players.Player) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) SpellAbility(mage.abilities.SpellAbility)

Aggregations

SpellAbility (mage.abilities.SpellAbility)75 Player (mage.players.Player)32 UUID (java.util.UUID)22 Card (mage.cards.Card)21 Permanent (mage.game.permanent.Permanent)21 Ability (mage.abilities.Ability)16 Spell (mage.game.stack.Spell)12 ManaCostsImpl (mage.abilities.costs.mana.ManaCostsImpl)10 FilterCard (mage.filter.FilterCard)9 Iterator (java.util.Iterator)8 MageObject (mage.MageObject)7 Target (mage.target.Target)7 ArrayList (java.util.ArrayList)6 ApprovingObject (mage.ApprovingObject)4 ActivatedAbility (mage.abilities.ActivatedAbility)4 Cost (mage.abilities.costs.Cost)4 Effect (mage.abilities.effects.Effect)4 Outcome (mage.constants.Outcome)4 FilterPermanent (mage.filter.FilterPermanent)4 PassAbility (mage.abilities.common.PassAbility)3