Search in sources :

Example 1 with Commander

use of mage.game.command.Commander in project mage by magefree.

the class ArcaneAdaptationEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    SubType subType = ChooseCreatureTypeEffect.getChosenCreatureType(source.getSourceId(), game);
    if (controller == null || subType == null) {
        return false;
    }
    // in graveyard
    for (UUID cardId : controller.getGraveyard()) {
        Card card = game.getCard(cardId);
        if (card != null && card.isCreature(game) && !card.hasSubtype(subType, game)) {
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().add(subType);
        }
    }
    // on Hand
    for (UUID cardId : controller.getHand()) {
        Card card = game.getCard(cardId);
        if (card != null && card.isCreature(game) && !card.hasSubtype(subType, game)) {
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().add(subType);
        }
    }
    // in Exile
    for (Card card : game.getState().getExile().getAllCards(game)) {
        if (card.isCreature(game) && !card.hasSubtype(subType, game)) {
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().add(subType);
        }
    }
    // in Library (e.g. for Mystical Teachings)
    for (Card card : controller.getLibrary().getCards(game)) {
        if (card.isOwnedBy(controller.getId()) && card.isCreature(game) && !card.hasSubtype(subType, game)) {
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().add(subType);
        }
    }
    // commander in command zone
    for (CommandObject commandObject : game.getState().getCommand()) {
        if (commandObject instanceof Commander) {
            Card card = game.getCard(((Commander) commandObject).getId());
            if (card != null && card.isOwnedBy(controller.getId()) && card.isCreature(game) && !card.hasSubtype(subType, game)) {
                game.getState().getCreateMageObjectAttribute(card, game).getSubtype().add(subType);
            }
        }
    }
    // creature spells you control
    for (Iterator<StackObject> iterator = game.getStack().iterator(); iterator.hasNext(); ) {
        StackObject stackObject = iterator.next();
        if (stackObject instanceof Spell && stackObject.isControlledBy(source.getControllerId()) && stackObject.isCreature(game) && !stackObject.hasSubtype(subType, game)) {
            Card card = ((Spell) stackObject).getCard();
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().add(subType);
        }
    }
    // creatures you control
    List<Permanent> creatures = game.getBattlefield().getAllActivePermanents(new FilterControlledCreaturePermanent(), source.getControllerId(), game);
    for (Permanent creature : creatures) {
        if (creature != null) {
            creature.addSubType(game, subType);
        }
    }
    return true;
}
Also used : CommandObject(mage.game.command.CommandObject) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) Commander(mage.game.command.Commander) StackObject(mage.game.stack.StackObject) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) UUID(java.util.UUID) Spell(mage.game.stack.Spell) Card(mage.cards.Card)

Example 2 with Commander

use of mage.game.command.Commander in project mage by magefree.

the class ZonesHandler method placeInDestinationZone.

private static void placeInDestinationZone(ZoneChangeInfo info, Game game, int createOrder) {
    // Handle unmelded cards
    if (info instanceof ZoneChangeInfo.Unmelded) {
        ZoneChangeInfo.Unmelded unmelded = (ZoneChangeInfo.Unmelded) info;
        Zone toZone = null;
        for (ZoneChangeInfo subInfo : unmelded.subInfo) {
            toZone = subInfo.event.getToZone();
            placeInDestinationZone(subInfo, game, createOrder);
        }
        // We arbitrarily prefer the bottom half card. This should never be relevant.
        if (toZone != null) {
            game.setZone(unmelded.event.getTargetId(), toZone);
        }
        return;
    }
    // Handle normal cases
    ZoneChangeEvent event = info.event;
    Zone toZone = event.getToZone();
    Card targetCard = getTargetCard(game, event.getTargetId());
    // moving real cards
    Cards cardsToMove = null;
    // updating all card's parts (must be ordered LinkedHashMap)
    Map<Zone, Cards> cardsToUpdate = new LinkedHashMap<>();
    cardsToUpdate.put(toZone, new CardsImpl());
    cardsToUpdate.put(Zone.OUTSIDE, new CardsImpl());
    // if we're moving a token it shouldn't be put into any zone as an object.
    if (!(targetCard instanceof Permanent) && targetCard != null) {
        if (targetCard instanceof MeldCard) {
            // meld/group cards must be independent (use can choose order)
            cardsToMove = ((MeldCard) targetCard).getHalves();
            cardsToUpdate.get(toZone).addAll(cardsToMove);
        } else if (targetCard instanceof ModalDoubleFacesCard || targetCard instanceof ModalDoubleFacesCardHalf) {
            // mdf cards must be moved as single object, but each half must be updated separately
            ModalDoubleFacesCard mdfCard = (ModalDoubleFacesCard) targetCard.getMainCard();
            cardsToMove = new CardsImpl(mdfCard);
            cardsToUpdate.get(toZone).add(mdfCard);
            // * right to outside (it helps to ignore all triggers and other effects from that card)
            switch(toZone) {
                case STACK:
                case BATTLEFIELD:
                    if (targetCard.getId().equals(mdfCard.getLeftHalfCard().getId())) {
                        // play left
                        cardsToUpdate.get(toZone).add(mdfCard.getLeftHalfCard());
                        cardsToUpdate.get(Zone.OUTSIDE).add(mdfCard.getRightHalfCard());
                    } else if (targetCard.getId().equals(mdfCard.getRightHalfCard().getId())) {
                        // play right
                        cardsToUpdate.get(toZone).add(mdfCard.getRightHalfCard());
                        cardsToUpdate.get(Zone.OUTSIDE).add(mdfCard.getLeftHalfCard());
                    } else {
                        // cast mdf (only on stack)
                        if (!toZone.equals(Zone.STACK)) {
                            throw new IllegalStateException("Wrong mdf card move to " + toZone + " in placeInDestinationZone");
                        }
                        cardsToUpdate.get(toZone).add(mdfCard.getLeftHalfCard());
                        cardsToUpdate.get(toZone).add(mdfCard.getRightHalfCard());
                    }
                    break;
                default:
                    // move all parts
                    cardsToUpdate.get(toZone).add(mdfCard.getLeftHalfCard());
                    cardsToUpdate.get(toZone).add(mdfCard.getRightHalfCard());
                    break;
            }
        } else {
            cardsToMove = new CardsImpl(targetCard);
            cardsToUpdate.get(toZone).addAll(cardsToMove);
        }
        Player owner = game.getPlayer(targetCard.getOwnerId());
        switch(toZone) {
            case HAND:
                for (Card card : cardsToMove.getCards(game)) {
                    game.getPlayer(card.getOwnerId()).getHand().add(card);
                }
                break;
            case GRAVEYARD:
                for (Card card : chooseOrder("order to put in graveyard (last chosen will be on top)", cardsToMove, owner, game)) {
                    game.getPlayer(card.getOwnerId()).getGraveyard().add(card);
                }
                break;
            case LIBRARY:
                if (info instanceof ZoneChangeInfo.Library && ((ZoneChangeInfo.Library) info).top) {
                    // on top
                    for (Card card : chooseOrder("order to put on top of library (last chosen will be topmost)", cardsToMove, owner, game)) {
                        game.getPlayer(card.getOwnerId()).getLibrary().putOnTop(card, game);
                    }
                } else {
                    // on bottom
                    for (Card card : chooseOrder("order to put on bottom of library (last chosen will be bottommost)", cardsToMove, owner, game)) {
                        game.getPlayer(card.getOwnerId()).getLibrary().putOnBottom(card, game);
                    }
                }
                break;
            case EXILED:
                for (Card card : cardsToMove.getCards(game)) {
                    if (info instanceof ZoneChangeInfo.Exile && ((ZoneChangeInfo.Exile) info).id != null) {
                        ZoneChangeInfo.Exile exileInfo = (ZoneChangeInfo.Exile) info;
                        game.getExile().createZone(exileInfo.id, exileInfo.name).add(card);
                    } else {
                        game.getExile().getPermanentExile().add(card);
                    }
                }
                break;
            case COMMAND:
                // There should never be more than one card here.
                for (Card card : cardsToMove.getCards(game)) {
                    game.addCommander(new Commander(card));
                }
                break;
            case STACK:
                // There should never be more than one card here.
                for (Card card : cardsToMove.getCards(game)) {
                    Spell spell;
                    if (info instanceof ZoneChangeInfo.Stack && ((ZoneChangeInfo.Stack) info).spell != null) {
                        spell = ((ZoneChangeInfo.Stack) info).spell;
                    } else {
                        spell = new Spell(card, card.getSpellAbility().copy(), card.getOwnerId(), event.getFromZone(), game);
                    }
                    spell.syncZoneChangeCounterOnStack(card, game);
                    game.getStack().push(spell);
                    game.getState().setZone(spell.getId(), Zone.STACK);
                    game.getState().setZone(card.getId(), Zone.STACK);
                }
                break;
            case BATTLEFIELD:
                Permanent permanent = event.getTarget();
                game.addPermanent(permanent, createOrder);
                game.getPermanentsEntering().remove(permanent.getId());
                break;
            default:
                throw new UnsupportedOperationException("to Zone " + toZone.toString() + " not supported yet");
        }
    }
    // update zone in main
    game.setZone(event.getTargetId(), event.getToZone());
    // update zone in other parts (meld cards, mdf half cards)
    cardsToUpdate.entrySet().forEach(entry -> {
        for (Card card : entry.getValue().getCards(game)) {
            if (!card.getId().equals(event.getTargetId())) {
                game.setZone(card.getId(), entry.getKey());
            }
        }
    });
    // reset meld status
    if (targetCard instanceof MeldCard) {
        if (event.getToZone() != Zone.BATTLEFIELD) {
            ((MeldCard) targetCard).setMelded(false, game);
        }
    }
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) Commander(mage.game.command.Commander) Zone(mage.constants.Zone) Spell(mage.game.stack.Spell) FilterCard(mage.filter.FilterCard) TargetCard(mage.target.TargetCard) PermanentCard(mage.game.permanent.PermanentCard) ZoneChangeEvent(mage.game.events.ZoneChangeEvent)

Example 3 with Commander

use of mage.game.command.Commander in project mage by magefree.

the class MaskwoodNexusEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller == null) {
        return false;
    }
    // in graveyard
    for (UUID cardId : controller.getGraveyard()) {
        Card card = game.getCard(cardId);
        if (card != null && card.isCreature(game)) {
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().setIsAllCreatureTypes(true);
        }
    }
    // on Hand
    for (UUID cardId : controller.getHand()) {
        Card card = game.getCard(cardId);
        if (card != null && card.isCreature(game)) {
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().setIsAllCreatureTypes(true);
        }
    }
    // in Exile
    for (Card card : game.getState().getExile().getAllCards(game)) {
        if (card.isCreature(game) && card.isOwnedBy(controller.getId())) {
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().setIsAllCreatureTypes(true);
        }
    }
    // in Library (e.g. for Mystical Teachings)
    for (Card card : controller.getLibrary().getCards(game)) {
        if (card.isOwnedBy(controller.getId()) && card.isCreature(game)) {
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().setIsAllCreatureTypes(true);
        }
    }
    // commander in command zone
    for (CommandObject commandObject : game.getState().getCommand()) {
        if (!(commandObject instanceof Commander)) {
            continue;
        }
        Card card = game.getCard(((Commander) commandObject).getId());
        if (card != null && card.isOwnedBy(controller.getId()) && card.isCreature(game)) {
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().setIsAllCreatureTypes(true);
        }
    }
    // creature spells you control
    for (Iterator<StackObject> iterator = game.getStack().iterator(); iterator.hasNext(); ) {
        StackObject stackObject = iterator.next();
        if (stackObject instanceof Spell && stackObject.isControlledBy(source.getControllerId()) && stackObject.isCreature(game)) {
            Card card = ((Spell) stackObject).getCard();
            game.getState().getCreateMageObjectAttribute(card, game).getSubtype().setIsAllCreatureTypes(true);
        }
    }
    // creatures you control
    List<Permanent> creatures = game.getBattlefield().getAllActivePermanents(new FilterControlledCreaturePermanent(), source.getControllerId(), game);
    for (Permanent creature : creatures) {
        if (creature != null) {
            creature.setIsAllCreatureTypes(game, true);
        }
    }
    return true;
}
Also used : CommandObject(mage.game.command.CommandObject) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) Commander(mage.game.command.Commander) StackObject(mage.game.stack.StackObject) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) UUID(java.util.UUID) Spell(mage.game.stack.Spell) Card(mage.cards.Card)

Example 4 with Commander

use of mage.game.command.Commander 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)

Aggregations

Commander (mage.game.command.Commander)4 Player (mage.players.Player)4 Permanent (mage.game.permanent.Permanent)3 Spell (mage.game.stack.Spell)3 UUID (java.util.UUID)2 Card (mage.cards.Card)2 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)2 CommandObject (mage.game.command.CommandObject)2 StackObject (mage.game.stack.StackObject)2 ArrayList (java.util.ArrayList)1 MageObject (mage.MageObject)1 Zone (mage.constants.Zone)1 FilterCard (mage.filter.FilterCard)1 ExileZone (mage.game.ExileZone)1 GameImpl (mage.game.GameImpl)1 ZoneChangeEvent (mage.game.events.ZoneChangeEvent)1 PermanentCard (mage.game.permanent.PermanentCard)1 TargetCard (mage.target.TargetCard)1 TargetPlayer (mage.target.TargetPlayer)1