Search in sources :

Example 11 with Combat

use of mage.game.combat.Combat in project mage by magefree.

the class SimulatedPlayer method addBlockers.

public List<Combat> addBlockers(Game game) {
    Map<Integer, Combat> engagements = new HashMap<>();
    int numGroups = game.getCombat().getGroups().size();
    if (numGroups == 0)
        return new ArrayList<>();
    // add a node with no blockers
    Game sim = game.copy();
    engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat());
    sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
    List<Permanent> blockers = getAvailableBlockers(game);
    addBlocker(game, blockers, engagements);
    return new ArrayList<>(engagements.values());
}
Also used : Game(mage.game.Game) Permanent(mage.game.permanent.Permanent) Combat(mage.game.combat.Combat)

Example 12 with Combat

use of mage.game.combat.Combat in project mage by magefree.

the class CombatUtil method getCombatInfo.

public static SurviveInfo getCombatInfo(Game game, UUID attackingPlayerId, UUID defendingPlayerId, Permanent attacker) {
    Game sim = game.copy();
    Combat combat = sim.getCombat();
    combat.setAttacker(attackingPlayerId);
    combat.setDefenders(sim);
    UUID defenderId = sim.getCombat().getDefenders().iterator().next();
    boolean triggered = false;
    sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, defendingPlayerId, defendingPlayerId));
    sim.checkStateAndTriggered();
    while (!sim.getStack().isEmpty()) {
        triggered = true;
        sim.getStack().resolve(sim);
        sim.applyEffects();
    }
    sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_POST, sim.getActivePlayerId(), sim.getActivePlayerId()));
    simulateStep(sim, new FirstCombatDamageStep());
    simulateStep(sim, new CombatDamageStep());
    simulateStep(sim, new EndOfCombatStep());
    // sim.checkStateAndTriggered();
    while (!sim.getStack().isEmpty()) {
        triggered = true;
        sim.getStack().resolve(sim);
        sim.applyEffects();
    }
    return new SurviveInfo(!sim.getBattlefield().containsPermanent(attacker.getId()), false, sim.getPlayer(defenderId), triggered);
}
Also used : FirstCombatDamageStep(mage.game.turn.FirstCombatDamageStep) CombatDamageStep(mage.game.turn.CombatDamageStep) Game(mage.game.Game) EndOfCombatStep(mage.game.turn.EndOfCombatStep) Combat(mage.game.combat.Combat) FirstCombatDamageStep(mage.game.turn.FirstCombatDamageStep)

Example 13 with Combat

use of mage.game.combat.Combat in project mage by magefree.

the class CombatUtil method willItSurvive2.

public static SurviveInfo willItSurvive2(Game game, UUID attackingPlayerId, UUID defendingPlayerId, Permanent attacker, Permanent blocker) {
    Game sim = game.copy();
    Combat combat = sim.getCombat();
    combat.setAttacker(attackingPlayerId);
    combat.setDefenders(sim);
    if (blocker == null || attacker == null || sim.getPlayer(defendingPlayerId) == null) {
        return null;
    }
    if (attacker.getPower().getValue() >= blocker.getToughness().getValue()) {
        sim.getBattlefield().removePermanent(blocker.getId());
    }
    if (attacker.getToughness().getValue() <= blocker.getPower().getValue()) {
        sim.getBattlefield().removePermanent(attacker.getId());
    }
    return new SurviveInfo(!sim.getBattlefield().containsPermanent(attacker.getId()), !sim.getBattlefield().containsPermanent(blocker.getId()));
}
Also used : Game(mage.game.Game) Combat(mage.game.combat.Combat)

Example 14 with Combat

use of mage.game.combat.Combat in project mage by magefree.

the class ComputerPlayer6 method minimaxAB.

protected int minimaxAB(SimulationNode2 node, int depth, int alpha, int beta) {
    logger.trace("Sim minimaxAB [" + depth + "] -- a: " + alpha + " b: " + beta + " <" + (node != null ? node.getScore() : "null") + '>');
    UUID currentPlayerId = node.getGame().getPlayerList().get();
    SimulationNode2 bestChild = null;
    for (SimulationNode2 child : node.getChildren()) {
        Combat _combat = child.getCombat();
        if (alpha >= beta) {
            break;
        }
        if (SimulationNode2.nodeCount > maxNodes) {
            break;
        }
        int val = addActions(child, depth - 1, alpha, beta);
        if (!currentPlayerId.equals(playerId)) {
            if (val < beta) {
                beta = val;
                bestChild = child;
                if (node.getCombat() == null) {
                    node.setCombat(_combat);
                    bestChild.setCombat(_combat);
                }
            }
            // no need to check other actions
            if (val == GameStateEvaluator2.LOSE_GAME_SCORE) {
                logger.debug("lose - break");
                break;
            }
        } else {
            if (val > alpha) {
                alpha = val;
                bestChild = child;
                if (node.getCombat() == null) {
                    node.setCombat(_combat);
                    bestChild.setCombat(_combat);
                }
            }
            // no need to check other actions
            if (val == GameStateEvaluator2.WIN_GAME_SCORE) {
                logger.debug("win - break");
                break;
            }
        }
    }
    node.children.clear();
    if (bestChild != null) {
        node.children.add(bestChild);
    }
    if (!currentPlayerId.equals(playerId)) {
        return beta;
    } else {
        return alpha;
    }
}
Also used : Combat(mage.game.combat.Combat)

Example 15 with Combat

use of mage.game.combat.Combat in project mage by magefree.

the class ComputerPlayer3 method simulateAttackers.

protected int simulateAttackers(Game game, SimulationNode node, UUID attackerId, int alpha, int beta, boolean counter) {
    if (Thread.interrupted()) {
        Thread.currentThread().interrupt();
        logger.debug(indent(node.depth) + "interrupted");
        return GameStateEvaluator.evaluate(playerId, game);
    }
    Integer val = null;
    SimulationNode bestNode = null;
    SimulatedPlayer attacker = (SimulatedPlayer) game.getPlayer(attackerId);
    UUID defenderId = game.getOpponents(attackerId).iterator().next();
    if (logger.isDebugEnabled()) {
        logger.debug(indent(node.depth) + attacker.getName() + "'s possible attackers: " + attacker.getAvailableAttackers(defenderId, game));
    }
    List<Combat> engagements = attacker.addAttackers(game);
    for (Combat engagement : engagements) {
        if (alpha >= beta) {
            logger.debug(indent(node.depth) + "simulating -- pruning attackers");
            break;
        }
        Game sim = game.copy();
        for (CombatGroup group : engagement.getGroups()) {
            for (UUID attackId : group.getAttackers()) {
                sim.getPlayer(attackerId).declareAttacker(attackId, defenderId, sim, false);
            }
        }
        sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_ATTACKERS, attackerId, attackerId));
        SimulationNode newNode = new SimulationNode(node, sim, attackerId);
        if (logger.isDebugEnabled()) {
            logger.debug(indent(node.depth) + "simulating attack for player:" + game.getPlayer(attackerId).getName());
        }
        sim.checkStateAndTriggered();
        while (!sim.getStack().isEmpty()) {
            sim.getStack().resolve(sim);
            logger.debug(indent(node.depth) + "resolving triggered abilities");
            sim.applyEffects();
        }
        sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_POST, sim.getActivePlayerId(), sim.getActivePlayerId()));
        Combat simCombat = sim.getCombat().copy();
        sim.getPhase().setStep(new DeclareBlockersStep());
        val = simulateCombat(sim, newNode, alpha, beta, counter);
        if (!attackerId.equals(playerId)) {
            if (val < beta) {
                beta = val;
                bestNode = newNode;
                node.setCombat(simCombat);
            }
        } else {
            if (val > alpha) {
                alpha = val;
                bestNode = newNode;
                node.setCombat(simCombat);
            }
        }
    }
    if (val == null) {
        val = GameStateEvaluator.evaluate(playerId, game);
    }
    if (bestNode != null) {
        node.children.clear();
        node.children.add(bestNode);
    }
    if (logger.isDebugEnabled()) {
        logger.debug(indent(node.depth) + "returning -- combat attacker score: " + val + " depth:" + node.depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
    }
    return val;
}
Also used : Game(mage.game.Game) Combat(mage.game.combat.Combat) UUID(java.util.UUID) CombatGroup(mage.game.combat.CombatGroup)

Aggregations

Combat (mage.game.combat.Combat)15 Game (mage.game.Game)11 UUID (java.util.UUID)6 CombatGroup (mage.game.combat.CombatGroup)5 Permanent (mage.game.permanent.Permanent)5 GameEvent (mage.game.events.GameEvent)2 CombatDamageStep (mage.game.turn.CombatDamageStep)2 EndOfCombatStep (mage.game.turn.EndOfCombatStep)2 FirstCombatDamageStep (mage.game.turn.FirstCombatDamageStep)2 HashSet (java.util.HashSet)1 List (java.util.List)1 Stream (java.util.stream.Stream)1 MageObject (mage.MageObject)1 Ability (mage.abilities.Ability)1 CastOnlyDuringPhaseStepSourceAbility (mage.abilities.common.CastOnlyDuringPhaseStepSourceAbility)1 ContinuousRuleModifyingEffectImpl (mage.abilities.effects.ContinuousRuleModifyingEffectImpl)1 Effect (mage.abilities.effects.Effect)1 OneShotEffect (mage.abilities.effects.OneShotEffect)1 CardImpl (mage.cards.CardImpl)1 CardSetInfo (mage.cards.CardSetInfo)1