Search in sources :

Example 6 with Combat

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

the class VortexElementalEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Combat combat = game.getState().getCombat();
        Set<UUID> creaturesToReturn = new HashSet<>();
        Set<UUID> playersToShuffle = new HashSet<>();
        creaturesToReturn.add(source.getSourceId());
        if (combat != null) {
            for (CombatGroup combatGroup : combat.getGroups()) {
                if (combatGroup.getAttackers().contains(source.getSourceId())) {
                    creaturesToReturn.addAll(combatGroup.getBlockers());
                } else if (combatGroup.getBlockers().contains(source.getSourceId())) {
                    creaturesToReturn.addAll(combatGroup.getAttackers());
                }
            }
        }
        for (UUID creatureId : creaturesToReturn) {
            Permanent creature = game.getPermanent(creatureId);
            if (creature != null) {
                playersToShuffle.add(creature.getControllerId());
            }
        }
        Cards toLib = new CardsImpl(creaturesToReturn);
        controller.putCardsOnTopOfLibrary(toLib, game, source, false);
        for (UUID playerId : playersToShuffle) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                player.shuffleLibrary(source, game);
            }
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) TargetCreaturePermanent(mage.target.common.TargetCreaturePermanent) Combat(mage.game.combat.Combat) UUID(java.util.UUID) CombatGroup(mage.game.combat.CombatGroup) Cards(mage.cards.Cards) CardsImpl(mage.cards.CardsImpl) HashSet(java.util.HashSet)

Example 7 with Combat

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

the class MandateOfPeaceEndCombatEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Combat combat = game.getCombat();
    List<UUID> attackerIds = combat.getAttackers();
    List<UUID> blockerIds = combat.getBlockers();
    Stream.concat(blockerIds.stream(), attackerIds.stream()).map(id -> game.getPermanent(id)).filter(e -> e != null).forEach(permanent -> permanent.removeFromCombat(game));
    combat.endCombat(game);
    if (!game.getStack().isEmpty()) {
        game.getStack().stream().filter(stackObject -> stackObject instanceof Spell).forEach(stackObject -> ((Spell) stackObject).moveToExile(null, "", null, game));
        game.getStack().stream().filter(stackObject -> stackObject instanceof Ability).forEach(stackObject -> game.getStack().remove(stackObject, game));
    }
    return true;
}
Also used : Combat(mage.game.combat.Combat) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) UUID(java.util.UUID) CardSetInfo(mage.cards.CardSetInfo) CastOnlyDuringPhaseStepSourceAbility(mage.abilities.common.CastOnlyDuringPhaseStepSourceAbility) Duration(mage.constants.Duration) List(java.util.List) Game(mage.game.Game) Stream(java.util.stream.Stream) Effect(mage.abilities.effects.Effect) GameEvent(mage.game.events.GameEvent) CardImpl(mage.cards.CardImpl) CardType(mage.constants.CardType) MageObject(mage.MageObject) TurnPhase(mage.constants.TurnPhase) ContinuousRuleModifyingEffectImpl(mage.abilities.effects.ContinuousRuleModifyingEffectImpl) Spell(mage.game.stack.Spell) Ability(mage.abilities.Ability) CastOnlyDuringPhaseStepSourceAbility(mage.abilities.common.CastOnlyDuringPhaseStepSourceAbility) Ability(mage.abilities.Ability) Combat(mage.game.combat.Combat) UUID(java.util.UUID) Spell(mage.game.stack.Spell)

Example 8 with Combat

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

the class SimulatedPlayer2 method addAttackers.

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 {
            logger.debug("simulating -- attack:" + sim.getCombat().getGroups().size());
        }
    }
    List list = new ArrayList<>(engagements.values());
    Collections.sort(list, new Comparator<Combat>() {

        @Override
        public int compare(Combat o1, Combat o2) {
            return Integer.valueOf(o2.getGroups().size()).compareTo(Integer.valueOf(o1.getGroups().size()));
        }
    });
    return list;
}
Also used : Permanent(mage.game.permanent.Permanent) Game(mage.game.Game) Combat(mage.game.combat.Combat)

Example 9 with Combat

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

the class SimulatedPlayer2 method addBlockers.

public List<Combat> addBlockers(Game game) {
    Map<Integer, Combat> engagements = new HashMap<>();
    int numGroups = game.getCombat().getGroups().size();
    if (numGroups == 0) {
        return Collections.emptyList();
    }
    // 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 10 with Combat

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

the class ComputerPlayer2 method playNext.

/*@Override
    public boolean playXMana(VariableManaCost cost, ManaCosts<ManaCost> costs, Game game) {
        //SimulatedPlayer.simulateVariableCosts method adds a generic mana cost for each option
        for (ManaCost manaCost: costs) {
            if (manaCost instanceof GenericManaCost) {
                cost.setPayment(manaCost.getPayment());
                logger.debug("using X = " + cost.getPayment().count());
                break;
            }
        }
        game.informPlayers(getName() + " payed " + cost.getPayment().count() + " for " + cost.getText());
        cost.setPaid();
        return true;
    }*/
public void playNext(Game game, UUID activePlayerId, SimulationNode node) {
    boolean skip = false;
    while (true) {
        Phase currentPhase = game.getPhase();
        if (!skip) {
            currentPhase.getStep().endStep(game, activePlayerId);
        }
        game.applyEffects();
        switch(currentPhase.getStep().getType()) {
            case UNTAP:
                game.getPhase().setStep(new UpkeepStep());
                break;
            case UPKEEP:
                game.getPhase().setStep(new DrawStep());
                break;
            case DRAW:
                game.getTurn().setPhase(new PreCombatMainPhase());
                game.getPhase().setStep(new PreCombatMainStep());
                break;
            case PRECOMBAT_MAIN:
                game.getTurn().setPhase(new CombatPhase());
                game.getPhase().setStep(new BeginCombatStep());
                break;
            case BEGIN_COMBAT:
                game.getPhase().setStep(new DeclareAttackersStep());
                break;
            case DECLARE_ATTACKERS:
                game.getPhase().setStep(new DeclareBlockersStep());
                break;
            case DECLARE_BLOCKERS:
                game.getPhase().setStep(new FirstCombatDamageStep());
                break;
            case FIRST_COMBAT_DAMAGE:
                game.getPhase().setStep(new CombatDamageStep());
                break;
            case COMBAT_DAMAGE:
                game.getPhase().setStep(new EndOfCombatStep());
                break;
            case END_COMBAT:
                game.getTurn().setPhase(new PostCombatMainPhase());
                game.getPhase().setStep(new PostCombatMainStep());
                break;
            case POSTCOMBAT_MAIN:
                game.getTurn().setPhase(new EndPhase());
                game.getPhase().setStep(new EndStep());
                break;
            case END_TURN:
                game.getPhase().setStep(new CleanupStep());
                break;
            case CLEANUP:
                game.getPhase().getStep().beginStep(game, activePlayerId);
                if (!game.checkStateAndTriggered() && !game.checkIfGameIsOver()) {
                    game.getState().setActivePlayerId(game.getState().getPlayerList(game.getActivePlayerId()).getNext());
                    game.getTurn().setPhase(new BeginningPhase());
                    game.getPhase().setStep(new UntapStep());
                }
        }
        if (!game.getStep().skipStep(game, game.getActivePlayerId())) {
            if (game.getTurn().getStepType() == PhaseStep.DECLARE_ATTACKERS) {
                game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_PRE, null, null, activePlayerId));
                if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, activePlayerId, activePlayerId))) {
                    for (Combat engagement : ((SimulatedPlayer) game.getPlayer(activePlayerId)).addAttackers(game)) {
                        Game sim = game.copy();
                        UUID defenderId = game.getOpponents(playerId).iterator().next();
                        for (CombatGroup group : engagement.getGroups()) {
                            for (UUID attackerId : group.getAttackers()) {
                                sim.getPlayer(activePlayerId).declareAttacker(attackerId, defenderId, sim, false);
                            }
                        }
                        sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_ATTACKERS, playerId, playerId));
                        SimulationNode newNode = new SimulationNode(node, sim, activePlayerId);
                        logger.debug(indent(node.depth) + "simulating -- node #:" + SimulationNode.getCount() + " declare attakers");
                        newNode.setCombat(sim.getCombat());
                        node.children.add(newNode);
                    }
                }
            } else if (game.getTurn().getStepType() == PhaseStep.DECLARE_BLOCKERS) {
                game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_PRE, null, null, activePlayerId));
                if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_BLOCKERS, activePlayerId, activePlayerId))) {
                    for (UUID defenderId : game.getCombat().getDefenders()) {
                        // check if defender is being attacked
                        if (game.getCombat().isAttacked(defenderId, game)) {
                            for (Combat engagement : ((SimulatedPlayer) game.getPlayer(defenderId)).addBlockers(game)) {
                                Game sim = game.copy();
                                for (CombatGroup group : engagement.getGroups()) {
                                    for (UUID blockerId : group.getBlockers()) {
                                        group.addBlocker(blockerId, defenderId, sim);
                                    }
                                }
                                sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
                                SimulationNode newNode = new SimulationNode(node, sim, defenderId);
                                logger.debug(indent(node.depth) + "simulating -- node #:" + SimulationNode.getCount() + " declare blockers");
                                newNode.setCombat(sim.getCombat());
                                node.children.add(newNode);
                            }
                        }
                    }
                }
            } else {
                game.getStep().beginStep(game, activePlayerId);
            }
            if (game.getStep().getHasPriority())
                break;
        } else {
            skip = true;
        }
    }
    game.checkStateAndTriggered();
}
Also used : GameEvent(mage.game.events.GameEvent) CombatGroup(mage.game.combat.CombatGroup) Game(mage.game.Game) Combat(mage.game.combat.Combat)

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