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

View file

@ -46,8 +46,7 @@ public final class BreechesEagerPillager extends CardImpl {
new CreateTokenEffect(new TreasureToken()), false, filter
);
ability.setModeTag("treasure");
ability.getModes().setEachModeOnlyOnce(true);
ability.getModes().setResetEachTurn(true);
ability.getModes().setLimitUsageByOnce(true);
// * Target creature can't block this turn.
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.
this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true);
this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// Proliferate.
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.
this.getSpellAbility().getModes().setMinModes(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.
this.getSpellAbility().addEffect(new CabarettiConfluenceEffect());

View file

@ -34,7 +34,7 @@ public final class CaptiveAudience extends CardImpl {
new SetPlayerLifeSourceEffect(4), TargetController.YOU, false
);
ability.setModeTag("life total becomes 4");
ability.getModes().setEachModeOnlyOnce(true);
ability.getModes().setLimitUsageByOnce(false);
// Discard your hand.
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;
Ability ability = new BeginningOfUpkeepTriggeredAbility(new DamageTargetEffect(4), TargetController.YOU, false);
ability.setModeTag("deals damage and gain life");
ability.getModes().setEachModeOnlyOnce(true);
ability.getModes().setLimitUsageByOnce(false);
ability.addTarget(new TargetAnyTarget());
Effect effect = new GainLifeEffect(4);
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}");
// An opponent chooses one
this.getSpellAbility().getModes().setModeChooser(TargetController.OPPONENT);
this.getSpellAbility().getModes().setChooseController(TargetController.OPPONENT);
// 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.
this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true);
this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// - Fiery Confluence deals 1 damage to each creature;
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.
Ability ability = new AllianceAbility(new AddCountersSourceEffect(CounterType.P1P1.createInstance()));
ability.setModeTag("put +1/+1 counter");
ability.getModes().setEachModeOnlyOnce(true);
ability.getModes().setResetEachTurn(true);
ability.getModes().setLimitUsageByOnce(true);
// Create a tapped Treasure token.
ability.addMode(

View file

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

View file

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

View file

@ -44,7 +44,7 @@ public final class HenrikaDomnathi extends CardImpl {
1, StaticFilters.FILTER_CONTROLLED_CREATURE_SHORT_TEXT
), TargetController.YOU, false);
ability.setModeTag("each player sacrifice");
ability.getModes().setEachModeOnlyOnce(true);
ability.getModes().setLimitUsageByOnce(false);
// You draw a card and you lose 1 life.
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)
);
ability.setModeTag("gets +1/+1");
ability.getModes().setEachModeOnlyOnce(true);
ability.getModes().setResetEachTurn(true);
ability.getModes().setLimitUsageByOnce(true);
// Target creature becomes a Coward until end of turn.
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}");
// 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;
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.
this.getSpellAbility().getModes().setMinModes(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.
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}");
// 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.
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.
this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true);
this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// - Counter target spell unless its controller pays {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.
this.getSpellAbility().getModes().setMinModes(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.
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.
this.getSpellAbility().getModes().setMinModes(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.
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.
this.getSpellAbility().getModes().setMinModes(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;
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.
this.getSpellAbility().getModes().setMinModes(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.
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
);
ability.setModeTag("draw");
ability.getModes().setEachModeOnlyOnce(true);
ability.getModes().setLimitUsageByOnce(false);
// * Each opponent loses 2 life and you gain 2 life.
ability.addMode(new Mode(new LoseLifeOpponentsEffect(2))

View file

@ -36,7 +36,7 @@ public final class ThreeBowlsOfPorridge extends CardImpl {
ability.addCost(new TapSourceCost());
ability.addTarget(new TargetCreaturePermanent());
ability.setModeTag("deals damage");
ability.getModes().setEachModeOnlyOnce(true);
ability.getModes().setLimitUsageByOnce(false);
// * Tap target creature.
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.
this.getSpellAbility().getModes().setMinModes(5);
this.getSpellAbility().getModes().setMaxModes(5);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true);
this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// Target permanent phases out.
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.
this.getSpellAbility().getModes().setMinModes(3);
this.getSpellAbility().getModes().setMaxModes(3);
this.getSpellAbility().getModes().setEachModeMoreThanOnce(true);
this.getSpellAbility().getModes().setMayChooseSameModeMoreThanOnce(true);
// - Put two +1/+1 counters on target creature;
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().setMaxModes(3);
ability.getModes().setEachModeOnlyOnce(true);
ability.getModes().setLimitUsageByOnce(false);
ability.getModes().setMaxModesFilter(filter0);
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.
this.getSpellAbility().getModes().setMinModes(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;
Effect effect = new LoseLifeTargetEffect(1);

View file

@ -454,7 +454,7 @@ public class TestPlayer implements Player {
Mode selectedMode;
if (targetName.startsWith("mode=")) {
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());
}
UUID modeId = ability.getModes().getModeId(modeNr);

View file

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

View file

@ -12,6 +12,7 @@ import mage.game.Game;
import mage.players.Player;
import mage.target.common.TargetOpponent;
import mage.util.CardUtil;
import mage.util.Copyable;
import mage.util.RandomUtil;
import java.util.*;
@ -20,38 +21,40 @@ import java.util.stream.Stream;
/**
* @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
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");
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 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 int minModes;
private int maxModes;
private TargetController modeChooser;
private boolean eachModeMoreThanOnce; // each mode can be selected multiple times during one choice
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 isRandom = false;
private Filter maxModesFilter; // calculates the max number of available modes
private Condition moreCondition; // allows multiple modes choose (example: choose one... if condition, you may choose both)
private boolean limitUsageByOnce = false; // limit mode selection to once per game
private boolean limitUsageResetOnNewTurn = false; // reset once per game limit on new turn, example: Galadriel, Light of Valinor
private String chooseText = null;
private boolean resetEachTurn = false;
private Condition moreCondition;
private TargetController chooseController;
private boolean mayChooseSameModeMoreThanOnce = false; // example: choose three... you may choose the same mode more than once
private boolean mayChooseNone = false;
private boolean isRandom = false;
public Modes() {
// add default mode
this.currentMode = new Mode((Effect) null);
this.put(currentMode.getId(), currentMode);
this.minModes = 1;
this.maxModes = 1;
this.addSelectedMode(currentMode.getId());
this.modeChooser = TargetController.YOU;
this.eachModeOnlyOnce = false;
this.eachModeMoreThanOnce = false;
this.chooseController = TargetController.YOU;
}
protected Modes(final Modes modes) {
@ -65,25 +68,28 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
this.minModes = modes.minModes;
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.moreCondition = modes.moreCondition;
this.limitUsageByOnce = modes.limitUsageByOnce;
this.limitUsageResetOnNewTurn = modes.limitUsageResetOnNewTurn;
this.isRandom = modes.isRandom;
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()) {
this.currentMode = values().iterator().next();
} 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() {
return new Modes(this);
}
@ -91,21 +97,21 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
@Override
public Mode get(Object key) {
Mode modeToGet = super.get(key);
if (modeToGet == null && eachModeMoreThanOnce) {
if (modeToGet == null && mayChooseSameModeMoreThanOnce) {
modeToGet = selectedDuplicateModes.get(key);
}
return modeToGet;
}
public Stream<Mode> stream() {
return super.values().stream();
}
public Stream<Mode> streamAlreadySelected(Ability source, Game game) {
public Stream<Mode> streamAlreadySelectedModes(Ability source, Game game) {
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() {
return currentMode;
}
@ -119,7 +125,7 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
*/
public UUID getModeId(int index) {
int idx = 0;
if (eachModeMoreThanOnce) {
if (mayChooseSameModeMoreThanOnce) {
for (UUID modeId : this.getSelectedModes()) {
idx++;
if (idx == index) {
@ -137,6 +143,9 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
return null;
}
/**
* Return full list of selected modes in default/rules order (without multi selects)
*/
public List<UUID> getSelectedModes() {
// modes can be selected in any order by user, but execution must be in rule's order
List<UUID> res = new ArrayList<>(this.size());
@ -202,6 +211,11 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
}
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;
}
@ -223,7 +237,7 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
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 (this.maxModesFilter instanceof FilterPlayer) {
realMaxModes = 0;
@ -242,12 +256,12 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
return realMaxModes;
}
public void setModeChooser(TargetController modeChooser) {
this.modeChooser = modeChooser;
public void setChooseController(TargetController chooseController) {
this.chooseController = chooseController;
}
public TargetController getModeChooser() {
return this.modeChooser;
public TargetController getChooseController() {
return this.chooseController;
}
public void setActiveMode(Mode mode) {
@ -268,12 +282,14 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
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) {
if (this.isResetEachTurn()) {
if (getTurnNum(game, source) != game.getTurnNum()) {
this.clearAlreadySelectedModes(source, game);
setTurnNum(game, source);
}
if (isAlreadySelectedModesOutdated(game, source)) {
this.clearAlreadySelectedModes(source, game);
}
if (this.size() > 1) {
this.clearSelectedModes();
@ -295,19 +311,19 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
}
// check if all modes can be activated automatically
if (this.size() == this.getMinModes() && !isEachModeMoreThanOnce()) {
if (this.size() == this.getMinModes() && !isMayChooseSameModeMoreThanOnce()) {
Set<UUID> onceSelectedModes = null;
if (isEachModeOnlyOnce()) {
if (isLimitUsageByOnce()) {
onceSelectedModes = getAlreadySelectedModes(source, game, true);
}
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)) {
this.addSelectedMode(mode.getId());
}
}
if (isEachModeOnlyOnce()) {
setAlreadySelectedModes(source, game);
if (isLimitUsageByOnce()) {
setOnceSelectedModes(source, game);
}
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.
// 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;
if (modeChooser == TargetController.OPPONENT) {
if (chooseController == TargetController.OPPONENT) {
TargetOpponent targetOpponent = new TargetOpponent();
targetOpponent.choose(Outcome.Benefit, source.getControllerId(), source.getSourceId(), source, game);
playerId = targetOpponent.getFirstTarget();
@ -336,8 +352,8 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
while (this.selectedModes.size() < currentMaxModes) {
Mode choice = player.chooseMode(this, source, game);
if (choice == null) {
if (isEachModeOnlyOnce()) {
setAlreadySelectedModes(source, game);
if (isLimitUsageByOnce()) {
setOnceSelectedModes(source, game);
}
return this.selectedModes.size() >= this.getMinModes()
|| (this.selectedModes.size() == 0 && mayChooseNone);
@ -347,10 +363,10 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
currentMode = choice;
}
}
if (isEachModeOnlyOnce()) {
setAlreadySelectedModes(source, game);
if (isLimitUsageByOnce()) {
setOnceSelectedModes(source, game);
}
if (modeChooser == TargetController.OPPONENT) {
if (chooseController == TargetController.OPPONENT) {
selectedModes
.stream()
.map(this::get)
@ -359,12 +375,10 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
}
} else {
// only one mode available
if (currentMode == null) {
this.clearSelectedModes();
Mode mode = this.values().iterator().next();
this.addSelectedMode(mode.getId());
this.setActiveMode(mode);
}
this.clearSelectedModes();
Mode mode = this.values().iterator().next();
this.addSelectedMode(mode.getId());
this.setActiveMode(mode);
}
return true;
}
@ -375,18 +389,20 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
* @param source
* @param game
*/
private void setAlreadySelectedModes(Ability source, Game game) {
private void setOnceSelectedModes(Ability source, Game game) {
for (UUID modeId : getSelectedModes()) {
String key = getKey(source, game, modeId);
String key = getSelectedModesKey(source, game, modeId);
game.getState().setValue(key, true);
}
}
private void clearAlreadySelectedModes(Ability source, Game game) {
// need full list to clear outdated data
for (UUID modeId : getAlreadySelectedModes(source, game, false)) {
String key = getKey(source, game, modeId);
String key = getSelectedModesKey(source, game, modeId);
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");
}
if (selectedModes.contains(modeId) && eachModeMoreThanOnce) {
if (selectedModes.contains(modeId) && mayChooseSameModeMoreThanOnce) {
Mode duplicateMode = get(modeId).copy();
UUID originalId = modeId;
duplicateMode.setRandomId();
modeId = duplicateMode.getId();
selectedDuplicateModes.put(modeId, duplicateMode);
selectedDuplicateToOriginalModeRefs.put(duplicateMode.getId(), originalId);
}
// TODO: bugged and allows to choose same mode multiple times without mayChooseSameModeMoreThanOnce?
this.selectedModes.add(modeId);
}
@ -418,39 +434,51 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
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
@SuppressWarnings("unchecked")
private Set<UUID> getAlreadySelectedModes(Ability source, Game game, boolean ignoreOldData) {
Set<UUID> onceSelectedModes = new HashSet<>();
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.
return onceSelectedModes;
/**
* Return already selected modes, used for GUI and modal cards check
* Can be outdated if each turn reset enabled
* <p>
* Warning, works with limitUsageByOnce only, other cards will not contain that info
*
* @param source
* @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()) {
Object exist = game.getState().getValue(getKey(source, game, modeId));
Object exist = game.getState().getValue(getSelectedModesKey(source, game, modeId));
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 getKey(Ability source, Game game, UUID modeId) {
private String getSelectedModesKey(Ability source, Game game, UUID modeId) {
return source.getSourceId().toString() + game.getState().getZoneChangeCounter(source.getSourceId()) + modeId.toString();
}
private static int getTurnNum(Game game, Ability source) {
String key = source.getSourceId().toString() + game.getState().getZoneChangeCounter(source.getSourceId()) + "turnNum";
Object object = game.getState().getValue(key);
private String getOnceTurnNumKey(Ability source, Game game) {
return source.getSourceId().toString() + game.getState().getZoneChangeCounter(source.getSourceId()) + "turnNum";
}
private int getOnceTurnNum(Game game, Ability source) {
Object object = game.getState().getValue(getOnceTurnNumKey(source, game));
if (object instanceof Integer) {
return (Integer) object;
}
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";
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) {
List<Mode> availableModes = new ArrayList<>();
Set<UUID> nonAvailableModes;
if (isEachModeMoreThanOnce()) {
if (isMayChooseSameModeMoreThanOnce()) {
nonAvailableModes = new HashSet<>();
} else {
nonAvailableModes = getAlreadySelectedModes(source, game, true);
}
for (Mode mode : this.values()) {
if (isEachModeOnlyOnce() && nonAvailableModes.contains(mode.getId())) {
if (isLimitUsageByOnce() && nonAvailableModes.contains(mode.getId())) {
continue;
}
availableModes.add(mode);
@ -488,7 +516,7 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
sb.append("you may ");
}
if (this.chooseText == null) {
if (modeChooser == TargetController.OPPONENT) {
if (chooseController == TargetController.OPPONENT) {
sb.append("an opponent chooses ");
} else {
sb.append("choose ");
@ -514,16 +542,16 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
sb.append(chooseText);
}
if (isEachModeOnlyOnce() && this.getMaxModesFilter() == null) {
if (isLimitUsageByOnce() && this.getMaxModesFilter() == null) {
sb.append(" that hasn't been chosen");
}
if (isResetEachTurn()) {
if (isLimitUsageResetOnNewTurn()) {
sb.append(" this turn");
}
if (this.getMaxModesFilter() != null) {
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.");
} else if (chooseText == null) {
sb.append(" &mdash;");
@ -543,32 +571,32 @@ public class Modes extends LinkedHashMap<UUID, Mode> {
return getText().replace("{this}", sourceName);
}
public boolean isEachModeOnlyOnce() {
return eachModeOnlyOnce;
public boolean isLimitUsageByOnce() {
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() {
return eachModeMoreThanOnce;
public boolean isMayChooseSameModeMoreThanOnce() {
return mayChooseSameModeMoreThanOnce;
}
public void setEachModeMoreThanOnce(boolean eachModeMoreThanOnce) {
this.eachModeMoreThanOnce = eachModeMoreThanOnce;
public void setMayChooseSameModeMoreThanOnce(boolean mayChooseSameModeMoreThanOnce) {
this.mayChooseSameModeMoreThanOnce = mayChooseSameModeMoreThanOnce;
}
public void setRandom(boolean isRandom) {
this.isRandom = isRandom;
}
public boolean isResetEachTurn() {
return resetEachTurn;
}
public void setResetEachTurn(boolean resetEachTurn) {
this.resetEachTurn = resetEachTurn;
public boolean isLimitUsageResetOnNewTurn() {
return limitUsageResetOnNewTurn;
}
public void setChooseText(String chooseText) {

View file

@ -23,7 +23,7 @@ public enum ModesAlreadyUsedHint implements Hint {
public String getText(Game game, Ability ability) {
List<String> used = ability
.getModes()
.streamAlreadySelected(ability, game)
.streamAlreadySelectedModes(ability, game)
.map(Mode::getModeTag)
.filter(tag -> tag != null && !tag.isEmpty())
.collect(Collectors.toList());
@ -33,7 +33,7 @@ public enum ModesAlreadyUsedHint implements Hint {
}
return "Already used"
+ (ability.getModes().isResetEachTurn() ? " this turn" : "")
+ (ability.getModes().isLimitUsageResetOnNewTurn() ? " this turn" : "")
+ ": [" + 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) {
// 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()) {
Ability newOption = option.copy();
// TODO: bugged? Research option.getModes().isMayChooseSameModeMoreThanOnce() - is it affected here
newOption.getModes().clearSelectedModes();
newOption.getModes().addSelectedMode(mode.getId());
newOption.getModes().setActiveMode(mode);