Search in sources :

Example 6 with CombatGroup

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

the class BalduvianWarlordUnblockEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent permanent = game.getPermanent(source.getTargets().getFirstTarget());
    if (controller != null && permanent != null) {
        // Remove target creature from combat
        Effect effect = new RemoveFromCombatTargetEffect();
        effect.apply(game, source);
        // Make blocked creatures unblocked
        BlockedByOnlyOneCreatureThisCombatWatcher watcher = game.getState().getWatcher(BlockedByOnlyOneCreatureThisCombatWatcher.class);
        if (watcher != null) {
            Set<CombatGroup> combatGroups = watcher.getBlockedOnlyByCreature(permanent.getId());
            if (combatGroups != null) {
                for (CombatGroup combatGroup : combatGroups) {
                    if (combatGroup != null) {
                        combatGroup.setBlocked(false, game);
                    }
                }
            }
        }
        // Choose new creature to block
        if (permanent.isCreature(game)) {
            // according to the following mail response from MTG Rules Management about False Orders:
            // "if Player A attacks Players B and C, Player B's creatures cannot block creatures attacking Player C"
            // therefore we need to single out creatures attacking the target blocker's controller (disappointing, I know)
            List<Permanent> list = new ArrayList<>();
            for (CombatGroup combatGroup : game.getCombat().getGroups()) {
                if (combatGroup.getDefendingPlayerId().equals(permanent.getControllerId())) {
                    for (UUID attackingCreatureId : combatGroup.getAttackers()) {
                        Permanent targetsControllerAttacker = game.getPermanent(attackingCreatureId);
                        list.add(targetsControllerAttacker);
                    }
                }
            }
            Player targetsController = game.getPlayer(permanent.getControllerId());
            if (targetsController != null) {
                FilterAttackingCreature filter = new FilterAttackingCreature("creature attacking " + targetsController.getLogName());
                filter.add(new PermanentInListPredicate(list));
                TargetAttackingCreature target = new TargetAttackingCreature(1, 1, filter, true);
                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 true;
                }
                Permanent chosenPermanent = game.getPermanent(target.getFirstTarget());
                if (chosenPermanent != null && chosenPermanent.isCreature(game)) {
                    CombatGroup chosenGroup = game.getCombat().findGroup(chosenPermanent.getId());
                    if (chosenGroup != null) {
                        // Relevant ruling for Balduvian Warlord:
                        // 7/15/2006 	If an attacking creature has an ability that triggers “When this creature becomes blocked,”
                        // it triggers when a creature blocks it due to the Warlord's ability only if it was unblocked at that point.
                        boolean notYetBlocked = chosenGroup.getBlockers().isEmpty();
                        chosenGroup.addBlockerToGroup(permanent.getId(), controller.getId(), game);
                        // 702.21h
                        game.getCombat().addBlockingGroup(permanent.getId(), chosenPermanent.getId(), controller.getId(), game);
                        if (notYetBlocked) {
                            game.fireEvent(GameEvent.getEvent(GameEvent.EventType.CREATURE_BLOCKED, chosenPermanent.getId(), source, null));
                            Set<MageObjectReference> morSet = new HashSet<>();
                            morSet.add(new MageObjectReference(chosenPermanent, game));
                            for (UUID bandedId : chosenPermanent.getBandedCards()) {
                                CombatGroup bandedGroup = game.getCombat().findGroup(bandedId);
                                if (bandedGroup != null && chosenGroup.getBlockers().size() == 1) {
                                    morSet.add(new MageObjectReference(bandedId, game));
                                    game.fireEvent(GameEvent.getEvent(GameEvent.EventType.CREATURE_BLOCKED, bandedId, source, null));
                                }
                            }
                            String key = UUID.randomUUID().toString();
                            game.getState().setValue("becameBlocked_" + key, morSet);
                            game.fireEvent(GameEvent.getEvent(GameEvent.EventType.BATCH_BLOCK_NONCOMBAT, source.getSourceId(), source, source.getControllerId(), key, 0));
                        }
                        game.fireEvent(new BlockerDeclaredEvent(chosenPermanent.getId(), permanent.getId(), permanent.getControllerId()));
                    }
                    // a new blockingGroup is formed, so it's necessary to find it again
                    CombatGroup blockGroup = findBlockingGroup(permanent, game);
                    if (blockGroup != null) {
                        blockGroup.pickAttackerOrder(permanent.getControllerId(), game);
                    }
                }
            }
            return true;
        }
    }
    return false;
}
Also used : Player(mage.players.Player) PermanentInListPredicate(mage.filter.predicate.permanent.PermanentInListPredicate) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) FilterAttackingCreature(mage.filter.common.FilterAttackingCreature) RemoveFromCombatTargetEffect(mage.abilities.effects.common.RemoveFromCombatTargetEffect) TargetAttackingCreature(mage.target.common.TargetAttackingCreature) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) RemoveFromCombatTargetEffect(mage.abilities.effects.common.RemoveFromCombatTargetEffect) BlockedByOnlyOneCreatureThisCombatWatcher(mage.watchers.common.BlockedByOnlyOneCreatureThisCombatWatcher) BlockerDeclaredEvent(mage.game.events.BlockerDeclaredEvent) CombatGroup(mage.game.combat.CombatGroup) MageObjectReference(mage.MageObjectReference)

Example 7 with CombatGroup

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

the class BrimazKingOfOreskosEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Token token = new CatSoldierCreatureToken();
        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 catToken = game.getPermanent(tokenId);
                    if (catToken != 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 : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) Token(mage.game.permanent.token.Token) CatSoldierCreatureToken(mage.game.permanent.token.CatSoldierCreatureToken) UUID(java.util.UUID) CombatGroup(mage.game.combat.CombatGroup) CatSoldierCreatureToken(mage.game.permanent.token.CatSoldierCreatureToken)

Example 8 with CombatGroup

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

the class FalseOrdersUnblockEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent permanent = game.getPermanent(source.getTargets().getFirstTarget());
    if (controller == null || permanent == null) {
        return false;
    }
    // Remove target creature from combat
    Effect effect = new RemoveFromCombatTargetEffect();
    effect.apply(game, source);
    // Make blocked creatures unblocked
    BlockedByOnlyOneCreatureThisCombatWatcher watcher = game.getState().getWatcher(BlockedByOnlyOneCreatureThisCombatWatcher.class);
    if (watcher != null) {
        Set<CombatGroup> combatGroups = watcher.getBlockedOnlyByCreature(permanent.getId());
        if (combatGroups != null) {
            for (CombatGroup combatGroup : combatGroups) {
                if (combatGroup != null) {
                    combatGroup.setBlocked(false, game);
                }
            }
        }
    }
    if (!permanent.isCreature(game) || !controller.chooseUse(Outcome.Benefit, "Have " + permanent.getLogName() + " block an attacking creature?", source, game)) {
        return false;
    }
    // Choose new creature to block
    // according to the following mail response from MTG Rules Management about False Orders:
    // "if Player A attacks Players B and C, Player B's creatures cannot block creatures attacking Player C"
    // therefore we need to single out creatures attacking the target blocker's controller (disappointing, I know)
    List<Permanent> list = new ArrayList<>();
    for (CombatGroup combatGroup : game.getCombat().getGroups()) {
        if (combatGroup.getDefendingPlayerId().equals(permanent.getControllerId())) {
            for (UUID attackingCreatureId : combatGroup.getAttackers()) {
                Permanent targetsControllerAttacker = game.getPermanent(attackingCreatureId);
                list.add(targetsControllerAttacker);
            }
        }
    }
    Player targetsController = game.getPlayer(permanent.getControllerId());
    if (targetsController == null) {
        return false;
    }
    FilterAttackingCreature filter = new FilterAttackingCreature("creature attacking " + targetsController.getLogName());
    filter.add(new PermanentInListPredicate(list));
    TargetAttackingCreature target = new TargetAttackingCreature(1, 1, filter, true);
    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 true;
    }
    Permanent chosenPermanent = game.getPermanent(target.getFirstTarget());
    if (chosenPermanent == null || !chosenPermanent.isCreature(game)) {
        return false;
    }
    CombatGroup chosenGroup = game.getCombat().findGroup(chosenPermanent.getId());
    if (chosenGroup != null) {
        // Relevant ruling for Balduvian Warlord:
        // 7/15/2006 	If an attacking creature has an ability that triggers “When this creature becomes blocked,”
        // it triggers when a creature blocks it due to the Warlord's ability only if it was unblocked at that point.
        boolean notYetBlocked = chosenGroup.getBlockers().isEmpty();
        chosenGroup.addBlockerToGroup(permanent.getId(), controller.getId(), game);
        // 702.21h
        game.getCombat().addBlockingGroup(permanent.getId(), chosenPermanent.getId(), controller.getId(), game);
        if (notYetBlocked) {
            game.fireEvent(GameEvent.getEvent(GameEvent.EventType.CREATURE_BLOCKED, chosenPermanent.getId(), source, null));
            Set<MageObjectReference> morSet = new HashSet<>();
            morSet.add(new MageObjectReference(chosenPermanent, game));
            for (UUID bandedId : chosenPermanent.getBandedCards()) {
                CombatGroup bandedGroup = game.getCombat().findGroup(bandedId);
                if (bandedGroup != null && chosenGroup.getBlockers().size() == 1) {
                    morSet.add(new MageObjectReference(bandedId, game));
                    game.fireEvent(GameEvent.getEvent(GameEvent.EventType.CREATURE_BLOCKED, bandedId, source, null));
                }
            }
            String key = UUID.randomUUID().toString();
            game.getState().setValue("becameBlocked_" + key, morSet);
            game.fireEvent(GameEvent.getEvent(GameEvent.EventType.BATCH_BLOCK_NONCOMBAT, source.getSourceId(), source, source.getControllerId(), key, 0));
        }
        game.fireEvent(new BlockerDeclaredEvent(chosenPermanent.getId(), permanent.getId(), permanent.getControllerId()));
    }
    // a new blockingGroup is formed, so it's necessary to find it again
    CombatGroup blockGroup = findBlockingGroup(permanent, game);
    if (blockGroup != null) {
        blockGroup.pickAttackerOrder(permanent.getControllerId(), game);
    }
    return true;
}
Also used : Player(mage.players.Player) ObjectSourcePlayer(mage.filter.predicate.ObjectSourcePlayer) PermanentInListPredicate(mage.filter.predicate.permanent.PermanentInListPredicate) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) FilterAttackingCreature(mage.filter.common.FilterAttackingCreature) RemoveFromCombatTargetEffect(mage.abilities.effects.common.RemoveFromCombatTargetEffect) TargetAttackingCreature(mage.target.common.TargetAttackingCreature) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) RemoveFromCombatTargetEffect(mage.abilities.effects.common.RemoveFromCombatTargetEffect) BlockedByOnlyOneCreatureThisCombatWatcher(mage.watchers.common.BlockedByOnlyOneCreatureThisCombatWatcher) BlockerDeclaredEvent(mage.game.events.BlockerDeclaredEvent) CombatGroup(mage.game.combat.CombatGroup) MageObjectReference(mage.MageObjectReference)

Example 9 with CombatGroup

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

the class RideDownEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Permanent blockingCreature = game.getPermanent(getTargetPointer().getFirst(game, source));
        if (blockingCreature != null) {
            for (CombatGroup combatGroup : game.getCombat().getGroups()) {
                if (combatGroup.getBlockers().contains(blockingCreature.getId())) {
                    for (UUID attackerId : combatGroup.getAttackers()) {
                        ContinuousEffect effect = new GainAbilityTargetEffect(TrampleAbility.getInstance(), Duration.EndOfTurn);
                        effect.setTargetPointer(new FixedTarget(attackerId, game));
                        game.addEffect(effect, source);
                    }
                    break;
                }
            }
            blockingCreature.destroy(source, game, false);
        }
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) GainAbilityTargetEffect(mage.abilities.effects.common.continuous.GainAbilityTargetEffect) ContinuousEffect(mage.abilities.effects.ContinuousEffect) UUID(java.util.UUID) CombatGroup(mage.game.combat.CombatGroup)

Example 10 with CombatGroup

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

the class DefendersForestCount method calculate.

@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
    for (CombatGroup group : game.getCombat().getGroups()) {
        if (group.getAttackers().contains(sourceAbility.getSourceId())) {
            UUID defenderId = group.getDefenderId();
            if (group.isDefenderIsPlaneswalker()) {
                Permanent permanent = game.getPermanent(defenderId);
                if (permanent != null) {
                    defenderId = permanent.getControllerId();
                }
            }
            FilterLandPermanent filter = new FilterLandPermanent("forest");
            filter.add(SubType.FOREST.getPredicate());
            return game.getBattlefield().countAll(filter, defenderId, game);
        }
    }
    return 0;
}
Also used : FilterLandPermanent(mage.filter.common.FilterLandPermanent) FilterLandPermanent(mage.filter.common.FilterLandPermanent) TargetLandPermanent(mage.target.common.TargetLandPermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) UUID(java.util.UUID) 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