Search in sources :

Example 1 with TargetAmount

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

the class PreventDamageToTargetMultiAmountEffect method init.

@Override
public void init(Ability source, Game game) {
    super.init(source, game);
    Target target = source.getTargets().get(0);
    MageObject sourceObject = game.getObject(source.getSourceId());
    if (target instanceof TargetAmount && sourceObject != null) {
        TargetAmount multiTarget = (TargetAmount) target;
        for (UUID targetId : multiTarget.getTargets()) {
            Player player = null;
            Permanent permanent = game.getPermanent(targetId);
            if (permanent == null) {
                player = game.getPlayer(targetId);
            }
            targetAmountMap.put(targetId, multiTarget.getTargetAmount(targetId));
            if (!game.isSimulation()) {
                StringBuilder sb = new StringBuilder(sourceObject.getName()).append(": Prevent the next ");
                sb.append(multiTarget.getTargetAmount(targetId)).append(" damage to ");
                if (player != null) {
                    sb.append(player.getLogName());
                } else if (permanent != null) {
                    sb.append(permanent.getName());
                }
                game.informPlayers(sb.toString());
            }
        }
    }
}
Also used : Target(mage.target.Target) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) MageObject(mage.MageObject) UUID(java.util.UUID) TargetAmount(mage.target.TargetAmount)

Example 2 with TargetAmount

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

the class ComputerPlayer6 method listTargets.

/**
 * Return info about targets list (targeting objects)
 *
 * @param game
 * @param targets
 * @param format    example: my %s in data
 * @param emptyText default text for empty targets list
 * @return
 */
protected String listTargets(Game game, Targets targets, String format, String emptyText) {
    List<String> res = new ArrayList<>();
    for (Target target : targets) {
        for (UUID id : target.getTargets()) {
            MageObject object = game.getObject(id);
            if (object != null) {
                String prefix = "";
                if (target instanceof TargetAmount) {
                    prefix = " " + target.getTargetAmount(id) + "x ";
                }
                res.add(prefix + object.getIdName());
            }
        }
    }
    String info = String.join("; ", res);
    if (info.isEmpty()) {
        return emptyText;
    } else {
        return String.format(format, info);
    }
}
Also used : Target(mage.target.Target) MageObject(mage.MageObject) TargetAmount(mage.target.TargetAmount)

Example 3 with TargetAmount

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

the class PlayerImpl method addTargetOptions.

protected void addTargetOptions(List<Ability> options, Ability option, int targetNum, Game game) {
    for (Target target : option.getTargets().getUnchosen().get(targetNum).getTargetOptions(option, game)) {
        Ability newOption = option.copy();
        if (target instanceof TargetAmount) {
            for (UUID targetId : target.getTargets()) {
                int amount = target.getTargetAmount(targetId);
                newOption.getTargets().get(targetNum).addTarget(targetId, amount, newOption, game, true);
            }
        } else {
            for (UUID targetId : target.getTargets()) {
                newOption.getTargets().get(targetNum).addTarget(targetId, newOption, game, true);
            }
        }
        if (targetNum < option.getTargets().size() - 2) {
            addTargetOptions(options, newOption, targetNum + 1, game);
        } else if (!option.getCosts().getTargets().isEmpty()) {
            addCostTargetOptions(options, newOption, 0, game);
        } else {
            options.add(newOption);
        }
    }
}
Also used : AlternateManaPaymentAbility(mage.abilities.costs.mana.AlternateManaPaymentAbility) StackAbility(mage.game.stack.StackAbility) WhileSearchingPlayFromLibraryAbility(mage.abilities.common.WhileSearchingPlayFromLibraryAbility) AtTheEndOfTurnStepPostDelayedTriggeredAbility(mage.abilities.common.delayed.AtTheEndOfTurnStepPostDelayedTriggeredAbility) PassAbility(mage.abilities.common.PassAbility) PlayLandAsCommanderAbility(mage.abilities.common.PlayLandAsCommanderAbility) Target(mage.target.Target) TargetAmount(mage.target.TargetAmount)

Example 4 with TargetAmount

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

the class StackObjectImpl method chooseNewTarget.

/**
 * Handles the change of one target instance of a mode
 *
 * @param targetController         - player that can choose the new target
 * @param ability
 * @param mode
 * @param target
 * @param forceChange
 * @param newTargetFilterPredicate
 * @param game
 * @return
 */
private Target chooseNewTarget(Player targetController, Ability ability, Mode mode, Target target, boolean forceChange, Predicate newTargetFilterPredicate, Game game) {
    Target newTarget = target.copy();
    // filter targets
    if (newTargetFilterPredicate != null) {
        newTarget.getFilter().add(newTargetFilterPredicate);
        // If adding a predicate, there will only be one choice and therefore target can be automatic
        newTarget.setRandom(true);
    }
    newTarget.setEventReporting(false);
    if (!targetController.getId().equals(getControllerId())) {
        // target controller for the change is different from spell controller
        newTarget.setTargetController(targetController.getId());
        newTarget.setAbilityController(getControllerId());
    }
    newTarget.clearChosen();
    for (UUID targetId : target.getTargets()) {
        String targetNames = getNamesOftargets(targetId, game);
        String targetAmount = "";
        if (target.getTargetAmount(targetId) > 0) {
            targetAmount = " (amount: " + target.getTargetAmount(targetId) + ")";
        }
        // change the target?
        Outcome outcome = mode.getEffects().getOutcome(ability);
        if (targetNames != null && (forceChange || targetController.chooseUse(outcome, "Change this target: " + targetNames + targetAmount + '?', ability, game))) {
            Set<UUID> possibleTargets = target.possibleTargets(this.getSourceId(), getControllerId(), game);
            // choose exactly one other target - already targeted objects are not counted
            if (forceChange && possibleTargets != null && possibleTargets.size() > 1) {
                // controller of spell must be used (e.g. TargetOpponent)
                int iteration = 0;
                do {
                    if (iteration > 0 && !game.isSimulation()) {
                        game.informPlayer(targetController, "You may only select exactly one target that must be different from the origin target!");
                    }
                    iteration++;
                    newTarget.clearChosen();
                    newTarget.chooseTarget(outcome, getControllerId(), ability, game);
                    // workaround to stop infinite AI choose (remove after chooseTarget can be called with extra filter to disable some ids)
                    if (iteration > 10) {
                        break;
                    }
                } while (targetController.canRespond() && (targetId.equals(newTarget.getFirstTarget()) || newTarget.getTargets().size() != 1));
            // choose a new target
            } else {
                // build a target definition with exactly one possible target to select that replaces old target
                Target tempTarget = target.copy();
                if (newTargetFilterPredicate != null) {
                    tempTarget.getFilter().add(newTargetFilterPredicate);
                    // If adding a predicate, there will only be one choice and therefore target can be automatic
                    tempTarget.setRandom(true);
                }
                tempTarget.setEventReporting(false);
                if (target instanceof TargetAmount) {
                    ((TargetAmount) tempTarget).setAmountDefinition(StaticValue.get(target.getTargetAmount(targetId)));
                }
                tempTarget.setMinNumberOfTargets(1);
                tempTarget.setMaxNumberOfTargets(1);
                if (!targetController.getId().equals(getControllerId())) {
                    tempTarget.setTargetController(targetController.getId());
                    tempTarget.setAbilityController(getControllerId());
                }
                boolean again;
                do {
                    again = false;
                    tempTarget.clearChosen();
                    if (!tempTarget.chooseTarget(outcome, getControllerId(), ability, game)) {
                        if (targetController.chooseUse(Outcome.Benefit, "No target object selected. Reset to original target?", ability, game)) {
                            // use previous target no target was selected
                            newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
                        } else {
                            again = true;
                        }
                    } else // if possible add the alternate Target - it may not be included in the old definition nor in the already selected targets of the new definition
                    {
                        if (newTarget.getTargets().contains(tempTarget.getFirstTarget()) || target.getTargets().contains(tempTarget.getFirstTarget())) {
                            if (targetController.isHuman()) {
                                if (targetController.chooseUse(Outcome.Benefit, "This target was already selected from origin spell. Reset to original target?", ability, game)) {
                                    // use previous target no target was selected
                                    newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
                                } else {
                                    again = true;
                                }
                            } else {
                                newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
                            }
                        } else if (!target.canTarget(getControllerId(), tempTarget.getFirstTarget(), ability, game)) {
                            if (targetController.isHuman()) {
                                game.informPlayer(targetController, "This target is not valid!");
                                again = true;
                            } else {
                                // keep the old
                                newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
                            }
                        } else {
                            // valid target was selected, add it to the new target definition
                            newTarget.addTarget(tempTarget.getFirstTarget(), target.getTargetAmount(targetId), ability, game, true);
                        }
                    }
                } while (again && targetController.canRespond());
            }
        } else // keep the target
        {
            newTarget.addTarget(targetId, target.getTargetAmount(targetId), ability, game, true);
        }
    }
    return newTarget;
}
Also used : Target(mage.target.Target) Outcome(mage.constants.Outcome) TargetAmount(mage.target.TargetAmount)

Example 5 with TargetAmount

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

the class InvokeJusticeEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Player player = game.getPlayer(getTargetPointer().getFirst(game, source));
    if (controller == null || player == null) {
        return false;
    }
    FilterPermanent filter = new FilterPermanent("creatures and/or Vehicles controlled by " + player.getName());
    filter.add(new ControllerIdPredicate(player.getId()));
    if (!game.getBattlefield().contains(filter, source, game, 1)) {
        return false;
    }
    TargetAmount target = new TargetPermanentAmount(4, filter);
    target.setNotTarget(true);
    controller.choose(outcome, target, source.getSourceId(), game);
    for (UUID targetId : target.getTargets()) {
        Permanent permanent = game.getPermanent(targetId);
        if (permanent != null) {
            permanent.addCounters(CounterType.P1P1.createInstance(target.getTargetAmount(targetId)), source, game);
        }
    }
    return true;
}
Also used : TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) FilterPermanent(mage.filter.FilterPermanent) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) UUID(java.util.UUID) TargetPermanentAmount(mage.target.common.TargetPermanentAmount) TargetAmount(mage.target.TargetAmount)

Aggregations

TargetAmount (mage.target.TargetAmount)7 UUID (java.util.UUID)4 Permanent (mage.game.permanent.Permanent)4 Player (mage.players.Player)4 Target (mage.target.Target)4 FilterPermanent (mage.filter.FilterPermanent)3 MageObject (mage.MageObject)2 PassAbility (mage.abilities.common.PassAbility)1 PlayLandAsCommanderAbility (mage.abilities.common.PlayLandAsCommanderAbility)1 WhileSearchingPlayFromLibraryAbility (mage.abilities.common.WhileSearchingPlayFromLibraryAbility)1 AtTheEndOfTurnStepPostDelayedTriggeredAbility (mage.abilities.common.delayed.AtTheEndOfTurnStepPostDelayedTriggeredAbility)1 AlternateManaPaymentAbility (mage.abilities.costs.mana.AlternateManaPaymentAbility)1 Outcome (mage.constants.Outcome)1 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)1 ObjectSourcePlayer (mage.filter.predicate.ObjectSourcePlayer)1 ControllerIdPredicate (mage.filter.predicate.permanent.ControllerIdPredicate)1 StackAbility (mage.game.stack.StackAbility)1 TargetPlayer (mage.target.TargetPlayer)1 TargetCreatureOrPlaneswalkerAmount (mage.target.common.TargetCreatureOrPlaneswalkerAmount)1 TargetCreaturePermanentAmount (mage.target.common.TargetCreaturePermanentAmount)1