refactored SupertypePredicate to be part of SuperType

This commit is contained in:
Evan Kranzler 2020-01-06 19:21:27 -05:00
parent d919818daa
commit ecf26585b5
190 changed files with 341 additions and 529 deletions

View file

@ -9,7 +9,6 @@ import mage.constants.SuperType;
import mage.constants.Zone;
import mage.filter.FilterPermanent;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.SupertypePredicate;
import mage.game.Game;
import mage.game.events.GameEvent;
@ -40,7 +39,7 @@ class LegendarySpellAbilityCheckEffect extends ContinuousRuleModifyingEffectImpl
static {
filter.add(
Predicates.and(
new SupertypePredicate(SuperType.LEGENDARY),
SuperType.LEGENDARY.getPredicate(),
Predicates.or(
CardType.CREATURE.getPredicate(),
CardType.PLANESWALKER.getPredicate()

View file

@ -6,7 +6,6 @@ import mage.abilities.costs.Cost;
import mage.constants.ColoredManaSymbol;
import mage.constants.SuperType;
import mage.filter.FilterObject;
import mage.filter.predicate.mageobject.SupertypePredicate;
import mage.game.Game;
import mage.players.ManaPool;
@ -15,7 +14,7 @@ public class SnowManaCost extends ManaCostImpl {
private static final FilterObject filter = new FilterObject("Snow object");
static {
filter.add(new SupertypePredicate(SuperType.SNOW));
filter.add(SuperType.SNOW.getPredicate());
}
public SnowManaCost() {

View file

@ -1,5 +1,9 @@
package mage.constants;
import mage.MageObject;
import mage.filter.predicate.Predicate;
import mage.game.Game;
/**
* Created by IGOUDT on 26-3-2017.
*/
@ -12,10 +16,31 @@ public enum SuperType {
SNOW("Snow"),
WORLD("World");
String text;
public static class SuperTypePredicate implements Predicate<MageObject> {
private final SuperType supertype;
private SuperTypePredicate(SuperType supertype) {
this.supertype = supertype;
}
@Override
public boolean apply(MageObject input, Game game) {
return input.getSuperType().contains(supertype);
}
@Override
public String toString() {
return "Supertype(" + supertype + ')';
}
}
private final String text;
private final SuperTypePredicate predicate;
SuperType(String text) {
this.text = text;
this.predicate = new SuperTypePredicate(this);
}
@Override
@ -23,4 +48,7 @@ public enum SuperType {
return text;
}
public SuperTypePredicate getPredicate() {
return predicate;
}
}

View file

@ -12,7 +12,6 @@ import mage.constants.TargetController;
import mage.filter.common.*;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.MulticoloredPredicate;
import mage.filter.predicate.mageobject.SupertypePredicate;
import mage.filter.predicate.other.PlayerPredicate;
import mage.filter.predicate.permanent.AnotherPredicate;
import mage.filter.predicate.permanent.AttackingPredicate;
@ -617,7 +616,7 @@ public final class StaticFilters {
public static final FilterPermanent FILTER_PERMANENT_LEGENDARY = new FilterPermanent();
static {
FILTER_PERMANENT_LEGENDARY.add(new SupertypePredicate(SuperType.LEGENDARY));
FILTER_PERMANENT_LEGENDARY.add(SuperType.LEGENDARY.getPredicate());
FILTER_PERMANENT_LEGENDARY.setLockedFilter(true);
}

View file

@ -4,7 +4,6 @@ package mage.filter.common;
import mage.constants.CardType;
import mage.constants.SuperType;
import mage.filter.FilterCard;
import mage.filter.predicate.mageobject.SupertypePredicate;
/**
*
@ -19,7 +18,7 @@ public class FilterBasicLandCard extends FilterCard {
public FilterBasicLandCard(String name) {
super(name);
this.add(CardType.LAND.getPredicate());
this.add(new SupertypePredicate(SuperType.BASIC));
this.add(SuperType.BASIC.getPredicate());
}
public FilterBasicLandCard(final FilterBasicLandCard filter) {

View file

@ -5,7 +5,6 @@ package mage.filter.common;
import mage.constants.CardType;
import mage.constants.SuperType;
import mage.filter.FilterCard;
import mage.filter.predicate.mageobject.SupertypePredicate;
/**
*
@ -24,7 +23,7 @@ public class FilterLandCard extends FilterCard {
public static FilterLandCard basicLandCard() {
FilterLandCard filter = new FilterLandCard("basic land card");
filter.add(new SupertypePredicate(SuperType.BASIC));
filter.add(SuperType.BASIC.getPredicate());
return filter;
}

View file

@ -7,7 +7,6 @@ import mage.constants.SubType;
import mage.constants.SuperType;
import mage.filter.FilterPermanent;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.SupertypePredicate;
/**
* @author BetaSteward_at_googlemail.com
@ -31,13 +30,13 @@ public class FilterLandPermanent extends FilterPermanent {
public static FilterLandPermanent nonbasicLand() {
FilterLandPermanent filter = new FilterLandPermanent("nonbasic land");
filter.add(Predicates.not(new SupertypePredicate(SuperType.BASIC)));
filter.add(Predicates.not(SuperType.BASIC.getPredicate()));
return filter;
}
public static FilterLandPermanent nonbasicLands() {
FilterLandPermanent filter = new FilterLandPermanent("nonbasic lands");
filter.add(Predicates.not(new SupertypePredicate(SuperType.BASIC)));
filter.add(Predicates.not(SuperType.BASIC.getPredicate()));
return filter;
}

View file

@ -1,30 +0,0 @@
package mage.filter.predicate.mageobject;
import mage.MageObject;
import mage.constants.SuperType;
import mage.filter.predicate.Predicate;
import mage.game.Game;
/**
*
* @author North
*/
public class SupertypePredicate implements Predicate<MageObject> {
private final SuperType supertype;
public SupertypePredicate(SuperType supertype) {
this.supertype = supertype;
}
@Override
public boolean apply(MageObject input, Game game) {
return input.getSuperType().contains(supertype);
}
@Override
public String toString() {
return "Supertype(" + supertype + ')';
}
}

View file

@ -35,7 +35,6 @@ import mage.filter.FilterPermanent;
import mage.filter.StaticFilters;
import mage.filter.common.FilterControlledPermanent;
import mage.filter.predicate.mageobject.NamePredicate;
import mage.filter.predicate.mageobject.SupertypePredicate;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.combat.Combat;
import mage.game.command.CommandObject;
@ -2133,7 +2132,7 @@ public abstract class GameImpl implements Game, Serializable {
if (legendary.size() > 1) { //don't bother checking if less than 2 legends in play
for (Permanent legend : legendary) {
FilterPermanent filterLegendName = new FilterPermanent();
filterLegendName.add(new SupertypePredicate(SuperType.LEGENDARY));
filterLegendName.add(SuperType.LEGENDARY.getPredicate());
filterLegendName.add(new NamePredicate(legend.getName()));
filterLegendName.add(new ControllerIdPredicate(legend.getControllerId()));
if (getBattlefield().contains(filterLegendName, legend.getControllerId(), this, 2)) {

View file

@ -18,7 +18,6 @@ import mage.filter.predicate.Predicate;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.AbilityPredicate;
import mage.filter.predicate.mageobject.NamePredicate;
import mage.filter.predicate.mageobject.SupertypePredicate;
import mage.filter.predicate.permanent.AttackingSameNotBandedPredicate;
import mage.filter.predicate.permanent.PermanentIdPredicate;
import mage.game.Game;
@ -297,128 +296,132 @@ public class Combat implements Serializable, Copyable<Combat> {
private void handleBanding(UUID creatureId, Game game) {
Player player = game.getPlayer(attackingPlayerId);
Permanent attacker = game.getPermanent(creatureId);
if (attacker != null
&& player != null) {
CombatGroup combatGroup = findGroup(attacker.getId());
if (combatGroup != null
&& attacker.getBandedCards().isEmpty()
&& getAttackers().size() > 1) {
boolean canBand = attacker.getAbilities().containsKey(BandingAbility.getInstance().getId());
List<Ability> bandsWithOther = new ArrayList<>();
for (Ability ability : attacker.getAbilities()) {
if (ability.getClass().equals(BandsWithOtherAbility.class)) {
bandsWithOther.add(ability);
}
if (attacker == null
|| player == null) {
return;
}
CombatGroup combatGroup = findGroup(attacker.getId());
if (combatGroup == null
|| !attacker.getBandedCards().isEmpty()
|| getAttackers().size() <= 1) {
return;
}
boolean canBand = attacker.getAbilities().containsKey(BandingAbility.getInstance().getId());
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)));
filter.add(new AttackingSameNotBandedPredicate(combatGroup.getDefenderId())); // creature that isn't already banded, and is attacking the same player or planeswalker
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());
}
boolean canBandWithOther = !bandsWithOther.isEmpty();
if (canBand || canBandWithOther) {
boolean isBanded = false;
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("attacking creature to band with " + attacker.getLogName());
filter.add(Predicates.not(new PermanentIdPredicate(creatureId)));
filter.add(new AttackingSameNotBandedPredicate(combatGroup.getDefenderId())); // creature that isn't already banded, and is attacking the same player or planeswalker
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(new SupertypePredicate(ability.getSupertype()));
}
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,
"Do you wish to " + (isBanded ? "band " + attacker.getLogName()
+ " with another " : "form a band with " + attacker.getLogName() + " and an ")
+ "attacking creature?", null, game)) {
break;
}
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,
"Do you wish to " + (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)) {
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.getAbilities().containsKey(BandingAbility.getInstance().getId())) {
filter.add(new AbilityPredicate(BandingAbility.class));
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(new SupertypePredicate(ability.getSupertype()));
}
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.getAbilities().containsKey(BandingAbility.getInstance().getId())) {
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;
}
} 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) {
StringBuilder sb = new StringBuilder(player.getLogName()).append(" formed a band with ").append((attacker.getBandedCards().size() + 1) + " 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());
}
}
}
}
if (!isBanded) {
return;
}
StringBuilder sb = new StringBuilder(player.getLogName()).append(" formed a band with ").append((attacker.getBandedCards().size() + 1) + " 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());
}
protected void checkAttackRequirements(Player player, Game game) {

View file

@ -5,7 +5,6 @@ package mage.target.common;
import mage.constants.CardType;
import mage.constants.SuperType;
import mage.constants.Zone;
import mage.filter.predicate.mageobject.SupertypePredicate;
import mage.target.TargetCard;
/**
@ -16,7 +15,7 @@ public class TargetBasicLandCard extends TargetCard {
public TargetBasicLandCard(Zone zone) {
super(zone);
filter.add(new SupertypePredicate(SuperType.BASIC));
filter.add(SuperType.BASIC.getPredicate());
filter.add(CardType.LAND.getPredicate());
}

View file

@ -5,7 +5,6 @@ package mage.target.common;
import mage.constants.SuperType;
import mage.filter.common.FilterLandPermanent;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.SupertypePredicate;
/**
*
@ -15,7 +14,7 @@ public class TargetNonBasicLandPermanent extends TargetLandPermanent {
public TargetNonBasicLandPermanent() {
this.filter = new FilterLandPermanent();
this.filter.add(Predicates.not(new SupertypePredicate(SuperType.BASIC)));
this.filter.add(Predicates.not(SuperType.BASIC.getPredicate()));
this.targetName = "nonbasic land";
}