Search in sources :

Example 46 with TargetControlledCreaturePermanent

use of mage.target.common.TargetControlledCreaturePermanent in project mage by magefree.

the class ShadowgrangeArchfiendEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller == null) {
        return false;
    }
    List<Permanent> toSacrifice = new ArrayList<>();
    // Iterate through each opponent
    for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
        if (!controller.hasOpponent(playerId, game)) {
            continue;
        }
        Player opponent = game.getPlayer(playerId);
        if (opponent == null) {
            continue;
        }
        int greatestPower = Integer.MIN_VALUE;
        int numberOfCreatures = 0;
        Permanent creatureToSacrifice = null;
        // Iterature through each creature
        for (Permanent permanent : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, playerId, game)) {
            if (permanent.getPower().getValue() > greatestPower) {
                greatestPower = permanent.getPower().getValue();
                numberOfCreatures = 1;
                creatureToSacrifice = permanent;
            } else if (permanent.getPower().getValue() == greatestPower) {
                numberOfCreatures++;
            }
        }
        // If multiple creatures are tied for having the greatest power
        if (numberOfCreatures > 1) {
            FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("creature to sacrifice with power equal to " + greatestPower);
            filter.add(new PowerPredicate(ComparisonType.EQUAL_TO, greatestPower));
            Target target = new TargetControlledCreaturePermanent(filter);
            if (opponent.choose(outcome, target, playerId, game)) {
                creatureToSacrifice = game.getPermanent(target.getFirstTarget());
            }
        }
        if (creatureToSacrifice != null) {
            toSacrifice.add(creatureToSacrifice);
        }
    }
    int greatestPowerAmongAllCreaturesSacked = Integer.MIN_VALUE;
    int powerOfCurrentCreature;
    // Sack the creatures and save the greaterest power amoung those which were sacked
    for (Permanent permanent : toSacrifice) {
        powerOfCurrentCreature = permanent.getPower().getValue();
        // Try to sack it
        if (permanent.sacrifice(source, game)) {
            if (powerOfCurrentCreature > greatestPowerAmongAllCreaturesSacked) {
                greatestPowerAmongAllCreaturesSacked = powerOfCurrentCreature;
            }
        }
    }
    // Gain life equal to the power of greatest creature sacked, if it is positive
    if (greatestPowerAmongAllCreaturesSacked > 0) {
        new GainLifeEffect(greatestPowerAmongAllCreaturesSacked).apply(game, source);
    }
    return true;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) ArrayList(java.util.ArrayList) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) PowerPredicate(mage.filter.predicate.mageobject.PowerPredicate) UUID(java.util.UUID) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) GainLifeEffect(mage.abilities.effects.common.GainLifeEffect)

Example 47 with TargetControlledCreaturePermanent

use of mage.target.common.TargetControlledCreaturePermanent in project mage by magefree.

the class TourachsChantEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(targetPointer.getFirst(game, source));
    Permanent sourcePermanent = game.getPermanentOrLKIBattlefield(source.getSourceId());
    if (player != null && sourcePermanent != null) {
        boolean paid = false;
        TargetControlledCreaturePermanent target = new TargetControlledCreaturePermanent();
        target.setNotTarget(true);
        if (player.chooseUse(Outcome.Detriment, "Put a -1/-1 counter on a creature you control? (otherwise " + sourcePermanent.getLogName() + " deals 3 damage to you)", source, game) && player.choose(Outcome.UnboostCreature, target, source.getSourceId(), game)) {
            Permanent permanent = game.getPermanent(target.getFirstTarget());
            if (permanent != null) {
                permanent.addCounters(CounterType.M1M1.createInstance(), player.getId(), source, game);
                paid = true;
            }
        }
        if (!paid) {
            player.damage(3, source.getSourceId(), source, game);
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent)

Example 48 with TargetControlledCreaturePermanent

use of mage.target.common.TargetControlledCreaturePermanent in project mage by magefree.

the class CrewCost method pay.

@Override
public boolean pay(Ability ability, Game game, Ability source, UUID controllerId, boolean noMana, Cost costToPay) {
    if (handleAltCost(ability, game, source, controllerId, noMana, costToPay)) {
        paid = true;
        return true;
    }
    Target target = new TargetControlledCreaturePermanent(0, Integer.MAX_VALUE, filter, true) {

        @Override
        public String getMessage() {
            // shows selected power
            int selectedPower = this.targets.entrySet().stream().map(entry -> (game.getPermanent(entry.getKey()))).filter(Objects::nonNull).mapToInt(p -> (getCrewPower(p, game))).sum();
            String extraInfo = "(selected power " + selectedPower + " of " + value + ")";
            if (selectedPower >= value) {
                extraInfo = HintUtils.prepareText(extraInfo, Color.GREEN);
            }
            return super.getMessage() + " " + extraInfo;
        }
    };
    // can cancel
    if (target.choose(Outcome.Tap, controllerId, source.getSourceId(), game)) {
        int sumPower = 0;
        for (UUID targetId : target.getTargets()) {
            GameEvent event = new GameEvent(GameEvent.EventType.CREW_VEHICLE, targetId, source, controllerId);
            if (!game.replaceEvent(event)) {
                Permanent permanent = game.getPermanent(targetId);
                if (permanent != null && permanent.tap(source, game)) {
                    sumPower += getCrewPower(permanent, game);
                }
            }
        }
        paid = sumPower >= value;
        if (paid) {
            for (UUID targetId : target.getTargets()) {
                game.fireEvent(GameEvent.getEvent(GameEvent.EventType.CREWED_VEHICLE, targetId, source, controllerId));
            }
        }
    } else {
        return false;
    }
    return paid;
}
Also used : Target(mage.target.Target) SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) CrewAbilityIcon(mage.abilities.icon.abilities.CrewAbilityIcon) Zone(mage.constants.Zone) TappedPredicate(mage.filter.predicate.permanent.TappedPredicate) AddCardTypeSourceEffect(mage.abilities.effects.common.continuous.AddCardTypeSourceEffect) Player(mage.players.Player) CrewIncreasedPowerAbility(mage.abilities.common.CrewIncreasedPowerAbility) Cost(mage.abilities.costs.Cost) CardType(mage.constants.CardType) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) CrewWithToughnessAbility(mage.abilities.common.CrewWithToughnessAbility) CostImpl(mage.abilities.costs.CostImpl) SimpleActivatedAbility(mage.abilities.common.SimpleActivatedAbility) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) CardUtil(mage.util.CardUtil) UUID(java.util.UUID) java.awt(java.awt) Objects(java.util.Objects) Duration(mage.constants.Duration) Game(mage.game.Game) GameEvent(mage.game.events.GameEvent) InfoEffect(mage.abilities.effects.common.InfoEffect) Permanent(mage.game.permanent.Permanent) HintUtils(mage.abilities.hint.HintUtils) AnotherPredicate(mage.filter.predicate.mageobject.AnotherPredicate) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) Ability(mage.abilities.Ability) Target(mage.target.Target) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) Permanent(mage.game.permanent.Permanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) Objects(java.util.Objects) GameEvent(mage.game.events.GameEvent) UUID(java.util.UUID) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent)

Example 49 with TargetControlledCreaturePermanent

use of mage.target.common.TargetControlledCreaturePermanent in project mage by magefree.

the class InvokeDespairEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player opponent = game.getPlayer(source.getFirstTarget());
    Player controller = game.getPlayer(source.getControllerId());
    if (opponent == null || controller == null) {
        return false;
    }
    Target target = new TargetControlledCreaturePermanent();
    target.setNotTarget(true);
    opponent.choose(outcome, target, source.getSourceId(), game);
    Permanent permanent = game.getPermanent(target.getFirstTarget());
    boolean sacrificed = false;
    if (permanent != null) {
        sacrificed = permanent.sacrifice(source, game);
    }
    if (!sacrificed) {
        opponent.loseLife(2, game, source, false);
        controller.drawCards(1, source, game);
    }
    target = new TargetControlledPermanent(enchantmentFilter);
    target.setNotTarget(true);
    opponent.choose(outcome, target, source.getSourceId(), game);
    permanent = game.getPermanent(target.getFirstTarget());
    sacrificed = false;
    if (permanent != null) {
        sacrificed = permanent.sacrifice(source, game);
    }
    if (!sacrificed) {
        opponent.loseLife(2, game, source, false);
        controller.drawCards(1, source, game);
    }
    target = new TargetControlledPermanent(StaticFilters.FILTER_CONTROLLED_PERMANENT_PLANESWALKER);
    target.setNotTarget(true);
    opponent.choose(outcome, target, source.getSourceId(), game);
    permanent = game.getPermanent(target.getFirstTarget());
    sacrificed = false;
    if (permanent != null) {
        sacrificed = permanent.sacrifice(source, game);
    }
    if (!sacrificed) {
        opponent.loseLife(2, game, source, false);
        controller.drawCards(1, source, game);
    }
    return true;
}
Also used : TargetControlledPermanent(mage.target.common.TargetControlledPermanent) Player(mage.players.Player) Target(mage.target.Target) FilterControlledEnchantmentPermanent(mage.filter.common.FilterControlledEnchantmentPermanent) Permanent(mage.game.permanent.Permanent) TargetControlledPermanent(mage.target.common.TargetControlledPermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent)

Example 50 with TargetControlledCreaturePermanent

use of mage.target.common.TargetControlledCreaturePermanent in project mage by magefree.

the class PlungeIntoDarknessSearchEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    if (player != null) {
        Target target = new TargetControlledCreaturePermanent(0, Integer.MAX_VALUE, new FilterControlledCreaturePermanent(), true);
        player.chooseTarget(Outcome.Sacrifice, target, source, game);
        int numSacrificed = 0;
        for (UUID permanentId : target.getTargets()) {
            Permanent permanent = game.getPermanent(permanentId);
            if (permanent != null) {
                if (permanent.sacrifice(source, game)) {
                    numSacrificed++;
                }
            }
        }
        if (numSacrificed > 0) {
            player.gainLife(3 * numSacrificed, game, source);
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) UUID(java.util.UUID) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent)

Aggregations

TargetControlledCreaturePermanent (mage.target.common.TargetControlledCreaturePermanent)78 Player (mage.players.Player)73 Permanent (mage.game.permanent.Permanent)64 Target (mage.target.Target)41 UUID (java.util.UUID)29 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)27 TargetPermanent (mage.target.TargetPermanent)18 ArrayList (java.util.ArrayList)14 Cost (mage.abilities.costs.Cost)10 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)9 SacrificeTargetCost (mage.abilities.costs.common.SacrificeTargetCost)8 FixedTarget (mage.target.targetpointer.FixedTarget)8 Ability (mage.abilities.Ability)6 FilterPermanent (mage.filter.FilterPermanent)6 MageObject (mage.MageObject)5 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)5 OneShotEffect (mage.abilities.effects.OneShotEffect)4 Card (mage.cards.Card)4 SimpleActivatedAbility (mage.abilities.common.SimpleActivatedAbility)3 SimpleStaticAbility (mage.abilities.common.SimpleStaticAbility)3