other: reworked target selection: (#13638)

- WIP: AI and multi targets, human and X=0 use cases, human and impossible targets use cases;
- improved stability and shared logic (related to #13606, #11134, #11666, continue from a53eb66b58, close #13617, close #13613);
- improved test logs and debug info to show more target info on errors;
- improved test framework to support multiple addTarget calls;
- improved test framework to find bad commands order for targets (related to #11666);
- fixed game freezes on auto-choice usages with disconnected or under control players (related to #11285);
- gui, game: fixed that player doesn't mark avatar as selected/green in "up to" targeting;
- gui, game: fixed small font in some popup messages on big screens (related to #969);
- gui, game: added min targets info for target selection dialog;
- for devs: added new cheat option to call and test any game dialog (define own dialogs, targets, etc in HumanDialogsTester);
- for devs: now tests require complete an any or up to target selection by addTarget + TestPlayer.TARGET_SKIP or setChoice + TestPlayer.CHOICE_SKIP (if not all max/possible targets used);
- for devs: added detail targets info for activate/trigger/cast, can be useful to debug unit tests, auto-choose or AI (see DebugUtil.GAME_SHOW_CHOOSE_TARGET_LOGS)
This commit is contained in:
Oleg Agafonov 2025-05-16 13:55:54 +04:00 committed by GitHub
parent 80d62727e1
commit 133e4fe425
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
84 changed files with 2737 additions and 743 deletions

View file

@ -173,11 +173,14 @@ public abstract class TargetImpl implements Target {
if (getMaxNumberOfTargets() != 1) {
StringBuilder sb = new StringBuilder();
sb.append("Select ").append(targetName);
sb.append(" (selected ").append(targets.size());
if (getMaxNumberOfTargets() > 0 && getMaxNumberOfTargets() != Integer.MAX_VALUE) {
sb.append(" (selected ").append(targets.size()).append(" of ").append(getMaxNumberOfTargets()).append(')');
} else {
sb.append(" (selected ").append(targets.size()).append(')');
sb.append(" of ").append(getMaxNumberOfTargets());
}
if (getMinNumberOfTargets() > 0) {
sb.append(", min ").append(getMinNumberOfTargets());
}
sb.append(')');
sb.append(suffix);
return sb.toString();
}
@ -245,12 +248,12 @@ public abstract class TargetImpl implements Target {
// limit by max amount
if (getMaxNumberOfTargets() > 0 && targets.size() > getMaxNumberOfTargets()) {
return chosen;
return false;
}
// limit by min amount
if (getMinNumberOfTargets() > 0 && targets.size() < getMinNumberOfTargets()) {
return chosen;
return false;
}
// all fine
@ -258,8 +261,44 @@ public abstract class TargetImpl implements Target {
}
@Override
public boolean doneChoosing(Game game) {
return isChoiceSelected() && isChosen(game);
@Deprecated // TODO: replace usage in cards by full version from choose methods
public boolean isChoiceCompleted(Game game) {
return isChoiceCompleted(null, null, game);
}
@Override
public boolean isChoiceCompleted(UUID abilityControllerId, Ability source, Game game) {
// make sure target request called one time minimum (for "up to" targets)
// choice is selected after any addTarget call (by test, AI or human players)
if (!isChoiceSelected()) {
return false;
}
// make sure selected targets are valid
if (!isChosen(game)) {
return false;
}
// make sure to auto-finish on all targets selection
// - human player can select and deselect targets until fill all targets amount or press done button
// - AI player can select all new targets as much as possible
if (getMaxNumberOfTargets() > 0) {
if (getMaxNumberOfTargets() == Integer.MAX_VALUE) {
if (abilityControllerId != null && source != null) {
// any amount - nothing to choose
return this.getSize() >= this.possibleTargets(abilityControllerId, source, game).size();
} else {
// any amount - any selected
return this.getSize() > 0;
}
} else {
// check selected limit
return this.getSize() >= getMaxNumberOfTargets();
}
}
// all other use cases are fine
return true;
}
@Override
@ -289,7 +328,7 @@ public abstract class TargetImpl implements Target {
@Override
public void remove(UUID id) {
if (targets.containsKey(id)) {
targets.remove(id);
targets.remove(id); // TODO: miss chosen update here?
zoneChangeCounters.remove(id);
}
}
@ -315,6 +354,8 @@ public abstract class TargetImpl implements Target {
}
} else {
targets.put(id, 0);
rememberZoneChangeCounter(id, game);
chosen = isChosen(game);
}
}
}
@ -366,20 +407,45 @@ public abstract class TargetImpl implements Target {
return false;
}
UUID abilityControllerId = playerId;
if (this.getTargetController() != null && this.getAbilityController() != null) {
abilityControllerId = this.getAbilityController();
}
chosen = false;
do {
if (!targetController.canRespond()) {
chosen = isChosen(game);
return chosen;
}
if (!targetController.choose(outcome, this, source, game)) {
chosen = isChosen(game);
return chosen;
}
chosen = isChosen(game);
} while (!doneChoosing(game));
int prevTargetsCount = this.getTargets().size();
return isChosen(game);
// stop by disconnect
if (!targetController.canRespond()) {
break;
}
// stop by cancel/done
if (!targetController.choose(outcome, this, source, game)) {
break;
}
// TODO: miss auto-choose code? see chooseTarget below
// TODO: miss random code? see chooseTarget below
chosen = isChosen(game);
// stop by full complete
if (isChoiceCompleted(abilityControllerId, source, game)) {
break;
}
// stop by nothing to use (actual for human and done button)
if (prevTargetsCount == this.getTargets().size()) {
break;
}
// can select next target
} while (true);
chosen = isChosen(game);
return this.getTargets().size() > 0;
}
@Override
@ -389,44 +455,80 @@ public abstract class TargetImpl implements Target {
return false;
}
List<UUID> possibleTargets = new ArrayList<>(possibleTargets(playerId, source, game));
UUID abilityControllerId = playerId;
if (this.getTargetController() != null && this.getAbilityController() != null) {
abilityControllerId = this.getAbilityController();
}
List<UUID> randomPossibleTargets = new ArrayList<>(possibleTargets(playerId, source, game));
chosen = false;
do {
int prevTargetsCount = this.getTargets().size();
// stop by disconnect
if (!targetController.canRespond()) {
chosen = isChosen(game);
return chosen;
break;
}
// MAKE A CHOICE
if (isRandom()) {
if (possibleTargets.isEmpty()) {
chosen = isChosen(game);
return chosen;
// random choice
// stop on nothing to choose
if (randomPossibleTargets.isEmpty()) {
break;
}
// find valid target
while (!possibleTargets.isEmpty()) {
int index = RandomUtil.nextInt(possibleTargets.size());
if (this.canTarget(playerId, possibleTargets.get(index), source, game)) {
this.addTarget(possibleTargets.get(index), source, game);
possibleTargets.remove(index);
// add valid random target one by one
while (!randomPossibleTargets.isEmpty()) {
UUID possibleTarget = RandomUtil.randomFromCollection(randomPossibleTargets);
if (this.canTarget(playerId, possibleTarget, source, game) && !this.contains(possibleTarget)) {
this.addTarget(possibleTarget, source, game);
randomPossibleTargets.remove(possibleTarget);
break;
} else {
possibleTargets.remove(index);
randomPossibleTargets.remove(possibleTarget);
}
}
// continue to next target
} else {
// Try to autochoosen
// player's choice
UUID autoChosenId = tryToAutoChoose(playerId, source, game);
if (autoChosenId != null) {
if (autoChosenId != null && !this.contains(autoChosenId)) {
// auto-choose
addTarget(autoChosenId, source, game);
} else if (!targetController.chooseTarget(outcome, this, source, game)) { // If couldn't autochoose ask player
chosen = isChosen(game);
return chosen;
// continue to next target (example: auto-choose must fill min/max = 2 from 2 possible cards)
} else {
// manual
// stop by cancel/done
if (!targetController.chooseTarget(outcome, this, source, game)) {
break;
}
// continue to next target
}
}
chosen = isChosen(game);
} while (!doneChoosing(game));
return isChosen(game);
chosen = isChosen(game);
// stop by full complete
if (isChoiceCompleted(abilityControllerId, source, game)) {
break;
}
// stop by nothing to choose (actual for human and done button?)
if (prevTargetsCount == this.getTargets().size()) {
break;
}
// can select next target
} while (true);
chosen = isChosen(game);
return this.getTargets().size() > 0;
}
@Override
@ -665,6 +767,7 @@ public abstract class TargetImpl implements Target {
public void setTargetAmount(UUID targetId, int amount, Game game) {
targets.put(targetId, amount);
rememberZoneChangeCounter(targetId, game);
chosen = isChosen(game);
}
@Override
@ -716,9 +819,20 @@ public abstract class TargetImpl implements Target {
} else {
playerAutoTargetLevel = 2;
}
// freeze protection on disconnect - auto-choice works for online players only
boolean isOnline = player.canRespond();
if (!player.isGameUnderControl()) {
Player controllingPlayer = game.getPlayer(player.getTurnControlledBy());
if (player.isHuman()) {
isOnline = controllingPlayer.canRespond();
}
}
String abilityText = source.getRule(true).toLowerCase();
boolean strictModeEnabled = player.getStrictChooseMode();
boolean canAutoChoose = this.getMinNumberOfTargets() == this.getMaxNumberOfTargets() // Targets must be picked
&& isOnline
&& possibleTargets.size() == this.getMinNumberOfTargets() - this.getSize() // Available targets are equal to the number that must be picked
&& !strictModeEnabled // Test AI is not set to strictChooseMode(true)
&& playerAutoTargetLevel > 0 // Human player has enabled auto-choose in settings
@ -775,4 +889,13 @@ public abstract class TargetImpl implements Target {
return null;
}
@Override
public String toString() {
return this.getClass().getSimpleName()
+ ", from " + this.getMinNumberOfTargets()
+ " to " + this.getMaxNumberOfTargets()
+ ", " + this.getDescription()
+ ", selected " + this.getTargets().size();
}
}