Search in sources :

Example 96 with ManaOptions

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

the class AddManaInAnyCombinationEffect method getNetMana.

@Override
public List<Mana> getNetMana(Game game, Ability source) {
    List<Mana> netMana = new ArrayList<>();
    if (game != null) {
        if (game.inCheckPlayableState()) {
            int count = netAmount.calculate(game, source, this);
            if (count > 0) {
                // add color combinations
                ManaOptions allPossibleMana = new ManaOptions();
                for (int i = 0; i < count; ++i) {
                    ManaOptions currentPossibleMana = new ManaOptions();
                    for (ColoredManaSymbol coloredManaSymbol : manaSymbols) {
                        currentPossibleMana.add(new Mana(coloredManaSymbol));
                    }
                    allPossibleMana.addMana(currentPossibleMana);
                }
                allPossibleMana.removeDuplicated();
                return allPossibleMana.stream().collect(Collectors.toList());
            }
        } else {
            int amountOfManaLeft = amount.calculate(game, source, this);
            if (amountOfManaLeft > 0) {
                netMana.add(Mana.AnyMana(amountOfManaLeft));
            }
        }
    }
    return netMana;
}
Also used : ManaOptions(mage.abilities.mana.ManaOptions) Mana(mage.Mana) ColoredManaSymbol(mage.constants.ColoredManaSymbol)

Example 97 with ManaOptions

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

the class ComputerPlayer method playManaHandling.

protected boolean playManaHandling(Ability ability, ManaCost unpaid, final Game game) {
    // log.info("paying for " + unpaid.getText());
    ApprovingObject approvingObject = game.getContinuousEffects().asThough(ability.getSourceId(), AsThoughEffectType.SPEND_OTHER_MANA, ability, ability.getControllerId(), game);
    ManaCost cost;
    List<MageObject> producers;
    if (unpaid instanceof ManaCosts) {
        ManaCosts<ManaCost> manaCosts = (ManaCosts<ManaCost>) unpaid;
        cost = manaCosts.get(manaCosts.size() - 1);
        producers = getSortedProducers((ManaCosts) unpaid, game);
    } else {
        cost = unpaid;
        producers = this.getAvailableManaProducers(game);
        producers.addAll(this.getAvailableManaProducersWithCost(game));
    }
    for (MageObject mageObject : producers) {
        // otherwise the computer may not be able to pay the cost for that source
        ManaAbility: for (ActivatedManaAbilityImpl manaAbility : getManaAbilitiesSortedByManaCount(mageObject, game)) {
            int colored = 0;
            for (Mana mana : manaAbility.getNetMana(game)) {
                if (!unpaid.getMana().includesMana(mana)) {
                    continue ManaAbility;
                }
                colored += mana.countColored();
            }
            if (colored > 1 && (cost instanceof ColoredManaCost)) {
                for (Mana netMana : manaAbility.getNetMana(game)) {
                    if (cost.testPay(netMana)) {
                        if (netMana instanceof ConditionalMana && !((ConditionalMana) netMana).apply(ability, game, getId(), cost)) {
                            continue;
                        }
                        if (approvingObject != null && !canUseAsThoughManaToPayManaCost(cost, ability, netMana, manaAbility, mageObject, game)) {
                            continue;
                        }
                        if (activateAbility(manaAbility, game)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    for (MageObject mageObject : producers) {
        // pay all colored costs first
        for (ActivatedManaAbilityImpl manaAbility : getManaAbilitiesSortedByManaCount(mageObject, game)) {
            if (cost instanceof ColoredManaCost) {
                for (Mana netMana : manaAbility.getNetMana(game)) {
                    if (cost.testPay(netMana) || approvingObject != null) {
                        if (netMana instanceof ConditionalMana && !((ConditionalMana) netMana).apply(ability, game, getId(), cost)) {
                            continue;
                        }
                        if (approvingObject != null && !canUseAsThoughManaToPayManaCost(cost, ability, netMana, manaAbility, mageObject, game)) {
                            continue;
                        }
                        if (activateAbility(manaAbility, game)) {
                            return true;
                        }
                    }
                }
            }
        }
        // pay snow covered mana
        for (ActivatedManaAbilityImpl manaAbility : getManaAbilitiesSortedByManaCount(mageObject, game)) {
            if (cost instanceof SnowManaCost) {
                for (Mana netMana : manaAbility.getNetMana(game)) {
                    if (cost.testPay(netMana) || approvingObject != null) {
                        if (netMana instanceof ConditionalMana && !((ConditionalMana) netMana).apply(ability, game, getId(), cost)) {
                            continue;
                        }
                        if (approvingObject != null && !canUseAsThoughManaToPayManaCost(cost, ability, netMana, manaAbility, mageObject, game)) {
                            continue;
                        }
                        if (activateAbility(manaAbility, game)) {
                            return true;
                        }
                    }
                }
            }
        }
        // then pay hybrid
        for (ActivatedManaAbilityImpl manaAbility : getManaAbilitiesSortedByManaCount(mageObject, game)) {
            if (cost instanceof HybridManaCost) {
                for (Mana netMana : manaAbility.getNetMana(game)) {
                    if (cost.testPay(netMana) || approvingObject != null) {
                        if (netMana instanceof ConditionalMana && !((ConditionalMana) netMana).apply(ability, game, getId(), cost)) {
                            continue;
                        }
                        if (approvingObject != null && !canUseAsThoughManaToPayManaCost(cost, ability, netMana, manaAbility, mageObject, game)) {
                            continue;
                        }
                        if (activateAbility(manaAbility, game)) {
                            return true;
                        }
                    }
                }
            }
        }
        // then pay mono hybrid
        for (ActivatedManaAbilityImpl manaAbility : getManaAbilitiesSortedByManaCount(mageObject, game)) {
            if (cost instanceof MonoHybridManaCost) {
                for (Mana netMana : manaAbility.getNetMana(game)) {
                    if (cost.testPay(netMana) || approvingObject != null) {
                        if (netMana instanceof ConditionalMana && !((ConditionalMana) netMana).apply(ability, game, getId(), cost)) {
                            continue;
                        }
                        if (approvingObject != null && !canUseAsThoughManaToPayManaCost(cost, ability, netMana, manaAbility, mageObject, game)) {
                            continue;
                        }
                        if (activateAbility(manaAbility, game)) {
                            return true;
                        }
                    }
                }
            }
        }
        // pay colorless
        for (ActivatedManaAbilityImpl manaAbility : getManaAbilitiesSortedByManaCount(mageObject, game)) {
            if (cost instanceof ColorlessManaCost) {
                for (Mana netMana : manaAbility.getNetMana(game)) {
                    if (cost.testPay(netMana) || approvingObject != null) {
                        if (netMana instanceof ConditionalMana && !((ConditionalMana) netMana).apply(ability, game, getId(), cost)) {
                            continue;
                        }
                        if (approvingObject != null && !canUseAsThoughManaToPayManaCost(cost, ability, netMana, manaAbility, mageObject, game)) {
                            continue;
                        }
                        if (activateAbility(manaAbility, game)) {
                            return true;
                        }
                    }
                }
            }
        }
        // finally pay generic
        for (ActivatedManaAbilityImpl manaAbility : getManaAbilitiesSortedByManaCount(mageObject, game)) {
            if (cost instanceof GenericManaCost) {
                for (Mana netMana : manaAbility.getNetMana(game)) {
                    if (cost.testPay(netMana) || approvingObject != null) {
                        if (netMana instanceof ConditionalMana && !((ConditionalMana) netMana).apply(ability, game, getId(), cost)) {
                            continue;
                        }
                        if (approvingObject != null && !canUseAsThoughManaToPayManaCost(cost, ability, netMana, manaAbility, mageObject, game)) {
                            continue;
                        }
                        if (activateAbility(manaAbility, game)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    // pay phyrexian life costs
    if (cost.isPhyrexian()) {
        return cost.pay(ability, game, ability, playerId, false, null) || approvingObject != null;
    }
    // pay special mana like convoke cost (tap for pay)
    // GUI: user see "special" button while pay spell's cost
    // TODO: AI can't prioritize special mana types to pay, e.g. it will use first available
    SpecialAction specialAction = game.getState().getSpecialActions().getControlledBy(this.getId(), true).values().stream().findFirst().orElse(null);
    ManaOptions specialMana = specialAction == null ? null : specialAction.getManaOptions(ability, game, unpaid);
    if (specialMana != null) {
        for (Mana netMana : specialMana) {
            if (cost.testPay(netMana) || approvingObject != null) {
                if (netMana instanceof ConditionalMana && !((ConditionalMana) netMana).apply(ability, game, getId(), cost)) {
                    continue;
                }
                specialAction.setUnpaidMana(unpaid);
                if (activateAbility(specialAction, game)) {
                    return true;
                }
                // only one time try to pay
                break;
            }
        }
    }
    return false;
}
Also used : ManaOptions(mage.abilities.mana.ManaOptions) ApprovingObject(mage.ApprovingObject) ConditionalMana(mage.ConditionalMana) Mana(mage.Mana) MageObject(mage.MageObject) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) ConditionalMana(mage.ConditionalMana)

Example 98 with ManaOptions

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

the class ComputerPlayer method findPlayables.

protected void findPlayables(Game game) {
    playableInstant.clear();
    playableNonInstant.clear();
    unplayable.clear();
    playableAbilities.clear();
    Set<Card> nonLands = hand.getCards(new FilterNonlandCard(), game);
    ManaOptions available = getManaAvailable(game);
    for (Card card : nonLands) {
        ManaOptions options = card.getManaCost().getOptions();
        if (!card.getManaCost().getVariableCosts().isEmpty()) {
            // don't use variable mana costs unless there is at least 3 extra mana for X
            for (Mana option : options) {
                option.add(Mana.GenericMana(3));
            }
        }
        for (Mana mana : options) {
            for (Mana avail : available) {
                if (mana.enough(avail)) {
                    SpellAbility ability = card.getSpellAbility();
                    GameEvent castEvent = GameEvent.getEvent(GameEvent.EventType.CAST_SPELL, ability.getId(), ability, playerId);
                    castEvent.setZone(game.getState().getZone(card.getMainCard().getId()));
                    if (ability != null && ability.canActivate(playerId, game).canActivate() && !game.getContinuousEffects().preventedByRuleModification(castEvent, ability, game, true)) {
                        if (card.isInstant(game) || card.hasAbility(FlashAbility.getInstance(), game)) {
                            playableInstant.add(card);
                        } else {
                            playableNonInstant.add(card);
                        }
                    }
                } else if (!playableInstant.contains(card) && !playableNonInstant.contains(card)) {
                    unplayable.put(mana.needed(avail), card);
                }
            }
        }
    }
    // TODO: wtf?! change to player.getPlayable
    for (Permanent permanent : game.getBattlefield().getAllActivePermanents(playerId)) {
        for (ActivatedAbility ability : permanent.getAbilities().getActivatedAbilities(Zone.BATTLEFIELD)) {
            if (!(ability instanceof ActivatedManaAbilityImpl) && ability.canActivate(playerId, game).canActivate()) {
                if (ability instanceof EquipAbility && permanent.getAttachedTo() != null) {
                    continue;
                }
                ManaOptions abilityOptions = ability.getManaCosts().getOptions();
                if (!ability.getManaCosts().getVariableCosts().isEmpty()) {
                    // don't use variable mana costs unless there is at least 3 extra mana for X
                    for (Mana option : abilityOptions) {
                        option.add(Mana.GenericMana(3));
                    }
                }
                if (abilityOptions.isEmpty()) {
                    playableAbilities.add(ability);
                } else {
                    for (Mana mana : abilityOptions) {
                        for (Mana avail : available) {
                            if (mana.enough(avail)) {
                                playableAbilities.add(ability);
                            }
                        }
                    }
                }
            }
        }
    }
    for (Card card : graveyard.getCards(game)) {
        for (ActivatedAbility ability : card.getAbilities(game).getActivatedAbilities(Zone.GRAVEYARD)) {
            if (ability.canActivate(playerId, game).canActivate()) {
                ManaOptions abilityOptions = ability.getManaCosts().getOptions();
                if (abilityOptions.isEmpty()) {
                    playableAbilities.add(ability);
                } else {
                    for (Mana mana : abilityOptions) {
                        for (Mana avail : available) {
                            if (mana.enough(avail)) {
                                playableAbilities.add(ability);
                            }
                        }
                    }
                }
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("findPlayables: " + playableInstant.toString() + "---" + playableNonInstant.toString() + "---" + playableAbilities.toString());
    }
}
Also used : ManaOptions(mage.abilities.mana.ManaOptions) ConditionalMana(mage.ConditionalMana) Mana(mage.Mana) Permanent(mage.game.permanent.Permanent) FilterPermanent(mage.filter.FilterPermanent) GameEvent(mage.game.events.GameEvent) ActivatedManaAbilityImpl(mage.abilities.mana.ActivatedManaAbilityImpl) RateCard(mage.game.draft.RateCard) Card(mage.cards.Card) FilterCard(mage.filter.FilterCard)

Example 99 with ManaOptions

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

the class CharmedPendantManaEffect method produceMana.

@Override
public Mana produceMana(Game game, Ability source) {
    Mana mana = new Mana();
    if (game == null) {
        return mana;
    }
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        for (Cost cost : source.getCosts()) {
            if (cost instanceof MillCardsCost) {
                Set<Card> cards = ((MillCardsCost) cost).getCardsMovedToGraveyard();
                if (!cards.isEmpty()) {
                    Card card = cards.iterator().next();
                    if (card != null && card.getManaCost() != null) {
                        ManaCosts<ManaCost> newManaCosts = new ManaCostsImpl<>();
                        for (ManaCost manaCost : card.getManaCost()) {
                            if (manaCost instanceof ColorlessManaCost || manaCost instanceof GenericManaCost || manaCost instanceof VariableManaCost) {
                                continue;
                            }
                            if (manaCost instanceof MonoHybridManaCost) {
                                newManaCosts.add(new ColoredManaCost(((MonoHybridManaCost) manaCost).getManaColor()));
                            } else {
                                newManaCosts.add(manaCost.copy());
                            }
                        }
                        ManaOptions manaOptions = newManaCosts.getOptions();
                        if (manaOptions.size() == 1) {
                            mana = newManaCosts.getMana();
                        } else {
                            Choice manaChoice = new ChoiceImpl(true);
                            manaChoice.setMessage("Select which mana you like to produce");
                            for (Mana manaOption : manaOptions) {
                                manaChoice.getChoices().add(manaOption.toString());
                            }
                            if (manaChoice.getChoices().isEmpty()) {
                                // no mana choices available
                                return mana;
                            }
                            if (controller.choose(outcome, manaChoice, game)) {
                                for (Mana manaOption : manaOptions) {
                                    if (manaChoice.getChoice().equals(manaOption.toString())) {
                                        mana = manaOption;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return mana;
}
Also used : ManaOptions(mage.abilities.mana.ManaOptions) Player(mage.players.Player) Mana(mage.Mana) MillCardsCost(mage.abilities.costs.common.MillCardsCost) Choice(mage.choices.Choice) MillCardsCost(mage.abilities.costs.common.MillCardsCost) Cost(mage.abilities.costs.Cost) TapSourceCost(mage.abilities.costs.common.TapSourceCost) Card(mage.cards.Card) ChoiceImpl(mage.choices.ChoiceImpl)

Aggregations

ManaOptions (mage.abilities.mana.ManaOptions)99 Test (org.junit.Test)85 ManaOptionsTestUtils.assertManaOptions (org.mage.test.utils.ManaOptionsTestUtils.assertManaOptions)75 ManaOptionsTestUtils.assertDuplicatedManaOptions (org.mage.test.utils.ManaOptionsTestUtils.assertDuplicatedManaOptions)38 Permanent (mage.game.permanent.Permanent)9 Mana (mage.Mana)8 Player (mage.players.Player)5 ActivatedManaAbilityImpl (mage.abilities.mana.ActivatedManaAbilityImpl)4 FilterCard (mage.filter.FilterCard)4 FilterPermanent (mage.filter.FilterPermanent)4 TargetPermanent (mage.target.TargetPermanent)4 AlternateManaPaymentAbility (mage.abilities.costs.mana.AlternateManaPaymentAbility)3 ManaCost (mage.abilities.costs.mana.ManaCost)3 ValueHint (mage.abilities.hint.ValueHint)3 FilterControlledPermanent (mage.filter.common.FilterControlledPermanent)3 UUID (java.util.UUID)2 ConditionalMana (mage.ConditionalMana)2 PassAbility (mage.abilities.common.PassAbility)2 PlayLandAsCommanderAbility (mage.abilities.common.PlayLandAsCommanderAbility)2 WhileSearchingPlayFromLibraryAbility (mage.abilities.common.WhileSearchingPlayFromLibraryAbility)2