Search in sources :

Example 1 with Targets

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

the class SeismicWaveEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Targets targets = source.getTargets();
    if (targets.size() < 2) {
        return false;
    }
    UUID firstTarget = targets.get(0).getFirstTarget();
    if (firstTarget != null) {
        Permanent targetPermanent = game.getPermanent(firstTarget);
        if (targetPermanent != null) {
            targetPermanent.damage(2, source, game);
        } else {
            Player targetPlayer = game.getPlayer(firstTarget);
            if (targetPlayer != null) {
                targetPlayer.damage(2, source, game);
            }
        }
    }
    Target targetOpponent = targets.get(1);
    UUID opponentId = targetOpponent.getFirstTarget();
    if (opponentId != null && targetOpponent.isLegal(source, game)) {
        // Needs this check in case opponent gets hexproof at instant speed
        for (Permanent permanent : game.getBattlefield().getAllActivePermanents(filter, opponentId, game)) {
            permanent.damage(1, source, game);
        }
    }
    return true;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) TargetAnyTarget(mage.target.common.TargetAnyTarget) Permanent(mage.game.permanent.Permanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Targets(mage.target.Targets) UUID(java.util.UUID)

Example 2 with Targets

use of mage.target.Targets 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 3 with Targets

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

the class SpellskiteEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget());
    MageObject sourceObject = game.getObject(source.getSourceId());
    if (stackObject != null && sourceObject != null) {
        Targets targets = new Targets();
        Ability sourceAbility;
        String oldTargetName = null;
        if (stackObject instanceof Spell) {
            Spell spell = (Spell) stackObject;
            sourceAbility = spell.getSpellAbility();
        } else if (stackObject instanceof StackAbility) {
            StackAbility stackAbility = (StackAbility) stackObject;
            sourceAbility = stackAbility;
        } else {
            return false;
        }
        for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
            Mode mode = sourceAbility.getModes().get(modeId);
            targets.addAll(mode.getTargets());
        }
        boolean twoTimesTarget = false;
        if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) {
            Target target = targets.get(0);
            if (target.getFirstTarget().equals(source.getSourceId())) {
                // Target was already the same source, so no change / target event to create
                return true;
            }
            if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
                oldTargetName = getTargetName(targets.getFirstTarget(), game);
                target.clearChosen();
                // The source is still the spell on the stack
                target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
            }
        } else {
            // needed for targeted source's with multiple targets
            Player controller = game.getPlayer(source.getControllerId());
            boolean validTargets = false;
            do {
                for (Target target : targets) {
                    if (target.getTargets().contains(source.getSourceId())) {
                        // You can't change a traget to Spellskite, if Spellskite is already targeted by this target
                        continue;
                    }
                    for (UUID targetId : target.getTargets()) {
                        String name = getTargetName(targetId, game);
                        if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
                            validTargets = true;
                            if (name != null && controller.chooseUse(Outcome.Neutral, "Change target from " + name + " to " + sourceObject.getLogName() + '?', source, game)) {
                                oldTargetName = getTargetName(targetId, game);
                                int damageAmount = target.getTargetAmount(targetId);
                                target.remove(targetId);
                                // The source is still the spell on the stack
                                // add the source permanent id and set the damage if any
                                target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
                                target.setTargetAmount(source.getSourceId(), damageAmount, game);
                                break;
                            }
                        }
                    }
                    if (oldTargetName != null) {
                        break;
                    }
                }
                if (oldTargetName == null) {
                    game.informPlayer(controller, "You have to select at least one target to change to " + sourceObject.getIdName() + '!');
                }
            } while (validTargets && oldTargetName == null);
        }
        if (oldTargetName != null) {
            game.informPlayers(sourceObject.getLogName() + ": Changed target of " + stackObject.getLogName() + " from " + oldTargetName + " to " + sourceObject.getLogName());
        } else if (twoTimesTarget) {
            game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getLogName());
        } else {
            game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getLogName());
        }
        return true;
    }
    return false;
}
Also used : SimpleActivatedAbility(mage.abilities.common.SimpleActivatedAbility) StackAbility(mage.game.stack.StackAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Mode(mage.abilities.Mode) TargetStackObject(mage.target.TargetStackObject) StackObject(mage.game.stack.StackObject) MageObject(mage.MageObject) Targets(mage.target.Targets) Spell(mage.game.stack.Spell) StackAbility(mage.game.stack.StackAbility) Target(mage.target.Target) UUID(java.util.UUID)

Example 4 with Targets

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

the class CompositeCost method getTargets.

@Override
public Targets getTargets() {
    Targets result = new Targets();
    result.addAll(firstCost.getTargets());
    result.addAll(secondCost.getTargets());
    return result;
}
Also used : Targets(mage.target.Targets)

Example 5 with Targets

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

the class YoseiTheMorningStarTapEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Targets targets = source.getTargets();
    Target target1 = targets.get(1);
    for (UUID target : target1.getTargets()) {
        Permanent permanent = game.getPermanent(target);
        if (permanent != null) {
            permanent.tap(source, game);
        } else {
            return false;
        }
    }
    return true;
}
Also used : Target(mage.target.Target) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) Targets(mage.target.Targets) UUID(java.util.UUID)

Aggregations

Targets (mage.target.Targets)6 UUID (java.util.UUID)5 Target (mage.target.Target)5 Player (mage.players.Player)4 MageObject (mage.MageObject)3 Ability (mage.abilities.Ability)3 Mode (mage.abilities.Mode)3 Spell (mage.game.stack.Spell)3 Permanent (mage.game.permanent.Permanent)2 StackAbility (mage.game.stack.StackAbility)2 StackObject (mage.game.stack.StackObject)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 SimpleActivatedAbility (mage.abilities.common.SimpleActivatedAbility)1 SpellCastAllTriggeredAbility (mage.abilities.common.SpellCastAllTriggeredAbility)1 FilterPermanent (mage.filter.FilterPermanent)1 FilterSpell (mage.filter.FilterSpell)1 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)1 ObjectSourcePlayer (mage.filter.predicate.ObjectSourcePlayer)1