Search in sources :

Example 81 with ZoneChangeEvent

use of mage.game.events.ZoneChangeEvent 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 82 with ZoneChangeEvent

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

the class AftermathExileAsResolvesFromGraveyard method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
    if (zEvent.getFromZone() == Zone.STACK && zEvent.getToZone() != Zone.EXILED) {
        // Moving something from stack to somewhere else
        // Get the source id, getting the whole split card's ID, because
        // that's the card that is changing zones in the event, but
        // source.getSourceId is only the split card half.
        // If branch so that we also support putting Aftermath on
        // non-split cards for... whatever reason, in case somebody
        // wants to do that in the future.
        UUID sourceId = source.getSourceId();
        Card sourceCard = game.getCard(source.getSourceId());
        if (sourceCard instanceof SplitCardHalf) {
            sourceCard = ((SplitCardHalf) sourceCard).getParentCard();
            sourceId = sourceCard.getId();
        }
        if (sourceCard instanceof ModalDoubleFacesCardHalf) {
            sourceCard = ((ModalDoubleFacesCardHalf) sourceCard).getParentCard();
            sourceId = sourceCard.getId();
        }
        if (zEvent.getTargetId().equals(sourceId)) {
            // Moving this spell from stack to yard
            Spell spell = game.getStack().getSpell(source.getSourceId());
            // And this spell was cast from the graveyard, so we need to exile it
            return spell != null && spell.getFromZone() == Zone.GRAVEYARD;
        }
    }
    return false;
}
Also used : ZoneChangeEvent(mage.game.events.ZoneChangeEvent) ModalDoubleFacesCardHalf(mage.cards.ModalDoubleFacesCardHalf) SplitCardHalf(mage.cards.SplitCardHalf) UUID(java.util.UUID) Spell(mage.game.stack.Spell) Card(mage.cards.Card)

Example 83 with ZoneChangeEvent

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

the class KayaTheInexorableEffect method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
    if (zEvent.getTargetId().equals(this.getSourceId())) {
        this.getEffects().clear();
        this.addEffect(new KayaTheInexorableEffect(new MageObjectReference(zEvent.getTarget(), game)));
        this.addEffect(new CreateTokenEffect(new SpiritWhiteToken()));
        return true;
    }
    return false;
}
Also used : ZoneChangeEvent(mage.game.events.ZoneChangeEvent) SpiritWhiteToken(mage.game.permanent.token.SpiritWhiteToken) CreateTokenEffect(mage.abilities.effects.common.CreateTokenEffect) MageObjectReference(mage.MageObjectReference)

Example 84 with ZoneChangeEvent

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

the class RienneAngelOfRebirthEffect method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
    if (zEvent.getTarget() == null || zEvent.getTarget().getId().equals(this.getSourceId())) {
        return false;
    }
    Permanent permanent = game.getPermanentOrLKIBattlefield(zEvent.getTarget().getId());
    if (permanent != null && zEvent.isDiesEvent() && permanent.isCreature(game) && permanent.getColor(game).isMulticolored() && permanent.isControlledBy(this.controllerId)) {
        this.getEffects().setTargetPointer(new FixedTarget(zEvent.getTargetId()));
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) ZoneChangeEvent(mage.game.events.ZoneChangeEvent) Permanent(mage.game.permanent.Permanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent)

Example 85 with ZoneChangeEvent

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

the class ReyhanLastOfTheAbzanTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    ZoneChangeEvent zcEvent = (ZoneChangeEvent) event;
    // Dies or is put in the command zone
    if (zcEvent.getFromZone() != Zone.BATTLEFIELD) {
        return false;
    }
    if (zcEvent.getToZone() != Zone.GRAVEYARD && zcEvent.getToZone() != Zone.COMMAND) {
        return false;
    }
    // A creature
    Permanent permanent = (Permanent) game.getLastKnownInformation(event.getTargetId(), Zone.BATTLEFIELD);
    if (permanent == null || !permanent.isCreature(game)) {
        return false;
    }
    // You control
    Player player = game.getPlayer(this.getControllerId());
    if (player == null || !permanent.isControlledBy(getControllerId())) {
        return false;
    }
    // If it had one or more +1/+1 counters on it
    int countersOn = permanent.getCounters(game).getCount(CounterType.P1P1);
    if (countersOn == 0) {
        return false;
    }
    // You may put that may +1/+1 counters on target creature
    this.getEffects().clear();
    this.addEffect(new AddCountersTargetEffect(CounterType.P1P1.createInstance(countersOn)));
    return true;
}
Also used : ZoneChangeEvent(mage.game.events.ZoneChangeEvent) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) AddCountersTargetEffect(mage.abilities.effects.common.counter.AddCountersTargetEffect)

Aggregations

ZoneChangeEvent (mage.game.events.ZoneChangeEvent)94 Permanent (mage.game.permanent.Permanent)56 Player (mage.players.Player)27 FixedTarget (mage.target.targetpointer.FixedTarget)23 Card (mage.cards.Card)20 UUID (java.util.UUID)14 MageObject (mage.MageObject)11 MageObjectReference (mage.MageObjectReference)10 Effect (mage.abilities.effects.Effect)9 FilterCard (mage.filter.FilterCard)8 PermanentToken (mage.game.permanent.PermanentToken)8 FilterPermanent (mage.filter.FilterPermanent)7 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)7 Ability (mage.abilities.Ability)6 OneShotEffect (mage.abilities.effects.OneShotEffect)6 CreateTokenEffect (mage.abilities.effects.common.CreateTokenEffect)6 Spell (mage.game.stack.Spell)6 Zone (mage.constants.Zone)5 TargetPermanent (mage.target.TargetPermanent)5 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)5