[filters] Replaced ControllerId condition in FilterPermanent with Predicate

This commit is contained in:
North 2012-07-16 21:41:00 +03:00
parent 4563e518a6
commit 30cbdd643d
15 changed files with 131 additions and 66 deletions

View file

@ -38,6 +38,7 @@ import mage.abilities.effects.OneShotEffect;
import mage.abilities.effects.RequirementEffect;
import mage.cards.CardImpl;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
@ -95,7 +96,7 @@ class AggraveteEffect extends OneShotEffect<AggraveteEffect> {
Player player = game.getPlayer(source.getFirstTarget());
if (player != null) {
FilterCreaturePermanent filter = new FilterCreaturePermanent();
filter.getControllerId().add(player.getId());
filter.add(new ControllerIdPredicate(player.getId()));
List<Permanent> creatures = game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source.getId(), game);
for (Permanent creature : creatures) {
creature.damage(1, source.getSourceId(), game, true, false);

View file

@ -39,6 +39,7 @@ import mage.abilities.effects.common.DestroyTargetEffect;
import mage.cards.CardImpl;
import mage.filter.common.FilterControlledPermanent;
import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
@ -98,15 +99,13 @@ class FeastOfWormsEffect extends OneShotEffect<FeastOfWormsEffect> {
@Override
public boolean apply(Game game, Ability source) {
//Player player = game.getPlayer(source.getControllerId());
Permanent permanent = (Permanent) game.getLastKnownInformation(source.getFirstTarget(), Zone.BATTLEFIELD);
Player targetPlayer = game.getPlayer(permanent.getControllerId());
if (targetPlayer != null && permanent != null
&& (permanent.getSupertype().get(0).toString().equals("Legendary"))) {
FilterControlledPermanent filter = new FilterControlledPermanent("land to sacrifice");
filter.add(new CardTypePredicate(CardType.LAND));
filter.getControllerId().add(targetPlayer.getId());
filter.setNotController(false);
filter.add(new ControllerIdPredicate(targetPlayer.getId()));
TargetControlledPermanent target = new TargetControlledPermanent(1, 1, filter, false);
if (target.canChoose(targetPlayer.getId(), game)) {

View file

@ -40,6 +40,7 @@ import mage.abilities.effects.common.SkipNextPlayerUntapStepEffect;
import mage.abilities.keyword.FlyingAbility;
import mage.cards.CardImpl;
import mage.filter.FilterPermanent;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
@ -85,8 +86,10 @@ public class YoseiTheMorningStar extends CardImpl<YoseiTheMorningStar> {
class YoseiTheMorningStarTarget extends TargetPermanent {
private static final FilterPermanent filterTemplate = new FilterPermanent("up to five target permanents that player controls that will be tapped");
public YoseiTheMorningStarTarget() {
super(0, 5, new FilterPermanent("up to five target permanents that player controls that will be tapped"), false);
super(0, 5, filterTemplate, false);
}
public YoseiTheMorningStarTarget(final YoseiTheMorningStarTarget target) {
@ -97,8 +100,8 @@ public class YoseiTheMorningStar extends CardImpl<YoseiTheMorningStar> {
public boolean canTarget(UUID controllerId, UUID id, Ability source, Game game) {
Player player = game.getPlayer(source.getFirstTarget());
if (player != null) {
filter.getControllerId().clear();
filter.getControllerId().add(player.getId());
this.filter = filterTemplate.copy();
this.filter.add(new ControllerIdPredicate(player.getId()));
return super.canTarget(controllerId, id, source, game);
}
return false;

View file

@ -40,6 +40,7 @@ import mage.abilities.keyword.FlyingAbility;
import mage.abilities.keyword.TransformAbility;
import mage.cards.CardImpl;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.events.DamagedPlayerEvent;
import mage.game.events.GameEvent;
@ -105,7 +106,8 @@ class SoulSeizerTriggeredAbility extends TriggeredAbilityImpl<SoulSeizerTriggere
Player opponent = game.getPlayer(event.getPlayerId());
if (opponent != null) {
FilterCreaturePermanent filter = new FilterCreaturePermanent("creature " + opponent.getName() + " controls");
filter.getControllerId().add(opponent.getId());
filter.add(new ControllerIdPredicate(opponent.getId()));
this.getTargets().clear();
this.addTarget(new TargetCreaturePermanent(filter));
return true;

View file

@ -38,6 +38,7 @@ import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.common.SacrificeTargetEffect;
import mage.cards.CardImpl;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.events.DamagedPlayerEvent;
import mage.game.events.GameEvent;
@ -95,9 +96,10 @@ class AshlingTheExtinguisherTriggeredAbility extends TriggeredAbilityImpl<Ashlin
if (damageEvent.isCombatDamage() && event.getSourceId().equals(this.getSourceId())) {
Player opponent = game.getPlayer(event.getPlayerId());
if (opponent != null) {
this.getTargets().clear();
FilterCreaturePermanent filter = new FilterCreaturePermanent("creature " + opponent.getName() + " controls");
filter.getControllerId().add(opponent.getId());
filter.add(new ControllerIdPredicate(opponent.getId()));
this.getTargets().clear();
this.addTarget(new TargetCreaturePermanent(filter));
return true;
}

View file

@ -46,6 +46,7 @@ import mage.counters.CounterType;
import mage.filter.common.FilterControlledCreaturePermanent;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.permanent.AnotherPredicate;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.target.common.TargetControlledCreaturePermanent;
@ -113,7 +114,9 @@ class GrimgrinCorpseBornAbility extends TriggeredAbilityImpl<GrimgrinCorpseBornA
if (event.getType() == GameEvent.EventType.ATTACKER_DECLARED && event.getSourceId().equals(this.getSourceId())) {
FilterCreaturePermanent filter = new FilterCreaturePermanent("creature defending player controls");
UUID defenderId = game.getCombat().getDefendingPlayer(sourceId);
filter.getControllerId().add(defenderId);
filter.add(new ControllerIdPredicate(defenderId));
this.getTargets().clear();
TargetCreaturePermanent target = new TargetCreaturePermanent(filter);
target.setRequired(true);
this.addTarget(target);

View file

@ -40,10 +40,12 @@ import mage.cards.CardImpl;
import mage.filter.FilterPermanent;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.ColorPredicate;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.events.DamagedPlayerEvent;
import mage.game.events.GameEvent;
import mage.game.events.GameEvent.EventType;
import mage.players.Player;
import mage.target.TargetPermanent;
/**
@ -95,16 +97,19 @@ class LightwielderPaladinTriggeredAbility extends TriggeredAbilityImpl<Lightwiel
public boolean checkTrigger(GameEvent event, Game game) {
if (event.getType() == EventType.DAMAGED_PLAYER && event.getSourceId().equals(this.sourceId)
&& ((DamagedPlayerEvent) event).isCombatDamage()) {
FilterPermanent filter = new FilterPermanent("black or red permanent");
Player player = game.getPlayer(event.getTargetId());
if (player != null) {
FilterPermanent filter = new FilterPermanent("black or red permanent controlled by " + player.getName());
filter.add(Predicates.or(
new ColorPredicate(ObjectColor.BLACK),
new ColorPredicate(ObjectColor.RED)));
filter.getControllerId().add(event.getTargetId());
filter.setNotController(false);
filter.add(new ControllerIdPredicate(event.getTargetId()));
this.getTargets().clear();
this.addTarget(new TargetPermanent(filter));
return true;
}
}
return false;
}

View file

@ -42,6 +42,7 @@ import mage.abilities.effects.common.DamageTargetEffect;
import mage.abilities.keyword.FlyingAbility;
import mage.cards.CardImpl;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.target.common.TargetCreaturePermanent;
@ -77,18 +78,14 @@ public class AncientHellkite extends CardImpl<AncientHellkite> {
class AncientHellkiteAbility extends ActivatedAbilityImpl<AncientHellkiteAbility> {
private FilterCreaturePermanent filter = new FilterCreaturePermanent("creature defending player controls");
public AncientHellkiteAbility() {
super(Zone.BATTLEFIELD, new DamageTargetEffect(1));
addCost(new AncientHellkiteCost());
addTarget(new TargetCreaturePermanent(filter));
addManaCost(new ColoredManaCost(ColoredManaSymbol.R));
}
public AncientHellkiteAbility(final AncientHellkiteAbility ability) {
super(ability);
this.filter = ability.filter;
}
@Override
@ -100,9 +97,10 @@ class AncientHellkiteAbility extends ActivatedAbilityImpl<AncientHellkiteAbility
public boolean activate(Game game, boolean noMana) {
UUID defenderId = game.getCombat().getDefendingPlayer(sourceId);
if (defenderId != null) {
getTargets().clear();
FilterCreaturePermanent filter = new FilterCreaturePermanent("creature defending player controls");
filter.getControllerId().add(defenderId);
filter.add(new ControllerIdPredicate(defenderId));
this.getTargets().clear();
TargetCreaturePermanent target = new TargetCreaturePermanent(filter);
target.setRequired(true);
this.addTarget(target);

View file

@ -38,6 +38,7 @@ import mage.abilities.common.AttacksTriggeredAbility;
import mage.abilities.effects.OneShotEffect;
import mage.cards.CardImpl;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
@ -89,7 +90,7 @@ class CyclopsGladiatorEffect extends OneShotEffect<CyclopsGladiatorEffect> {
UUID defenderId = game.getCombat().getDefendingPlayer(source.getSourceId());
if (defenderId != null) {
FilterCreaturePermanent filter = new FilterCreaturePermanent("creature defending player controls");
filter.getControllerId().add(defenderId);
filter.add(new ControllerIdPredicate(defenderId));
TargetCreaturePermanent target = new TargetCreaturePermanent(filter);
Player player = game.getPlayer(source.getControllerId());
if (target.canChoose(source.getSourceId(), source.getControllerId(), game)) {

View file

@ -38,6 +38,7 @@ import mage.abilities.effects.common.continious.GainAbilityControlledEffect;
import mage.cards.CardImpl;
import mage.filter.common.FilterControlledCreaturePermanent;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.players.Player;
@ -57,6 +58,7 @@ public class ArmWithAEther extends CardImpl<ArmWithAEther> {
this.color.setBlue(true);
// Until end of turn, creatures you control gain "Whenever this creature deals damage to an opponent, you may return target creature that player controls to its owner's hand."
this.getSpellAbility().addEffect(new GainAbilityControlledEffect(new ArmWithAEtherTriggeredAbility(), Duration.EndOfTurn, filter));
}
@ -89,9 +91,10 @@ class ArmWithAEtherTriggeredAbility extends TriggeredAbilityImpl<ArmWithAEtherTr
public boolean checkTrigger(GameEvent event, Game game) {
Player opponent = game.getPlayer(event.getPlayerId());
if (opponent != null && event.getType() == GameEvent.EventType.DAMAGED_PLAYER && event.getSourceId().equals(this.sourceId)) {
this.getTargets().clear();
FilterCreaturePermanent filter = new FilterCreaturePermanent("creature " + opponent.getName() + " controls");
filter.getControllerId().add(opponent.getId());
filter.add(new ControllerIdPredicate(opponent.getId()));
this.getTargets().clear();
this.addTarget(new TargetCreaturePermanent(filter));
return true;
}

View file

@ -39,6 +39,7 @@ import mage.abilities.effects.OneShotEffect;
import mage.abilities.keyword.IntimidateAbility;
import mage.cards.CardImpl;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.events.DamagedPlayerEvent;
import mage.game.events.GameEvent;
@ -64,6 +65,7 @@ public class BlindZealot extends CardImpl<BlindZealot> {
this.toughness = new MageInt(2);
this.addAbility(IntimidateAbility.getInstance());
// Whenever Blind Zealot deals combat damage to a player, you may sacrifice it. If you do, destroy target creature that player controls.
this.addAbility(new BlindZealotTriggeredAbility());
}
@ -106,11 +108,11 @@ class BlindZealotTriggeredAbility extends TriggeredAbilityImpl<BlindZealotTrigge
sb.append(" to destroy target creature controlled by ");
sb.append(game.getPlayer(event.getTargetId()).getName()).append("?");
if (player.chooseUse(Outcome.DestroyPermanent, sb.toString(), game)) {
this.getTargets().clear();
FilterCreaturePermanent filter = new FilterCreaturePermanent();
filter.getControllerId().add(event.getTargetId());
filter.setNotController(false);
filter.add(new ControllerIdPredicate(event.getTargetId()));
filter.setMessage("creature controlled by " + game.getPlayer(event.getTargetId()).getName());
this.getTargets().clear();
this.addTarget(new TargetCreaturePermanent(filter));
return true;
}

View file

@ -43,6 +43,7 @@ import mage.abilities.keyword.EquipAbility;
import mage.cards.CardImpl;
import mage.filter.FilterPermanent;
import mage.filter.predicate.mageobject.SubtypePredicate;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.events.DamagedPlayerEvent;
import mage.game.events.GameEvent;
@ -59,8 +60,12 @@ public class HammerOfRuin extends CardImpl<HammerOfRuin> {
super(ownerId, 124, "Hammer of Ruin", Rarity.UNCOMMON, new CardType[]{CardType.ARTIFACT}, "{2}");
this.expansionSetCode = "WWK";
this.subtype.add("Equipment");
// Equipped creature gets +2/+0.
this.addAbility(new SimpleStaticAbility(Zone.BATTLEFIELD, new BoostEquippedEffect(2, 0)));
// Whenever equipped creature deals combat damage to a player, you may destroy target Equipment that player controls.
this.addAbility(new HammerOfRuinTriggeredAbility());
// Equip {2}
this.addAbility(new EquipAbility(Outcome.AddAbility, new GenericManaCost(2)));
}
@ -79,9 +84,6 @@ class HammerOfRuinTriggeredAbility extends TriggeredAbilityImpl<HammerOfRuinTrig
HammerOfRuinTriggeredAbility() {
super(Zone.BATTLEFIELD, new DestroyTargetEffect(), true);
FilterPermanent filter = new FilterPermanent("Equipment that player controls");
filter.add(new SubtypePredicate("Equipment"));
this.addTarget(new TargetPermanent(filter));
}
HammerOfRuinTriggeredAbility(final HammerOfRuinTriggeredAbility ability) {
@ -99,9 +101,13 @@ class HammerOfRuinTriggeredAbility extends TriggeredAbilityImpl<HammerOfRuinTrig
DamagedPlayerEvent damageEvent = (DamagedPlayerEvent)event;
Permanent p = game.getPermanent(event.getSourceId());
if (damageEvent.isCombatDamage() && p != null && p.getAttachments().contains(this.getSourceId())) {
FilterPermanent filter = (FilterPermanent)getTargets().get(0).getFilter();
filter.getControllerId().clear();
filter.getControllerId().add(event.getPlayerId());
FilterPermanent filter = new FilterPermanent("Equipment that player controls");
filter.add(new SubtypePredicate("Equipment"));
filter.add(new ControllerIdPredicate(event.getPlayerId()));
filter.setMessage("creature controlled by " + game.getPlayer(event.getTargetId()).getName());
this.getTargets().clear();
this.addTarget(new TargetPermanent(filter));
return true;
}
}

View file

@ -40,12 +40,10 @@ import mage.game.permanent.Permanent;
/**
*
* @author BetaSteward_at_googlemail.com
* @author North
*/
public class FilterPermanent extends FilterObject<Permanent> {
protected List<ObjectPlayerPredicate<ObjectPlayer<Permanent>>> extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Permanent>>>();
protected List<UUID> controllerId = new ArrayList<UUID>();
protected boolean notController;
public FilterPermanent() {
super("permanent");
@ -53,8 +51,6 @@ public class FilterPermanent extends FilterObject<Permanent> {
public FilterPermanent(final FilterPermanent filter) {
super(filter);
this.controllerId = new ArrayList<UUID>(filter.controllerId);
this.notController = filter.notController;
this.extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Permanent>>>(extraPredicates);
}
@ -62,17 +58,6 @@ public class FilterPermanent extends FilterObject<Permanent> {
super(name);
}
@Override
public boolean match(Permanent permanent, Game game) {
if (!super.match(permanent, game))
return notFilter;
if (controllerId.size() > 0 && controllerId.contains(permanent.getControllerId()) == notController)
return notFilter;
return !notFilter;
}
public boolean match(Permanent permanent, UUID sourceId, UUID playerId, Game game) {
if (!this.match(permanent, game))
return false;
@ -84,14 +69,6 @@ public class FilterPermanent extends FilterObject<Permanent> {
extraPredicates.add(predicate);
}
public List<UUID> getControllerId() {
return controllerId;
}
public void setNotController(boolean notController) {
this.notController = notController;
}
@Override
public FilterPermanent copy() {
return new FilterPermanent(this);

View file

@ -28,15 +28,18 @@
package mage.filter.common;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
import mage.filter.FilterImpl;
import mage.filter.FilterPlayer;
import mage.filter.predicate.Predicate;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.permanent.ControllerIdPredicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
import java.util.Set;
import java.util.UUID;
/**
*
* @author BetaSteward_at_googlemail.com
@ -48,8 +51,12 @@ public class FilterPlaneswalkerOrPlayer extends FilterImpl<Object> {
public FilterPlaneswalkerOrPlayer(Set<UUID> defenders) {
super("planeswalker or player");
ArrayList<Predicate<Permanent>> permanentPredicates = new ArrayList<Predicate<Permanent>>();
for (UUID defenderId : defenders) {
permanentPredicates.add(new ControllerIdPredicate(defenderId));
}
planeswalkerFilter = new FilterPlaneswalkerPermanent();
planeswalkerFilter.getControllerId().addAll(defenders);
planeswalkerFilter.add(Predicates.or(permanentPredicates));
playerFilter = new FilterPlayer();
playerFilter.getPlayerId().addAll(defenders);
}

View file

@ -0,0 +1,56 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.filter.predicate.permanent;
import java.util.UUID;
import mage.filter.predicate.Predicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
/**
*
* @author North
*/
public class ControllerIdPredicate implements Predicate<Permanent> {
private final UUID controllerId;
public ControllerIdPredicate(UUID controllerId) {
this.controllerId = controllerId;
}
@Override
public boolean apply(Permanent input, Game game) {
return controllerId.equals(input.getControllerId());
}
@Override
public String toString() {
return "ControllerId(" + controllerId + ')';
}
}