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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations