Added max targets limit support in TargetAmount

This commit is contained in:
Oleg Agafonov 2019-09-24 07:44:14 +04:00
parent 2daa9b6004
commit bad26b182a
2 changed files with 43 additions and 3 deletions

View file

@ -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();

View file

@ -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<UUID> possibleTargets(UUID sourceId, UUID sourceControllerId, Game game) {
Set<UUID> 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<UUID> possibleTargets(UUID sourceControllerId, Game game) {
Set<UUID> 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()