Search in sources :

Example 66 with TargetControlledCreaturePermanent

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

the class QuestForTheHolyRelicEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller == null) {
        return false;
    }
    FilterCard filter = new FilterCard("Equipment");
    filter.add(SubType.EQUIPMENT.getPredicate());
    TargetCardInLibrary target = new TargetCardInLibrary(filter);
    if (controller.searchLibrary(target, source, game)) {
        Card card = controller.getLibrary().getCard(target.getFirstTarget(), game);
        if (card != null && controller.moveCards(card, Zone.BATTLEFIELD, source, game)) {
            Permanent equipment = game.getPermanent(card.getId());
            Target targetCreature = new TargetControlledCreaturePermanent();
            if (equipment != null && controller.choose(Outcome.BoostCreature, targetCreature, source.getSourceId(), game)) {
                Permanent permanent = game.getPermanent(targetCreature.getFirstTarget());
                permanent.addAttachment(equipment.getId(), source, game);
            }
        }
    }
    controller.shuffleLibrary(source, game);
    return true;
}
Also used : FilterCard(mage.filter.FilterCard) Player(mage.players.Player) Target(mage.target.Target) Permanent(mage.game.permanent.Permanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) Card(mage.cards.Card) FilterCard(mage.filter.FilterCard)

Example 67 with TargetControlledCreaturePermanent

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

the class SwordOfTruthAndJusticeEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    if (player == null) {
        return false;
    }
    Target target = new TargetControlledCreaturePermanent();
    target.setNotTarget(true);
    if (player.choose(outcome, target, source.getSourceId(), game)) {
        Permanent permanent = game.getPermanent(target.getFirstTarget());
        if (permanent != null) {
            permanent.addCounters(CounterType.P1P1.createInstance(), source.getControllerId(), source, game);
        }
    }
    return new ProliferateEffect().apply(game, source);
}
Also used : ProliferateEffect(mage.abilities.effects.common.counter.ProliferateEffect) Player(mage.players.Player) Target(mage.target.Target) Permanent(mage.game.permanent.Permanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent)

Example 68 with TargetControlledCreaturePermanent

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

the class ThelonsCurseEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(targetPointer.getFirst(game, source));
    Permanent sourcePermanent = game.getPermanent(source.getSourceId());
    if (player != null && sourcePermanent != null) {
        int countBattlefield = game.getBattlefield().getAllActivePermanents(filter, game.getActivePlayerId(), game).size();
        while (player.canRespond() && countBattlefield > 0 && player.chooseUse(Outcome.AIDontUseIt, "Pay {U} and untap a tapped blue creature under your control?", source, game)) {
            Target tappedCreatureTarget = new TargetControlledCreaturePermanent(1, 1, filter, true);
            if (player.choose(Outcome.Detriment, tappedCreatureTarget, source.getSourceId(), game)) {
                Cost cost = new ManaCostsImpl("U");
                Permanent tappedCreature = game.getPermanent(tappedCreatureTarget.getFirstTarget());
                if (cost.pay(source, game, source, player.getId(), false)) {
                    tappedCreature.untap(game);
                }
            }
            countBattlefield = game.getBattlefield().getAllActivePermanents(filter, game.getActivePlayerId(), game).size();
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) Cost(mage.abilities.costs.Cost) ManaCostsImpl(mage.abilities.costs.mana.ManaCostsImpl)

Example 69 with TargetControlledCreaturePermanent

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

the class XathridDemonEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent sourcePermanent = game.getPermanent(source.getSourceId());
    if (sourcePermanent == null) {
        sourcePermanent = (Permanent) game.getLastKnownInformation(source.getSourceId(), Zone.BATTLEFIELD);
    }
    if (controller == null || sourcePermanent == null) {
        return false;
    }
    FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("creature other than " + sourcePermanent.getName());
    filter.add(AnotherPredicate.instance);
    Target target = new TargetControlledCreaturePermanent(1, 1, filter, true);
    if (target.canChoose(source.getSourceId(), controller.getId(), game)) {
        controller.choose(Outcome.Sacrifice, target, source.getSourceId(), game);
        Permanent permanent = game.getPermanent(target.getFirstTarget());
        if (permanent != null) {
            int amount = permanent.getPower().getValue();
            permanent.sacrifice(source, game);
            if (amount > 0) {
                Set<UUID> opponents = game.getOpponents(source.getControllerId());
                for (UUID opponentId : opponents) {
                    Player opponent = game.getPlayer(opponentId);
                    if (opponent != null) {
                        opponent.loseLife(amount, game, source, false);
                    }
                }
            }
            return true;
        }
    } else {
        sourcePermanent.tap(source, game);
        controller.loseLife(7, game, source, false);
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) UUID(java.util.UUID) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent)

Example 70 with TargetControlledCreaturePermanent

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

the class CamouflageEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Map<UUID, List<List<Permanent>>> masterMap = new HashMap<>();
        // and divides them into a number of piles equal to the number of attacking creatures for whom that player is the defending player (piles can be empty)
        for (UUID defenderId : game.getCombat().getPlayerDefenders(game)) {
            Player defender = game.getPlayer(defenderId);
            if (defender != null) {
                List<List<Permanent>> masterList = new ArrayList<>();
                int attackerCount = 0;
                for (CombatGroup combatGroup : game.getCombat().getGroups()) {
                    if (combatGroup.getDefendingPlayerId().equals(defenderId)) {
                        attackerCount += combatGroup.getAttackers().size();
                    }
                }
                // This shouldn't be necessary, but just in case
                for (Permanent permanent : game.getBattlefield().getAllActivePermanents(new FilterCreaturePermanent(), defenderId, game)) {
                    permanent.setBlocking(0);
                }
                boolean declinedChoice = false;
                while (masterList.size() < attackerCount) {
                    List<Permanent> newPile = new ArrayList<>();
                    if (!declinedChoice) {
                        FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("creatures you control not yet assigned to a pile");
                        for (List<Permanent> list : masterList) {
                            // Creatures they control that can block additional creatures may likewise be put into additional piles.
                            // (This temporarily manipulates Blocking values to "test" how many blockers the creature has still left to assign)
                            List<Permanent> spentBlockers = new ArrayList<>();
                            for (Permanent possibleBlocker : list) {
                                if (possibleBlocker.getMaxBlocks() != 0 && possibleBlocker.getBlocking() >= possibleBlocker.getMaxBlocks()) {
                                    spentBlockers.add(possibleBlocker);
                                }
                            }
                            filter.add(Predicates.not(new PermanentInListPredicate(spentBlockers)));
                        }
                        if (defender.chooseUse(Outcome.Neutral, "Make a new blocker pile? If not, all remaining piles stay empty. (remaining piles: " + (attackerCount - masterList.size()) + ')', source, game)) {
                            Target target = new TargetControlledCreaturePermanent(0, Integer.MAX_VALUE, filter, true);
                            if (target.canChoose(source.getSourceId(), defenderId, game)) {
                                if (defender.chooseTarget(Outcome.Neutral, target, source, game)) {
                                    for (UUID creatureId : target.getTargets()) {
                                        Permanent creature = game.getPermanent(creatureId);
                                        if (creature != null) {
                                            creature.setBlocking(creature.getBlocking() + 1);
                                            newPile.add(creature);
                                        }
                                    }
                                }
                            }
                        } else {
                            declinedChoice = true;
                        }
                    }
                    masterList.add(newPile);
                    StringBuilder sb = new StringBuilder("Blocker pile of ").append(defender.getLogName()).append(" (no. " + masterList.size() + "): ");
                    int i = 0;
                    for (Permanent permanent : newPile) {
                        i++;
                        sb.append(permanent.getLogName());
                        if (i < newPile.size()) {
                            sb.append(", ");
                        }
                    }
                    game.informPlayers(sb.toString());
                }
                // Clear all test Blocking values before assigning piles
                for (Permanent permanent : game.getBattlefield().getAllActivePermanents(new FilterCreaturePermanent(), defenderId, game)) {
                    permanent.setBlocking(0);
                }
                masterMap.put(defenderId, masterList);
            }
        }
        // Assign each pile to a different one of those attacking creatures at random. Each creature in a pile that can block the creature that pile is assigned to does so
        if (!masterMap.isEmpty()) {
            for (UUID playerId : masterMap.keySet()) {
                List<Permanent> available = new ArrayList<>();
                for (CombatGroup combatGroup : game.getCombat().getGroups()) {
                    if (combatGroup.getDefendingPlayerId().equals(playerId)) {
                        for (UUID attackerId : combatGroup.getAttackers()) {
                            Permanent permanent = game.getPermanent(attackerId);
                            if (permanent != null && permanent.isCreature(game)) {
                                available.add(permanent);
                            }
                        }
                    }
                }
                List<List<Permanent>> allPiles = masterMap.get(playerId);
                for (List<Permanent> pile : allPiles) {
                    if (available.isEmpty()) {
                        break;
                    }
                    int randomAttacker = RandomUtil.nextInt(available.size());
                    Permanent attacker = available.get(randomAttacker);
                    if (attacker != null) {
                        available.remove(randomAttacker);
                        for (Permanent blocker : pile) {
                            CombatGroup group = game.getCombat().findGroup(attacker.getId());
                            if (group != null) {
                                if (blocker.canBlock(attacker.getId(), game) && (blocker.getMaxBlocks() == 0 || group.getAttackers().size() <= blocker.getMaxBlocks())) {
                                    boolean notYetBlocked = group.getBlockers().isEmpty();
                                    group.addBlockerToGroup(blocker.getId(), blocker.getControllerId(), game);
                                    game.getCombat().addBlockingGroup(blocker.getId(), attacker.getId(), blocker.getControllerId(), game);
                                    if (notYetBlocked) {
                                        game.fireEvent(GameEvent.getEvent(GameEvent.EventType.CREATURE_BLOCKED, attacker.getId(), source, null));
                                    }
                                    // TODO: find an alternate event solution for multi-blockers (as per issue #4285), this will work fine for single blocker creatures though
                                    game.fireEvent(new BlockerDeclaredEvent(attacker.getId(), blocker.getId(), blocker.getControllerId()));
                                }
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) PermanentInListPredicate(mage.filter.predicate.permanent.PermanentInListPredicate) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) Target(mage.target.Target) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) BlockerDeclaredEvent(mage.game.events.BlockerDeclaredEvent) CombatGroup(mage.game.combat.CombatGroup)

Aggregations

TargetControlledCreaturePermanent (mage.target.common.TargetControlledCreaturePermanent)78 Player (mage.players.Player)73 Permanent (mage.game.permanent.Permanent)64 Target (mage.target.Target)41 UUID (java.util.UUID)29 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)27 TargetPermanent (mage.target.TargetPermanent)18 ArrayList (java.util.ArrayList)14 Cost (mage.abilities.costs.Cost)10 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)9 SacrificeTargetCost (mage.abilities.costs.common.SacrificeTargetCost)8 FixedTarget (mage.target.targetpointer.FixedTarget)8 Ability (mage.abilities.Ability)6 FilterPermanent (mage.filter.FilterPermanent)6 MageObject (mage.MageObject)5 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)5 OneShotEffect (mage.abilities.effects.OneShotEffect)4 Card (mage.cards.Card)4 SimpleActivatedAbility (mage.abilities.common.SimpleActivatedAbility)3 SimpleStaticAbility (mage.abilities.common.SimpleStaticAbility)3