Search in sources :

Example 6 with Targets

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

the class ChangeATargetOfTargetSpellAbilityToSourceEffect 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 {
            Player controller = game.getPlayer(source.getControllerId());
            boolean validTargets = false;
            do {
                for (Target target : targets) {
                    for (UUID targetId : target.getTargets()) {
                        String name = getTargetName(targets.getFirstTarget(), game);
                        if (!targetId.equals(source.getSourceId()) && target.getTargets().contains(source.getSourceId())) {
                            // you can't change this target to source because the source is already another targetId of that target.
                            twoTimesTarget = true;
                            continue;
                        }
                        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);
                                target.remove(targetId);
                                // The source is still the spell on the stack
                                target.addTarget(source.getSourceId(), stackObject.getStackAbility(), 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 : StackAbility(mage.game.stack.StackAbility) Ability(mage.abilities.Ability) Target(mage.target.Target) Player(mage.players.Player) Mode(mage.abilities.Mode) StackObject(mage.game.stack.StackObject) MageObject(mage.MageObject) Targets(mage.target.Targets) UUID(java.util.UUID) Spell(mage.game.stack.Spell) StackAbility(mage.game.stack.StackAbility)

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