refactor: removed outdated Player::assignDamage by multi amount dialog, fixed getMultiAmount to work with min values, added additional checks

This commit is contained in:
Oleg Agafonov 2024-10-24 15:31:04 +04:00
parent 86fc0028c1
commit 2d9ac4e732
12 changed files with 50 additions and 127 deletions

View file

@ -2232,13 +2232,6 @@ public class ComputerPlayer extends PlayerImpl {
return null; return null;
} }
@Override
public void assignDamage(int damage, List<UUID> targets, String singleTargetName, UUID attackerId, Ability source, Game game) {
log.debug("assignDamage");
//TODO: improve this
game.getPermanent(targets.get(0)).damage(damage, attackerId, source, game);
}
@Override @Override
// TODO: add AI support with outcome and replace random with min/max // TODO: add AI support with outcome and replace random with min/max
public int getAmount(int min, int max, String message, Game game) { public int getAmount(int min, int max, String message, Game game) {
@ -2254,11 +2247,11 @@ public class ComputerPlayer extends PlayerImpl {
@Override @Override
public List<Integer> getMultiAmountWithIndividualConstraints(Outcome outcome, List<MultiAmountMessage> messages, public List<Integer> getMultiAmountWithIndividualConstraints(Outcome outcome, List<MultiAmountMessage> messages,
int min, int max, MultiAmountType type, Game game) { int totalMin, int totalMax, MultiAmountType type, Game game) {
log.debug("getMultiAmount"); log.debug("getMultiAmount");
int needCount = messages.size(); int needCount = messages.size();
List<Integer> defaultList = MultiAmountType.prepareDefaltValues(messages, min, max); List<Integer> defaultList = MultiAmountType.prepareDefaltValues(messages, totalMin, totalMax);
if (needCount == 0) { if (needCount == 0) {
return defaultList; return defaultList;
} }
@ -2273,7 +2266,7 @@ public class ComputerPlayer extends PlayerImpl {
// GOOD effect // GOOD effect
// values must be stable, so AI must able to simulate it and choose correct actions // values must be stable, so AI must able to simulate it and choose correct actions
// fill max values as much as possible // fill max values as much as possible
return MultiAmountType.prepareMaxValues(messages, min, max); return MultiAmountType.prepareMaxValues(messages, totalMin, totalMax);
} }
@Override @Override

View file

@ -390,38 +390,6 @@ public final class SimulatedPlayerMCTS extends MCTSPlayer {
return super.chooseBlockerOrder(blockers, combatGroup, blockerOrder, game); return super.chooseBlockerOrder(blockers, combatGroup, blockerOrder, game);
} }
@Override
public void assignDamage(int damage, List<UUID> targets, String singleTargetName, UUID attackerId, Ability source, Game game) {
if (this.isHuman()) {
int remainingDamage = damage;
UUID targetId;
int amount;
while (remainingDamage > 0) {
if (targets.size() == 1) {
targetId = targets.get(0);
amount = remainingDamage;
} else {
targetId = targets.get(RandomUtil.nextInt(targets.size()));
amount = RandomUtil.nextInt(damage + 1);
}
Permanent permanent = game.getPermanent(targetId);
if (permanent != null) {
permanent.damage(amount, attackerId, source, game, false, true);
remainingDamage -= amount;
} else {
Player player = game.getPlayer(targetId);
if (player != null) {
player.damage(amount, attackerId, source, game);
remainingDamage -= amount;
}
}
targets.remove(targetId);
}
} else {
super.assignDamage(damage, targets, singleTargetName, attackerId, source, game);
}
}
@Override @Override
public int getAmount(int min, int max, String message, Game game) { public int getAmount(int min, int max, String message, Game game) {
if (this.isHuman()) { if (this.isHuman()) {

View file

@ -41,7 +41,6 @@ import mage.target.Target;
import mage.target.TargetAmount; import mage.target.TargetAmount;
import mage.target.TargetCard; import mage.target.TargetCard;
import mage.target.TargetPermanent; import mage.target.TargetPermanent;
import mage.target.common.TargetAnyTarget;
import mage.target.common.TargetAttackingCreature; import mage.target.common.TargetAttackingCreature;
import mage.target.common.TargetDefender; import mage.target.common.TargetDefender;
import mage.target.targetpointer.TargetPointer; import mage.target.targetpointer.TargetPointer;
@ -1140,7 +1139,7 @@ public class HumanPlayer extends PlayerImpl {
} }
// ask and assign new amount // ask and assign new amount
List<Integer> targetValues = getMultiAmount(outcome, targetNames, 1, amountTotal, multiAmountType, game); List<Integer> targetValues = getMultiAmount(outcome, targetNames, 1, amountTotal, amountTotal, multiAmountType, game);
for (int i = 0; i < targetValues.size(); i++) { for (int i = 0; i < targetValues.size(); i++) {
int newAmount = targetValues.get(i); int newAmount = targetValues.get(i);
UUID targetId = targets.get(i); UUID targetId = targets.get(i);
@ -1927,7 +1926,7 @@ public class HumanPlayer extends PlayerImpl {
if (playerToAttack != null) { if (playerToAttack != null) {
playersToAttackIfAble.add(playerToAttack); playersToAttackIfAble.add(playerToAttack);
} }
if (effect.mustAttack(game)){ if (effect.mustAttack(game)) {
mustAttack = true; mustAttack = true;
} }
} }
@ -1967,10 +1966,10 @@ public class HumanPlayer extends PlayerImpl {
} }
} }
if (mustAttack && game.getCombat().getAttackers().isEmpty()){ if (mustAttack && game.getCombat().getAttackers().isEmpty()) {
// no attackers, but required to attack with something -- check if anything can attack // no attackers, but required to attack with something -- check if anything can attack
for (Permanent attacker : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, getId(), game)) { for (Permanent attacker : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, getId(), game)) {
if (attacker.canAttackInPrinciple(null, game)){ if (attacker.canAttackInPrinciple(null, game)) {
game.informPlayer(this, "You are forced to attack with at least one creature, e.g. " + attacker.getIdName() + "."); game.informPlayer(this, "You are forced to attack with at least one creature, e.g. " + attacker.getIdName() + ".");
return false; return false;
} }
@ -2206,33 +2205,6 @@ public class HumanPlayer extends PlayerImpl {
} }
} }
@Override
public void assignDamage(int damage, java.util.List<UUID> targets, String singleTargetName, UUID attackerId, Ability source, Game game) {
int remainingDamage = damage;
while (remainingDamage > 0 && canRespond()) {
Target target = new TargetAnyTarget();
target.withNotTarget(true);
if (singleTargetName != null) {
target.withTargetName(singleTargetName);
}
this.choose(Outcome.Damage, target, source, game);
if (targets.isEmpty() || targets.contains(target.getFirstTarget())) {
int damageAmount = this.getAmount(0, remainingDamage, "Select amount", game);
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent != null) {
permanent.damage(damageAmount, attackerId, source, game, false, true);
remainingDamage -= damageAmount;
} else {
Player player = game.getPlayer(target.getFirstTarget());
if (player != null) {
player.damage(damageAmount, attackerId, source, game);
remainingDamage -= damageAmount;
}
}
}
}
}
@Override @Override
public int getAmount(int min, int max, String message, Game game) { public int getAmount(int min, int max, String message, Game game) {
if (gameInCheckPlayableState(game)) { if (gameInCheckPlayableState(game)) {
@ -2262,15 +2234,16 @@ public class HumanPlayer extends PlayerImpl {
public List<Integer> getMultiAmountWithIndividualConstraints( public List<Integer> getMultiAmountWithIndividualConstraints(
Outcome outcome, Outcome outcome,
List<MultiAmountMessage> messages, List<MultiAmountMessage> messages,
int min, int totalMin,
int max, int totalMax,
MultiAmountType type, MultiAmountType type,
Game game Game game
) { ) {
int needCount = messages.size(); int needCount = messages.size();
List<Integer> defaultList = MultiAmountType.prepareDefaltValues(messages, min, max); List<Integer> defaultList = MultiAmountType.prepareDefaltValues(messages, totalMin, totalMax);
if (needCount == 0 || (needCount == 1 && min == max) if (needCount == 0 || (needCount == 1 && totalMin == totalMax)
|| messages.stream().map(m -> m.min == m.max).reduce(true, Boolean::logicalAnd)) { || messages.stream().map(m -> m.min == m.max).reduce(true, Boolean::logicalAnd)) {
// nothing to choose
return defaultList; return defaultList;
} }
@ -2288,19 +2261,19 @@ public class HumanPlayer extends PlayerImpl {
if (type.isCanCancel()) { if (type.isCanCancel()) {
options.put("canCancel", true); options.put("canCancel", true);
} }
game.fireGetMultiAmountEvent(playerId, messages, min, max, options); game.fireGetMultiAmountEvent(playerId, messages, totalMin, totalMax, options);
} }
waitForResponse(game); waitForResponse(game);
// waiting correct values only // waiting correct values only
if (response.getString() != null) { if (response.getString() != null) {
answer = MultiAmountType.parseAnswer(response.getString(), messages, min, max, false); answer = MultiAmountType.parseAnswer(response.getString(), messages, totalMin, totalMax, false);
if (MultiAmountType.isGoodValues(answer, messages, min, max)) { if (MultiAmountType.isGoodValues(answer, messages, totalMin, totalMax)) {
break; break;
} else { } else {
// it's not normal: can be cheater or a wrong GUI checks // it's not normal: can be cheater or a wrong GUI checks
answer = null; answer = null;
logger.error(String.format("GUI return wrong MultiAmountType values: %d %d %d - %s", needCount, min, max, response.getString())); logger.error(String.format("GUI return wrong MultiAmountType values: %d %d %d - %s", needCount, totalMin, totalMax, response.getString()));
game.informPlayer(this, "Error, you must enter correct values."); game.informPlayer(this, "Error, you must enter correct values.");
} }
} else if (type.isCanCancel() && response.getBoolean() != null) { } else if (type.isCanCancel() && response.getBoolean() != null) {
@ -2594,8 +2567,8 @@ public class HumanPlayer extends PlayerImpl {
modeText = Character.toUpperCase(modeText.charAt(0)) + modeText.substring(1); modeText = Character.toUpperCase(modeText.charAt(0)) + modeText.substring(1);
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (mode.getPawPrintValue() > 0){ if (mode.getPawPrintValue() > 0) {
for (int i = 0; i < mode.getPawPrintValue(); ++i){ for (int i = 0; i < mode.getPawPrintValue(); ++i) {
sb.append("{P}"); sb.append("{P}");
} }
sb.append(": "); sb.append(": ");
@ -2617,7 +2590,7 @@ public class HumanPlayer extends PlayerImpl {
// prepare dialog // prepare dialog
String message; String message;
if (modes.getMaxPawPrints() == 0){ if (modes.getMaxPawPrints() == 0) {
message = "Choose mode (selected " + modes.getSelectedModes().size() + " of " + modes.getMaxModes(game, source) message = "Choose mode (selected " + modes.getSelectedModes().size() + " of " + modes.getMaxModes(game, source)
+ ", min " + modes.getMinModes() + ")"; + ", min " + modes.getMinModes() + ")";
} else { } else {

View file

@ -73,7 +73,7 @@ class GrandWarlordRadhaEffect extends OneShotEffect {
if (controller == null || amount < 1) { if (controller == null || amount < 1) {
return false; return false;
} }
List<Integer> manaList = controller.getMultiAmount(this.outcome, Arrays.asList("R", "G"), 0, amount, MultiAmountType.MANA, game); List<Integer> manaList = controller.getMultiAmount(this.outcome, Arrays.asList("R", "G"), 0, amount, amount, MultiAmountType.MANA, game);
Mana mana = new Mana(); Mana mana = new Mana();
mana.add(new Mana(ColoredManaSymbol.R, manaList.get(0))); mana.add(new Mana(ColoredManaSymbol.R, manaList.get(0)));

View file

@ -94,7 +94,7 @@ class KlauthUnrivaledAncientEffect extends OneShotEffect {
.mapToInt(MageInt::getValue) .mapToInt(MageInt::getValue)
.sum(); .sum();
List<Integer> manaList = player.getMultiAmount( List<Integer> manaList = player.getMultiAmount(
outcome, manaSymbols, 0, attackerPower, MultiAmountType.MANA, game outcome, manaSymbols, 0, attackerPower, attackerPower, MultiAmountType.MANA, game
); );
player.getManaPool().addMana( player.getManaPool().addMana(
new KlauthUnrivaledAncientConditionalMana(manaList), game, source, true new KlauthUnrivaledAncientConditionalMana(manaList), game, source, true

View file

@ -2875,11 +2875,11 @@ public class TestPlayer implements Player {
@Override @Override
public List<Integer> getMultiAmountWithIndividualConstraints(Outcome outcome, List<MultiAmountMessage> messages, public List<Integer> getMultiAmountWithIndividualConstraints(Outcome outcome, List<MultiAmountMessage> messages,
int min, int max, MultiAmountType type, Game game) { int totalMin, int totalMax, MultiAmountType type, Game game) {
assertAliasSupportInChoices(false); assertAliasSupportInChoices(false);
int needCount = messages.size(); int needCount = messages.size();
List<Integer> defaultList = MultiAmountType.prepareDefaltValues(messages, min, max); List<Integer> defaultList = MultiAmountType.prepareDefaltValues(messages, totalMin, totalMax);
if (needCount == 0) { if (needCount == 0) {
return defaultList; return defaultList;
} }
@ -2905,7 +2905,7 @@ public class TestPlayer implements Player {
} }
// extra check // extra check
if (!MultiAmountType.isGoodValues(answer, messages, min, max)) { if (!MultiAmountType.isGoodValues(answer, messages, totalMin, totalMax)) {
Assert.fail("Wrong choices in multi amount: " + answer Assert.fail("Wrong choices in multi amount: " + answer
.stream() .stream()
.map(String::valueOf) .map(String::valueOf)
@ -2916,7 +2916,7 @@ public class TestPlayer implements Player {
} }
this.chooseStrictModeFailed("choice", game, "Multi amount: " + type.getHeader()); this.chooseStrictModeFailed("choice", game, "Multi amount: " + type.getHeader());
return computerPlayer.getMultiAmountWithIndividualConstraints(outcome, messages, min, max, type, game); return computerPlayer.getMultiAmountWithIndividualConstraints(outcome, messages, totalMin, totalMax, type, game);
} }
@Override @Override
@ -4384,14 +4384,6 @@ public class TestPlayer implements Player {
return computerPlayer.chooseBlockerOrder(blockers, combatGroup, blockerOrder, game); return computerPlayer.chooseBlockerOrder(blockers, combatGroup, blockerOrder, game);
} }
@Override
public void assignDamage(int damage, List<UUID> targets,
String singleTargetName, UUID attackerId, Ability source,
Game game
) {
computerPlayer.assignDamage(damage, targets, singleTargetName, attackerId, source, game);
}
@Override @Override
public void sideboard(Match match, Deck deck public void sideboard(Match match, Deck deck
) { ) {

View file

@ -104,7 +104,7 @@ public class AddConditionalManaOfAnyColorEffect extends ManaEffect {
manaStrings.add("B"); manaStrings.add("B");
manaStrings.add("R"); manaStrings.add("R");
manaStrings.add("G"); manaStrings.add("G");
List<Integer> choices = controller.getMultiAmount(this.outcome, manaStrings, 0, value, MultiAmountType.MANA, game); List<Integer> choices = controller.getMultiAmount(this.outcome, manaStrings, 0, value, value, MultiAmountType.MANA, game);
Mana mana = new Mana(choices.get(0), choices.get(1), choices.get(2), choices.get(3), choices.get(4), 0, 0, 0); Mana mana = new Mana(choices.get(0), choices.get(1), choices.get(2), choices.get(3), choices.get(4), 0, 0, 0);
return manaBuilder.setMana(mana, source, game).build(); return manaBuilder.setMana(mana, source, game).build();
} }

View file

@ -117,7 +117,7 @@ public class AddManaInAnyCombinationEffect extends ManaEffect {
// Ask player for color distribution // Ask player for color distribution
int manaAmount = amount.calculate(game, source, this); int manaAmount = amount.calculate(game, source, this);
List<Integer> manaList = player.getMultiAmount(this.outcome, manaStrings, 0, manaAmount, MultiAmountType.MANA, game); List<Integer> manaList = player.getMultiAmount(this.outcome, manaStrings, 0, manaAmount, manaAmount, MultiAmountType.MANA, game);
// Convert choices to mana // Convert choices to mana
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {

View file

@ -159,7 +159,7 @@ public class DynamicManaEffect extends ManaEffect {
manaStrings.add("B"); manaStrings.add("B");
manaStrings.add("R"); manaStrings.add("R");
manaStrings.add("G"); manaStrings.add("G");
List<Integer> choices = controller.getMultiAmount(this.outcome, manaStrings, 0, count, MultiAmountType.MANA, game); List<Integer> choices = controller.getMultiAmount(this.outcome, manaStrings, 0, count, 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)); computedMana.add(new Mana(choices.get(0), choices.get(1), choices.get(2), choices.get(3), choices.get(4), 0, 0, 0));
} }
} }

View file

@ -140,7 +140,7 @@ public enum MultiAmountType {
return res; return res;
} }
public static boolean isGoodValues(List<Integer> values, List<MultiAmountMessage> constraints, int min, int max) { public static boolean isGoodValues(List<Integer> values, List<MultiAmountMessage> constraints, int totalMin, int totalMax) {
if (values.size() != constraints.size()) { if (values.size() != constraints.size()) {
return false; return false;
} }
@ -156,7 +156,7 @@ public enum MultiAmountType {
currentSum += value; currentSum += value;
} }
return currentSum >= min && currentSum <= max; return currentSum >= totalMin && currentSum <= totalMax;
} }
public static List<Integer> parseAnswer(String answerToParse, List<MultiAmountMessage> constraints, int min, public static List<Integer> parseAnswer(String answerToParse, List<MultiAmountMessage> constraints, int min,

View file

@ -413,7 +413,7 @@ public interface Player extends MageItem, Copyable<Player> {
/** /**
* Draw cards. If you call it in replace events then use method with event param instead (for appliedEffects) * Draw cards. If you call it in replace events then use method with event param instead (for appliedEffects)
* *
* @param num cards to draw * @param num cards to draw
* @param source can be null for game default draws (non effects, example: start of the turn) * @param source can be null for game default draws (non effects, example: start of the turn)
* @return number of cards drawn, including as a result of replacement effects * @return number of cards drawn, including as a result of replacement effects
*/ */
@ -422,7 +422,7 @@ public interface Player extends MageItem, Copyable<Player> {
/** /**
* Draw cards with applied effects, for replaceEvent * Draw cards with applied effects, for replaceEvent
* *
* @param num cards to draw * @param num cards to draw
* @param source can be null for game default draws (non effects, example: start of the turn) * @param source can be null for game default draws (non effects, example: start of the turn)
* @param event original draw event in replacement code * @param event original draw event in replacement code
* @return number of cards drawn, including as a result of replacement effects * @return number of cards drawn, including as a result of replacement effects
@ -760,26 +760,28 @@ public interface Player extends MageItem, Copyable<Player> {
*/ */
UUID chooseBlockerOrder(List<Permanent> blockers, CombatGroup combatGroup, List<UUID> blockerOrder, Game game); UUID chooseBlockerOrder(List<Permanent> blockers, CombatGroup combatGroup, List<UUID> blockerOrder, Game game);
void assignDamage(int damage, List<UUID> targets, String singleTargetName, UUID attackerId, Ability source, Game game);
int getAmount(int min, int max, String message, Game game); int getAmount(int min, int max, String message, Game game);
/** /**
* Player distributes amount among multiple options * Player distributes amount among multiple options
* *
* @param outcome AI hint * @param outcome AI hint
* @param messages List of options to distribute amount among * @param messages List of options to distribute amount among
* @param min Minimum value per option * @param optionMin Minimum value per option
* @param max Total amount to be distributed * @param totalMin Minimum total amount to be distributed
* @param type MultiAmountType enum to set dialog options such as title and header * @param totalMax Total amount to be distributed
* @param game Game * @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. * @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) { default List<Integer> getMultiAmount(Outcome outcome, List<String> messages, int optionMin, int totalMin, int totalMax, MultiAmountType type, Game game) {
if (optionMin > totalMax || optionMin * messages.size() > totalMin) {
throw new IllegalArgumentException(String.format("Wrong code usage: getMultiAmount found bad option min/max values: %d/%d", optionMin, totalMax));
}
List<MultiAmountMessage> constraints = messages.stream() List<MultiAmountMessage> constraints = messages.stream()
.map(s -> new MultiAmountMessage(s, min, max)) .map(s -> new MultiAmountMessage(s, optionMin, totalMax))
.collect(Collectors.toList()); .collect(Collectors.toList());
return getMultiAmountWithIndividualConstraints(outcome, constraints, min, max, type, game); return getMultiAmountWithIndividualConstraints(outcome, constraints, totalMin, totalMax, type, game);
} }
/** /**
@ -787,13 +789,13 @@ public interface Player extends MageItem, Copyable<Player> {
* *
* @param outcome AI hint * @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 messages List of options to distribute amount among. Each option has a constraint on the min, max chosen for it
* @param min Total minimum amount to be distributed * @param totalMin Total minimum amount to be distributed
* @param max Total amount to be distributed * @param totalMax Total amount to be distributed
* @param type MultiAmountType enum to set dialog options such as title and header * @param type MultiAmountType enum to set dialog options such as title and header
* @param game Game * @param game Game
* @return List of integers with size equal to messages.size(). The sum of the integers is equal to max. * @return List of integers with size equal to messages.size(). The sum of the integers is equal to max.
*/ */
List<Integer> getMultiAmountWithIndividualConstraints(Outcome outcome, List<MultiAmountMessage> messages, int min, int max, MultiAmountType type, Game game); List<Integer> getMultiAmountWithIndividualConstraints(Outcome outcome, List<MultiAmountMessage> messages, int totalMin, int totalMax, MultiAmountType type, Game game);
void sideboard(Match match, Deck deck); void sideboard(Match match, Deck deck);

View file

@ -200,11 +200,6 @@ public class StubPlayer extends PlayerImpl {
return null; return null;
} }
@Override
public void assignDamage(int damage, List<UUID> targets, String singleTargetName, UUID attackerId, Ability source, Game game) {
}
@Override @Override
public int getAmount(int min, int max, String message, Game game) { public int getAmount(int min, int max, String message, Game game) {
return 0; return 0;
@ -212,7 +207,7 @@ public class StubPlayer extends PlayerImpl {
@Override @Override
public List<Integer> getMultiAmountWithIndividualConstraints(Outcome outcome, List<MultiAmountMessage> messages, public List<Integer> getMultiAmountWithIndividualConstraints(Outcome outcome, List<MultiAmountMessage> messages,
int min, int max, MultiAmountType type, Game game) { int totalMin, int totalMax, MultiAmountType type, Game game) {
return null; return null;
} }