Search in sources :

Example 1 with FirstTargetPointer

use of mage.target.targetpointer.FirstTargetPointer in project mage by magefree.

the class ExileTargetEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Set<Card> toExile = new LinkedHashSet<>();
        if (multitargetHandling && targetPointer instanceof FirstTargetPointer && (source.getTargets().size() > 1 || (source.getTargets().size() > 0 && source.getTargets().get(0).getTargets().size() > 1))) {
            for (Target target : source.getTargets()) {
                for (UUID targetId : target.getTargets()) {
                    Permanent permanent = game.getPermanent(targetId);
                    if (permanent != null && permanent.isPhasedIn()) {
                        Zone currentZone = game.getState().getZone(permanent.getId());
                        if (currentZone != Zone.EXILED && (onlyFromZone == null || onlyFromZone == Zone.BATTLEFIELD)) {
                            toExile.add(permanent);
                        }
                    } else {
                        Card card = game.getCard(targetId);
                        if (card != null) {
                            Zone currentZone = game.getState().getZone(card.getId());
                            if (currentZone != Zone.EXILED && (onlyFromZone == null || onlyFromZone == currentZone)) {
                                toExile.add(card);
                            }
                        } else {
                            StackObject stackObject = game.getStack().getStackObject(targetId);
                            if (stackObject instanceof Spell) {
                                toExile.add((Spell) stackObject);
                            }
                        }
                    }
                }
            }
        } else {
            for (UUID targetId : getTargetPointer().getTargets(game, source)) {
                Permanent permanent = game.getPermanent(targetId);
                if (permanent != null && permanent.isPhasedIn()) {
                    Zone currentZone = game.getState().getZone(permanent.getId());
                    if (currentZone != Zone.EXILED && (onlyFromZone == null || onlyFromZone == Zone.BATTLEFIELD)) {
                        toExile.add(permanent);
                    }
                } else {
                    Card card = game.getCard(targetId);
                    if (card != null) {
                        Zone currentZone = game.getState().getZone(card.getId());
                        if (currentZone != Zone.EXILED && (onlyFromZone == null || onlyFromZone == currentZone)) {
                            toExile.add(card);
                        }
                    } else {
                        StackObject stackObject = game.getStack().getStackObject(targetId);
                        if (stackObject instanceof Spell) {
                            toExile.add((Spell) stackObject);
                        }
                    }
                }
            }
        }
        if (toSourceExileZone) {
            MageObject sourceObject = source.getSourceObject(game);
            exileId = CardUtil.getExileZoneId(game, source.getSourceId(), source.getSourceObjectZoneChangeCounter());
            if (sourceObject != null) {
                exileZone = sourceObject.getIdName();
            }
        }
        controller.moveCardsToExile(toExile, source, game, withName, exileId, exileZone);
        return true;
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Player(mage.players.Player) Target(mage.target.Target) Permanent(mage.game.permanent.Permanent) Zone(mage.constants.Zone) StackObject(mage.game.stack.StackObject) MageObject(mage.MageObject) FirstTargetPointer(mage.target.targetpointer.FirstTargetPointer) UUID(java.util.UUID) Spell(mage.game.stack.Spell) Card(mage.cards.Card)

Example 2 with FirstTargetPointer

use of mage.target.targetpointer.FirstTargetPointer in project mage by magefree.

the class ExileTargetForSourceEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourceObject = source.getSourceObject(game);
    if (controller == null || sourceObject == null) {
        return false;
    }
    Set<UUID> objectsToMove = new LinkedHashSet<>();
    if (this.targetPointer instanceof FirstTargetPointer && source.getTargets().size() > 1) {
        for (Target target : source.getTargets()) {
            objectsToMove.addAll(target.getTargets());
        }
    } else {
        if (this.targetPointer != null && !this.targetPointer.getTargets(game, source).isEmpty()) {
            objectsToMove.addAll(this.targetPointer.getTargets(game, source));
        } else {
            // issue with Madness keyword  #6889
            UUID fixedTargetId = null;
            for (Effect effect : source.getEffects()) {
                TargetPointer targetPointerId = effect.getTargetPointer();
                if (targetPointerId instanceof FixedTarget) {
                    fixedTargetId = (((FixedTarget) targetPointerId).getTarget());
                }
            }
            if (fixedTargetId != null) {
                objectsToMove.add(fixedTargetId);
            }
        }
    }
    UUID exileId = CardUtil.getExileZoneId(game, source.getSourceId(), source.getSourceObjectZoneChangeCounter());
    // it can target permanents on battlefield, so use objects first
    Set<Card> cardsToMove = objectsToMove.stream().map(game::getObject).filter(Objects::nonNull).map(object -> {
        if (object instanceof Card) {
            return (Card) object;
        } else {
            return game.getCard(object.getId());
        }
    }).filter(Objects::nonNull).collect(Collectors.toSet());
    return controller.moveCardsToExile(cardsToMove, source, game, true, exileId, sourceObject.getIdName());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) MageObject(mage.MageObject) Card(mage.cards.Card) Target(mage.target.Target) FixedTarget(mage.target.targetpointer.FixedTarget) FirstTargetPointer(mage.target.targetpointer.FirstTargetPointer) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) UUID(java.util.UUID) TargetPointer(mage.target.targetpointer.TargetPointer) FirstTargetPointer(mage.target.targetpointer.FirstTargetPointer)

Example 3 with FirstTargetPointer

use of mage.target.targetpointer.FirstTargetPointer in project mage by magefree.

the class DestroyTargetEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    int affectedTargets = 0;
    if (multitargetHandling && source.getTargets().size() > 1 && targetPointer instanceof FirstTargetPointer) {
        // Decimate
        for (Target target : source.getTargets()) {
            for (UUID permanentId : target.getTargets()) {
                Permanent permanent = game.getPermanent(permanentId);
                if (permanent != null && permanent.isPhasedIn() && !permanent.isPhasedOutIndirectly()) {
                    permanent.destroy(source, game, noRegen);
                    affectedTargets++;
                }
            }
        }
    } else {
        for (UUID permanentId : targetPointer.getTargets(game, source)) {
            Permanent permanent = game.getPermanent(permanentId);
            if (permanent != null && permanent.isPhasedIn() && !permanent.isPhasedOutIndirectly()) {
                permanent.destroy(source, game, noRegen);
                affectedTargets++;
            }
        }
    }
    return affectedTargets > 0;
}
Also used : Target(mage.target.Target) Permanent(mage.game.permanent.Permanent) FirstTargetPointer(mage.target.targetpointer.FirstTargetPointer) UUID(java.util.UUID)

Example 4 with FirstTargetPointer

use of mage.target.targetpointer.FirstTargetPointer in project mage by magefree.

the class GodsendRuleModifyingEffect method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    Permanent equipment = game.getPermanentOrLKIBattlefield((this.getSourceId()));
    if (equipment != null && equipment.getAttachedTo() != null) {
        Permanent equippedPermanent = game.getPermanentOrLKIBattlefield((equipment.getAttachedTo()));
        if (equippedPermanent != null) {
            possibleTargets.clear();
            String targetName = "";
            if (equippedPermanent.isAttacking()) {
                for (CombatGroup group : game.getCombat().getGroups()) {
                    if (group.getAttackers().contains(equippedPermanent.getId())) {
                        possibleTargets.addAll(group.getBlockers());
                    }
                }
                targetName = "a creature blocking attacker ";
            } else if (equippedPermanent.getBlocking() > 0) {
                for (CombatGroup group : game.getCombat().getGroups()) {
                    if (group.getBlockers().contains(equippedPermanent.getId())) {
                        possibleTargets.addAll(group.getAttackers());
                    }
                }
                targetName = "a creature blocked by creature ";
            }
            if (!possibleTargets.isEmpty()) {
                this.getTargets().clear();
                if (possibleTargets.size() == 1) {
                    this.getEffects().get(0).setTargetPointer(new FixedTarget(possibleTargets.iterator().next(), game));
                } else {
                    this.getEffects().get(0).setTargetPointer(new FirstTargetPointer());
                    targetName = targetName + " equipped by " + equipment.getName();
                    FilterCreaturePermanent filter = new FilterCreaturePermanent(targetName);
                    List<PermanentIdPredicate> uuidPredicates = new ArrayList<>();
                    for (UUID creatureId : possibleTargets) {
                        uuidPredicates.add(new PermanentIdPredicate(creatureId));
                    }
                    filter.add(Predicates.or(uuidPredicates));
                    this.getTargets().add(new TargetCreaturePermanent(filter));
                }
                return true;
            }
        }
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) FirstTargetPointer(mage.target.targetpointer.FirstTargetPointer) CombatGroup(mage.game.combat.CombatGroup)

Aggregations

FirstTargetPointer (mage.target.targetpointer.FirstTargetPointer)4 UUID (java.util.UUID)3 Permanent (mage.game.permanent.Permanent)3 Target (mage.target.Target)3 LinkedHashSet (java.util.LinkedHashSet)2 MageObject (mage.MageObject)2 Card (mage.cards.Card)2 Player (mage.players.Player)2 FixedTarget (mage.target.targetpointer.FixedTarget)2 Effect (mage.abilities.effects.Effect)1 OneShotEffect (mage.abilities.effects.OneShotEffect)1 Zone (mage.constants.Zone)1 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)1 PermanentIdPredicate (mage.filter.predicate.permanent.PermanentIdPredicate)1 CombatGroup (mage.game.combat.CombatGroup)1 Spell (mage.game.stack.Spell)1 StackObject (mage.game.stack.StackObject)1 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)1 TargetPointer (mage.target.targetpointer.TargetPointer)1