use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class TargetControlledCreatureEachColor method makeFilter.
private static final FilterControlledPermanent makeFilter(String colors) {
List<ObjectColor> objectColors = Arrays.stream(colors.split("")).map(ObjectColor::new).collect(Collectors.toList());
FilterControlledPermanent filter = new FilterControlledCreaturePermanent(CardUtil.concatWithAnd(objectColors.stream().map(ObjectColor::getDescription).map(s -> CardUtil.addArticle(s) + " creature").collect(Collectors.toList())));
filter.add(Predicates.or(objectColors.stream().map(ColorPredicate::new).collect(Collectors.toList())));
return filter;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class CamouflageEffect method applies.
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
Map<UUID, List<List<Permanent>>> masterMap = new HashMap<>();
// and divides them into a number of piles equal to the number of attacking creatures for whom that player is the defending player (piles can be empty)
for (UUID defenderId : game.getCombat().getPlayerDefenders(game)) {
Player defender = game.getPlayer(defenderId);
if (defender != null) {
List<List<Permanent>> masterList = new ArrayList<>();
int attackerCount = 0;
for (CombatGroup combatGroup : game.getCombat().getGroups()) {
if (combatGroup.getDefendingPlayerId().equals(defenderId)) {
attackerCount += combatGroup.getAttackers().size();
}
}
// This shouldn't be necessary, but just in case
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(new FilterCreaturePermanent(), defenderId, game)) {
permanent.setBlocking(0);
}
boolean declinedChoice = false;
while (masterList.size() < attackerCount) {
List<Permanent> newPile = new ArrayList<>();
if (!declinedChoice) {
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("creatures you control not yet assigned to a pile");
for (List<Permanent> list : masterList) {
// Creatures they control that can block additional creatures may likewise be put into additional piles.
// (This temporarily manipulates Blocking values to "test" how many blockers the creature has still left to assign)
List<Permanent> spentBlockers = new ArrayList<>();
for (Permanent possibleBlocker : list) {
if (possibleBlocker.getMaxBlocks() != 0 && possibleBlocker.getBlocking() >= possibleBlocker.getMaxBlocks()) {
spentBlockers.add(possibleBlocker);
}
}
filter.add(Predicates.not(new PermanentInListPredicate(spentBlockers)));
}
if (defender.chooseUse(Outcome.Neutral, "Make a new blocker pile? If not, all remaining piles stay empty. (remaining piles: " + (attackerCount - masterList.size()) + ')', source, game)) {
Target target = new TargetControlledCreaturePermanent(0, Integer.MAX_VALUE, filter, true);
if (target.canChoose(source.getSourceId(), defenderId, game)) {
if (defender.chooseTarget(Outcome.Neutral, target, source, game)) {
for (UUID creatureId : target.getTargets()) {
Permanent creature = game.getPermanent(creatureId);
if (creature != null) {
creature.setBlocking(creature.getBlocking() + 1);
newPile.add(creature);
}
}
}
}
} else {
declinedChoice = true;
}
}
masterList.add(newPile);
StringBuilder sb = new StringBuilder("Blocker pile of ").append(defender.getLogName()).append(" (no. " + masterList.size() + "): ");
int i = 0;
for (Permanent permanent : newPile) {
i++;
sb.append(permanent.getLogName());
if (i < newPile.size()) {
sb.append(", ");
}
}
game.informPlayers(sb.toString());
}
// Clear all test Blocking values before assigning piles
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(new FilterCreaturePermanent(), defenderId, game)) {
permanent.setBlocking(0);
}
masterMap.put(defenderId, masterList);
}
}
// Assign each pile to a different one of those attacking creatures at random. Each creature in a pile that can block the creature that pile is assigned to does so
if (!masterMap.isEmpty()) {
for (UUID playerId : masterMap.keySet()) {
List<Permanent> available = new ArrayList<>();
for (CombatGroup combatGroup : game.getCombat().getGroups()) {
if (combatGroup.getDefendingPlayerId().equals(playerId)) {
for (UUID attackerId : combatGroup.getAttackers()) {
Permanent permanent = game.getPermanent(attackerId);
if (permanent != null && permanent.isCreature(game)) {
available.add(permanent);
}
}
}
}
List<List<Permanent>> allPiles = masterMap.get(playerId);
for (List<Permanent> pile : allPiles) {
if (available.isEmpty()) {
break;
}
int randomAttacker = RandomUtil.nextInt(available.size());
Permanent attacker = available.get(randomAttacker);
if (attacker != null) {
available.remove(randomAttacker);
for (Permanent blocker : pile) {
CombatGroup group = game.getCombat().findGroup(attacker.getId());
if (group != null) {
if (blocker.canBlock(attacker.getId(), game) && (blocker.getMaxBlocks() == 0 || group.getAttackers().size() <= blocker.getMaxBlocks())) {
boolean notYetBlocked = group.getBlockers().isEmpty();
group.addBlockerToGroup(blocker.getId(), blocker.getControllerId(), game);
game.getCombat().addBlockingGroup(blocker.getId(), attacker.getId(), blocker.getControllerId(), game);
if (notYetBlocked) {
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.CREATURE_BLOCKED, attacker.getId(), source, null));
}
// TODO: find an alternate event solution for multi-blockers (as per issue #4285), this will work fine for single blocker creatures though
game.fireEvent(new BlockerDeclaredEvent(attacker.getId(), blocker.getId(), blocker.getControllerId()));
}
}
}
}
}
}
}
return true;
}
return false;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class BreathOfFuryEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent enchantment = game.getPermanent(source.getSourceId());
if (enchantment == null) {
return false;
}
Permanent enchantedCreature = game.getPermanent((UUID) getValue("TriggeringCreatureId"));
Player controller = game.getPlayer(source.getControllerId());
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("creature you control that could be enchanted by " + enchantment.getName());
filter.add(new CanBeEnchantedByPredicate(enchantment));
Target target = new TargetControlledCreaturePermanent(filter);
target.setNotTarget(true);
// Commanders going to the command zone and Rest in Peace style replacement effects don't make Permanent.sacrifice return false.
if (enchantedCreature != null && controller != null && enchantedCreature.sacrifice(source, game) && target.canChoose(source.getSourceId(), controller.getId(), game)) {
controller.choose(outcome, target, source.getSourceId(), game);
Permanent newCreature = game.getPermanent(target.getFirstTarget());
boolean success = false;
if (newCreature != null) {
Permanent oldCreature = game.getPermanent(enchantment.getAttachedTo());
if (oldCreature != null) {
if (oldCreature.getId().equals(newCreature.getId())) {
success = true;
} else {
if (oldCreature.removeAttachment(enchantment.getId(), source, game) && newCreature.addAttachment(enchantment.getId(), source, game)) {
game.informPlayers(enchantment.getLogName() + " was unattached from " + oldCreature.getLogName() + " and attached to " + newCreature.getLogName());
success = true;
}
}
} else if (newCreature.addAttachment(enchantment.getId(), source, game)) {
game.informPlayers(enchantment.getLogName() + " was attached to " + newCreature.getLogName());
success = true;
}
}
if (success) {
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(new FilterControlledCreaturePermanent(), controller.getId(), game)) {
permanent.untap(game);
}
game.getState().getTurnMods().add(new TurnMod(source.getControllerId(), TurnPhase.COMBAT, null, false));
}
return true;
}
return false;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class BravadoBoostEnchantedEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent();
int count = game.getBattlefield().count(filter, source.getSourceId(), source.getControllerId(), game) - 1;
if (count > 0) {
Permanent enchantment = game.getPermanent(source.getSourceId());
if (enchantment != null && enchantment.getAttachedTo() != null) {
Permanent creature = game.getPermanent(enchantment.getAttachedTo());
if (creature != null) {
creature.addPower(count);
creature.addToughness(count);
return true;
}
}
}
return false;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class AdviceFromTheFaeEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
MageObject mageObject = game.getObject(source.getSourceId());
if (controller != null && mageObject != null) {
Set<Card> topCards = controller.getLibrary().getTopCards(game, 5);
Cards cardsFromLibrary = new CardsImpl();
for (Card card : topCards) {
cardsFromLibrary.add(card);
}
controller.lookAtCards(mageObject.getIdName(), cardsFromLibrary, game);
int max = 0;
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
FilterCreaturePermanent filter = new FilterCreaturePermanent();
filter.add(new ControllerIdPredicate(playerId));
if (!Objects.equals(playerId, controller.getId())) {
if (max < game.getBattlefield().countAll(filter, playerId, game)) {
max = game.getBattlefield().countAll(filter, playerId, game);
}
}
}
boolean moreCreatures = game.getBattlefield().countAll(new FilterControlledCreaturePermanent(), controller.getId(), game) > max;
TargetCard target = new TargetCard(moreCreatures ? 2 : 1, Zone.LIBRARY, new FilterCard());
if (controller.choose(Outcome.DrawCard, cardsFromLibrary, target, game)) {
cardsFromLibrary.removeAll(target.getTargets());
controller.moveCards(new CardsImpl(target.getTargets()), Zone.HAND, source, game);
}
controller.putCardsOnBottomOfLibrary(cardsFromLibrary, game, source, true);
return true;
}
return false;
}
Aggregations