CardIdPredicate, fixed bug in FilterCard where wrong playerId was used for match, some changes for ExileFromHandCost

This commit is contained in:
LevelX2 2012-11-06 16:55:34 +01:00
parent e62c5a7913
commit 5856e69f84
7 changed files with 178 additions and 33 deletions

View file

@ -77,7 +77,7 @@ public class ExileFromHandCost extends CostImpl<ExileFromHandCost> {
@Override
public boolean canPay(UUID sourceId, UUID controllerId, Game game) {
return targets.canChoose(controllerId, game);
return targets.canChoose(sourceId, controllerId, game);
}
@Override

View file

@ -49,6 +49,7 @@ public interface Cards extends Set<UUID>, 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();
}

View file

@ -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<UUID> 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<UUID> 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<Card> getCards(FilterCard filter, Game game) {
Set<Card> cards = new LinkedHashSet<Card>();

View file

@ -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<Card> {
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);
}

View file

@ -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);
}
}

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.mageobject;
import java.util.UUID;
import mage.MageObject;
import mage.filter.predicate.Predicate;
import mage.game.Game;
/**
*
* @author LevelX2
*/
public class CardIdPredicate implements Predicate<MageObject> {
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() + ")";
}
}

View file

@ -87,7 +87,34 @@ public class TargetCard<T extends TargetCard<T>> extends TargetObject<TargetCard
*/
@Override
public boolean canChoose(UUID sourceId, UUID sourceControllerId, Game game) {
return canChoose(sourceControllerId, game);
for (UUID playerId: game.getPlayer(sourceControllerId).getInRange()) {
Player player = game.getPlayer(playerId);
if (player != null) {
switch (zone) {
case HAND:
if (player.getHand().count(filter, sourceId, sourceControllerId, game) >= 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<T extends TargetCard<T>> extends TargetObject<TargetCard
*/
@Override
public boolean canChoose(UUID sourceControllerId, Game game) {
for (UUID playerId: game.getPlayer(sourceControllerId).getInRange()) {
Player player = game.getPlayer(playerId);
if (player != null) {
switch (zone) {
case HAND:
if (player.getHand().count(filter, player.getId(), game) >= 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