Search in sources :

Example 56 with SpellAbility

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

the class AuraReplacementEffect method replaceEvent.

@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
    Zone fromZone = ((ZoneChangeEvent) event).getFromZone();
    Card card = game.getCard(event.getTargetId());
    if (card == null) {
        return false;
    }
    Card firstCardFace = null;
    if (game.getState().getValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + card.getId()) != null) {
        firstCardFace = card;
        card = card.getSecondCardFace();
        if (!card.isEnchantment(game) || !card.hasSubtype(SubType.AURA, game)) {
            return false;
        }
    }
    // Aura cards that go to battlefield face down (Manifest) don't have to select targets
    if (card.isFaceDown(game)) {
        return false;
    }
    // Aura enters the battlefield attached
    Object object = game.getState().getValue("attachTo:" + card.getId());
    if (object != null) {
        if (object instanceof Permanent) {
            // Aura is attached to a permanent on the battlefield
            return false;
        }
        if (object instanceof UUID) {
            Player player = game.getPlayer((UUID) object);
            if (player != null) {
                // Aura is attached to a player
                return false;
            }
        }
    }
    UUID targetId = null;
    MageObject sourceObject = game.getObject(event.getSourceId());
    boolean enchantCardInGraveyard = false;
    if (sourceObject instanceof StackAbility) {
        StackAbility stackAbility = (StackAbility) sourceObject;
        if (!stackAbility.getEffects().isEmpty()) {
            targetId = stackAbility.getEffects().get(0).getTargetPointer().getFirst(game, stackAbility);
        }
    }
    // So continuousEffects are removed if previous effect of the same ability did move objects that cause continuous effects
    game.applyEffects();
    Player controllingPlayer = null;
    if (targetId == null) {
        SpellAbility spellAbility = card.getSpellAbility();
        if (spellAbility.getTargets().isEmpty()) {
            for (Ability ability : card.getAbilities(game)) {
                if ((ability instanceof SpellAbility) && SpellAbilityType.BASE_ALTERNATE == ((SpellAbility) ability).getSpellAbilityType() && !ability.getTargets().isEmpty()) {
                    spellAbility = (SpellAbility) ability;
                    break;
                }
            }
        }
        if (spellAbility.getTargets().isEmpty()) {
            return false;
        }
        Target target = spellAbility.getTargets().get(0).copy();
        Outcome auraOutcome = Outcome.BoostCreature;
        for (Effect effect : spellAbility.getEffects()) {
            if (effect instanceof AttachEffect) {
                auraOutcome = effect.getOutcome();
                break;
            }
        }
        enchantCardInGraveyard = target instanceof TargetCardInGraveyard;
        if (target != null) {
            // always not target because this way it's not handled targeted
            target.setNotTarget(true);
            // necessary if e.g. aura is blinked multiple times
            target.clearChosen();
        }
        if (event.getPlayerId() != null) {
            controllingPlayer = game.getPlayer(event.getPlayerId());
        } else {
            controllingPlayer = game.getPlayer(card.getOwnerId());
        }
        if (target != null && controllingPlayer != null && controllingPlayer.choose(auraOutcome, target, card.getId(), game)) {
            targetId = target.getFirstTarget();
        }
    }
    Card targetCard = null;
    Permanent targetPermanent = null;
    if (enchantCardInGraveyard) {
        targetCard = game.getCard(targetId);
    } else {
        targetPermanent = game.getPermanent(targetId);
    }
    Player targetPlayer = game.getPlayer(targetId);
    if (targetCard != null || targetPermanent != null || targetPlayer != null) {
        if (firstCardFace != null) {
            // transforming card. remove first face (original card) from old zone
            firstCardFace.removeFromZone(game, fromZone, source);
        } else {
            card.removeFromZone(game, fromZone, source);
        }
        PermanentCard permanent = new PermanentCard(card, (controllingPlayer == null ? card.getOwnerId() : controllingPlayer.getId()), game);
        ZoneChangeEvent zoneChangeEvent = new ZoneChangeEvent(permanent, event.getPlayerId(), fromZone, Zone.BATTLEFIELD);
        permanent.updateZoneChangeCounter(game, zoneChangeEvent);
        game.addPermanent(permanent, 0);
        card.setZone(Zone.BATTLEFIELD, game);
        if (permanent.entersBattlefield(source, game, fromZone, true)) {
            String attachToName = null;
            if (targetCard != null) {
                permanent.attachTo(targetCard.getId(), source, game);
                attachToName = targetCard.getLogName();
            } else if (targetPermanent != null) {
                targetPermanent.addAttachment(permanent.getId(), source, game);
                attachToName = targetPermanent.getLogName();
            } else if (targetPlayer != null) {
                targetPlayer.addAttachment(permanent.getId(), source, game);
                attachToName = targetPlayer.getLogName();
            }
            game.applyEffects();
            game.fireEvent(zoneChangeEvent);
            if (!game.isSimulation()) {
                if (controllingPlayer != null && fromZone != null && permanent != null) {
                    game.informPlayers(controllingPlayer.getLogName() + " puts " + (card.getLogName()) + " from " + fromZone.toString().toLowerCase(Locale.ENGLISH) + " onto the Battlefield attached to " + attachToName);
                }
            }
            return true;
        }
    }
    return false;
}
Also used : StackAbility(mage.game.stack.StackAbility) SpellAbility(mage.abilities.SpellAbility) TransformAbility(mage.abilities.keyword.TransformAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) MageObject(mage.MageObject) SpellAbility(mage.abilities.SpellAbility) StackAbility(mage.game.stack.StackAbility) PermanentCard(mage.game.permanent.PermanentCard) Card(mage.cards.Card) AttachEffect(mage.abilities.effects.common.AttachEffect) ZoneChangeEvent(mage.game.events.ZoneChangeEvent) Target(mage.target.Target) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) MageObject(mage.MageObject) AttachEffect(mage.abilities.effects.common.AttachEffect) UUID(java.util.UUID) PermanentCard(mage.game.permanent.PermanentCard)

Example 57 with SpellAbility

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

the class AwakenTheBloodAvatarCostReductionEffect method apply.

@Override
public boolean apply(Game game, Ability source, Ability abilityToModify) {
    SpellAbility spellAbility = (SpellAbility) abilityToModify;
    for (Cost cost : spellAbility.getCosts()) {
        if (!(cost instanceof SacrificeXTargetCost)) {
            continue;
        }
        if (game.inCheckPlayableState()) {
            // allows to cast in getPlayable
            int reduction = ((SacrificeXTargetCost) cost).getMaxValue(spellAbility, game);
            CardUtil.adjustCost(spellAbility, reduction * 2);
        } else {
            // real cast
            int reduction = ((SacrificeXTargetCost) cost).getAmount();
            CardUtil.adjustCost(spellAbility, reduction * 2);
        }
        break;
    }
    return true;
}
Also used : SpellAbility(mage.abilities.SpellAbility) SacrificeXTargetCost(mage.abilities.costs.common.SacrificeXTargetCost) SacrificeXTargetCost(mage.abilities.costs.common.SacrificeXTargetCost) Cost(mage.abilities.costs.Cost)

Example 58 with SpellAbility

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

the class HeraldOfWarCostReductionEffect method apply.

@Override
public boolean apply(Game game, Ability source, Ability abilityToModify) {
    SpellAbility spellAbility = (SpellAbility) abilityToModify;
    Permanent sourcePermanent = game.getPermanent(source.getSourceId());
    if (sourcePermanent != null) {
        int amount = sourcePermanent.getCounters(game).getCount(CounterType.P1P1);
        if (amount > 0) {
            CardUtil.adjustCost(spellAbility, amount);
            return true;
        }
    }
    return false;
}
Also used : Permanent(mage.game.permanent.Permanent) SpellAbility(mage.abilities.SpellAbility)

Example 59 with SpellAbility

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

the class KasminaEnigmaticMentorCostModificationEffect method applies.

@Override
public boolean applies(Ability abilityToModify, Ability source, Game game) {
    if (!(abilityToModify instanceof SpellAbility)) {
        return false;
    }
    if (!game.getOpponents(source.getControllerId()).contains(abilityToModify.getControllerId())) {
        return false;
    }
    Spell spell = (Spell) game.getStack().getStackObject(abilityToModify.getId());
    Set<UUID> allTargets;
    if (spell != null) {
        // real cast
        allTargets = CardUtil.getAllSelectedTargets(abilityToModify, game);
    } else {
        // playable
        allTargets = CardUtil.getAllPossibleTargets(abilityToModify, game);
        // can target without cost increase
        if (allTargets.stream().map(game::getPermanent).filter(Objects::nonNull).anyMatch(permanent -> !isTargetCompatible(permanent, source, game))) {
            return false;
        }
        ;
    }
    return allTargets.stream().map(game::getPermanent).filter(Objects::nonNull).anyMatch(permanent -> isTargetCompatible(permanent, source, game));
}
Also used : SpellAbility(mage.abilities.SpellAbility) UUID(java.util.UUID) Spell(mage.game.stack.Spell)

Example 60 with SpellAbility

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

the class KazaRoilChaserWatcher method applies.

@Override
public boolean applies(Ability abilityToModify, Ability source, Game game) {
    KazaRoilChaserWatcher watcher = game.getState().getWatcher(KazaRoilChaserWatcher.class);
    if (watcher == null) {
        return false;
    }
    if (watcher.getCount(source.getControllerId()) > spellsCast) {
        // only one use
        discard();
        return false;
    }
    if (!(abilityToModify instanceof SpellAbility) || !abilityToModify.isControlledBy(source.getControllerId())) {
        return false;
    }
    Card spellCard = ((SpellAbility) abilityToModify).getCharacteristics(game);
    return spellCard != null && spellCard.isInstantOrSorcery(game);
}
Also used : SpellAbility(mage.abilities.SpellAbility) Card(mage.cards.Card)

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