Search in sources :

Example 11 with MageObjectReference

use of mage.MageObjectReference in project mage by magefree.

the class WardOfPietyPreventDamageTargetEffect method init.

@Override
public void init(Ability source, Game game) {
    super.init(source, game);
    redirectToObject = new MageObjectReference(source.getTargets().get(0).getFirstTarget(), game);
}
Also used : MageObjectReference(mage.MageObjectReference)

Example 12 with MageObjectReference

use of mage.MageObjectReference 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 13 with MageObjectReference

use of mage.MageObjectReference in project mage by magefree.

the class BackdraftHellkiteEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    if (player == null) {
        return false;
    }
    player.getGraveyard().stream().filter(cardId -> affectedObjectList.contains(new MageObjectReference(cardId, game))).forEachOrdered(cardId -> {
        Card card = game.getCard(cardId);
        if (card == null) {
            return;
        }
        FlashbackAbility ability = new FlashbackAbility(card, card.getManaCost());
        ability.setSourceId(cardId);
        ability.setControllerId(card.getOwnerId());
        game.getState().addOtherAbility(card, ability);
    });
    return true;
}
Also used : AttacksTriggeredAbility(mage.abilities.common.AttacksTriggeredAbility) MageObjectReference(mage.MageObjectReference) UUID(java.util.UUID) MageInt(mage.MageInt) Player(mage.players.Player) CardSetInfo(mage.cards.CardSetInfo) ContinuousEffectImpl(mage.abilities.effects.ContinuousEffectImpl) Game(mage.game.Game) CardImpl(mage.cards.CardImpl) FlashbackAbility(mage.abilities.keyword.FlashbackAbility) FlyingAbility(mage.abilities.keyword.FlyingAbility) Card(mage.cards.Card) mage.constants(mage.constants) Ability(mage.abilities.Ability) FlashbackAbility(mage.abilities.keyword.FlashbackAbility) Player(mage.players.Player) MageObjectReference(mage.MageObjectReference) Card(mage.cards.Card)

Example 14 with MageObjectReference

use of mage.MageObjectReference in project mage by magefree.

the class BrineHagEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent sourcePermanent = game.getPermanentOrLKIBattlefield(source.getSourceId());
    if (sourcePermanent != null) {
        List<Permanent> list = new ArrayList<>();
        for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                for (Permanent creature : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, playerId, game)) {
                    if (sourcePermanent.getDealtDamageByThisTurn().contains(new MageObjectReference(creature.getId(), game))) {
                        list.add(creature);
                    }
                }
            }
        }
        if (!list.isEmpty()) {
            FilterCreaturePermanent filter = new FilterCreaturePermanent();
            filter.add(new PermanentInListPredicate(list));
            game.addEffect(new SetPowerToughnessAllEffect(0, 2, Duration.Custom, filter, true), source);
        }
        return true;
    }
    return false;
}
Also used : SetPowerToughnessAllEffect(mage.abilities.effects.common.continuous.SetPowerToughnessAllEffect) Player(mage.players.Player) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) PermanentInListPredicate(mage.filter.predicate.permanent.PermanentInListPredicate) Permanent(mage.game.permanent.Permanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) ArrayList(java.util.ArrayList) UUID(java.util.UUID) MageObjectReference(mage.MageObjectReference)

Example 15 with MageObjectReference

use of mage.MageObjectReference in project mage by magefree.

the class CavernOfSoulsCantCounterEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    CavernOfSoulsWatcher watcher = game.getState().getWatcher(CavernOfSoulsWatcher.class, source.getSourceId());
    Spell spell = game.getStack().getSpell(event.getTargetId());
    return spell != null && watcher != null && watcher.spellCantBeCountered(new MageObjectReference(spell, game));
}
Also used : Spell(mage.game.stack.Spell) MageObjectReference(mage.MageObjectReference)

Aggregations

MageObjectReference (mage.MageObjectReference)250 Permanent (mage.game.permanent.Permanent)147 Player (mage.players.Player)76 UUID (java.util.UUID)47 Card (mage.cards.Card)45 Ability (mage.abilities.Ability)34 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)33 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)26 OneShotEffect (mage.abilities.effects.OneShotEffect)24 FilterPermanent (mage.filter.FilterPermanent)23 Game (mage.game.Game)22 Spell (mage.game.stack.Spell)21 TargetPermanent (mage.target.TargetPermanent)20 CardImpl (mage.cards.CardImpl)18 CardSetInfo (mage.cards.CardSetInfo)18 MageObject (mage.MageObject)17 Effect (mage.abilities.effects.Effect)16 HashSet (java.util.HashSet)15 mage.constants (mage.constants)14 GameEvent (mage.game.events.GameEvent)13