From b04ac6786054ea4f3d9868f18b069d4859da198d Mon Sep 17 00:00:00 2001 From: Krazy Date: Wed, 2 Aug 2017 01:42:01 +0000 Subject: [PATCH] Make booster quest pool generation respect starting pool set selections --- .../main/java/forge/quest/BoosterUtils.java | 58 ++++-- .../java/forge/quest/QuestController.java | 13 +- .../main/java/forge/quest/QuestUtilCards.java | 8 +- .../forge/quest/data/GameFormatQuest.java | 194 +++++++++--------- 4 files changed, 136 insertions(+), 137 deletions(-) diff --git a/forge-gui/src/main/java/forge/quest/BoosterUtils.java b/forge-gui/src/main/java/forge/quest/BoosterUtils.java index 59c34089dfe..2212a7d01fe 100644 --- a/forge-gui/src/main/java/forge/quest/BoosterUtils.java +++ b/forge-gui/src/main/java/forge/quest/BoosterUtils.java @@ -32,10 +32,7 @@ import forge.util.MyRandom; import forge.util.PredicateString.StringOp; import org.apache.commons.lang3.StringUtils; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; +import java.util.*; import static forge.quest.QuestUtilCards.isLegalInQuestFormat; @@ -77,7 +74,7 @@ public final class BoosterUtils { /** * Gets the quest starter deck. * - * @param filter + * @param formatStartingPool * the filter * @param numCommons * the num common @@ -89,8 +86,8 @@ public final class BoosterUtils { * the starting pool preferences * @return the quest starter deck */ - public static List getQuestStarterDeck(final Predicate filter, final int numCommons, - final int numUncommons, final int numRares, final StartingPoolPreferences userPrefs, final QuestController questController) { + public static List getQuestStarterDeck(final GameFormat formatStartingPool, final int numCommons, + final int numUncommons, final int numRares, final StartingPoolPreferences userPrefs) { if (possibleColors.isEmpty()) { possibleColors.add(MagicColor.BLACK); @@ -101,12 +98,11 @@ public final class BoosterUtils { possibleColors.add(MagicColor.COLORLESS); } - final List cardPool = Lists.newArrayList(Iterables.filter(FModel.getMagicDb().getCommonCards().getAllCards(), filter)); final List cards = new ArrayList<>(); if (userPrefs != null && userPrefs.getPoolType() == StartingPoolPreferences.PoolType.BOOSTERS) { - for (InventoryItem inventoryItem : generateRandomBoosterPacks(userPrefs.getNumberOfBoosters(), questController)) { + for (InventoryItem inventoryItem : generateRandomBoosterPacks(userPrefs.getNumberOfBoosters(), formatStartingPool.editionLegalPredicate)) { cards.addAll(((BoosterPack) inventoryItem).getCards()); } @@ -114,6 +110,13 @@ public final class BoosterUtils { } + Predicate filter = Predicates.alwaysTrue(); + if (formatStartingPool != null) { + filter = formatStartingPool.getFilterPrinted(); + } + + final List cardPool = Lists.newArrayList(Iterables.filter(FModel.getMagicDb().getCommonCards().getAllCards(), filter)); + if (userPrefs != null && userPrefs.grantCompleteSet()) { for (PaperCard card : cardPool) { cards.add(card); @@ -149,24 +152,35 @@ public final class BoosterUtils { * @return A list containing the booster packs */ public static List generateRandomBoosterPacks(final int quantity, final QuestController questController) { + if (questController.getFormat() != null) { + return generateRandomBoosterPacks(quantity, isLegalInQuestFormat(questController.getFormat())); + } else { + final int rollD100 = MyRandom.getRandom().nextInt(100); + return generateRandomBoosterPacks(quantity, rollD100 < 40 ? filterT2booster : (rollD100 < 75 ? filterExtButT2 : filterNotExt)); + } + } + + /** + * Generates a number of booster packs from random editions using the specified edition predicate. + * @param quantity The number of booster packs to generate + * @param editionFilter The filter to use for picking booster editions + * @return A list containing the booster packs + */ + public static List generateRandomBoosterPacks(final int quantity, final Predicate editionFilter) { List output = new ArrayList<>(); + Predicate filter = Predicates.and(CardEdition.Predicates.CAN_MAKE_BOOSTER, editionFilter); + Iterable possibleEditions = Iterables.filter(FModel.getMagicDb().getEditions(), filter); + + if (!possibleEditions.iterator().hasNext()) { + System.err.println("No sets found in starting pool that can create boosters."); + return output; + } + for (int i = 0; i < quantity; i++) { - final int rollD100 = MyRandom.getRandom().nextInt(100); - - Predicate filter = rollD100 < 40 ? filterT2booster : (rollD100 < 75 ? filterExtButT2 : filterNotExt); - if (questController.getFormat() != null) { - filter = Predicates.and(CardEdition.Predicates.CAN_MAKE_BOOSTER, isLegalInQuestFormat(questController.getFormat())); - } - - Iterable rightEditions = Iterables.filter(FModel.getMagicDb().getEditions(), filter); - if (!rightEditions.iterator().hasNext()) { - continue; - } - - CardEdition edition = Aggregates.random(rightEditions); + CardEdition edition = Aggregates.random(possibleEditions); BoosterPack pack = BoosterPack.FN_FROM_SET.apply(edition); if (pack != null) { diff --git a/forge-gui/src/main/java/forge/quest/QuestController.java b/forge-gui/src/main/java/forge/quest/QuestController.java index 9017eaceba1..33a7d23c679 100644 --- a/forge-gui/src/main/java/forge/quest/QuestController.java +++ b/forge-gui/src/main/java/forge/quest/QuestController.java @@ -17,8 +17,6 @@ */ package forge.quest; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; import com.google.common.collect.Lists; import com.google.common.eventbus.Subscribe; import forge.card.CardEdition; @@ -27,7 +25,6 @@ import forge.deck.DeckGroup; import forge.game.GameFormat; import forge.game.event.GameEvent; import forge.game.event.GameEventMulligan; -import forge.item.PaperCard; import forge.item.PreconDeck; import forge.model.FModel; import forge.player.GamePlayerUtil; @@ -272,16 +269,12 @@ public class QuestController { if (startingCards != null) { this.myCards.addDeck(startingCards); - } - else { - Predicate filter = Predicates.alwaysTrue(); - if (formatStartingPool != null) { - filter = formatStartingPool.getFilterPrinted(); - } - this.myCards.setupNewGameCardPool(filter, difficulty, userPrefs, this); + } else { + this.myCards.setupNewGameCardPool(formatStartingPool, difficulty, userPrefs); } this.getAssets().setCredits(FModel.getQuestPreferences().getPrefInt(DifficultyPrefs.STARTING_CREDITS, difficulty)); + } /** diff --git a/forge-gui/src/main/java/forge/quest/QuestUtilCards.java b/forge-gui/src/main/java/forge/quest/QuestUtilCards.java index 2f67cec6c15..4b9597005be 100644 --- a/forge-gui/src/main/java/forge/quest/QuestUtilCards.java +++ b/forge-gui/src/main/java/forge/quest/QuestUtilCards.java @@ -298,20 +298,20 @@ public final class QuestUtilCards { /** * Setup new game card pool. * - * @param filter - * the filter + * @param formatStartingPool + * the starting pool format for the new quest * @param idxDifficulty * the idx difficulty * @param userPrefs * user preferences */ - public void setupNewGameCardPool(final Predicate filter, final int idxDifficulty, final StartingPoolPreferences userPrefs, final QuestController questController) { + public void setupNewGameCardPool(final GameFormat formatStartingPool, final int idxDifficulty, final StartingPoolPreferences userPrefs) { final int nC = questPreferences.getPrefInt(DifficultyPrefs.STARTING_COMMONS, idxDifficulty); final int nU = questPreferences.getPrefInt(DifficultyPrefs.STARTING_UNCOMMONS, idxDifficulty); final int nR = questPreferences.getPrefInt(DifficultyPrefs.STARTING_RARES, idxDifficulty); - addAllCards(BoosterUtils.getQuestStarterDeck(filter, nC, nU, nR, userPrefs, questController)); + addAllCards(BoosterUtils.getQuestStarterDeck(formatStartingPool, nC, nU, nR, userPrefs)); } diff --git a/forge-gui/src/main/java/forge/quest/data/GameFormatQuest.java b/forge-gui/src/main/java/forge/quest/data/GameFormatQuest.java index a44a2da24e2..012a21e6140 100644 --- a/forge-gui/src/main/java/forge/quest/data/GameFormatQuest.java +++ b/forge-gui/src/main/java/forge/quest/data/GameFormatQuest.java @@ -6,12 +6,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -35,119 +35,111 @@ import java.util.List; */ public final class GameFormatQuest extends GameFormat { - private final boolean allowUnlocks; - private int unlocksUsed = 0; + private final boolean allowUnlocks; + private int unlocksUsed = 0; - /** - * Instantiates a new game format based on two lists. - * - * @param newName - * String, the name - * @param setsToAllow - * List, these are the allowed sets - * @param cardsToBan - * List, these will be the banned cards - */ - public GameFormatQuest(final String newName, final List setsToAllow, final List cardsToBan) { - super(newName, setsToAllow, cardsToBan); - allowUnlocks = false; - } + /** + * Instantiates a new game format based on two lists. + * + * @param newName String, the name + * @param setsToAllow List, these are the allowed sets + * @param cardsToBan List, these will be the banned cards + */ + public GameFormatQuest(final String newName, final List setsToAllow, final List cardsToBan) { + super(newName, setsToAllow, cardsToBan); + allowUnlocks = false; + } - public GameFormatQuest(final String newName, final List setsToAllow, final List cardsToBan, boolean allowSetUnlocks) { - super(newName, setsToAllow, cardsToBan); - allowUnlocks = allowSetUnlocks; - } - /** - * Instantiates a new game format based on an existing format. - * - * @param toCopy - * an existing format - * @param allowSetUnlocks - */ - public GameFormatQuest(final GameFormat toCopy, boolean allowSetUnlocks) { - super(toCopy.getName(), toCopy.getAllowedSetCodes(), toCopy.getBannedCardNames(), toCopy.getRestrictedCards(), toCopy.getIndex()); - allowUnlocks = allowSetUnlocks; - } + public GameFormatQuest(final String newName, final List setsToAllow, final List cardsToBan, boolean allowSetUnlocks) { + super(newName, setsToAllow, cardsToBan); + allowUnlocks = allowSetUnlocks; + } + /** + * Instantiates a new game format based on an existing format. + * + * @param toCopy an existing format + * @param allowSetUnlocks + */ + public GameFormatQuest(final GameFormat toCopy, boolean allowSetUnlocks) { + super(toCopy.getName(), toCopy.getAllowedSetCodes(), toCopy.getBannedCardNames(), toCopy.getRestrictedCards(), toCopy.getIndex()); + allowUnlocks = allowSetUnlocks; + } - /** - * Get the list of excluded sets. - * - * @return unmodifiable list of excluded sets. - */ - public List getLockedSets() { + /** + * Get the list of excluded sets. + * + * @return unmodifiable list of excluded sets. + */ + public List getLockedSets() { - List exSets = new ArrayList(); - if (this.allowedSetCodes.isEmpty()) { - return exSets; - } + List exSets = new ArrayList(); + if (this.allowedSetCodes.isEmpty()) { + return exSets; + } - for (CardEdition ce : FModel.getMagicDb().getEditions()) { - if (!isSetLegal(ce.getCode())) { - exSets.add(ce.getCode()); - } - } - return exSets; - } + for (CardEdition ce : FModel.getMagicDb().getEditions()) { + if (!isSetLegal(ce.getCode())) { + exSets.add(ce.getCode()); + } + } + return exSets; + } - /** - * Add a set to allowed set codes. - * - * @param setCode String, set code. - */ - public void unlockSet(final String setCode) { - if (!canUnlockSets() || this.allowedSetCodes_ro.isEmpty() || this.allowedSetCodes_ro.contains(setCode)) { - return; - } - this.allowedSetCodes.add(setCode); - unlocksUsed++; - } + /** + * Add a set to allowed set codes. + * + * @param setCode String, set code. + */ + public void unlockSet(final String setCode) { + if (!canUnlockSets() || this.allowedSetCodes_ro.isEmpty() || this.allowedSetCodes_ro.contains(setCode)) { + return; + } + this.allowedSetCodes.add(setCode); + unlocksUsed++; + } - /** - * Checks if the current format contains sets with snow-land (horrible hack...). - * @return boolean, contains snow-land sets. - * - */ - public boolean hasSnowLands() { - return (this.isSetLegal("ICE") || this.isSetLegal("CSP")); - } + /** + * Checks if the current format contains sets with snow-land (horrible hack...). + * + * @return boolean, contains snow-land sets. + */ + public boolean hasSnowLands() { + return (this.isSetLegal("ICE") || this.isSetLegal("CSP")); + } - public boolean canUnlockSets() { - return allowUnlocks; - } + public boolean canUnlockSets() { + return allowUnlocks; + } - public int getUnlocksUsed() { - return unlocksUsed; - } + public int getUnlocksUsed() { + return unlocksUsed; + } + public abstract static class Predicates { + /** + * Checks if is legal in quest format. + * + * @param qFormat the format + * @return the predicate + */ + public static Predicate isLegalInFormatQuest(final GameFormatQuest qFormat) { + return new LegalInFormatQuest(qFormat); + } - /** - * The Class Predicates. - */ - public abstract static class Predicates { - /** - * Checks if is legal in quest format. - * - * @param qFormat the format - * @return the predicate - */ - public static Predicate isLegalInFormatQuest(final GameFormatQuest qFormat) { - return new LegalInFormatQuest(qFormat); - } + private static final class LegalInFormatQuest implements Predicate { + private final GameFormatQuest qFormat; - private static class LegalInFormatQuest implements Predicate { - private final GameFormatQuest qFormat; + private LegalInFormatQuest(final GameFormatQuest fmt) { + this.qFormat = fmt; + } - public LegalInFormatQuest(final GameFormatQuest fmt) { - this.qFormat = fmt; - } - - @Override - public boolean apply(final CardEdition subject) { - return this.qFormat.isSetLegal(subject.getCode()); - } - } - } + @Override + public boolean apply(final CardEdition subject) { + return this.qFormat.isSetLegal(subject.getCode()); + } + } + } }