refactor: improved ability's modes code (related to #11333)

This commit is contained in:
Oleg Agafonov 2023-10-23 21:53:29 +04:00
parent fec5de873b
commit b7ce9c80f0
32 changed files with 162 additions and 137 deletions

View file

@ -2409,7 +2409,7 @@ public class HumanPlayer extends PlayerImpl {
Mode selectedMode = modes.get(selectedModeId); Mode selectedMode = modes.get(selectedModeId);
if (mode.getId().equals(selectedMode.getId())) { if (mode.getId().equals(selectedMode.getId())) {
// mode selected // mode selected
if (modes.isEachModeMoreThanOnce()) { if (modes.isMayChooseSameModeMoreThanOnce()) {
// can select again // can select again
} else { } else {
// hide mode from dialog // hide mode from dialog
@ -2423,7 +2423,7 @@ public class HumanPlayer extends PlayerImpl {
if (obj != null) { if (obj != null) {
modeText = modeText.replace("{this}", obj.getName()); modeText = modeText.replace("{this}", obj.getName());
} }
if (modes.isEachModeMoreThanOnce()) { if (modes.isMayChooseSameModeMoreThanOnce()) {
if (timesSelected > 0) { if (timesSelected > 0) {
modeText = "(selected " + timesSelected + "x) " + modeText; modeText = "(selected " + timesSelected + "x) " + modeText;
} }

View file

@ -46,8 +46,7 @@ public final class BreechesEagerPillager extends CardImpl {
new CreateTokenEffect(new TreasureToken()), false, filter new CreateTokenEffect(new TreasureToken()), false, filter
); );
ability.setModeTag("treasure"); ability.setModeTag("treasure");
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(true);
ability.getModes().setResetEachTurn(true);
// * Target creature can't block this turn. // * Target creature can't block this turn.
Mode mode = new Mode(new CantBlockTargetEffect(Duration.EndOfTurn)) Mode mode = new Mode(new CantBlockTargetEffect(Duration.EndOfTurn))

View file

@ -23,7 +23,7 @@ public final class BrokersConfluence extends CardImpl {
// Choose three. You may choose the same mode more than once. // Choose three. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(3); this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3); this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// Proliferate. // Proliferate.
this.getSpellAbility().addEffect(new ProliferateEffect()); this.getSpellAbility().addEffect(new ProliferateEffect());

View file

@ -40,7 +40,7 @@ public final class CabarettiConfluence extends CardImpl {
// Choose three. You may choose the same mode more than once. // Choose three. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(3); this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3); this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// Create a token that's a copy of target creature you control. It gains haste. Sacrifice it at the beginning of the next end step. // Create a token that's a copy of target creature you control. It gains haste. Sacrifice it at the beginning of the next end step.
this.getSpellAbility().addEffect(new CabarettiConfluenceEffect()); this.getSpellAbility().addEffect(new CabarettiConfluenceEffect());

View file

@ -34,7 +34,7 @@ public final class CaptiveAudience extends CardImpl {
new SetPlayerLifeSourceEffect(4), TargetController.YOU, false new SetPlayerLifeSourceEffect(4), TargetController.YOU, false
); );
ability.setModeTag("life total becomes 4"); ability.setModeTag("life total becomes 4");
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(false);
// Discard your hand. // Discard your hand.
ability.addMode( ability.addMode(

View file

@ -33,7 +33,7 @@ public final class DemonicPact extends CardImpl {
// - Demonic Pact deals 4 damage to any target and you gain 4 life; // - Demonic Pact deals 4 damage to any target and you gain 4 life;
Ability ability = new BeginningOfUpkeepTriggeredAbility(new DamageTargetEffect(4), TargetController.YOU, false); Ability ability = new BeginningOfUpkeepTriggeredAbility(new DamageTargetEffect(4), TargetController.YOU, false);
ability.setModeTag("deals damage and gain life"); ability.setModeTag("deals damage and gain life");
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(false);
ability.addTarget(new TargetAnyTarget()); ability.addTarget(new TargetAnyTarget());
Effect effect = new GainLifeEffect(4); Effect effect = new GainLifeEffect(4);
effect.setText("and you gain 4 life"); effect.setText("and you gain 4 life");

View file

@ -30,7 +30,7 @@ public final class FatalLore extends CardImpl {
super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{2}{B}{B}"); super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{2}{B}{B}");
// An opponent chooses one // An opponent chooses one
this.getSpellAbility().getModes().setModeChooser(TargetController.OPPONENT); this.getSpellAbility().getModes().setChooseController(TargetController.OPPONENT);
// You draw three cards. // You draw three cards.
this.getSpellAbility().addEffect(new DrawCardSourceControllerEffect(1).setText("you draw three cards")); this.getSpellAbility().addEffect(new DrawCardSourceControllerEffect(1).setText("you draw three cards"));

View file

@ -25,7 +25,7 @@ public final class FieryConfluence extends CardImpl {
// Choose three. You may choose the same mode more than once. // Choose three. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(3); this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3); this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// - Fiery Confluence deals 1 damage to each creature; // - Fiery Confluence deals 1 damage to each creature;
this.getSpellAbility().addEffect(new DamageAllEffect(1, new FilterCreaturePermanent())); this.getSpellAbility().addEffect(new DamageAllEffect(1, new FilterCreaturePermanent()));

View file

@ -34,8 +34,7 @@ public final class GalaGreeters extends CardImpl {
// Put a +1/+1 counter on Gala Greeters. // Put a +1/+1 counter on Gala Greeters.
Ability ability = new AllianceAbility(new AddCountersSourceEffect(CounterType.P1P1.createInstance())); Ability ability = new AllianceAbility(new AddCountersSourceEffect(CounterType.P1P1.createInstance()));
ability.setModeTag("put +1/+1 counter"); ability.setModeTag("put +1/+1 counter");
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(true);
ability.getModes().setResetEachTurn(true);
// Create a tapped Treasure token. // Create a tapped Treasure token.
ability.addMode( ability.addMode(

View file

@ -38,8 +38,7 @@ public final class GaladrielLightOfValinor extends CardImpl {
// Add {G}{G}{G}. // Add {G}{G}{G}.
Ability ability = new AllianceAbility(new AddManaToManaPoolSourceControllerEffect(Mana.GreenMana(3))); Ability ability = new AllianceAbility(new AddManaToManaPoolSourceControllerEffect(Mana.GreenMana(3)));
ability.setModeTag("add mana"); ability.setModeTag("add mana");
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(true);
ability.getModes().setResetEachTurn(true);
// Put a +1/+1 counter on each creature you control. // Put a +1/+1 counter on each creature you control.
ability.addMode( ability.addMode(

View file

@ -51,7 +51,7 @@ public final class GandalfTheGrey extends CardImpl {
); );
ability.setModeTag("tap or untap"); ability.setModeTag("tap or untap");
ability.addTarget(new TargetPermanent()); ability.addTarget(new TargetPermanent());
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(false);
// * Gandalf the Grey deals 3 damage to each opponent. // * Gandalf the Grey deals 3 damage to each opponent.
ability.addMode( ability.addMode(

View file

@ -44,7 +44,7 @@ public final class HenrikaDomnathi extends CardImpl {
1, StaticFilters.FILTER_CONTROLLED_CREATURE_SHORT_TEXT 1, StaticFilters.FILTER_CONTROLLED_CREATURE_SHORT_TEXT
), TargetController.YOU, false); ), TargetController.YOU, false);
ability.setModeTag("each player sacrifice"); ability.setModeTag("each player sacrifice");
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(false);
// You draw a card and you lose 1 life. // You draw a card and you lose 1 life.
Mode mode = new Mode(new DrawCardSourceControllerEffect(1).setText("you draw a card")); Mode mode = new Mode(new DrawCardSourceControllerEffect(1).setText("you draw a card"));

View file

@ -49,8 +49,7 @@ public final class KarganIntimidator extends CardImpl {
new BoostSourceEffect(1, 1, Duration.EndOfTurn), new GenericManaCost(1) new BoostSourceEffect(1, 1, Duration.EndOfTurn), new GenericManaCost(1)
); );
ability.setModeTag("gets +1/+1"); ability.setModeTag("gets +1/+1");
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(true);
ability.getModes().setResetEachTurn(true);
// Target creature becomes a Coward until end of turn. // Target creature becomes a Coward until end of turn.
Mode mode = new Mode(new BecomesCreatureTypeTargetEffect( Mode mode = new Mode(new BecomesCreatureTypeTargetEffect(

View file

@ -22,7 +22,7 @@ public final class LibraryOfLatNam extends CardImpl {
super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{4}{U}"); super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{4}{U}");
// An opponent chooses one // An opponent chooses one
this.getSpellAbility().getModes().setModeChooser(TargetController.OPPONENT); this.getSpellAbility().getModes().setChooseController(TargetController.OPPONENT);
// You draw three cards at the beginning of the next turn's upkeep; // You draw three cards at the beginning of the next turn's upkeep;
this.getSpellAbility().addEffect(new CreateDelayedTriggeredAbilityEffect(new AtTheBeginOfNextUpkeepDelayedTriggeredAbility(new DrawCardSourceControllerEffect(3).setText("you draw three cards")), false)); this.getSpellAbility().addEffect(new CreateDelayedTriggeredAbilityEffect(new AtTheBeginOfNextUpkeepDelayedTriggeredAbility(new DrawCardSourceControllerEffect(3).setText("you draw three cards")), false));

View file

@ -39,7 +39,7 @@ public final class MaestrosConfluence extends CardImpl {
// Choose three. You may choose the same mode more than once. // Choose three. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(3); this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3); this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// Return target monocolored instant or sorcery card from your graveyard to your hand. // Return target monocolored instant or sorcery card from your graveyard to your hand.
this.getSpellAbility().addEffect(new ReturnFromGraveyardToHandTargetEffect()); this.getSpellAbility().addEffect(new ReturnFromGraveyardToHandTargetEffect());

View file

@ -27,7 +27,7 @@ public final class Misfortune extends CardImpl {
super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{1}{B}{R}{G}"); super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{1}{B}{R}{G}");
// An opponent chooses one // An opponent chooses one
this.getSpellAbility().getModes().setModeChooser(TargetController.OPPONENT); this.getSpellAbility().getModes().setChooseController(TargetController.OPPONENT);
// You put a +1/+1 counter on each creature you control and gain 4 life. // You put a +1/+1 counter on each creature you control and gain 4 life.
this.getSpellAbility().addEffect(new AddCountersAllEffect( this.getSpellAbility().addEffect(new AddCountersAllEffect(

View file

@ -25,7 +25,7 @@ public final class MysticConfluence extends CardImpl {
// Choose three. You may choose the same mode more than once. // Choose three. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(3); this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3); this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// - Counter target spell unless its controller pays {3}; // - Counter target spell unless its controller pays {3};
this.getSpellAbility().addEffect(new CounterUnlessPaysEffect(new GenericManaCost(3))); this.getSpellAbility().addEffect(new CounterUnlessPaysEffect(new GenericManaCost(3)));

View file

@ -34,7 +34,7 @@ public final class ObscuraConfluence extends CardImpl {
// Choose three. You may choose the same mode more than once. // Choose three. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(3); this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3); this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// Until end of turn, target creature loses all abilities and has base power and toughness 1/1. // Until end of turn, target creature loses all abilities and has base power and toughness 1/1.
this.getSpellAbility().addEffect(new LoseAllAbilitiesTargetEffect(Duration.EndOfTurn) this.getSpellAbility().addEffect(new LoseAllAbilitiesTargetEffect(Duration.EndOfTurn)

View file

@ -28,7 +28,7 @@ public final class PlanewideCelebration extends CardImpl {
// Choose four. You may choose the same mode more than once. // Choose four. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(4); this.getSpellAbility().getModes().setMinModes(4);
this.getSpellAbility().getModes().setMaxModes(4); this.getSpellAbility().getModes().setMaxModes(4);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// Create a 2/2 Citizen creature token that's all colors. // Create a 2/2 Citizen creature token that's all colors.
this.getSpellAbility().addEffect(new CreateTokenEffect(new PlanewideCelebrationToken())); this.getSpellAbility().addEffect(new CreateTokenEffect(new PlanewideCelebrationToken()));

View file

@ -24,7 +24,7 @@ public final class RighteousConfluence extends CardImpl {
// Choose three - You may choose the same mode more than once. // Choose three - You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(3); this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3); this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// - Create a 2/2 white Knight creature token with vigilance; // - Create a 2/2 white Knight creature token with vigilance;
this.getSpellAbility().addEffect(new CreateTokenEffect(new KnightToken())); this.getSpellAbility().addEffect(new CreateTokenEffect(new KnightToken()));

View file

@ -32,7 +32,7 @@ public class RiveteersConfluence extends CardImpl {
// Choose three. You may choose the same mode more than once. // Choose three. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(3); this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3); this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// You draw a card and you lose 1 life. // You draw a card and you lose 1 life.
this.getSpellAbility().addEffect(new DrawCardSourceControllerEffect(1).setText("you draw a card")); this.getSpellAbility().addEffect(new DrawCardSourceControllerEffect(1).setText("you draw a card"));

View file

@ -53,7 +53,7 @@ public final class SolKanarTheTainted extends CardImpl {
new DrawCardSourceControllerEffect(1), TargetController.YOU, false new DrawCardSourceControllerEffect(1), TargetController.YOU, false
); );
ability.setModeTag("draw"); ability.setModeTag("draw");
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(false);
// * Each opponent loses 2 life and you gain 2 life. // * Each opponent loses 2 life and you gain 2 life.
ability.addMode(new Mode(new LoseLifeOpponentsEffect(2)) ability.addMode(new Mode(new LoseLifeOpponentsEffect(2))

View file

@ -36,7 +36,7 @@ public final class ThreeBowlsOfPorridge extends CardImpl {
ability.addCost(new TapSourceCost()); ability.addCost(new TapSourceCost());
ability.addTarget(new TargetCreaturePermanent()); ability.addTarget(new TargetCreaturePermanent());
ability.setModeTag("deals damage"); ability.setModeTag("deals damage");
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(false);
// * Tap target creature. // * Tap target creature.
Mode mode = new Mode(new TapTargetEffect()); Mode mode = new Mode(new TapTargetEffect());

View file

@ -23,7 +23,7 @@ public final class UniteTheCoalition extends CardImpl {
// Choose five. You may choose the same mode more than once. // Choose five. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(5); this.getSpellAbility().getModes().setMinModes(5);
this.getSpellAbility().getModes().setMaxModes(5); this.getSpellAbility().getModes().setMaxModes(5);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// Target permanent phases out. // Target permanent phases out.
this.getSpellAbility().addEffect(new PhaseOutTargetEffect()); this.getSpellAbility().addEffect(new PhaseOutTargetEffect());

View file

@ -29,7 +29,7 @@ public final class VerdantConfluence extends CardImpl {
// Choose three. You may choose the same mode more than once. // Choose three. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(3); this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3); this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// - Put two +1/+1 counters on target creature; // - Put two +1/+1 counters on target creature;
this.getSpellAbility().addEffect(new AddCountersTargetEffect(CounterType.P1P1.createInstance(2))); this.getSpellAbility().addEffect(new AddCountersTargetEffect(CounterType.P1P1.createInstance(2)));

View file

@ -50,7 +50,7 @@ public final class VindictiveLich extends CardImpl {
)); ));
ability.getModes().setMinModes(1); ability.getModes().setMinModes(1);
ability.getModes().setMaxModes(3); ability.getModes().setMaxModes(3);
ability.getModes().setEachModeOnlyOnce(true); ability.getModes().setLimitUsageByOnce(false);
ability.getModes().setMaxModesFilter(filter0); ability.getModes().setMaxModesFilter(filter0);
ability.addTarget(new TargetPlayer(filter1).setTargetTag(1).withChooseHint("to sacrifice a creature")); ability.addTarget(new TargetPlayer(filter1).setTargetTag(1).withChooseHint("to sacrifice a creature"));

View file

@ -29,7 +29,7 @@ public final class WretchedConfluence extends CardImpl {
// Choose three. You may choose the same mode more than once. // Choose three. You may choose the same mode more than once.
this.getSpellAbility().getModes().setMinModes(3); this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3); this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true); this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// - Target player draws a card and loses 1 life; // - Target player draws a card and loses 1 life;
Effect effect = new LoseLifeTargetEffect(1); Effect effect = new LoseLifeTargetEffect(1);

View file

@ -454,7 +454,7 @@ public class TestPlayer implements Player {
Mode selectedMode; Mode selectedMode;
if (targetName.startsWith("mode=")) { if (targetName.startsWith("mode=")) {
int modeNr = Integer.parseInt(targetName.substring(5, 6)); int modeNr = Integer.parseInt(targetName.substring(5, 6));
if (modeNr == 0 || modeNr > (ability.getModes().isEachModeMoreThanOnce() ? ability.getModes().getSelectedModes().size() : ability.getModes().size())) { if (modeNr == 0 || modeNr > (ability.getModes().isMayChooseSameModeMoreThanOnce() ? ability.getModes().getSelectedModes().size() : ability.getModes().size())) {
throw new UnsupportedOperationException("Given mode number (" + modeNr + ") not available for " + ability.toString()); throw new UnsupportedOperationException("Given mode number (" + modeNr + ") not available for " + ability.toString());
} }
UUID modeId = ability.getModes().getModeId(modeNr); UUID modeId = ability.getModes().getModeId(modeNr);

View file

@ -986,7 +986,7 @@ public abstract class AbilityImpl implements Ability {
if (validTargets) { if (validTargets) {
found++; found++;
if (modes.isEachModeMoreThanOnce()) { if (modes.isMayChooseSameModeMoreThanOnce()) {
return true; return true;
} }
if (found >= modes.getMinModes()) { if (found >= modes.getMinModes()) {

View file

@ -12,6 +12,7 @@ import mage.game.Game;
import mage.players.Player; import mage.players.Player;
import mage.target.common.TargetOpponent; import mage.target.common.TargetOpponent;
import mage.util.CardUtil; import mage.util.CardUtil;
import mage.util.Copyable;
import mage.util.RandomUtil; import mage.util.RandomUtil;
import java.util.*; import java.util.*;
@ -20,38 +21,40 @@ import java.util.stream.Stream;
/** /**
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
*/ */
public class Modes extends LinkedHashMap<UUID, Mode> { public class Modes extends LinkedHashMap<UUID, Mode> implements Copyable<Modes> {
// choose ID for options in ability/mode picker dialogs // choose ID for options in ability/mode picker dialogs
public static final UUID CHOOSE_OPTION_DONE_ID = UUID.fromString("33e72ad6-17ae-4bfb-a097-6e7aa06b49e9"); public static final UUID CHOOSE_OPTION_DONE_ID = UUID.fromString("33e72ad6-17ae-4bfb-a097-6e7aa06b49e9");
public static final UUID CHOOSE_OPTION_CANCEL_ID = UUID.fromString("0125bd0c-5610-4eba-bc80-fc6d0a7b9de6"); public static final UUID CHOOSE_OPTION_CANCEL_ID = UUID.fromString("0125bd0c-5610-4eba-bc80-fc6d0a7b9de6");
private Mode currentMode; // the current mode of the selected modes private Mode currentMode; // current active mode for resolving
private final List<UUID> selectedModes = new ArrayList<>(); // all selected modes (this + duplicate), use getSelectedModes all the time to keep modes order private final List<UUID> selectedModes = new ArrayList<>(); // all selected modes (this + duplicate), use getSelectedModes all the time to keep modes order
private final Map<UUID, Mode> selectedDuplicateModes = new LinkedHashMap<>(); // for 2x selects: copy mode and put it to duplicate list private final Map<UUID, Mode> selectedDuplicateModes = new LinkedHashMap<>(); // for 2x selects: additional selected modes
private final Map<UUID, UUID> selectedDuplicateToOriginalModeRefs = new LinkedHashMap<>(); // for 2x selects: stores ref from duplicate to original mode private final Map<UUID, UUID> selectedDuplicateToOriginalModeRefs = new LinkedHashMap<>(); // for 2x selects: stores ref from duplicate to original mode
private int minModes; private int minModes;
private int maxModes; private int maxModes;
private TargetController modeChooser; private Filter maxModesFilter; // calculates the max number of available modes
private boolean eachModeMoreThanOnce; // each mode can be selected multiple times during one choice private Condition moreCondition; // allows multiple modes choose (example: choose one... if condition, you may choose both)
private boolean eachModeOnlyOnce; // state if each mode can be chosen only once as long as the source object exists
private Filter maxModesFilter = null; // calculates the max number of available modes private boolean limitUsageByOnce = false; // limit mode selection to once per game
private boolean isRandom = false; private boolean limitUsageResetOnNewTurn = false; // reset once per game limit on new turn, example: Galadriel, Light of Valinor
private String chooseText = null; private String chooseText = null;
private boolean resetEachTurn = false; private TargetController chooseController;
private Condition moreCondition; private boolean mayChooseSameModeMoreThanOnce = false; // example: choose three... you may choose the same mode more than once
private boolean mayChooseNone = false; private boolean mayChooseNone = false;
private boolean isRandom = false;
public Modes() { public Modes() {
// add default mode
this.currentMode = new Mode((Effect) null); this.currentMode = new Mode((Effect) null);
this.put(currentMode.getId(), currentMode); this.put(currentMode.getId(), currentMode);
this.minModes = 1; this.minModes = 1;
this.maxModes = 1; this.maxModes = 1;
this.addSelectedMode(currentMode.getId()); this.addSelectedMode(currentMode.getId());
this.modeChooser = TargetController.YOU; this.chooseController = TargetController.YOU;
this.eachModeOnlyOnce = false;
this.eachModeMoreThanOnce = false;
} }
protected Modes(final Modes modes) { protected Modes(final Modes modes) {
@ -65,25 +68,28 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
this.minModes = modes.minModes; this.minModes = modes.minModes;
this.maxModes = modes.maxModes; this.maxModes = modes.maxModes;
this.selectedModes.addAll(modes.getSelectedModes());
this.modeChooser = modes.modeChooser;
this.eachModeOnlyOnce = modes.eachModeOnlyOnce;
this.eachModeMoreThanOnce = modes.eachModeMoreThanOnce;
this.maxModesFilter = modes.maxModesFilter; // can't change so no copy needed this.maxModesFilter = modes.maxModesFilter; // can't change so no copy needed
this.moreCondition = modes.moreCondition;
this.limitUsageByOnce = modes.limitUsageByOnce;
this.limitUsageResetOnNewTurn = modes.limitUsageResetOnNewTurn;
this.isRandom = modes.isRandom;
this.chooseText = modes.chooseText; this.chooseText = modes.chooseText;
this.resetEachTurn = modes.resetEachTurn; this.chooseController = modes.chooseController;
this.mayChooseSameModeMoreThanOnce = modes.mayChooseSameModeMoreThanOnce;
this.mayChooseNone = modes.mayChooseNone;
this.isRandom = modes.isRandom;
// current mode must be "copied" at the end
this.selectedModes.addAll(modes.getSelectedModes()); // TODO: bugged - can lost multi selects here?
if (modes.getSelectedModes().isEmpty()) { if (modes.getSelectedModes().isEmpty()) {
this.currentMode = values().iterator().next(); this.currentMode = values().iterator().next();
} else { } else {
this.currentMode = get(modes.getMode().getId()); // need fix? this.currentMode = get(modes.getMode().getId()); // TODO: bugged - can lost multi selects here?
} }
this.moreCondition = modes.moreCondition;
this.mayChooseNone = modes.mayChooseNone;
} }
@Override
public Modes copy() { public Modes copy() {
return new Modes(this); return new Modes(this);
} }
@ -91,21 +97,21 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
@Override @Override
public Mode get(Object key) { public Mode get(Object key) {
Mode modeToGet = super.get(key); Mode modeToGet = super.get(key);
if (modeToGet == null && eachModeMoreThanOnce) { if (modeToGet == null && mayChooseSameModeMoreThanOnce) {
modeToGet = selectedDuplicateModes.get(key); modeToGet = selectedDuplicateModes.get(key);
} }
return modeToGet; return modeToGet;
} }
public Stream<Mode> stream() { public Stream<Mode> streamAlreadySelectedModes(Ability source, Game game) {
return super.values().stream();
}
public Stream<Mode> streamAlreadySelected(Ability source, Game game) {
Set<UUID> selected = getAlreadySelectedModes(source, game, true); Set<UUID> selected = getAlreadySelectedModes(source, game, true);
return stream().filter(m -> selected.contains(m.getId())); return super.values().stream().filter(m -> selected.contains(m.getId()));
} }
/**
* For card constructor: returns first/default mode
* For game: returns current resolving mode
*/
public Mode getMode() { public Mode getMode() {
return currentMode; return currentMode;
} }
@ -119,7 +125,7 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
*/ */
public UUID getModeId(int index) { public UUID getModeId(int index) {
int idx = 0; int idx = 0;
if (eachModeMoreThanOnce) { if (mayChooseSameModeMoreThanOnce) {
for (UUID modeId : this.getSelectedModes()) { for (UUID modeId : this.getSelectedModes()) {
idx++; idx++;
if (idx == index) { if (idx == index) {
@ -137,6 +143,9 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
return null; return null;
} }
/**
* Return full list of selected modes in default/rules order (without multi selects)
*/
public List<UUID> getSelectedModes() { public List<UUID> getSelectedModes() {
// modes can be selected in any order by user, but execution must be in rule's order // modes can be selected in any order by user, but execution must be in rule's order
List<UUID> res = new ArrayList<>(this.size()); List<UUID> res = new ArrayList<>(this.size());
@ -202,6 +211,11 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
} }
public void setMaxModesFilter(Filter maxModesFilter) { public void setMaxModesFilter(Filter maxModesFilter) {
// verify check
if (maxModesFilter != null && !(maxModesFilter instanceof FilterPlayer)) {
throw new IllegalArgumentException("Wrong code usage: max modes filter support only FilterPlayer");
}
this.maxModesFilter = maxModesFilter; this.maxModesFilter = maxModesFilter;
} }
@ -223,7 +237,7 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
realMaxModes = 2; realMaxModes = 2;
} }
// use case: limit max modes by opponents (wtf?!) // use case: limit max modes by opponents (example: choose one or more... each mode must target a different player)
if (getMaxModesFilter() != null) { if (getMaxModesFilter() != null) {
if (this.maxModesFilter instanceof FilterPlayer) { if (this.maxModesFilter instanceof FilterPlayer) {
realMaxModes = 0; realMaxModes = 0;
@ -242,12 +256,12 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
return realMaxModes; return realMaxModes;
} }
public void setModeChooser(TargetController modeChooser) { public void setChooseController(TargetController chooseController) {
this.modeChooser = modeChooser; this.chooseController = chooseController;
} }
public TargetController getModeChooser() { public TargetController getChooseController() {
return this.modeChooser; return this.chooseController;
} }
public void setActiveMode(Mode mode) { public void setActiveMode(Mode mode) {
@ -268,12 +282,14 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
this.moreCondition = moreCondition; this.moreCondition = moreCondition;
} }
private boolean isAlreadySelectedModesOutdated(Game game, Ability source) {
return this.isLimitUsageResetOnNewTurn()
&& getOnceTurnNum(game, source) != game.getTurnNum();
}
public boolean choose(Game game, Ability source) { public boolean choose(Game game, Ability source) {
if (this.isResetEachTurn()) { if (isAlreadySelectedModesOutdated(game, source)) {
if (getTurnNum(game, source) != game.getTurnNum()) { this.clearAlreadySelectedModes(source, game);
this.clearAlreadySelectedModes(source, game);
setTurnNum(game, source);
}
} }
if (this.size() > 1) { if (this.size() > 1) {
this.clearSelectedModes(); this.clearSelectedModes();
@ -295,19 +311,19 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
} }
// check if all modes can be activated automatically // check if all modes can be activated automatically
if (this.size() == this.getMinModes() && !isEachModeMoreThanOnce()) { if (this.size() == this.getMinModes() && !isMayChooseSameModeMoreThanOnce()) {
Set<UUID> onceSelectedModes = null; Set<UUID> onceSelectedModes = null;
if (isEachModeOnlyOnce()) { if (isLimitUsageByOnce()) {
onceSelectedModes = getAlreadySelectedModes(source, game, true); onceSelectedModes = getAlreadySelectedModes(source, game, true);
} }
for (Mode mode : this.values()) { for (Mode mode : this.values()) {
if ((!isEachModeOnlyOnce() || onceSelectedModes == null || !onceSelectedModes.contains(mode.getId())) if ((!isLimitUsageByOnce() || onceSelectedModes == null || !onceSelectedModes.contains(mode.getId()))
&& mode.getTargets().canChoose(source.getControllerId(), source, game)) { && mode.getTargets().canChoose(source.getControllerId(), source, game)) {
this.addSelectedMode(mode.getId()); this.addSelectedMode(mode.getId());
} }
} }
if (isEachModeOnlyOnce()) { if (isLimitUsageByOnce()) {
setAlreadySelectedModes(source, game); setOnceSelectedModes(source, game);
} }
return !selectedModes.isEmpty(); return !selectedModes.isEmpty();
} }
@ -317,7 +333,7 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
// In that case, the other player does so when the spell or ability's controller normally would do so. // 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. // 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; UUID playerId;
if (modeChooser == TargetController.OPPONENT) { if (chooseController == TargetController.OPPONENT) {
TargetOpponent targetOpponent = new TargetOpponent(); TargetOpponent targetOpponent = new TargetOpponent();
targetOpponent.choose(Outcome.Benefit, source.getControllerId(), source.getSourceId(), source, game); targetOpponent.choose(Outcome.Benefit, source.getControllerId(), source.getSourceId(), source, game);
playerId = targetOpponent.getFirstTarget(); playerId = targetOpponent.getFirstTarget();
@ -336,8 +352,8 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
while (this.selectedModes.size() < currentMaxModes) { while (this.selectedModes.size() < currentMaxModes) {
Mode choice = player.chooseMode(this, source, game); Mode choice = player.chooseMode(this, source, game);
if (choice == null) { if (choice == null) {
if (isEachModeOnlyOnce()) { if (isLimitUsageByOnce()) {
setAlreadySelectedModes(source, game); setOnceSelectedModes(source, game);
} }
return this.selectedModes.size() >= this.getMinModes() return this.selectedModes.size() >= this.getMinModes()
|| (this.selectedModes.size() == 0 && mayChooseNone); || (this.selectedModes.size() == 0 && mayChooseNone);
@ -347,10 +363,10 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
currentMode = choice; currentMode = choice;
} }
} }
if (isEachModeOnlyOnce()) { if (isLimitUsageByOnce()) {
setAlreadySelectedModes(source, game); setOnceSelectedModes(source, game);
} }
if (modeChooser == TargetController.OPPONENT) { if (chooseController == TargetController.OPPONENT) {
selectedModes selectedModes
.stream() .stream()
.map(this::get) .map(this::get)
@ -359,12 +375,10 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
} }
} else { } else {
// only one mode available // only one mode available
if (currentMode == null) { this.clearSelectedModes();
this.clearSelectedModes(); Mode mode = this.values().iterator().next();
Mode mode = this.values().iterator().next(); this.addSelectedMode(mode.getId());
this.addSelectedMode(mode.getId()); this.setActiveMode(mode);
this.setActiveMode(mode);
}
} }
return true; return true;
} }
@ -375,18 +389,20 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
* @param source * @param source
* @param game * @param game
*/ */
private void setAlreadySelectedModes(Ability source, Game game) { private void setOnceSelectedModes(Ability source, Game game) {
for (UUID modeId : getSelectedModes()) { for (UUID modeId : getSelectedModes()) {
String key = getKey(source, game, modeId); String key = getSelectedModesKey(source, game, modeId);
game.getState().setValue(key, true); game.getState().setValue(key, true);
} }
} }
private void clearAlreadySelectedModes(Ability source, Game game) { private void clearAlreadySelectedModes(Ability source, Game game) {
// need full list to clear outdated data
for (UUID modeId : getAlreadySelectedModes(source, game, false)) { for (UUID modeId : getAlreadySelectedModes(source, game, false)) {
String key = getKey(source, game, modeId); String key = getSelectedModesKey(source, game, modeId);
game.getState().setValue(key, false); game.getState().setValue(key, false);
} }
setOnceTurnNum(game, source);
} }
/** /**
@ -400,15 +416,15 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
throw new IllegalArgumentException("Unknown modeId to select"); throw new IllegalArgumentException("Unknown modeId to select");
} }
if (selectedModes.contains(modeId) && eachModeMoreThanOnce) { if (selectedModes.contains(modeId) && mayChooseSameModeMoreThanOnce) {
Mode duplicateMode = get(modeId).copy(); Mode duplicateMode = get(modeId).copy();
UUID originalId = modeId; UUID originalId = modeId;
duplicateMode.setRandomId(); duplicateMode.setRandomId();
modeId = duplicateMode.getId(); modeId = duplicateMode.getId();
selectedDuplicateModes.put(modeId, duplicateMode); selectedDuplicateModes.put(modeId, duplicateMode);
selectedDuplicateToOriginalModeRefs.put(duplicateMode.getId(), originalId); selectedDuplicateToOriginalModeRefs.put(duplicateMode.getId(), originalId);
} }
// TODO: bugged and allows to choose same mode multiple times without mayChooseSameModeMoreThanOnce?
this.selectedModes.add(modeId); this.selectedModes.add(modeId);
} }
@ -418,39 +434,51 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
this.selectedDuplicateToOriginalModeRefs.remove(modeId); this.selectedDuplicateToOriginalModeRefs.remove(modeId);
} }
// The already once selected modes for a modal card are stored as a state value /**
// That's important for modal abilities with modes that can only selected once while the object stays in its zone * Return already selected modes, used for GUI and modal cards check
@SuppressWarnings("unchecked") * Can be outdated if each turn reset enabled
private Set<UUID> getAlreadySelectedModes(Ability source, Game game, boolean ignoreOldData) { * <p>
Set<UUID> onceSelectedModes = new HashSet<>(); * Warning, works with limitUsageByOnce only, other cards will not contain that info
if (ignoreOldData && this.isResetEachTurn() && getTurnNum(game, source) != game.getTurnNum()) { *
// Selected modes is not for current turn, so we ignore any value that may be there. * @param source
return onceSelectedModes; * @param game
* @param ignoreOutdatedData if true then return full selected modes (used in clear code on new turn)
* @return
*/
private Set<UUID> getAlreadySelectedModes(Ability source, Game game, boolean ignoreOutdatedData) {
Set<UUID> res = new HashSet<>();
// if selected modes is not for current turn, so we ignore any value that may be there
if (!ignoreOutdatedData && isAlreadySelectedModesOutdated(game, source)) {
return res;
} }
for (UUID modeId : this.keySet()) { for (UUID modeId : this.keySet()) {
Object exist = game.getState().getValue(getKey(source, game, modeId)); Object exist = game.getState().getValue(getSelectedModesKey(source, game, modeId));
if (exist == Boolean.TRUE) { if (exist == Boolean.TRUE) {
onceSelectedModes.add(modeId); res.add(modeId);
} }
} }
return onceSelectedModes; return res;
} }
// creates the key the selected modes are saved with to the state values private String getSelectedModesKey(Ability source, Game game, UUID modeId) {
private String getKey(Ability source, Game game, UUID modeId) {
return source.getSourceId().toString() + game.getState().getZoneChangeCounter(source.getSourceId()) + modeId.toString(); return source.getSourceId().toString() + game.getState().getZoneChangeCounter(source.getSourceId()) + modeId.toString();
} }
private static int getTurnNum(Game game, Ability source) { private String getOnceTurnNumKey(Ability source, Game game) {
String key = source.getSourceId().toString() + game.getState().getZoneChangeCounter(source.getSourceId()) + "turnNum"; return source.getSourceId().toString() + game.getState().getZoneChangeCounter(source.getSourceId()) + "turnNum";
Object object = game.getState().getValue(key); }
private int getOnceTurnNum(Game game, Ability source) {
Object object = game.getState().getValue(getOnceTurnNumKey(source, game));
if (object instanceof Integer) { if (object instanceof Integer) {
return (Integer) object; return (Integer) object;
} }
return 0; return 0;
} }
private static void setTurnNum(Game game, Ability source) { private void setOnceTurnNum(Game game, Ability source) {
String key = source.getSourceId().toString() + game.getState().getZoneChangeCounter(source.getSourceId()) + "turnNum"; String key = source.getSourceId().toString() + game.getState().getZoneChangeCounter(source.getSourceId()) + "turnNum";
game.getState().setValue(key, game.getTurnNum()); game.getState().setValue(key, game.getTurnNum());
} }
@ -465,13 +493,13 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
public List<Mode> getAvailableModes(Ability source, Game game) { public List<Mode> getAvailableModes(Ability source, Game game) {
List<Mode> availableModes = new ArrayList<>(); List<Mode> availableModes = new ArrayList<>();
Set<UUID> nonAvailableModes; Set<UUID> nonAvailableModes;
if (isEachModeMoreThanOnce()) { if (isMayChooseSameModeMoreThanOnce()) {
nonAvailableModes = new HashSet<>(); nonAvailableModes = new HashSet<>();
} else { } else {
nonAvailableModes = getAlreadySelectedModes(source, game, true); nonAvailableModes = getAlreadySelectedModes(source, game, true);
} }
for (Mode mode : this.values()) { for (Mode mode : this.values()) {
if (isEachModeOnlyOnce() && nonAvailableModes.contains(mode.getId())) { if (isLimitUsageByOnce() && nonAvailableModes.contains(mode.getId())) {
continue; continue;
} }
availableModes.add(mode); availableModes.add(mode);
@ -488,7 +516,7 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
sb.append("you may "); sb.append("you may ");
} }
if (this.chooseText == null) { if (this.chooseText == null) {
if (modeChooser == TargetController.OPPONENT) { if (chooseController == TargetController.OPPONENT) {
sb.append("an opponent chooses "); sb.append("an opponent chooses ");
} else { } else {
sb.append("choose "); sb.append("choose ");
@ -514,16 +542,16 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
sb.append(chooseText); sb.append(chooseText);
} }
if (isEachModeOnlyOnce() && this.getMaxModesFilter() == null) { if (isLimitUsageByOnce() && this.getMaxModesFilter() == null) {
sb.append(" that hasn't been chosen"); sb.append(" that hasn't been chosen");
} }
if (isResetEachTurn()) { if (isLimitUsageResetOnNewTurn()) {
sb.append(" this turn"); sb.append(" this turn");
} }
if (this.getMaxModesFilter() != null) { if (this.getMaxModesFilter() != null) {
sb.append(". Each mode must target ").append(getMaxModesFilter().getMessage()).append('.'); sb.append(". Each mode must target ").append(getMaxModesFilter().getMessage()).append('.');
} else if (isEachModeMoreThanOnce()) { } else if (isMayChooseSameModeMoreThanOnce()) {
sb.append(". You may choose the same mode more than once."); sb.append(". You may choose the same mode more than once.");
} else if (chooseText == null) { } else if (chooseText == null) {
sb.append(" &mdash;"); sb.append(" &mdash;");
@ -543,32 +571,32 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
return getText().replace("{this}", sourceName); return getText().replace("{this}", sourceName);
} }
public boolean isEachModeOnlyOnce() { public boolean isLimitUsageByOnce() {
return eachModeOnlyOnce; return limitUsageByOnce;
} }
public void setEachModeOnlyOnce(boolean eachModeOnlyOnce) { /**
this.eachModeOnlyOnce = eachModeOnlyOnce; * Limit modes usage to once per game or once per turn
*/
public void setLimitUsageByOnce(boolean resetOnNewTurn) {
this.limitUsageByOnce = true;
this.limitUsageResetOnNewTurn = resetOnNewTurn;
} }
public boolean isEachModeMoreThanOnce() { public boolean isMayChooseSameModeMoreThanOnce() {
return eachModeMoreThanOnce; return mayChooseSameModeMoreThanOnce;
} }
public void setEachModeMoreThanOnce(boolean eachModeMoreThanOnce) { public void setMayChooseSameModeMoreThanOnce(boolean mayChooseSameModeMoreThanOnce) {
this.eachModeMoreThanOnce = eachModeMoreThanOnce; this.mayChooseSameModeMoreThanOnce = mayChooseSameModeMoreThanOnce;
} }
public void setRandom(boolean isRandom) { public void setRandom(boolean isRandom) {
this.isRandom = isRandom; this.isRandom = isRandom;
} }
public boolean isResetEachTurn() { public boolean isLimitUsageResetOnNewTurn() {
return resetEachTurn; return limitUsageResetOnNewTurn;
}
public void setResetEachTurn(boolean resetEachTurn) {
this.resetEachTurn = resetEachTurn;
} }
public void setChooseText(String chooseText) { public void setChooseText(String chooseText) {

View file

@ -23,7 +23,7 @@ public enum ModesAlreadyUsedHint implements Hint {
public String getText(Game game, Ability ability) { public String getText(Game game, Ability ability) {
List<String> used = ability List<String> used = ability
.getModes() .getModes()
.streamAlreadySelected(ability, game) .streamAlreadySelectedModes(ability, game)
.map(Mode::getModeTag) .map(Mode::getModeTag)
.filter(tag -> tag != null && !tag.isEmpty()) .filter(tag -> tag != null && !tag.isEmpty())
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -33,7 +33,7 @@ public enum ModesAlreadyUsedHint implements Hint {
} }
return "Already used" return "Already used"
+ (ability.getModes().isResetEachTurn() ? " this turn" : "") + (ability.getModes().isLimitUsageResetOnNewTurn() ? " this turn" : "")
+ ": [" + String.join(", ", used) + "]"; + ": [" + String.join(", ", used) + "]";
} }

View file

@ -4300,9 +4300,10 @@ public abstract class PlayerImpl implements Player, Serializable {
} }
private void addModeOptions(List<Ability> options, Ability option, Game game) { private void addModeOptions(List<Ability> options, Ability option, Game game) {
// TODO: Support modal spells with more than one selectable mode // TODO: support modal spells with more than one selectable mode (also must use max modes filter)
for (Mode mode : option.getModes().values()) { for (Mode mode : option.getModes().values()) {
Ability newOption = option.copy(); Ability newOption = option.copy();
// TODO: bugged? Research option.getModes().isMayChooseSameModeMoreThanOnce() - is it affected here
newOption.getModes().clearSelectedModes(); newOption.getModes().clearSelectedModes();
newOption.getModes().addSelectedMode(mode.getId()); newOption.getModes().addSelectedMode(mode.getId());
newOption.getModes().setActiveMode(mode); newOption.getModes().setActiveMode(mode);