Search in sources :

Example 1 with OptionalAdditionalModeSourceCosts

use of mage.abilities.costs.OptionalAdditionalModeSourceCosts in project mage by magefree.

the class Modes method choose.

public boolean choose(Game game, Ability source) {
    if (this.isResetEachTurn()) {
        if (getTurnNum(game, source) != game.getTurnNum()) {
            this.clearAlreadySelectedModes(source, game);
            setTurnNum(game, source);
        }
    }
    if (this.size() > 1) {
        this.clearSelectedModes();
        if (this.isRandom) {
            List<Mode> modes = getAvailableModes(source, game);
            this.addSelectedMode(modes.get(RandomUtil.nextInt(modes.size())).getId());
            return true;
        }
        // check if mode modifying abilities exist
        Card card = game.getCard(source.getSourceId());
        if (card != null) {
            for (Ability modeModifyingAbility : card.getAbilities(game)) {
                if (modeModifyingAbility instanceof OptionalAdditionalModeSourceCosts) {
                    // cost must check activation condition in changeModes
                    ((OptionalAdditionalModeSourceCosts) modeModifyingAbility).changeModes(source, game);
                }
            }
        }
        // check if all modes can be activated automatically
        if (this.size() == this.getMinModes() && !isEachModeMoreThanOnce()) {
            Set<UUID> onceSelectedModes = null;
            if (isEachModeOnlyOnce()) {
                onceSelectedModes = getAlreadySelectedModes(source, game);
            }
            for (Mode mode : this.values()) {
                if ((!isEachModeOnlyOnce() || onceSelectedModes == null || !onceSelectedModes.contains(mode.getId())) && mode.getTargets().canChoose(source.getSourceId(), source.getControllerId(), game)) {
                    this.addSelectedMode(mode.getId());
                }
            }
            if (isEachModeOnlyOnce()) {
                setAlreadySelectedModes(source, game);
            }
            return !selectedModes.isEmpty();
        }
        // 700.2d
        // Some spells and abilities specify that a player other than their controller chooses a mode for it.
        // In that case, the other player does so when the spell or ability's controller normally would do so.
        // If there is more than one other player who could make such a choice, the spell or ability's controller decides which of those players will make the choice.
        UUID playerId = null;
        if (modeChooser == TargetController.OPPONENT) {
            TargetOpponent targetOpponent = new TargetOpponent();
            if (targetOpponent.choose(Outcome.Benefit, source.getControllerId(), source.getSourceId(), game)) {
                playerId = targetOpponent.getFirstTarget();
            }
        } else {
            playerId = source.getControllerId();
        }
        if (playerId == null) {
            return false;
        }
        Player player = game.getPlayer(playerId);
        // player chooses modes manually
        this.currentMode = null;
        int currentMaxModes = this.getMaxModes(game, source);
        while (this.selectedModes.size() < currentMaxModes) {
            Mode choice = player.chooseMode(this, source, game);
            if (choice == null) {
                if (isEachModeOnlyOnce()) {
                    setAlreadySelectedModes(source, game);
                }
                return this.selectedModes.size() >= this.getMinModes() || (this.selectedModes.size() == 0 && mayChooseNone);
            }
            this.addSelectedMode(choice.getId());
            if (currentMode == null) {
                currentMode = choice;
            }
        }
        if (isEachModeOnlyOnce()) {
            setAlreadySelectedModes(source, game);
        }
        return true;
    } else {
        // only one mode available
        if (currentMode == null) {
            this.clearSelectedModes();
            Mode mode = this.values().iterator().next();
            this.addSelectedMode(mode.getId());
            this.setActiveMode(mode);
        }
        return true;
    }
}
Also used : TargetOpponent(mage.target.common.TargetOpponent) Player(mage.players.Player) FilterPlayer(mage.filter.FilterPlayer) OptionalAdditionalModeSourceCosts(mage.abilities.costs.OptionalAdditionalModeSourceCosts) Card(mage.cards.Card)

Aggregations

OptionalAdditionalModeSourceCosts (mage.abilities.costs.OptionalAdditionalModeSourceCosts)1 Card (mage.cards.Card)1 FilterPlayer (mage.filter.FilterPlayer)1 Player (mage.players.Player)1 TargetOpponent (mage.target.common.TargetOpponent)1