Search in sources :

Example 51 with Spell

use of mage.game.stack.Spell in project mage by magefree.

the class PathOfAncestryTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (!getSourceId().equals(event.getSourceId())) {
        return false;
    }
    Permanent sourcePermanent = game.getPermanentOrLKIBattlefield(getSourceId());
    if (sourcePermanent == null || sourcePermanent.getAbilities(game).stream().filter(CommanderColorIdentityManaAbility.class::isInstance).map(Ability::getOriginalId).map(UUID::toString).noneMatch(event.getData()::equals)) {
        return false;
    }
    Spell spell = game.getStack().getSpell(event.getTargetId());
    if (spell == null) {
        return false;
    }
    Player player = game.getPlayer(getControllerId());
    if (player == null) {
        return false;
    }
    // share creature type with commander
    for (Card commander : game.getCommanderCardsFromAnyZones(player, CommanderCardType.COMMANDER_OR_OATHBREAKER, Zone.ALL)) {
        if (spell.getCard().shareCreatureTypes(game, commander)) {
            return true;
        }
    }
    return false;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) CommanderColorIdentityManaAbility(mage.abilities.mana.CommanderColorIdentityManaAbility) UUID(java.util.UUID) Spell(mage.game.stack.Spell) Card(mage.cards.Card)

Example 52 with Spell

use of mage.game.stack.Spell in project mage by magefree.

the class RicochetEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Spell spell = game.getStack().getSpell(this.getTargetPointer().getFirst(game, source));
    if (spell != null) {
        Targets targets = new Targets();
        Ability sourceAbility = spell.getSpellAbility();
        for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
            Mode mode = sourceAbility.getModes().get(modeId);
            targets.addAll(mode.getTargets());
        }
        if (targets.size() != 1 || targets.get(0).getTargets().size() != 1) {
            return false;
        }
        Map<Player, Integer> playerRolls = new HashMap<>();
        for (UUID playerId : game.getPlayerList().copy()) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                playerRolls.put(player, 7);
            }
        }
        do {
            for (Player player : playerRolls.keySet()) {
                // bad outcome - ai must choose lowest value
                playerRolls.put(player, player.rollDice(Outcome.Detriment, source, game, 6));
            }
            int minValueInMap = Collections.min(playerRolls.values());
            for (Map.Entry<Player, Integer> mapEntry : new HashSet<>(playerRolls.entrySet())) {
                if (mapEntry.getValue() > minValueInMap) {
                    playerRolls.remove(mapEntry.getKey());
                }
            }
        } while (playerRolls.size() > 1);
        if (playerRolls.size() == 1) {
            Player loser = (Player) playerRolls.keySet().toArray()[0];
            UUID loserId = loser.getId();
            Target target = targets.get(0);
            if (target.getFirstTarget().equals(loserId)) {
                return true;
            }
            String oldTargetName = null;
            if (target.canTarget(spell.getControllerId(), loserId, sourceAbility, game)) {
                Player oldPlayer = game.getPlayer(targets.getFirstTarget());
                if (oldPlayer != null) {
                    oldTargetName = oldPlayer.getLogName();
                }
                target.clearChosen();
                target.addTarget(loserId, sourceAbility, game);
            }
            MageObject sourceObject = game.getObject(source.getSourceId());
            if (oldTargetName != null && sourceObject != null) {
                game.informPlayers(sourceObject.getLogName() + ": Changed target of " + spell.getLogName() + " from " + oldTargetName + " to " + loser.getLogName());
            }
        }
        return true;
    }
    return false;
}
Also used : SpellCastAllTriggeredAbility(mage.abilities.common.SpellCastAllTriggeredAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) ObjectSourcePlayer(mage.filter.predicate.ObjectSourcePlayer) HashMap(java.util.HashMap) Mode(mage.abilities.Mode) MageObject(mage.MageObject) Targets(mage.target.Targets) Spell(mage.game.stack.Spell) FilterSpell(mage.filter.FilterSpell) Target(mage.target.Target) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 53 with Spell

use of mage.game.stack.Spell in project mage by magefree.

the class RideTheAvalancheTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (!isControlledBy(event.getPlayerId())) {
        return false;
    }
    Spell spell = game.getSpell(event.getTargetId());
    if (spell == null) {
        return false;
    }
    this.getEffects().clear();
    this.addEffect(new AddCountersTargetEffect(CounterType.P1P1.createInstance(spell.getManaValue())));
    return true;
}
Also used : AddCountersTargetEffect(mage.abilities.effects.common.counter.AddCountersTargetEffect) Spell(mage.game.stack.Spell)

Example 54 with Spell

use of mage.game.stack.Spell in project mage by magefree.

the class ShowOfConfidenceEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Spell spell = (Spell) getValue("spellCast");
    SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
    if (spell == null || watcher == null) {
        return false;
    }
    int copies = watcher.getSpellsCastThisTurn(source.getControllerId()).stream().filter(Objects::nonNull).filter(spell1 -> spell1.isInstantOrSorcery(game)).filter(s -> !s.getSourceId().equals(source.getSourceId()) || s.getZoneChangeCounter(game) != source.getSourceObjectZoneChangeCounter()).mapToInt(x -> 1).sum();
    if (copies > 0) {
        spell.createCopyOnStack(game, source, source.getControllerId(), true, copies);
    }
    return true;
}
Also used : AddCountersTargetEffect(mage.abilities.effects.common.counter.AddCountersTargetEffect) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) UUID(java.util.UUID) CastSourceTriggeredAbility(mage.abilities.effects.common.CastSourceTriggeredAbility) CardSetInfo(mage.cards.CardSetInfo) Objects(java.util.Objects) Duration(mage.constants.Duration) Game(mage.game.Game) VigilanceAbility(mage.abilities.keyword.VigilanceAbility) CardImpl(mage.cards.CardImpl) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) SpellsCastWatcher(mage.watchers.common.SpellsCastWatcher) CardType(mage.constants.CardType) Spell(mage.game.stack.Spell) CounterType(mage.counters.CounterType) Ability(mage.abilities.Ability) GainAbilityTargetEffect(mage.abilities.effects.common.continuous.GainAbilityTargetEffect) SpellsCastWatcher(mage.watchers.common.SpellsCastWatcher) Objects(java.util.Objects) Spell(mage.game.stack.Spell)

Example 55 with Spell

use of mage.game.stack.Spell in project mage by magefree.

the class WookieeMysticWatcher method watch.

@Override
public void watch(GameEvent event, Game game) {
    if (event.getType() == GameEvent.EventType.MANA_PAID) {
        MageObject target = game.getObject(event.getTargetId());
        if (event.getSourceId() != null && event.getSourceId().equals(this.getSourceId()) && target != null && target.isCreature(game) && event.getFlag()) {
            if (target instanceof Spell) {
                this.creatures.add(((Spell) target).getCard().getId());
            }
        }
    }
    if (event.getType() == GameEvent.EventType.COUNTERED) {
        if (creatures.contains(event.getTargetId())) {
            creatures.remove(event.getSourceId());
        }
    }
    if (event.getType() == GameEvent.EventType.ZONE_CHANGE) {
        if (creatures.contains(event.getSourceId())) {
            ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
            // spell was e.g. exiled and goes again to stack, so previous cast has not resolved.
            if (zEvent.getToZone() == Zone.STACK) {
                creatures.remove(event.getSourceId());
            }
        }
    }
    if (event.getType() == GameEvent.EventType.ENTERS_THE_BATTLEFIELD) {
        if (creatures.contains(event.getSourceId())) {
            Permanent creature = game.getPermanent(event.getSourceId());
            creature.addCounters(CounterType.P1P1.createInstance(), source.getControllerId(), source, game);
            creatures.remove(event.getSourceId());
        }
    }
}
Also used : ZoneChangeEvent(mage.game.events.ZoneChangeEvent) Permanent(mage.game.permanent.Permanent) MageObject(mage.MageObject) Spell(mage.game.stack.Spell)

Aggregations

Spell (mage.game.stack.Spell)311 Player (mage.players.Player)155 Permanent (mage.game.permanent.Permanent)90 UUID (java.util.UUID)79 MageObject (mage.MageObject)69 Card (mage.cards.Card)51 TargetSpell (mage.target.TargetSpell)48 StackObject (mage.game.stack.StackObject)42 FilterCard (mage.filter.FilterCard)38 FilterSpell (mage.filter.FilterSpell)36 FixedTarget (mage.target.targetpointer.FixedTarget)33 MageObjectReference (mage.MageObjectReference)24 Effect (mage.abilities.effects.Effect)22 OneShotEffect (mage.abilities.effects.OneShotEffect)22 Target (mage.target.Target)22 Ability (mage.abilities.Ability)20 SpellAbility (mage.abilities.SpellAbility)16 FilterPermanent (mage.filter.FilterPermanent)15 TargetCard (mage.target.TargetCard)15 TargetPlayer (mage.target.TargetPlayer)14