Search in sources :

Example 1 with CombatGroup

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

the class ComputerPlayer method selectBlockers.

@Override
public void selectBlockers(Ability source, Game game, UUID defendingPlayerId) {
    log.debug("selectBlockers");
    List<Permanent> blockers = getAvailableBlockers(game);
    CombatSimulator sim = simulateBlock(CombatSimulator.load(game), blockers, game);
    List<CombatGroup> groups = game.getCombat().getGroups();
    for (int i = 0; i < groups.size(); i++) {
        for (CreatureSimulator creature : sim.groups.get(i).blockers) {
            groups.get(i).addBlocker(creature.id, playerId, game);
        }
    }
}
Also used : Permanent(mage.game.permanent.Permanent) FilterPermanent(mage.filter.FilterPermanent) CombatSimulator(mage.player.ai.simulators.CombatSimulator) CreatureSimulator(mage.player.ai.simulators.CreatureSimulator) CombatGroup(mage.game.combat.CombatGroup)

Example 2 with CombatGroup

use of mage.game.combat.CombatGroup 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 CombatGroup

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

the class SimulatedPlayerMCTS method selectBlockers.

@Override
public void selectBlockers(Ability source, Game game, UUID defendingPlayerId) {
    // logger.info("select blockers");
    int numGroups = game.getCombat().getGroups().size();
    if (numGroups == 0) {
        return;
    }
    List<Permanent> blockers = getAvailableBlockers(game);
    for (Permanent blocker : blockers) {
        int check = RandomUtil.nextInt(numGroups + 1);
        if (check < numGroups) {
            CombatGroup group = game.getCombat().getGroups().get(check);
            if (!group.getAttackers().isEmpty()) {
                this.declareBlocker(this.getId(), blocker.getId(), group.getAttackers().get(0), game);
            }
        }
    }
    actionCount++;
}
Also used : Permanent(mage.game.permanent.Permanent) CombatGroup(mage.game.combat.CombatGroup)

Example 4 with CombatGroup

use of mage.game.combat.CombatGroup 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 CombatGroup

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

the class ImprisonUnblockEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent enchantment = game.getPermanent(source.getSourceId());
    if (enchantment != null && enchantment.getAttachedTo() != null) {
        Permanent permanent = game.getPermanent(enchantment.getAttachedTo());
        if (permanent != null) {
            if (permanent.isCreature(game)) {
                // Tap the creature
                permanent.tap(source, game);
                // Remove it from combat
                Effect effect = new RemoveFromCombatTargetEffect();
                effect.setTargetPointer(new FixedTarget(permanent, game));
                effect.apply(game, source);
                // Make blocked creatures unblocked
                BlockedByOnlyOneCreatureThisCombatWatcher watcher = game.getState().getWatcher(BlockedByOnlyOneCreatureThisCombatWatcher.class);
                if (watcher != null) {
                    Set<CombatGroup> combatGroups = watcher.getBlockedOnlyByCreature(permanent.getId());
                    if (combatGroups != null) {
                        for (CombatGroup combatGroup : combatGroups) {
                            if (combatGroup != null) {
                                combatGroup.setBlocked(false, game);
                            }
                        }
                    }
                }
                return true;
            }
        }
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) RemoveFromCombatTargetEffect(mage.abilities.effects.common.RemoveFromCombatTargetEffect) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) TargetPermanent(mage.target.TargetPermanent) CounterTargetEffect(mage.abilities.effects.common.CounterTargetEffect) DestroySourceEffect(mage.abilities.effects.common.DestroySourceEffect) AttachEffect(mage.abilities.effects.common.AttachEffect) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) RemoveFromCombatTargetEffect(mage.abilities.effects.common.RemoveFromCombatTargetEffect) BlockedByOnlyOneCreatureThisCombatWatcher(mage.watchers.common.BlockedByOnlyOneCreatureThisCombatWatcher) CombatGroup(mage.game.combat.CombatGroup)

Aggregations

CombatGroup (mage.game.combat.CombatGroup)44 Permanent (mage.game.permanent.Permanent)39 UUID (java.util.UUID)25 Player (mage.players.Player)23 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)9 FixedTarget (mage.target.targetpointer.FixedTarget)9 TargetPermanent (mage.target.TargetPermanent)8 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)8 HashSet (java.util.HashSet)7 OneShotEffect (mage.abilities.effects.OneShotEffect)6 Game (mage.game.Game)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Combat (mage.game.combat.Combat)5 BlockerDeclaredEvent (mage.game.events.BlockerDeclaredEvent)5 Target (mage.target.Target)5 MageObjectReference (mage.MageObjectReference)4 Ability (mage.abilities.Ability)4 PermanentInListPredicate (mage.filter.predicate.permanent.PermanentInListPredicate)4 BlockedByOnlyOneCreatureThisCombatWatcher (mage.watchers.common.BlockedByOnlyOneCreatureThisCombatWatcher)4