use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class KathrilAspectWarperEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if (player == null || game.getBattlefield().countAll(StaticFilters.FILTER_PERMANENT_CREATURE, source.getControllerId(), game) == 0) {
return false;
}
Set<CounterType> counterSet = player.getGraveyard().getCards(StaticFilters.FILTER_CARD_CREATURE, game).stream().map(Card::getAbilities).flatMap(Collection::stream).map(this::checkAbility).collect(Collectors.toSet());
if (counterSet == null || counterSet.size() == 0) {
return false;
}
int countersAdded = 0;
for (CounterType counterType : counterSet) {
if (counterType == null) {
continue;
}
FilterControlledPermanent filter = new FilterControlledCreaturePermanent("creature to give a " + counterType + " counter");
Target target = new TargetControlledPermanent(filter);
target.setNotTarget(true);
if (!player.choose(outcome, target, source.getSourceId(), game)) {
continue;
}
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent == null) {
continue;
}
if (permanent.addCounters(counterType.createInstance(), source.getControllerId(), source, game)) {
countersAdded++;
}
}
if (countersAdded == 0) {
return false;
}
Permanent permanent = game.getPermanent(source.getSourceId());
if (permanent == null) {
return true;
}
permanent.addCounters(CounterType.P1P1.createInstance(countersAdded), source.getControllerId(), source, game);
return true;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class MisfortuneEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Player chosenOpponent = game.getPlayer(targetPointer.getFirst(game, source));
if (controller != null && chosenOpponent != null) {
if (chosenOpponent.chooseUse(Outcome.Neutral, "If you choose Yes, the controller puts a +1/+1 counter" + "on each creature they control and they gain 4 life. If no, the controller puts a -1/-1 counter" + "on each creature you control and {this} deals 4 damage to you.", source, game)) {
Effect putP1P1CounterOnEachControlledCreature = new AddCountersAllEffect(CounterType.P1P1.createInstance(), new FilterControlledCreaturePermanent());
putP1P1CounterOnEachControlledCreature.apply(game, source);
controller.gainLife(4, game, source);
} else {
FilterCreaturePermanent filterOpponentCreatures = new FilterCreaturePermanent();
filterOpponentCreatures.add(new ControllerIdPredicate(chosenOpponent.getId()));
Effect putM1M1CounterOnEachOpponentCreature = new AddCountersAllEffect(CounterType.M1M1.createInstance(), filterOpponentCreatures);
putM1M1CounterOnEachOpponentCreature.apply(game, source);
chosenOpponent.damage(4, source.getSourceId(), source, game);
}
return true;
}
return false;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class OrzhovAdvokistEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
List<UUID> players = new ArrayList<>();
List<UUID> creatures = new ArrayList<>();
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
if (player.chooseUse(outcome, "Put two +1/+1 counters on a creature you control?", source, game)) {
Target target = new TargetControlledCreaturePermanent(new FilterControlledCreaturePermanent("a creature you control (to add two +1/+1 counters on it)"));
if (player.choose(outcome, target, playerId, game)) {
creatures.add(target.getFirstTarget());
players.add(player.getId());
}
}
}
}
for (UUID creatureId : creatures) {
Permanent creature = game.getPermanent(creatureId);
if (creature != null) {
creature.addCounters(CounterType.P1P1.createInstance(2), creature.getControllerId(), source, game);
}
}
for (UUID playerId : players) {
if (!Objects.equals(playerId, source.getControllerId())) {
FilterCreaturePermanent filter = new FilterCreaturePermanent();
filter.add(new ControllerIdPredicate(playerId));
game.addEffect(new CantAttackYouAllEffect(Duration.UntilYourNextTurn, filter, true), source);
}
}
return true;
}
return false;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class ReinsOfPowerEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
UUID opponentId = this.getTargetPointer().getFirst(game, source);
if (opponentId != null) {
// Untap all creatures you control and all creatures target opponent controls.
FilterCreaturePermanent filter = new FilterCreaturePermanent();
filter.add(Predicates.or(new ControllerIdPredicate(source.getControllerId()), new ControllerIdPredicate(opponentId)));
new UntapAllEffect(filter).apply(game, source);
// You and that opponent each gain control of all creatures the other controls until end of turn.
Set<UUID> yourCreatures = new HashSet<>();
Set<UUID> opponentCreatures = new HashSet<>();
for (Permanent permanent : game.getBattlefield().getActivePermanents(new FilterControlledCreaturePermanent(), source.getControllerId(), source.getSourceId(), game)) {
yourCreatures.add(permanent.getId());
}
FilterCreaturePermanent filterOpponent = new FilterCreaturePermanent();
filterOpponent.add(new ControllerIdPredicate(opponentId));
for (Permanent permanent : game.getBattlefield().getActivePermanents(filterOpponent, source.getControllerId(), source.getSourceId(), game)) {
opponentCreatures.add(permanent.getId());
}
for (UUID creatureId : yourCreatures) {
ContinuousEffect effect = new GainControlTargetEffect(Duration.EndOfTurn, opponentId);
effect.setTargetPointer(new FixedTarget(creatureId, game));
game.addEffect(effect, source);
}
for (UUID creatureId : opponentCreatures) {
ContinuousEffect effect = new GainControlTargetEffect(Duration.EndOfTurn);
effect.setTargetPointer(new FixedTarget(creatureId, game));
game.addEffect(effect, source);
}
// Those creatures gain haste until end of turn.
game.addEffect(new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filter), source);
return true;
}
return false;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class Combat method handleBanding.
private void handleBanding(UUID creatureId, Game game) {
Player player = game.getPlayer(attackingPlayerId);
Permanent attacker = game.getPermanent(creatureId);
if (attacker == null || player == null) {
return;
}
CombatGroup combatGroup = findGroup(attacker.getId());
if (combatGroup == null || !attacker.getBandedCards().isEmpty() || getAttackers().size() <= 1) {
return;
}
boolean canBand = attacker.hasAbility(BandingAbility.getInstance(), game);
List<Ability> bandsWithOther = new ArrayList<>();
for (Ability ability : attacker.getAbilities()) {
if (ability.getClass().equals(BandsWithOtherAbility.class)) {
bandsWithOther.add(ability);
}
}
boolean canBandWithOther = !bandsWithOther.isEmpty();
if (!canBand && !canBandWithOther) {
return;
}
boolean isBanded = false;
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("attacking creature to band with " + attacker.getLogName());
filter.add(Predicates.not(new PermanentIdPredicate(creatureId)));
// creature that isn't already banded, and is attacking the same player or planeswalker
filter.add(new AttackingSameNotBandedPredicate(combatGroup.getDefenderId()));
List<Predicate<MageObject>> predicates = new ArrayList<>();
if (!canBand && canBandWithOther) {
for (Ability ab : bandsWithOther) {
BandsWithOtherAbility ability = (BandsWithOtherAbility) ab;
if (ability.getSubtype() != null) {
predicates.add(ability.getSubtype().getPredicate());
}
if (ability.getSupertype() != null) {
predicates.add(ability.getSupertype().getPredicate());
}
if (ability.getName() != null) {
predicates.add(new NamePredicate(ability.getName()));
}
}
filter.add(Predicates.or(predicates));
}
while (player.canRespond()) {
TargetControlledPermanent target = new TargetControlledPermanent(1, 1, filter, true);
target.setRequired(false);
canBand &= target.canChoose(attackingPlayerId, game);
canBandWithOther &= target.canChoose(attackingPlayerId, game);
if (game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, attackingPlayerId, attackingPlayerId)) || (!canBand && !canBandWithOther) || !player.chooseUse(Outcome.Benefit, (isBanded ? "Band " + attacker.getLogName() + " with another " : "Form a band with " + attacker.getLogName() + " and an ") + "attacking creature?", null, game)) {
break;
}
if (canBand && canBandWithOther) {
if (player.chooseUse(Outcome.Detriment, "Choose type of banding ability to apply:", attacker.getLogName(), "Banding", "Bands with other", null, game)) {
canBandWithOther = false;
} else {
canBand = false;
for (Ability ab : bandsWithOther) {
BandsWithOtherAbility ability = (BandsWithOtherAbility) ab;
if (ability.getSubtype() != null) {
predicates.add(ability.getSubtype().getPredicate());
}
if (ability.getSupertype() != null) {
predicates.add(ability.getSupertype().getPredicate());
}
if (ability.getName() != null) {
predicates.add(new NamePredicate(ability.getName()));
}
}
filter.add(Predicates.or(predicates));
}
}
if (target.choose(Outcome.Benefit, attackingPlayerId, null, game)) {
isBanded = true;
for (UUID targetId : target.getTargets()) {
Permanent permanent = game.getPermanent(targetId);
if (permanent != null) {
for (UUID bandedId : attacker.getBandedCards()) {
permanent.addBandedCard(bandedId);
Permanent banded = game.getPermanent(bandedId);
if (banded != null) {
banded.addBandedCard(targetId);
}
}
permanent.addBandedCard(creatureId);
attacker.addBandedCard(targetId);
if (canBand) {
if (!permanent.hasAbility(BandingAbility.getInstance(), game)) {
filter.add(new AbilityPredicate(BandingAbility.class));
canBandWithOther = false;
}
} else if (canBandWithOther) {
List<Predicate<MageObject>> newPredicates = new ArrayList<>();
for (Predicate<MageObject> predicate : predicates) {
if (predicate.apply(permanent, game)) {
newPredicates.add(predicate);
}
}
filter.add(Predicates.or(newPredicates));
canBand = false;
}
}
}
}
}
if (!isBanded) {
return;
}
StringBuilder sb = new StringBuilder(player.getLogName()).append(" formed a band with ").append(attacker.getBandedCards().size()).append(1).append(" creatures: ");
sb.append(attacker.getLogName());
for (UUID id : attacker.getBandedCards()) {
sb.append(", ");
Permanent permanent = game.getPermanent(id);
if (permanent != null) {
sb.append(permanent.getLogName());
}
}
game.informPlayers(sb.toString());
}
Aggregations