Search in sources :

Example 1 with FilterPlayer

use of mage.filter.FilterPlayer in project mage by magefree.

the class LoxodonPeacekeeperEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Permanent sourcePermanent = game.getPermanent(source.getSourceId());
        if (sourcePermanent != null) {
            int lowLife = Integer.MAX_VALUE;
            Set<UUID> tiedPlayers = new HashSet<>();
            for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
                Player player = game.getPlayer(playerId);
                if (player != null) {
                    if (player.getLife() < lowLife) {
                        lowLife = player.getLife();
                    }
                }
            }
            for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
                Player player = game.getPlayer(playerId);
                if (player != null) {
                    if (player.getLife() == lowLife) {
                        tiedPlayers.add(playerId);
                    }
                }
            }
            if (tiedPlayers.size() > 0) {
                UUID newControllerId = null;
                if (tiedPlayers.size() > 1) {
                    FilterPlayer filter = new FilterPlayer("a player tied for lowest life total");
                    for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
                        if (!tiedPlayers.contains(playerId)) {
                            filter.add(Predicates.not(new PlayerIdPredicate(playerId)));
                        }
                    }
                    TargetPlayer target = new TargetPlayer(1, 1, true, filter);
                    if (target.canChoose(source.getSourceId(), controller.getId(), game)) {
                        while (!target.isChosen() && target.canChoose(source.getSourceId(), controller.getId(), game) && controller.canRespond()) {
                            controller.chooseTarget(outcome, target, source, game);
                        }
                    } else {
                        return false;
                    }
                    newControllerId = game.getPlayer(target.getFirstTarget()).getId();
                } else {
                    for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
                        if (tiedPlayers.contains(playerId)) {
                            newControllerId = playerId;
                            break;
                        }
                    }
                }
                if (newControllerId != null) {
                    ContinuousEffect effect = new GainControlTargetEffect(Duration.Custom, newControllerId);
                    effect.setTargetPointer(new FixedTarget(sourcePermanent, game));
                    game.addEffect(effect, source);
                    game.informPlayers(game.getPlayer(newControllerId).getLogName() + " has gained control of " + sourcePermanent.getLogName());
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) FilterPlayer(mage.filter.FilterPlayer) Permanent(mage.game.permanent.Permanent) FilterPlayer(mage.filter.FilterPlayer) PlayerIdPredicate(mage.filter.predicate.other.PlayerIdPredicate) ContinuousEffect(mage.abilities.effects.ContinuousEffect) UUID(java.util.UUID) GainControlTargetEffect(mage.abilities.effects.common.continuous.GainControlTargetEffect) TargetPlayer(mage.target.TargetPlayer) HashSet(java.util.HashSet)

Example 2 with FilterPlayer

use of mage.filter.FilterPlayer in project mage by magefree.

the class CapricopianEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    Permanent permanent = game.getPermanent(source.getSourceId());
    if (player == null || permanent == null) {
        return false;
    }
    if (!player.chooseUse(outcome, "Reselect attacker for " + permanent.getIdName() + "?", source, game)) {
        return false;
    }
    FilterPlayer filterPlayer = new FilterPlayer();
    filterPlayer.add(Predicates.not(new PlayerIdPredicate(permanent.getControllerId())));
    filterPlayer.add(Predicates.not(new PlayerIdPredicate(game.getCombat().getDefenderId(permanent.getId()))));
    TargetPlayer targetPlayer = new TargetPlayer(0, 1, true, filterPlayer);
    player.choose(outcome, targetPlayer, source.getSourceId(), game);
    Player newPlayer = game.getPlayer(targetPlayer.getFirstTarget());
    if (newPlayer == null) {
        return false;
    }
    game.getCombat().removeAttacker(permanent.getId(), game);
    return game.getCombat().addAttackingCreature(permanent.getId(), game, newPlayer.getId());
}
Also used : TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) FilterPlayer(mage.filter.FilterPlayer) Permanent(mage.game.permanent.Permanent) FilterPlayer(mage.filter.FilterPlayer) PlayerIdPredicate(mage.filter.predicate.other.PlayerIdPredicate) TargetPlayer(mage.target.TargetPlayer)

Example 3 with FilterPlayer

use of mage.filter.FilterPlayer in project mage by magefree.

the class EonFrolickerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getFirstTarget());
    if (player == null) {
        return false;
    }
    game.getState().getTurnMods().add(new TurnMod(player.getId(), false));
    FilterPlayer filter = new FilterPlayer(player.getName());
    filter.add(new PlayerIdPredicate(player.getId()));
    Ability ability = new ProtectionAbility(filter);
    game.addEffect(new GainAbilityControlledEffect(ability, Duration.UntilYourNextTurn, StaticFilters.FILTER_PERMANENT_PLANESWALKER), source);
    game.addEffect(new GainAbilityControllerEffect(ability, Duration.UntilYourNextTurn), source);
    return true;
}
Also used : EntersBattlefieldTriggeredAbility(mage.abilities.common.EntersBattlefieldTriggeredAbility) ConditionalInterveningIfTriggeredAbility(mage.abilities.decorator.ConditionalInterveningIfTriggeredAbility) FlyingAbility(mage.abilities.keyword.FlyingAbility) ProtectionAbility(mage.abilities.keyword.ProtectionAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) FilterPlayer(mage.filter.FilterPlayer) GainAbilityControlledEffect(mage.abilities.effects.common.continuous.GainAbilityControlledEffect) FilterPlayer(mage.filter.FilterPlayer) PlayerIdPredicate(mage.filter.predicate.other.PlayerIdPredicate) ProtectionAbility(mage.abilities.keyword.ProtectionAbility) TurnMod(mage.game.turn.TurnMod) GainAbilityControllerEffect(mage.abilities.effects.common.continuous.GainAbilityControllerEffect)

Example 4 with FilterPlayer

use of mage.filter.FilterPlayer in project mage by magefree.

the class PreventAllDamageToAllEffect method createFilter.

private static FilterPermanentOrPlayer createFilter(FilterPermanent filterPermanent, FilterPlayer filterPlayer) {
    String mes1 = filterPermanent != null ? filterPermanent.getMessage() : "";
    String mes2 = filterPlayer != null ? filterPlayer.getMessage() : "";
    String message;
    if (!mes1.isEmpty() && !mes2.isEmpty()) {
        message = mes1 + " and " + mes2;
    } else {
        message = mes1 + mes2;
    }
    FilterPermanent filter1 = filterPermanent;
    if (filter1 == null) {
        filter1 = new FilterPermanent();
        // disable filter
        filter1.add(new PermanentIdPredicate(UUID.randomUUID()));
    }
    FilterPlayer filter2 = filterPlayer;
    if (filter2 == null) {
        filter2 = new FilterPlayer();
        // disable filter
        filter2.add(new PlayerIdPredicate(UUID.randomUUID()));
    }
    return new FilterPermanentOrPlayer(message, filter1, filter2);
}
Also used : PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) FilterPermanentOrPlayer(mage.filter.common.FilterPermanentOrPlayer) FilterPermanent(mage.filter.FilterPermanent) FilterPlayer(mage.filter.FilterPlayer) PlayerIdPredicate(mage.filter.predicate.other.PlayerIdPredicate)

Example 5 with FilterPlayer

use of mage.filter.FilterPlayer in project mage by magefree.

the class BendOrBreakEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        // Map of players and their piles
        Map<UUID, List<List<Permanent>>> playerPermanents = new LinkedHashMap<>();
        PlayerList playerList = game.getState().getPlayerList().copy();
        while (!playerList.get().equals(source.getControllerId()) && controller.canRespond()) {
            playerList.getNext();
        }
        Player currentPlayer = game.getPlayer(playerList.get());
        Player nextPlayer;
        UUID firstNextPlayer = null;
        while (!getNextPlayerInDirection(true, playerList).equals(firstNextPlayer) && controller.canRespond()) {
            nextPlayer = game.getPlayer(playerList.get());
            if (nextPlayer == null) {
                return false;
            }
            if (firstNextPlayer == null) {
                firstNextPlayer = nextPlayer.getId();
            }
            if (!nextPlayer.canRespond()) {
                continue;
            }
            // Each player separates all nontoken lands they control into two piles
            if (currentPlayer != null && game.getState().getPlayersInRange(controller.getId(), game).contains(currentPlayer.getId())) {
                List<Permanent> firstPile = new ArrayList<>();
                List<Permanent> secondPile = new ArrayList<>();
                FilterControlledLandPermanent filter = new FilterControlledLandPermanent("lands you control to assign to the first pile (lands not chosen will be assigned to the second pile)");
                TargetPermanent target = new TargetControlledPermanent(0, Integer.MAX_VALUE, filter, true);
                if (target.canChoose(source.getSourceId(), currentPlayer.getId(), game)) {
                    // TODO: add support for AI (50/50), need AI hints mechanic here
                    currentPlayer.chooseTarget(Outcome.Neutral, target, source, game);
                    for (Permanent permanent : game.getBattlefield().getAllActivePermanents(filter, currentPlayer.getId(), game)) {
                        if (target.getTargets().contains(permanent.getId())) {
                            firstPile.add(permanent);
                        } else {
                            secondPile.add(permanent);
                        }
                    }
                    StringBuilder sb = new StringBuilder("First pile of ").append(currentPlayer.getLogName()).append(": ");
                    sb.append(firstPile.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
                    game.informPlayers(sb.toString());
                    sb = new StringBuilder("Second pile of ").append(currentPlayer.getLogName()).append(": ");
                    sb.append(secondPile.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
                    game.informPlayers(sb.toString());
                }
                List<List<Permanent>> playerPiles = new ArrayList<>();
                playerPiles.add(firstPile);
                playerPiles.add(secondPile);
                playerPermanents.put(currentPlayer.getId(), playerPiles);
            }
            currentPlayer = nextPlayer;
        }
        // For each player, one of their piles is chosen by one of their opponents of their choice
        for (Map.Entry<UUID, List<List<Permanent>>> playerPiles : playerPermanents.entrySet()) {
            Player player = game.getPlayer(playerPiles.getKey());
            if (player != null) {
                FilterPlayer filter = new FilterPlayer("opponent");
                List<PlayerIdPredicate> opponentPredicates = new ArrayList<>();
                for (UUID opponentId : game.getOpponents(player.getId())) {
                    opponentPredicates.add(new PlayerIdPredicate(opponentId));
                }
                filter.add(Predicates.or(opponentPredicates));
                Target target = new TargetPlayer(1, 1, true, filter);
                target.setTargetController(player.getId());
                target.setAbilityController(source.getControllerId());
                if (player.chooseTarget(outcome, target, source, game)) {
                    Player chosenOpponent = game.getPlayer(target.getFirstTarget());
                    if (chosenOpponent != null) {
                        List<Permanent> firstPile = playerPiles.getValue().get(0);
                        List<Permanent> secondPile = playerPiles.getValue().get(1);
                        game.informPlayers(player.getLogName() + " chose " + chosenOpponent.getLogName() + " to choose their pile");
                        if (chosenOpponent.choosePile(outcome, "Piles of " + player.getName(), firstPile, secondPile, game)) {
                            List<List<Permanent>> lists = playerPiles.getValue();
                            lists.clear();
                            lists.add(firstPile);
                            lists.add(secondPile);
                            game.informPlayers(player.getLogName() + " will have their first pile destroyed");
                        } else {
                            List<List<Permanent>> lists = playerPiles.getValue();
                            lists.clear();
                            lists.add(secondPile);
                            lists.add(firstPile);
                            game.informPlayers(player.getLogName() + " will have their second pile destroyed");
                        }
                    }
                }
            }
        }
        // Destroy all lands in the chosen piles. Tap all lands in the other piles
        for (Map.Entry<UUID, List<List<Permanent>>> playerPiles : playerPermanents.entrySet()) {
            Player player = game.getPlayer(playerPiles.getKey());
            if (player != null) {
                List<Permanent> pileToSac = playerPiles.getValue().get(0);
                List<Permanent> pileToTap = playerPiles.getValue().get(1);
                for (Permanent permanent : pileToSac) {
                    if (permanent != null) {
                        permanent.destroy(source, game, false);
                    }
                }
                for (Permanent permanent : pileToTap) {
                    if (permanent != null) {
                        permanent.tap(source, game);
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Also used : TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) FilterPlayer(mage.filter.FilterPlayer) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent) TargetPermanent(mage.target.TargetPermanent) PlayerList(mage.players.PlayerList) FilterPlayer(mage.filter.FilterPlayer) TargetPlayer(mage.target.TargetPlayer) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Target(mage.target.Target) PlayerIdPredicate(mage.filter.predicate.other.PlayerIdPredicate) PlayerList(mage.players.PlayerList) TargetPermanent(mage.target.TargetPermanent) FilterControlledLandPermanent(mage.filter.common.FilterControlledLandPermanent)

Aggregations

FilterPlayer (mage.filter.FilterPlayer)6 PlayerIdPredicate (mage.filter.predicate.other.PlayerIdPredicate)6 Player (mage.players.Player)5 Permanent (mage.game.permanent.Permanent)4 TargetPlayer (mage.target.TargetPlayer)4 ContinuousEffect (mage.abilities.effects.ContinuousEffect)2 Target (mage.target.Target)2 FixedTarget (mage.target.targetpointer.FixedTarget)2 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 Ability (mage.abilities.Ability)1 EntersBattlefieldTriggeredAbility (mage.abilities.common.EntersBattlefieldTriggeredAbility)1 AtTheBeginOfNextEndStepDelayedTriggeredAbility (mage.abilities.common.delayed.AtTheBeginOfNextEndStepDelayedTriggeredAbility)1 ConditionalInterveningIfTriggeredAbility (mage.abilities.decorator.ConditionalInterveningIfTriggeredAbility)1 GainAbilityControlledEffect (mage.abilities.effects.common.continuous.GainAbilityControlledEffect)1 GainAbilityControllerEffect (mage.abilities.effects.common.continuous.GainAbilityControllerEffect)1 GainControlTargetEffect (mage.abilities.effects.common.continuous.GainControlTargetEffect)1 FlyingAbility (mage.abilities.keyword.FlyingAbility)1 ProtectionAbility (mage.abilities.keyword.ProtectionAbility)1 FilterPermanent (mage.filter.FilterPermanent)1