use of mage.filter.predicate.permanent.PermanentIdPredicate in project mage by magefree.
the class DispersalEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if (player == null) {
return false;
}
Set<PermanentIdPredicate> permsToReturn = new HashSet<>();
for (UUID opponentId : game.getOpponents(player.getId())) {
Player opponent = game.getPlayer(opponentId);
if (opponent == null) {
continue;
}
int highestCMC = 0;
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(opponentId)) {
if (permanent != null) {
highestCMC = Math.max(highestCMC, permanent.getManaValue());
}
}
FilterPermanent filter = new FilterNonlandPermanent("permanent you control with mana value " + highestCMC);
filter.add(new ManaValuePredicate(ComparisonType.EQUAL_TO, highestCMC));
filter.add(new ControllerIdPredicate(opponentId));
Target target = new TargetPermanent(1, 1, filter, true);
if (opponent.choose(outcome, target, source.getSourceId(), game)) {
if (target.getFirstTarget() == null) {
continue;
}
permsToReturn.add(new PermanentIdPredicate(target.getFirstTarget()));
}
}
FilterPermanent filter = new FilterPermanent();
filter.add(Predicates.or(permsToReturn));
new ReturnToHandFromBattlefieldAllEffect(filter).apply(game, source);
new DiscardEachPlayerEffect(StaticValue.get(1), false, TargetController.OPPONENT).apply(game, source);
return true;
}
use of mage.filter.predicate.permanent.PermanentIdPredicate in project mage by magefree.
the class MutinyFirstTarget method addTarget.
@Override
public void addTarget(UUID id, Ability source, Game game, boolean skipEvent) {
super.addTarget(id, source, game, skipEvent);
// Update the second target
UUID firstController = game.getControllerId(id);
if (firstController != null && source.getTargets().size() > 1) {
Player controllingPlayer = game.getPlayer(firstController);
TargetCreaturePermanent targetCreaturePermanent = (TargetCreaturePermanent) source.getTargets().get(1);
// Set a new filter to the second target with the needed restrictions
FilterCreaturePermanent filter = new FilterCreaturePermanent("another creature that player " + controllingPlayer.getName() + " controls");
filter.add(new ControllerIdPredicate(firstController));
filter.add(Predicates.not(new PermanentIdPredicate(id)));
targetCreaturePermanent.replaceFilter(filter);
}
}
use of mage.filter.predicate.permanent.PermanentIdPredicate 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.permanent.PermanentIdPredicate in project mage by magefree.
the class AlphaBrawlEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
UUID targetId = source.getFirstTarget();
FilterCreaturePermanent filter = new FilterCreaturePermanent("each other creature that player controls");
filter.add(Predicates.not(new PermanentIdPredicate(targetId)));
Permanent creature = game.getPermanent(targetId);
if (creature != null) {
Player player = game.getPlayer(creature.getControllerId());
if (player != null) {
int power = creature.getPower().getValue();
for (Permanent perm : game.getBattlefield().getAllActivePermanents(filter, player.getId(), game)) {
perm.damage(power, creature.getId(), source, game, false, true);
}
for (Permanent perm : game.getBattlefield().getAllActivePermanents(filter, player.getId(), game)) {
creature.damage(perm.getPower().getValue(), perm.getId(), source, game, false, true);
}
return true;
}
}
return false;
}
use of mage.filter.predicate.permanent.PermanentIdPredicate in project mage by magefree.
the class TreefolkUmbraEffect method apply.
@Override
public boolean apply(Layer layer, SubLayer sublayer, Ability source, Game game) {
Permanent permanent = game.getPermanent(source.getSourceId());
if (permanent == null || permanent.getAttachedTo() == null) {
return false;
}
FilterCreaturePermanent filter = new FilterCreaturePermanent();
filter.add(new PermanentIdPredicate(permanent.getAttachedTo()));
game.getCombat().setUseToughnessForDamage(true);
game.getCombat().addUseToughnessForDamageFilter(filter);
return true;
}
Aggregations