Search in sources :

Example 71 with GameEvent

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

the class RandomPlayer method triggerAbility.

@Override
public boolean triggerAbility(TriggeredAbility source, Game game) {
    if (source != null && source.canChooseTarget(game, playerId)) {
        Ability ability;
        List<Ability> options = getPlayableOptions(source, game);
        if (options.isEmpty()) {
            ability = source;
        } else {
            if (options.size() == 1) {
                ability = options.get(0);
            } else {
                ability = options.get(RandomUtil.nextInt(options.size()));
            }
        }
        if (ability.isUsesStack()) {
            game.getStack().push(new StackAbility(ability, playerId));
            if (ability.activate(game, false)) {
                game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
                actionCount++;
                return true;
            }
        } else {
            if (ability.activate(game, false)) {
                ability.resolve(game);
                actionCount++;
                return true;
            }
        }
    }
    return false;
}
Also used : PassAbility(mage.abilities.common.PassAbility) StackAbility(mage.game.stack.StackAbility) GameEvent(mage.game.events.GameEvent) StackAbility(mage.game.stack.StackAbility)

Example 72 with GameEvent

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

the class ClashEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    MageObject sourceObject = source.getSourceObject(game);
    if (controller == null || sourceObject == null || game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.CLASH, controller.getId(), source, controller.getId()))) {
        return false;
    }
    // choose opponent
    Target target = new TargetOpponent(true);
    target.setTargetName("an opponent to clash with");
    target.setNotTarget(true);
    if (!controller.choose(Outcome.Benefit, target, source.getSourceId(), game)) {
        return false;
    }
    Player opponent = game.getPlayer(target.getFirstTarget());
    if (opponent == null) {
        return false;
    }
    int cmcController = Integer.MIN_VALUE;
    Card cardController = null;
    boolean topController = true;
    int cmcOpponent = Integer.MIN_VALUE;
    Card cardOpponent = null;
    boolean topOpponent = true;
    // Reveal top cards of involved players
    StringBuilder message = new StringBuilder("Clash: ");
    message.append(controller.getLogName());
    if (controller.getLibrary().hasCards()) {
        Cards cards = new CardsImpl();
        cardController = controller.getLibrary().getFromTop(game);
        cards.add(cardController);
        controller.revealCards(sourceObject.getIdName() + ": Clash card of " + controller.getName(), cards, game);
        cmcController = cardController.getManaValue();
        message.append(" (").append(cmcController).append(')');
    } else {
        message.append(" no card");
    }
    message.append(" vs. ").append(opponent.getLogName());
    if (opponent.getLibrary().hasCards()) {
        Cards cards = new CardsImpl();
        cardOpponent = opponent.getLibrary().getFromTop(game);
        cards.add(cardOpponent);
        opponent.revealCards(sourceObject.getIdName() + ": Clash card of " + opponent.getName(), cards, game);
        cmcOpponent = cardOpponent.getManaValue();
        message.append(" (").append(cmcOpponent).append(')');
    } else {
        message.append(" no card");
    }
    message.append(" - ");
    if (!game.isSimulation()) {
        if (cmcController > cmcOpponent) {
            message.append(controller.getLogName()).append(" won the clash");
            game.informPlayer(controller, "You won the clash!");
        } else if (cmcController < cmcOpponent) {
            message.append(opponent.getLogName()).append(" won the clash");
            game.informPlayer(controller, opponent.getLogName() + " won the clash!");
        } else {
            message.append(" no winner ");
        }
        game.informPlayers(message.toString());
    }
    // decide to put the cards on top or on the buttom of library in turn order beginning with the active player in turn order
    PlayerList playerList = game.getPlayerList().copy();
    playerList.setCurrent(game.getActivePlayerId());
    Player nextPlayer;
    do {
        Player current = playerList.getCurrent(game);
        if (cardController != null && current.getId().equals(controller.getId())) {
            topController = current.chooseUse(Outcome.Detriment, "Put " + cardController.getLogName() + " back on top of your library? (otherwise it goes to bottom)", source, game);
        }
        if (cardOpponent != null && current.getId().equals(opponent.getId())) {
            topOpponent = current.chooseUse(Outcome.Detriment, "Put " + cardOpponent.getLogName() + " back on top of your library? (otherwise it goes to bottom)", source, game);
        }
        nextPlayer = playerList.getNext(game, false);
    } while (nextPlayer != null && !nextPlayer.getId().equals(game.getActivePlayerId()));
    // put the cards back to library
    if (cardController != null) {
        controller.moveCardToLibraryWithInfo(cardController, source, game, Zone.LIBRARY, topController, true);
    }
    if (cardOpponent != null) {
        opponent.moveCardToLibraryWithInfo(cardOpponent, source, game, Zone.LIBRARY, topOpponent, true);
    }
    // fire CLASHED event with info about who won
    game.fireEvent(new GameEvent(GameEvent.EventType.CLASHED, controller.getId(), source, opponent.getId(), 0, cmcController > cmcOpponent));
    game.fireEvent(new GameEvent(GameEvent.EventType.CLASHED, opponent.getId(), source, controller.getId(), 0, cmcOpponent > cmcController));
    // set opponent to DoIfClashWonEffect
    for (Effect effect : source.getEffects()) {
        if (effect instanceof DoIfClashWonEffect) {
            effect.setValue("clashOpponent", opponent);
        }
    }
    return cmcController > cmcOpponent;
}
Also used : Player(mage.players.Player) TargetOpponent(mage.target.common.TargetOpponent) PlayerList(mage.players.PlayerList) MageObject(mage.MageObject) Card(mage.cards.Card) Target(mage.target.Target) GameEvent(mage.game.events.GameEvent) OneShotEffect(mage.abilities.effects.OneShotEffect) Effect(mage.abilities.effects.Effect) Cards(mage.cards.Cards) CardsImpl(mage.cards.CardsImpl)

Example 73 with GameEvent

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

the class ManaPool method removeConditional.

private void removeConditional(ConditionalManaInfo manaInfo, Ability ability, Game game, Cost costToPay, Mana usedManaToPay) {
    for (ConditionalMana mana : getConditionalMana()) {
        if (mana.get(manaInfo.manaType) > 0 && mana.apply(ability, game, mana.getManaProducerId(), costToPay)) {
            mana.set(manaInfo.manaType, CardUtil.overflowDec(mana.get(manaInfo.manaType), 1));
            usedManaToPay.increase(manaInfo.manaType);
            GameEvent event = new ManaPaidEvent(ability, mana.getManaProducerId(), mana.getFlag(), mana.getManaProducerOriginalId(), manaInfo.sourceObject, manaInfo.manaType);
            game.fireEvent(event);
            break;
        }
    }
}
Also used : ConditionalMana(mage.ConditionalMana) GameEvent(mage.game.events.GameEvent) ManaPaidEvent(mage.game.events.ManaPaidEvent)

Example 74 with GameEvent

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

the class ManaPool method pay.

/**
 * @param manaType      the mana type that should be paid
 * @param ability
 * @param filter
 * @param game
 * @param costToPay     complete costs to pay (needed to check conditional
 *                      mana)
 * @param usedManaToPay the information about what mana was paid
 * @return
 */
public boolean pay(ManaType manaType, Ability ability, Filter filter, Game game, Cost costToPay, Mana usedManaToPay) {
    if (!isAutoPayment() && manaType != unlockedManaType) {
        // if manual payment and the needed mana type was not unlocked, nothing will be paid
        return false;
    }
    ManaType possibleAsThoughPoolManaType = null;
    if (isAutoPayment() && isAutoPaymentRestricted() && // was not more mana added than at the start of casting something
    !wasManaAddedBeyondStock() && manaType != unlockedManaType) {
        // and the needed mana type was not unlocked, nothing will be paid
        if (unlockedManaType != null) {
            ManaPoolItem checkItem = new ManaPoolItem();
            checkItem.add(unlockedManaType, 1);
            possibleAsThoughPoolManaType = game.getContinuousEffects().asThoughMana(manaType, checkItem, ability.getSourceId(), ability, ability.getControllerId(), game);
        }
        // Check if it's possible to use mana as thought for the unlocked manatype in the mana pool for this ability
        if (possibleAsThoughPoolManaType == null || possibleAsThoughPoolManaType != unlockedManaType) {
            // if it's not possible return
            return false;
        }
    }
    // first try to pay from conditional mana (the returned manaType can be changed if AsThoughEffects are active)
    ConditionalManaInfo manaInfo = getConditional(manaType, ability, filter, game, costToPay, possibleAsThoughPoolManaType);
    if (manaInfo != null) {
        removeConditional(manaInfo, ability, game, costToPay, usedManaToPay);
        // pay only one mana if mana payment is set to manually
        lockManaType();
        return true;
    }
    for (ManaPoolItem mana : manaItems) {
        if (filter != null) {
            if (!filter.match(mana.getSourceObject(), game)) {
                // Prevent that cost reduction by convoke is filtered out
                if (!(mana.getSourceObject() instanceof Spell) || ability.getSourceId().equals(mana.getSourceId())) {
                    continue;
                }
            }
        }
        if (possibleAsThoughPoolManaType == null && manaType != unlockedManaType && isAutoPayment() && isAutoPaymentRestricted() && mana.count() == mana.getStock()) {
            // no mana added beyond the stock so don't auto pay this
            continue;
        }
        ManaType usableManaType = game.getContinuousEffects().asThoughMana(manaType, mana, ability.getSourceId(), ability, ability.getControllerId(), game);
        if (usableManaType == null) {
            continue;
        }
        if (mana.get(usableManaType) > 0) {
            GameEvent event = new ManaPaidEvent(ability, mana.getSourceId(), mana.getFlag(), mana.getOriginalId(), mana.getSourceObject(), usableManaType);
            game.fireEvent(event);
            usedManaToPay.increase(usableManaType);
            mana.remove(usableManaType);
            if (mana.count() == 0) {
                // so no items with count 0 stay in list
                manaItems.remove(mana);
            }
            // pay only one mana if mana payment is set to manually
            lockManaType();
            return true;
        }
    }
    return false;
}
Also used : GameEvent(mage.game.events.GameEvent) ManaType(mage.constants.ManaType) ManaPaidEvent(mage.game.events.ManaPaidEvent) Spell(mage.game.stack.Spell)

Example 75 with GameEvent

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

the class Targets method chooseTargets.

public boolean chooseTargets(Outcome outcome, UUID playerId, Ability source, boolean noMana, Game game, boolean canCancel) {
    if (this.size() > 0) {
        if (!canChoose(source.getSourceId(), playerId, game)) {
            return false;
        }
        // int state = game.bookmarkState();
        while (!isChosen()) {
            Target target = this.getUnchosen().get(0);
            UUID targetController = playerId;
            // some targets can have controller different than ability controller
            if (target.getTargetController() != null) {
                targetController = target.getTargetController();
            }
            // if cast without mana (e.g. by suspend you may not be able to cancel the casting if you are able to cast it
            if (noMana) {
                target.setRequired(true);
            }
            // can be cancel by user
            if (canCancel) {
                target.setRequired(false);
            }
            // make response checks
            if (!target.chooseTarget(outcome, targetController, source, game)) {
                return false;
            }
            // Check if there are some rules for targets are violated, if so reset the targets and start again
            if (this.getUnchosen().isEmpty() && game.replaceEvent(new GameEvent(GameEvent.EventType.TARGETS_VALID, source.getSourceId(), source, source.getControllerId()), source)) {
                // game.restoreState(state, "Targets");
                clearChosen();
            }
        }
    }
    return true;
}
Also used : GameEvent(mage.game.events.GameEvent) UUID(java.util.UUID)

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