Search in sources :

Example 16 with ActivatedManaAbilityImpl

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

the class DrainPowerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Player targetPlayer = game.getPlayer(source.getFirstTarget());
    if (targetPlayer != null) {
        List<Permanent> ignorePermanents = new ArrayList<>();
        Map<Permanent, List<ActivatedManaAbilityImpl>> manaAbilitiesMap = new HashMap<>();
        TargetPermanent target = null;
        while (true) {
            targetPlayer.setPayManaMode(true);
            manaAbilitiesMap.clear();
            for (Permanent permanent : game.getBattlefield().getAllActivePermanents(filter, targetPlayer.getId(), game)) {
                if (!ignorePermanents.contains(permanent)) {
                    List<ActivatedManaAbilityImpl> manaAbilities = new ArrayList<>();
                    abilitySearch: for (Ability ability : permanent.getAbilities()) {
                        if (ability instanceof ActivatedAbility && ability.getAbilityType() == AbilityType.MANA) {
                            ActivatedManaAbilityImpl manaAbility = (ActivatedManaAbilityImpl) ability;
                            if (manaAbility.canActivate(targetPlayer.getId(), game).canActivate()) {
                                // so it's necessary to filter them out manually - might be buggy in some fringe cases
                                for (ManaCost manaCost : manaAbility.getManaCosts()) {
                                    if (!targetPlayer.getManaPool().getMana().includesMana(manaCost.getMana())) {
                                        continue abilitySearch;
                                    }
                                }
                                manaAbilities.add(manaAbility);
                            }
                        }
                    }
                    if (!manaAbilities.isEmpty()) {
                        manaAbilitiesMap.put(permanent, manaAbilities);
                    }
                }
            }
            if (manaAbilitiesMap.isEmpty()) {
                break;
            }
            List<Permanent> permList = new ArrayList<>(manaAbilitiesMap.keySet());
            Permanent permanent;
            if (permList.size() > 1 || target != null) {
                FilterLandPermanent filter2 = new FilterLandPermanent("land you control to tap for mana (remaining: " + permList.size() + ')');
                filter2.add(new PermanentInListPredicate(permList));
                target = new TargetPermanent(1, 1, filter2, true);
                while (!target.isChosen() && target.canChoose(source.getSourceId(), targetPlayer.getId(), game) && targetPlayer.canRespond()) {
                    targetPlayer.chooseTarget(Outcome.Neutral, target, source, game);
                }
                permanent = game.getPermanent(target.getFirstTarget());
            } else {
                permanent = permList.get(0);
            }
            if (permanent != null) {
                int i = 0;
                for (ActivatedManaAbilityImpl manaAbility : manaAbilitiesMap.get(permanent)) {
                    i++;
                    if (manaAbilitiesMap.get(permanent).size() <= i || targetPlayer.chooseUse(Outcome.Neutral, "Activate mana ability \"" + manaAbility.getRule() + "\" of " + permanent.getLogName() + "? (Choose \"no\" to activate next mana ability)", source, game)) {
                        boolean originalCanUndo = manaAbility.isUndoPossible();
                        // prevents being able to undo Drain Power
                        manaAbility.setUndoPossible(false);
                        if (targetPlayer.activateAbility(manaAbility, game)) {
                            ignorePermanents.add(permanent);
                        }
                        // resets undoPossible to its original state
                        manaAbility.setUndoPossible(originalCanUndo);
                        break;
                    }
                }
            }
        }
        targetPlayer.setPayManaMode(false);
        // 106.12. One card (Drain Power) causes one player to lose unspent mana and another to add “the mana lost this way.” (Note that these may be the same player.)
        // This empties the former player's mana pool and causes the mana emptied this way to be put into the latter player's mana pool. Which permanents, spells, and/or
        // abilities produced that mana are unchanged, as are any restrictions or additional effects associated with any of that mana.
        List<ManaPoolItem> manaItems = targetPlayer.getManaPool().getManaItems();
        targetPlayer.getManaPool().emptyPool(game);
        for (ManaPoolItem manaPoolItem : manaItems) {
            controller.getManaPool().addMana(manaPoolItem.isConditional() ? manaPoolItem.getConditionalMana() : manaPoolItem.getMana(), game, source, Duration.EndOfTurn.equals(manaPoolItem.getDuration()));
        }
        return true;
    }
    return false;
}
Also used : ActivatedAbility(mage.abilities.ActivatedAbility) Ability(mage.abilities.Ability) TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) PermanentInListPredicate(mage.filter.predicate.permanent.PermanentInListPredicate) FilterLandPermanent(mage.filter.common.FilterLandPermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) ActivatedAbility(mage.abilities.ActivatedAbility) FilterLandPermanent(mage.filter.common.FilterLandPermanent) ManaPoolItem(mage.players.ManaPoolItem) ManaCost(mage.abilities.costs.mana.ManaCost) ArrayList(java.util.ArrayList) List(java.util.List) TargetPermanent(mage.target.TargetPermanent)

Example 17 with ActivatedManaAbilityImpl

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

the class KurkeshOnakkeAncientTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (!event.getPlayerId().equals(getControllerId())) {
        return false;
    }
    Card source = game.getPermanentOrLKIBattlefield(event.getSourceId());
    if (source == null || !source.isArtifact(game)) {
        return false;
    }
    StackAbility stackAbility = (StackAbility) game.getStack().getStackObject(event.getSourceId());
    if (stackAbility == null || stackAbility.getStackAbility() instanceof ActivatedManaAbilityImpl) {
        return false;
    }
    this.getEffects().setValue("stackAbility", stackAbility);
    return true;
}
Also used : ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) StackAbility(mage.game.stack.StackAbility) Card(mage.cards.Card)

Example 18 with ActivatedManaAbilityImpl

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

the class CardImpl method addAbility.

/**
 * Public in order to support adding abilities to SplitCardHalf's
 *
 * @param ability
 */
@Override
public void addAbility(Ability ability) {
    ability.setSourceId(this.getId());
    abilities.add(ability);
    abilities.addAll(ability.getSubAbilities());
    // reason: triggered abilities are not processing here
    if (this instanceof PermanentCard) {
        throw new IllegalArgumentException("Wrong code usage. Don't use that method for permanents, use permanent.addAbility(a, source, game) instead.");
    }
    // verify test will catch that errors
    if (ability instanceof ActivatedManaAbilityImpl) {
        ActivatedManaAbilityImpl manaAbility = (ActivatedManaAbilityImpl) ability;
        String rule = manaAbility.getRule().toLowerCase(Locale.ENGLISH);
        if (manaAbility.getEffects().stream().anyMatch(e -> e.getOutcome().equals(Outcome.DrawCard)) || rule.contains("reveal ") || rule.contains("draw ")) {
            if (manaAbility.isUndoPossible()) {
                throw new IllegalArgumentException("Ability contains draw/reveal effect, but isUndoPossible is true. Ability: " + ability.getClass().getSimpleName() + "; " + ability.getRule());
            }
        }
    }
}
Also used : ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) PermanentCard(mage.game.permanent.PermanentCard)

Example 19 with ActivatedManaAbilityImpl

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

the class RingsOfBrighthearthTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (!event.getPlayerId().equals(getControllerId())) {
        return false;
    }
    StackAbility stackAbility = (StackAbility) game.getStack().getStackObject(event.getSourceId());
    if (stackAbility == null || stackAbility.getStackAbility() instanceof ActivatedManaAbilityImpl) {
        return false;
    }
    this.getEffects().setValue("stackAbility", stackAbility);
    return true;
}
Also used : ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) StackAbility(mage.game.stack.StackAbility)

Example 20 with ActivatedManaAbilityImpl

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

the class ManaUtilTest method testManaToPayVsLand.

/**
 * Common way to test ManaUtil.tryToAutoPay
 *
 * We get all mana abilities, then try to auto pay and compare to expected1
 * and expected2 params.
 *
 * @param manaToPay Mana that should be paid using land.
 * @param landName Land to use as mana producer.
 * @param expected1 The amount of mana abilities the land should have.
 * @param expected2 The amount of mana abilities that ManaUtil.tryToAutoPay
 * should be returned after optimization.
 */
private void testManaToPayVsLand(String manaToPay, String landName, int expected1, int expected2) {
    ManaCost unpaid = new ManaCostsImpl(manaToPay);
    Card card = CardRepository.instance.findCard(landName).getCard();
    Assert.assertNotNull(card);
    Map<UUID, ActivatedManaAbilityImpl> useableAbilities = getManaAbilities(card);
    Assert.assertEquals(expected1, useableAbilities.size());
    useableAbilities = ManaUtil.tryToAutoPay(unpaid, (LinkedHashMap<UUID, ActivatedManaAbilityImpl>) useableAbilities);
    Assert.assertEquals(expected2, useableAbilities.size());
}
Also used : ManaCost(mage.abilities.costs.mana.ManaCost) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) UUID(java.util.UUID) ManaCostsImpl(mage.abilities.costs.mana.ManaCostsImpl) Card(mage.cards.Card) LinkedHashMap(java.util.LinkedHashMap)

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