[filters] Replaced conditions in StackObject filters with Predicates

This commit is contained in:
North 2012-07-17 18:58:18 +03:00
parent 8598f9ead1
commit b7f57c8a23
9 changed files with 63 additions and 167 deletions

View file

@ -27,7 +27,6 @@
*/
package mage.sets.avacynrestored;
import mage.Constants;
import mage.Constants.CardType;
import mage.Constants.Rarity;
import mage.abilities.effects.common.CounterTargetEffect;
@ -35,7 +34,7 @@ import mage.cards.CardImpl;
import mage.filter.FilterSpell;
import mage.game.Game;
import mage.game.stack.Spell;
import mage.game.stack.StackObject;
import mage.filter.predicate.Predicate;
import mage.target.TargetSpell;
import mage.watchers.common.CastSpellLastTurnWatcher;
@ -47,6 +46,12 @@ import java.util.UUID;
*/
public class SecondGuess extends CardImpl<SecondGuess> {
private static final FilterSpell filter = new FilterSpell("spell that's the second spell cast this turn");
static {
filter.add(new SecondSpellPredicate());
}
public SecondGuess(UUID ownerId) {
super(ownerId, 74, "Second Guess", Rarity.UNCOMMON, new CardType[]{CardType.INSTANT}, "{1}{U}");
this.expansionSetCode = "AVR";
@ -55,7 +60,7 @@ public class SecondGuess extends CardImpl<SecondGuess> {
// Counter target spell that's the second spell cast this turn.
this.getSpellAbility().addEffect(new CounterTargetEffect());
this.getSpellAbility().addTarget(new TargetSpell(new FilterSecondSpell()));
this.getSpellAbility().addTarget(new TargetSpell(filter));
}
public SecondGuess(final SecondGuess card) {
@ -68,65 +73,21 @@ public class SecondGuess extends CardImpl<SecondGuess> {
}
}
class FilterSecondSpell extends FilterSpell {
public FilterSecondSpell() {
super("spell that's the second spell cast this turn");
}
public FilterSecondSpell(final FilterSecondSpell filter) {
super(filter);
}
class SecondSpellPredicate implements Predicate<Spell> {
@Override
public boolean match(StackObject spell, Game game) {
if (!super.match(spell, game))
return notFilter;
public boolean apply(Spell input, Game game) {
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get("CastSpellLastTurnWatcher");
if (spell instanceof Spell) {
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get("CastSpellLastTurnWatcher");
int index = watcher.getSpellOrder((Spell)spell);
if (index == 2) {
return !notFilter;
}
if (watcher.getSpellOrder(input) == 2) {
return true;
}
return notFilter;
return false;
}
@Override
public boolean match(StackObject spell, UUID playerId, Game game) {
if (!super.match(spell, playerId, game))
return notFilter;
if (spell instanceof Spell) {
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get("CastSpellLastTurnWatcher");
int index = watcher.getSpellOrder((Spell)spell);
if (index == 2) {
return notFilter;
}
}
return !notFilter;
public String toString() {
return "SecondSpellThisTurn";
}
@Override
public FilterSecondSpell copy() {
return new FilterSecondSpell(this);
}
@Override
public void setFromZone(Constants.Zone fromZone) {
this.fromZone = fromZone;
}
@Override
public void setNotFromZone(boolean notFromZone) {
this.notFromZone = notFromZone;
}
}

View file

@ -39,6 +39,7 @@ import mage.cards.CardImpl;
import mage.filter.FilterSpell;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.filter.predicate.permanent.ControllerPredicate;
import mage.game.Game;
import mage.game.stack.Spell;
import mage.target.TargetSpell;
@ -55,7 +56,7 @@ public class IncreasingVengeance extends CardImpl<IncreasingVengeance> {
filter.add(Predicates.or(
new CardTypePredicate(CardType.INSTANT),
new CardTypePredicate(CardType.SORCERY)));
filter.setTargetController(Constants.TargetController.YOU);
filter.add(new ControllerPredicate(Constants.TargetController.YOU));
}
public IncreasingVengeance(UUID ownerId) {

View file

@ -28,13 +28,16 @@
package mage.sets.darkascension;
import java.util.UUID;
import mage.Constants;
import mage.Constants.CardType;
import mage.Constants.Rarity;
import mage.Constants.Zone;
import mage.abilities.common.SpellCastTriggeredAbility;
import mage.abilities.effects.common.DrawCardControllerEffect;
import mage.cards.CardImpl;
import mage.filter.FilterSpell;
import mage.filter.predicate.Predicate;
import mage.game.Game;
import mage.game.stack.Spell;
/**
*
@ -45,7 +48,7 @@ public class SecretsOfTheDead extends CardImpl<SecretsOfTheDead> {
private final static FilterSpell filter = new FilterSpell("a spell from your graveyard");
static {
filter.setFromZone(Constants.Zone.GRAVEYARD);
filter.add(new SpellZonePredicate(Zone.GRAVEYARD));
}
public SecretsOfTheDead(UUID ownerId) {
@ -67,3 +70,22 @@ public class SecretsOfTheDead extends CardImpl<SecretsOfTheDead> {
return new SecretsOfTheDead(this);
}
}
class SpellZonePredicate implements Predicate<Spell> {
private final Zone zone;
public SpellZonePredicate(Zone zone) {
this.zone = zone;
}
@Override
public boolean apply(Spell input, Game game) {
return input.getFromZone().match(zone);
}
@Override
public String toString() {
return "SpellZone(" + zone + ')';
}
}

View file

@ -39,6 +39,7 @@ import mage.abilities.keyword.HexproofAbility;
import mage.abilities.keyword.LeylineAbility;
import mage.cards.CardImpl;
import mage.filter.FilterStackObject;
import mage.filter.predicate.permanent.ControllerPredicate;
/**
*
@ -49,7 +50,7 @@ public class LeylineOfSanctity extends CardImpl<LeylineOfSanctity> {
private static final FilterStackObject filter = new FilterStackObject("spells or abilities your opponents control");
static {
filter.setTargetController(TargetController.OPPONENT);
filter.add(new ControllerPredicate(TargetController.OPPONENT));
}
public LeylineOfSanctity(UUID ownerId) {

View file

@ -39,6 +39,7 @@ import mage.abilities.effects.ReplacementEffectImpl;
import mage.abilities.effects.common.RegenerateTargetEffect;
import mage.cards.CardImpl;
import mage.filter.FilterStackObject;
import mage.filter.predicate.permanent.ControllerPredicate;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
@ -55,7 +56,7 @@ public class Asceticism extends CardImpl<Asceticism> {
private static final FilterStackObject filter = new FilterStackObject("spells or abilities your opponents control");
static {
filter.setTargetController(Constants.TargetController.OPPONENT);
filter.add(new ControllerPredicate(Constants.TargetController.OPPONENT));
}
public Asceticism(UUID ownerId) {

View file

@ -86,18 +86,15 @@ public class CantCounterControlledEffect extends ReplacementEffectImpl<CantCount
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == EventType.COUNTER) {
filterTarget.getControllerId().clear();
filterTarget.getControllerId().add(source.getControllerId());
Spell spell = game.getStack().getSpell(event.getTargetId());
if (spell != null) {
if (filterTarget.match(spell, game)) {
if (filterSource == null)
if (spell != null && spell.getControllerId().equals(source.getControllerId())
&& filterTarget.match(spell, game)) {
if (filterSource == null)
return true;
else {
MageObject sourceObject = game.getObject(source.getSourceId());
if (sourceObject != null && filterSource.match(sourceObject, game)) {
return true;
else {
MageObject sourceObject = game.getObject(source.getSourceId());
if (sourceObject != null && filterSource.match(sourceObject, game)) {
return true;
}
}
}
}

View file

@ -5,6 +5,7 @@ import mage.Constants;
import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.effects.common.CantTargetSourceEffect;
import mage.filter.FilterStackObject;
import mage.filter.predicate.permanent.ControllerPredicate;
/**
* Hexproof
@ -19,7 +20,7 @@ public class HexproofAbility extends SimpleStaticAbility {
static {
filter = new FilterStackObject("spells or abilities your opponents control");
filter.setTargetController(Constants.TargetController.OPPONENT);
filter.add(new ControllerPredicate(Constants.TargetController.OPPONENT));
fINSTANCE = new HexproofAbility();
}

View file

@ -28,21 +28,13 @@
package mage.filter;
import mage.Constants.Zone;
import mage.game.Game;
import mage.game.stack.Spell;
import mage.game.stack.StackObject;
import java.util.UUID;
/**
*
* @author BetaSteward_at_googlemail.com
* @author North
*/
public class FilterSpell extends FilterStackObject {
protected Zone fromZone = Zone.ALL;
protected boolean notFromZone = false;
public class FilterSpell extends FilterStackObject<Spell> {
public FilterSpell() {
super("spell");
@ -54,43 +46,10 @@ public class FilterSpell extends FilterStackObject {
public FilterSpell(final FilterSpell filter) {
super(filter);
for (Object cId: filter.getControllerId()) {
this.controllerId.add((UUID)cId);
}
this.notController = filter.notController;
this.controller = filter.controller;
}
@Override
public boolean match(StackObject spell, Game game) {
if (!(spell instanceof Spell))
return notFilter;
if (((Spell)spell).getFromZone().match(fromZone) == notFromZone)
return notFilter;
return super.match(spell, game);
}
@Override
public boolean match(StackObject spell, UUID playerId, Game game) {
if (!this.match(spell, game))
return notFilter;
return super.match(spell, playerId, game);
}
@Override
public FilterSpell copy() {
return new FilterSpell(this);
}
public void setFromZone(Zone fromZone) {
this.fromZone = fromZone;
}
public void setNotFromZone(boolean notFromZone) {
this.notFromZone = notFromZone;
}
}

View file

@ -31,24 +31,20 @@ package mage.filter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import mage.Constants.TargetController;
import mage.filter.predicate.ObjectPlayer;
import mage.filter.predicate.ObjectPlayerPredicate;
import mage.filter.predicate.Predicates;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.game.stack.StackObject;
/**
*
* @author BetaSteward_at_googlemail.com
* @author North
*/
public class FilterStackObject extends FilterObject<StackObject> {
public class FilterStackObject<T extends StackObject> extends FilterObject<T> {
protected List<ObjectPlayerPredicate<ObjectPlayer<Permanent>>> extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Permanent>>>();
protected List<UUID> controllerId = new ArrayList<UUID>();
protected boolean notController = false;
protected TargetController controller = TargetController.ANY;
public FilterStackObject() {
super("spell or ability");
@ -60,64 +56,21 @@ public class FilterStackObject extends FilterObject<StackObject> {
public FilterStackObject(final FilterStackObject filter) {
super(filter);
this.controllerId.addAll(filter.controllerId);
this.notController = filter.notController;
this.controller = filter.controller;
this.extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Permanent>>>(extraPredicates);
}
@Override
public boolean match(StackObject spell, Game game) {
if (!super.match(spell, game))
return notFilter;
if (controllerId.size() > 0 && controllerId.contains(spell.getControllerId()) == notController)
return notFilter;
return !notFilter;
}
public boolean match(StackObject spell, UUID playerId, Game game) {
if (!this.match(spell, game))
return notFilter;
if (controller != TargetController.ANY && playerId != null) {
switch(controller) {
case YOU:
if (!spell.getControllerId().equals(playerId))
return notFilter;
break;
case OPPONENT:
if (!game.getOpponents(playerId).contains(spell.getControllerId()))
return notFilter;
break;
case NOT_YOU:
if (spell.getControllerId().equals(playerId))
return notFilter;
break;
}
public boolean match(T stackObject, UUID playerId, Game game) {
if (!this.match(stackObject, game)) {
return false;
}
return !notFilter;
return Predicates.and(extraPredicates).apply(new ObjectPlayer(stackObject, playerId), game);
}
public void add(ObjectPlayerPredicate predicate) {
extraPredicates.add(predicate);
}
public List<UUID> getControllerId() {
return controllerId;
}
public void setNotController(boolean notController) {
this.notController = notController;
}
public void setTargetController(TargetController controller) {
this.controller = controller;
}
@Override
public FilterStackObject copy() {
return new FilterStackObject(this);