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);
}
}
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;
}
}
}
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;
}
Aggregations