Set for simulated games of the AI the simulation flag (please report if this causes any problems).

This commit is contained in:
LevelX2 2014-06-02 08:32:22 +02:00
parent 9dd35eced4
commit 77fb744991
2 changed files with 45 additions and 28 deletions

View file

@ -428,7 +428,7 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
* @return * @return
*/ */
protected Integer addActionsTimed() { protected Integer addActionsTimed() {
FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() { FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
@Override @Override
public Integer call() throws Exception { public Integer call() throws Exception {
return addActions(root, maxDepth, Integer.MIN_VALUE, Integer.MAX_VALUE); return addActions(root, maxDepth, Integer.MIN_VALUE, Integer.MAX_VALUE);
@ -528,6 +528,7 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
break; break;
} }
Game sim = game.copy(); Game sim = game.copy();
sim.setSimulation(true);
if (sim.getPlayer(currentPlayer.getId()).activateAbility((ActivatedAbility) action.copy(), sim)) { if (sim.getPlayer(currentPlayer.getId()).activateAbility((ActivatedAbility) action.copy(), sim)) {
sim.applyEffects(); sim.applyEffects();
if (checkForRepeatedAction(sim, node, action, currentPlayer.getId())) { if (checkForRepeatedAction(sim, node, action, currentPlayer.getId())) {
@ -867,7 +868,7 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
} }
private List<Permanent> filterOutNonblocking(Game game, List<Permanent> attackers, List<Permanent> blockers) { private List<Permanent> filterOutNonblocking(Game game, List<Permanent> attackers, List<Permanent> blockers) {
List<Permanent> blockersLeft = new ArrayList<Permanent>(); List<Permanent> blockersLeft = new ArrayList<>();
for (Permanent blocker : blockers) { for (Permanent blocker : blockers) {
for (Permanent attacker : attackers) { for (Permanent attacker : attackers) {
if (blocker.canBlock(attacker.getId(), game)) { if (blocker.canBlock(attacker.getId(), game)) {
@ -880,7 +881,7 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
} }
private List<Permanent> filterOutUnblockable(Game game, List<Permanent> attackers, List<Permanent> blockers) { private List<Permanent> filterOutUnblockable(Game game, List<Permanent> attackers, List<Permanent> blockers) {
List<Permanent> attackersLeft = new ArrayList<Permanent>(); List<Permanent> attackersLeft = new ArrayList<>();
for (Permanent attacker : attackers) { for (Permanent attacker : attackers) {
if (CombatUtil.canBeBlocked(game, attacker, blockers)) { if (CombatUtil.canBeBlocked(game, attacker, blockers)) {
attackersLeft.add(attacker); attackersLeft.add(attacker);
@ -895,7 +896,7 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
return null; return null;
} }
List<Permanent> attackers = new ArrayList<Permanent>(); List<Permanent> attackers = new ArrayList<>();
for (UUID attackerId : attackersUUID) { for (UUID attackerId : attackersUUID) {
Permanent permanent = game.getPermanent(attackerId); Permanent permanent = game.getPermanent(attackerId);
attackers.add(permanent); attackers.add(permanent);
@ -1291,6 +1292,7 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
*/ */
protected Game createSimulation(Game game) { protected Game createSimulation(Game game) {
Game sim = game.copy(); Game sim = game.copy();
sim.setSimulation(true);
for (Player copyPlayer : sim.getState().getPlayers().values()) { for (Player copyPlayer : sim.getState().getPlayers().values()) {
Player origPlayer = game.getState().getPlayers().get(copyPlayer.getId()).copy(); Player origPlayer = game.getState().getPlayers().get(copyPlayer.getId()).copy();
if (!suggested.isEmpty()) { if (!suggested.isEmpty()) {

View file

@ -28,13 +28,20 @@
package mage.player.ai; package mage.player.ai;
import java.util.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import mage.abilities.costs.mana.ManaCostsImpl;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.TriggeredAbility; import mage.abilities.TriggeredAbility;
import mage.abilities.common.PassAbility; import mage.abilities.common.PassAbility;
import mage.abilities.costs.mana.ManaCost; import mage.abilities.costs.mana.ManaCost;
import mage.abilities.costs.mana.ManaCostsImpl;
import mage.abilities.costs.mana.VariableManaCost; import mage.abilities.costs.mana.VariableManaCost;
import mage.abilities.effects.Effect; import mage.abilities.effects.Effect;
import mage.cards.Card; import mage.cards.Card;
@ -48,7 +55,6 @@ import mage.players.Player;
import mage.target.Target; import mage.target.Target;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/** /**
* *
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
@ -56,12 +62,12 @@ import org.apache.log4j.Logger;
public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> { public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
private static final transient Logger logger = Logger.getLogger(SimulatedPlayer2.class); private static final transient Logger logger = Logger.getLogger(SimulatedPlayer2.class);
private boolean isSimulatedPlayer; private final boolean isSimulatedPlayer;
private transient ConcurrentLinkedQueue<Ability> allActions; private transient ConcurrentLinkedQueue<Ability> allActions;
private boolean forced; private boolean forced;
private static PassAbility pass = new PassAbility(); private static PassAbility pass = new PassAbility();
private List<String> suggested; private final List<String> suggested;
public SimulatedPlayer2(UUID id, boolean isSimulatedPlayer, List<String> suggested) { public SimulatedPlayer2(UUID id, boolean isSimulatedPlayer, List<String> suggested) {
super(id); super(id);
@ -73,7 +79,7 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
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<String>(); this.suggested = new ArrayList<>();
for (String s : player.suggested) { for (String s : player.suggested) {
this.suggested.add(s); this.suggested.add(s);
} }
@ -86,13 +92,13 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
} }
public List<Ability> simulatePriority(Game game) { public List<Ability> simulatePriority(Game game) {
allActions = new ConcurrentLinkedQueue<Ability>(); allActions = new ConcurrentLinkedQueue<>();
Game sim = game.copy(); Game sim = game.copy();
sim.setSimulation(true);
forced = false; forced = false;
simulateOptions(sim); simulateOptions(sim);
ArrayList<Ability> list = new ArrayList<Ability>(allActions); ArrayList<Ability> list = new ArrayList<>(allActions);
Collections.reverse(list); Collections.reverse(list);
if (!forced) { if (!forced) {
@ -170,7 +176,9 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
// add the specific value for x // add the specific value for x
newAbility.getManaCostsToPay().add(new ManaCostsImpl(new StringBuilder("{").append(xAmount).append("}").toString())); newAbility.getManaCostsToPay().add(new ManaCostsImpl(new StringBuilder("{").append(xAmount).append("}").toString()));
newAbility.getManaCostsToPay().setX(xAmount); newAbility.getManaCostsToPay().setX(xAmount);
varCost.setPaid(); if (varCost != null) {
varCost.setPaid();
}
card.adjustTargets(newAbility, game); card.adjustTargets(newAbility, game);
// add the different possible target option for the specific X value // add the different possible target option for the specific X value
if (newAbility.getTargets().getUnchosen().size() > 0) { if (newAbility.getTargets().getUnchosen().size() > 0) {
@ -211,7 +219,7 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
if (suggested == null || suggested.isEmpty()) { if (suggested == null || suggested.isEmpty()) {
return playables; return playables;
} }
List<Ability> filtered = new ArrayList<Ability>(); List<Ability> filtered = new ArrayList<>();
for (Ability ability : playables) { for (Ability ability : playables) {
Card card = game.getCard(ability.getSourceId()); Card card = game.getCard(ability.getSourceId());
for (String s : suggested) { for (String s : suggested) {
@ -235,7 +243,7 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
if (suggested == null || suggested.isEmpty()) { if (suggested == null || suggested.isEmpty()) {
return options; return options;
} }
List<Ability> filtered = new ArrayList<Ability>(); List<Ability> filtered = new ArrayList<>();
for (Ability option : options) { for (Ability option : options) {
if (option.getTargets().size() > 0 && option.getTargets().get(0).getMaxNumberOfTargets() == 1) { if (option.getTargets().size() > 0 && option.getTargets().get(0).getMaxNumberOfTargets() == 1) {
Card card = game.getCard(ability.getSourceId()); Card card = game.getCard(ability.getSourceId());
@ -245,15 +253,15 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
if (groups.length == 2) { if (groups.length == 2) {
if (groups[0].equals(card.getName()) && groups[1].startsWith("name=")) { if (groups[0].equals(card.getName()) && groups[1].startsWith("name=")) {
// extract target and compare to suggested // extract target and compare to suggested
String name = groups[1].split("=")[1]; String targetName = groups[1].split("=")[1];
Player player = game.getPlayer(option.getFirstTarget()); Player player = game.getPlayer(option.getFirstTarget());
if (player != null && name.equals(player.getName())) { if (player != null && targetName.equals(player.getName())) {
System.out.println("matched(option): " + s); System.out.println("matched(option): " + s);
filtered.add(option); filtered.add(option);
return filtered; return filtered;
} else { } else {
Card target = game.getCard(option.getFirstTarget()); Card target = game.getCard(option.getFirstTarget());
if (target != null && target.getName().equals(name)) { if (target != null && target.getName().equals(targetName)) {
System.out.println("matched(option): " + s); System.out.println("matched(option): " + s);
filtered.add(option); filtered.add(option);
return filtered; return filtered;
@ -323,7 +331,7 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
} }
public List<Combat> addAttackers(Game game) { public List<Combat> addAttackers(Game game) {
Map<Integer, Combat> engagements = new HashMap<Integer, Combat>(); Map<Integer, Combat> engagements = new HashMap<>();
//useful only for two player games - will only attack first opponent //useful only for two player games - will only attack first opponent
UUID defenderId = game.getOpponents(playerId).iterator().next(); UUID defenderId = game.getOpponents(playerId).iterator().next();
List<Permanent> attackersList = super.getAvailableAttackers(game); List<Permanent> attackersList = super.getAvailableAttackers(game);
@ -338,8 +346,9 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
binary.insert(0, "0"); binary.insert(0, "0");
} }
for (int j = 0; j < attackersList.size(); j++) { for (int j = 0; j < attackersList.size(); j++) {
if (binary.charAt(j) == '1') if (binary.charAt(j) == '1') {
sim.getCombat().declareAttacker(attackersList.get(j).getId(), defenderId, sim); sim.getCombat().declareAttacker(attackersList.get(j).getId(), defenderId, sim);
}
} }
if (engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat()) != null) { if (engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat()) != null) {
logger.debug("simulating -- found redundant attack combination"); logger.debug("simulating -- found redundant attack combination");
@ -348,7 +357,7 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
logger.debug("simulating -- attack:" + sim.getCombat().getGroups().size()); logger.debug("simulating -- attack:" + sim.getCombat().getGroups().size());
} }
} }
List list = new ArrayList<Combat>(engagements.values()); List list = new ArrayList<>(engagements.values());
Collections.sort(list, new Comparator<Combat>() { Collections.sort(list, new Comparator<Combat>() {
@Override @Override
public int compare(Combat o1, Combat o2) { public int compare(Combat o1, Combat o2) {
@ -359,9 +368,11 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
} }
public List<Combat> addBlockers(Game game) { public List<Combat> addBlockers(Game game) {
Map<Integer, Combat> engagements = new HashMap<Integer, Combat>(); Map<Integer, Combat> engagements = new HashMap<>();
int numGroups = game.getCombat().getGroups().size(); int numGroups = game.getCombat().getGroups().size();
if (numGroups == 0) return new ArrayList<Combat>(); if (numGroups == 0) {
return new ArrayList<>();
}
//add a node with no blockers //add a node with no blockers
Game sim = game.copy(); Game sim = game.copy();
@ -371,12 +382,13 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
List<Permanent> blockers = getAvailableBlockers(game); List<Permanent> blockers = getAvailableBlockers(game);
addBlocker(game, blockers, engagements); addBlocker(game, blockers, engagements);
return new ArrayList<Combat>(engagements.values()); return new ArrayList<>(engagements.values());
} }
protected void addBlocker(Game game, List<Permanent> blockers, Map<Integer, Combat> engagements) { protected void addBlocker(Game game, List<Permanent> blockers, Map<Integer, Combat> engagements) {
if (blockers.isEmpty()) if (blockers.isEmpty()) {
return; return;
}
int numGroups = game.getCombat().getGroups().size(); int numGroups = game.getCombat().getGroups().size();
//try to block each attacker with each potential blocker //try to block each attacker with each potential blocker
Permanent blocker = blockers.get(0); Permanent blocker = blockers.get(0);
@ -386,8 +398,9 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
if (game.getCombat().getGroups().get(i).canBlock(blocker, game)) { if (game.getCombat().getGroups().get(i).canBlock(blocker, game)) {
Game sim = game.copy(); Game sim = game.copy();
sim.getCombat().getGroups().get(i).addBlocker(blocker.getId(), playerId, sim); sim.getCombat().getGroups().get(i).addBlocker(blocker.getId(), playerId, sim);
if (engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat()) != null) if (engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat()) != null) {
logger.debug("simulating -- found redundant block combination"); logger.debug("simulating -- found redundant block combination");
}
addBlocker(sim, remaining, engagements); // and recurse minus the used blocker addBlocker(sim, remaining, engagements); // and recurse minus the used blocker
} }
} }
@ -408,7 +421,9 @@ public class SimulatedPlayer2 extends ComputerPlayer<SimulatedPlayer2> {
else { else {
SimulationNode2 parent = (SimulationNode2) game.getCustomData(); SimulationNode2 parent = (SimulationNode2) game.getCustomData();
int depth = parent.getDepth() - 1; int depth = parent.getDepth() - 1;
if (depth == 0) return true; if (depth == 0) {
return true;
}
logger.debug("simulating -- triggered ability - adding children:" + options.size()); logger.debug("simulating -- triggered ability - adding children:" + options.size());
for (Ability option: options) { for (Ability option: options) {
addAbilityNode(parent, option, depth, game); addAbilityNode(parent, option, depth, game);