Search in sources :

Example 1 with NumberOfTriggersEvent

use of mage.game.events.NumberOfTriggersEvent in project mage by magefree.

the class WulfgarOfIcewindDaleEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    NumberOfTriggersEvent numberOfTriggersEvent = (NumberOfTriggersEvent) event;
    Permanent sourcePermanent = game.getPermanent(numberOfTriggersEvent.getSourceId());
    if (sourcePermanent == null || !sourcePermanent.isControlledBy(source.getControllerId())) {
        return false;
    }
    GameEvent sourceEvent = numberOfTriggersEvent.getSourceEvent();
    switch(sourceEvent.getType()) {
        case ATTACKER_DECLARED:
            return source.isControlledBy(sourceEvent.getPlayerId());
        case DECLARED_ATTACKERS:
            return game.getCombat().getAttackers().stream().map(game::getControllerId).anyMatch(source::isControlledBy);
        case DEFENDER_ATTACKED:
            return ((DefenderAttackedEvent) sourceEvent).getAttackers(game).stream().map(Controllable::getControllerId).anyMatch(source::isControlledBy);
    }
    return false;
}
Also used : NumberOfTriggersEvent(mage.game.events.NumberOfTriggersEvent) Permanent(mage.game.permanent.Permanent) GameEvent(mage.game.events.GameEvent)

Example 2 with NumberOfTriggersEvent

use of mage.game.events.NumberOfTriggersEvent in project mage by magefree.

the class VeyranVoiceOfDualityEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    NumberOfTriggersEvent numberOfTriggersEvent = (NumberOfTriggersEvent) event;
    GameEvent sourceEvent = numberOfTriggersEvent.getSourceEvent();
    if (sourceEvent.getType() == GameEvent.EventType.SPELL_CAST || sourceEvent.getType() == GameEvent.EventType.COPIED_STACKOBJECT) {
        Spell spell = game.getSpell(sourceEvent.getTargetId());
        Permanent permanent = game.getPermanent(((NumberOfTriggersEvent) event).getSourceId());
        return spell != null && permanent != null && spell.isInstantOrSorcery(game) && spell.isControlledBy(source.getControllerId()) && permanent.isControlledBy(source.getControllerId());
    }
    return false;
}
Also used : NumberOfTriggersEvent(mage.game.events.NumberOfTriggersEvent) Permanent(mage.game.permanent.Permanent) GameEvent(mage.game.events.GameEvent) Spell(mage.game.stack.Spell)

Example 3 with NumberOfTriggersEvent

use of mage.game.events.NumberOfTriggersEvent in project mage by magefree.

the class TriggeredAbilities method checkTrigger.

private void checkTrigger(TriggeredAbility ability, GameEvent event, Game game) {
    // for effects like when leaves battlefield or destroyed use ShortLKI to check if permanent was in the correct zone before (e.g. Oblivion Ring or Karmic Justice)
    MageObject object = game.getObject(ability.getSourceId());
    if (ability.isInUseableZone(game, object, event)) {
        if (event == null || !game.getContinuousEffects().preventedByRuleModification(event, ability, game, false)) {
            if (object != null) {
                boolean controllerSet = false;
                if (ability.getZone() != Zone.COMMAND && event != null && event.getTargetId() != null && ability.isLeavesTheBattlefieldTrigger() && game.getLKI().get(Zone.BATTLEFIELD) != null && game.getLKI().get(Zone.BATTLEFIELD).containsKey(ability.getSourceId())) {
                    // need to check if object was face down for dies and destroy events because the ability triggers in the new zone, zone counter -1 is used
                    Permanent permanent = (Permanent) game.getLastKnownInformation(ability.getSourceId(), Zone.BATTLEFIELD, ability.getSourceObjectZoneChangeCounter() - 1);
                    if (permanent != null) {
                        if (permanent.isFaceDown(game) && // the face down creature got the ability from an effect => so it should work
                        !isGainedAbility(ability, permanent) && !ability.getWorksFaceDown()) {
                            // Not all triggered abilities of face down creatures work if they are faced down
                            return;
                        }
                        controllerSet = true;
                        ability.setControllerId(permanent.getControllerId());
                    }
                }
                if (!controllerSet) {
                    if (object instanceof Permanent) {
                        ability.setControllerId(((Permanent) object).getControllerId());
                    } else if (object instanceof Spell) {
                        // needed so that cast triggered abilities have to correct controller (e.g. Ulamog, the Infinite Gyre).
                        ability.setControllerId(((Spell) object).getControllerId());
                    }
                }
            }
            if (ability.checkTrigger(event, game) && ability.checkTriggeredAlready(game)) {
                NumberOfTriggersEvent numberOfTriggersEvent = new NumberOfTriggersEvent(ability, event);
                if (!game.replaceEvent(numberOfTriggersEvent)) {
                    for (int i = 0; i < numberOfTriggersEvent.getAmount(); i++) {
                        ability.trigger(game, ability.getControllerId(), event);
                    }
                }
            }
        }
    }
}
Also used : NumberOfTriggersEvent(mage.game.events.NumberOfTriggersEvent) Permanent(mage.game.permanent.Permanent) MageObject(mage.MageObject) Spell(mage.game.stack.Spell)

Example 4 with NumberOfTriggersEvent

use of mage.game.events.NumberOfTriggersEvent in project mage by magefree.

the class IsshinTwoHeavensAsOneEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    NumberOfTriggersEvent numberOfTriggersEvent = (NumberOfTriggersEvent) event;
    Permanent sourcePermanent = game.getPermanent(numberOfTriggersEvent.getSourceId());
    if (sourcePermanent == null || !sourcePermanent.isControlledBy(source.getControllerId())) {
        return false;
    }
    GameEvent sourceEvent = numberOfTriggersEvent.getSourceEvent();
    switch(sourceEvent.getType()) {
        case ATTACKER_DECLARED:
        case DECLARED_ATTACKERS:
        case DEFENDER_ATTACKED:
            return true;
    }
    return false;
}
Also used : NumberOfTriggersEvent(mage.game.events.NumberOfTriggersEvent) Permanent(mage.game.permanent.Permanent) GameEvent(mage.game.events.GameEvent)

Example 5 with NumberOfTriggersEvent

use of mage.game.events.NumberOfTriggersEvent in project mage by magefree.

the class YarokTheDesecratedEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    if (!(event instanceof NumberOfTriggersEvent)) {
        return false;
    }
    NumberOfTriggersEvent numberOfTriggersEvent = (NumberOfTriggersEvent) event;
    if (!source.isControlledBy(event.getPlayerId())) {
        return false;
    }
    GameEvent sourceEvent = numberOfTriggersEvent.getSourceEvent();
    // Only EtB triggers
    if (sourceEvent == null || sourceEvent.getType() != GameEvent.EventType.ENTERS_THE_BATTLEFIELD || !(sourceEvent instanceof EntersTheBattlefieldEvent)) {
        return false;
    }
    // Only for triggers of permanents
    return game.getPermanent(numberOfTriggersEvent.getSourceId()) != null;
}
Also used : NumberOfTriggersEvent(mage.game.events.NumberOfTriggersEvent) EntersTheBattlefieldEvent(mage.game.events.EntersTheBattlefieldEvent) GameEvent(mage.game.events.GameEvent)

Aggregations

NumberOfTriggersEvent (mage.game.events.NumberOfTriggersEvent)6 GameEvent (mage.game.events.GameEvent)5 Permanent (mage.game.permanent.Permanent)4 EntersTheBattlefieldEvent (mage.game.events.EntersTheBattlefieldEvent)2 Spell (mage.game.stack.Spell)2 MageObject (mage.MageObject)1