Search in sources :

Example 1 with ManaPoolItem

use of mage.players.ManaPoolItem 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 2 with ManaPoolItem

use of mage.players.ManaPoolItem in project mage by magefree.

the class ComputerPlayer method canUseAsThoughManaToPayManaCost.

boolean canUseAsThoughManaToPayManaCost(ManaCost checkCost, Ability abilityToPay, Mana manaOption, Ability manaAbility, MageObject manaProducer, Game game) {
    // asThoughMana can change producing mana type, so you must check it here
    // cause some effects adds additional checks in getAsThoughManaType (example: Draugr Necromancer with snow mana sources)
    // simulate real asThoughMana usage
    ManaPoolItem possiblePoolItem;
    if (manaOption instanceof ConditionalMana) {
        ConditionalMana conditionalNetMana = (ConditionalMana) manaOption;
        possiblePoolItem = new ManaPoolItem(conditionalNetMana, manaAbility.getSourceObject(game), conditionalNetMana.getManaProducerOriginalId() != null ? conditionalNetMana.getManaProducerOriginalId() : manaAbility.getOriginalId());
    } else {
        possiblePoolItem = new ManaPoolItem(manaOption.getRed(), manaOption.getGreen(), manaOption.getBlue(), manaOption.getWhite(), manaOption.getBlack(), manaOption.getGeneric() + manaOption.getColorless(), manaProducer, manaAbility.getOriginalId(), manaOption.getFlag());
    }
    // cost can contains multiple mana types, must check each type (is it possible to pay a cost)
    for (ManaType checkType : ManaUtil.getManaTypesInCost(checkCost)) {
        // affected asThoughMana effect must fit a checkType with pool mana
        ManaType possibleAsThoughPoolManaType = game.getContinuousEffects().asThoughMana(checkType, possiblePoolItem, abilityToPay.getSourceId(), abilityToPay, abilityToPay.getControllerId(), game);
        if (possibleAsThoughPoolManaType == null) {
            // no affected asThough effects
            continue;
        }
        boolean canPay;
        if (possibleAsThoughPoolManaType == ManaType.COLORLESS) {
            // colorless can be payed by any color from the pool
            canPay = possiblePoolItem.count() > 0;
        } else {
            // colored must be payed by specific color from the pool (AsThough already changed it to fit with mana pool)
            canPay = possiblePoolItem.get(possibleAsThoughPoolManaType) > 0;
        }
        if (canPay) {
            return true;
        }
    }
    return false;
}
Also used : ManaPoolItem(mage.players.ManaPoolItem) ConditionalMana(mage.ConditionalMana)

Aggregations

ManaPoolItem (mage.players.ManaPoolItem)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ConditionalMana (mage.ConditionalMana)1 Ability (mage.abilities.Ability)1 ActivatedAbility (mage.abilities.ActivatedAbility)1 ManaCost (mage.abilities.costs.mana.ManaCost)1 ActivatedManaAbilityImpl (mage.abilities.mana.ActivatedManaAbilityImpl)1 FilterLandPermanent (mage.filter.common.FilterLandPermanent)1 PermanentInListPredicate (mage.filter.predicate.permanent.PermanentInListPredicate)1 Permanent (mage.game.permanent.Permanent)1 Player (mage.players.Player)1 TargetPermanent (mage.target.TargetPermanent)1 TargetPlayer (mage.target.TargetPlayer)1