Search in sources :

Example 1 with PlayerIdPredicate

use of mage.filter.predicate.other.PlayerIdPredicate 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 PlayerIdPredicate

use of mage.filter.predicate.other.PlayerIdPredicate 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 PlayerIdPredicate

use of mage.filter.predicate.other.PlayerIdPredicate 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 PlayerIdPredicate

use of mage.filter.predicate.other.PlayerIdPredicate in project mage by magefree.

the class HansErikssonEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    if (player == null) {
        return false;
    }
    Card card = player.getLibrary().getFromTop(game);
    if (card == null) {
        return false;
    }
    player.revealCards(source, new CardsImpl(card), game);
    if (!card.isCreature(game)) {
        return player.moveCards(card, Zone.HAND, source, game);
    }
    player.moveCards(card, Zone.BATTLEFIELD, source, game, true, false, false, null);
    Permanent permanent = game.getPermanent(card.getId());
    if (permanent == null) {
        return true;
    }
    UUID defendingPlayerId = getTargetPointer().getFirst(game, source);
    UUID defenderId;
    if (game.getBattlefield().count(StaticFilters.FILTER_CONTROLLED_PERMANENT_PLANESWALKER, source.getSourceId(), defendingPlayerId, game) < 1) {
        defenderId = defendingPlayerId;
    } else {
        FilterPlayerOrPlaneswalker filter = new FilterPlayerOrPlaneswalker("defending player or a planeswalker they control");
        filter.getPlayerFilter().add(new PlayerIdPredicate(defendingPlayerId));
        filter.getPermanentFilter().add(new ControllerIdPredicate(defendingPlayerId));
        TargetPlayerOrPlaneswalker target = new TargetPlayerOrPlaneswalker(filter);
        target.setNotTarget(true);
        player.choose(outcome, target, source.getSourceId(), game);
        defenderId = target.getFirstTarget();
    }
    if (defenderId != null) {
        game.getCombat().addAttackerToCombat(permanent.getId(), defenderId, game);
    }
    Effect fightEffect = new FightTargetSourceEffect();
    fightEffect.setTargetPointer(new FixedTarget(permanent, game));
    ReflexiveTriggeredAbility ability = new ReflexiveTriggeredAbility(fightEffect, false, "When you put a creature card onto the battlefield this way, it fights {this}");
    game.fireReflexiveTriggeredAbility(ability, source);
    return true;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) FightTargetSourceEffect(mage.abilities.effects.common.FightTargetSourceEffect) FilterPlayerOrPlaneswalker(mage.filter.common.FilterPlayerOrPlaneswalker) Card(mage.cards.Card) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) PlayerIdPredicate(mage.filter.predicate.other.PlayerIdPredicate) ReflexiveTriggeredAbility(mage.abilities.common.delayed.ReflexiveTriggeredAbility) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) FightTargetSourceEffect(mage.abilities.effects.common.FightTargetSourceEffect) UUID(java.util.UUID) CardsImpl(mage.cards.CardsImpl) TargetPlayerOrPlaneswalker(mage.target.common.TargetPlayerOrPlaneswalker)

Example 5 with PlayerIdPredicate

use of mage.filter.predicate.other.PlayerIdPredicate in project mage by magefree.

the class ZaraRenegadeRecruiterEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Player player = game.getPlayer(targetPointer.getFirst(game, source));
    if (controller == null || player == null || player.getHand().isEmpty()) {
        return false;
    }
    TargetCardInHand targetCard = new TargetCardInHand(0, 1, StaticFilters.FILTER_CARD_CREATURE);
    controller.choose(outcome, player.getHand(), targetCard, game);
    Card card = game.getCard(targetCard.getFirstTarget());
    if (card == null) {
        return false;
    }
    controller.moveCards(card, Zone.BATTLEFIELD, source, game, true, false, false, null);
    Permanent permanent = game.getPermanent(card.getId());
    if (permanent == null) {
        return false;
    }
    UUID defenderId;
    if (game.getBattlefield().count(StaticFilters.FILTER_CONTROLLED_PERMANENT_PLANESWALKER, source.getSourceId(), player.getId(), game) < 1) {
        defenderId = player.getId();
    } else {
        FilterPlayerOrPlaneswalker filter = new FilterPlayerOrPlaneswalker(player.getName() + " or a planeswalker controlled by " + player.getName());
        filter.getPlayerFilter().add(new PlayerIdPredicate(player.getId()));
        filter.getPermanentFilter().add(new ControllerIdPredicate(player.getId()));
        TargetPlayerOrPlaneswalker target = new TargetPlayerOrPlaneswalker(filter);
        target.setNotTarget(true);
        controller.choose(outcome, target, source.getSourceId(), game);
        defenderId = target.getFirstTarget();
    }
    game.getCombat().addAttackerToCombat(permanent.getId(), defenderId, game);
    game.addDelayedTriggeredAbility(new AtTheBeginOfNextEndStepDelayedTriggeredAbility(new ReturnToHandTargetEffect().setTargetPointer(new FixedTarget(permanent, game))), source);
    return true;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) AtTheBeginOfNextEndStepDelayedTriggeredAbility(mage.abilities.common.delayed.AtTheBeginOfNextEndStepDelayedTriggeredAbility) Permanent(mage.game.permanent.Permanent) TargetCardInHand(mage.target.common.TargetCardInHand) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) PlayerIdPredicate(mage.filter.predicate.other.PlayerIdPredicate) ReturnToHandTargetEffect(mage.abilities.effects.common.ReturnToHandTargetEffect) UUID(java.util.UUID) FilterPlayerOrPlaneswalker(mage.filter.common.FilterPlayerOrPlaneswalker) Card(mage.cards.Card) TargetPlayerOrPlaneswalker(mage.target.common.TargetPlayerOrPlaneswalker)

Aggregations

PlayerIdPredicate (mage.filter.predicate.other.PlayerIdPredicate)8 Player (mage.players.Player)7 FilterPlayer (mage.filter.FilterPlayer)6 Permanent (mage.game.permanent.Permanent)6 TargetPlayer (mage.target.TargetPlayer)4 FixedTarget (mage.target.targetpointer.FixedTarget)4 UUID (java.util.UUID)3 AtTheBeginOfNextEndStepDelayedTriggeredAbility (mage.abilities.common.delayed.AtTheBeginOfNextEndStepDelayedTriggeredAbility)2 ContinuousEffect (mage.abilities.effects.ContinuousEffect)2 Card (mage.cards.Card)2 FilterPlayerOrPlaneswalker (mage.filter.common.FilterPlayerOrPlaneswalker)2 ControllerIdPredicate (mage.filter.predicate.permanent.ControllerIdPredicate)2 Target (mage.target.Target)2 TargetPlayerOrPlaneswalker (mage.target.common.TargetPlayerOrPlaneswalker)2 HashSet (java.util.HashSet)1 Ability (mage.abilities.Ability)1 EntersBattlefieldTriggeredAbility (mage.abilities.common.EntersBattlefieldTriggeredAbility)1 ReflexiveTriggeredAbility (mage.abilities.common.delayed.ReflexiveTriggeredAbility)1 ConditionalInterveningIfTriggeredAbility (mage.abilities.decorator.ConditionalInterveningIfTriggeredAbility)1 Effect (mage.abilities.effects.Effect)1