mirror of
https://github.com/magefree/mage.git
synced 2025-12-20 10:40:06 -08:00
refactor, ai: removed outdated and unused code (suggest actions for AI simulations);
This commit is contained in:
parent
07915394c7
commit
d1cca988c4
6 changed files with 6 additions and 193 deletions
|
|
@ -77,10 +77,6 @@ public class ComputerPlayer6 extends ComputerPlayer {
|
||||||
List<Permanent> attackersList = new ArrayList<>();
|
List<Permanent> attackersList = new ArrayList<>();
|
||||||
List<Permanent> attackersToCheck = 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;
|
protected Set<String> actionCache;
|
||||||
private static final List<TreeOptimizer> optimizers = new ArrayList<>();
|
private static final List<TreeOptimizer> optimizers = new ArrayList<>();
|
||||||
protected int lastLoggedTurn = 0;
|
protected int lastLoggedTurn = 0;
|
||||||
|
|
@ -97,13 +93,12 @@ public class ComputerPlayer6 extends ComputerPlayer {
|
||||||
public ComputerPlayer6(String name, RangeOfInfluence range, int skill) {
|
public ComputerPlayer6(String name, RangeOfInfluence range, int skill) {
|
||||||
super(name, range);
|
super(name, range);
|
||||||
if (skill < 4) {
|
if (skill < 4) {
|
||||||
maxDepth = 4;
|
maxDepth = 4; // wtf
|
||||||
} else {
|
} else {
|
||||||
maxDepth = skill;
|
maxDepth = skill;
|
||||||
}
|
}
|
||||||
maxThink = skill * 3;
|
maxThink = skill * 3;
|
||||||
maxNodes = MAX_SIMULATED_NODES_PER_CALC;
|
maxNodes = MAX_SIMULATED_NODES_PER_CALC;
|
||||||
getSuggestedActions();
|
|
||||||
this.actionCache = new HashSet<>();
|
this.actionCache = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -202,17 +197,6 @@ public class ComputerPlayer6 extends ComputerPlayer {
|
||||||
if (ability.isUsesStack()) {
|
if (ability.isUsesStack()) {
|
||||||
usedStack = true;
|
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) {
|
if (usedStack) {
|
||||||
pass(game);
|
pass(game);
|
||||||
|
|
@ -323,9 +307,6 @@ public class ComputerPlayer6 extends ComputerPlayer {
|
||||||
root = root.children.get(0);
|
root = root.children.get(0);
|
||||||
}
|
}
|
||||||
logger.trace("Sim getNextAction -- game value:" + game.getState().getValue(true) + " test value:" + test.gameValue);
|
logger.trace("Sim getNextAction -- game value:" + game.getState().getValue(true) + " test value:" + test.gameValue);
|
||||||
if (!suggestedActions.isEmpty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (root.playerId.equals(playerId)
|
if (root.playerId.equals(playerId)
|
||||||
&& root.abilities != null
|
&& root.abilities != null
|
||||||
&& game.getState().getValue(true).hashCode() == test.gameValue) {
|
&& game.getState().getValue(true).hashCode() == test.gameValue) {
|
||||||
|
|
@ -1069,10 +1050,7 @@ public class ComputerPlayer6 extends ComputerPlayer {
|
||||||
for (Player oldPlayer : sim.getState().getPlayers().values()) {
|
for (Player oldPlayer : sim.getState().getPlayers().values()) {
|
||||||
// replace original player by simulated player and find result (execute/resolve current action)
|
// replace original player by simulated player and find result (execute/resolve current action)
|
||||||
Player origPlayer = game.getState().getPlayers().get(oldPlayer.getId()).copy();
|
Player origPlayer = game.getState().getPlayers().get(oldPlayer.getId()).copy();
|
||||||
if (!suggestedActions.isEmpty()) {
|
SimulatedPlayer2 simPlayer = new SimulatedPlayer2(oldPlayer, oldPlayer.getId().equals(playerId));
|
||||||
logger.debug(origPlayer.getName() + " suggested: " + suggestedActions);
|
|
||||||
}
|
|
||||||
SimulatedPlayer2 simPlayer = new SimulatedPlayer2(oldPlayer, oldPlayer.getId().equals(playerId), suggestedActions);
|
|
||||||
simPlayer.restore(origPlayer);
|
simPlayer.restore(origPlayer);
|
||||||
sim.getState().getPlayers().put(oldPlayer.getId(), simPlayer);
|
sim.getState().getPlayers().put(oldPlayer.getId(), simPlayer);
|
||||||
}
|
}
|
||||||
|
|
@ -1107,51 +1085,6 @@ public class ComputerPlayer6 extends ComputerPlayer {
|
||||||
return false;
|
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)
|
* Return info about targets list (targeting objects)
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -35,16 +35,13 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
|
||||||
private static final Logger logger = Logger.getLogger(SimulatedPlayer2.class);
|
private static final Logger logger = Logger.getLogger(SimulatedPlayer2.class);
|
||||||
|
|
||||||
private final boolean isSimulatedPlayer;
|
private final boolean isSimulatedPlayer;
|
||||||
private final List<String> suggested;
|
|
||||||
private transient ConcurrentLinkedQueue<Ability> allActions;
|
private transient ConcurrentLinkedQueue<Ability> allActions;
|
||||||
private boolean forced;
|
|
||||||
private final Player originalPlayer; // copy of the original player, source of choices/results in tests
|
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());
|
super(originalPlayer.getId());
|
||||||
this.originalPlayer = originalPlayer.copy();
|
this.originalPlayer = originalPlayer.copy();
|
||||||
this.isSimulatedPlayer = isSimulatedPlayer;
|
this.isSimulatedPlayer = isSimulatedPlayer;
|
||||||
this.suggested = suggested;
|
|
||||||
this.userData = UserData.getDefaultUserDataView();
|
this.userData = UserData.getDefaultUserDataView();
|
||||||
this.matchPlayer = new MatchPlayer(originalPlayer.getMatchPlayer(), this);
|
this.matchPlayer = new MatchPlayer(originalPlayer.getMatchPlayer(), this);
|
||||||
}
|
}
|
||||||
|
|
@ -52,7 +49,6 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
|
||||||
public SimulatedPlayer2(final SimulatedPlayer2 player) {
|
public SimulatedPlayer2(final SimulatedPlayer2 player) {
|
||||||
super(player);
|
super(player);
|
||||||
this.isSimulatedPlayer = player.isSimulatedPlayer;
|
this.isSimulatedPlayer = player.isSimulatedPlayer;
|
||||||
this.suggested = new ArrayList<>(player.suggested);
|
|
||||||
// this.allActions = player.allActions; // dynamic, no need to copy
|
// this.allActions = player.allActions; // dynamic, no need to copy
|
||||||
this.originalPlayer = player.originalPlayer.copy();
|
this.originalPlayer = player.originalPlayer.copy();
|
||||||
}
|
}
|
||||||
|
|
@ -65,15 +61,14 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
|
||||||
public List<Ability> simulatePriority(Game game) {
|
public List<Ability> simulatePriority(Game game) {
|
||||||
allActions = new ConcurrentLinkedQueue<>();
|
allActions = new ConcurrentLinkedQueue<>();
|
||||||
Game sim = game.createSimulationForAI();
|
Game sim = game.createSimulationForAI();
|
||||||
forced = false;
|
|
||||||
simulateOptions(sim);
|
simulateOptions(sim);
|
||||||
|
|
||||||
|
// possible actions
|
||||||
List<Ability> list = new ArrayList<>(allActions);
|
List<Ability> list = new ArrayList<>(allActions);
|
||||||
Collections.reverse(list);
|
Collections.reverse(list);
|
||||||
|
|
||||||
if (!forced) {
|
// pass action
|
||||||
list.add(new PassAbility());
|
list.add(new PassAbility());
|
||||||
}
|
|
||||||
|
|
||||||
if (logger.isTraceEnabled()) {
|
if (logger.isTraceEnabled()) {
|
||||||
for (Ability a : allActions) {
|
for (Ability a : allActions) {
|
||||||
|
|
@ -97,13 +92,11 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
|
||||||
|
|
||||||
protected void simulateOptions(Game game) {
|
protected void simulateOptions(Game game) {
|
||||||
List<ActivatedAbility> playables = game.getPlayer(playerId).getPlayable(game, isSimulatedPlayer);
|
List<ActivatedAbility> playables = game.getPlayer(playerId).getPlayable(game, isSimulatedPlayer);
|
||||||
playables = filterAbilities(game, playables, suggested);
|
|
||||||
for (ActivatedAbility ability : playables) {
|
for (ActivatedAbility ability : playables) {
|
||||||
if (ability.isManaAbility()) {
|
if (ability.isManaAbility()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
List<Ability> options = game.getPlayer(playerId).getPlayableOptions(ability, game);
|
List<Ability> options = game.getPlayer(playerId).getPlayableOptions(ability, game);
|
||||||
options = filterOptions(game, options, ability, suggested);
|
|
||||||
options = optimizeOptions(game, options, ability);
|
options = optimizeOptions(game, options, ability);
|
||||||
if (options.isEmpty()) {
|
if (options.isEmpty()) {
|
||||||
allActions.add(ability);
|
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) {
|
protected List<Ability> optimizeOptions(Game game, List<Ability> options, Ability ability) {
|
||||||
|
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
play:Swamp
|
|
||||||
cast:Duress
|
|
||||||
|
|
@ -194,14 +194,6 @@ public class TestPlayer implements Player {
|
||||||
return computerPlayer.getManaAvailable(game);
|
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) {
|
public void addAction(PlayerAction playerAction) {
|
||||||
actions.add(playerAction);
|
actions.add(playerAction);
|
||||||
}
|
}
|
||||||
|
|
@ -557,11 +549,6 @@ public class TestPlayer implements Player {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getActionCount() {
|
|
||||||
return actions.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TestPlayer copy() {
|
public TestPlayer copy() {
|
||||||
return new TestPlayer(this);
|
return new TestPlayer(this);
|
||||||
|
|
@ -3773,11 +3760,6 @@ public class TestPlayer implements Player {
|
||||||
computerPlayer.setUserData(userData);
|
computerPlayer.setUserData(userData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addAction(String action) {
|
|
||||||
computerPlayer.addAction(action);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAllowBadMoves(boolean allowBadMoves) {
|
public void setAllowBadMoves(boolean allowBadMoves) {
|
||||||
computerPlayer.setAllowBadMoves(allowBadMoves);
|
computerPlayer.setAllowBadMoves(allowBadMoves);
|
||||||
|
|
|
||||||
|
|
@ -375,10 +375,6 @@ public interface Player extends MageItem, Copyable<Player> {
|
||||||
|
|
||||||
void setTestMode(boolean value);
|
void setTestMode(boolean value);
|
||||||
|
|
||||||
void addAction(String action);
|
|
||||||
|
|
||||||
int getActionCount();
|
|
||||||
|
|
||||||
void setAllowBadMoves(boolean allowBadMoves);
|
void setAllowBadMoves(boolean allowBadMoves);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -4466,17 +4466,6 @@ public abstract class PlayerImpl implements Player, Serializable {
|
||||||
getManaPool().setAutoPaymentRestricted(userData.isManaPoolAutomaticRestricted());
|
getManaPool().setAutoPaymentRestricted(userData.isManaPoolAutomaticRestricted());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addAction(String action
|
|
||||||
) {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getActionCount() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAllowBadMoves(boolean allowBadMoves) {
|
public void setAllowBadMoves(boolean allowBadMoves) {
|
||||||
// do nothing
|
// do nothing
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue