Search in sources :

Example 1 with TargetControlledPermanent

use of mage.target.common.TargetControlledPermanent in project mage by magefree.

the class DefilerOfSoulsEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("monocolored creature");
    Player player = game.getPlayer(targetPointer.getFirst(game, source));
    if (player == null) {
        return false;
    }
    filter.add(MonocoloredPredicate.instance);
    int amount;
    int realCount = game.getBattlefield().countAll(filter, player.getId(), game);
    amount = Math.min(1, realCount);
    Target target = new TargetControlledPermanent(amount, amount, filter, false);
    target.setNotTarget(true);
    // had, if thats the case this ability should fizzle.
    if (amount > 0 && target.canChoose(source.getSourceId(), player.getId(), game)) {
        boolean abilityApplied = false;
        while (player.canRespond() && !target.isChosen() && target.canChoose(source.getSourceId(), player.getId(), game)) {
            player.choose(Outcome.Sacrifice, target, source.getSourceId(), game);
        }
        for (int idx = 0; idx < target.getTargets().size(); idx++) {
            Permanent permanent = game.getPermanent(target.getTargets().get(idx));
            if (permanent != null) {
                abilityApplied |= permanent.sacrifice(source, game);
            }
        }
        return abilityApplied;
    }
    return false;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) Target(mage.target.Target) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent)

Example 2 with TargetControlledPermanent

use of mage.target.common.TargetControlledPermanent in project mage by magefree.

the class NomadMythmakerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Card aura = game.getCard(source.getFirstTarget());
    if (controller == null || aura == null) {
        return false;
    }
    FilterControlledCreaturePermanent FILTER = new FilterControlledCreaturePermanent("Choose a creature you control");
    TargetControlledPermanent target = new TargetControlledPermanent(FILTER);
    target.setNotTarget(true);
    if (controller.choose(Outcome.PutCardInPlay, target, source.getSourceId(), game)) {
        Permanent permanent = game.getPermanent(target.getFirstTarget());
        if (permanent != null && !permanent.cantBeAttachedBy(aura, source, game, false)) {
            game.getState().setValue("attachTo:" + aura.getId(), permanent);
            controller.moveCards(aura, Zone.BATTLEFIELD, source, game);
            return permanent.addAttachment(aura.getId(), source, game);
        }
    }
    return false;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterCard(mage.filter.FilterCard) Card(mage.cards.Card)

Example 3 with TargetControlledPermanent

use of mage.target.common.TargetControlledPermanent in project mage by magefree.

the class TheFirstEruptionEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller == null) {
        return false;
    }
    Target target = new TargetControlledPermanent(1, 1, filter, false);
    boolean sacrificed = false;
    if (target.canChoose(source.getSourceId(), controller.getId(), game)) {
        while (controller.canRespond() && !target.isChosen() && target.canChoose(source.getSourceId(), controller.getId(), game)) {
            controller.chooseTarget(Outcome.Sacrifice, target, source, game);
        }
        for (int idx = 0; idx < target.getTargets().size(); idx++) {
            Permanent permanent = game.getPermanent(target.getTargets().get(idx));
            if (permanent != null) {
                sacrificed |= permanent.sacrifice(source, game);
            }
        }
    }
    if (sacrificed) {
        return new DamageAllEffect(3, StaticFilters.FILTER_PERMANENT_CREATURE).apply(game, source);
    }
    return false;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) Target(mage.target.Target) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) DamageAllEffect(mage.abilities.effects.common.DamageAllEffect)

Example 4 with TargetControlledPermanent

use of mage.target.common.TargetControlledPermanent in project mage by magefree.

the class TransmuteArtifactEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller == null) {
        return false;
    }
    // Sacrifice an artifact.
    int manaValue = 0;
    boolean sacrifice = false;
    TargetControlledPermanent targetArtifact = new TargetControlledPermanent(new FilterControlledArtifactPermanent());
    if (controller.chooseTarget(Outcome.Sacrifice, targetArtifact, source, game)) {
        Permanent permanent = game.getPermanent(targetArtifact.getFirstTarget());
        if (permanent != null) {
            manaValue = permanent.getManaValue();
            sacrifice = permanent.sacrifice(source, game);
        }
    } else {
        return true;
    }
    // If you do, search your library for an artifact card.
    if (sacrifice && controller.searchLibrary(target, source, game)) {
        if (!target.getTargets().isEmpty()) {
            for (UUID cardId : target.getTargets()) {
                Card card = controller.getLibrary().getCard(cardId, game);
                if (card != null) {
                    // If that card's converted mana cost is less than or equal to the sacrificed artifact's converted mana cost, put it onto the battlefield.
                    if (card.getManaValue() <= manaValue) {
                        controller.moveCards(card, Zone.BATTLEFIELD, source, game);
                    } else {
                        // If it's greater, you may pay {X}, where X is the difference. If you do, put it onto the battlefield.
                        Cost cost = ManaUtil.createManaCost(card.getManaValue() - manaValue, true);
                        boolean payed = false;
                        if (controller.chooseUse(Outcome.Benefit, "Do you want to pay " + cost.getText() + " to put it onto the battlefield?", source, game) && cost.pay(source, game, source, source.getControllerId(), false)) {
                            payed = true;
                        }
                        if (payed) {
                            controller.moveCards(card, Zone.BATTLEFIELD, source, game);
                        } else {
                            // If you don't, put it into its owner's graveyard. Then shuffle your library
                            controller.moveCards(card, Zone.GRAVEYARD, source, game);
                        }
                    }
                }
            }
        }
        controller.shuffleLibrary(source, game);
        return true;
    }
    controller.shuffleLibrary(source, game);
    return false;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) FilterControlledArtifactPermanent(mage.filter.common.FilterControlledArtifactPermanent) FilterControlledArtifactPermanent(mage.filter.common.FilterControlledArtifactPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) UUID(java.util.UUID) Cost(mage.abilities.costs.Cost) Card(mage.cards.Card) FilterArtifactCard(mage.filter.common.FilterArtifactCard)

Example 5 with TargetControlledPermanent

use of mage.target.common.TargetControlledPermanent in project mage by magefree.

the class SacrificeTargetCost method canPay.

@Override
public boolean canPay(Ability ability, Ability source, UUID controllerId, Game game) {
    UUID activator = controllerId;
    if (ability.getAbilityType() == AbilityType.ACTIVATED || ability.getAbilityType() == AbilityType.SPECIAL_ACTION) {
        if (((ActivatedAbilityImpl) ability).getActivatorId() != null) {
            activator = ((ActivatedAbilityImpl) ability).getActivatorId();
        } else {
            // Activator not filled?
            activator = controllerId;
        }
    }
    int validTargets = 0;
    int neededtargets = targets.get(0).getNumberOfTargets();
    for (Permanent permanent : game.getBattlefield().getAllActivePermanents(((TargetControlledPermanent) targets.get(0)).getFilter(), controllerId, game)) {
        if (game.getPlayer(activator).canPaySacrificeCost(permanent, source, controllerId, game)) {
            validTargets++;
            if (validTargets >= neededtargets) {
                return true;
            }
        }
    }
    // solves issue #8097, if a sacrifice cost is optional and you don't have valid targets, then the cost can be paid
    if (validTargets == 0 && targets.get(0).getMinNumberOfTargets() == 0) {
        return true;
    }
    return false;
}
Also used : FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) UUID(java.util.UUID)

Aggregations

TargetControlledPermanent (mage.target.common.TargetControlledPermanent)100 Player (mage.players.Player)94 Permanent (mage.game.permanent.Permanent)87 FilterControlledPermanent (mage.filter.common.FilterControlledPermanent)49 UUID (java.util.UUID)47 Target (mage.target.Target)45 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)23 Card (mage.cards.Card)17 FilterControlledLandPermanent (mage.filter.common.FilterControlledLandPermanent)16 ArrayList (java.util.ArrayList)13 TargetPermanent (mage.target.TargetPermanent)13 SacrificeTargetCost (mage.abilities.costs.common.SacrificeTargetCost)10 FilterCard (mage.filter.FilterCard)10 TargetPlayer (mage.target.TargetPlayer)10 FilterPermanent (mage.filter.FilterPermanent)8 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)8 Cost (mage.abilities.costs.Cost)7 FilterControlledArtifactPermanent (mage.filter.common.FilterControlledArtifactPermanent)7 TargetCardInHand (mage.target.common.TargetCardInHand)6 OneShotEffect (mage.abilities.effects.OneShotEffect)5