Search in sources :

Example 1 with BlockedAttackerWatcher

use of mage.watchers.common.BlockedAttackerWatcher in project mage by magefree.

the class GlyphOfReincarnationEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent targetWall = getTargetPointer().getFirstTargetPermanentOrLKI(game, source);
    if (controller != null && targetWall != null) {
        BlockedAttackerWatcher watcher = game.getState().getWatcher(BlockedAttackerWatcher.class);
        if (watcher != null) {
            Map<UUID, Player> destroyed = new HashMap<>();
            for (Permanent creature : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, source.getControllerId(), source.getSourceId(), game)) {
                if (!creature.getId().equals(targetWall.getId())) {
                    if (watcher.creatureHasBlockedAttacker(new MageObjectReference(creature, game), new MageObjectReference(targetWall, game), game)) {
                        if (creature.destroy(source, game, true) && game.getState().getZone(creature.getId()) == Zone.GRAVEYARD) {
                            // If a commander is replaced to command zone, the creature does not die
                            Player permController = game.getPlayer(creature.getControllerId());
                            if (permController != null) {
                                destroyed.put(creature.getId(), permController);
                            }
                        }
                    }
                }
            }
            // onto the battlefield under its owner’s control
            for (Map.Entry<UUID, Player> entry : destroyed.entrySet()) {
                Permanent permanent = (Permanent) game.getLastKnownInformation(entry.getKey(), Zone.BATTLEFIELD);
                Player player = entry.getValue();
                if (permanent != null && player != null) {
                    FilterCreatureCard filter = new FilterCreatureCard("a creature card from " + player.getName() + "'s graveyard");
                    filter.add(new OwnerIdPredicate(player.getId()));
                    Target targetCreature = new TargetCardInGraveyard(filter);
                    targetCreature.setNotTarget(true);
                    if (targetCreature.canChoose(source.getSourceId(), controller.getId(), game) && controller.chooseTarget(outcome, targetCreature, source, game)) {
                        Card card = game.getCard(targetCreature.getFirstTarget());
                        if (card != null && game.getState().getZone(card.getId()) == Zone.GRAVEYARD) {
                            controller.moveCards(card, Zone.BATTLEFIELD, source, game, false, false, true, null);
                        }
                    }
                }
            }
            return true;
        }
    }
    return false;
}
Also used : Player(mage.players.Player) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) HashMap(java.util.HashMap) FilterCreatureCard(mage.filter.common.FilterCreatureCard) Card(mage.cards.Card) FilterCreatureCard(mage.filter.common.FilterCreatureCard) OwnerIdPredicate(mage.filter.predicate.card.OwnerIdPredicate) Target(mage.target.Target) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) BlockedAttackerWatcher(mage.watchers.common.BlockedAttackerWatcher) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) MageObjectReference(mage.MageObjectReference)

Example 2 with BlockedAttackerWatcher

use of mage.watchers.common.BlockedAttackerWatcher in project mage by magefree.

the class JovensFerretsEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent sourcePermanent = game.getPermanentOrLKIBattlefield(source.getSourceId());
    if (controller != null && sourcePermanent != null) {
        BlockedAttackerWatcher watcher = game.getState().getWatcher(BlockedAttackerWatcher.class);
        if (watcher != null) {
            List<Permanent> toTap = new ArrayList<>();
            for (Permanent creature : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, source.getControllerId(), source.getSourceId(), game)) {
                if (!creature.getId().equals(source.getSourceId())) {
                    if (watcher.creatureHasBlockedAttacker(sourcePermanent, creature, game)) {
                        toTap.add(creature);
                    }
                }
            }
            for (Permanent creature : toTap) {
                creature.tap(source, game);
                DontUntapInControllersNextUntapStepTargetEffect effect = new DontUntapInControllersNextUntapStepTargetEffect();
                effect.setTargetPointer(new FixedTarget(creature.getId(), game));
                game.addEffect(effect, source);
            }
            return true;
        }
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) DontUntapInControllersNextUntapStepTargetEffect(mage.abilities.effects.common.DontUntapInControllersNextUntapStepTargetEffect) BlockedAttackerWatcher(mage.watchers.common.BlockedAttackerWatcher) ArrayList(java.util.ArrayList)

Aggregations

Permanent (mage.game.permanent.Permanent)2 Player (mage.players.Player)2 BlockedAttackerWatcher (mage.watchers.common.BlockedAttackerWatcher)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UUID (java.util.UUID)1 MageObjectReference (mage.MageObjectReference)1 DontUntapInControllersNextUntapStepTargetEffect (mage.abilities.effects.common.DontUntapInControllersNextUntapStepTargetEffect)1 Card (mage.cards.Card)1 FilterCreatureCard (mage.filter.common.FilterCreatureCard)1 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)1 OwnerIdPredicate (mage.filter.predicate.card.OwnerIdPredicate)1 Target (mage.target.Target)1 TargetCardInGraveyard (mage.target.common.TargetCardInGraveyard)1 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)1 FixedTarget (mage.target.targetpointer.FixedTarget)1