use of mage.abilities.Ability in project mage by magefree.
the class ComputerPlayer2 method act.
protected void act(Game game) {
if (actions == null || actions.isEmpty())
pass(game);
else {
boolean usedStack = false;
while (actions.peek() != null) {
Ability ability = actions.poll();
this.activateAbility((ActivatedAbility) ability, game);
if (logger.isDebugEnabled())
logger.debug("activating: " + ability);
if (ability.isUsesStack())
usedStack = true;
}
if (usedStack)
pass(game);
}
logger.info("Turn " + game.getTurnNum() + " Step " + game.getStep().toString() + " Player " + name + " Life " + life);
}
use of mage.abilities.Ability in project mage by magefree.
the class ComputerPlayer2 method simulatePriority.
protected int simulatePriority(SimulationNode node, Game game, int alpha, int beta) {
// NOT USED in real AI, see ComputerPlayer6
if (Thread.interrupted()) {
Thread.currentThread().interrupt();
logger.debug(indent(node.depth) + "interrupted");
return GameStateEvaluator.evaluate(playerId, game);
}
node.setGameValue(game.getState().getValue(true).hashCode());
SimulatedPlayer currentPlayer = (SimulatedPlayer) game.getPlayer(game.getPlayerList().get());
boolean isSimulatedPlayer = currentPlayer.getId().equals(playerId);
logger.debug(indent(node.depth) + "simulating priority -- player " + currentPlayer.getName());
SimulationNode bestNode = null;
List<Ability> allActions = currentPlayer.simulatePriority(game);
if (logger.isDebugEnabled())
logger.debug(indent(node.depth) + "simulating -- adding " + allActions.size() + " children:" + allActions);
for (Ability action : allActions) {
if (Thread.interrupted()) {
Thread.currentThread().interrupt();
logger.debug(indent(node.depth) + "interrupted");
break;
}
Game sim = game.copy();
if (sim.getPlayer(currentPlayer.getId()).activateAbility((ActivatedAbility) action.copy(), sim)) {
sim.applyEffects();
if (checkForUselessAction(sim, node, action, currentPlayer.getId())) {
logger.debug(indent(node.depth) + "found useless action: " + action);
continue;
}
if (!sim.checkIfGameIsOver() && action.isUsesStack()) {
// only pass if the last action uses the stack
sim.getPlayer(currentPlayer.getId()).pass(game);
sim.getPlayerList().getNext();
}
SimulationNode newNode = new SimulationNode(node, sim, action, currentPlayer.getId());
if (logger.isDebugEnabled())
logger.debug(indent(newNode.depth) + "simulating -- node #:" + SimulationNode.getCount() + " actions:" + action);
sim.checkStateAndTriggered();
int val = addActions(newNode, alpha, beta);
if (!isSimulatedPlayer) {
if (val < beta) {
beta = val;
bestNode = newNode;
node.setCombat(newNode.getCombat());
}
if (val == GameStateEvaluator.LOSE_SCORE) {
logger.debug(indent(node.depth) + "simulating -- lose, can't do worse than this");
break;
}
} else {
if (val > alpha) {
alpha = val;
bestNode = newNode;
node.setCombat(newNode.getCombat());
if (!node.getTargets().isEmpty())
targets = node.getTargets();
if (!node.getChoices().isEmpty())
choices = node.getChoices();
}
if (val == GameStateEvaluator.WIN_SCORE) {
logger.debug(indent(node.depth) + "simulating -- win, can't do better than this");
break;
}
}
if (alpha >= beta) {
logger.debug(indent(node.depth) + "simulating -- pruning");
break;
}
// if (SimulationNode.nodeCount > maxNodes) {
// logger.debug(indent(node.depth) + "simulating -- reached end-state");
// break;
// }
}
}
if (bestNode != null) {
node.children.clear();
node.children.add(bestNode);
}
if (!isSimulatedPlayer) {
logger.debug(indent(node.depth) + "returning priority beta: " + beta);
return beta;
} else {
logger.debug(indent(node.depth) + "returning priority alpha: " + alpha);
return alpha;
}
}
use of mage.abilities.Ability in project mage by magefree.
the class SimulatedPlayer method simulateOptions.
protected void simulateOptions(Game game, ActivatedAbility previousActions) {
allActions.add(previousActions);
List<ActivatedAbility> playables = game.getPlayer(playerId).getPlayable(game, isSimulatedPlayer);
for (ActivatedAbility ability : playables) {
List<Ability> options = game.getPlayer(playerId).getPlayableOptions(ability, game);
if (options.isEmpty()) {
if (!ability.getManaCosts().getVariableCosts().isEmpty()) {
simulateVariableCosts(ability, game);
} else {
allActions.add(ability);
}
// simulateAction(game, previousActions, ability);
} else {
// ExecutorService simulationExecutor = Executors.newFixedThreadPool(4);
for (Ability option : options) {
if (!ability.getManaCosts().getVariableCosts().isEmpty()) {
simulateVariableCosts(option, game);
} else {
allActions.add(option);
}
// SimulationWorker worker = new SimulationWorker(game, this, previousActions, option);
// simulationExecutor.submit(worker);
}
// simulationExecutor.shutdown();
// while(!simulationExecutor.isTerminated()) {}
}
}
}
use of mage.abilities.Ability in project mage by magefree.
the class SimulatedPlayer method triggerAbility.
@Override
public boolean triggerAbility(TriggeredAbility source, Game game) {
Ability ability = source.copy();
List<Ability> options = getPlayableOptions(ability, game);
if (options.isEmpty()) {
if (logger.isDebugEnabled())
logger.debug("simulating -- triggered ability:" + ability);
game.getStack().push(new StackAbility(ability, playerId));
if (ability.activate(game, false) && ability.isUsesStack()) {
game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
}
game.applyEffects();
game.getPlayers().resetPassed();
} else {
SimulationNode parent = (SimulationNode) game.getCustomData();
if (parent.getDepth() == maxDepth)
return true;
logger.debug(indent(parent.getDepth()) + "simulating -- triggered ability - adding children:" + options.size());
for (Ability option : options) {
addAbilityNode(parent, option, game);
}
}
return true;
}
use of mage.abilities.Ability in project mage by magefree.
the class DeflectingPalmEffect method replaceEvent.
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
PreventionEffectData preventionData = preventDamageAction(event, source, game);
this.used = true;
// only one use
this.discard();
if (preventionData.getPreventedDamage() > 0) {
MageObject damageDealingObject = game.getObject(target.getFirstTarget());
UUID objectControllerId = null;
if (damageDealingObject instanceof Permanent) {
objectControllerId = ((Permanent) damageDealingObject).getControllerId();
} else if (damageDealingObject instanceof Ability) {
objectControllerId = ((Ability) damageDealingObject).getControllerId();
} else if (damageDealingObject instanceof Spell) {
objectControllerId = ((Spell) damageDealingObject).getControllerId();
}
if (objectControllerId != null) {
Player objectController = game.getPlayer(objectControllerId);
if (objectController != null) {
objectController.damage(preventionData.getPreventedDamage(), source.getSourceId(), source, game);
}
}
}
return true;
}
Aggregations