Search in sources :

Example 56 with Target

use of mage.target.Target in project mage by magefree.

the class RaidingPartyEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent sourcePermanent = game.getPermanentOrLKIBattlefield(source.getSourceId());
    if (sourcePermanent != null) {
        Set<UUID> plainsToSave = new HashSet<>();
        for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                int countBattlefield = game.getBattlefield().getAllActivePermanents(filter, game.getActivePlayerId(), game).size();
                int tappedCount = 0;
                Target untappedCreatureTarget = new TargetControlledCreaturePermanent(0, Integer.MAX_VALUE, filter, true);
                if (player.choose(Outcome.Benefit, untappedCreatureTarget, source.getSourceId(), game)) {
                    tappedCount = untappedCreatureTarget.getTargets().size();
                    for (UUID creatureId : untappedCreatureTarget.getTargets()) {
                        Permanent creature = game.getPermanentOrLKIBattlefield(creatureId);
                        if (creature != null) {
                            creature.tap(source, game);
                        }
                    }
                }
                if (tappedCount > 0) {
                    Target plainsToSaveTarget = new TargetPermanent(0, tappedCount * 2, filter2, true);
                    if (player.choose(Outcome.Benefit, plainsToSaveTarget, source.getSourceId(), game)) {
                        for (UUID plainsId : plainsToSaveTarget.getTargets()) {
                            plainsToSave.add(plainsId);
                            Permanent plains = game.getPermanent(plainsId);
                            if (plains != null) {
                                game.informPlayers(player.getLogName() + " chose " + plains.getLogName() + " to not be destroyed by " + sourcePermanent.getLogName());
                            }
                        }
                    }
                }
            }
        }
        for (Permanent plains : game.getBattlefield().getActivePermanents(filter2, source.getControllerId(), source.getSourceId(), game)) {
            if (!plainsToSave.contains(plains.getId())) {
                plains.destroy(source, game, false);
            }
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) FilterPermanent(mage.filter.FilterPermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) TargetPermanent(mage.target.TargetPermanent) TargetPermanent(mage.target.TargetPermanent) UUID(java.util.UUID) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) HashSet(java.util.HashSet)

Example 57 with Target

use of mage.target.Target in project mage by magefree.

the class RicochetEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Spell spell = game.getStack().getSpell(this.getTargetPointer().getFirst(game, source));
    if (spell != null) {
        Targets targets = new Targets();
        Ability sourceAbility = spell.getSpellAbility();
        for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
            Mode mode = sourceAbility.getModes().get(modeId);
            targets.addAll(mode.getTargets());
        }
        if (targets.size() != 1 || targets.get(0).getTargets().size() != 1) {
            return false;
        }
        Map<Player, Integer> playerRolls = new HashMap<>();
        for (UUID playerId : game.getPlayerList().copy()) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                playerRolls.put(player, 7);
            }
        }
        do {
            for (Player player : playerRolls.keySet()) {
                // bad outcome - ai must choose lowest value
                playerRolls.put(player, player.rollDice(Outcome.Detriment, source, game, 6));
            }
            int minValueInMap = Collections.min(playerRolls.values());
            for (Map.Entry<Player, Integer> mapEntry : new HashSet<>(playerRolls.entrySet())) {
                if (mapEntry.getValue() > minValueInMap) {
                    playerRolls.remove(mapEntry.getKey());
                }
            }
        } while (playerRolls.size() > 1);
        if (playerRolls.size() == 1) {
            Player loser = (Player) playerRolls.keySet().toArray()[0];
            UUID loserId = loser.getId();
            Target target = targets.get(0);
            if (target.getFirstTarget().equals(loserId)) {
                return true;
            }
            String oldTargetName = null;
            if (target.canTarget(spell.getControllerId(), loserId, sourceAbility, game)) {
                Player oldPlayer = game.getPlayer(targets.getFirstTarget());
                if (oldPlayer != null) {
                    oldTargetName = oldPlayer.getLogName();
                }
                target.clearChosen();
                target.addTarget(loserId, sourceAbility, game);
            }
            MageObject sourceObject = game.getObject(source.getSourceId());
            if (oldTargetName != null && sourceObject != null) {
                game.informPlayers(sourceObject.getLogName() + ": Changed target of " + spell.getLogName() + " from " + oldTargetName + " to " + loser.getLogName());
            }
        }
        return true;
    }
    return false;
}
Also used : SpellCastAllTriggeredAbility(mage.abilities.common.SpellCastAllTriggeredAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) ObjectSourcePlayer(mage.filter.predicate.ObjectSourcePlayer) HashMap(java.util.HashMap) Mode(mage.abilities.Mode) MageObject(mage.MageObject) Targets(mage.target.Targets) Spell(mage.game.stack.Spell) FilterSpell(mage.filter.FilterSpell) Target(mage.target.Target) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 58 with Target

use of mage.target.Target in project mage by magefree.

the class ShowOfDominanceEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        int highestPower = Integer.MIN_VALUE;
        Permanent selectedCreature = null;
        for (Permanent permanent : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, controller.getId(), game)) {
            if (highestPower < permanent.getPower().getValue()) {
                highestPower = permanent.getPower().getValue();
                selectedCreature = permanent;
            } else if (highestPower == permanent.getPower().getValue()) {
                highestPower = permanent.getPower().getValue();
                selectedCreature = null;
            }
        }
        if (highestPower != Integer.MIN_VALUE) {
            if (selectedCreature == null) {
                FilterPermanent filter = new FilterCreaturePermanent("creature with power " + highestPower);
                filter.add(new PowerPredicate(ComparisonType.EQUAL_TO, highestPower));
                Target target = new TargetPermanent(1, 1, filter, true);
                if (controller.chooseTarget(outcome, target, source, game)) {
                    selectedCreature = game.getPermanent(target.getFirstTarget());
                }
            }
            if (selectedCreature != null) {
                FixedTarget target = new FixedTarget(selectedCreature.getId(), game);
                Effect effect = new AddCountersTargetEffect(CounterType.P1P1.createInstance(4));
                effect.setTargetPointer(target);
                effect.apply(game, source);
                ContinuousEffect continuousEffect = new GainAbilityTargetEffect(TrampleAbility.getInstance(), Duration.EndOfTurn);
                continuousEffect.setTargetPointer(target);
                game.addEffect(continuousEffect, source);
                return true;
            }
        }
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) FilterPermanent(mage.filter.FilterPermanent) FilterPermanent(mage.filter.FilterPermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) PowerPredicate(mage.filter.predicate.mageobject.PowerPredicate) Target(mage.target.Target) FixedTarget(mage.target.targetpointer.FixedTarget) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) GainAbilityTargetEffect(mage.abilities.effects.common.continuous.GainAbilityTargetEffect) ContinuousEffect(mage.abilities.effects.ContinuousEffect) AddCountersTargetEffect(mage.abilities.effects.common.counter.AddCountersTargetEffect) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) GainAbilityTargetEffect(mage.abilities.effects.common.continuous.GainAbilityTargetEffect) TargetPermanent(mage.target.TargetPermanent) ContinuousEffect(mage.abilities.effects.ContinuousEffect) AddCountersTargetEffect(mage.abilities.effects.common.counter.AddCountersTargetEffect)

Example 59 with Target

use of mage.target.Target in project mage by magefree.

the class SoulfireEruptionCastEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller == null) {
        return false;
    }
    for (UUID targetId : source.getTargets().stream().map(Target::getTargets).flatMap(Collection::stream).collect(Collectors.toSet())) {
        Permanent permanent = game.getPermanent(targetId);
        Player player = game.getPlayer(targetId);
        if (permanent == null && player == null) {
            continue;
        }
        Card card = controller.getLibrary().getFromTop(game);
        if (card == null) {
            continue;
        }
        controller.moveCards(card, Zone.EXILED, source, game);
        game.addEffect(new SoulfireEruptionCastEffect().setTargetPointer(new FixedTarget(card, game)), source);
        if (card.getManaValue() < 1) {
            continue;
        }
        if (permanent != null) {
            permanent.damage(card.getManaValue(), source.getSourceId(), source, game);
        }
        if (player != null) {
            player.damage(card.getManaValue(), source.getSourceId(), source, game);
        }
    }
    return true;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Player(mage.players.Player) Target(mage.target.Target) TargetAnyTarget(mage.target.common.TargetAnyTarget) FixedTarget(mage.target.targetpointer.FixedTarget) Permanent(mage.game.permanent.Permanent) UUID(java.util.UUID) Card(mage.cards.Card)

Example 60 with Target

use of mage.target.Target in project mage by magefree.

the class VesuvanDoppelgangerCopyEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent permanent = game.getPermanent(source.getSourceId());
    if (permanent == null) {
        permanent = game.getPermanentEntering(source.getSourceId());
    }
    final Permanent sourcePermanent = permanent;
    if (controller != null && sourcePermanent != null) {
        Target target = new TargetPermanent(new FilterCreaturePermanent("target creature (you copy from)"));
        target.setRequired(true);
        if (source instanceof SimpleStaticAbility) {
            target = new TargetPermanent(new FilterCreaturePermanent("creature (you copy from)"));
            target.setRequired(false);
            target.setNotTarget(true);
        }
        if (target.canChoose(source.getSourceId(), source.getControllerId(), game)) {
            controller.choose(Outcome.Copy, target, source.getSourceId(), game);
            Permanent copyFromPermanent = game.getPermanent(target.getFirstTarget());
            if (copyFromPermanent != null) {
                game.copyPermanent(copyFromPermanent, sourcePermanent.getId(), source, new CopyApplier() {

                    @Override
                    public boolean apply(Game game, MageObject blueprint, Ability source, UUID copyToObjectId) {
                        blueprint.getColor().setColor(sourcePermanent.getColor(game));
                        blueprint.getAbilities().add(new BeginningOfUpkeepTriggeredAbility(Zone.BATTLEFIELD, new VesuvanDoppelgangerCopyEffect(), TargetController.YOU, true, false, rule2));
                        return true;
                    }
                });
                return true;
            }
        }
    }
    return false;
}
Also used : SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) BeginningOfUpkeepTriggeredAbility(mage.abilities.common.BeginningOfUpkeepTriggeredAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) TargetPermanent(mage.target.TargetPermanent) SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) MageObject(mage.MageObject) BeginningOfUpkeepTriggeredAbility(mage.abilities.common.BeginningOfUpkeepTriggeredAbility) Target(mage.target.Target) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Game(mage.game.Game) CopyApplier(mage.util.functions.CopyApplier) TargetPermanent(mage.target.TargetPermanent) UUID(java.util.UUID)

Aggregations

Target (mage.target.Target)385 Player (mage.players.Player)291 Permanent (mage.game.permanent.Permanent)223 UUID (java.util.UUID)155 Card (mage.cards.Card)86 TargetPermanent (mage.target.TargetPermanent)80 FilterCard (mage.filter.FilterCard)62 FilterPermanent (mage.filter.FilterPermanent)56 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)54 FixedTarget (mage.target.targetpointer.FixedTarget)49 TargetControlledCreaturePermanent (mage.target.common.TargetControlledCreaturePermanent)45 TargetControlledPermanent (mage.target.common.TargetControlledPermanent)45 MageObject (mage.MageObject)43 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)42 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)38 TargetOpponent (mage.target.common.TargetOpponent)35 CardsImpl (mage.cards.CardsImpl)32 ArrayList (java.util.ArrayList)31 ContinuousEffect (mage.abilities.effects.ContinuousEffect)31 FilterControlledPermanent (mage.filter.common.FilterControlledPermanent)30