Search in sources :

Example 21 with FilterLandPermanent

use of mage.filter.common.FilterLandPermanent 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 22 with FilterLandPermanent

use of mage.filter.common.FilterLandPermanent in project mage by magefree.

the class PlanarOverlayEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Set<Card> lands = new HashSet<>();
        for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                for (SubType landName : SubType.getBasicLands()) {
                    FilterLandPermanent filter = new FilterLandPermanent(landName + " to return to hand");
                    filter.add(landName.getPredicate());
                    filter.add(TargetController.YOU.getControllerPredicate());
                    Target target = new TargetLandPermanent(1, 1, filter, true);
                    if (target.canChoose(source.getSourceId(), player.getId(), game)) {
                        player.chooseTarget(outcome, target, source, game);
                        lands.add(game.getPermanent(target.getFirstTarget()));
                    }
                }
            }
        }
        controller.moveCards(lands, Zone.HAND, source, game);
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) FilterLandPermanent(mage.filter.common.FilterLandPermanent) TargetLandPermanent(mage.target.common.TargetLandPermanent) UUID(java.util.UUID) Card(mage.cards.Card) HashSet(java.util.HashSet)

Example 23 with FilterLandPermanent

use of mage.filter.common.FilterLandPermanent in project mage by magefree.

the class SkyshroudWarBeastEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        MageObject target = game.getObject(source.getSourceId());
        if (target != null) {
            UUID playerId = (UUID) game.getState().getValue(source.getSourceId().toString() + ChooseOpponentEffect.VALUE_KEY);
            FilterLandPermanent filter = FilterLandPermanent.nonbasicLand();
            filter.add(new ControllerIdPredicate(playerId));
            int number = new PermanentsOnBattlefieldCount(filter).calculate(game, source, this);
            target.getPower().setValue(number);
            target.getToughness().setValue(number);
            return true;
        }
    }
    return false;
}
Also used : Player(mage.players.Player) FilterLandPermanent(mage.filter.common.FilterLandPermanent) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) MageObject(mage.MageObject) PermanentsOnBattlefieldCount(mage.abilities.dynamicvalue.common.PermanentsOnBattlefieldCount) UUID(java.util.UUID)

Example 24 with FilterLandPermanent

use of mage.filter.common.FilterLandPermanent in project mage by magefree.

the class TitheEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    int numYourLands = game.getBattlefield().countAll(new FilterLandPermanent(), source.getControllerId(), game);
    int numOpponentLands = game.getBattlefield().countAll(new FilterLandPermanent(), this.getTargetPointer().getFirst(game, source), game);
    new SearchLibraryPutInHandEffect(new TargetCardInLibrary(0, (numOpponentLands > numYourLands ? 2 : 1), filter), true).apply(game, source);
    return true;
}
Also used : FilterLandPermanent(mage.filter.common.FilterLandPermanent) SearchLibraryPutInHandEffect(mage.abilities.effects.common.search.SearchLibraryPutInHandEffect) TargetCardInLibrary(mage.target.common.TargetCardInLibrary)

Example 25 with FilterLandPermanent

use of mage.filter.common.FilterLandPermanent in project mage by magefree.

the class ManaVaporsEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player targetPlayer = game.getPlayer(getTargetPointer().getFirst(game, source));
    if (targetPlayer != null) {
        FilterLandPermanent filter = new FilterLandPermanent();
        filter.add(new ControllerIdPredicate(targetPlayer.getId()));
        ContinuousEffect effect = new DontUntapInPlayersNextUntapStepAllEffect(filter);
        effect.setTargetPointer(new FixedTarget(targetPlayer.getId()));
        game.addEffect(effect, source);
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) FilterLandPermanent(mage.filter.common.FilterLandPermanent) DontUntapInPlayersNextUntapStepAllEffect(mage.abilities.effects.common.DontUntapInPlayersNextUntapStepAllEffect) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) ContinuousEffect(mage.abilities.effects.ContinuousEffect)

Aggregations

FilterLandPermanent (mage.filter.common.FilterLandPermanent)32 Player (mage.players.Player)22 Permanent (mage.game.permanent.Permanent)18 ControllerIdPredicate (mage.filter.predicate.permanent.ControllerIdPredicate)13 UUID (java.util.UUID)12 TargetLandPermanent (mage.target.common.TargetLandPermanent)10 Target (mage.target.Target)8 FilterPermanent (mage.filter.FilterPermanent)6 TargetCardInLibrary (mage.target.common.TargetCardInLibrary)5 ArrayList (java.util.ArrayList)4 TargetPermanent (mage.target.TargetPermanent)4 TargetPlayer (mage.target.TargetPlayer)4 MageObject (mage.MageObject)3 CardsImpl (mage.cards.CardsImpl)3 FilterControlledLandPermanent (mage.filter.common.FilterControlledLandPermanent)3 PermanentIdPredicate (mage.filter.predicate.permanent.PermanentIdPredicate)3 HashSet (java.util.HashSet)2 MageObjectReference (mage.MageObjectReference)2 Mana (mage.Mana)2 Ability (mage.abilities.Ability)2