Search in sources :

Example 16 with CombatGroup

use of mage.game.combat.CombatGroup in project mage by magefree.

the class FlashFoliageEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Token token = new SaprolingToken();
        token.putOntoBattlefield(1, game, source, source.getControllerId());
        Permanent attackingCreature = game.getPermanent(getTargetPointer().getFirst(game, source));
        if (attackingCreature != null && game.getState().getCombat() != null) {
            // Possible ruling (see Aetherplasm)
            // The token you created is blocking the attacking creature,
            // even if the block couldn't legally be declared (for example, if that creature
            // enters the battlefield tapped, or it can't block, or the attacking creature
            // has protection from it)
            CombatGroup combatGroup = game.getState().getCombat().findGroup(attackingCreature.getId());
            if (combatGroup != null) {
                for (UUID tokenId : token.getLastAddedTokenIds()) {
                    Permanent saprolingToken = game.getPermanent(tokenId);
                    if (saprolingToken != null) {
                        combatGroup.addBlocker(tokenId, source.getControllerId(), game);
                        game.getCombat().addBlockingGroup(tokenId, attackingCreature.getId(), controller.getId(), game);
                    }
                }
                combatGroup.pickBlockerOrder(attackingCreature.getControllerId(), game);
            }
        }
        return true;
    }
    return false;
}
Also used : SaprolingToken(mage.game.permanent.token.SaprolingToken) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) SaprolingToken(mage.game.permanent.token.SaprolingToken) Token(mage.game.permanent.token.Token) UUID(java.util.UUID) CombatGroup(mage.game.combat.CombatGroup)

Example 17 with CombatGroup

use of mage.game.combat.CombatGroup in project mage by magefree.

the class GeneralJarkeldSwitchBlockersEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    List<UUID> targets = source.getTargets().get(0).getTargets();
    if (controller != null && targets != null) {
        Permanent attacker1 = game.getPermanent(targets.get(0));
        Permanent attacker2 = game.getPermanent(targets.get(1));
        if (attacker1 != null && attacker2 != null) {
            CombatGroup chosenGroup1 = game.getCombat().findGroup(attacker1.getId());
            CombatGroup chosenGroup2 = game.getCombat().findGroup(attacker2.getId());
            if (chosenGroup1 != null && chosenGroup2 != null) {
                Set<Permanent> blockers1 = new HashSet<>();
                Set<Permanent> blockers2 = new HashSet<>();
                Set<Permanent> multiBlockers = new HashSet<>();
                blockerSearch1: for (UUID blockerId : chosenGroup1.getBlockers()) {
                    Permanent blocker = game.getPermanent(blockerId);
                    if (blocker != null) {
                        if (game.getCombat().blockingGroupsContains(blocker.getId())) {
                            // if (blocker.getBlocking() > 1) {
                            for (CombatGroup group : game.getCombat().getBlockingGroups()) {
                                if (group.getBlockers().contains(blocker.getId())) {
                                    int attackerCount = group.getAttackers().size();
                                    if (attackerCount > 1) {
                                        multiBlockers.add(blocker);
                                    } else if (attackerCount == 1) {
                                        blockers1.add(blocker);
                                    }
                                    continue blockerSearch1;
                                }
                            }
                        } else {
                            blockers1.add(blocker);
                        }
                    }
                }
                blockerSearch2: for (UUID blockerId : chosenGroup2.getBlockers()) {
                    Permanent blocker = game.getPermanent(blockerId);
                    if (blocker != null) {
                        if (game.getCombat().blockingGroupsContains(blocker.getId())) {
                            // if (blocker.getBlocking() > 1) {
                            for (CombatGroup group : game.getCombat().getBlockingGroups()) {
                                if (group.getBlockers().contains(blocker.getId())) {
                                    int attackerCount = group.getAttackers().size();
                                    if (attackerCount > 1) {
                                        multiBlockers.add(blocker);
                                    } else if (attackerCount == 1) {
                                        blockers2.add(blocker);
                                    }
                                    continue blockerSearch2;
                                }
                            }
                        } else {
                            blockers2.add(blocker);
                        }
                    }
                }
                handleSingleBlockers(blockers1, chosenGroup1, chosenGroup2, controller, game);
                handleSingleBlockers(blockers2, chosenGroup2, chosenGroup1, controller, game);
                handleMultiBlockers(multiBlockers, chosenGroup1, chosenGroup2, controller, game);
                // the ability doesn't unblock a group that loses all blockers, however it will newly block a previously unblocked group if it gains a blocker this way
                if (!(chosenGroup1.getBlockers().isEmpty())) {
                    chosenGroup1.setBlocked(true, game);
                    chosenGroup1.pickBlockerOrder(attacker1.getControllerId(), game);
                }
                if (!(chosenGroup2.getBlockers().isEmpty())) {
                    chosenGroup2.setBlocked(true, game);
                    chosenGroup2.pickBlockerOrder(attacker2.getControllerId(), game);
                }
                return true;
            }
        }
    }
    return false;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) UUID(java.util.UUID) CombatGroup(mage.game.combat.CombatGroup) HashSet(java.util.HashSet)

Example 18 with CombatGroup

use of mage.game.combat.CombatGroup in project mage by magefree.

the class MirrorMatchEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        for (UUID attackerId : game.getCombat().getAttackers()) {
            Permanent attacker = game.getPermanent(attackerId);
            if (attacker != null && source.isControlledBy(game.getCombat().getDefendingPlayerId(attackerId, game))) {
                CreateTokenCopyTargetEffect effect = new CreateTokenCopyTargetEffect(source.getControllerId(), null, false);
                effect.setTargetPointer(new FixedTarget(attacker, game));
                effect.apply(game, source);
                CombatGroup group = game.getCombat().findGroup(attacker.getId());
                boolean isCreature = false;
                if (group != null) {
                    for (Permanent addedToken : effect.getAddedPermanents()) {
                        if (addedToken.isCreature(game)) {
                            group.addBlockerToGroup(addedToken.getId(), attackerId, game);
                            isCreature = true;
                        }
                    }
                    ExileTargetEffect exileEffect = new ExileTargetEffect("Exile those tokens at end of combat");
                    exileEffect.setTargetPointer(new FixedTargets(effect.getAddedPermanents(), game));
                    game.addDelayedTriggeredAbility(new AtTheEndOfCombatDelayedTriggeredAbility(exileEffect), source);
                    if (isCreature) {
                        group.pickBlockerOrder(attacker.getControllerId(), game);
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) FixedTargets(mage.target.targetpointer.FixedTargets) AtTheEndOfCombatDelayedTriggeredAbility(mage.abilities.common.delayed.AtTheEndOfCombatDelayedTriggeredAbility) UUID(java.util.UUID) CreateTokenCopyTargetEffect(mage.abilities.effects.common.CreateTokenCopyTargetEffect) CombatGroup(mage.game.combat.CombatGroup) ExileTargetEffect(mage.abilities.effects.common.ExileTargetEffect)

Example 19 with CombatGroup

use of mage.game.combat.CombatGroup in project mage by magefree.

the class PortalMageEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Permanent attackingCreature = game.getPermanent(getTargetPointer().getFirst(game, source));
        if (attackingCreature != null) {
            CombatGroup combatGroupTarget = null;
            for (CombatGroup combatGroup : game.getCombat().getGroups()) {
                if (combatGroup.getAttackers().contains(attackingCreature.getId())) {
                    combatGroupTarget = combatGroup;
                    break;
                }
            }
            if (combatGroupTarget == null) {
                return false;
            }
            // Reselecting which player or planeswalker a creature is attacking ignores all requirements,
            // restrictions, and costs associated with attacking.
            // Update possible defender
            Set<UUID> defenders = new LinkedHashSet<>();
            for (UUID playerId : game.getCombat().getAttackablePlayers(game)) {
                defenders.add(playerId);
                for (Permanent permanent : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_PLANESWALKER, playerId, game)) {
                    defenders.add(permanent.getId());
                }
            }
            // Select the new defender
            TargetDefender target = new TargetDefender(defenders, null);
            if (controller.chooseTarget(Outcome.Damage, target, source, game)) {
                if (!combatGroupTarget.getDefenderId().equals(target.getFirstTarget())) {
                    if (combatGroupTarget.changeDefenderPostDeclaration(target.getFirstTarget(), game)) {
                        String attacked = "";
                        Player player = game.getPlayer(target.getFirstTarget());
                        if (player != null) {
                            attacked = player.getLogName();
                        } else {
                            Permanent permanent = game.getPermanent(target.getFirstTarget());
                            if (permanent != null) {
                                attacked = permanent.getLogName();
                            }
                        }
                        game.informPlayers(attackingCreature.getLogName() + " now attacks " + attacked);
                        return true;
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) TargetDefender(mage.target.common.TargetDefender) UUID(java.util.UUID) CombatGroup(mage.game.combat.CombatGroup)

Example 20 with CombatGroup

use of mage.game.combat.CombatGroup in project mage by magefree.

the class YdwenEfreetEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent creature = game.getPermanent(source.getSourceId());
    if (controller != null && creature != null) {
        if (!controller.flipCoin(source, game, true)) {
            creature.removeFromCombat(game);
            creature.setMaxBlocks(0);
            // Make blocked creatures unblocked
            BlockedByOnlyOneCreatureThisCombatWatcher watcher = game.getState().getWatcher(BlockedByOnlyOneCreatureThisCombatWatcher.class);
            if (watcher != null) {
                Set<CombatGroup> combatGroups = watcher.getBlockedOnlyByCreature(creature.getId());
                if (combatGroups != null) {
                    for (CombatGroup combatGroup : combatGroups) {
                        if (combatGroup != null) {
                            combatGroup.setBlocked(false, game);
                        }
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) BlockedByOnlyOneCreatureThisCombatWatcher(mage.watchers.common.BlockedByOnlyOneCreatureThisCombatWatcher) CombatGroup(mage.game.combat.CombatGroup)

Aggregations

CombatGroup (mage.game.combat.CombatGroup)44 Permanent (mage.game.permanent.Permanent)39 UUID (java.util.UUID)25 Player (mage.players.Player)23 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)9 FixedTarget (mage.target.targetpointer.FixedTarget)9 TargetPermanent (mage.target.TargetPermanent)8 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)8 HashSet (java.util.HashSet)7 OneShotEffect (mage.abilities.effects.OneShotEffect)6 Game (mage.game.Game)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Combat (mage.game.combat.Combat)5 BlockerDeclaredEvent (mage.game.events.BlockerDeclaredEvent)5 Target (mage.target.Target)5 MageObjectReference (mage.MageObjectReference)4 Ability (mage.abilities.Ability)4 PermanentInListPredicate (mage.filter.predicate.permanent.PermanentInListPredicate)4 BlockedByOnlyOneCreatureThisCombatWatcher (mage.watchers.common.BlockedByOnlyOneCreatureThisCombatWatcher)4