Search in sources :

Example 6 with SpellAbility

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

the class EscapesWithEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent permanent = game.getPermanent(source.getSourceId());
    if (permanent == null && source.getAbilityType() == AbilityType.STATIC) {
        permanent = game.getPermanentEntering(source.getSourceId());
    }
    if (permanent == null) {
        return false;
    }
    SpellAbility spellAbility = (SpellAbility) getValue(EntersBattlefieldEffect.SOURCE_CAST_SPELL_ABILITY);
    if (!(spellAbility instanceof EscapeAbility) || !spellAbility.getSourceId().equals(source.getSourceId()) || permanent.getZoneChangeCounter(game) != spellAbility.getSourceObjectZoneChangeCounter()) {
        return false;
    }
    List<UUID> appliedEffects = (ArrayList<UUID>) this.getValue("appliedEffects");
    permanent.addCounters(CounterType.P1P1.createInstance(counter), source.getControllerId(), source, game, appliedEffects);
    if (this.delayedTriggeredAbility != null) {
        game.addDelayedTriggeredAbility(this.delayedTriggeredAbility, source);
    }
    return true;
}
Also used : Permanent(mage.game.permanent.Permanent) ArrayList(java.util.ArrayList) SpellAbility(mage.abilities.SpellAbility) UUID(java.util.UUID) EscapeAbility(mage.abilities.keyword.EscapeAbility)

Example 7 with SpellAbility

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

the class CipherStoreEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Card cipherCard = game.getCard(cipherCardId);
    if (cipherCard != null && controller != null) {
        Card copyCard = game.copyCard(cipherCard, source, controller.getId());
        SpellAbility ability = copyCard.getSpellAbility();
        // remove the cipher effect from the copy
        ability.getEffects().removeIf(effect -> effect instanceof CipherEffect);
        controller.cast(ability, game, true, new ApprovingObject(source, game));
    }
    return false;
}
Also used : Player(mage.players.Player) ApprovingObject(mage.ApprovingObject) SpellAbility(mage.abilities.SpellAbility) Card(mage.cards.Card)

Example 8 with SpellAbility

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

the class CopyPermanentEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourcePermanent = game.getPermanentEntering(source.getSourceId());
    if (sourcePermanent == null) {
        sourcePermanent = game.getObject(source.getSourceId());
    }
    if (controller == null || sourcePermanent == null) {
        return false;
    }
    Permanent copyFromPermanent = null;
    if (useTargetOfAbility) {
        copyFromPermanent = game.getPermanent(getTargetPointer().getFirst(game, source));
    } else {
        Target target = new TargetPermanent(filter);
        target.setNotTarget(true);
        if (target.canChoose(source.getSourceId(), controller.getId(), game)) {
            controller.choose(Outcome.Copy, target, source.getSourceId(), game);
            copyFromPermanent = game.getPermanent(target.getFirstTarget());
        }
    }
    if (copyFromPermanent == null) {
        return true;
    }
    bluePrintPermanent = game.copyPermanent(duration, copyFromPermanent, sourcePermanent.getId(), source, applier);
    if (bluePrintPermanent == null) {
        return false;
    }
    // if object is a copy of an aura, it needs to attach again for new target
    if (!bluePrintPermanent.hasSubtype(SubType.AURA, game)) {
        return true;
    }
    // copied from mage.cards.c.CopyEnchantment.java
    // permanent can be attached (Estrid's Mask) or enchant (Utopia Sprawl)
    // TODO: fix Animate Dead -- it's can't be copied (can't retarget)
    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 : bluePrintPermanent.getAbilities()) {
        if (!(ability instanceof SpellAbility)) {
            continue;
        }
        auraOutcome = ability.getEffects().getOutcome(ability);
        for (Effect effect : ability.getEffects()) {
            if (!(effect instanceof AttachEffect)) {
                continue;
            }
            if (bluePrintPermanent.getSpellAbility().getTargets().size() > 0) {
                auraTarget = bluePrintPermanent.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 : bluePrintPermanent.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) {
        return true;
    }
    // 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)) {
        return true;
    }
    UUID targetId = auraTarget.getFirstTarget();
    Permanent targetPermanent = game.getPermanent(targetId);
    Player targetPlayer = game.getPlayer(targetId);
    if (targetPermanent != null) {
        targetPermanent.addAttachment(sourcePermanent.getId(), source, game);
    } else if (targetPlayer != null) {
        targetPlayer.addAttachment(sourcePermanent.getId(), source, game);
    } else {
        return false;
    }
    return true;
}
Also used : EnchantAbility(mage.abilities.keyword.EnchantAbility) SpellAbility(mage.abilities.SpellAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Target(mage.target.Target) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) Outcome(mage.constants.Outcome) MageObject(mage.MageObject) SpellAbility(mage.abilities.SpellAbility) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) TargetPermanent(mage.target.TargetPermanent) EnchantAbility(mage.abilities.keyword.EnchantAbility) UUID(java.util.UUID)

Example 9 with SpellAbility

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

the class SpellsCostModificationThatTargetSourceEffect method applies.

@Override
public boolean applies(Ability abilityToModify, Ability source, Game game) {
    if (!(abilityToModify instanceof SpellAbility)) {
        return false;
    }
    Player sourceController = game.getPlayer(source.getControllerId());
    Player abilityController = game.getPlayer(abilityToModify.getControllerId());
    if (sourceController == null || abilityController == null) {
        return false;
    }
    switch(this.targetController) {
        case ANY:
            break;
        case YOU:
            if (!sourceController.getId().equals(abilityController.getId())) {
                return false;
            }
            break;
        case OPPONENT:
            if (!sourceController.hasOpponent(abilityController.getId(), game)) {
                return false;
            }
            break;
        default:
            return false;
    }
    Card spellCard = ((SpellAbility) abilityToModify).getCharacteristics(game);
    if (!this.spellFilter.match(spellCard, game)) {
        return false;
    }
    if (game.getStack().getStackObject(abilityToModify.getId()) != null) {
        // real cast
        Set<UUID> allTargets = CardUtil.getAllSelectedTargets(abilityToModify, game);
        return allTargets.contains(source.getSourceId());
    } else {
        // playable
        Set<UUID> allTargets = CardUtil.getAllPossibleTargets(abilityToModify, game);
        switch(this.getModificationType()) {
            case REDUCE_COST:
                // must reduce all the time
                return allTargets.contains(source.getSourceId());
            case INCREASE_COST:
                // must increase if can't target another (e.g. user can choose another target without cost increase)
                return allTargets.contains(source.getSourceId()) && allTargets.size() <= 1;
        }
    }
    return false;
}
Also used : Player(mage.players.Player) SpellAbility(mage.abilities.SpellAbility) UUID(java.util.UUID) FilterCard(mage.filter.FilterCard) Card(mage.cards.Card)

Example 10 with SpellAbility

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

the class DralnusPetEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent permanent = game.getPermanent(source.getSourceId());
    if (permanent == null && source.getAbilityType() == AbilityType.STATIC) {
        permanent = game.getPermanentEntering(source.getSourceId());
    }
    if (controller != null && permanent != null) {
        SpellAbility spellAbility = (SpellAbility) getValue(EntersBattlefieldEffect.SOURCE_CAST_SPELL_ABILITY);
        if (spellAbility != null && spellAbility.getSourceId().equals(source.getSourceId()) && permanent.getZoneChangeCounter(game) == spellAbility.getSourceObjectZoneChangeCounter()) {
            int cmc = 0;
            for (Cost cost : spellAbility.getCosts()) {
                if (cost instanceof DiscardCardCost && !((DiscardCardCost) cost).getCards().isEmpty()) {
                    cmc = ((DiscardCardCost) cost).getCards().get(0).getManaValue();
                }
                if (cmc > 0) {
                    return new AddCountersSourceEffect(CounterType.P1P1.createInstance(cmc), true).apply(game, source);
                }
            }
        }
        return true;
    }
    return false;
}
Also used : DiscardCardCost(mage.abilities.costs.common.DiscardCardCost) AddCountersSourceEffect(mage.abilities.effects.common.counter.AddCountersSourceEffect) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) SpellAbility(mage.abilities.SpellAbility) DiscardCardCost(mage.abilities.costs.common.DiscardCardCost) Cost(mage.abilities.costs.Cost)

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