2010-09-01 03:01:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
* permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
* conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
* of conditions and the following disclaimer in the documentation and/or other materials
|
|
|
|
* provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* The views and conclusions contained in the software and documentation are those of the
|
|
|
|
* authors and should not be interpreted as representing official policies, either expressed
|
|
|
|
* or implied, of BetaSteward_at_googlemail.com.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package mage.player.ai;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.FutureTask;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
import mage.Constants.Outcome;
|
|
|
|
import mage.Constants.PhaseStep;
|
|
|
|
import mage.Constants.RangeOfInfluence;
|
2011-02-25 03:01:04 +00:00
|
|
|
import mage.Mana;
|
2010-09-01 03:01:43 +00:00
|
|
|
import mage.abilities.Ability;
|
|
|
|
import mage.abilities.ActivatedAbility;
|
2011-02-19 04:22:31 +00:00
|
|
|
import mage.abilities.common.PassAbility;
|
2011-02-25 03:01:04 +00:00
|
|
|
import mage.abilities.costs.mana.GenericManaCost;
|
|
|
|
import mage.abilities.costs.mana.ManaCost;
|
|
|
|
import mage.abilities.costs.mana.ManaCosts;
|
|
|
|
import mage.abilities.costs.mana.VariableManaCost;
|
2010-09-01 03:01:43 +00:00
|
|
|
import mage.abilities.effects.Effect;
|
|
|
|
import mage.abilities.effects.SearchEffect;
|
|
|
|
import mage.cards.Cards;
|
|
|
|
import mage.cards.decks.Deck;
|
|
|
|
import mage.choices.Choice;
|
|
|
|
import mage.filter.FilterAbility;
|
|
|
|
import mage.game.Game;
|
|
|
|
import mage.game.combat.Combat;
|
|
|
|
import mage.game.combat.CombatGroup;
|
|
|
|
import mage.game.events.GameEvent;
|
|
|
|
import mage.game.stack.StackAbility;
|
|
|
|
import mage.game.stack.StackObject;
|
|
|
|
import mage.game.turn.BeginCombatStep;
|
|
|
|
import mage.game.turn.BeginningPhase;
|
|
|
|
import mage.game.turn.CleanupStep;
|
|
|
|
import mage.game.turn.CombatDamageStep;
|
|
|
|
import mage.game.turn.CombatPhase;
|
|
|
|
import mage.game.turn.DeclareAttackersStep;
|
|
|
|
import mage.game.turn.DeclareBlockersStep;
|
|
|
|
import mage.game.turn.DrawStep;
|
|
|
|
import mage.game.turn.EndOfCombatStep;
|
|
|
|
import mage.game.turn.EndPhase;
|
|
|
|
import mage.game.turn.EndStep;
|
|
|
|
import mage.game.turn.Phase;
|
|
|
|
import mage.game.turn.PostCombatMainPhase;
|
|
|
|
import mage.game.turn.PostCombatMainStep;
|
|
|
|
import mage.game.turn.PreCombatMainPhase;
|
|
|
|
import mage.game.turn.PreCombatMainStep;
|
|
|
|
import mage.game.turn.UntapStep;
|
|
|
|
import mage.game.turn.UpkeepStep;
|
|
|
|
import mage.players.Player;
|
|
|
|
import mage.target.Target;
|
|
|
|
import mage.target.TargetCard;
|
2011-02-14 19:41:29 +00:00
|
|
|
import org.apache.log4j.Logger;
|
2010-09-01 03:01:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author BetaSteward_at_googlemail.com
|
|
|
|
*/
|
|
|
|
public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements Player {
|
|
|
|
|
2011-02-14 19:41:29 +00:00
|
|
|
private static final transient Logger logger = Logger.getLogger(ComputerPlayer2.class);
|
2010-09-01 03:01:43 +00:00
|
|
|
private static final ExecutorService pool = Executors.newFixedThreadPool(1);
|
|
|
|
|
|
|
|
protected int maxDepth;
|
|
|
|
protected int maxNodes;
|
|
|
|
protected LinkedList<Ability> actions = new LinkedList<Ability>();
|
|
|
|
protected List<UUID> targets = new ArrayList<UUID>();
|
|
|
|
protected List<String> choices = new ArrayList<String>();
|
|
|
|
protected Combat combat;
|
|
|
|
protected int currentScore;
|
|
|
|
protected SimulationNode root;
|
|
|
|
|
2010-12-29 20:00:18 +00:00
|
|
|
public ComputerPlayer2(String name, RangeOfInfluence range) {
|
|
|
|
super(name, range);
|
2010-09-01 03:01:43 +00:00
|
|
|
maxDepth = Config.maxDepth;
|
|
|
|
maxNodes = Config.maxNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ComputerPlayer2(final ComputerPlayer2 player) {
|
|
|
|
super(player);
|
|
|
|
this.maxDepth = player.maxDepth;
|
|
|
|
this.currentScore = player.currentScore;
|
|
|
|
if (player.combat != null)
|
|
|
|
this.combat = player.combat.copy();
|
|
|
|
for (Ability ability: player.actions) {
|
|
|
|
actions.add(ability);
|
|
|
|
}
|
|
|
|
for (UUID targetId: player.targets) {
|
|
|
|
targets.add(targetId);
|
|
|
|
}
|
|
|
|
for (String choice: player.choices) {
|
|
|
|
choices.add(choice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ComputerPlayer2 copy() {
|
|
|
|
return new ComputerPlayer2(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void priority(Game game) {
|
|
|
|
logState(game);
|
|
|
|
game.firePriorityEvent(playerId);
|
|
|
|
switch (game.getTurn().getStepType()) {
|
|
|
|
case UPKEEP:
|
|
|
|
case DRAW:
|
|
|
|
pass();
|
|
|
|
break;
|
|
|
|
case PRECOMBAT_MAIN:
|
|
|
|
case BEGIN_COMBAT:
|
|
|
|
case DECLARE_ATTACKERS:
|
|
|
|
case DECLARE_BLOCKERS:
|
|
|
|
case COMBAT_DAMAGE:
|
|
|
|
case END_COMBAT:
|
|
|
|
case POSTCOMBAT_MAIN:
|
|
|
|
if (actions.size() == 0) {
|
|
|
|
calculateActions(game);
|
|
|
|
}
|
|
|
|
act(game);
|
|
|
|
break;
|
|
|
|
case END_TURN:
|
|
|
|
case CLEANUP:
|
|
|
|
pass();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void act(Game game) {
|
|
|
|
if (actions == null || actions.size() == 0)
|
|
|
|
pass();
|
|
|
|
else {
|
|
|
|
boolean usedStack = false;
|
|
|
|
while (actions.peek() != null) {
|
|
|
|
Ability ability = actions.poll();
|
|
|
|
this.activateAbility((ActivatedAbility) ability, game);
|
2011-02-19 04:22:31 +00:00
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
logger.debug("activating: " + ability);
|
2010-09-01 03:01:43 +00:00
|
|
|
if (ability.isUsesStack())
|
|
|
|
usedStack = true;
|
|
|
|
}
|
|
|
|
if (usedStack)
|
|
|
|
pass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void calculateActions(Game game) {
|
|
|
|
currentScore = GameStateEvaluator.evaluate(playerId, game);
|
|
|
|
if (!getNextAction(game)) {
|
|
|
|
Game sim = createSimulation(game);
|
|
|
|
SimulationNode.resetCount();
|
2011-02-19 04:22:31 +00:00
|
|
|
root = new SimulationNode(null, sim, maxDepth, playerId);
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating actions");
|
2010-09-01 03:01:43 +00:00
|
|
|
addActionsTimed(new FilterAbility());
|
|
|
|
if (root.children.size() > 0) {
|
|
|
|
root = root.children.get(0);
|
|
|
|
actions = new LinkedList<Ability>(root.abilities);
|
|
|
|
combat = root.combat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean getNextAction(Game game) {
|
|
|
|
if (root != null && root.children.size() > 0) {
|
|
|
|
SimulationNode test = root;
|
|
|
|
root = root.children.get(0);
|
|
|
|
while (root.children.size() > 0 && !root.playerId.equals(playerId)) {
|
|
|
|
test = root;
|
|
|
|
root = root.children.get(0);
|
|
|
|
}
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simlating -- game value:" + game.getState().getValue() + " test value:" + test.gameValue);
|
2010-09-01 03:01:43 +00:00
|
|
|
if (root.playerId.equals(playerId) && root.abilities != null && game.getState().getValue() == test.gameValue) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating -- continuing previous action chain");
|
2010-09-01 03:01:43 +00:00
|
|
|
actions = new LinkedList<Ability>(root.abilities);
|
|
|
|
combat = root.combat;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int minimaxAB(SimulationNode node, FilterAbility filter, int depth, int alpha, int beta) {
|
|
|
|
UUID currentPlayerId = node.getGame().getPlayerList().get();
|
|
|
|
SimulationNode bestChild = null;
|
2011-02-26 05:16:04 +00:00
|
|
|
boolean isSimulatedPlayer = currentPlayerId.equals(playerId);
|
2010-09-01 03:01:43 +00:00
|
|
|
for (SimulationNode child: node.getChildren()) {
|
|
|
|
if (alpha >= beta) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("alpha beta pruning");
|
2010-09-01 03:01:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (SimulationNode.nodeCount > maxNodes) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating -- reached end-state");
|
2010-09-01 03:01:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
int val = addActions(child, filter, depth-1, alpha, beta);
|
2011-02-26 05:16:04 +00:00
|
|
|
if (!isSimulatedPlayer) {
|
2010-09-01 03:01:43 +00:00
|
|
|
if (val < beta) {
|
|
|
|
beta = val;
|
|
|
|
bestChild = child;
|
2011-02-26 05:16:04 +00:00
|
|
|
node.setCombat(child.getCombat());
|
|
|
|
}
|
|
|
|
if (val == GameStateEvaluator.LOSE_SCORE) {
|
|
|
|
logger.debug("simulating -- lose, can't do worse than this");
|
|
|
|
break;
|
2010-09-01 03:01:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (val > alpha) {
|
|
|
|
alpha = val;
|
|
|
|
bestChild = child;
|
2011-02-26 05:16:04 +00:00
|
|
|
node.setCombat(child.getCombat());
|
|
|
|
}
|
|
|
|
if (val == GameStateEvaluator.WIN_SCORE) {
|
|
|
|
logger.debug("simulating -- win, can't do better than this");
|
|
|
|
break;
|
2010-09-01 03:01:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node.children.clear();
|
|
|
|
if (bestChild != null)
|
|
|
|
node.children.add(bestChild);
|
2011-02-26 05:16:04 +00:00
|
|
|
if (!isSimulatedPlayer) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("returning minimax beta: " + beta);
|
2010-09-01 03:01:43 +00:00
|
|
|
return beta;
|
|
|
|
}
|
|
|
|
else {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("returning minimax alpha: " + alpha);
|
2010-09-01 03:01:43 +00:00
|
|
|
return alpha;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected SearchEffect getSearchEffect(StackAbility ability) {
|
|
|
|
for (Effect effect: ability.getEffects()) {
|
|
|
|
if (effect instanceof SearchEffect) {
|
|
|
|
return (SearchEffect) effect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void resolve(SimulationNode node, int depth, Game game) {
|
|
|
|
StackObject ability = game.getStack().pop();
|
|
|
|
if (ability instanceof StackAbility) {
|
|
|
|
SearchEffect effect = getSearchEffect((StackAbility) ability);
|
|
|
|
if (effect != null && ability.getControllerId().equals(playerId)) {
|
|
|
|
Target target = effect.getTarget();
|
|
|
|
if (!target.doneChosing()) {
|
|
|
|
for (UUID targetId: target.possibleTargets(ability.getSourceId(), ability.getControllerId(), game)) {
|
|
|
|
Game sim = game.copy();
|
|
|
|
StackAbility newAbility = (StackAbility) ability.copy();
|
|
|
|
SearchEffect newEffect = getSearchEffect((StackAbility) newAbility);
|
|
|
|
newEffect.getTarget().addTarget(targetId, newAbility, sim);
|
|
|
|
sim.getStack().push(newAbility);
|
2011-02-19 04:22:31 +00:00
|
|
|
SimulationNode newNode = new SimulationNode(node, sim, depth, ability.getControllerId());
|
2010-09-01 03:01:43 +00:00
|
|
|
node.children.add(newNode);
|
|
|
|
newNode.getTargets().add(targetId);
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating search -- node#: " + SimulationNode.getCount() + "for player: " + sim.getPlayer(ability.getControllerId()).getName());
|
2010-09-01 03:01:43 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating resolve ");
|
2010-09-01 03:01:43 +00:00
|
|
|
ability.resolve(game);
|
|
|
|
game.applyEffects();
|
|
|
|
game.getPlayers().resetPassed();
|
|
|
|
game.getPlayerList().setCurrent(game.getActivePlayerId());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void addActionsTimed(final FilterAbility filter) {
|
|
|
|
FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
|
|
|
|
public Integer call() throws Exception
|
|
|
|
{
|
|
|
|
return addActions(root, filter, maxDepth, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
pool.execute(task);
|
|
|
|
try {
|
|
|
|
task.get(Config.maxThinkSeconds, TimeUnit.SECONDS);
|
|
|
|
} catch (TimeoutException e) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating - timed out");
|
2010-09-01 03:01:43 +00:00
|
|
|
task.cancel(true);
|
|
|
|
} catch (ExecutionException e) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.fatal("Simulation error", e);
|
2011-02-13 13:22:35 +00:00
|
|
|
task.cancel(true);
|
2010-09-01 03:01:43 +00:00
|
|
|
} catch (InterruptedException e) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.fatal("Simulation interrupted", e);
|
2011-02-13 13:22:35 +00:00
|
|
|
task.cancel(true);
|
2010-09-01 03:01:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int addActions(SimulationNode node, FilterAbility filter, int depth, int alpha, int beta) {
|
|
|
|
Game game = node.getGame();
|
|
|
|
int val;
|
|
|
|
if (Thread.interrupted()) {
|
|
|
|
Thread.currentThread().interrupt();
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("interrupted");
|
2010-09-01 03:01:43 +00:00
|
|
|
return GameStateEvaluator.evaluate(playerId, game);
|
|
|
|
}
|
|
|
|
if (depth <= 0 || SimulationNode.nodeCount > maxNodes || game.isGameOver()) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating -- reached end state");
|
2010-09-01 03:01:43 +00:00
|
|
|
val = GameStateEvaluator.evaluate(playerId, game);
|
|
|
|
}
|
|
|
|
else if (node.getChildren().size() > 0) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating -- somthing added children:" + node.getChildren().size());
|
2010-09-01 03:01:43 +00:00
|
|
|
val = minimaxAB(node, filter, depth-1, alpha, beta);
|
|
|
|
}
|
|
|
|
else {
|
2011-02-14 19:41:29 +00:00
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
logger.debug("simulating -- alpha: " + alpha + " beta: " + beta + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + (node.getPlayerId().equals(playerId)?"yes":"no"));
|
2010-09-01 03:01:43 +00:00
|
|
|
if (allPassed(game)) {
|
|
|
|
if (!game.getStack().isEmpty()) {
|
|
|
|
resolve(node, depth, game);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// int testScore = GameStateEvaluator.evaluate(playerId, game);
|
|
|
|
// if (testScore < currentScore) {
|
|
|
|
// // if score at end of step is worse than original score don't check any further
|
2011-02-14 19:41:29 +00:00
|
|
|
// logger.debug("simulating -- abandoning current check, no immediate benefit");
|
2010-09-01 03:01:43 +00:00
|
|
|
// return testScore;
|
|
|
|
// }
|
|
|
|
game.getPlayers().resetPassed();
|
|
|
|
playNext(game, game.getActivePlayerId(), node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (game.isGameOver()) {
|
|
|
|
val = GameStateEvaluator.evaluate(playerId, game);
|
|
|
|
}
|
|
|
|
else if (node.getChildren().size() > 0) {
|
|
|
|
//declared attackers or blockers or triggered abilities
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating -- attack/block/trigger added children:" + node.getChildren().size());
|
2010-09-01 03:01:43 +00:00
|
|
|
val = minimaxAB(node, filter, depth-1, alpha, beta);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
val = simulatePriority(node, game, filter, depth, alpha, beta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 19:41:29 +00:00
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
logger.debug("returning -- score: " + val + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
2010-09-01 03:01:43 +00:00
|
|
|
return val;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int simulatePriority(SimulationNode node, Game game, FilterAbility filter, int depth, int alpha, int beta) {
|
|
|
|
if (Thread.interrupted()) {
|
|
|
|
Thread.currentThread().interrupt();
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("interrupted");
|
2010-09-01 03:01:43 +00:00
|
|
|
return GameStateEvaluator.evaluate(playerId, game);
|
|
|
|
}
|
|
|
|
node.setGameValue(game.getState().getValue());
|
|
|
|
SimulatedPlayer currentPlayer = (SimulatedPlayer) game.getPlayer(game.getPlayerList().get());
|
2011-02-26 05:16:04 +00:00
|
|
|
boolean isSimulatedPlayer = currentPlayer.getId().equals(playerId);
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating -- player " + currentPlayer.getName());
|
2010-09-01 03:01:43 +00:00
|
|
|
SimulationNode bestNode = null;
|
|
|
|
List<Ability> allActions = currentPlayer.simulatePriority(game, filter);
|
2011-02-14 19:41:29 +00:00
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
logger.debug("simulating -- adding " + allActions.size() + " children:" + allActions);
|
2010-09-01 03:01:43 +00:00
|
|
|
for (Ability action: allActions) {
|
|
|
|
Game sim = game.copy();
|
2010-12-13 05:01:35 +00:00
|
|
|
if (sim.getPlayer(currentPlayer.getId()).activateAbility((ActivatedAbility) action.copy(), sim)) {
|
2010-09-01 03:01:43 +00:00
|
|
|
sim.applyEffects();
|
2011-02-19 04:22:31 +00:00
|
|
|
if (checkForRepeatedAction(sim, node, action, currentPlayer.getId()))
|
|
|
|
continue;
|
2010-09-01 03:01:43 +00:00
|
|
|
if (!sim.isGameOver() && action.isUsesStack()) {
|
|
|
|
// only pass if the last action uses the stack
|
|
|
|
sim.getPlayer(currentPlayer.getId()).pass();
|
|
|
|
sim.getPlayerList().getNext();
|
|
|
|
}
|
2011-02-19 04:22:31 +00:00
|
|
|
SimulationNode newNode = new SimulationNode(node, sim, action, depth, currentPlayer.getId());
|
2011-02-14 19:41:29 +00:00
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
logger.debug("simulating -- node #:" + SimulationNode.getCount() + " actions:" + action);
|
2010-09-01 03:01:43 +00:00
|
|
|
sim.checkStateAndTriggered();
|
|
|
|
int val = addActions(newNode, filter, depth-1, alpha, beta);
|
2011-02-26 05:16:04 +00:00
|
|
|
if (!isSimulatedPlayer) {
|
2010-09-01 03:01:43 +00:00
|
|
|
if (val < beta) {
|
|
|
|
beta = val;
|
|
|
|
bestNode = newNode;
|
|
|
|
node.setCombat(newNode.getCombat());
|
|
|
|
}
|
2011-02-26 05:16:04 +00:00
|
|
|
if (val == GameStateEvaluator.LOSE_SCORE) {
|
|
|
|
logger.debug("simulating -- lose, can't do worse than this");
|
|
|
|
break;
|
|
|
|
}
|
2010-09-01 03:01:43 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (val > alpha) {
|
|
|
|
alpha = val;
|
|
|
|
bestNode = newNode;
|
|
|
|
node.setCombat(newNode.getCombat());
|
|
|
|
if (node.getTargets().size() > 0)
|
|
|
|
targets = node.getTargets();
|
|
|
|
if (node.getChoices().size() > 0)
|
|
|
|
choices = node.getChoices();
|
|
|
|
}
|
2011-02-26 05:16:04 +00:00
|
|
|
if (val == GameStateEvaluator.WIN_SCORE) {
|
|
|
|
logger.debug("simulating -- win, can't do better than this");
|
|
|
|
break;
|
|
|
|
}
|
2010-09-01 03:01:43 +00:00
|
|
|
}
|
|
|
|
if (alpha >= beta) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating -- pruning");
|
2010-09-01 03:01:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (SimulationNode.nodeCount > maxNodes) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating -- reached end-state");
|
2010-09-01 03:01:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bestNode != null) {
|
|
|
|
node.children.clear();
|
|
|
|
node.children.add(bestNode);
|
|
|
|
}
|
2011-02-26 05:16:04 +00:00
|
|
|
if (!isSimulatedPlayer) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("returning priority beta: " + beta);
|
2010-09-01 03:01:43 +00:00
|
|
|
return beta;
|
|
|
|
}
|
|
|
|
else {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("returning priority alpha: " + alpha);
|
2010-09-01 03:01:43 +00:00
|
|
|
return alpha;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean allPassed(Game game) {
|
|
|
|
for (Player player: game.getPlayers().values()) {
|
|
|
|
if (!player.isPassed() && !player.hasLost() && !player.hasLeft())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean choose(Outcome outcome, Choice choice, Game game) {
|
|
|
|
if (choices.size() == 0)
|
|
|
|
return super.choose(outcome, choice, game);
|
|
|
|
if (!choice.isChosen()) {
|
|
|
|
for (String achoice: choices) {
|
|
|
|
choice.setChoice(achoice);
|
|
|
|
if (choice.isChosen()) {
|
|
|
|
choices.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean chooseTarget(Cards cards, TargetCard target, Ability source, Game game) {
|
|
|
|
if (targets.size() == 0)
|
|
|
|
return super.chooseTarget(cards, target, source, game);
|
|
|
|
if (!target.doneChosing()) {
|
|
|
|
for (UUID targetId: targets) {
|
|
|
|
target.addTarget(targetId, source, game);
|
|
|
|
if (target.doneChosing()) {
|
|
|
|
targets.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-23 05:08:45 +00:00
|
|
|
@Override
|
|
|
|
public boolean choose(Cards cards, TargetCard target, Game game) {
|
|
|
|
if (targets.size() == 0)
|
|
|
|
return super.choose(cards, target, game);
|
|
|
|
if (!target.doneChosing()) {
|
|
|
|
for (UUID targetId: targets) {
|
|
|
|
target.add(targetId, game);
|
|
|
|
if (target.doneChosing()) {
|
|
|
|
targets.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-25 03:01:04 +00:00
|
|
|
@Override
|
|
|
|
public boolean playXMana(VariableManaCost cost, ManaCosts<ManaCost> costs, Game game) {
|
|
|
|
//SimulatedPlayer.simulateVariableCosts method adds a generic mana cost for each option
|
|
|
|
for (ManaCost manaCost: costs) {
|
|
|
|
if (manaCost instanceof GenericManaCost) {
|
|
|
|
cost.setPayment(manaCost.getPayment());
|
|
|
|
logger.debug("using X = " + cost.getPayment().count());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cost.setPaid();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-01 03:01:43 +00:00
|
|
|
public void playNext(Game game, UUID activePlayerId, SimulationNode node) {
|
|
|
|
boolean skip = false;
|
|
|
|
while (true) {
|
|
|
|
Phase currentPhase = game.getPhase();
|
|
|
|
if (!skip)
|
|
|
|
currentPhase.getStep().endStep(game, activePlayerId);
|
|
|
|
game.applyEffects();
|
|
|
|
switch (currentPhase.getStep().getType()) {
|
|
|
|
case UNTAP:
|
|
|
|
game.getPhase().setStep(new UpkeepStep());
|
|
|
|
break;
|
|
|
|
case UPKEEP:
|
|
|
|
game.getPhase().setStep(new DrawStep());
|
|
|
|
break;
|
|
|
|
case DRAW:
|
|
|
|
game.getTurn().setPhase(new PreCombatMainPhase());
|
|
|
|
game.getPhase().setStep(new PreCombatMainStep());
|
|
|
|
break;
|
|
|
|
case PRECOMBAT_MAIN:
|
|
|
|
game.getTurn().setPhase(new CombatPhase());
|
|
|
|
game.getPhase().setStep(new BeginCombatStep());
|
|
|
|
break;
|
|
|
|
case BEGIN_COMBAT:
|
|
|
|
game.getPhase().setStep(new DeclareAttackersStep());
|
|
|
|
break;
|
|
|
|
case DECLARE_ATTACKERS:
|
|
|
|
game.getPhase().setStep(new DeclareBlockersStep());
|
|
|
|
break;
|
|
|
|
case DECLARE_BLOCKERS:
|
|
|
|
game.getPhase().setStep(new CombatDamageStep(true));
|
|
|
|
break;
|
|
|
|
case COMBAT_DAMAGE:
|
|
|
|
if (((CombatDamageStep)currentPhase.getStep()).getFirst())
|
|
|
|
game.getPhase().setStep(new CombatDamageStep(false));
|
|
|
|
else
|
|
|
|
game.getPhase().setStep(new EndOfCombatStep());
|
|
|
|
break;
|
|
|
|
case END_COMBAT:
|
|
|
|
game.getTurn().setPhase(new PostCombatMainPhase());
|
|
|
|
game.getPhase().setStep(new PostCombatMainStep());
|
|
|
|
break;
|
|
|
|
case POSTCOMBAT_MAIN:
|
|
|
|
game.getTurn().setPhase(new EndPhase());
|
|
|
|
game.getPhase().setStep(new EndStep());
|
|
|
|
break;
|
|
|
|
case END_TURN:
|
|
|
|
game.getPhase().setStep(new CleanupStep());
|
|
|
|
break;
|
|
|
|
case CLEANUP:
|
|
|
|
game.getPhase().getStep().beginStep(game, activePlayerId);
|
|
|
|
if (!game.checkStateAndTriggered() && !game.isGameOver()) {
|
|
|
|
game.getState().setActivePlayerId(game.getState().getPlayerList(game.getActivePlayerId()).getNext());
|
|
|
|
game.getTurn().setPhase(new BeginningPhase());
|
|
|
|
game.getPhase().setStep(new UntapStep());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!game.getStep().skipStep(game, game.getActivePlayerId())) {
|
|
|
|
if (game.getTurn().getStepType() == PhaseStep.DECLARE_ATTACKERS) {
|
|
|
|
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_PRE, null, null, activePlayerId));
|
|
|
|
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, activePlayerId, activePlayerId))) {
|
|
|
|
for (Combat engagement: ((SimulatedPlayer)game.getPlayer(activePlayerId)).addAttackers(game)) {
|
|
|
|
Game sim = game.copy();
|
|
|
|
UUID defenderId = game.getOpponents(playerId).iterator().next();
|
|
|
|
for (CombatGroup group: engagement.getGroups()) {
|
|
|
|
for (UUID attackerId: group.getAttackers()) {
|
|
|
|
sim.getPlayer(activePlayerId).declareAttacker(attackerId, defenderId, sim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_ATTACKERS, playerId, playerId));
|
2011-02-19 04:22:31 +00:00
|
|
|
SimulationNode newNode = new SimulationNode(node, sim, node.getDepth()-1, activePlayerId);
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating -- node #:" + SimulationNode.getCount() + " declare attakers");
|
2010-09-01 03:01:43 +00:00
|
|
|
newNode.setCombat(sim.getCombat());
|
|
|
|
node.children.add(newNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (game.getTurn().getStepType() == PhaseStep.DECLARE_BLOCKERS) {
|
|
|
|
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_PRE, null, null, activePlayerId));
|
|
|
|
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_BLOCKERS, activePlayerId, activePlayerId))) {
|
|
|
|
for (UUID defenderId: game.getCombat().getDefenders()) {
|
|
|
|
//check if defender is being attacked
|
|
|
|
if (game.getCombat().isAttacked(defenderId, game)) {
|
|
|
|
for (Combat engagement: ((SimulatedPlayer)game.getPlayer(defenderId)).addBlockers(game)) {
|
|
|
|
Game sim = game.copy();
|
|
|
|
for (CombatGroup group: engagement.getGroups()) {
|
|
|
|
for (UUID blockerId: group.getBlockers()) {
|
|
|
|
group.addBlocker(blockerId, defenderId, sim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
|
2011-02-19 04:22:31 +00:00
|
|
|
SimulationNode newNode = new SimulationNode(node, sim, node.getDepth()-1, defenderId);
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("simulating -- node #:" + SimulationNode.getCount() + " declare blockers");
|
2010-09-01 03:01:43 +00:00
|
|
|
newNode.setCombat(sim.getCombat());
|
|
|
|
node.children.add(newNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
game.getStep().beginStep(game, activePlayerId);
|
|
|
|
}
|
|
|
|
if (game.getStep().getHasPriority())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
skip = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
game.checkStateAndTriggered();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void selectAttackers(Game game) {
|
2011-02-27 21:30:14 +00:00
|
|
|
if (logger.isDebugEnabled() && (combat == null || combat.getGroups().isEmpty()))
|
2011-02-25 03:01:04 +00:00
|
|
|
logger.debug("not attacking");
|
2010-09-01 03:01:43 +00:00
|
|
|
if (combat != null) {
|
|
|
|
UUID opponentId = game.getCombat().getDefenders().iterator().next();
|
|
|
|
for (UUID attackerId: combat.getAttackers()) {
|
|
|
|
this.declareAttacker(attackerId, opponentId, game);
|
2011-02-25 03:01:04 +00:00
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
logger.debug("attacking with:" + game.getPermanent(attackerId).getName());
|
|
|
|
}
|
2010-09-01 03:01:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void selectBlockers(Game game) {
|
2011-02-14 19:41:29 +00:00
|
|
|
logger.debug("selectBlockers");
|
2010-09-01 03:01:43 +00:00
|
|
|
if (combat != null && combat.getGroups().size() > 0) {
|
|
|
|
List<CombatGroup> groups = game.getCombat().getGroups();
|
2010-12-22 04:26:51 +00:00
|
|
|
for (int i = 0; i < groups.size(); i++) {
|
|
|
|
if (i < combat.getGroups().size()) {
|
|
|
|
for (UUID blockerId: combat.getGroups().get(i).getBlockers()) {
|
|
|
|
this.declareBlocker(blockerId, groups.get(i).getAttackers().get(0), game);
|
2011-02-25 03:01:04 +00:00
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
logger.debug("blocking with:" + game.getPermanent(blockerId).getName());
|
2010-12-22 04:26:51 +00:00
|
|
|
}
|
2010-09-01 03:01:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies game and replaces all players in copy with simulated players
|
|
|
|
*
|
|
|
|
* @param game
|
|
|
|
* @return a new game object with simulated players
|
|
|
|
*/
|
|
|
|
protected Game createSimulation(Game game) {
|
|
|
|
Game sim = game.copy();
|
|
|
|
|
|
|
|
for (Player copyPlayer: sim.getState().getPlayers().values()) {
|
|
|
|
Player origPlayer = game.getState().getPlayers().get(copyPlayer.getId());
|
|
|
|
SimulatedPlayer newPlayer = new SimulatedPlayer(copyPlayer.getId(), copyPlayer.getId().equals(playerId));
|
|
|
|
newPlayer.restore(origPlayer);
|
|
|
|
sim.getState().getPlayers().put(copyPlayer.getId(), newPlayer);
|
|
|
|
}
|
|
|
|
return sim;
|
|
|
|
}
|
|
|
|
|
2011-02-19 04:22:31 +00:00
|
|
|
private boolean checkForRepeatedAction(Game sim, SimulationNode node, Ability action, UUID playerId) {
|
|
|
|
if (action instanceof PassAbility)
|
|
|
|
return false;
|
|
|
|
int val = GameStateEvaluator.evaluate(playerId, sim);
|
|
|
|
SimulationNode test = node.getParent();
|
|
|
|
while (test != null && !test.getPlayerId().equals(playerId)) {
|
|
|
|
test = test.getParent();
|
|
|
|
}
|
|
|
|
if (test != null && test.getAbilities() != null && test.getAbilities().size() == 1) {
|
|
|
|
if (action.toString().equals(test.getAbilities().get(0).toString()) && GameStateEvaluator.evaluate(playerId, sim) == val) {
|
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
logger.debug("found repeated action " + action);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-01 03:01:43 +00:00
|
|
|
}
|