Search in sources :

Example 11 with RequirementEffect

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

the class CrashingBoarsEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player defendingPlayer = game.getPlayer(this.getTargetPointer().getFirst(game, source));
    if (defendingPlayer != null) {
        Target target = new TargetControlledCreaturePermanent(1, 1, filter, true);
        if (target.choose(Outcome.Neutral, defendingPlayer.getId(), source.getSourceId(), game)) {
            RequirementEffect effect = new MustBeBlockedByTargetSourceEffect();
            effect.setTargetPointer(new FixedTarget(target.getFirstTarget(), game));
            game.addEffect(effect, source);
        }
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) RequirementEffect(mage.abilities.effects.RequirementEffect) Player(mage.players.Player) Target(mage.target.Target) FixedTarget(mage.target.targetpointer.FixedTarget) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) MustBeBlockedByTargetSourceEffect(mage.abilities.effects.common.combat.MustBeBlockedByTargetSourceEffect)

Example 12 with RequirementEffect

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

the class HumanPlayer method checkIfAttackersValid.

private boolean checkIfAttackersValid(Game game) {
    if (!game.getCombat().getCreaturesForcedToAttack().isEmpty()) {
        if (!game.getCombat().getAttackers().containsAll(game.getCombat().getCreaturesForcedToAttack().keySet())) {
            int forcedAttackers = 0;
            StringBuilder sb = new StringBuilder();
            for (UUID creatureId : game.getCombat().getCreaturesForcedToAttack().keySet()) {
                boolean validForcedAttacker = false;
                if (game.getCombat().getAttackers().contains(creatureId)) {
                    Set<UUID> possibleDefender = game.getCombat().getCreaturesForcedToAttack().get(creatureId);
                    if (possibleDefender.isEmpty() || possibleDefender.contains(game.getCombat().getDefenderId(creatureId))) {
                        validForcedAttacker = true;
                    }
                }
                if (validForcedAttacker) {
                    forcedAttackers++;
                } else {
                    Permanent creature = game.getPermanent(creatureId);
                    if (creature != null) {
                        sb.append(creature.getIdName()).append(' ');
                    }
                }
            }
            if (game.getCombat().getMaxAttackers() > forcedAttackers) {
                int requireToAttack = Math.min(game.getCombat().getMaxAttackers() - forcedAttackers, game.getCombat().getCreaturesForcedToAttack().size() - forcedAttackers);
                String message = (requireToAttack == 1 ? " more attacker that is " : " more attackers that are ") + "forced to attack.\nCreature" + (requireToAttack == 1 ? "" : "s") + " forced to attack: ";
                game.informPlayer(this, sb.insert(0, message).insert(0, requireToAttack).insert(0, "You have to attack with ").toString());
                return false;
            }
        }
    }
    // check if enough attackers are declared
    // check if players have to be attacked
    Set<UUID> playersToAttackIfAble = new HashSet<>();
    for (Map.Entry<RequirementEffect, Set<Ability>> entry : game.getContinuousEffects().getApplicableRequirementEffects(null, true, game).entrySet()) {
        RequirementEffect effect = entry.getKey();
        for (Ability ability : entry.getValue()) {
            UUID playerToAttack = effect.playerMustBeAttackedIfAble(ability, game);
            if (playerToAttack != null) {
                playersToAttackIfAble.add(playerToAttack);
            }
        }
    }
    if (!playersToAttackIfAble.isEmpty()) {
        Set<UUID> checkPlayersToAttackIfAble = new HashSet<>(playersToAttackIfAble);
        for (CombatGroup combatGroup : game.getCombat().getGroups()) {
            checkPlayersToAttackIfAble.remove(combatGroup.getDefendingPlayerId());
        }
        for (UUID forcedToAttackId : checkPlayersToAttackIfAble) {
            Player forcedToAttack = game.getPlayer(forcedToAttackId);
            for (Permanent attacker : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, getId(), game)) {
                if (game.getContinuousEffects().checkIfThereArePayCostToAttackBlockEffects(new DeclareAttackerEvent(forcedToAttackId, attacker.getId(), attacker.getControllerId()), game)) {
                    continue;
                }
                // if attacker needs a specific defender to attack so select that one instead
                if (game.getCombat().getCreaturesForcedToAttack().containsKey(attacker.getId())) {
                    Set<UUID> possibleDefenders = game.getCombat().getCreaturesForcedToAttack().get(attacker.getId());
                    if (!possibleDefenders.isEmpty() && !possibleDefenders.contains(forcedToAttackId)) {
                        continue;
                    }
                }
                UUID defendingPlayerId = game.getCombat().getDefendingPlayerId(attacker.getId(), game);
                if (playersToAttackIfAble.contains(defendingPlayerId)) {
                    // already attacks other player taht has to be attacked
                    continue;
                }
                if (defendingPlayerId != null || attacker.canAttackInPrinciple(forcedToAttackId, game)) {
                    game.informPlayer(this, "You are forced to attack " + forcedToAttack.getName() + " or a controlled planeswalker e.g. with " + attacker.getIdName() + ".");
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : ManaAbility(mage.abilities.mana.ManaAbility) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) DeclareAttackerEvent(mage.game.events.DeclareAttackerEvent) RequirementEffect(mage.abilities.effects.RequirementEffect) CombatGroup(mage.game.combat.CombatGroup)

Aggregations

RequirementEffect (mage.abilities.effects.RequirementEffect)12 Player (mage.players.Player)10 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)8 Permanent (mage.game.permanent.Permanent)5 Ability (mage.abilities.Ability)4 AttacksIfAbleAllEffect (mage.abilities.effects.common.combat.AttacksIfAbleAllEffect)4 RestrictionEffect (mage.abilities.effects.RestrictionEffect)3 BandingAbility (mage.abilities.keyword.BandingAbility)3 BandsWithOtherAbility (mage.abilities.keyword.BandsWithOtherAbility)3 VigilanceAbility (mage.abilities.keyword.VigilanceAbility)3 JohanVigilanceAbility (mage.abilities.keyword.special.JohanVigilanceAbility)3 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)3 ControllerIdPredicate (mage.filter.predicate.permanent.ControllerIdPredicate)3 TargetPlayer (mage.target.TargetPlayer)3 TargetControlledPermanent (mage.target.common.TargetControlledPermanent)3 MageObject (mage.MageObject)2 Target (mage.target.Target)2 FixedTarget (mage.target.targetpointer.FixedTarget)2 Map (java.util.Map)1 Set (java.util.Set)1