Search in sources :

Example 1 with ChoiceLeftOrRight

use of mage.choices.ChoiceLeftOrRight in project mage by magefree.

the class OrderOfSuccessionEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Map<UUID, UUID> playerCreature = new HashMap<>(2);
        Choice choice = new ChoiceLeftOrRight();
        if (!controller.choose(Outcome.Neutral, choice, game)) {
            return false;
        }
        boolean left = choice.getChoice().equals("Left");
        PlayerList playerList = game.getState().getPlayerList().copy();
        // set playerlist to controller
        while (!playerList.get().equals(source.getControllerId()) && controller.canRespond()) {
            playerList.getNext();
        }
        Player currentPlayer = game.getPlayer(playerList.get());
        Player nextPlayer;
        UUID firstNextPlayer = null;
        while (!getNextPlayerInDirection(left, playerList, game).equals(firstNextPlayer) && controller.canRespond()) {
            nextPlayer = game.getPlayer(playerList.get());
            if (nextPlayer == null) {
                return false;
            }
            // save first next player to check for iteration stop
            if (firstNextPlayer == null) {
                firstNextPlayer = nextPlayer.getId();
            }
            if (!nextPlayer.canRespond()) {
                continue;
            }
            // if player is in range they choose a creature to control
            if (currentPlayer != null && game.getState().getPlayersInRange(controller.getId(), game).contains(currentPlayer.getId())) {
                FilterCreaturePermanent filter = new FilterCreaturePermanent("creature controlled by " + nextPlayer.getLogName());
                filter.add(new ControllerIdPredicate(nextPlayer.getId()));
                Target target = new TargetCreaturePermanent(filter);
                target.setNotTarget(true);
                if (target.canChoose(source.getSourceId(), currentPlayer.getId(), game)) {
                    if (currentPlayer.chooseTarget(outcome, target, source, game)) {
                        playerCreature.put(currentPlayer.getId(), target.getFirstTarget());
                    }
                }
            }
            currentPlayer = nextPlayer;
        }
        // change control of targets
        for (Map.Entry<UUID, UUID> entry : playerCreature.entrySet()) {
            Player player = game.getPlayer(entry.getKey());
            if (player != null) {
                Permanent creature = game.getPermanent(entry.getValue());
                if (creature != null) {
                    ContinuousEffect effect = new GainControlTargetEffect(Duration.EndOfGame, player.getId());
                    effect.setTargetPointer(new FixedTarget(creature.getId(), game));
                    game.addEffect(effect, source);
                    game.informPlayers(new StringBuilder(player.getLogName()).append(" gains control of ").append(creature.getName()).toString());
                }
            }
        }
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) Choice(mage.choices.Choice) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) HashMap(java.util.HashMap) PlayerList(mage.players.PlayerList) GainControlTargetEffect(mage.abilities.effects.common.continuous.GainControlTargetEffect) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) Target(mage.target.Target) FixedTarget(mage.target.targetpointer.FixedTarget) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) ChoiceLeftOrRight(mage.choices.ChoiceLeftOrRight) ContinuousEffect(mage.abilities.effects.ContinuousEffect) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ChoiceLeftOrRight

use of mage.choices.ChoiceLeftOrRight in project mage by magefree.

the class AminatouUltimateEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Choice choice = new ChoiceLeftOrRight();
        if (!controller.choose(Outcome.Neutral, choice, game)) {
            return false;
        }
        boolean left = choice.getChoice().equals("Left");
        PlayerList playerList = game.getState().getPlayerList().copy();
        // set playerlist to controller
        while (!playerList.get().equals(source.getControllerId())) {
            playerList.getNext();
        }
        UUID currentPlayer = playerList.get();
        UUID nextPlayer;
        UUID firstNextPlayer = null;
        while (!getNextPlayerInDirection(left, playerList, game).equals(firstNextPlayer)) {
            nextPlayer = playerList.get();
            if (nextPlayer == null) {
                return false;
            }
            // skip players out of range
            if (!game.getState().getPlayersInRange(controller.getId(), game).contains(nextPlayer)) {
                continue;
            }
            // save first next player to check for iteration stop
            if (firstNextPlayer == null) {
                firstNextPlayer = nextPlayer;
            }
            FilterNonlandPermanent nextPlayerNonlandPermanentsFilter = new FilterNonlandPermanent();
            nextPlayerNonlandPermanentsFilter.add(new ControllerIdPredicate(nextPlayer));
            for (Permanent permanent : game.getBattlefield().getAllActivePermanents(nextPlayerNonlandPermanentsFilter, game)) {
                if (permanent.getId().equals(source.getSourceId())) {
                    continue;
                }
                ContinuousEffect effect = new GainControlTargetEffect(Duration.EndOfGame, currentPlayer);
                effect.setTargetPointer(new FixedTarget(permanent, game));
                game.addEffect(effect, source);
            }
            currentPlayer = nextPlayer;
        }
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) Choice(mage.choices.Choice) FilterNonlandPermanent(mage.filter.common.FilterNonlandPermanent) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) PlayerList(mage.players.PlayerList) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) ChoiceLeftOrRight(mage.choices.ChoiceLeftOrRight) ContinuousEffect(mage.abilities.effects.ContinuousEffect) UUID(java.util.UUID) FilterNonlandPermanent(mage.filter.common.FilterNonlandPermanent) GainControlTargetEffect(mage.abilities.effects.common.continuous.GainControlTargetEffect)

Aggregations

UUID (java.util.UUID)2 ContinuousEffect (mage.abilities.effects.ContinuousEffect)2 GainControlTargetEffect (mage.abilities.effects.common.continuous.GainControlTargetEffect)2 Choice (mage.choices.Choice)2 ChoiceLeftOrRight (mage.choices.ChoiceLeftOrRight)2 ControllerIdPredicate (mage.filter.predicate.permanent.ControllerIdPredicate)2 Permanent (mage.game.permanent.Permanent)2 Player (mage.players.Player)2 PlayerList (mage.players.PlayerList)2 FixedTarget (mage.target.targetpointer.FixedTarget)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 FilterPermanent (mage.filter.FilterPermanent)1 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)1 FilterNonlandPermanent (mage.filter.common.FilterNonlandPermanent)1 Target (mage.target.Target)1 TargetPermanent (mage.target.TargetPermanent)1 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)1