Search in sources :

Example 76 with ExileZone

use of mage.game.ExileZone in project mage by magefree.

the class ReturnFromExileForSourceEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller == null) {
        return false;
    }
    // effect uses in two use cases:
    // * on battlefield
    // * after leaves the battlefield
    // so ZCC must be different in different use cases
    UUID exileId;
    Permanent permanentLeftBattlefield = (Permanent) getValue("permanentLeftBattlefield");
    if (permanentLeftBattlefield != null) {
        exileId = CardUtil.getExileZoneId(game, source.getSourceId(), permanentLeftBattlefield.getZoneChangeCounter(game));
    } else {
        exileId = CardUtil.getExileZoneId(game, source.getSourceId(), source.getSourceObjectZoneChangeCounter());
    }
    ExileZone exile = game.getExile().getExileZone(exileId);
    if (exile != null) {
        // null is valid if source left battlefield before enters the battlefield effect resolved
        if (Zone.BATTLEFIELD.equals(returnToZone)) {
            controller.moveCards(exile.getCards(game), returnToZone, source, game, false, false, true, null);
        } else {
            controller.moveCards(exile, returnToZone, source, game);
        }
    }
    return true;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) ExileZone(mage.game.ExileZone) UUID(java.util.UUID)

Example 77 with ExileZone

use of mage.game.ExileZone in project mage by magefree.

the class FreeForAllLeavesBattlefieldEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(game.getActivePlayerId());
    ExileZone exZone = game.getExile().getExileZone(CardUtil.getExileZoneId(game, source));
    if (player == null || exZone == null || exZone.isEmpty()) {
        return false;
    }
    Cards exiledCards = new CardsImpl(exZone.getCards(game));
    return player.moveCards(exiledCards.getRandom(game), Zone.BATTLEFIELD, source, game);
}
Also used : Player(mage.players.Player) ExileZone(mage.game.ExileZone) Cards(mage.cards.Cards) CardsImpl(mage.cards.CardsImpl)

Example 78 with ExileZone

use of mage.game.ExileZone in project mage by magefree.

the class IzzetChemisterCastFromExileEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    ExileZone exile = game.getExile().getExileZone(exileId);
    Player controller = game.getPlayer(source.getControllerId());
    FilterCard filter = new FilterCard();
    if (controller != null && exile != null) {
        Cards cardsToExile = new CardsImpl();
        cardsToExile.addAll(exile.getCards(game));
        OuterLoop: while (cardsToExile.count(filter, game) > 0) {
            if (!controller.canRespond()) {
                return false;
            }
            TargetCardInExile target = new TargetCardInExile(0, 1, filter, exileId, false);
            target.setNotTarget(true);
            while (controller.canRespond() && cardsToExile.count(filter, game) > 0 && controller.choose(Outcome.PlayForFree, cardsToExile, target, game)) {
                Card card = game.getCard(target.getFirstTarget());
                if (card != null) {
                    game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), Boolean.TRUE);
                    controller.cast(controller.chooseAbilityForCast(card, game, true), game, true, new ApprovingObject(source, game));
                    game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), null);
                    cardsToExile.remove(card);
                } else {
                    break OuterLoop;
                }
                target.clearChosen();
            }
        }
        return true;
    }
    return false;
}
Also used : FilterCard(mage.filter.FilterCard) Player(mage.players.Player) ApprovingObject(mage.ApprovingObject) TargetCardInExile(mage.target.common.TargetCardInExile) ExileZone(mage.game.ExileZone) FilterCard(mage.filter.FilterCard) FilterOwnedCard(mage.filter.common.FilterOwnedCard) TargetCard(mage.target.TargetCard)

Example 79 with ExileZone

use of mage.game.ExileZone in project mage by magefree.

the class KarnPlayerExileEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    MageObject sourceObject = source.getSourceObject(game);
    if (sourceObject == null) {
        return false;
    }
    List<Card> cards = new ArrayList<>();
    for (ExileZone zone : game.getExile().getExileZones()) {
        exileId = CardUtil.getExileZoneId(game, source.getSourceId(), source.getSourceObjectZoneChangeCounter());
        if (zone.getId().equals(exileId)) {
            for (Card card : zone.getCards(game)) {
                if (!card.hasSubtype(SubType.AURA, game) && card.isPermanent(game)) {
                    cards.add(card);
                }
            }
        }
    }
    // dirty hack for game restart, can cause bugs due strange clear code (some data like ZCC keeping on new game)
    // see testCommanderRestoredToBattlefieldAfterKarnUltimate for more details
    game.getState().clearOnGameRestart();
    // default watchers init, TODO: remove all restart/init code to game
    ((GameImpl) game).initGameDefaultWatchers();
    for (Card card : game.getCards()) {
        game.getState().addCard(card);
    }
    for (Player player : game.getPlayers().values()) {
        if (player.canRespond()) {
            // only players alive are in the restarted game
            player.getGraveyard().clear();
            player.getHand().clear();
            player.getLibrary().clear();
            for (Card card : game.getCards()) {
                if (// no copies
                card.isOwnedBy(player.getId()) && !card.isCopy() && !player.getSideboard().contains(card.getId()) && !cards.contains(card)) {
                    // not the exiled cards
                    if (game.getCommandersIds(player, CommanderCardType.ANY, false).contains(card.getId())) {
                        // TODO: check restart and init
                        game.addCommander(new Commander(card));
                        // no needs in initCommander call -- it's used on game startup (init)
                        game.setZone(card.getId(), Zone.COMMAND);
                    } else {
                        player.getLibrary().putOnTop(card, game);
                    }
                }
            }
            ((GameImpl) game).initPlayerDefaultWatchers(player.getId());
            player.init(game);
        }
    }
    for (Card card : cards) {
        game.getState().setZone(card.getId(), Zone.EXILED);
        game.getExile().add(exileId, sourceObject.getIdName(), card);
    }
    game.addDelayedTriggeredAbility(new KarnLiberatedDelayedTriggeredAbility(exileId), source);
    game.setStartingPlayerId(source.getControllerId());
    game.start(null);
    return true;
}
Also used : GameImpl(mage.game.GameImpl) TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) Commander(mage.game.command.Commander) MageObject(mage.MageObject) ArrayList(java.util.ArrayList) ExileZone(mage.game.ExileZone)

Example 80 with ExileZone

use of mage.game.ExileZone in project mage by magefree.

the class MournersShieldEffect method init.

@Override
public void init(Ability source, Game game) {
    ObjectColor colorsAmongImprinted = new ObjectColor();
    Permanent sourceObject = game.getPermanent(source.getSourceId());
    ExileZone exileZone = game.getExile().getExileZone(CardUtil.getCardExileZoneId(game, source.getSourceId()));
    if (sourceObject == null || sourceObject.getImprinted() == null) {
        noneExiled = true;
        return;
    }
    for (UUID imprinted : sourceObject.getImprinted()) {
        if (imprinted != null && exileZone.contains(imprinted)) {
            Card card = game.getCard(imprinted);
            if (card != null) {
                colorsAmongImprinted = colorsAmongImprinted.union(card.getColor(game));
            }
        }
    }
    FilterObject filterObject = new FilterObject("a source of your choice that shares a color with the exiled card");
    filterObject.add(new SharesColorPredicate(colorsAmongImprinted));
    this.target = new TargetSource(filterObject);
    this.target.choose(Outcome.PreventDamage, source.getControllerId(), source.getSourceId(), game);
    if (target.getFirstTarget() != null) {
        mageObjectReference = new MageObjectReference(target.getFirstTarget(), game);
    } else {
        mageObjectReference = null;
    }
}
Also used : TargetSource(mage.target.TargetSource) Permanent(mage.game.permanent.Permanent) SharesColorPredicate(mage.filter.predicate.mageobject.SharesColorPredicate) FilterObject(mage.filter.FilterObject) ObjectColor(mage.ObjectColor) ExileZone(mage.game.ExileZone) UUID(java.util.UUID) MageObjectReference(mage.MageObjectReference) Card(mage.cards.Card)

Aggregations

ExileZone (mage.game.ExileZone)92 Player (mage.players.Player)61 UUID (java.util.UUID)44 Card (mage.cards.Card)44 MageObject (mage.MageObject)24 Permanent (mage.game.permanent.Permanent)23 FilterCard (mage.filter.FilterCard)15 HashSet (java.util.HashSet)11 ApprovingObject (mage.ApprovingObject)9 CardsImpl (mage.cards.CardsImpl)9 TargetCard (mage.target.TargetCard)9 Cards (mage.cards.Cards)7 MageObjectReference (mage.MageObjectReference)6 Ability (mage.abilities.Ability)5 Spell (mage.game.stack.Spell)5 SimpleStaticAbility (mage.abilities.common.SimpleStaticAbility)3 TransformSourceEffect (mage.abilities.effects.common.TransformSourceEffect)3 TargetPlayer (mage.target.TargetPlayer)3 Objects (java.util.Objects)2 Set (java.util.Set)2