Search in sources :

Example 11 with Game

use of mage.game.Game 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 12 with Game

use of mage.game.Game in project mage by magefree.

the class BeamsplitterMageApplier method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (!isControlledBy(event.getPlayerId())) {
        return false;
    }
    Spell spell = game.getSpellOrLKIStack(event.getTargetId());
    if (spell == null || !spell.isInstantOrSorcery(game)) {
        return false;
    }
    if (spell.getSpellAbilities().stream().map(AbilityImpl::getModes).flatMap(m -> m.getSelectedModes().stream().map(m::get)).filter(Objects::nonNull).map(Mode::getTargets).flatMap(Collection::stream).filter(t -> !t.isNotTarget()).map(Target::getTargets).flatMap(Collection::stream).anyMatch(uuid -> !getSourceId().equals(uuid) && uuid != null)) {
        return false;
    }
    this.getEffects().setValue("spellCast", spell);
    return true;
}
Also used : Target(mage.target.Target) java.util(java.util) Zone(mage.constants.Zone) Predicate(mage.filter.predicate.Predicate) MageObjectReference(mage.MageObjectReference) MageObjectReferencePredicate(mage.filter.predicate.mageobject.MageObjectReferencePredicate) SubType(mage.constants.SubType) FilterPermanent(mage.filter.FilterPermanent) Mode(mage.abilities.Mode) Player(mage.players.Player) StackObjectCopyApplier(mage.util.functions.StackObjectCopyApplier) CardType(mage.constants.CardType) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) StackObject(mage.game.stack.StackObject) Spell(mage.game.stack.Spell) StaticFilters(mage.filter.StaticFilters) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) MageInt(mage.MageInt) CardSetInfo(mage.cards.CardSetInfo) TriggeredAbilityImpl(mage.abilities.TriggeredAbilityImpl) Game(mage.game.Game) AbilityImpl(mage.abilities.AbilityImpl) GameEvent(mage.game.events.GameEvent) CardImpl(mage.cards.CardImpl) Permanent(mage.game.permanent.Permanent) AnotherPredicate(mage.filter.predicate.mageobject.AnotherPredicate) TargetPermanent(mage.target.TargetPermanent) Ability(mage.abilities.Ability) TriggeredAbilityImpl(mage.abilities.TriggeredAbilityImpl) AbilityImpl(mage.abilities.AbilityImpl) Target(mage.target.Target) Spell(mage.game.stack.Spell)

Example 13 with Game

use of mage.game.Game in project mage by magefree.

the class FrontierWarmongerTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    Set<UUID> opponents = game.getOpponents(this.getControllerId());
    Predicate<UUID> predicate = uuid -> opponents.contains(game.getCombat().getDefendingPlayerId(uuid, game));
    if (game.getCombat().getAttackers().stream().noneMatch(predicate)) {
        return false;
    }
    List<Permanent> permanents = game.getCombat().getAttackers().stream().filter(predicate).map(game::getPermanent).filter(Objects::nonNull).collect(Collectors.toList());
    this.getEffects().setTargetPointer(new FixedTargets(permanents, game));
    return true;
}
Also used : Zone(mage.constants.Zone) Predicate(java.util.function.Predicate) Set(java.util.Set) UUID(java.util.UUID) MageInt(mage.MageInt) SubType(mage.constants.SubType) Collectors(java.util.stream.Collectors) CardSetInfo(mage.cards.CardSetInfo) Objects(java.util.Objects) Duration(mage.constants.Duration) TriggeredAbilityImpl(mage.abilities.TriggeredAbilityImpl) Game(mage.game.Game) List(java.util.List) GameEvent(mage.game.events.GameEvent) CardImpl(mage.cards.CardImpl) Permanent(mage.game.permanent.Permanent) CardType(mage.constants.CardType) FixedTargets(mage.target.targetpointer.FixedTargets) MenaceAbility(mage.abilities.keyword.MenaceAbility) GainAbilityTargetEffect(mage.abilities.effects.common.continuous.GainAbilityTargetEffect) Permanent(mage.game.permanent.Permanent) FixedTargets(mage.target.targetpointer.FixedTargets) UUID(java.util.UUID)

Example 14 with Game

use of mage.game.Game in project mage by magefree.

the class HauntingEchoesPredicate method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Player player = game.getPlayer(source.getFirstTarget());
    if (controller == null || player == null) {
        return false;
    }
    Cards cards = new CardsImpl();
    player.getGraveyard().getCards(game).stream().filter(Objects::nonNull).filter(card -> !card.isBasic() || !card.isLand(game)).forEach(cards::add);
    controller.moveCards(cards, Zone.EXILED, source, game);
    cards.removeIf(uuid -> game.getState().getZone(uuid) != Zone.EXILED);
    FilterCard filter = new FilterCard("cards with the same name as a card exiled this way");
    filter.add(new HauntingEchoesPredicate(cards));
    TargetCardInLibrary target = new TargetCardInLibrary(0, Integer.MAX_VALUE, filter);
    controller.searchLibrary(target, source, game, player.getId());
    cards.clear();
    cards.addAll(target.getTargets());
    controller.moveCards(cards, Zone.EXILED, source, game);
    player.shuffleLibrary(source, game);
    return true;
}
Also used : FilterCard(mage.filter.FilterCard) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) Zone(mage.constants.Zone) Predicate(mage.filter.predicate.Predicate) mage.cards(mage.cards) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) CardUtil(mage.util.CardUtil) TargetPlayer(mage.target.TargetPlayer) UUID(java.util.UUID) Player(mage.players.Player) Objects(java.util.Objects) Game(mage.game.Game) CardType(mage.constants.CardType) Ability(mage.abilities.Ability) FilterCard(mage.filter.FilterCard) TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) mage.cards(mage.cards) Objects(java.util.Objects) TargetCardInLibrary(mage.target.common.TargetCardInLibrary)

Example 15 with Game

use of mage.game.Game in project mage by magefree.

the class KotoseTheSilentSpiderWatcher method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Card card = game.getCard(getTargetPointer().getFirst(game, source));
    if (controller == null || card == null) {
        return false;
    }
    Player opponent = game.getPlayer(card.getOwnerId());
    if (opponent == null) {
        return false;
    }
    UUID exileId = CardUtil.getExileZoneId(game, source);
    String exileName = CardUtil.getSourceName(game, source);
    controller.moveCardsToExile(card, source, game, true, exileId, exileName);
    Cards cards = new CardsImpl();
    FilterCard filter = new FilterCard("cards named " + card.getName() + " from " + opponent.getName() + "'s graveyard");
    filter.add(new NamePredicate(card.getName()));
    TargetCardInGraveyard targetCardInGraveyard = new TargetCardInGraveyard(0, Integer.MAX_VALUE, filter);
    controller.choose(outcome, opponent.getGraveyard(), targetCardInGraveyard, game);
    cards.addAll(targetCardInGraveyard.getTargets());
    filter.setMessage("cards named " + card.getName() + " from " + opponent.getName() + "'s hand");
    TargetCardInHand targetCardInHand = new TargetCardInHand(0, Integer.MAX_VALUE, filter);
    controller.choose(outcome, opponent.getHand(), targetCardInHand, game);
    cards.addAll(targetCardInHand.getTargets());
    filter.setMessage("cards named " + card.getName() + " from " + opponent.getName() + "'s library");
    TargetCardInLibrary target = new TargetCardInLibrary(0, Integer.MAX_VALUE, filter);
    controller.searchLibrary(target, source, game, opponent.getId());
    target.getTargets().stream().map(cardId -> opponent.getLibrary().getCard(cardId, game)).forEach(cards::add);
    Set<Card> cardSet = cards.getCards(game);
    controller.moveCardsToExile(cardSet, source, game, true, exileId, exileName);
    opponent.shuffleLibrary(source, game);
    cardSet.add(card);
    if (cardSet.isEmpty() || source.getSourcePermanentIfItStillExists(game) == null) {
        return true;
    }
    KotoseTheSilentSpiderWatcher.addCards(source, cardSet, game);
    for (Card exiledCard : cardSet) {
        CardUtil.makeCardPlayable(game, source, exiledCard, Duration.WhileControlled, true, null, new KotoseTheSilentSpiderCondition(exiledCard, game));
    }
    return true;
}
Also used : FilterCard(mage.filter.FilterCard) java.util(java.util) TargetCardInHand(mage.target.common.TargetCardInHand) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) mage.cards(mage.cards) EntersBattlefieldTriggeredAbility(mage.abilities.common.EntersBattlefieldTriggeredAbility) Condition(mage.abilities.condition.Condition) MageObjectReference(mage.MageObjectReference) OneShotEffect(mage.abilities.effects.OneShotEffect) Predicates(mage.filter.predicate.Predicates) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard) CardUtil(mage.util.CardUtil) MageInt(mage.MageInt) Collectors(java.util.stream.Collectors) Player(mage.players.Player) Game(mage.game.Game) Watcher(mage.watchers.Watcher) GameEvent(mage.game.events.GameEvent) Spell(mage.game.stack.Spell) mage.constants(mage.constants) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) TargetCardInOpponentsGraveyard(mage.target.common.TargetCardInOpponentsGraveyard) Ability(mage.abilities.Ability) Player(mage.players.Player) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) mage.cards(mage.cards) TargetCardInHand(mage.target.common.TargetCardInHand) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) FilterCard(mage.filter.FilterCard) FilterCard(mage.filter.FilterCard) TargetCardInGraveyard(mage.target.common.TargetCardInGraveyard)

Aggregations

Game (mage.game.Game)212 Ability (mage.abilities.Ability)139 Player (mage.players.Player)126 UUID (java.util.UUID)117 OneShotEffect (mage.abilities.effects.OneShotEffect)104 CardSetInfo (mage.cards.CardSetInfo)102 CardImpl (mage.cards.CardImpl)100 CardType (mage.constants.CardType)82 Outcome (mage.constants.Outcome)78 Permanent (mage.game.permanent.Permanent)66 MageInt (mage.MageInt)60 mage.constants (mage.constants)47 Zone (mage.constants.Zone)46 GameEvent (mage.game.events.GameEvent)44 Objects (java.util.Objects)41 StaticFilters (mage.filter.StaticFilters)40 Collectors (java.util.stream.Collectors)35 SubType (mage.constants.SubType)34 Card (mage.cards.Card)32 MageObjectReference (mage.MageObjectReference)30