Search in sources :

Example 1 with ManaPaidEvent

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

the class ManaPaidSourceWatcher method watch.

@Override
public void watch(GameEvent event, Game game) {
    switch(event.getType()) {
        case ZONE_CHANGE:
            if (((ZoneChangeEvent) event).getFromZone() == Zone.BATTLEFIELD) {
                manaMap.remove(event.getTargetId());
            }
            return;
        case MANA_PAID:
            ManaPaidEvent manaEvent = (ManaPaidEvent) event;
            manaMap.computeIfAbsent(manaEvent.getTargetId(), x -> new ManaPaidTracker()).increment(manaEvent.getSourceObject(), manaEvent.getManaType(), game);
            manaMap.computeIfAbsent(manaEvent.getSourcePaidId(), x -> new ManaPaidTracker()).increment(manaEvent.getSourceObject(), manaEvent.getManaType(), game);
    }
}
Also used : Zone(mage.constants.Zone) HashMap(java.util.HashMap) UUID(java.util.UUID) SubType(mage.constants.SubType) ManaPaidEvent(mage.game.events.ManaPaidEvent) Serializable(java.io.Serializable) Game(mage.game.Game) Watcher(mage.watchers.Watcher) GameEvent(mage.game.events.GameEvent) ZoneChangeEvent(mage.game.events.ZoneChangeEvent) ManaType(mage.constants.ManaType) Map(java.util.Map) WatcherScope(mage.constants.WatcherScope) MageObject(mage.MageObject) Spell(mage.game.stack.Spell) ObjectColor(mage.ObjectColor) Copyable(mage.util.Copyable) ManaPaidEvent(mage.game.events.ManaPaidEvent)

Example 2 with ManaPaidEvent

use of mage.game.events.ManaPaidEvent 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 3 with ManaPaidEvent

use of mage.game.events.ManaPaidEvent 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)

Aggregations

GameEvent (mage.game.events.GameEvent)3 ManaPaidEvent (mage.game.events.ManaPaidEvent)3 ManaType (mage.constants.ManaType)2 Spell (mage.game.stack.Spell)2 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UUID (java.util.UUID)1 ConditionalMana (mage.ConditionalMana)1 MageObject (mage.MageObject)1 ObjectColor (mage.ObjectColor)1 SubType (mage.constants.SubType)1 WatcherScope (mage.constants.WatcherScope)1 Zone (mage.constants.Zone)1 Game (mage.game.Game)1 ZoneChangeEvent (mage.game.events.ZoneChangeEvent)1 Copyable (mage.util.Copyable)1 Watcher (mage.watchers.Watcher)1