Search in sources :

Example 1 with Combat

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

the class ComputerPlayerMCTS method selectAttackers.

@Override
public void selectAttackers(Game game, UUID attackingPlayerId) {
    StringBuilder sb = new StringBuilder();
    sb.append(game.getTurn().getValue(game.getTurnNum())).append(" player ").append(name).append(" attacking with: ");
    getNextAction(game, NextAction.SELECT_ATTACKERS);
    Combat combat = root.getCombat();
    UUID opponentId = game.getCombat().getDefenders().iterator().next();
    for (UUID attackerId : combat.getAttackers()) {
        this.declareAttacker(attackerId, opponentId, game, false);
        sb.append(game.getPermanent(attackerId).getName()).append(',');
    }
    logger.info(sb.toString());
    MCTSNode.logHitMiss();
}
Also used : Combat(mage.game.combat.Combat) UUID(java.util.UUID)

Example 2 with Combat

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

the class ComputerPlayerMCTS method selectBlockers.

@Override
public void selectBlockers(Ability source, Game game, UUID defendingPlayerId) {
    StringBuilder sb = new StringBuilder();
    sb.append(game.getTurn().getValue(game.getTurnNum())).append(" player ").append(name).append(" blocking: ");
    getNextAction(game, NextAction.SELECT_BLOCKERS);
    Combat simulatedCombat = root.getCombat();
    List<CombatGroup> currentGroups = game.getCombat().getGroups();
    for (int i = 0; i < currentGroups.size(); i++) {
        if (i < simulatedCombat.getGroups().size()) {
            CombatGroup currentGroup = currentGroups.get(i);
            CombatGroup simulatedGroup = simulatedCombat.getGroups().get(i);
            sb.append(game.getPermanent(currentGroup.getAttackers().get(0)).getName()).append(" with: ");
            for (UUID blockerId : simulatedGroup.getBlockers()) {
                // blockers can be added automaticly by requirement effects, so we must add only missing blockers
                if (!currentGroup.getBlockers().contains(blockerId)) {
                    this.declareBlocker(this.getId(), blockerId, currentGroup.getAttackers().get(0), game);
                    sb.append(game.getPermanent(blockerId).getName()).append(',');
                }
            }
            sb.append('|');
        }
    }
    logger.info(sb.toString());
    MCTSNode.logHitMiss();
}
Also used : Combat(mage.game.combat.Combat) UUID(java.util.UUID) CombatGroup(mage.game.combat.CombatGroup)

Example 3 with Combat

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

the class SimulatedPlayer method addAttackers.

/*@Override
    public boolean playXMana(VariableManaCost cost, ManaCosts<ManaCost> costs, Game game) {
        //simulateVariableCosts method adds a generic mana cost for each option
        for (ManaCost manaCost: costs) {
            if (manaCost instanceof GenericManaCost) {
                cost.setPayment(manaCost.getPayment());
                logger.debug("simulating -- X = " + cost.getPayment().count());
                break;
            }
        }
        cost.setPaid();
        return true;
    }*/
public List<Combat> addAttackers(Game game) {
    Map<Integer, Combat> engagements = new HashMap<>();
    // useful only for two player games - will only attack first opponent
    UUID defenderId = game.getOpponents(playerId).iterator().next();
    List<Permanent> attackersList = super.getAvailableAttackers(defenderId, game);
    // use binary digits to calculate powerset of attackers
    int powerElements = (int) Math.pow(2, attackersList.size());
    StringBuilder binary = new StringBuilder();
    for (int i = powerElements - 1; i >= 0; i--) {
        Game sim = game.copy();
        binary.setLength(0);
        binary.append(Integer.toBinaryString(i));
        while (binary.length() < attackersList.size()) {
            binary.insert(0, '0');
        }
        for (int j = 0; j < attackersList.size(); j++) {
            if (binary.charAt(j) == '1') {
                // makes it possible to UNDO a declared attacker with costs from e.g. Propaganda
                setStoredBookmark(sim.bookmarkState());
                if (!sim.getCombat().declareAttacker(attackersList.get(j).getId(), defenderId, playerId, sim)) {
                    sim.undo(playerId);
                }
            }
        }
        if (engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat()) != null) {
            logger.debug("simulating -- found redundant attack combination");
        } else if (logger.isDebugEnabled()) {
            logger.debug("simulating -- attack:" + sim.getCombat().getGroups().size());
        }
    }
    return new ArrayList<>(engagements.values());
}
Also used : Game(mage.game.Game) Permanent(mage.game.permanent.Permanent) Combat(mage.game.combat.Combat)

Example 4 with Combat

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

the class ComputerPlayer3 method simulateBlockers.

protected int simulateBlockers(Game game, SimulationNode node, UUID defenderId, 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;
    // check if defender is being attacked
    if (game.getCombat().isAttacked(defenderId, game)) {
        SimulatedPlayer defender = (SimulatedPlayer) game.getPlayer(defenderId);
        if (logger.isDebugEnabled()) {
            logger.debug(indent(node.depth) + defender.getName() + "'s possible blockers: " + defender.getAvailableBlockers(game));
        }
        for (Combat engagement : defender.addBlockers(game)) {
            if (alpha >= beta) {
                logger.debug(indent(node.depth) + "simulating -- pruning blockers");
                break;
            }
            Game sim = game.copy();
            for (CombatGroup group : engagement.getGroups()) {
                if (!group.getAttackers().isEmpty()) {
                    UUID attackerId = group.getAttackers().get(0);
                    for (UUID blockerId : group.getBlockers()) {
                        sim.getPlayer(defenderId).declareBlocker(defenderId, blockerId, attackerId, sim);
                    }
                }
            }
            sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, defenderId, defenderId));
            SimulationNode newNode = new SimulationNode(node, sim, defenderId);
            if (logger.isDebugEnabled()) {
                logger.debug(indent(node.depth) + "simulating block for player:" + game.getPlayer(defenderId).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_BLOCKERS_STEP_POST, sim.getActivePlayerId(), sim.getActivePlayerId()));
            Combat simCombat = sim.getCombat().copy();
            finishCombat(sim);
            if (sim.checkIfGameIsOver()) {
                val = GameStateEvaluator.evaluate(playerId, sim);
            } else if (!counter) {
                val = simulatePostCombatMain(sim, newNode, alpha, beta);
            } else
                val = GameStateEvaluator.evaluate(playerId, sim);
            if (!defenderId.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 blocker 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)

Example 5 with Combat

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

the class CombatUtil method willItSurvive.

public static SurviveInfo willItSurvive(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;
    }
    sim.getPlayer(defendingPlayerId).declareBlocker(defendingPlayerId, blocker.getId(), attacker.getId(), sim);
    sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, defendingPlayerId, defendingPlayerId));
    sim.checkStateAndTriggered();
    while (!sim.getStack().isEmpty()) {
        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()) {
        sim.getStack().resolve(sim);
        sim.applyEffects();
    }
    return new SurviveInfo(!sim.getBattlefield().containsPermanent(attacker.getId()), !sim.getBattlefield().containsPermanent(blocker.getId()));
}
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)

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