Added the possibility to limit library search to an amount of cards (Aven Mindcensor).

This commit is contained in:
LevelX2 2013-02-28 17:15:40 +01:00
parent 03720ee1fd
commit 1baa60e9de
3 changed files with 48 additions and 9 deletions

View file

@ -169,6 +169,21 @@ public class Library implements Serializable {
return cards; return cards;
} }
public List<Card> getTopCards(Game game, int amount) {
List<Card> cards = new ArrayList<Card>();
Iterator<UUID> it = library.iterator();
int count = 0;
while(it.hasNext() && count < amount) {
UUID cardId = it.next();
Card card = game.getCard(cardId);
if (card != null) {
cards.add(card);
++count;
}
}
return cards;
}
public Collection<Card> getUniqueCards(Game game) { public Collection<Card> getUniqueCards(Game game) {
Map<String, Card> cards = new HashMap<String, Card>(); Map<String, Card> cards = new HashMap<String, Card>();
for (UUID cardId: library) { for (UUID cardId: library) {

View file

@ -1177,7 +1177,16 @@ public abstract class PlayerImpl<T extends PlayerImpl<T>> implements Player, Ser
//20091005 - 701.14c //20091005 - 701.14c
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.SEARCH_LIBRARY, targetPlayerId, playerId))) { if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.SEARCH_LIBRARY, targetPlayerId, playerId))) {
TargetCardInLibrary newTarget = target.copy(); TargetCardInLibrary newTarget = target.copy();
int count = library.count(target.getFilter(), game); int count;
Integer cardLimit = (Integer) game.getState().getValue("LibrarySearchLimit");
if (cardLimit != null) {
newTarget.setCardLimit(cardLimit);
game.getState().setValue("LibrarySearchLimit", null);
count = Math.min(library.count(target.getFilter(), game),cardLimit.intValue());
} else {
count = library.count(target.getFilter(), game);
}
if (count < target.getNumberOfTargets()) { if (count < target.getNumberOfTargets()) {
newTarget.setMinNumberOfTargets(count); newTarget.setMinNumberOfTargets(count);
} }

View file

@ -28,6 +28,8 @@
package mage.target.common; package mage.target.common;
import java.util.ArrayList;
import java.util.List;
import mage.Constants.Outcome; import mage.Constants.Outcome;
import mage.Constants.Zone; import mage.Constants.Zone;
import mage.abilities.Ability; import mage.abilities.Ability;
@ -46,6 +48,8 @@ import java.util.UUID;
*/ */
public class TargetCardInLibrary extends TargetCard<TargetCardInLibrary> { public class TargetCardInLibrary extends TargetCard<TargetCardInLibrary> {
private Integer cardLimit;
public TargetCardInLibrary() { public TargetCardInLibrary() {
this(1, 1, new FilterCard()); this(1, 1, new FilterCard());
} }
@ -64,6 +68,7 @@ public class TargetCardInLibrary extends TargetCard<TargetCardInLibrary> {
public TargetCardInLibrary(final TargetCardInLibrary target) { public TargetCardInLibrary(final TargetCardInLibrary target) {
super(target); super(target);
this.cardLimit = target.cardLimit;
} }
@Override @Override
@ -73,27 +78,32 @@ public class TargetCardInLibrary extends TargetCard<TargetCardInLibrary> {
if (targetPlayer == null) { if (targetPlayer == null) {
targetPlayer = player; targetPlayer = player;
} }
List<Card> cards;
if (cardLimit == null) {
cards = targetPlayer.getLibrary().getCards(game);
} else {
int maxCards = Math.min(cardLimit.intValue(), targetPlayer.getLibrary().size());
cards = targetPlayer.getLibrary().getTopCards(game, maxCards);
}
while (!isChosen() && !doneChosing()) { while (!isChosen() && !doneChosing()) {
chosen = targets.size() >= minNumberOfTargets; chosen = targets.size() >= minNumberOfTargets;
if (!player.choose(outcome, new CardsImpl(Zone.LIBRARY, targetPlayer.getLibrary().getCards(game)), this, game)) { if (!player.choose(outcome, new CardsImpl(Zone.LIBRARY, cards), this, game)) {
return chosen; return chosen;
} }
chosen = targets.size() >= minNumberOfTargets; chosen = targets.size() >= minNumberOfTargets;
} }
// Issue 231
/*while (!doneChosing()) {
if (!player.choose(outcome, new CardsImpl(Zone.LIBRARY, player.getLibrary().getCards(game)), this, game)) {
break;
}
}*/
return chosen = true; return chosen = true;
} }
@Override @Override
public boolean canTarget(UUID id, Ability source, Game game) { public boolean canTarget(UUID id, Ability source, Game game) {
Card card = game.getPlayer(source.getControllerId()).getLibrary().getCard(id, game); Card card = game.getPlayer(source.getControllerId()).getLibrary().getCard(id, game);
if (card != null) if (card != null) {
return filter.match(card, game); return filter.match(card, game);
}
return false; return false;
} }
@ -106,4 +116,9 @@ public class TargetCardInLibrary extends TargetCard<TargetCardInLibrary> {
this.minNumberOfTargets = minNumberOfTargets; this.minNumberOfTargets = minNumberOfTargets;
} }
public void setCardLimit(Integer cardLimit) {
this.cardLimit = new Integer(cardLimit.intValue());
}
} }