Skip to content

Commit

Permalink
Improvements to player selection of TargetAmount (#11341)
Browse files Browse the repository at this point in the history
  • Loading branch information
xenohedron authored Oct 26, 2023
1 parent bd298d6 commit 18c6596
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void showDialog(List<MultiAmountMessage> messages, int min, int max, Map<
labelList.add(label);

JSpinner spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(0, messages.get(i).min, messages.get(i).max, 1));
spinner.setModel(new SpinnerNumberModel(messages.get(i).min, messages.get(i).min, messages.get(i).max, 1));
spinnerC.weightx = 0.5;
spinnerC.gridx = 1;
spinnerC.gridy = i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,14 @@ public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability
}

int amountTotal = target.getAmountTotal(game, source);
if (amountTotal == 0) {
return false; // nothing to distribute
}
MultiAmountType multiAmountType = source.getRule().contains("damage") ? MultiAmountType.DAMAGE : MultiAmountType.P1P1;

// 601.2d. If the spell requires the player to divide or distribute an effect (such as damage or counters)
// among one or more targets, the player announces the division.
// Each of these targets must receive at least one of whatever is being divided.

// Two steps logic:
// 1. Select targets
Expand All @@ -896,7 +904,7 @@ public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability
// 1. Select targets
while (canRespond()) {
Set<UUID> possibleTargetIds = target.possibleTargets(abilityControllerId, source, game);
boolean required = target.isRequired(source != null ? source.getSourceId() : null, game);
boolean required = target.isRequired(source.getSourceId(), game);
if (possibleTargetIds.isEmpty()
|| target.getSize() >= target.getNumberOfTargets()) {
required = false;
Expand Down Expand Up @@ -928,9 +936,9 @@ public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability
updateGameStatePriority("chooseTargetAmount", game);
prepareForResponse(game);
if (!isExecutingMacro()) {
// target amount uses for damage only, if you see another use case then message must be changed here and on getMultiAmount call

game.fireSelectTargetEvent(playerId, new MessageToClient(target.getMessage(), getRelatedObjectName(source, game)), possibleTargetIds, required, options);
String multiType = multiAmountType == MultiAmountType.DAMAGE ? " to divide %d damage" : " to distribute %d counters";
String message = target.getMessage() + String.format(multiType, amountTotal);
game.fireSelectTargetEvent(playerId, new MessageToClient(message, getRelatedObjectName(source, game)), possibleTargetIds, required, options);
}
waitForResponse(game);

Expand All @@ -941,7 +949,9 @@ public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability
if (target.contains(responseId)) {
// unselect
target.remove(responseId);
} else if (possibleTargetIds.contains(responseId) && target.canTarget(abilityControllerId, responseId, source, game)) {
} else if (possibleTargetIds.contains(responseId)
&& target.canTarget(abilityControllerId, responseId, source, game)
&& target.getSize() < amountTotal) {
// select
target.addTarget(responseId, source, game);
}
Expand All @@ -958,6 +968,26 @@ public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability

// 2. Distribute amount between selected targets

// if only one target, it gets full amount, no possible choice
if (targets.size() == 1) {
target.setTargetAmount(targets.get(0), amountTotal, source, game);
return true;
}

// if number of targets equal to amount, each get 1, no possible choice
if (targets.size() == amountTotal) {
for (UUID targetId : targets) {
target.setTargetAmount(targetId, 1, source, game);
}
return true;
}

// should not be able to have more targets than amount, but in such case it's illegal
if (targets.size() > amountTotal) {
target.clearChosen();
return false;
}

// prepare targets list with p/t or life stats (cause that's dialog used for damage distribute)
List<String> targetNames = new ArrayList<>();
for (UUID targetId : targets) {
Expand All @@ -978,7 +1008,6 @@ public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability
}
}

MultiAmountType multiAmountType = source.toString().contains("counters") ? MultiAmountType.P1P1 : MultiAmountType.DAMAGE;
// ask and assign new amount
List<Integer> targetValues = getMultiAmount(outcome, targetNames, 1, amountTotal, multiAmountType, game);
for (int i = 0; i < targetValues.size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion Mage.Sets/src/mage/cards/k/KlauthUnrivaledAncient.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public boolean apply(Game game, Ability source) {
.mapToInt(MageInt::getValue)
.sum();
List<Integer> manaList = player.getMultiAmount(
outcome, manaSymbols, attackerPower, attackerPower, MultiAmountType.MANA, game
outcome, manaSymbols, 0, attackerPower, MultiAmountType.MANA, game
);
player.getManaPool().addMana(
new KlauthUnrivaledAncientConditionalMana(manaList), game, source, true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Mana produceMana(Game game, Ability source) {

// Ask player for color distribution
int manaAmount = amount.calculate(game, source, this);
List<Integer> manaList = player.getMultiAmount(this.outcome, manaStrings, manaAmount, manaAmount, MultiAmountType.MANA, game);
List<Integer> manaList = player.getMultiAmount(this.outcome, manaStrings, 0, manaAmount, MultiAmountType.MANA, game);

// Convert choices to mana
for (int i = 0; i < size; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Mana produceMana(Game game, Ability source) {
manaStrings.add("B");
manaStrings.add("R");
manaStrings.add("G");
List<Integer> choices = controller.getMultiAmount(this.outcome, manaStrings, count, count, MultiAmountType.MANA, game);
List<Integer> choices = controller.getMultiAmount(this.outcome, manaStrings, 0, count, MultiAmountType.MANA, game);
computedMana.add(new Mana(choices.get(0), choices.get(1), choices.get(2), choices.get(3), choices.get(4), 0, 0, 0));
}
}
Expand Down
9 changes: 5 additions & 4 deletions Mage/src/main/java/mage/players/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,8 +758,9 @@ default int announceXMana(int min, int max, String message, Game game, Ability a
* @return List of integers with size equal to messages.size(). The sum of the integers is equal to max.
*/
default List<Integer> getMultiAmount(Outcome outcome, List<String> messages, int min, int max, MultiAmountType type, Game game) {
List<MultiAmountMessage> constraints = messages.stream().map(s -> new MultiAmountMessage(s, 0, max)).collect(Collectors.toList());

List<MultiAmountMessage> constraints = messages.stream()
.map(s -> new MultiAmountMessage(s, min, max))
.collect(Collectors.toList());
return getMultiAmountWithIndividualConstraints(outcome, constraints, min, max, type, game);
}

Expand All @@ -768,8 +769,8 @@ default List<Integer> getMultiAmount(Outcome outcome, List<String> messages, int
*
* @param outcome AI hint
* @param messages List of options to distribute amount among. Each option has a constraint on the min, max chosen for it
* @param totalMin Total minimum amount to be distributed
* @param totalMax Total amount to be distributed
* @param min Total minimum amount to be distributed
* @param max Total amount to be distributed
* @param type MultiAmountType enum to set dialog options such as title and header
* @param game Game
* @return List of integers with size equal to messages.size(). The sum of the integers is equal to max.
Expand Down

0 comments on commit 18c6596

Please sign in to comment.