refactor, ai: removed outdated and unused code (suggest actions for AI simulations);

This commit is contained in:
Oleg Agafonov 2024-05-05 20:47:54 +04:00
parent 07915394c7
commit d1cca988c4
6 changed files with 6 additions and 193 deletions

View file

@ -77,10 +77,6 @@ public class ComputerPlayer6 extends ComputerPlayer {
List<Permanent> attackersList = new ArrayList<>();
List<Permanent> attackersToCheck = new ArrayList<>();
private static final boolean AI_SUGGEST_BY_FILE_ENABLE = true; // old method to instruct AI to play cards (use cheat commands instead now)
private static final String AI_SUGGEST_BY_FILE_SOURCE = "config/ai.please.cast.this.txt";
private final List<String> suggestedActions = new ArrayList<>();
protected Set<String> actionCache;
private static final List<TreeOptimizer> optimizers = new ArrayList<>();
protected int lastLoggedTurn = 0;
@ -97,13 +93,12 @@ public class ComputerPlayer6 extends ComputerPlayer {
public ComputerPlayer6(String name, RangeOfInfluence range, int skill) {
super(name, range);
if (skill < 4) {
maxDepth = 4;
maxDepth = 4; // wtf
} else {
maxDepth = skill;
}
maxThink = skill * 3;
maxNodes = MAX_SIMULATED_NODES_PER_CALC;
getSuggestedActions();
this.actionCache = new HashSet<>();
}
@ -202,17 +197,6 @@ public class ComputerPlayer6 extends ComputerPlayer {
if (ability.isUsesStack()) {
usedStack = true;
}
if (!suggestedActions.isEmpty() && !(ability instanceof PassAbility)) {
Iterator<String> it = suggestedActions.iterator();
while (it.hasNext()) {
String action = it.next();
Card card = game.getCard(ability.getSourceId());
if (card != null && action.equals(card.getName())) {
logger.info("-> removed from suggested=" + action);
it.remove();
}
}
}
}
if (usedStack) {
pass(game);
@ -323,9 +307,6 @@ public class ComputerPlayer6 extends ComputerPlayer {
root = root.children.get(0);
}
logger.trace("Sim getNextAction -- game value:" + game.getState().getValue(true) + " test value:" + test.gameValue);
if (!suggestedActions.isEmpty()) {
return false;
}
if (root.playerId.equals(playerId)
&& root.abilities != null
&& game.getState().getValue(true).hashCode() == test.gameValue) {
@ -1069,10 +1050,7 @@ public class ComputerPlayer6 extends ComputerPlayer {
for (Player oldPlayer : sim.getState().getPlayers().values()) {
// replace original player by simulated player and find result (execute/resolve current action)
Player origPlayer = game.getState().getPlayers().get(oldPlayer.getId()).copy();
if (!suggestedActions.isEmpty()) {
logger.debug(origPlayer.getName() + " suggested: " + suggestedActions);
}
SimulatedPlayer2 simPlayer = new SimulatedPlayer2(oldPlayer, oldPlayer.getId().equals(playerId), suggestedActions);
SimulatedPlayer2 simPlayer = new SimulatedPlayer2(oldPlayer, oldPlayer.getId().equals(playerId));
simPlayer.restore(origPlayer);
sim.getState().getPlayers().put(oldPlayer.getId(), simPlayer);
}
@ -1107,51 +1085,6 @@ public class ComputerPlayer6 extends ComputerPlayer {
return false;
}
protected final void getSuggestedActions() {
if (!AI_SUGGEST_BY_FILE_ENABLE) {
return;
}
Scanner scanner = null;
try {
File file = new File(AI_SUGGEST_BY_FILE_SOURCE);
if (file.exists()) {
scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("cast:")
|| line.startsWith("play:")) {
suggestedActions.add(line.substring(5));
}
}
System.out.println("suggested::");
for (int i = 0; i < suggestedActions.size(); i++) {
System.out.println(" " + suggestedActions.get(i));
}
}
} catch (Exception e) {
// swallow
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
}
@Override
public void addAction(String action) {
if (action != null
&& (action.startsWith("cast:")
|| action.startsWith("play:"))) {
suggestedActions.add(action.substring(5));
}
}
@Override
public int getActionCount() {
return suggestedActions.size();
}
/**
* Return info about targets list (targeting objects)
*

View file

@ -35,16 +35,13 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
private static final Logger logger = Logger.getLogger(SimulatedPlayer2.class);
private final boolean isSimulatedPlayer;
private final List<String> suggested;
private transient ConcurrentLinkedQueue<Ability> allActions;
private boolean forced;
private final Player originalPlayer; // copy of the original player, source of choices/results in tests
public SimulatedPlayer2(Player originalPlayer, boolean isSimulatedPlayer, List<String> suggested) {
public SimulatedPlayer2(Player originalPlayer, boolean isSimulatedPlayer) {
super(originalPlayer.getId());
this.originalPlayer = originalPlayer.copy();
this.isSimulatedPlayer = isSimulatedPlayer;
this.suggested = suggested;
this.userData = UserData.getDefaultUserDataView();
this.matchPlayer = new MatchPlayer(originalPlayer.getMatchPlayer(), this);
}
@ -52,7 +49,6 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
public SimulatedPlayer2(final SimulatedPlayer2 player) {
super(player);
this.isSimulatedPlayer = player.isSimulatedPlayer;
this.suggested = new ArrayList<>(player.suggested);
// this.allActions = player.allActions; // dynamic, no need to copy
this.originalPlayer = player.originalPlayer.copy();
}
@ -65,15 +61,14 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
public List<Ability> simulatePriority(Game game) {
allActions = new ConcurrentLinkedQueue<>();
Game sim = game.createSimulationForAI();
forced = false;
simulateOptions(sim);
// possible actions
List<Ability> list = new ArrayList<>(allActions);
Collections.reverse(list);
if (!forced) {
list.add(new PassAbility());
}
// pass action
list.add(new PassAbility());
if (logger.isTraceEnabled()) {
for (Ability a : allActions) {
@ -97,13 +92,11 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
protected void simulateOptions(Game game) {
List<ActivatedAbility> playables = game.getPlayer(playerId).getPlayable(game, isSimulatedPlayer);
playables = filterAbilities(game, playables, suggested);
for (ActivatedAbility ability : playables) {
if (ability.isManaAbility()) {
continue;
}
List<Ability> options = game.getPlayer(playerId).getPlayableOptions(ability, game);
options = filterOptions(game, options, ability, suggested);
options = optimizeOptions(game, options, ability);
if (options.isEmpty()) {
allActions.add(ability);
@ -165,84 +158,6 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
}
}
}
/**
* if suggested abilities exist, return only those from playables
*
* @param game
* @param playables
* @param suggested
* @return
*/
protected List<ActivatedAbility> filterAbilities(Game game, List<ActivatedAbility> playables, List<String> suggested) {
if (playables.isEmpty()) {
return playables;
}
if (suggested == null || suggested.isEmpty()) {
return playables;
}
List<ActivatedAbility> filtered = new ArrayList<>();
for (ActivatedAbility ability : playables) {
Card card = game.getCard(ability.getSourceId());
if (card != null) {
for (String s : suggested) {
if (s.equals(card.getName())) {
logger.debug("matched: " + s);
forced = true;
filtered.add(ability);
}
}
}
}
if (!filtered.isEmpty()) {
return filtered;
}
return playables;
}
protected List<Ability> filterOptions(Game game, List<Ability> options, ActivatedAbility ability, List<String> suggested) {
if (options.isEmpty()) {
return options;
}
if (suggested == null || suggested.isEmpty()) {
return options;
}
List<Ability> filtered = new ArrayList<>();
for (Ability option : options) {
if (!option.getTargets().isEmpty() && option.getTargets().get(0).getMaxNumberOfTargets() == 1) {
Card card = game.getCard(ability.getSourceId());
if (card != null) {
for (String s : suggested) {
String[] groups = s.split(";");
logger.trace("s=" + s + ";groups=" + groups.length);
if (groups.length == 2) {
if (groups[0].equals(card.getName()) && groups[1].startsWith("name=")) {
// extract target and compare to suggested
String targetName = groups[1].split("=")[1];
Player player = game.getPlayer(option.getFirstTarget());
if (player != null && targetName.equals(player.getName())) {
System.out.println("matched(option): " + s);
filtered.add(option);
return filtered;
} else {
Card target = game.getCard(option.getFirstTarget());
if (target != null && target.getName().equals(targetName)) {
System.out.println("matched(option): " + s);
filtered.add(option);
return filtered;
}
System.out.println("not equal UUID for target, player=" + player);
}
}
}
}
}
}
}
// no option was found
return options;
}
protected List<Ability> optimizeOptions(Game game, List<Ability> options, Ability ability) {

View file

@ -1,2 +0,0 @@
play:Swamp
cast:Duress

View file

@ -194,14 +194,6 @@ public class TestPlayer implements Player {
return computerPlayer.getManaAvailable(game);
}
public void addAction(int turnNum, PhaseStep step, String action) {
actions.add(new PlayerAction("", turnNum, step, action));
}
public void addAction(String actionName, int turnNum, PhaseStep step, String action) {
actions.add(new PlayerAction(actionName, turnNum, step, action));
}
public void addAction(PlayerAction playerAction) {
actions.add(playerAction);
}
@ -557,11 +549,6 @@ public class TestPlayer implements Player {
return result;
}
@Override
public int getActionCount() {
return actions.size();
}
@Override
public TestPlayer copy() {
return new TestPlayer(this);
@ -3773,11 +3760,6 @@ public class TestPlayer implements Player {
computerPlayer.setUserData(userData);
}
@Override
public void addAction(String action) {
computerPlayer.addAction(action);
}
@Override
public void setAllowBadMoves(boolean allowBadMoves) {
computerPlayer.setAllowBadMoves(allowBadMoves);

View file

@ -375,10 +375,6 @@ public interface Player extends MageItem, Copyable<Player> {
void setTestMode(boolean value);
void addAction(String action);
int getActionCount();
void setAllowBadMoves(boolean allowBadMoves);
/**

View file

@ -4466,17 +4466,6 @@ public abstract class PlayerImpl implements Player, Serializable {
getManaPool().setAutoPaymentRestricted(userData.isManaPoolAutomaticRestricted());
}
@Override
public void addAction(String action
) {
// do nothing
}
@Override
public int getActionCount() {
return 0;
}
@Override
public void setAllowBadMoves(boolean allowBadMoves) {
// do nothing