Search in sources :

Example 16 with GameEvent

use of mage.game.events.GameEvent in project mage by magefree.

the class StonewiseFortifierPreventAllDamageToEffect method replaceEvent.

@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
    GameEvent preventEvent = new PreventDamageEvent(event.getTargetId(), event.getSourceId(), source, source.getControllerId(), event.getAmount(), ((DamageEvent) event).isCombatDamage());
    if (!game.replaceEvent(preventEvent)) {
        int preventedDamage = event.getAmount();
        MageObject damageSource = game.getObject(event.getSourceId());
        MageObject preventionSource = game.getObject(source.getSourceId());
        if (damageSource != null && preventionSource != null) {
            String message = " damage from " + damageSource.getName() + " prevented " + '(' + preventionSource + ')';
            game.informPlayers(message);
        }
        event.setAmount(0);
        game.fireEvent(new PreventedDamageEvent(event.getTargetId(), source.getSourceId(), source, source.getControllerId(), preventedDamage));
    }
    return false;
}
Also used : GameEvent(mage.game.events.GameEvent) MageObject(mage.MageObject) PreventedDamageEvent(mage.game.events.PreventedDamageEvent) PreventDamageEvent(mage.game.events.PreventDamageEvent)

Example 17 with GameEvent

use of mage.game.events.GameEvent in project mage by magefree.

the class FatesealEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Target target = new TargetOpponent(true);
        if (controller.choose(outcome, target, source.getSourceId(), game)) {
            Player opponent = game.getPlayer(target.getFirstTarget());
            if (opponent == null) {
                return false;
            }
            // by looking at the cards with fateseal you have not to reveal the next card
            boolean revealed = opponent.isTopCardRevealed();
            opponent.setTopCardRevealed(false);
            Cards cards = new CardsImpl();
            int count = Math.min(fatesealNumber, opponent.getLibrary().size());
            if (count == 0) {
                return true;
            }
            for (int i = 0; i < count; i++) {
                Card card = opponent.getLibrary().removeFromTop(game);
                cards.add(card);
            }
            TargetCard target1 = new TargetCard(Zone.LIBRARY, filter1);
            target1.setRequired(false);
            // move cards to the bottom of the library
            while (!cards.isEmpty() && controller.choose(Outcome.Detriment, cards, target1, game)) {
                if (!controller.canRespond() || !opponent.canRespond()) {
                    return false;
                }
                Card card = cards.get(target1.getFirstTarget(), game);
                if (card != null) {
                    cards.remove(card);
                    controller.moveCardToLibraryWithInfo(card, source, game, Zone.LIBRARY, false, false);
                }
                target1.clearChosen();
            }
            // move cards to the top of the library
            controller.putCardsOnTopOfLibrary(cards, game, source, true);
            game.fireEvent(new GameEvent(GameEvent.EventType.FATESEALED, opponent.getId(), source, source.getControllerId()));
            controller.setTopCardRevealed(revealed);
            return true;
        }
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) TargetOpponent(mage.target.common.TargetOpponent) GameEvent(mage.game.events.GameEvent) TargetCard(mage.target.TargetCard) Cards(mage.cards.Cards) CardsImpl(mage.cards.CardsImpl) FilterCard(mage.filter.FilterCard) TargetCard(mage.target.TargetCard) Card(mage.cards.Card)

Example 18 with GameEvent

use of mage.game.events.GameEvent in project mage by magefree.

the class ComputerPlayer6 method declareAttackers.

/**
 * Choose attackers based on static information. That means that AI won't
 * look to the future as it was before, but just choose attackers based on
 * current state of the game. This is worse, but at least it is easier to
 * implement and won't lead to the case when AI doesn't do anything -
 * neither attack nor block.
 *
 * @param game
 * @param activePlayerId
 */
private void declareAttackers(Game game, UUID activePlayerId) {
    attackersToCheck.clear();
    attackersList.clear();
    game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_PRE, null, null, activePlayerId));
    if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, activePlayerId, activePlayerId))) {
        Player attackingPlayer = game.getPlayer(activePlayerId);
        // check alpha strike first (all in attack to kill)
        for (UUID defenderId : game.getOpponents(playerId)) {
            Player defender = game.getPlayer(defenderId);
            if (!defender.isInGame()) {
                continue;
            }
            attackersList = super.getAvailableAttackers(defenderId, game);
            if (attackersList.isEmpty()) {
                continue;
            }
            List<Permanent> possibleBlockers = defender.getAvailableBlockers(game);
            List<Permanent> killers = CombatUtil.canKillOpponent(game, attackersList, possibleBlockers, defender);
            if (!killers.isEmpty()) {
                for (Permanent attacker : killers) {
                    attackingPlayer.declareAttacker(attacker.getId(), defenderId, game, false);
                }
                return;
            }
        }
        // check all other actions
        for (UUID defenderId : game.getOpponents(playerId)) {
            Player defender = game.getPlayer(defenderId);
            if (!defender.isInGame()) {
                continue;
            }
            attackersList = super.getAvailableAttackers(defenderId, game);
            if (attackersList.isEmpty()) {
                continue;
            }
            List<Permanent> possibleBlockers = defender.getAvailableBlockers(game);
            // The AI will now attack more sanely.  Simple, but good enough for now.
            // The sim minmax does not work at the moment.
            boolean safeToAttack;
            CombatEvaluator eval = new CombatEvaluator();
            for (Permanent attacker : attackersList) {
                safeToAttack = true;
                int attackerValue = eval.evaluate(attacker, game);
                for (Permanent blocker : possibleBlockers) {
                    int blockerValue = eval.evaluate(blocker, game);
                    // blocker can kill attacker
                    if (attacker.getPower().getValue() <= blocker.getToughness().getValue() && attacker.getToughness().getValue() <= blocker.getPower().getValue()) {
                        safeToAttack = false;
                    }
                    // attacker and blocker have the same P/T, check their overall value
                    if (attacker.getToughness().getValue() == blocker.getPower().getValue() && attacker.getPower().getValue() == blocker.getToughness().getValue()) {
                        if (attackerValue > blockerValue || blocker.getAbilities().containsKey(FirstStrikeAbility.getInstance().getId()) || blocker.getAbilities().containsKey(DoubleStrikeAbility.getInstance().getId()) || blocker.getAbilities().contains(new ExaltedAbility()) || blocker.getAbilities().containsKey(DeathtouchAbility.getInstance().getId()) || blocker.getAbilities().containsKey(IndestructibleAbility.getInstance().getId()) || !attacker.getAbilities().containsKey(FirstStrikeAbility.getInstance().getId()) || !attacker.getAbilities().containsKey(DoubleStrikeAbility.getInstance().getId()) || !attacker.getAbilities().contains(new ExaltedAbility())) {
                            safeToAttack = false;
                        }
                    }
                    // attacker can kill by deathtouch
                    if (attacker.getAbilities().containsKey(DeathtouchAbility.getInstance().getId()) || attacker.getAbilities().containsKey(IndestructibleAbility.getInstance().getId())) {
                        safeToAttack = true;
                    }
                    // attacker has flying and blocker has neither flying nor reach
                    if (attacker.getAbilities().containsKey(FlyingAbility.getInstance().getId()) && !blocker.getAbilities().containsKey(FlyingAbility.getInstance().getId()) && !blocker.getAbilities().containsKey(ReachAbility.getInstance().getId())) {
                        safeToAttack = true;
                    }
                    // if any check fails, move on to the next possible attacker
                    if (!safeToAttack) {
                        break;
                    }
                }
                // 0 power, don't bother attacking
                if (attacker.getPower().getValue() == 0) {
                    safeToAttack = false;
                }
                // add attacker to the next list of all attackers that can safely attack
                if (safeToAttack) {
                    attackersToCheck.add(attacker);
                }
            }
            // now we have a list of all attackers that can safely attack:
            // first check to see if any Planeswalkers can be killed
            int totalPowerOfAttackers = 0;
            int loyaltyCounters = 0;
            for (Permanent planeswalker : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_PLANESWALKER, defender.getId(), game)) {
                if (planeswalker != null) {
                    loyaltyCounters = planeswalker.getCounters(game).getCount(CounterType.LOYALTY);
                    // verify the attackers can kill the planeswalker, otherwise attack the player
                    for (Permanent attacker : attackersToCheck) {
                        totalPowerOfAttackers += attacker.getPower().getValue();
                    }
                    if (totalPowerOfAttackers < loyaltyCounters) {
                        break;
                    }
                    // kill the Planeswalker
                    for (Permanent attacker : attackersToCheck) {
                        loyaltyCounters -= attacker.getPower().getValue();
                        attackingPlayer.declareAttacker(attacker.getId(), planeswalker.getId(), game, true);
                        if (loyaltyCounters <= 0) {
                            break;
                        }
                    }
                }
            }
            // any remaining attackers go for the player
            for (Permanent attackingPermanent : attackersToCheck) {
                // if not already attacking a Planeswalker...
                if (!attackingPermanent.isAttacking()) {
                    attackingPlayer.declareAttacker(attackingPermanent.getId(), defenderId, game, true);
                }
            }
        }
    }
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) GameEvent(mage.game.events.GameEvent)

Example 19 with GameEvent

use of mage.game.events.GameEvent in project mage by magefree.

the class ComputerPlayer6 method declareBlockers.

private void declareBlockers(Game game, UUID activePlayerId) {
    game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_PRE, null, null, activePlayerId));
    if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_BLOCKERS, activePlayerId, activePlayerId))) {
        List<Permanent> attackers = getAttackers(game);
        if (attackers == null) {
            return;
        }
        List<Permanent> possibleBlockers = super.getAvailableBlockers(game);
        possibleBlockers = filterOutNonblocking(game, attackers, possibleBlockers);
        if (possibleBlockers.isEmpty()) {
            return;
        }
        attackers = filterOutUnblockable(game, attackers, possibleBlockers);
        if (attackers.isEmpty()) {
            return;
        }
        CombatUtil.sortByPower(attackers, false);
        CombatInfo combatInfo = CombatUtil.blockWithGoodTrade2(game, attackers, possibleBlockers);
        Player player = game.getPlayer(playerId);
        boolean blocked = false;
        for (Map.Entry<Permanent, List<Permanent>> entry : combatInfo.getCombat().entrySet()) {
            UUID attackerId = entry.getKey().getId();
            List<Permanent> blockers = entry.getValue();
            if (blockers != null) {
                for (Permanent blocker : blockers) {
                    player.declareBlocker(player.getId(), blocker.getId(), attackerId, game);
                    blocked = true;
                }
            }
        }
        if (blocked) {
            game.getPlayers().resetPassed();
        }
    }
}
Also used : CombatInfo(mage.player.ai.util.CombatInfo) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) GameEvent(mage.game.events.GameEvent)

Example 20 with GameEvent

use of mage.game.events.GameEvent in project mage by magefree.

the class SimulatedPlayer2 method triggerAbility.

@Override
public boolean triggerAbility(TriggeredAbility source, Game game) {
    Ability ability = source.copy();
    List<Ability> options = getPlayableOptions(ability, game);
    if (options.isEmpty()) {
        logger.debug("simulating -- triggered ability:" + ability);
        game.getStack().push(new StackAbility(ability, playerId));
        if (ability.activate(game, false) && ability.isUsesStack()) {
            game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
        }
        game.applyEffects();
        game.getPlayers().resetPassed();
    } else {
        SimulationNode2 parent = (SimulationNode2) game.getCustomData();
        int depth = parent.getDepth() - 1;
        if (depth == 0) {
            return true;
        }
        logger.debug("simulating -- triggered ability - adding children:" + options.size());
        for (Ability option : options) {
            addAbilityNode(parent, option, depth, game);
        }
    }
    return true;
}
Also used : PassAbility(mage.abilities.common.PassAbility) StackAbility(mage.game.stack.StackAbility) TriggeredAbility(mage.abilities.TriggeredAbility) ActivatedAbility(mage.abilities.ActivatedAbility) Ability(mage.abilities.Ability) GameEvent(mage.game.events.GameEvent) StackAbility(mage.game.stack.StackAbility)

Aggregations

GameEvent (mage.game.events.GameEvent)82 Permanent (mage.game.permanent.Permanent)28 Player (mage.players.Player)24 PreventDamageEvent (mage.game.events.PreventDamageEvent)23 PreventedDamageEvent (mage.game.events.PreventedDamageEvent)23 Game (mage.game.Game)22 Ability (mage.abilities.Ability)15 MageInt (mage.MageInt)13 CardImpl (mage.cards.CardImpl)13 CardSetInfo (mage.cards.CardSetInfo)13 java.util (java.util)12 OneShotEffect (mage.abilities.effects.OneShotEffect)12 Watcher (mage.watchers.Watcher)12 MageObjectReference (mage.MageObjectReference)11 Card (mage.cards.Card)11 UUID (java.util.UUID)10 TriggeredAbilityImpl (mage.abilities.TriggeredAbilityImpl)10 mage.constants (mage.constants)10 MageObject (mage.MageObject)9 Spell (mage.game.stack.Spell)9