From 1baa60e9de292a24ec14f1076cd8ec9b880277ec Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Thu, 28 Feb 2013 17:15:40 +0100 Subject: [PATCH] Added the possibility to limit library search to an amount of cards (Aven Mindcensor). --- Mage/src/mage/players/Library.java | 15 +++++++++ Mage/src/mage/players/PlayerImpl.java | 11 ++++++- .../target/common/TargetCardInLibrary.java | 31 ++++++++++++++----- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/Mage/src/mage/players/Library.java b/Mage/src/mage/players/Library.java index 41fd2c1530..090367b5d2 100644 --- a/Mage/src/mage/players/Library.java +++ b/Mage/src/mage/players/Library.java @@ -169,6 +169,21 @@ public class Library implements Serializable { return cards; } + public List getTopCards(Game game, int amount) { + List cards = new ArrayList(); + Iterator 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 getUniqueCards(Game game) { Map cards = new HashMap(); for (UUID cardId: library) { diff --git a/Mage/src/mage/players/PlayerImpl.java b/Mage/src/mage/players/PlayerImpl.java index 8f793935c7..ba86bb24e9 100644 --- a/Mage/src/mage/players/PlayerImpl.java +++ b/Mage/src/mage/players/PlayerImpl.java @@ -1177,7 +1177,16 @@ public abstract class PlayerImpl> implements Player, Ser //20091005 - 701.14c if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.SEARCH_LIBRARY, targetPlayerId, playerId))) { 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()) { newTarget.setMinNumberOfTargets(count); } diff --git a/Mage/src/mage/target/common/TargetCardInLibrary.java b/Mage/src/mage/target/common/TargetCardInLibrary.java index 7a3017033a..51e48ab78f 100644 --- a/Mage/src/mage/target/common/TargetCardInLibrary.java +++ b/Mage/src/mage/target/common/TargetCardInLibrary.java @@ -28,6 +28,8 @@ package mage.target.common; +import java.util.ArrayList; +import java.util.List; import mage.Constants.Outcome; import mage.Constants.Zone; import mage.abilities.Ability; @@ -46,6 +48,8 @@ import java.util.UUID; */ public class TargetCardInLibrary extends TargetCard { + private Integer cardLimit; + public TargetCardInLibrary() { this(1, 1, new FilterCard()); } @@ -64,6 +68,7 @@ public class TargetCardInLibrary extends TargetCard { public TargetCardInLibrary(final TargetCardInLibrary target) { super(target); + this.cardLimit = target.cardLimit; } @Override @@ -73,27 +78,32 @@ public class TargetCardInLibrary extends TargetCard { if (targetPlayer == null) { targetPlayer = player; } + + List 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()) { 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; } 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; } @Override public boolean canTarget(UUID id, Ability source, Game game) { Card card = game.getPlayer(source.getControllerId()).getLibrary().getCard(id, game); - if (card != null) + if (card != null) { return filter.match(card, game); + } return false; } @@ -106,4 +116,9 @@ public class TargetCardInLibrary extends TargetCard { this.minNumberOfTargets = minNumberOfTargets; } + public void setCardLimit(Integer cardLimit) { + this.cardLimit = new Integer(cardLimit.intValue()); + } + + }