From 5856e69f842d1cd199038d0691eef1eb0888ef2f Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Tue, 6 Nov 2012 16:55:34 +0100 Subject: [PATCH] CardIdPredicate, fixed bug in FilterCard where wrong playerId was used for match, some changes for ExileFromHandCost --- .../costs/common/ExileFromHandCost.java | 2 +- Mage/src/mage/cards/Cards.java | 1 + Mage/src/mage/cards/CardsImpl.java | 27 ++++++++- Mage/src/mage/filter/FilterCard.java | 9 +++ .../mage/filter/common/FilterOwnedCard.java | 58 +++++++++++++++++++ .../predicate/mageobject/CardIdPredicate.java | 56 ++++++++++++++++++ Mage/src/mage/target/TargetCard.java | 58 +++++++++---------- 7 files changed, 178 insertions(+), 33 deletions(-) create mode 100644 Mage/src/mage/filter/common/FilterOwnedCard.java create mode 100644 Mage/src/mage/filter/predicate/mageobject/CardIdPredicate.java diff --git a/Mage/src/mage/abilities/costs/common/ExileFromHandCost.java b/Mage/src/mage/abilities/costs/common/ExileFromHandCost.java index 4286c6f4e09..8f1c2468a6c 100644 --- a/Mage/src/mage/abilities/costs/common/ExileFromHandCost.java +++ b/Mage/src/mage/abilities/costs/common/ExileFromHandCost.java @@ -77,7 +77,7 @@ public class ExileFromHandCost extends CostImpl { @Override public boolean canPay(UUID sourceId, UUID controllerId, Game game) { - return targets.canChoose(controllerId, game); + return targets.canChoose(sourceId, controllerId, game); } @Override diff --git a/Mage/src/mage/cards/Cards.java b/Mage/src/mage/cards/Cards.java index 7edfb785695..fc0230ca0f7 100644 --- a/Mage/src/mage/cards/Cards.java +++ b/Mage/src/mage/cards/Cards.java @@ -49,6 +49,7 @@ public interface Cards extends Set, Serializable { public Card getRandom(Game game); public int count(FilterCard filter, Game game); public int count(FilterCard filter, UUID playerId, Game game); + public int count(FilterCard filter, UUID sourceId, UUID playerId, Game game); public Cards copy(); } diff --git a/Mage/src/mage/cards/CardsImpl.java b/Mage/src/mage/cards/CardsImpl.java index 81aae08395b..8875f3256ea 100644 --- a/Mage/src/mage/cards/CardsImpl.java +++ b/Mage/src/mage/cards/CardsImpl.java @@ -28,12 +28,19 @@ package mage.cards; +import java.io.Serializable; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.UUID; import mage.Constants.Zone; import mage.filter.FilterCard; import mage.game.Game; -import java.io.Serializable; -import java.util.*; /** * @@ -118,7 +125,7 @@ public class CardsImpl extends LinkedHashSet implements Cards, Serializabl return result; } - @Override + @Override public int count(FilterCard filter, UUID playerId, Game game) { int result = 0; for (UUID card: this) { @@ -128,6 +135,20 @@ public class CardsImpl extends LinkedHashSet implements Cards, Serializabl return result; } + @Override + public int count(FilterCard filter, UUID sourceId, UUID playerId, Game game) { + if (sourceId == null) { + return count(filter, playerId, game); + } + int result = 0; + for (UUID card: this) { + if (filter.match(game.getCard(card), sourceId, playerId, game)) { + result++; + } + } + return result; + } + @Override public Set getCards(FilterCard filter, Game game) { Set cards = new LinkedHashSet(); diff --git a/Mage/src/mage/filter/FilterCard.java b/Mage/src/mage/filter/FilterCard.java index 682ac8dffe9..6aa2f30f438 100644 --- a/Mage/src/mage/filter/FilterCard.java +++ b/Mage/src/mage/filter/FilterCard.java @@ -35,6 +35,7 @@ import java.util.UUID; import mage.cards.Card; import mage.filter.predicate.ObjectPlayer; import mage.filter.predicate.ObjectPlayerPredicate; +import mage.filter.predicate.ObjectSourcePlayer; import mage.filter.predicate.Predicates; import mage.game.Game; import mage.game.permanent.Permanent; @@ -70,6 +71,14 @@ public class FilterCard extends FilterObject { return Predicates.and(extraPredicates).apply(new ObjectPlayer(card, playerId), game); } + public boolean match(Card card, UUID sourceId, UUID playerId, Game game) { + if (!this.match(card, game)) { + return false; + } + + return Predicates.and(extraPredicates).apply(new ObjectSourcePlayer(card, sourceId, playerId), game); + } + public void add(ObjectPlayerPredicate predicate) { extraPredicates.add(predicate); } diff --git a/Mage/src/mage/filter/common/FilterOwnedCard.java b/Mage/src/mage/filter/common/FilterOwnedCard.java new file mode 100644 index 00000000000..d2c21985024 --- /dev/null +++ b/Mage/src/mage/filter/common/FilterOwnedCard.java @@ -0,0 +1,58 @@ +/* + * 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.common; + +import mage.Constants; +import mage.filter.FilterCard; +import mage.filter.predicate.other.OwnerPredicate; + +/** + * + * @author LevelX2 + */ +public class FilterOwnedCard extends FilterCard { + + public FilterOwnedCard() { + this("creature card"); + } + + public FilterOwnedCard(String name) { + super(name); + this.add(new OwnerPredicate(Constants.TargetController.YOU)); + } + + public FilterOwnedCard(final FilterOwnedCard filter) { + super(filter); + } + + @Override + public FilterOwnedCard copy() { + return new FilterOwnedCard(this); + } +} diff --git a/Mage/src/mage/filter/predicate/mageobject/CardIdPredicate.java b/Mage/src/mage/filter/predicate/mageobject/CardIdPredicate.java new file mode 100644 index 00000000000..efdf7e57679 --- /dev/null +++ b/Mage/src/mage/filter/predicate/mageobject/CardIdPredicate.java @@ -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.mageobject; + +import java.util.UUID; +import mage.MageObject; +import mage.filter.predicate.Predicate; +import mage.game.Game; + +/** + * + * @author LevelX2 + */ +public class CardIdPredicate implements Predicate { + + private final UUID cardId; + + public CardIdPredicate(UUID cardId) { + this.cardId = cardId; + } + + @Override + public boolean apply(MageObject input, Game game) { + return input.getId().equals(cardId); + } + + @Override + public String toString() { + return "CardId(" + cardId.toString() + ")"; + } +} diff --git a/Mage/src/mage/target/TargetCard.java b/Mage/src/mage/target/TargetCard.java index fe909547867..c2db1f832de 100644 --- a/Mage/src/mage/target/TargetCard.java +++ b/Mage/src/mage/target/TargetCard.java @@ -87,7 +87,34 @@ public class TargetCard> extends TargetObject= this.minNumberOfTargets) { + return true; + } + break; + case GRAVEYARD: + if (player.getGraveyard().count(filter, sourceId, sourceControllerId, game) >= this.minNumberOfTargets) { + return true; + } + break; + case LIBRARY: + if (player.getLibrary().count(filter, game) >= this.minNumberOfTargets) { + return true; + } + break; + case EXILED: + if (game.getExile().getPermanentExile().count(filter, sourceId, sourceControllerId, game) >= this.minNumberOfTargets) { + return true; + } + break; + } + } + } + return false; } /** @@ -99,34 +126,7 @@ public class TargetCard> extends TargetObject= this.minNumberOfTargets) { - return true; - } - break; - case GRAVEYARD: - if (player.getGraveyard().count(filter, player.getId(), game) >= this.minNumberOfTargets) { - return true; - } - break; - case LIBRARY: - if (player.getLibrary().count(filter, game) >= this.minNumberOfTargets) { - return true; - } - break; - case EXILED: - if (game.getExile().getPermanentExile().count(filter, player.getId(), game) >= this.minNumberOfTargets) { - return true; - } - break; - } - } - } - return false; + return canChoose(null, sourceControllerId, game); } @Override