Search in sources :

Example 6 with RestrictionEffect

use of mage.abilities.effects.RestrictionEffect in project mage by magefree.

the class Combat method checkBlockRestrictionsAfter.

/**
 * Checks the canBeBlockedCheckAfter RestrictionEffect Is the block still
 * valid after all block decisions are done
 *
 * @param player
 * @param controller
 * @param game
 * @return
 */
public boolean checkBlockRestrictionsAfter(Player player, Player controller, Game game) {
    // Restrictions applied to blocking creatures
    for (UUID blockingCreatureId : this.getBlockers()) {
        Permanent blockingCreature = game.getPermanent(blockingCreatureId);
        if (blockingCreature != null) {
            for (Map.Entry<RestrictionEffect, Set<Ability>> entry : game.getContinuousEffects().getApplicableRestrictionEffects(blockingCreature, game).entrySet()) {
                RestrictionEffect effect = entry.getKey();
                for (Ability ability : entry.getValue()) {
                    if (!effect.canBlockCheckAfter(ability, game, true)) {
                        if (controller.isHuman()) {
                            controller.resetPlayerPassedActions();
                            game.informPlayer(controller, blockingCreature.getLogName() + " can't block this way.");
                            return false;
                        } else {
                            // remove blocking creatures for AI
                            removeBlocker(blockingCreatureId, game);
                        }
                    }
                }
            }
        }
    }
    // Restrictions applied because of attacking creatures
    for (UUID attackingCreatureId : this.getAttackers()) {
        Permanent attackingCreature = game.getPermanent(attackingCreatureId);
        if (attackingCreature != null) {
            for (Map.Entry<RestrictionEffect, Set<Ability>> entry : game.getContinuousEffects().getApplicableRestrictionEffects(attackingCreature, game).entrySet()) {
                RestrictionEffect effect = entry.getKey();
                for (Ability ability : entry.getValue()) {
                    if (!effect.canBeBlockedCheckAfter(attackingCreature, ability, game, true)) {
                        if (controller.isHuman()) {
                            controller.resetPlayerPassedActions();
                            game.informPlayer(controller, attackingCreature.getLogName() + " can't be blocked this way.");
                            return false;
                        } else {
                            // remove blocking creatures for AI
                            for (CombatGroup combatGroup : this.getGroups()) {
                                if (combatGroup.getAttackers().contains(attackingCreatureId)) {
                                    for (UUID blockerId : combatGroup.getBlockers()) {
                                        removeBlocker(blockerId, game);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return true;
}
Also used : JohanVigilanceAbility(mage.abilities.keyword.special.JohanVigilanceAbility) VigilanceAbility(mage.abilities.keyword.VigilanceAbility) BandingAbility(mage.abilities.keyword.BandingAbility) BandsWithOtherAbility(mage.abilities.keyword.BandsWithOtherAbility) Ability(mage.abilities.Ability) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) RestrictionEffect(mage.abilities.effects.RestrictionEffect)

Example 7 with RestrictionEffect

use of mage.abilities.effects.RestrictionEffect in project mage by magefree.

the class Combat method checkAttackRequirements.

protected void checkAttackRequirements(Player player, Game game) {
    // 20101001 - 508.1d
    for (Permanent creature : player.getAvailableAttackers(game)) {
        boolean mustAttack = false;
        Set<UUID> defendersForcedToAttack = new HashSet<>();
        if (creature.getGoadingPlayers().isEmpty()) {
            // check if a creature has to attack
            for (Map.Entry<RequirementEffect, Set<Ability>> entry : game.getContinuousEffects().getApplicableRequirementEffects(creature, false, game).entrySet()) {
                RequirementEffect effect = entry.getKey();
                if (!effect.mustAttack(game)) {
                    continue;
                }
                mustAttack = true;
                for (Ability ability : entry.getValue()) {
                    UUID defenderId = effect.mustAttackDefender(ability, game);
                    if (defenderId != null && defenders.contains(defenderId)) {
                        defendersForcedToAttack.add(defenderId);
                    }
                    break;
                }
            }
        } else {
            // if creature is goaded then we start with assumption that it needs to attack any player
            mustAttack = true;
            defendersForcedToAttack.addAll(defenders);
        }
        if (!mustAttack) {
            continue;
        }
        // check which defenders the forced to attack creature can attack without paying a cost
        Set<UUID> defendersCostlessAttackable = new HashSet<>(defenders);
        for (UUID defenderId : defenders) {
            if (game.getContinuousEffects().checkIfThereArePayCostToAttackBlockEffects(new DeclareAttackerEvent(defenderId, creature.getId(), creature.getControllerId()), game)) {
                defendersCostlessAttackable.remove(defenderId);
                defendersForcedToAttack.remove(defenderId);
                continue;
            }
            for (Map.Entry<RestrictionEffect, Set<Ability>> entry : game.getContinuousEffects().getApplicableRestrictionEffects(creature, game).entrySet()) {
                if (entry.getValue().stream().anyMatch(ability -> entry.getKey().canAttack(creature, defenderId, ability, game, false))) {
                    continue;
                }
                defendersCostlessAttackable.remove(defenderId);
                defendersForcedToAttack.remove(defenderId);
                break;
            }
        }
        // then they attack one of those players, otherwise they attack any player
        if (!defendersForcedToAttack.stream().allMatch(creature.getGoadingPlayers()::contains)) {
            defendersForcedToAttack.removeAll(creature.getGoadingPlayers());
        }
        // force attack only if a defender can be attacked without paying a cost
        if (defendersCostlessAttackable.isEmpty()) {
            continue;
        }
        creaturesForcedToAttack.put(creature.getId(), defendersForcedToAttack);
        // No need to attack a special defender
        Set<UUID> defendersToChooseFrom = defendersForcedToAttack.isEmpty() ? defendersCostlessAttackable : defendersForcedToAttack;
        if (defendersToChooseFrom.size() == 1) {
            player.declareAttacker(creature.getId(), defendersToChooseFrom.iterator().next(), game, false);
            continue;
        }
        TargetDefender target = new TargetDefender(defendersToChooseFrom, creature.getId());
        target.setRequired(true);
        target.setTargetName("planeswalker or player for " + creature.getLogName() + " to attack (must attack effect)");
        if (player.chooseTarget(Outcome.Damage, target, null, game)) {
            player.declareAttacker(creature.getId(), target.getFirstTarget(), game, false);
        }
    }
}
Also used : JohanVigilanceAbility(mage.abilities.keyword.special.JohanVigilanceAbility) VigilanceAbility(mage.abilities.keyword.VigilanceAbility) BandingAbility(mage.abilities.keyword.BandingAbility) BandsWithOtherAbility(mage.abilities.keyword.BandsWithOtherAbility) Ability(mage.abilities.Ability) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) TargetDefender(mage.target.common.TargetDefender) RequirementEffect(mage.abilities.effects.RequirementEffect) RestrictionEffect(mage.abilities.effects.RestrictionEffect)

Example 8 with RestrictionEffect

use of mage.abilities.effects.RestrictionEffect in project mage by magefree.

the class StandOrFallEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    // 802.2. As the combat phase starts, the attacking player doesn’t choose an opponent to become the defending player.
    // Instead, all the attacking player’s opponents are defending players during the combat phase.
    // 
    // 802.2a Any rule, object, or effect that refers to a “defending player” refers to one specific defending player, not to all of the defending players.
    // If an ability of an attacking creature refers to a defending player, or a spell or ability refers to both an attacking creature and a defending player,
    // then unless otherwise specified, the defending player it’s referring to is the player that creature was attacking at the time it became an attacking
    // creature that combat, or the controller of the planeswalker that creature was attacking at the time it became an attacking creature that combat. If a spell or ability
    // could apply to multiple attacking creatures, the appropriate defending player is individually determined for each of those attacking creatures.
    // If there are multiple defending players that could be chosen, the controller of the spell or ability chooses one.
    // 
    // https://www.mtgsalvation.com/forums/magic-fundamentals/magic-rulings/756140-stand-or-fall-mechanics
    Player player = game.getPlayer(source.getControllerId());
    Set<UUID> opponents = game.getOpponents(source.getControllerId());
    if (!opponents.isEmpty()) {
        Player targetPlayer = game.getPlayer(opponents.iterator().next());
        if (opponents.size() > 1) {
            TargetOpponent targetOpponent = new TargetOpponent(true);
            if (player != null && player.chooseTarget(Outcome.Neutral, targetOpponent, source, game)) {
                targetPlayer = game.getPlayer(targetOpponent.getFirstTarget());
                game.informPlayers(player.getLogName() + " chose " + targetPlayer.getLogName() + " as the defending player");
            }
        }
        if (player != null && targetPlayer != null) {
            int count = game.getBattlefield().countAll(StaticFilters.FILTER_PERMANENT_CREATURES, targetPlayer.getId(), game);
            TargetCreaturePermanent creatures = new TargetCreaturePermanent(0, count, new FilterCreaturePermanent("creatures to put in the first pile"), true);
            List<Permanent> pile1 = new ArrayList<>();
            creatures.setRequired(false);
            if (player.choose(Outcome.Neutral, creatures, source.getSourceId(), game)) {
                List<UUID> targets = creatures.getTargets();
                for (UUID targetId : targets) {
                    Permanent p = game.getPermanent(targetId);
                    if (p != null) {
                        pile1.add(p);
                    }
                }
            }
            List<Permanent> pile2 = new ArrayList<>();
            for (Permanent p : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, targetPlayer.getId(), game)) {
                if (!pile1.contains(p)) {
                    pile2.add(p);
                }
            }
            boolean choice = targetPlayer.choosePile(outcome, "Choose which pile can block this turn.", pile1, pile2, game);
            List<Permanent> chosenPile = choice ? pile2 : pile1;
            List<Permanent> otherPile = choice ? pile1 : pile2;
            for (Permanent permanent : chosenPile) {
                if (permanent != null) {
                    RestrictionEffect effect = new CantBlockTargetEffect(Duration.EndOfTurn);
                    effect.setText("");
                    effect.setTargetPointer(new FixedTarget(permanent, game));
                    game.addEffect(effect, source);
                }
            }
            game.informPlayers("Creatures that can block this turn: " + otherPile.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
            return true;
        }
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) TargetOpponent(mage.target.common.TargetOpponent) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) CantBlockTargetEffect(mage.abilities.effects.common.combat.CantBlockTargetEffect) ArrayList(java.util.ArrayList) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) UUID(java.util.UUID) RestrictionEffect(mage.abilities.effects.RestrictionEffect)

Example 9 with RestrictionEffect

use of mage.abilities.effects.RestrictionEffect in project mage by magefree.

the class EmberGaleEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player targetPlayer = game.getPlayer(source.getFirstTarget());
    if (targetPlayer != null) {
        FilterCreaturePermanent filter = new FilterCreaturePermanent();
        filter.add(new ControllerIdPredicate(targetPlayer.getId()));
        RestrictionEffect effect = new CantBlockAllEffect(filter, Duration.EndOfTurn);
        game.addEffect(effect, source);
        FilterPermanent filter2 = new FilterPermanent();
        filter2.add(new ControllerIdPredicate(targetPlayer.getId()));
        filter2.add(Predicates.or(new ColorPredicate(ObjectColor.WHITE), new ColorPredicate(ObjectColor.BLUE)));
        filter2.add(CardType.CREATURE.getPredicate());
        for (Permanent creature : game.getBattlefield().getAllActivePermanents(filter2, targetPlayer.getId(), game)) {
            creature.damage(1, source.getSourceId(), source, game, false, true);
        }
        return true;
    }
    return false;
}
Also used : ColorPredicate(mage.filter.predicate.mageobject.ColorPredicate) TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) FilterPermanent(mage.filter.FilterPermanent) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) CantBlockAllEffect(mage.abilities.effects.common.combat.CantBlockAllEffect) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) RestrictionEffect(mage.abilities.effects.RestrictionEffect)

Example 10 with RestrictionEffect

use of mage.abilities.effects.RestrictionEffect in project mage by magefree.

the class PeaceTalksPlayersAndPermanentsCantBeTargetsOfSpellsOrActivatedAbilities method apply.

@Override
public boolean apply(Game game, Ability source) {
    RestrictionEffect effect = new PeaceTalksCantAttackEffect();
    game.addEffect(effect, source);
    ContinuousRuleModifyingEffect effect2 = new PeaceTalksPlayersAndPermanentsCantBeTargetsOfSpellsOrActivatedAbilities();
    game.addEffect(effect2, source);
    return true;
}
Also used : ContinuousRuleModifyingEffect(mage.abilities.effects.ContinuousRuleModifyingEffect) RestrictionEffect(mage.abilities.effects.RestrictionEffect)

Aggregations

RestrictionEffect (mage.abilities.effects.RestrictionEffect)16 Permanent (mage.game.permanent.Permanent)11 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)8 Ability (mage.abilities.Ability)7 Player (mage.players.Player)7 MageObject (mage.MageObject)5 FixedTarget (mage.target.targetpointer.FixedTarget)5 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)4 ArrayList (java.util.ArrayList)3 UUID (java.util.UUID)3 RequirementEffect (mage.abilities.effects.RequirementEffect)3 BandingAbility (mage.abilities.keyword.BandingAbility)3 BandsWithOtherAbility (mage.abilities.keyword.BandsWithOtherAbility)3 FlyingAbility (mage.abilities.keyword.FlyingAbility)3 VigilanceAbility (mage.abilities.keyword.VigilanceAbility)3 JohanVigilanceAbility (mage.abilities.keyword.special.JohanVigilanceAbility)3 TargetControlledPermanent (mage.target.common.TargetControlledPermanent)3 SpellAbility (mage.abilities.SpellAbility)2 StaticAbility (mage.abilities.StaticAbility)2 TriggeredAbility (mage.abilities.TriggeredAbility)2