Search in sources :

Example 1 with Ability

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);
}
Also used : PassAbility(mage.abilities.common.PassAbility) StackAbility(mage.game.stack.StackAbility) ActivatedAbility(mage.abilities.ActivatedAbility) Ability(mage.abilities.Ability)

Example 2 with Ability

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;
    }
}
Also used : PassAbility(mage.abilities.common.PassAbility) StackAbility(mage.game.stack.StackAbility) ActivatedAbility(mage.abilities.ActivatedAbility) Ability(mage.abilities.Ability) Game(mage.game.Game)

Example 3 with Ability

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()) {}
        }
    }
}
Also used : PassAbility(mage.abilities.common.PassAbility) StackAbility(mage.game.stack.StackAbility) TriggeredAbility(mage.abilities.TriggeredAbility) SpellAbility(mage.abilities.SpellAbility) ActivatedAbility(mage.abilities.ActivatedAbility) Ability(mage.abilities.Ability) ActivatedAbility(mage.abilities.ActivatedAbility)

Example 4 with Ability

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;
}
Also used : PassAbility(mage.abilities.common.PassAbility) StackAbility(mage.game.stack.StackAbility) TriggeredAbility(mage.abilities.TriggeredAbility) SpellAbility(mage.abilities.SpellAbility) ActivatedAbility(mage.abilities.ActivatedAbility) Ability(mage.abilities.Ability) GameEvent(mage.game.events.GameEvent) StackAbility(mage.game.stack.StackAbility)

Example 5 with Ability

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;
}
Also used : Ability(mage.abilities.Ability) PreventionEffectData(mage.abilities.effects.PreventionEffectData) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) MageObject(mage.MageObject) UUID(java.util.UUID) Spell(mage.game.stack.Spell)

Aggregations

Ability (mage.abilities.Ability)359 Player (mage.players.Player)173 Permanent (mage.game.permanent.Permanent)134 UUID (java.util.UUID)126 Game (mage.game.Game)123 OneShotEffect (mage.abilities.effects.OneShotEffect)98 SimpleStaticAbility (mage.abilities.common.SimpleStaticAbility)87 CardSetInfo (mage.cards.CardSetInfo)87 CardImpl (mage.cards.CardImpl)85 CardType (mage.constants.CardType)78 Outcome (mage.constants.Outcome)76 Card (mage.cards.Card)72 SimpleActivatedAbility (mage.abilities.common.SimpleActivatedAbility)64 MageObject (mage.MageObject)57 MageInt (mage.MageInt)47 SpellAbility (mage.abilities.SpellAbility)43 Objects (java.util.Objects)38 Zone (mage.constants.Zone)38 mage.constants (mage.constants)37 FilterCard (mage.filter.FilterCard)35