Search in sources :

Example 31 with FilterControlledCreaturePermanent

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

the class KathrilAspectWarperEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    if (player == null || game.getBattlefield().countAll(StaticFilters.FILTER_PERMANENT_CREATURE, source.getControllerId(), game) == 0) {
        return false;
    }
    Set<CounterType> counterSet = player.getGraveyard().getCards(StaticFilters.FILTER_CARD_CREATURE, game).stream().map(Card::getAbilities).flatMap(Collection::stream).map(this::checkAbility).collect(Collectors.toSet());
    if (counterSet == null || counterSet.size() == 0) {
        return false;
    }
    int countersAdded = 0;
    for (CounterType counterType : counterSet) {
        if (counterType == null) {
            continue;
        }
        FilterControlledPermanent filter = new FilterControlledCreaturePermanent("creature to give a " + counterType + " counter");
        Target target = new TargetControlledPermanent(filter);
        target.setNotTarget(true);
        if (!player.choose(outcome, target, source.getSourceId(), game)) {
            continue;
        }
        Permanent permanent = game.getPermanent(target.getFirstTarget());
        if (permanent == null) {
            continue;
        }
        if (permanent.addCounters(counterType.createInstance(), source.getControllerId(), source, game)) {
            countersAdded++;
        }
    }
    if (countersAdded == 0) {
        return false;
    }
    Permanent permanent = game.getPermanent(source.getSourceId());
    if (permanent == null) {
        return true;
    }
    permanent.addCounters(CounterType.P1P1.createInstance(countersAdded), source.getControllerId(), source, game);
    return true;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) Target(mage.target.Target) CounterType(mage.counters.CounterType) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) Collection(java.util.Collection) FilterControlledPermanent(mage.filter.common.FilterControlledPermanent)

Example 32 with FilterControlledCreaturePermanent

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

the class MisfortuneEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Player chosenOpponent = game.getPlayer(targetPointer.getFirst(game, source));
    if (controller != null && chosenOpponent != null) {
        if (chosenOpponent.chooseUse(Outcome.Neutral, "If you choose Yes, the controller puts a +1/+1 counter" + "on each creature they control and they gain 4 life. If no, the controller puts a -1/-1 counter" + "on each creature you control and {this} deals 4 damage to you.", source, game)) {
            Effect putP1P1CounterOnEachControlledCreature = new AddCountersAllEffect(CounterType.P1P1.createInstance(), new FilterControlledCreaturePermanent());
            putP1P1CounterOnEachControlledCreature.apply(game, source);
            controller.gainLife(4, game, source);
        } else {
            FilterCreaturePermanent filterOpponentCreatures = new FilterCreaturePermanent();
            filterOpponentCreatures.add(new ControllerIdPredicate(chosenOpponent.getId()));
            Effect putM1M1CounterOnEachOpponentCreature = new AddCountersAllEffect(CounterType.M1M1.createInstance(), filterOpponentCreatures);
            putM1M1CounterOnEachOpponentCreature.apply(game, source);
            chosenOpponent.damage(4, source.getSourceId(), source, game);
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) OneShotEffect(mage.abilities.effects.OneShotEffect) AddCountersAllEffect(mage.abilities.effects.common.counter.AddCountersAllEffect) Effect(mage.abilities.effects.Effect) AddCountersAllEffect(mage.abilities.effects.common.counter.AddCountersAllEffect)

Example 33 with FilterControlledCreaturePermanent

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

the class OrzhovAdvokistEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        List<UUID> players = new ArrayList<>();
        List<UUID> creatures = new ArrayList<>();
        for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
            Player player = game.getPlayer(playerId);
            if (player != null) {
                if (player.chooseUse(outcome, "Put two +1/+1 counters on a creature you control?", source, game)) {
                    Target target = new TargetControlledCreaturePermanent(new FilterControlledCreaturePermanent("a creature you control (to add two +1/+1 counters on it)"));
                    if (player.choose(outcome, target, playerId, game)) {
                        creatures.add(target.getFirstTarget());
                        players.add(player.getId());
                    }
                }
            }
        }
        for (UUID creatureId : creatures) {
            Permanent creature = game.getPermanent(creatureId);
            if (creature != null) {
                creature.addCounters(CounterType.P1P1.createInstance(2), creature.getControllerId(), source, game);
            }
        }
        for (UUID playerId : players) {
            if (!Objects.equals(playerId, source.getControllerId())) {
                FilterCreaturePermanent filter = new FilterCreaturePermanent();
                filter.add(new ControllerIdPredicate(playerId));
                game.addEffect(new CantAttackYouAllEffect(Duration.UntilYourNextTurn, filter, true), source);
            }
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) ArrayList(java.util.ArrayList) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) UUID(java.util.UUID) CantAttackYouAllEffect(mage.abilities.effects.common.combat.CantAttackYouAllEffect) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent)

Example 34 with FilterControlledCreaturePermanent

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

the class ReinsOfPowerEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    UUID opponentId = this.getTargetPointer().getFirst(game, source);
    if (opponentId != null) {
        // Untap all creatures you control and all creatures target opponent controls.
        FilterCreaturePermanent filter = new FilterCreaturePermanent();
        filter.add(Predicates.or(new ControllerIdPredicate(source.getControllerId()), new ControllerIdPredicate(opponentId)));
        new UntapAllEffect(filter).apply(game, source);
        // You and that opponent each gain control of all creatures the other controls until end of turn.
        Set<UUID> yourCreatures = new HashSet<>();
        Set<UUID> opponentCreatures = new HashSet<>();
        for (Permanent permanent : game.getBattlefield().getActivePermanents(new FilterControlledCreaturePermanent(), source.getControllerId(), source.getSourceId(), game)) {
            yourCreatures.add(permanent.getId());
        }
        FilterCreaturePermanent filterOpponent = new FilterCreaturePermanent();
        filterOpponent.add(new ControllerIdPredicate(opponentId));
        for (Permanent permanent : game.getBattlefield().getActivePermanents(filterOpponent, source.getControllerId(), source.getSourceId(), game)) {
            opponentCreatures.add(permanent.getId());
        }
        for (UUID creatureId : yourCreatures) {
            ContinuousEffect effect = new GainControlTargetEffect(Duration.EndOfTurn, opponentId);
            effect.setTargetPointer(new FixedTarget(creatureId, game));
            game.addEffect(effect, source);
        }
        for (UUID creatureId : opponentCreatures) {
            ContinuousEffect effect = new GainControlTargetEffect(Duration.EndOfTurn);
            effect.setTargetPointer(new FixedTarget(creatureId, game));
            game.addEffect(effect, source);
        }
        // Those creatures gain haste until end of turn.
        game.addEffect(new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filter), source);
        return true;
    }
    return false;
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) ContinuousEffect(mage.abilities.effects.ContinuousEffect) UUID(java.util.UUID) UntapAllEffect(mage.abilities.effects.common.UntapAllEffect) GainAbilityAllEffect(mage.abilities.effects.common.continuous.GainAbilityAllEffect) GainControlTargetEffect(mage.abilities.effects.common.continuous.GainControlTargetEffect) HashSet(java.util.HashSet)

Example 35 with FilterControlledCreaturePermanent

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

the class Combat method handleBanding.

private void handleBanding(UUID creatureId, Game game) {
    Player player = game.getPlayer(attackingPlayerId);
    Permanent attacker = game.getPermanent(creatureId);
    if (attacker == null || player == null) {
        return;
    }
    CombatGroup combatGroup = findGroup(attacker.getId());
    if (combatGroup == null || !attacker.getBandedCards().isEmpty() || getAttackers().size() <= 1) {
        return;
    }
    boolean canBand = attacker.hasAbility(BandingAbility.getInstance(), game);
    List<Ability> bandsWithOther = new ArrayList<>();
    for (Ability ability : attacker.getAbilities()) {
        if (ability.getClass().equals(BandsWithOtherAbility.class)) {
            bandsWithOther.add(ability);
        }
    }
    boolean canBandWithOther = !bandsWithOther.isEmpty();
    if (!canBand && !canBandWithOther) {
        return;
    }
    boolean isBanded = false;
    FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("attacking creature to band with " + attacker.getLogName());
    filter.add(Predicates.not(new PermanentIdPredicate(creatureId)));
    // creature that isn't already banded, and is attacking the same player or planeswalker
    filter.add(new AttackingSameNotBandedPredicate(combatGroup.getDefenderId()));
    List<Predicate<MageObject>> predicates = new ArrayList<>();
    if (!canBand && canBandWithOther) {
        for (Ability ab : bandsWithOther) {
            BandsWithOtherAbility ability = (BandsWithOtherAbility) ab;
            if (ability.getSubtype() != null) {
                predicates.add(ability.getSubtype().getPredicate());
            }
            if (ability.getSupertype() != null) {
                predicates.add(ability.getSupertype().getPredicate());
            }
            if (ability.getName() != null) {
                predicates.add(new NamePredicate(ability.getName()));
            }
        }
        filter.add(Predicates.or(predicates));
    }
    while (player.canRespond()) {
        TargetControlledPermanent target = new TargetControlledPermanent(1, 1, filter, true);
        target.setRequired(false);
        canBand &= target.canChoose(attackingPlayerId, game);
        canBandWithOther &= target.canChoose(attackingPlayerId, game);
        if (game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, attackingPlayerId, attackingPlayerId)) || (!canBand && !canBandWithOther) || !player.chooseUse(Outcome.Benefit, (isBanded ? "Band " + attacker.getLogName() + " with another " : "Form a band with " + attacker.getLogName() + " and an ") + "attacking creature?", null, game)) {
            break;
        }
        if (canBand && canBandWithOther) {
            if (player.chooseUse(Outcome.Detriment, "Choose type of banding ability to apply:", attacker.getLogName(), "Banding", "Bands with other", null, game)) {
                canBandWithOther = false;
            } else {
                canBand = false;
                for (Ability ab : bandsWithOther) {
                    BandsWithOtherAbility ability = (BandsWithOtherAbility) ab;
                    if (ability.getSubtype() != null) {
                        predicates.add(ability.getSubtype().getPredicate());
                    }
                    if (ability.getSupertype() != null) {
                        predicates.add(ability.getSupertype().getPredicate());
                    }
                    if (ability.getName() != null) {
                        predicates.add(new NamePredicate(ability.getName()));
                    }
                }
                filter.add(Predicates.or(predicates));
            }
        }
        if (target.choose(Outcome.Benefit, attackingPlayerId, null, game)) {
            isBanded = true;
            for (UUID targetId : target.getTargets()) {
                Permanent permanent = game.getPermanent(targetId);
                if (permanent != null) {
                    for (UUID bandedId : attacker.getBandedCards()) {
                        permanent.addBandedCard(bandedId);
                        Permanent banded = game.getPermanent(bandedId);
                        if (banded != null) {
                            banded.addBandedCard(targetId);
                        }
                    }
                    permanent.addBandedCard(creatureId);
                    attacker.addBandedCard(targetId);
                    if (canBand) {
                        if (!permanent.hasAbility(BandingAbility.getInstance(), game)) {
                            filter.add(new AbilityPredicate(BandingAbility.class));
                            canBandWithOther = false;
                        }
                    } else if (canBandWithOther) {
                        List<Predicate<MageObject>> newPredicates = new ArrayList<>();
                        for (Predicate<MageObject> predicate : predicates) {
                            if (predicate.apply(permanent, game)) {
                                newPredicates.add(predicate);
                            }
                        }
                        filter.add(Predicates.or(newPredicates));
                        canBand = false;
                    }
                }
            }
        }
    }
    if (!isBanded) {
        return;
    }
    StringBuilder sb = new StringBuilder(player.getLogName()).append(" formed a band with ").append(attacker.getBandedCards().size()).append(1).append(" creatures: ");
    sb.append(attacker.getLogName());
    for (UUID id : attacker.getBandedCards()) {
        sb.append(", ");
        Permanent permanent = game.getPermanent(id);
        if (permanent != null) {
            sb.append(permanent.getLogName());
        }
    }
    game.informPlayers(sb.toString());
}
Also used : JohanVigilanceAbility(mage.abilities.keyword.special.JohanVigilanceAbility) VigilanceAbility(mage.abilities.keyword.VigilanceAbility) BandingAbility(mage.abilities.keyword.BandingAbility) BandsWithOtherAbility(mage.abilities.keyword.BandsWithOtherAbility) Ability(mage.abilities.Ability) PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) Player(mage.players.Player) AttackingSameNotBandedPredicate(mage.filter.predicate.permanent.AttackingSameNotBandedPredicate) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) MageObject(mage.MageObject) PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) Predicate(mage.filter.predicate.Predicate) AttackingSameNotBandedPredicate(mage.filter.predicate.permanent.AttackingSameNotBandedPredicate) NamePredicate(mage.filter.predicate.mageobject.NamePredicate) AbilityPredicate(mage.filter.predicate.mageobject.AbilityPredicate) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) BandingAbility(mage.abilities.keyword.BandingAbility) BandsWithOtherAbility(mage.abilities.keyword.BandsWithOtherAbility) PlayerList(mage.players.PlayerList) AbilityPredicate(mage.filter.predicate.mageobject.AbilityPredicate)

Aggregations

FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)75 Permanent (mage.game.permanent.Permanent)62 Player (mage.players.Player)60 UUID (java.util.UUID)29 Target (mage.target.Target)27 TargetControlledCreaturePermanent (mage.target.common.TargetControlledCreaturePermanent)21 TargetControlledPermanent (mage.target.common.TargetControlledPermanent)16 ArrayList (java.util.ArrayList)13 Card (mage.cards.Card)11 TargetPermanent (mage.target.TargetPermanent)11 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)10 FixedTarget (mage.target.targetpointer.FixedTarget)9 MageObject (mage.MageObject)7 OneShotEffect (mage.abilities.effects.OneShotEffect)7 Effect (mage.abilities.effects.Effect)6 Ability (mage.abilities.Ability)5 PermanentsOnBattlefieldCount (mage.abilities.dynamicvalue.common.PermanentsOnBattlefieldCount)5 FilterPermanent (mage.filter.FilterPermanent)5 ControllerIdPredicate (mage.filter.predicate.permanent.ControllerIdPredicate)5 Spell (mage.game.stack.Spell)5