use of mage.filter.common.FilterControlledPermanent in project mage by magefree.
the class BalancingActEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
int minPermanent = Integer.MAX_VALUE, minCard = Integer.MAX_VALUE;
// count minimal permanents
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
int count = game.getBattlefield().getActivePermanents(new FilterControlledPermanent(), player.getId(), source.getSourceId(), game).size();
if (count < minPermanent) {
minPermanent = count;
}
}
}
// sacrifice permanents over the minimum
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
TargetControlledPermanent target = new TargetControlledPermanent(minPermanent, minPermanent, new FilterControlledPermanent(), true);
if (target.choose(Outcome.Benefit, player.getId(), source.getSourceId(), game)) {
for (Permanent permanent : game.getBattlefield().getActivePermanents(new FilterControlledPermanent(), player.getId(), source.getSourceId(), game)) {
if (permanent != null && !target.getTargets().contains(permanent.getId())) {
permanent.sacrifice(source, game);
}
}
}
}
}
// count minimal cards in hand
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
int count = player.getHand().size();
if (count < minCard) {
minCard = count;
}
}
}
// discard cards over the minimum
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
TargetCardInHand target = new TargetCardInHand(minCard, new FilterCard());
if (target.choose(Outcome.Benefit, player.getId(), source.getSourceId(), game)) {
Cards cards = player.getHand().copy();
cards.removeIf(target.getTargets()::contains);
player.discard(cards, false, source, game);
}
}
}
return true;
}
return false;
}
use of mage.filter.common.FilterControlledPermanent in project mage by magefree.
the class LuminescentRainEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
Choice typeChoice = new ChoiceCreatureType(game.getObject(source.getSourceId()));
if (player != null && player.choose(Outcome.BoostCreature, typeChoice, game)) {
FilterControlledPermanent filter = new FilterControlledPermanent();
filter.add(SubType.byDescription(typeChoice.getChoice()).getPredicate());
return new GainLifeEffect(new PermanentsOnBattlefieldCount(filter, 2)).apply(game, source);
}
return false;
}
use of mage.filter.common.FilterControlledPermanent in project mage by magefree.
the class WordsOfWindEffect method replaceEvent.
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
game.informPlayers("Each player returns a permanent they control to its owner's hand instead");
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
TargetControlledPermanent target = new TargetControlledPermanent();
List<Permanent> liste = game.getBattlefield().getActivePermanents(new FilterControlledPermanent(), playerId, game);
if (!liste.isEmpty()) {
while (!player.choose(Outcome.ReturnToHand, target, source.getSourceId(), game)) {
if (!player.canRespond()) {
return false;
}
}
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent != null) {
player.moveCards(permanent, Zone.HAND, source, game);
}
}
}
}
this.used = true;
discard();
return true;
}
use of mage.filter.common.FilterControlledPermanent in project mage by magefree.
the class WorldQuellerEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
List<Card> chosen = new ArrayList<>();
Player player = game.getPlayer(source.getControllerId());
Permanent sourceCreature = game.getPermanent(source.getSourceId());
if (player != null && sourceCreature != null) {
Choice choiceImpl = new ChoiceImpl();
choiceImpl.setChoices(choice);
if (!player.choose(Outcome.Neutral, choiceImpl, game)) {
return false;
}
CardType type = null;
String choosenType = choiceImpl.getChoice();
if (choosenType.equals(CardType.ARTIFACT.toString())) {
type = CardType.ARTIFACT;
} else if (choosenType.equals(CardType.LAND.toString())) {
type = CardType.LAND;
} else if (choosenType.equals(CardType.CREATURE.toString())) {
type = CardType.CREATURE;
} else if (choosenType.equals(CardType.ENCHANTMENT.toString())) {
type = CardType.ENCHANTMENT;
} else if (choosenType.equals(CardType.INSTANT.toString())) {
type = CardType.INSTANT;
} else if (choosenType.equals(CardType.SORCERY.toString())) {
type = CardType.SORCERY;
} else if (choosenType.equals(CardType.PLANESWALKER.toString())) {
type = CardType.PLANESWALKER;
} else if (choosenType.equals(CardType.TRIBAL.toString())) {
type = CardType.TRIBAL;
}
if (type != null) {
FilterControlledPermanent filter = new FilterControlledPermanent("permanent you control of type " + type.toString());
filter.add(type.getPredicate());
TargetPermanent target = new TargetControlledPermanent(1, 1, filter, false);
target.setNotTarget(true);
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player2 = game.getPlayer(playerId);
if (player2 != null && target.canChoose(source.getSourceId(), playerId, game)) {
while (player2.canRespond() && !target.isChosen() && target.canChoose(source.getSourceId(), playerId, game)) {
player2.chooseTarget(Outcome.Sacrifice, target, source, game);
}
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent != null) {
chosen.add(permanent);
}
target.clearChosen();
}
}
// all chosen permanents are sacrificed together
for (Permanent permanent : game.getBattlefield().getAllActivePermanents()) {
if (chosen.contains(permanent)) {
permanent.sacrifice(source, game);
}
}
return true;
}
}
return false;
}
use of mage.filter.common.FilterControlledPermanent in project mage by magefree.
the class BalanceEffect method choosePermanentsToKeep.
private void choosePermanentsToKeep(Game game, Ability source, Player controller, FilterControlledPermanent filterPermanent) {
int lowestPermanentsCount = Integer.MAX_VALUE;
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player == null) {
continue;
}
lowestPermanentsCount = Math.min(lowestPermanentsCount, game.getBattlefield().countAll(filterPermanent, player.getId(), game));
}
List<Permanent> permanentsToSacrifice = new ArrayList<>();
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player == null) {
continue;
}
List<Permanent> allPermanentsOfType = game.getBattlefield().getActivePermanents(filterPermanent, player.getId(), source.getSourceId(), game);
List<Permanent> permanentsToKeep = new ArrayList<>();
if (lowestPermanentsCount > 0) {
TargetControlledPermanent target = new TargetControlledPermanent(lowestPermanentsCount, lowestPermanentsCount, filterPermanent, true);
if (target.choose(Outcome.Protect, player.getId(), source.getSourceId(), game)) {
for (Permanent permanent : allPermanentsOfType) {
if (permanent != null && target.getTargets().contains(permanent.getId())) {
permanentsToKeep.add(permanent);
}
}
// Prevent possible cheat by disconnecting. If no targets are chosen, just pick the first in the list.
// https://github.com/magefree/mage/issues/4263
} else {
int numPermanents = 0;
for (Permanent permanent : allPermanentsOfType) {
if (numPermanents >= lowestPermanentsCount) {
break;
}
if (permanent != null) {
permanentsToKeep.add(permanent);
numPermanents++;
}
}
}
}
List<Permanent> playerPermanentsToSacrifice = allPermanentsOfType.stream().filter(e -> !permanentsToKeep.contains(e)).collect(Collectors.toList());
permanentsToSacrifice.addAll(playerPermanentsToSacrifice);
if (!playerPermanentsToSacrifice.isEmpty()) {
game.informPlayers(player.getLogName() + " chose permanents to be sacrificed: " + playerPermanentsToSacrifice.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
}
}
for (Permanent permanent : permanentsToSacrifice) {
if (permanent != null) {
permanent.sacrifice(source, game);
}
}
}
Aggregations