use of mage.filter.predicate.mageobject.PowerPredicate in project mage by magefree.
the class CracklingDoomEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
List<Permanent> toSacrifice = new ArrayList<>();
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
if (controller.hasOpponent(playerId, game)) {
Player opponent = game.getPlayer(playerId);
if (opponent != null) {
int greatestPower = Integer.MIN_VALUE;
int numberOfCreatures = 0;
Permanent permanentToSacrifice = null;
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, playerId, game)) {
if (permanent.getPower().getValue() > greatestPower) {
greatestPower = permanent.getPower().getValue();
numberOfCreatures = 1;
permanentToSacrifice = permanent;
} else if (permanent.getPower().getValue() == greatestPower) {
numberOfCreatures++;
}
}
if (numberOfCreatures == 1) {
if (permanentToSacrifice != null) {
toSacrifice.add(permanentToSacrifice);
}
} else if (greatestPower != Integer.MIN_VALUE) {
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)) {
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent != null) {
toSacrifice.add(permanent);
}
}
}
}
}
}
for (Permanent permanent : toSacrifice) {
permanent.sacrifice(source, game);
}
return true;
}
return false;
}
use of mage.filter.predicate.mageobject.PowerPredicate in project mage by magefree.
the class MayaelsAriaEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
// put a +1/+1 counter on each creature you control if you control a creature with power 5 or greater.
FilterCreaturePermanent filter = new FilterCreaturePermanent();
filter.add(new PowerPredicate(ComparisonType.MORE_THAN, 4));
if (game.getState().getBattlefield().countAll(filter, controller.getId(), game) > 0) {
for (Permanent creature : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, source.getControllerId(), game)) {
creature.addCounters(CounterType.P1P1.createInstance(), source.getControllerId(), source, game);
}
}
// needed because otehrwise the +1/+1 counters wouldn't be taken into account
game.getState().processAction(game);
// Then you gain 10 life if you control a creature with power 10 or greater.
filter = new FilterCreaturePermanent();
filter.add(new PowerPredicate(ComparisonType.MORE_THAN, 9));
if (game.getState().getBattlefield().countAll(filter, controller.getId(), game) > 0) {
controller.gainLife(10, game, source);
}
// Then you win the game if you control a creature with power 20 or greater.
filter = new FilterCreaturePermanent();
filter.add(new PowerPredicate(ComparisonType.MORE_THAN, 19));
if (game.getState().getBattlefield().countAll(filter, controller.getId(), game) > 0) {
controller.won(game);
}
return true;
}
use of mage.filter.predicate.mageobject.PowerPredicate in project mage by magefree.
the class SlaughterTheStrongEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
boolean selectionDone = false;
Set<UUID> selectedCreatures = new HashSet<>();
while (player.canRespond() && selectionDone == false) {
int powerSum = 0;
for (UUID creatureId : selectedCreatures) {
Permanent creature = game.getPermanent(creatureId);
if (creature != null) {
powerSum += creature.getPower().getValue();
}
}
FilterControlledCreaturePermanent currentFilter = new FilterControlledCreaturePermanent("any number of creatures you control with total power 4 or less (already selected total power " + powerSum + ")");
Set<PermanentIdPredicate> alreadySelectedCreatures = new HashSet<>();
if (!selectedCreatures.isEmpty()) {
for (UUID creatureId : selectedCreatures) {
alreadySelectedCreatures.add(new PermanentIdPredicate(creatureId));
}
currentFilter.add(Predicates.or(new PowerPredicate(ComparisonType.FEWER_THAN, 5 - powerSum), Predicates.or(alreadySelectedCreatures)));
} else {
currentFilter.add(new PowerPredicate(ComparisonType.FEWER_THAN, 5 - powerSum));
}
// human can de-select targets, but AI must choose only one time
Target target;
if (player.isComputer()) {
// AI settings
FilterControlledCreaturePermanent strictFilter = currentFilter.copy();
selectedCreatures.stream().forEach(id -> {
strictFilter.add(Predicates.not(new PermanentIdPredicate(id)));
});
target = new TargetPermanent(0, 1, strictFilter, true);
} else {
// Human settings
target = new TargetPermanent(0, 1, currentFilter, true);
}
player.chooseTarget(Outcome.BoostCreature, target, source, game);
if (target.getFirstTarget() != null) {
if (selectedCreatures.contains(target.getFirstTarget())) {
selectedCreatures.remove(target.getFirstTarget());
} else {
selectedCreatures.add(target.getFirstTarget());
}
} else {
if (player.isComputer()) {
// AI stops
selectionDone = true;
} else {
// Human can continue
String selected = "Selected: ";
for (UUID creatureId : selectedCreatures) {
Permanent creature = game.getPermanent(creatureId);
if (creature != null) {
selected += creature.getLogName() + " ";
}
}
selectionDone = player.chooseUse(Outcome.Detriment, "Creature selection", selected, "End the selection", "Continue the selection", source, game);
}
}
}
for (Permanent creature : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, playerId, game)) {
if (!selectedCreatures.contains(creature.getId())) {
creature.sacrifice(source, game);
}
}
}
}
return true;
}
return false;
}
use of mage.filter.predicate.mageobject.PowerPredicate in project mage by magefree.
the class ValiantEndeavorEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
List<Integer> results = controller.rollDice(outcome, source, game, 6, 2, 0);
int firstResult = results.get(0);
int secondResult = results.get(1);
int first, second;
if (firstResult != secondResult && controller.chooseUse(outcome, "Destroy each creature with power greater than or equal to your choice", "The other number will be the amount of 2/2 white Knight tokens with vigilance.", "" + firstResult, "" + secondResult, source, game)) {
first = firstResult;
second = secondResult;
} else {
first = secondResult;
second = firstResult;
}
final FilterCreaturePermanent filter = new FilterCreaturePermanent(String.format("creatures with power greater than or equal to %s", first));
filter.add(new PowerPredicate(ComparisonType.MORE_THAN, first - 1));
Effect wrathEffect = new DestroyAllEffect(filter);
wrathEffect.apply(game, source);
new KnightToken().putOntoBattlefield(second, game, source, source.getControllerId());
return true;
}
use of mage.filter.predicate.mageobject.PowerPredicate in project mage by magefree.
the class CelestialJudgmentEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if (player == null) {
return false;
}
List<Permanent> permanents = game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, source.getControllerId(), source.getSourceId(), game);
Map<Integer, List<Permanent>> powerMap = permanents.stream().collect(Collectors.toMap(permanent -> permanent.getPower().getValue(), permanent -> Arrays.asList(permanent), (a1, a2) -> {
a1.addAll(a2);
return a1;
}));
Set<UUID> toKeep = new HashSet<>();
for (Map.Entry<Integer, List<Permanent>> entry : powerMap.entrySet()) {
if (entry.getValue().size() == 1) {
toKeep.add(entry.getValue().get(0).getId());
continue;
}
FilterPermanent filter = new FilterCreaturePermanent("creature with power " + entry.getKey() + " to save");
filter.add(new PowerPredicate(ComparisonType.EQUAL_TO, entry.getKey()));
TargetPermanent target = new TargetPermanent(filter);
target.setNotTarget(true);
player.choose(outcome, target, source.getSourceId(), game);
toKeep.add(target.getFirstTarget());
}
for (Permanent permanent : permanents) {
if (!toKeep.contains(permanent.getId())) {
permanent.destroy(source, game);
}
}
return true;
}
Aggregations