From bad26b182a19026a83cf76bb1b1cc6c6be409931 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Tue, 24 Sep 2019 07:44:14 +0400 Subject: [PATCH] Added max targets limit support in TargetAmount --- .../target/common/TargetAnyTargetAmount.java | 12 +++++-- .../common/TargetPermanentOrPlayerAmount.java | 34 ++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/Mage/src/main/java/mage/target/common/TargetAnyTargetAmount.java b/Mage/src/main/java/mage/target/common/TargetAnyTargetAmount.java index cb3fa0c8d4..544c8a6de3 100644 --- a/Mage/src/main/java/mage/target/common/TargetAnyTargetAmount.java +++ b/Mage/src/main/java/mage/target/common/TargetAnyTargetAmount.java @@ -21,16 +21,24 @@ public class TargetAnyTargetAmount extends TargetPermanentOrPlayerAmount { } public TargetAnyTargetAmount(int amount) { + this(amount, 0); + } + + public TargetAnyTargetAmount(int amount, int maxNumberOfTargets) { // 107.1c If a rule or ability instructs a player to choose “any number,” that player may choose // any positive number or zero, unless something (such as damage or counters) is being divided // or distributed among “any number” of players and/or objects. In that case, a nonzero number // of players and/or objects must be chosen if possible. - this(new StaticValue(amount)); + this(new StaticValue(amount), maxNumberOfTargets); this.minNumberOfTargets = 1; } public TargetAnyTargetAmount(DynamicValue amount) { - super(amount); + this(amount, 0); + } + + public TargetAnyTargetAmount(DynamicValue amount, int maxNumberOfTargets) { + super(amount, maxNumberOfTargets); this.zone = Zone.ALL; this.filter = defaultFilter; this.targetName = filter.getMessage(); diff --git a/Mage/src/main/java/mage/target/common/TargetPermanentOrPlayerAmount.java b/Mage/src/main/java/mage/target/common/TargetPermanentOrPlayerAmount.java index 623233dd23..adc75fae2c 100644 --- a/Mage/src/main/java/mage/target/common/TargetPermanentOrPlayerAmount.java +++ b/Mage/src/main/java/mage/target/common/TargetPermanentOrPlayerAmount.java @@ -4,7 +4,6 @@ import mage.MageObject; import mage.abilities.Ability; import mage.abilities.dynamicvalue.DynamicValue; import mage.filter.Filter; -import mage.filter.StaticFilters; import mage.filter.common.FilterPermanentOrPlayer; import mage.game.Game; import mage.game.permanent.Permanent; @@ -24,7 +23,12 @@ public abstract class TargetPermanentOrPlayerAmount extends TargetAmount { protected FilterPermanentOrPlayer filter; TargetPermanentOrPlayerAmount(DynamicValue amount) { + this(amount, 0); + } + + TargetPermanentOrPlayerAmount(DynamicValue amount, int maxNumberOfTargets) { super(amount); + this.maxNumberOfTargets = maxNumberOfTargets; } TargetPermanentOrPlayerAmount(final TargetPermanentOrPlayerAmount target) { @@ -39,6 +43,12 @@ public abstract class TargetPermanentOrPlayerAmount extends TargetAmount { @Override public boolean canTarget(UUID objectId, Game game) { + + // max targets limit reached (only selected can be choosen again) + if (getMaxNumberOfTargets() > 0 && getTargets().size() >= getMaxNumberOfTargets()) { + return getTargets().contains(objectId); + } + Permanent permanent = game.getPermanent(objectId); if (permanent != null) { return filter.match(permanent, game); @@ -49,6 +59,12 @@ public abstract class TargetPermanentOrPlayerAmount extends TargetAmount { @Override public boolean canTarget(UUID objectId, Ability source, Game game) { + + // max targets limit reached (only selected can be choosen again) + if (getMaxNumberOfTargets() > 0 && getTargets().size() >= getMaxNumberOfTargets()) { + return getTargets().contains(objectId); + } + Permanent permanent = game.getPermanent(objectId); Player player = game.getPlayer(objectId); @@ -77,6 +93,7 @@ public abstract class TargetPermanentOrPlayerAmount extends TargetAmount { @Override public boolean canChoose(UUID sourceId, UUID sourceControllerId, Game game) { + // no max targets limit here int count = 0; MageObject targetSource = game.getObject(sourceId); for (UUID playerId : game.getState().getPlayersInRange(sourceControllerId, game)) { @@ -105,6 +122,7 @@ public abstract class TargetPermanentOrPlayerAmount extends TargetAmount { @Override public boolean canChoose(UUID sourceControllerId, Game game) { + // no max targets limit here int count = 0; for (UUID playerId : game.getState().getPlayersInRange(sourceControllerId, game)) { Player player = game.getPlayer(playerId); @@ -128,6 +146,13 @@ public abstract class TargetPermanentOrPlayerAmount extends TargetAmount { @Override public Set possibleTargets(UUID sourceId, UUID sourceControllerId, Game game) { Set possibleTargets = new HashSet<>(); + + // max targets limit reached (only selected can be choosen again) + if (getMaxNumberOfTargets() > 0 && getTargets().size() >= getMaxNumberOfTargets()) { + possibleTargets.addAll(getTargets()); + return possibleTargets; + } + MageObject targetSource = game.getObject(sourceId); game.getState() @@ -155,6 +180,13 @@ public abstract class TargetPermanentOrPlayerAmount extends TargetAmount { @Override public Set possibleTargets(UUID sourceControllerId, Game game) { Set possibleTargets = new HashSet<>(); + + // max targets limit reached (only selected can be choosen again) + if (getMaxNumberOfTargets() > 0 && getTargets().size() >= getMaxNumberOfTargets()) { + possibleTargets.addAll(getTargets()); + return possibleTargets; + } + game.getState() .getPlayersInRange(sourceControllerId, game) .stream()