Search in sources :

Example 16 with PermanentCard

use of mage.game.permanent.PermanentCard in project mage by magefree.

the class ZonesHandler method maybeRemoveFromSourceZone.

private static boolean maybeRemoveFromSourceZone(ZoneChangeInfo info, Game game, Ability source) {
    ZoneChangeEvent event = info.event;
    // Handle Unmelded Cards
    if (info instanceof ZoneChangeInfo.Unmelded) {
        ZoneChangeInfo.Unmelded unmelded = (ZoneChangeInfo.Unmelded) info;
        MeldCard meld = game.getMeldCard(event.getTargetId());
        for (Iterator<ZoneChangeInfo> itr = unmelded.subInfo.iterator(); itr.hasNext(); ) {
            ZoneChangeInfo subInfo = itr.next();
            if (!maybeRemoveFromSourceZone(subInfo, game, source)) {
                itr.remove();
            } else if (Objects.equals(subInfo.event.getTargetId(), meld.getTopHalfCard().getId())) {
                meld.setTopLastZoneChangeCounter(meld.getTopHalfCard().getZoneChangeCounter(game));
            } else if (Objects.equals(subInfo.event.getTargetId(), meld.getBottomHalfCard().getId())) {
                meld.setBottomLastZoneChangeCounter(meld.getBottomHalfCard().getZoneChangeCounter(game));
            }
        }
        if (unmelded.subInfo.isEmpty()) {
            return false;
        }
        // We arbitrarily prefer the bottom half card. This should never be relevant.
        meld.updateZoneChangeCounter(game, unmelded.subInfo.get(unmelded.subInfo.size() - 1).event);
        return true;
    }
    // Handle all normal cases
    Card card = getTargetCard(game, event.getTargetId());
    if (card == null) {
        // If we can't find the card we can't remove it.
        return false;
    }
    boolean success = false;
    if (info.faceDown) {
        card.setFaceDown(true, game);
    } else if (event.getToZone().equals(Zone.BATTLEFIELD)) {
        if (!card.isPermanent(game) && (!card.isTransformable() || Boolean.FALSE.equals(game.getState().getValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + card.getId())))) {
            // Non permanents (Instants, Sorceries, ... stay in the zone they are if an abilty/effect tries to move it to the battlefield
            return false;
        }
    }
    if (!game.replaceEvent(event)) {
        Zone fromZone = event.getFromZone();
        if (event.getToZone() == Zone.BATTLEFIELD) {
            // prepare card and permanent
            // If needed take attributes from the spell (e.g. color of spell was changed)
            card = takeAttributesFromSpell(card, event, game);
            // controlling player can be replaced so use event player now
            Permanent permanent;
            if (card instanceof MeldCard) {
                permanent = new PermanentMeld(card, event.getPlayerId(), game);
            } else if (card instanceof ModalDoubleFacesCard) {
                // main mdf card must be processed before that call (e.g. only halfes can be moved to battlefield)
                throw new IllegalStateException("Unexpected trying of move mdf card to battlefield instead half");
            } else if (card instanceof Permanent) {
                throw new IllegalStateException("Unexpected trying of move permanent to battlefield instead card");
            } else {
                permanent = new PermanentCard(card, event.getPlayerId(), game);
            }
            // put onto battlefield with possible counters
            game.getPermanentsEntering().put(permanent.getId(), permanent);
            card.checkForCountersToAdd(permanent, source, game);
            permanent.setTapped(info instanceof ZoneChangeInfo.Battlefield && ((ZoneChangeInfo.Battlefield) info).tapped);
            permanent.setFaceDown(info.faceDown, game);
            if (info.faceDown) {
                card.setFaceDown(false, game);
            }
            // make sure the controller of all continuous effects of this card are switched to the current controller
            game.setScopeRelevant(true);
            game.getContinuousEffects().setController(permanent.getId(), permanent.getControllerId());
            if (permanent.entersBattlefield(source, game, fromZone, true) && card.removeFromZone(game, fromZone, source)) {
                success = true;
                event.setTarget(permanent);
            } else {
                // revert controller to owner if permanent does not enter
                game.getContinuousEffects().setController(permanent.getId(), permanent.getOwnerId());
                game.getPermanentsEntering().remove(permanent.getId());
            }
            game.setScopeRelevant(false);
        } else if (event.getTarget() != null) {
            card.setFaceDown(info.faceDown, game);
            Permanent target = event.getTarget();
            success = target.removeFromZone(game, fromZone, source) && game.getPlayer(target.getControllerId()).removeFromBattlefield(target, source, game);
        } else {
            card.setFaceDown(info.faceDown, game);
            success = card.removeFromZone(game, fromZone, source);
        }
    }
    if (success) {
        // KickerTest do many tests for token's zcc
        if (event.getToZone() == Zone.BATTLEFIELD && event.getTarget() != null) {
            event.getTarget().updateZoneChangeCounter(game, event);
        } else if (!(card instanceof Permanent)) {
            card.updateZoneChangeCounter(game, event);
        }
    }
    return success;
}
Also used : Permanent(mage.game.permanent.Permanent) Zone(mage.constants.Zone) FilterCard(mage.filter.FilterCard) TargetCard(mage.target.TargetCard) PermanentCard(mage.game.permanent.PermanentCard) ZoneChangeEvent(mage.game.events.ZoneChangeEvent) PermanentMeld(mage.game.permanent.PermanentMeld) PermanentCard(mage.game.permanent.PermanentCard)

Example 17 with PermanentCard

use of mage.game.permanent.PermanentCard in project mage by magefree.

the class CopyTokenFunction method apply.

@Override
public Token apply(Card source, Game game) {
    if (target == null) {
        throw new IllegalArgumentException("Target can't be null");
    }
    // A copy contains only the attributes of the basic card or basic Token that's the base of the permanent
    // else gained abililies would be copied too.
    MageObject sourceObj = source;
    if (source instanceof PermanentToken) {
        sourceObj = ((PermanentToken) source).getToken();
        // to show the source image, the original values have to be used
        target.setOriginalExpansionSetCode(((Token) sourceObj).getOriginalExpansionSetCode());
        target.setOriginalCardNumber(((Token) sourceObj).getOriginalCardNumber());
        target.setCopySourceCard(((PermanentToken) source).getToken().getCopySourceCard());
    } else if (source instanceof PermanentCard) {
        if (((PermanentCard) source).isMorphed() || ((PermanentCard) source).isManifested()) {
            MorphAbility.setPermanentToFaceDownCreature(target, game);
            return target;
        } else {
            if (((PermanentCard) source).isTransformed() && source.getSecondCardFace() != null) {
                sourceObj = ((PermanentCard) source).getSecondCardFace();
            } else {
                sourceObj = ((PermanentCard) source).getCard();
            }
            target.setOriginalExpansionSetCode(source.getExpansionSetCode());
            target.setOriginalCardNumber(source.getCardNumber());
            target.setCopySourceCard((Card) sourceObj);
        }
    } else {
        target.setOriginalExpansionSetCode(source.getExpansionSetCode());
        target.setOriginalCardNumber(source.getCardNumber());
        target.setCopySourceCard(source);
    }
    // modify all attributes permanently (without game usage)
    target.setName(sourceObj.getName());
    target.getColor().setColor(sourceObj.getColor());
    target.getManaCost().clear();
    target.getManaCost().add(sourceObj.getManaCost().copy());
    target.removeAllCardTypes();
    for (CardType type : sourceObj.getCardType()) {
        target.addCardType(type);
    }
    target.getSubtype().copyFrom(sourceObj.getSubtype());
    target.getSuperType().clear();
    for (SuperType type : sourceObj.getSuperType()) {
        target.addSuperType(type);
    }
    target.getAbilities().clear();
    for (Ability ability0 : sourceObj.getAbilities()) {
        Ability ability = ability0.copy();
        // The token is independant from the copy from object so it need a new original Id,
        // otherwise there are problems to check for created continuous effects to check if
        // the source (the Token) has still this ability
        ability.newOriginalId();
        target.addAbility(ability);
    }
    target.getPower().modifyBaseValue(sourceObj.getPower().getBaseValueModified());
    target.getToughness().modifyBaseValue(sourceObj.getToughness().getBaseValueModified());
    target.setStartingLoyalty(sourceObj.getStartingLoyalty());
    return target;
}
Also used : MorphAbility(mage.abilities.keyword.MorphAbility) Ability(mage.abilities.Ability) CardType(mage.constants.CardType) MageObject(mage.MageObject) PermanentToken(mage.game.permanent.PermanentToken) SuperType(mage.constants.SuperType) PermanentCard(mage.game.permanent.PermanentCard) PermanentCard(mage.game.permanent.PermanentCard) Card(mage.cards.Card)

Example 18 with PermanentCard

use of mage.game.permanent.PermanentCard in project mage by magefree.

the class PlayerImpl method moveCardToLibraryWithInfo.

@Override
public boolean moveCardToLibraryWithInfo(Card card, Ability source, Game game, Zone fromZone, boolean toTop, boolean withName) {
    if (card == null) {
        return false;
    }
    boolean result = false;
    if (card.moveToZone(Zone.LIBRARY, source, game, toTop)) {
        if (!game.isSimulation()) {
            if (card instanceof PermanentCard && game.getCard(card.getId()) != null) {
                card = game.getCard(card.getId());
            }
            StringBuilder sb = new StringBuilder(this.getLogName()).append(" puts ").append(withName ? card.getLogName() : "a card").append(' ');
            if (fromZone != null) {
                sb.append("from ").append(fromZone.toString().toLowerCase(Locale.ENGLISH)).append(' ');
            }
            sb.append("to the ").append(toTop ? "top" : "bottom");
            if (card.isOwnedBy(getId())) {
                sb.append(" of their library");
            } else {
                Player player = game.getPlayer(card.getOwnerId());
                if (player != null) {
                    sb.append(" of ").append(player.getLogName()).append("'s library");
                }
            }
            sb.append(CardUtil.getSourceLogName(game, source, card.getId()));
            game.informPlayers(sb.toString());
        }
        result = true;
    }
    return result;
}
Also used : MatchPlayer(mage.game.match.MatchPlayer) PermanentCard(mage.game.permanent.PermanentCard)

Example 19 with PermanentCard

use of mage.game.permanent.PermanentCard in project mage by magefree.

the class HauntingImitationEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Cards cards = new CardsImpl();
    for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
        Player player = game.getPlayer(playerId);
        if (player == null) {
            continue;
        }
        Card card = player.getLibrary().getFromTop(game);
        if (card == null) {
            continue;
        }
        player.revealCards(source, new CardsImpl(cards), game);
        if (card.isCreature(game)) {
            cards.add(card);
        }
    }
    if (cards.isEmpty()) {
        Player player = game.getPlayer(source.getControllerId());
        MageObject sourceObject = source.getSourceObjectIfItStillExists(game);
        if (player != null && sourceObject instanceof Card) {
            player.moveCards((Card) sourceObject, Zone.HAND, source, game);
        }
        return true;
    }
    CreateTokenCopyTargetEffect effect = new CreateTokenCopyTargetEffect(source.getControllerId(), null, false, 1, false, false, null, 1, 1, true);
    effect.setAdditionalSubType(SubType.SPIRIT);
    for (Card card : cards.getCards(game)) {
        effect.setSavedPermanent(new PermanentCard(card, source.getControllerId(), game));
        effect.apply(game, source);
    }
    return true;
}
Also used : Player(mage.players.Player) MageObject(mage.MageObject) UUID(java.util.UUID) CreateTokenCopyTargetEffect(mage.abilities.effects.common.CreateTokenCopyTargetEffect) PermanentCard(mage.game.permanent.PermanentCard) PermanentCard(mage.game.permanent.PermanentCard)

Example 20 with PermanentCard

use of mage.game.permanent.PermanentCard in project mage by magefree.

the class LazavTheMultifariousCopyApplier method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent lazavTheMultifarious = game.getPermanent(source.getSourceId());
    Permanent newBluePrint = null;
    if (controller != null && lazavTheMultifarious != null) {
        Card copyFromCard = game.getCard(source.getFirstTarget());
        if (copyFromCard != null) {
            newBluePrint = new PermanentCard(copyFromCard, source.getControllerId(), game);
            newBluePrint.assignNewId();
            CopyApplier applier = new LazavTheMultifariousCopyApplier();
            applier.apply(game, newBluePrint, source, lazavTheMultifarious.getId());
            CopyEffect copyEffect = new CopyEffect(Duration.Custom, newBluePrint, lazavTheMultifarious.getId());
            copyEffect.newId();
            copyEffect.setApplier(applier);
            Ability newAbility = source.copy();
            copyEffect.init(newAbility, game);
            game.addEffect(copyEffect, newAbility);
        }
        return true;
    }
    return false;
}
Also used : CopyEffect(mage.abilities.effects.common.CopyEffect) SimpleActivatedAbility(mage.abilities.common.SimpleActivatedAbility) EntersBattlefieldTriggeredAbility(mage.abilities.common.EntersBattlefieldTriggeredAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) CopyApplier(mage.util.functions.CopyApplier) FilterCreatureCard(mage.filter.common.FilterCreatureCard) Card(mage.cards.Card) FilterCard(mage.filter.FilterCard) PermanentCard(mage.game.permanent.PermanentCard) PermanentCard(mage.game.permanent.PermanentCard)

Aggregations

PermanentCard (mage.game.permanent.PermanentCard)42 Card (mage.cards.Card)22 Permanent (mage.game.permanent.Permanent)16 Player (mage.players.Player)16 Ability (mage.abilities.Ability)11 MageObject (mage.MageObject)9 CardInfo (mage.cards.repository.CardInfo)9 CopyEffect (mage.abilities.effects.common.CopyEffect)8 FilterCard (mage.filter.FilterCard)6 CopyApplier (mage.util.functions.CopyApplier)6 UUID (java.util.UUID)5 PermanentMeld (mage.game.permanent.PermanentMeld)4 Test (org.junit.Test)4 SimpleActivatedAbility (mage.abilities.common.SimpleActivatedAbility)3 SimpleStaticAbility (mage.abilities.common.SimpleStaticAbility)3 CreateTokenCopyTargetEffect (mage.abilities.effects.common.CreateTokenCopyTargetEffect)3 FilterCreatureCard (mage.filter.common.FilterCreatureCard)3 PermanentToken (mage.game.permanent.PermanentToken)3 TargetCard (mage.target.TargetCard)3 Matcher (java.util.regex.Matcher)2