Search in sources :

Example 16 with CardIdPredicate

use of mage.filter.predicate.mageobject.CardIdPredicate in project mage by magefree.

the class DescentIntoMadnessEffect method selectCards.

private void selectCards(Player player, List<UUID> selectedObjects, int count, Ability source, Game game) {
    int amount = Math.min(count, player.getHand().size() + game.getBattlefield().getAllActivePermanents(player.getId()).size());
    int cardsFromHand = 0;
    while (player.canRespond() && amount > 0) {
        Target target;
        do {
            FilterControlledPermanent filter = new FilterControlledPermanent();
            filter.setMessage("permanent you control (" + amount + " left in total)");
            List<PermanentIdPredicate> uuidPredicates = new ArrayList<>();
            for (UUID uuid : selectedObjects) {
                uuidPredicates.add(new PermanentIdPredicate(uuid));
            }
            filter.add(Predicates.not(Predicates.or(uuidPredicates)));
            target = new TargetControlledPermanent(0, 1, filter, true);
            if (target.canChoose(source.getSourceId(), player.getId(), game) && player.choose(Outcome.Exile, target, source.getSourceId(), game)) {
                for (UUID targetId : target.getTargets()) {
                    if (!selectedObjects.contains(targetId)) {
                        Permanent chosen = game.getPermanent(targetId);
                        if (chosen != null) {
                            amount--;
                            game.informPlayers(player.getLogName() + " selects " + chosen.getLogName() + " from battlefield");
                            selectedObjects.add(targetId);
                        }
                    }
                }
            }
        } while (amount > 0 && !target.getTargets().isEmpty() && player.canRespond());
        if (amount > 0) {
            TargetCard targetInHand;
            do {
                FilterCard filterInHand = new FilterCard();
                filterInHand.setMessage("card from your hand (" + amount + " left in total)");
                targetInHand = new TargetCard(0, 1, Zone.HAND, filterInHand);
                List<CardIdPredicate> uuidPredicates = new ArrayList<>();
                for (UUID uuid : selectedObjects) {
                    uuidPredicates.add(new CardIdPredicate(uuid));
                }
                filterInHand.add(Predicates.not(Predicates.or(uuidPredicates)));
                if (targetInHand.canChoose(source.getSourceId(), player.getId(), game) && player.choose(Outcome.Exile, player.getHand(), targetInHand, game)) {
                    Card card = player.getHand().get(targetInHand.getFirstTarget(), game);
                    if (card != null) {
                        selectedObjects.add(targetInHand.getFirstTarget());
                        amount--;
                        cardsFromHand++;
                    }
                }
            } while (amount > 0 && !targetInHand.getTargets().isEmpty() && player.canRespond());
        }
    }
    if (cardsFromHand > 0) {
        game.informPlayers(player.getLogName() + " selects " + cardsFromHand + (cardsFromHand == 1 ? " card" : " cards") + " from their hand");
    }
}
Also used : PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) ArrayList(java.util.ArrayList) TargetCard(mage.target.TargetCard) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) TargetCard(mage.target.TargetCard) Card(mage.cards.Card) FilterCard(mage.filter.FilterCard) FilterCard(mage.filter.FilterCard) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Target(mage.target.Target) UUID(java.util.UUID) CardIdPredicate(mage.filter.predicate.mageobject.CardIdPredicate)

Example 17 with CardIdPredicate

use of mage.filter.predicate.mageobject.CardIdPredicate in project mage by magefree.

the class ArmoryAutomatonEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    Permanent sourcePermanent = game.getPermanent(source.getSourceId());
    if (player != null && sourcePermanent != null) {
        // dynamic filter (can't selects own attaches and can't selects twice)
        FilterPermanent currentFilter = new FilterEquipmentPermanent();
        FilterPermanent filterSourceId = new FilterPermanent();
        filterSourceId.add(new CardIdPredicate(source.getSourceId()));
        currentFilter.add(Predicates.not(new AttachedToPredicate(filterSourceId)));
        int countBattlefield = game.getBattlefield().getAllActivePermanents(currentFilter, game).size();
        while (player.canRespond() && countBattlefield > 0 && player.chooseUse(Outcome.Benefit, "Select and attach a target Equipment?", source, game)) {
            Target targetEquipment = new TargetPermanent(currentFilter);
            targetEquipment.setRequired(false);
            if (player.choose(Outcome.Benefit, targetEquipment, source.getSourceId(), game) && targetEquipment.getFirstTarget() != null) {
                // exclude selected for next time
                currentFilter.add(Predicates.not(new PermanentIdPredicate(targetEquipment.getFirstTarget())));
                Permanent aura = game.getPermanent(targetEquipment.getFirstTarget());
                if (aura != null) {
                    Permanent attachedTo = game.getPermanent(aura.getAttachedTo());
                    if (attachedTo != null) {
                        attachedTo.removeAttachment(aura.getId(), source, game);
                    }
                    sourcePermanent.addAttachment(aura.getId(), source, game);
                }
            } else {
                break;
            }
            countBattlefield = game.getBattlefield().getAllActivePermanents(currentFilter, game).size();
        }
        return true;
    }
    return false;
}
Also used : PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) Player(mage.players.Player) Target(mage.target.Target) FilterPermanent(mage.filter.FilterPermanent) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) FilterEquipmentPermanent(mage.filter.common.FilterEquipmentPermanent) FilterArtifactPermanent(mage.filter.common.FilterArtifactPermanent) TargetPermanent(mage.target.TargetPermanent) FilterEquipmentPermanent(mage.filter.common.FilterEquipmentPermanent) AttachedToPredicate(mage.filter.predicate.permanent.AttachedToPredicate) TargetPermanent(mage.target.TargetPermanent) CardIdPredicate(mage.filter.predicate.mageobject.CardIdPredicate)

Example 18 with CardIdPredicate

use of mage.filter.predicate.mageobject.CardIdPredicate in project mage by magefree.

the class DanseMacabreEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller == null) {
        return false;
    }
    int toughness = 0;
    Cards cards = new CardsImpl();
    for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
        Player player = game.getPlayer(playerId);
        if (player == null || game.getBattlefield().count(filter, source.getSourceId(), source.getControllerId(), game) < 1) {
            continue;
        }
        TargetPermanent target = new TargetPermanent(filter);
        target.setNotTarget(true);
        player.choose(Outcome.Sacrifice, target, source.getSourceId(), game);
        Permanent permanent = game.getPermanent(target.getFirstTarget());
        if (permanent == null) {
            continue;
        }
        if (source.isControlledBy(playerId)) {
            toughness += permanent.getToughness().getValue();
        }
        cards.add(permanent);
        permanent.sacrifice(source, game);
    }
    int result = controller.rollDice(outcome, source, game, 20) + toughness;
    cards.retainZone(Zone.GRAVEYARD, game);
    if (cards.isEmpty()) {
        return true;
    }
    FilterCard filterCard = new FilterCard("card put into a graveyard this way");
    filterCard.add(Predicates.or(cards.stream().map(cardId -> new CardIdPredicate(cardId)).collect(Collectors.toSet())));
    TargetCardInGraveyard target;
    if (result >= 15) {
        target = new TargetCardInGraveyard(0, 2, filterCard);
    } else if (result > 0) {
        target = new TargetCardInGraveyard(filterCard);
    } else {
        return true;
    }
    target.setNotTarget(true);
    controller.choose(Outcome.PutCreatureInPlay, target, source.getSourceId(), game);
    controller.moveCards(new CardsImpl(target.getTargets()), Zone.BATTLEFIELD, source, game);
    return true;
}
Also used : FilterCard(mage.filter.FilterCard) FilterCard(mage.filter.FilterCard) Zone(mage.constants.Zone) Cards(mage.cards.Cards) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) Predicates(mage.filter.predicate.Predicates) CardIdPredicate(mage.filter.predicate.mageobject.CardIdPredicate) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) UUID(java.util.UUID) CardsImpl(mage.cards.CardsImpl) FilterPermanent(mage.filter.FilterPermanent) Collectors(java.util.stream.Collectors) Player(mage.players.Player) CardSetInfo(mage.cards.CardSetInfo) Game(mage.game.Game) CardImpl(mage.cards.CardImpl) Permanent(mage.game.permanent.Permanent) CardType(mage.constants.CardType) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetPermanent(mage.target.TargetPermanent) Ability(mage.abilities.Ability) TokenPredicate(mage.filter.predicate.permanent.TokenPredicate) Player(mage.players.Player) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetPermanent(mage.target.TargetPermanent) TargetPermanent(mage.target.TargetPermanent) UUID(java.util.UUID) Cards(mage.cards.Cards) CardsImpl(mage.cards.CardsImpl) CardIdPredicate(mage.filter.predicate.mageobject.CardIdPredicate)

Aggregations

CardIdPredicate (mage.filter.predicate.mageobject.CardIdPredicate)18 Player (mage.players.Player)13 FilterCard (mage.filter.FilterCard)11 Permanent (mage.game.permanent.Permanent)10 UUID (java.util.UUID)7 FilterPermanent (mage.filter.FilterPermanent)6 Target (mage.target.Target)6 TargetCardInGraveyard (mage.target.common.TargetCardInGraveyard)5 ArrayList (java.util.ArrayList)4 Card (mage.cards.Card)4 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)3 FilterCreatureCard (mage.filter.common.FilterCreatureCard)3 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)3 Predicate (mage.filter.predicate.Predicate)3 OwnerIdPredicate (mage.filter.predicate.card.OwnerIdPredicate)3 TargetPermanent (mage.target.TargetPermanent)3 MageObject (mage.MageObject)2 Ability (mage.abilities.Ability)2 Cost (mage.abilities.costs.Cost)2 ManaCostsImpl (mage.abilities.costs.mana.ManaCostsImpl)2