Search in sources :

Example 6 with ActivatedManaAbilityImpl

use of mage.abilities.mana.ActivatedManaAbilityImpl in project mage by magefree.

the class PithingNeedleEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    MageObject object = game.getObject(event.getSourceId());
    String cardName = (String) game.getState().getValue(source.getSourceId().toString() + ChooseACardNameEffect.INFO_KEY);
    Optional<Ability> ability = game.getAbility(event.getTargetId(), event.getSourceId());
    if (ability.isPresent() && object != null) {
        return // controller in range
        game.getState().getPlayersInRange(source.getControllerId(), game).contains(event.getPlayerId()) && // not an activated mana ability
        !(ability.get() instanceof ActivatedManaAbilityImpl) && CardUtil.haveSameNames(object, cardName, game);
    }
    return false;
}
Also used : SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) AsEntersBattlefieldAbility(mage.abilities.common.AsEntersBattlefieldAbility) Ability(mage.abilities.Ability) MageObject(mage.MageObject) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl)

Example 7 with ActivatedManaAbilityImpl

use of mage.abilities.mana.ActivatedManaAbilityImpl in project mage by magefree.

the class PowerSinkCounterUnlessPaysEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    StackObject spell = game.getStack().getStackObject(targetPointer.getFirst(game, source));
    if (spell != null) {
        Player player = game.getPlayer(spell.getControllerId());
        Player controller = game.getPlayer(source.getControllerId());
        MageObject sourceObject = game.getObject(source.getSourceId());
        if (player != null && controller != null && sourceObject != null) {
            int amount = source.getManaCostsToPay().getX();
            if (amount > 0) {
                Cost cost = ManaUtil.createManaCost(amount, true);
                if (player.chooseUse(Outcome.Benefit, "Pay " + cost.getText() + " to prevent?", source, game)) {
                    if (cost.pay(source, game, source, player.getId(), false)) {
                        game.informPlayers(sourceObject.getName() + ": additional cost was paid");
                        return true;
                    }
                }
                game.informPlayers(sourceObject.getName() + ": additional cost wasn't paid - countering " + spell.getName());
                // Counter target spell unless its controller pays {X}
                game.getStack().counter(source.getFirstTarget(), source, game);
                // that player taps all lands with mana abilities they control...
                List<Permanent> lands = game.getBattlefield().getAllActivePermanents(new FilterLandPermanent(), player.getId(), game);
                for (Permanent land : lands) {
                    Abilities<Ability> landAbilities = land.getAbilities();
                    for (Ability ability : landAbilities) {
                        if (ability instanceof ActivatedManaAbilityImpl) {
                            land.tap(source, game);
                            break;
                        }
                    }
                }
                // ...and empties their mana pool
                player.getManaPool().emptyPool(game);
            }
            return true;
        }
    }
    return false;
}
Also used : Ability(mage.abilities.Ability) Player(mage.players.Player) FilterLandPermanent(mage.filter.common.FilterLandPermanent) FilterLandPermanent(mage.filter.common.FilterLandPermanent) Permanent(mage.game.permanent.Permanent) StackObject(mage.game.stack.StackObject) MageObject(mage.MageObject) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) Cost(mage.abilities.costs.Cost)

Example 8 with ActivatedManaAbilityImpl

use of mage.abilities.mana.ActivatedManaAbilityImpl in project mage by magefree.

the class PlayerImpl method findActivatedAbilityFromPlayable.

protected ActivatedAbility findActivatedAbilityFromPlayable(MageObject object, ManaOptions availableMana, Ability ability, Game game) {
    // special mana to pay spell cost
    ManaOptions manaFull = availableMana.copy();
    if (ability instanceof SpellAbility) {
        for (AlternateManaPaymentAbility altAbility : CardUtil.getAbilities(object, game).stream().filter(a -> a instanceof AlternateManaPaymentAbility).map(a -> (AlternateManaPaymentAbility) a).collect(Collectors.toList())) {
            ManaOptions manaSpecial = altAbility.getManaOptions(ability, game, ability.getManaCostsToPay());
            manaFull.addMana(manaSpecial);
        }
    }
    // replace alternative abilities by real play abilities (e.g. morph/facedown static ability by play land)
    if (ability instanceof ActivatedManaAbilityImpl) {
        // mana ability
        if (((ActivatedManaAbilityImpl) ability).canActivate(this.getId(), game).canActivate()) {
            return (ActivatedManaAbilityImpl) ability;
        }
    } else if (ability instanceof AlternativeSourceCosts) {
        // alternative cost must be replaced by real play ability
        return findActivatedAbilityFromAlternativeSourceCost(object, manaFull, ability, game);
    } else if (ability instanceof ActivatedAbility) {
        // all other activated ability
        if (canPlay((ActivatedAbility) ability, manaFull, object, game)) {
            return (ActivatedAbility) ability;
        }
    }
    // non playable abilities like static
    return null;
}
Also used : MatchPlayer(mage.game.match.MatchPlayer) PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) UserData(mage.players.net.UserData) Counters(mage.counters.Counters) mage.abilities.costs(mage.abilities.costs) Logger(org.apache.log4j.Logger) ManaCost(mage.abilities.costs.mana.ManaCost) StackObject(mage.game.stack.StackObject) mage.constants(mage.constants) mage.abilities(mage.abilities) Choice(mage.choices.Choice) FilterMana(mage.filter.FilterMana) FilterCard(mage.filter.FilterCard) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) StaticFilters(mage.filter.StaticFilters) GameLog(mage.util.GameLog) AlternateManaPaymentAbility(mage.abilities.costs.mana.AlternateManaPaymentAbility) RestrictionUntapNotMoreThanEffect(mage.abilities.effects.RestrictionUntapNotMoreThanEffect) ImmutableMap(com.google.common.collect.ImmutableMap) mage.abilities.keyword(mage.abilities.keyword) mage.cards(mage.cards) FilterCreatureForCombatBlock(mage.filter.common.FilterCreatureForCombatBlock) TargetDiscard(mage.target.common.TargetDiscard) StackAbility(mage.game.stack.StackAbility) CardUtil(mage.util.CardUtil) Collectors(java.util.stream.Collectors) CommandObject(mage.game.command.CommandObject) Serializable(java.io.Serializable) FilterCreatureForCombat(mage.filter.common.FilterCreatureForCombat) Permanent(mage.game.permanent.Permanent) Entry(java.util.Map.Entry) TargetAmount(mage.target.TargetAmount) WhileSearchingPlayFromLibraryAbility(mage.abilities.common.WhileSearchingPlayFromLibraryAbility) CounterType(mage.counters.CounterType) Target(mage.target.Target) mage(mage) java.util(java.util) RestrictionEffect(mage.abilities.effects.RestrictionEffect) mage.game(mage.game) LoseControlOnOtherPlayersControllerEffect(mage.abilities.effects.common.LoseControlOnOtherPlayersControllerEffect) DesignationType(mage.designations.DesignationType) mage.game.events(mage.game.events) AtTheEndOfTurnStepPostDelayedTriggeredAbility(mage.abilities.common.delayed.AtTheEndOfTurnStepPostDelayedTriggeredAbility) Predicates(mage.filter.predicate.Predicates) FilterPermanent(mage.filter.FilterPermanent) ManaOptions(mage.abilities.mana.ManaOptions) Step(mage.game.turn.Step) Designation(mage.designations.Designation) PermanentToken(mage.game.permanent.PermanentToken) TargetCard(mage.target.TargetCard) Counter(mage.counters.Counter) CombatGroup(mage.game.combat.CombatGroup) Spell(mage.game.stack.Spell) MageDrawAction(mage.actions.MageDrawAction) RandomUtil(mage.util.RandomUtil) TargetCardInLibrary(mage.target.common.TargetCardInLibrary) ManaCosts(mage.abilities.costs.mana.ManaCosts) PassAbility(mage.abilities.common.PassAbility) SquirrelToken(mage.game.permanent.token.SquirrelToken) Deck(mage.cards.decks.Deck) PlayLandAsCommanderAbility(mage.abilities.common.PlayLandAsCommanderAbility) ManaCostsImpl(mage.abilities.costs.mana.ManaCostsImpl) ChoiceImpl(mage.choices.ChoiceImpl) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) ActivationStatus(mage.abilities.ActivatedAbility.ActivationStatus) PermanentCard(mage.game.permanent.PermanentCard) TargetPermanent(mage.target.TargetPermanent) ManaOptions(mage.abilities.mana.ManaOptions) AlternateManaPaymentAbility(mage.abilities.costs.mana.AlternateManaPaymentAbility) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl)

Example 9 with ActivatedManaAbilityImpl

use of mage.abilities.mana.ActivatedManaAbilityImpl in project mage by magefree.

the class PlayerImpl method getAvailableManaProducers.

// returns only mana producers that don't require mana payment
protected List<MageObject> getAvailableManaProducers(Game game) {
    List<MageObject> result = new ArrayList<>();
    for (Permanent permanent : game.getBattlefield().getActivePermanents(playerId, game)) {
        // Some permanents allow use of abilities from non controlling players. so check all permanents in range
        Boolean canUse = null;
        boolean canAdd = false;
        for (ActivatedManaAbilityImpl ability : permanent.getAbilities().getActivatedManaAbilities(Zone.BATTLEFIELD)) {
            if (!ability.getManaCosts().isEmpty()) {
                canAdd = false;
                break;
            }
            if (canUse == null) {
                canUse = permanent.canUseActivatedAbilities(game);
            }
            if (canUse && ability.canActivate(playerId, game).canActivate()) {
                canAdd = true;
            }
        }
        if (canAdd) {
            result.add(permanent);
        }
    }
    for (Card card : getHand().getCards(game)) {
        boolean canAdd = false;
        for (ActivatedManaAbilityImpl ability : card.getAbilities(game).getActivatedManaAbilities(Zone.HAND)) {
            if (!ability.getManaCosts().isEmpty()) {
                canAdd = false;
                break;
            }
            if (ability.canActivate(playerId, game).canActivate()) {
                canAdd = true;
            }
        }
        if (canAdd) {
            result.add(card);
        }
    }
    return result;
}
Also used : Permanent(mage.game.permanent.Permanent) FilterPermanent(mage.filter.FilterPermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) TargetPermanent(mage.target.TargetPermanent) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) FilterCard(mage.filter.FilterCard) TargetCard(mage.target.TargetCard) PermanentCard(mage.game.permanent.PermanentCard)

Example 10 with ActivatedManaAbilityImpl

use of mage.abilities.mana.ActivatedManaAbilityImpl in project mage by magefree.

the class PlayerImpl method getManaAvailable.

/**
 * Returns the mana options the player currently has. That means which
 * combinations of mana are available to cast spells or activate abilities
 * etc.
 *
 * @param game
 * @return
 */
@Override
public ManaOptions getManaAvailable(Game game) {
    boolean oldState = game.inCheckPlayableState();
    game.setCheckPlayableState(true);
    ManaOptions availableMana = new ManaOptions();
    availableMana.addMana(manaPool.getMana());
    // conditional mana
    for (ConditionalMana conditionalMana : manaPool.getConditionalMana()) {
        availableMana.addMana(conditionalMana);
    }
    List<Abilities<ActivatedManaAbilityImpl>> sourceWithoutManaCosts = new ArrayList<>();
    List<Abilities<ActivatedManaAbilityImpl>> sourceWithCosts = new ArrayList<>();
    for (Card card : getHand().getCards(game)) {
        Abilities<ActivatedManaAbilityImpl> manaAbilities = card.getAbilities(game).getAvailableActivatedManaAbilities(Zone.HAND, playerId, game);
        for (Iterator<ActivatedManaAbilityImpl> it = manaAbilities.iterator(); it.hasNext(); ) {
            ActivatedManaAbilityImpl ability = it.next();
            Abilities<ActivatedManaAbilityImpl> noTapAbilities = new AbilitiesImpl<>(ability);
            if (ability.getManaCosts().isEmpty() && !ability.isPoolDependant()) {
                sourceWithoutManaCosts.add(noTapAbilities);
            } else {
                sourceWithCosts.add(noTapAbilities);
            }
        }
    }
    for (Permanent permanent : game.getBattlefield().getActivePermanents(playerId, game)) {
        // Some permanents allow use of abilities from non controlling players. so check all permanents in range
        Boolean canUse = null;
        boolean canAdd = false;
        // sources with mana costs or mana pool dependency
        boolean useLater = false;
        Abilities<ActivatedManaAbilityImpl> manaAbilities = // returns ability only if canActivate is true
        permanent.getAbilities(game).getAvailableActivatedManaAbilities(Zone.BATTLEFIELD, playerId, game);
        for (Iterator<ActivatedManaAbilityImpl> it = manaAbilities.iterator(); it.hasNext(); ) {
            ActivatedManaAbilityImpl ability = it.next();
            if (canUse == null) {
                canUse = permanent.canUseActivatedAbilities(game);
            }
            if (canUse) {
                // abilities without Tap costs have to be handled as separate sources, because they can be used also
                if (!ability.hasTapCost()) {
                    it.remove();
                    Abilities<ActivatedManaAbilityImpl> noTapAbilities = new AbilitiesImpl<>(ability);
                    if (ability.getManaCosts().isEmpty() && !ability.isPoolDependant()) {
                        sourceWithoutManaCosts.add(noTapAbilities);
                    } else {
                        sourceWithCosts.add(noTapAbilities);
                    }
                    continue;
                }
                canAdd = true;
                if (!ability.getManaCosts().isEmpty() || ability.isPoolDependant()) {
                    useLater = true;
                    break;
                }
            }
        }
        if (canAdd) {
            if (useLater) {
                sourceWithCosts.add(manaAbilities);
            } else {
                sourceWithoutManaCosts.add(manaAbilities);
            }
        }
    }
    for (Abilities<ActivatedManaAbilityImpl> manaAbilities : sourceWithoutManaCosts) {
        availableMana.addMana(manaAbilities, game);
    }
    boolean anAbilityWasUsed = true;
    // use such abilities later than other if possible because it can maximize mana production
    boolean usePoolDependantAbilities = false;
    while (anAbilityWasUsed && !sourceWithCosts.isEmpty()) {
        anAbilityWasUsed = false;
        for (Iterator<Abilities<ActivatedManaAbilityImpl>> iterator = sourceWithCosts.iterator(); iterator.hasNext(); ) {
            Abilities<ActivatedManaAbilityImpl> manaAbilities = iterator.next();
            if (usePoolDependantAbilities || !manaAbilities.hasPoolDependantAbilities()) {
                boolean used;
                if (manaAbilities.hasPoolDependantAbilities()) {
                    used = availableMana.addManaPoolDependant(manaAbilities, game);
                } else {
                    used = availableMana.addManaWithCost(manaAbilities, game);
                }
                if (used) {
                    iterator.remove();
                    availableMana.removeDuplicated();
                    anAbilityWasUsed = true;
                }
            }
        }
        if (!anAbilityWasUsed && !usePoolDependantAbilities) {
            usePoolDependantAbilities = true;
            anAbilityWasUsed = true;
        }
    }
    // remove duplicated variants (see ManaOptionsTest for info - when that rises)
    availableMana.removeDuplicated();
    game.setCheckPlayableState(oldState);
    return availableMana;
}
Also used : ManaOptions(mage.abilities.mana.ManaOptions) Permanent(mage.game.permanent.Permanent) FilterPermanent(mage.filter.FilterPermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) TargetPermanent(mage.target.TargetPermanent) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) FilterCard(mage.filter.FilterCard) TargetCard(mage.target.TargetCard) PermanentCard(mage.game.permanent.PermanentCard)

Aggregations

ActivatedManaAbilityImpl (mage.abilities.mana.ActivatedManaAbilityImpl)29 Permanent (mage.game.permanent.Permanent)12 StackAbility (mage.game.stack.StackAbility)7 MageObject (mage.MageObject)6 Mana (mage.Mana)5 Ability (mage.abilities.Ability)5 ManaCost (mage.abilities.costs.mana.ManaCost)5 Card (mage.cards.Card)5 FilterCard (mage.filter.FilterCard)5 FilterPermanent (mage.filter.FilterPermanent)5 TargetPermanent (mage.target.TargetPermanent)5 UUID (java.util.UUID)4 ConditionalMana (mage.ConditionalMana)4 ManaOptions (mage.abilities.mana.ManaOptions)4 FilterControlledPermanent (mage.filter.common.FilterControlledPermanent)4 PermanentCard (mage.game.permanent.PermanentCard)4 LinkedHashMap (java.util.LinkedHashMap)3 ManaCostsImpl (mage.abilities.costs.mana.ManaCostsImpl)3 Counter (mage.counters.Counter)3 Player (mage.players.Player)3