mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 12:18:00 +00:00
Merge branch 'commanderDeckGenerator' into 'master'
Commander deck generator See merge request core-developers/forge!249
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package forge.deck;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import forge.card.CardRules;
|
||||
import forge.card.CardRulesPredicates;
|
||||
import forge.deck.generation.DeckGeneratorBase;
|
||||
import forge.deck.io.CardThemedMatrixIO;
|
||||
import forge.deck.io.DeckStorage;
|
||||
import forge.game.GameFormat;
|
||||
@@ -22,27 +25,42 @@ import java.util.*;
|
||||
*/
|
||||
public final class CardRelationMatrixGenerator {
|
||||
|
||||
public static HashMap<GameFormat,HashMap<String,List<Map.Entry<PaperCard,Integer>>>> cardPools = new HashMap<>();
|
||||
public static HashMap<String,HashMap<String,List<Map.Entry<PaperCard,Integer>>>> cardPools = new HashMap<>();
|
||||
|
||||
public static boolean initialize(){
|
||||
List<String> formatStrings = new ArrayList<>();
|
||||
formatStrings.add(FModel.getFormats().getStandard().getName());
|
||||
formatStrings.add(FModel.getFormats().getModern().getName());
|
||||
formatStrings.add(DeckFormat.Commander.toString());
|
||||
|
||||
public static void initialize(){
|
||||
HashMap<String,List<Map.Entry<PaperCard,Integer>>> standardMap = CardThemedMatrixIO.loadMatrix(FModel.getFormats().getStandard());
|
||||
HashMap<String,List<Map.Entry<PaperCard,Integer>>> modernMap = CardThemedMatrixIO.loadMatrix(FModel.getFormats().getModern());
|
||||
if(standardMap==null || modernMap==null){
|
||||
reInitialize();
|
||||
return;
|
||||
for (String formatString : formatStrings){
|
||||
if(!initializeFormat(formatString)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
cardPools.put(FModel.getFormats().getStandard(),standardMap);
|
||||
cardPools.put(FModel.getFormats().getModern(),modernMap);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void reInitialize(){
|
||||
cardPools.put(FModel.getFormats().getStandard(),initializeFormat(FModel.getFormats().getStandard()));
|
||||
cardPools.put(FModel.getFormats().getModern(),initializeFormat(FModel.getFormats().getModern()));
|
||||
for(GameFormat format:cardPools.keySet()){
|
||||
HashMap<String,List<Map.Entry<PaperCard,Integer>>> map = cardPools.get(format);
|
||||
CardThemedMatrixIO.saveMatrix(format,map);
|
||||
/** Try to load matrix .dat files, otherwise check for deck folders and build .dat, otherwise return false **/
|
||||
public static boolean initializeFormat(String format){
|
||||
HashMap<String,List<Map.Entry<PaperCard,Integer>>> formatMap = CardThemedMatrixIO.loadMatrix(format);
|
||||
if(formatMap==null) {
|
||||
if (CardThemedMatrixIO.getMatrixFolder(format).exists()) {
|
||||
if(format.equals(FModel.getFormats().getStandard().getName())){
|
||||
formatMap=initializeFormat(FModel.getFormats().getStandard());
|
||||
}else if(format.equals(FModel.getFormats().getModern().getName())){
|
||||
formatMap=initializeFormat(FModel.getFormats().getModern());
|
||||
}else{
|
||||
formatMap=initializeCommanderFormat();
|
||||
}
|
||||
CardThemedMatrixIO.saveMatrix(format, formatMap);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
cardPools.put(format, formatMap);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static HashMap<String,List<Map.Entry<PaperCard,Integer>>> initializeFormat(GameFormat format){
|
||||
@@ -58,8 +76,8 @@ public final class CardRelationMatrixGenerator {
|
||||
Map<String, Integer> cardIntegerMap = new HashMap<>();
|
||||
Map<Integer, PaperCard> integerCardMap = new HashMap<>();
|
||||
for (int i=0; i<cardList.size(); ++i){
|
||||
cardIntegerMap.put(cardList.get(i).getName(),i);
|
||||
integerCardMap.put(i,cardList.get(i));
|
||||
cardIntegerMap.put(cardList.get(i).getName(), i);
|
||||
integerCardMap.put(i, cardList.get(i));
|
||||
}
|
||||
|
||||
int[][] matrix = new int[cardList.size()][cardList.size()];
|
||||
@@ -71,7 +89,6 @@ public final class CardRelationMatrixGenerator {
|
||||
Predicates.compose(Predicates.not(CardRulesPredicates.Presets.IS_BASIC_LAND_NOT_WASTES), PaperCard.FN_GET_RULES))){
|
||||
if (!pairCard.getName().equals(card.getName())){
|
||||
try {
|
||||
|
||||
int old = matrix[cardIntegerMap.get(card.getName())][cardIntegerMap.get(pairCard.getName())];
|
||||
matrix[cardIntegerMap.get(card.getName())][cardIntegerMap.get(pairCard.getName())] = old + 1;
|
||||
}catch (NullPointerException ne){
|
||||
@@ -109,12 +126,107 @@ public final class CardRelationMatrixGenerator {
|
||||
if(excludeThisCard){
|
||||
continue;
|
||||
}
|
||||
cardPools.put(card.getName(),deckPool);
|
||||
cardPools.put(card.getName(), deckPool);
|
||||
}
|
||||
}
|
||||
return cardPools;
|
||||
}
|
||||
|
||||
public static HashMap<String,List<Map.Entry<PaperCard,Integer>>> initializeCommanderFormat(){
|
||||
|
||||
IStorage<Deck> decks = new StorageImmediatelySerialized<Deck>("Generator",
|
||||
new DeckStorage(new File(ForgeConstants.DECK_GEN_DIR,DeckFormat.Commander.toString()),
|
||||
ForgeConstants.DECK_GEN_DIR, false),
|
||||
true);
|
||||
|
||||
//get all cards
|
||||
final Iterable<PaperCard> cards = Iterables.filter(FModel.getMagicDb().getCommonCards().getUniqueCards()
|
||||
, Predicates.compose(Predicates.not(CardRulesPredicates.Presets.IS_BASIC_LAND_NOT_WASTES), PaperCard.FN_GET_RULES));
|
||||
List<PaperCard> cardList = Lists.newArrayList(cards);
|
||||
cardList.add(FModel.getMagicDb().getCommonCards().getCard("Wastes"));
|
||||
Map<String, Integer> cardIntegerMap = new HashMap<>();
|
||||
Map<Integer, PaperCard> integerCardMap = new HashMap<>();
|
||||
Map<String, Integer> legendIntegerMap = new HashMap<>();
|
||||
Map<Integer, PaperCard> integerLegendMap = new HashMap<>();
|
||||
//generate lookups for cards to link card names to matrix columns
|
||||
for (int i=0; i<cardList.size(); ++i){
|
||||
cardIntegerMap.put(cardList.get(i).getName(), i);
|
||||
integerCardMap.put(i, cardList.get(i));
|
||||
}
|
||||
|
||||
//filter to just legal commanders
|
||||
List<PaperCard> legends = Lists.newArrayList(Iterables.filter(cardList,Predicates.compose(
|
||||
new Predicate<CardRules>() {
|
||||
@Override
|
||||
public boolean apply(CardRules rules) {
|
||||
return DeckFormat.Commander.isLegalCommander(rules);
|
||||
}
|
||||
}, PaperCard.FN_GET_RULES)));
|
||||
|
||||
//generate lookups for legends to link commander names to matrix rows
|
||||
for (int i=0; i<legends.size(); ++i){
|
||||
legendIntegerMap.put(legends.get(i).getName(), i);
|
||||
integerLegendMap.put(i, legends.get(i));
|
||||
}
|
||||
int[][] matrix = new int[legends.size()][cardList.size()];
|
||||
|
||||
//loop through commanders and decks
|
||||
for (PaperCard legend:legends){
|
||||
for (Deck deck:decks){
|
||||
//if the deck has the commander
|
||||
if (deck.getCommanders().contains(legend)){
|
||||
//update the matrix by incrementing the connectivity count for each card in the deck
|
||||
updateLegendMatrix(deck, legend, cardIntegerMap, legendIntegerMap, matrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//convert the matrix into a map of pools for each commander
|
||||
HashMap<String,List<Map.Entry<PaperCard,Integer>>> cardPools = new HashMap<>();
|
||||
for (PaperCard card:legends){
|
||||
int col=legendIntegerMap.get(card.getName());
|
||||
int[] distances = matrix[col];
|
||||
int max = (Integer) Collections.max(Arrays.asList(ArrayUtils.toObject(distances)));
|
||||
if (max>0) {
|
||||
List<Map.Entry<PaperCard,Integer>> deckPool=new ArrayList<>();
|
||||
for(int k=0;k<cardList.size(); k++){
|
||||
if(matrix[col][k]>0){
|
||||
deckPool.add(new AbstractMap.SimpleEntry<PaperCard, Integer>(integerCardMap.get(k),matrix[col][k]));
|
||||
}
|
||||
}
|
||||
cardPools.put(card.getName(), deckPool);
|
||||
}
|
||||
}
|
||||
return cardPools;
|
||||
}
|
||||
|
||||
//update the matrix by incrementing the connectivity count for each card in the deck
|
||||
public static void updateLegendMatrix(Deck deck, PaperCard legend, Map<String, Integer> cardIntegerMap,
|
||||
Map<String, Integer> legendIntegerMap, int[][] matrix){
|
||||
for (PaperCard pairCard:Iterables.filter(deck.getMain().toFlatList(),
|
||||
Predicates.compose(Predicates.not(CardRulesPredicates.Presets.IS_BASIC_LAND_NOT_WASTES), PaperCard.FN_GET_RULES))){
|
||||
if (!pairCard.getName().equals(legend.getName())){
|
||||
try {
|
||||
int old = matrix[legendIntegerMap.get(legend.getName())][cardIntegerMap.get(pairCard.getName())];
|
||||
matrix[legendIntegerMap.get(legend.getName())][cardIntegerMap.get(pairCard.getName())] = old + 1;
|
||||
}catch (NullPointerException ne){
|
||||
//Todo: Not sure what was failing here
|
||||
ne.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//add partner commanders to matrix
|
||||
if(deck.getCommanders().size()>1){
|
||||
for(PaperCard partner:deck.getCommanders()){
|
||||
if(!partner.equals(legend)){
|
||||
int old = matrix[legendIntegerMap.get(legend.getName())][cardIntegerMap.get(partner.getName())];
|
||||
matrix[legendIntegerMap.get(legend.getName())][cardIntegerMap.get(partner.getName())] = old + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ArrayIndexComparator implements Comparator<Integer>
|
||||
{
|
||||
private final Integer[] array;
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package forge.deck;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import forge.card.CardEdition;
|
||||
import forge.game.GameFormat;
|
||||
import forge.item.PaperCard;
|
||||
import forge.model.FModel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -12,7 +16,7 @@ import java.util.List;
|
||||
public class CardThemedDeckGenerator extends DeckProxy implements Comparable<CardThemedDeckGenerator> {
|
||||
public static List<DeckProxy> getMatrixDecks(GameFormat format, boolean isForAi){
|
||||
final List<DeckProxy> decks = new ArrayList<DeckProxy>();
|
||||
for(String card: CardRelationMatrixGenerator.cardPools.get(format).keySet()) {
|
||||
for(String card: CardRelationMatrixGenerator.cardPools.get(format.getName()).keySet()) {
|
||||
decks.add(new CardThemedDeckGenerator(card, format, isForAi));
|
||||
}
|
||||
return decks;
|
||||
@@ -61,4 +65,15 @@ public class CardThemedDeckGenerator extends DeckProxy implements Comparable<Car
|
||||
public boolean isGeneratedDeck() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getImageKey(boolean altState) {
|
||||
/* Predicate<PaperCard> cardFilter = Predicates.and(format.getFilterPrinted(),PaperCard.Predicates.name(name));
|
||||
List<PaperCard> cards=FModel.getMagicDb().getCommonCards().getAllCards(cardFilter);
|
||||
return cards.get(cards.size()-1).getImageKey(altState);*/
|
||||
return FModel.getMagicDb().getCommonCards().getUniqueByName(name).getImageKey(altState);
|
||||
}
|
||||
|
||||
public PaperCard getPaperCard(){
|
||||
return FModel.getMagicDb().getCommonCards().getUniqueByName(name);
|
||||
}
|
||||
}
|
||||
|
||||
107
forge-gui/src/main/java/forge/deck/CommanderDeckGenerator.java
Normal file
107
forge-gui/src/main/java/forge/deck/CommanderDeckGenerator.java
Normal file
@@ -0,0 +1,107 @@
|
||||
package forge.deck;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.Iterables;
|
||||
import forge.card.CardEdition;
|
||||
import forge.card.CardRules;
|
||||
import forge.card.CardRulesPredicates;
|
||||
import forge.deck.generation.DeckGeneratorBase;
|
||||
import forge.deck.generation.IDeckGenPool;
|
||||
import forge.game.GameFormat;
|
||||
import forge.game.GameType;
|
||||
import forge.item.PaperCard;
|
||||
import forge.model.FModel;
|
||||
import forge.util.ItemPool;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by maustin on 09/05/2017.
|
||||
*/
|
||||
public class CommanderDeckGenerator extends DeckProxy implements Comparable<CommanderDeckGenerator> {
|
||||
public static List<DeckProxy> getCommanderDecks(DeckFormat format, boolean isForAi, boolean isCardGen){
|
||||
ItemPool uniqueCards;
|
||||
if(isCardGen){
|
||||
uniqueCards = new ItemPool<PaperCard>(PaperCard.class);
|
||||
Iterable<String> legendNames=CardRelationMatrixGenerator.cardPools.get(DeckFormat.Commander.toString()).keySet();
|
||||
for(String legendName:legendNames) {
|
||||
uniqueCards.add(FModel.getMagicDb().getCommonCards().getUniqueByName(legendName));
|
||||
}
|
||||
}else {
|
||||
uniqueCards = ItemPool.createFrom(FModel.getMagicDb().getCommonCards().getUniqueCards(), PaperCard.class);
|
||||
}
|
||||
Predicate<CardRules> canPlay = isForAi ? DeckGeneratorBase.AI_CAN_PLAY : DeckGeneratorBase.HUMAN_CAN_PLAY;
|
||||
@SuppressWarnings("unchecked")
|
||||
Iterable<PaperCard> legends = Iterables.filter(uniqueCards.toFlatList(), Predicates.compose(Predicates.and(
|
||||
new Predicate<CardRules>() {
|
||||
@Override
|
||||
public boolean apply(CardRules rules) {
|
||||
return format.isLegalCommander(rules);
|
||||
}
|
||||
},
|
||||
canPlay), PaperCard.FN_GET_RULES));
|
||||
final List<DeckProxy> decks = new ArrayList<DeckProxy>();
|
||||
for(PaperCard legend: legends) {
|
||||
decks.add(new CommanderDeckGenerator(legend, format, isForAi, isCardGen));
|
||||
}
|
||||
return decks;
|
||||
}
|
||||
|
||||
private final PaperCard legend;
|
||||
private final int index;
|
||||
private final DeckFormat format;
|
||||
private final boolean isForAi;
|
||||
private final boolean isCardgen;
|
||||
|
||||
|
||||
private CommanderDeckGenerator(PaperCard legend0, DeckFormat format0, boolean isForAi0, boolean isCardgen0) {
|
||||
super();
|
||||
legend = legend0;
|
||||
index = 0;
|
||||
isForAi=isForAi0;
|
||||
format=format0;
|
||||
isCardgen=isCardgen0;
|
||||
}
|
||||
|
||||
public CardEdition getEdition() {
|
||||
return CardEdition.UNKNOWN;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return legend.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return legend.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(final CommanderDeckGenerator d) {
|
||||
return this.getName().compareTo(d.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Deck getDeck() {
|
||||
|
||||
return DeckgenUtil.generateRandomCommanderDeck(legend, format,isForAi, isCardgen);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGeneratedDeck() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getImageKey(boolean altState) {
|
||||
return legend.getImageKey(altState);
|
||||
}
|
||||
|
||||
public PaperCard getPaperCard(){
|
||||
return legend;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ public enum DeckType {
|
||||
CUSTOM_DECK ("Custom User Decks"),
|
||||
CONSTRUCTED_DECK ("Constructed Decks"),
|
||||
COMMANDER_DECK ("Commander Decks"),
|
||||
RANDOM_COMMANDER_DECK ("Random Commander Decks"),
|
||||
RANDOM_CARDGEN_COMMANDER_DECK ("Random Commander Card-based Decks"),
|
||||
TINY_LEADERS_DECKS ("Tiny Leaders Decks"),
|
||||
SCHEME_DECKS ("Scheme Decks"),
|
||||
PLANAR_DECKS ("Planar Decks"),
|
||||
@@ -25,9 +27,10 @@ public enum DeckType {
|
||||
NET_COMMANDER_DECK ("Net Commander Decks");
|
||||
|
||||
public static DeckType[] ConstructedOptions;
|
||||
public static DeckType[] CommanderOptions;
|
||||
|
||||
static {
|
||||
if (!FModel.getPreferences().getPrefBoolean(ForgePreferences.FPref.LOAD_CARD_SCRIPTS_LAZILY)) {
|
||||
if (FModel.isdeckGenMatrixLoaded()) {
|
||||
ConstructedOptions = new DeckType[]{
|
||||
DeckType.CUSTOM_DECK,
|
||||
DeckType.PRECONSTRUCTED_DECK,
|
||||
@@ -55,6 +58,25 @@ public enum DeckType {
|
||||
};
|
||||
}
|
||||
}
|
||||
static {
|
||||
if (FModel.isdeckGenMatrixLoaded()) {
|
||||
CommanderOptions = new DeckType[]{
|
||||
DeckType.COMMANDER_DECK,
|
||||
DeckType.RANDOM_COMMANDER_DECK,
|
||||
DeckType.RANDOM_CARDGEN_COMMANDER_DECK,
|
||||
DeckType.RANDOM_DECK,
|
||||
DeckType.NET_COMMANDER_DECK
|
||||
};
|
||||
}else{
|
||||
CommanderOptions = new DeckType[]{
|
||||
DeckType.COMMANDER_DECK,
|
||||
DeckType.RANDOM_COMMANDER_DECK,
|
||||
DeckType.RANDOM_DECK,
|
||||
DeckType.NET_COMMANDER_DECK
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String value;
|
||||
private DeckType(final String value) {
|
||||
|
||||
@@ -16,6 +16,7 @@ import forge.game.GameFormat;
|
||||
import forge.game.GameType;
|
||||
import forge.item.PaperCard;
|
||||
import forge.itemmanager.IItemManager;
|
||||
import forge.limited.CardThemedCommanderDeckBuilder;
|
||||
import forge.limited.CardThemedDeckBuilder;
|
||||
import forge.model.FModel;
|
||||
import forge.properties.ForgePreferences.FPref;
|
||||
@@ -44,7 +45,7 @@ public class DeckgenUtil {
|
||||
public static Deck buildCardGenDeck(GameFormat format, boolean isForAI){
|
||||
Random random = new Random();
|
||||
try {
|
||||
List<String> keys = new ArrayList<>(CardRelationMatrixGenerator.cardPools.get(format).keySet());
|
||||
List<String> keys = new ArrayList<>(CardRelationMatrixGenerator.cardPools.get(format.getName()).keySet());
|
||||
String randomKey = keys.get( random.nextInt(keys.size()) );
|
||||
Predicate<PaperCard> cardFilter = Predicates.and(format.getFilterPrinted(),PaperCard.Predicates.name(randomKey));
|
||||
PaperCard keyCard = FModel.getMagicDb().getCommonCards().getAllCards(cardFilter).get(0);
|
||||
@@ -119,7 +120,7 @@ public class DeckgenUtil {
|
||||
*/
|
||||
public static Deck buildCardGenDeck(PaperCard card, GameFormat format, boolean isForAI){
|
||||
List<Map.Entry<PaperCard,Integer>> potentialCards = new ArrayList<>();
|
||||
potentialCards.addAll(CardRelationMatrixGenerator.cardPools.get(format).get(card.getName()));
|
||||
potentialCards.addAll(CardRelationMatrixGenerator.cardPools.get(format.getName()).get(card.getName()));
|
||||
Collections.sort(potentialCards,new CardDistanceComparator());
|
||||
Collections.reverse(potentialCards);
|
||||
//get second keycard
|
||||
@@ -146,7 +147,7 @@ public class DeckgenUtil {
|
||||
randMax=preSelectedCards.size();
|
||||
}
|
||||
PaperCard secondKeycard = preSelectedCards.get(r.nextInt(randMax));
|
||||
List<Map.Entry<PaperCard,Integer>> potentialSecondCards = CardRelationMatrixGenerator.cardPools.get(format).get(secondKeycard.getName());
|
||||
List<Map.Entry<PaperCard,Integer>> potentialSecondCards = CardRelationMatrixGenerator.cardPools.get(format.getName()).get(secondKeycard.getName());
|
||||
|
||||
//combine card distances from second key card and re-sort
|
||||
if(potentialSecondCards !=null && potentialSecondCards.size()>0) {
|
||||
@@ -486,7 +487,7 @@ public class DeckgenUtil {
|
||||
return res;
|
||||
}
|
||||
|
||||
/** Generate a 2-color Commander deck. */
|
||||
/** Generate a 2-5-color Commander deck. */
|
||||
public static Deck generateCommanderDeck(boolean forAi, GameType gameType) {
|
||||
final Deck deck;
|
||||
IDeckGenPool cardDb = FModel.getMagicDb().getCommonCards();
|
||||
@@ -504,7 +505,6 @@ public class DeckgenUtil {
|
||||
return format.isLegalCommander(rules);
|
||||
}
|
||||
},
|
||||
CardRulesPredicates.Presets.IS_MULTICOLOR,
|
||||
canPlay), PaperCard.FN_GET_RULES));
|
||||
|
||||
do {
|
||||
@@ -534,6 +534,104 @@ public class DeckgenUtil {
|
||||
return deck;
|
||||
}
|
||||
|
||||
/** Generate a ramdom Commander deck. */
|
||||
public static Deck generateRandomCommanderDeck(PaperCard commander, DeckFormat format, boolean forAi, boolean isCardGen) {
|
||||
final Deck deck;
|
||||
IDeckGenPool cardDb;
|
||||
DeckGeneratorBase gen = null;
|
||||
PaperCard selectedPartner=null;
|
||||
if(isCardGen){
|
||||
List<Map.Entry<PaperCard,Integer>> potentialCards = new ArrayList<>();
|
||||
potentialCards.addAll(CardRelationMatrixGenerator.cardPools.get(DeckFormat.Commander.toString()).get(commander.getName()));
|
||||
Random r = new Random();
|
||||
//Collections.shuffle(potentialCards, r);
|
||||
List<PaperCard> preSelectedCards = new ArrayList<>();
|
||||
for(Map.Entry<PaperCard,Integer> pair:potentialCards){
|
||||
if(format.isLegalCard(pair.getKey())) {
|
||||
preSelectedCards.add(pair.getKey());
|
||||
}
|
||||
}
|
||||
//check for partner commanders
|
||||
List<PaperCard> partners=new ArrayList<>();
|
||||
for(PaperCard c:preSelectedCards){
|
||||
if(c.getRules().canBePartnerCommander()){
|
||||
partners.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
if(partners.size()>0&&commander.getRules().canBePartnerCommander()){
|
||||
selectedPartner=partners.get(MyRandom.getRandom().nextInt(partners.size()));
|
||||
preSelectedCards.remove(selectedPartner);
|
||||
}
|
||||
//randomly remove cards
|
||||
int removeCount=0;
|
||||
int i=0;
|
||||
List<PaperCard> toRemove = new ArrayList<>();
|
||||
for(PaperCard c:preSelectedCards){
|
||||
if(!format.isLegalCard(c)){
|
||||
toRemove.add(c);
|
||||
removeCount++;
|
||||
}
|
||||
if(preSelectedCards.size()<75){
|
||||
break;
|
||||
}
|
||||
if(r.nextInt(100)>60+(15-(i/preSelectedCards.size())*preSelectedCards.size()) && removeCount<4 //randomly remove some cards - more likely as distance increases
|
||||
&&!c.getName().contains("Urza")&&!c.getName().contains("Wastes")){ //avoid breaking Tron decks
|
||||
toRemove.add(c);
|
||||
removeCount++;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
preSelectedCards.removeAll(toRemove);
|
||||
gen = new CardThemedCommanderDeckBuilder(commander, selectedPartner,preSelectedCards,forAi,format);
|
||||
}else{
|
||||
cardDb = FModel.getMagicDb().getCommonCards();
|
||||
ColorSet colorID;
|
||||
colorID = commander.getRules().getColorIdentity();
|
||||
List<String> comColors = new ArrayList<String>(2);
|
||||
if (colorID.hasWhite()) { comColors.add("White"); }
|
||||
if (colorID.hasBlue()) { comColors.add("Blue"); }
|
||||
if (colorID.hasBlack()) { comColors.add("Black"); }
|
||||
if (colorID.hasRed()) { comColors.add("Red"); }
|
||||
if (colorID.hasGreen()) { comColors.add("Green"); }
|
||||
|
||||
if(comColors.size()==1){
|
||||
gen = new DeckGeneratorMonoColor(cardDb, format, comColors.get(0));
|
||||
}else if(comColors.size()==2){
|
||||
gen = new DeckGenerator2Color(cardDb, format, comColors.get(0), comColors.get(1));
|
||||
}else if(comColors.size()==3){
|
||||
gen = new DeckGenerator3Color(cardDb, format, comColors.get(0), comColors.get(1), comColors.get(2));
|
||||
}else if(comColors.size()==4){
|
||||
gen = new DeckGenerator4Color(cardDb, format, comColors.get(0), comColors.get(1), comColors.get(2), comColors.get(3));
|
||||
}else if(comColors.size()==5){
|
||||
gen = new DeckGenerator5Color(cardDb, format);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
gen.setSingleton(true);
|
||||
gen.setUseArtifacts(!FModel.getPreferences().getPrefBoolean(FPref.DECKGEN_ARTIFACTS));
|
||||
CardPool cards = gen.getDeck(format.getMainRange().getMaximum(), forAi);
|
||||
|
||||
// After generating card lists, build deck.
|
||||
if(selectedPartner!=null){
|
||||
deck = new Deck("Generated " + format.toString() + " deck (" + commander.getName() +
|
||||
"--" + selectedPartner.getName() + ")");
|
||||
}else{
|
||||
deck = new Deck("Generated " + format.toString() + " deck (" + commander.getName() + ")");
|
||||
}
|
||||
deck.setDirectory("generated/commander");
|
||||
deck.getMain().addAll(cards);
|
||||
deck.getOrCreate(DeckSection.Commander).add(commander);
|
||||
if(selectedPartner!=null){
|
||||
deck.getOrCreate(DeckSection.Commander).add(selectedPartner);
|
||||
}
|
||||
|
||||
return deck;
|
||||
}
|
||||
|
||||
public static Map<ManaCostShard, Integer> suggestBasicLandCount(Deck d) {
|
||||
int W=0, U=0, R=0, B=0, G=0, total=0;
|
||||
List<PaperCard> cards = d.getOrCreate(DeckSection.Main).toFlatList();
|
||||
|
||||
@@ -22,7 +22,7 @@ public class CardThemedMatrixIO {
|
||||
/** suffix for all gauntlet data files */
|
||||
public static final String SUFFIX_DATA = ".dat";
|
||||
|
||||
public static void saveMatrix(GameFormat format, HashMap<String,List<Map.Entry<PaperCard,Integer>>> map){
|
||||
public static void saveMatrix(String format, HashMap<String,List<Map.Entry<PaperCard,Integer>>> map){
|
||||
File file = getMatrixFile(format);
|
||||
ObjectOutputStream s = null;
|
||||
try {
|
||||
@@ -43,7 +43,7 @@ public class CardThemedMatrixIO {
|
||||
}
|
||||
}
|
||||
|
||||
public static HashMap<String,List<Map.Entry<PaperCard,Integer>>> loadMatrix(GameFormat format){
|
||||
public static HashMap<String,List<Map.Entry<PaperCard,Integer>>> loadMatrix(String format){
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(getMatrixFile(format));
|
||||
ObjectInputStream s = new ObjectInputStream(fin);
|
||||
@@ -61,6 +61,10 @@ public class CardThemedMatrixIO {
|
||||
return new File(ForgeConstants.DECK_GEN_DIR, name + SUFFIX_DATA);
|
||||
}
|
||||
|
||||
public static File getMatrixFolder(final String name) {
|
||||
return new File(ForgeConstants.DECK_GEN_DIR, name);
|
||||
}
|
||||
|
||||
public static File getMatrixFile(final GameFormat gf) {
|
||||
return getMatrixFile(gf.getName());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,781 @@
|
||||
package forge.limited;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import forge.card.*;
|
||||
import forge.card.mana.ManaCost;
|
||||
import forge.card.mana.ManaCostShard;
|
||||
import forge.deck.CardPool;
|
||||
import forge.deck.Deck;
|
||||
import forge.deck.DeckFormat;
|
||||
import forge.deck.DeckSection;
|
||||
import forge.deck.generation.DeckGenPool;
|
||||
import forge.deck.generation.DeckGeneratorBase;
|
||||
import forge.item.IPaperCard;
|
||||
import forge.item.PaperCard;
|
||||
import forge.model.FModel;
|
||||
import forge.util.MyRandom;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Limited format deck.
|
||||
*/
|
||||
public class CardThemedCommanderDeckBuilder extends DeckGeneratorBase {
|
||||
@Override
|
||||
protected final float getLandPercentage() {
|
||||
return 0.44f;
|
||||
}
|
||||
@Override
|
||||
protected final float getCreaturePercentage() {
|
||||
return 0.33f;
|
||||
}
|
||||
@Override
|
||||
protected final float getSpellPercentage() {
|
||||
return 0.23f;
|
||||
}
|
||||
|
||||
protected int targetSize;
|
||||
protected int numSpellsNeeded;
|
||||
protected int numCreaturesToStart;
|
||||
protected int landsNeeded;
|
||||
|
||||
protected final PaperCard commanderCard;
|
||||
protected final PaperCard partnerCard;
|
||||
|
||||
protected Predicate<CardRules> hasColor;
|
||||
protected final List<PaperCard> availableList;
|
||||
protected final List<PaperCard> aiPlayables;
|
||||
protected final List<PaperCard> deckList = new ArrayList<>();
|
||||
protected final List<String> setsWithBasicLands = new ArrayList<>();
|
||||
protected List<PaperCard> rankedColorList;
|
||||
|
||||
// Views for aiPlayable
|
||||
protected Iterable<PaperCard> onColorCreatures;
|
||||
protected Iterable<PaperCard> onColorNonCreatures;
|
||||
|
||||
protected static final boolean logToConsole = false;
|
||||
protected static final boolean logColorsToConsole = false;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Constructor.
|
||||
*
|
||||
* @param dList
|
||||
* Cards to build the deck from.
|
||||
*/
|
||||
public CardThemedCommanderDeckBuilder(PaperCard commanderCard0,PaperCard partner0, final List<PaperCard> dList, boolean isForAI, DeckFormat format) {
|
||||
super(FModel.getMagicDb().getCommonCards(), format);
|
||||
this.availableList = dList;
|
||||
commanderCard = commanderCard0;
|
||||
partnerCard = partner0;
|
||||
// remove Unplayables
|
||||
if(isForAI) {
|
||||
final Iterable<PaperCard> playables = Iterables.filter(availableList,
|
||||
Predicates.compose(CardRulesPredicates.IS_KEPT_IN_AI_DECKS, PaperCard.FN_GET_RULES));
|
||||
this.aiPlayables = Lists.newArrayList(playables);
|
||||
}else{
|
||||
this.aiPlayables = Lists.newArrayList(availableList);
|
||||
}
|
||||
this.availableList.removeAll(aiPlayables);
|
||||
targetSize=format.getMainRange().getMinimum();
|
||||
colors = commanderCard.getRules().getColorIdentity();
|
||||
colors = ColorSet.fromMask(colors.getColor() | commanderCard.getRules().getColorIdentity().getColor());
|
||||
if(partnerCard!=null) {
|
||||
colors = ColorSet.fromMask(colors.getColor() | partnerCard.getRules().getColorIdentity().getColor());
|
||||
targetSize--;
|
||||
}
|
||||
numSpellsNeeded = ((Double)Math.floor(targetSize*(getCreaturePercentage()+getSpellPercentage()))).intValue();
|
||||
numCreaturesToStart = ((Double)Math.ceil(targetSize*(getCreaturePercentage()))).intValue();
|
||||
landsNeeded = ((Double)Math.ceil(targetSize*(getLandPercentage()))).intValue();;
|
||||
if (logColorsToConsole) {
|
||||
System.out.println(commanderCard.getName());
|
||||
System.out.println("Pre Colors: " + colors.toEnumSet().toString());
|
||||
}
|
||||
findBasicLandSets();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public CardPool getDeck(final int size, final boolean forAi) {
|
||||
return buildDeck().getMain();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* buildDeck.
|
||||
* </p>
|
||||
*
|
||||
* @return the new Deck.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public Deck buildDeck() {
|
||||
// 1. Prepare
|
||||
hasColor = Predicates.or(new MatchColorIdentity(colors), COLORLESS_CARDS);
|
||||
if (logColorsToConsole) {
|
||||
System.out.println(commanderCard.getName());
|
||||
System.out.println("Colors: " + colors.toEnumSet().toString());
|
||||
}
|
||||
Iterable<PaperCard> colorList = Iterables.filter(aiPlayables,
|
||||
Predicates.compose(hasColor, PaperCard.FN_GET_RULES));
|
||||
rankedColorList = Lists.newArrayList(colorList);
|
||||
onColorCreatures = Iterables.filter(rankedColorList,
|
||||
Predicates.compose(CardRulesPredicates.Presets.IS_CREATURE, PaperCard.FN_GET_RULES));
|
||||
onColorNonCreatures = Iterables.filter(rankedColorList,
|
||||
Predicates.compose(CardRulesPredicates.Presets.IS_NON_CREATURE_SPELL, PaperCard.FN_GET_RULES));
|
||||
// Guava iterables do not copy the collection contents, instead they act
|
||||
// as filters and iterate over _source_ collection each time. So even if
|
||||
// aiPlayable has changed, there is no need to create a new iterable.
|
||||
|
||||
// 2. Add any planeswalkers - removed - treat as non-creature
|
||||
|
||||
// 3. Add creatures, trying to follow mana curve
|
||||
|
||||
addManaCurveCards(onColorCreatures, numCreaturesToStart, "Creatures");
|
||||
if (logToConsole) {
|
||||
System.out.println("Post Creatures : " + deckList.size());
|
||||
}
|
||||
|
||||
// 4.Try to fill up to num needed with on-color non-creature cards
|
||||
addManaCurveCards(onColorNonCreatures, numSpellsNeeded - deckList.size(), "Spells");
|
||||
if (logToConsole) {
|
||||
System.out.println("Post Spells : " + deckList.size());
|
||||
}
|
||||
|
||||
// 5.If we couldn't get enough, try to fill up with on-color cards
|
||||
addCards(rankedColorList, numSpellsNeeded - deckList.size());
|
||||
if (logToConsole) {
|
||||
System.out.println("Post more creatures : " + deckList.size());
|
||||
}
|
||||
|
||||
// 6. If there are still on-color cards, and the average cmc is low, add
|
||||
// extras.
|
||||
double avCMC=getAverageCMC(deckList);
|
||||
int maxCMC=getMaxCMC(deckList);
|
||||
if (deckList.size() == numSpellsNeeded && avCMC < 4) {
|
||||
addLowCMCCard();
|
||||
if(targetSize>60){
|
||||
addLowCMCCard();
|
||||
}
|
||||
}
|
||||
if (deckList.size() >= numSpellsNeeded && avCMC < 3 && maxCMC<6) {
|
||||
addLowCMCCard();
|
||||
}
|
||||
if (deckList.size() >= numSpellsNeeded && avCMC < 2.5 && maxCMC<5) {
|
||||
addLowCMCCard();
|
||||
if(targetSize>60){
|
||||
addLowCMCCard();
|
||||
}
|
||||
}
|
||||
if (logToConsole) {
|
||||
System.out.println("Post lowcoc : " + deckList.size());
|
||||
}
|
||||
|
||||
/* // 7. If not enough cards yet, try to add a third color,
|
||||
// to try and avoid adding purely random cards.
|
||||
addThirdColorCards(numSpellsNeeded - deckList.size());
|
||||
if (logColorsToConsole) {
|
||||
System.out.println("Post 3rd colour : " + deckList.size());
|
||||
System.out.println("Colors: " + colors.toEnumSet().toString());
|
||||
}*/
|
||||
|
||||
// 8. Check for DeckNeeds cards.
|
||||
//checkRemRandomDeckCards(); - no need
|
||||
|
||||
// 9. If there are still less than 22 non-land cards add off-color
|
||||
// cards. This should be avoided.
|
||||
addRandomCards(numSpellsNeeded - deckList.size());
|
||||
if (logToConsole) {
|
||||
System.out.println("Post Randoms : " + deckList.size());
|
||||
}
|
||||
|
||||
List<String> duals = getDualLandList();
|
||||
addNonBasicLands();
|
||||
if (logToConsole) {
|
||||
System.out.println("Post Nonbasic lands : " + deckList.size());
|
||||
}
|
||||
|
||||
checkEvolvingWilds();
|
||||
|
||||
// 11. Fill up with basic lands.
|
||||
final int[] clrCnts = calculateLandNeeds();
|
||||
|
||||
// Add dual lands
|
||||
if (clrCnts.length>1) {
|
||||
|
||||
for (String s : duals) {
|
||||
this.cardCounts.put(s, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (landsNeeded > 0) {
|
||||
addLands(clrCnts);
|
||||
}
|
||||
if (logToConsole) {
|
||||
System.out.println("Post Lands : " + deckList.size());
|
||||
}
|
||||
if (commanderCard.getRules().getColorIdentity().isColorless()&&landsNeeded>0){
|
||||
// 10. Add non-basic lands that were drafted.
|
||||
addWastesIfRequired();
|
||||
}
|
||||
fixDeckSize();
|
||||
if (logToConsole) {
|
||||
System.out.println("Post Size fix : " + deckList.size());
|
||||
}
|
||||
|
||||
//Create Deck
|
||||
final Deck result = new Deck(generateName());
|
||||
result.getMain().add(deckList);
|
||||
|
||||
//Add remaining non-land colour matching cards to sideboard
|
||||
final CardPool cp = result.getOrCreate(DeckSection.Sideboard);
|
||||
Iterable<PaperCard> potentialSideboard = Iterables.filter(aiPlayables,
|
||||
Predicates.and(Predicates.compose(hasColor, PaperCard.FN_GET_RULES),
|
||||
Predicates.compose(CardRulesPredicates.Presets.IS_NON_LAND, PaperCard.FN_GET_RULES)));
|
||||
int i=0;
|
||||
while(i<15 && potentialSideboard.iterator().hasNext()){
|
||||
PaperCard sbCard = potentialSideboard.iterator().next();
|
||||
cp.add(sbCard);
|
||||
aiPlayables.remove(sbCard);
|
||||
rankedColorList.remove(sbCard);
|
||||
|
||||
|
||||
++i;
|
||||
}
|
||||
if (logToConsole) {
|
||||
debugFinalDeck();
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* If evolving wilds is in the deck and there are fewer than 4 spaces for basic lands - remove evolving wilds
|
||||
*/
|
||||
protected void checkEvolvingWilds(){
|
||||
List<PaperCard> evolvingWilds = Lists.newArrayList(Iterables.filter(deckList,PaperCard.Predicates.name("Evolving Wilds")));
|
||||
if((evolvingWilds.size()>0 && landsNeeded<4 ) || colors.countColors()<2){
|
||||
deckList.removeAll(evolvingWilds);
|
||||
landsNeeded=landsNeeded+evolvingWilds.size();
|
||||
aiPlayables.addAll(evolvingWilds);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void addLowCMCCard(){
|
||||
final Iterable<PaperCard> nonLands = Iterables.filter(aiPlayables,
|
||||
Predicates.compose(CardRulesPredicates.Presets.IS_NON_LAND, PaperCard.FN_GET_RULES));
|
||||
final PaperCard card = Iterables.getFirst(nonLands, null);
|
||||
if (card != null) {
|
||||
deckList.add(card);
|
||||
aiPlayables.remove(card);
|
||||
rankedColorList.remove(card);
|
||||
|
||||
landsNeeded--;
|
||||
if (logToConsole) {
|
||||
System.out.println("Low CMC: " + card.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the basic land pool
|
||||
* @param edition
|
||||
* @return
|
||||
*/
|
||||
protected boolean setBasicLandPool(String edition){
|
||||
Predicate<PaperCard> isSetBasicLand;
|
||||
if (edition !=null){
|
||||
isSetBasicLand = Predicates.and(IPaperCard.Predicates.printedInSet(edition),
|
||||
Predicates.compose(CardRulesPredicates.Presets.IS_BASIC_LAND, PaperCard.FN_GET_RULES));
|
||||
}else{
|
||||
isSetBasicLand = Predicates.compose(CardRulesPredicates.Presets.IS_BASIC_LAND, PaperCard.FN_GET_RULES);
|
||||
}
|
||||
|
||||
landPool = new DeckGenPool(format.getCardPool(fullCardDB).getAllCards(isSetBasicLand));
|
||||
return landPool.contains("Plains");
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a descriptive name.
|
||||
*
|
||||
* @return name
|
||||
*/
|
||||
private String generateName() {
|
||||
return commanderCard.getName() +" based commander deck";
|
||||
}
|
||||
|
||||
/**
|
||||
* Print out listing of all cards for debugging.
|
||||
*/
|
||||
private void debugFinalDeck() {
|
||||
int i = 0;
|
||||
System.out.println("DECK");
|
||||
for (final PaperCard c : deckList) {
|
||||
i++;
|
||||
System.out.println(i + ". " + c.toString() + ": " + c.getRules().getManaCost().toString());
|
||||
}
|
||||
i = 0;
|
||||
System.out.println("NOT PLAYABLE");
|
||||
for (final PaperCard c : availableList) {
|
||||
i++;
|
||||
System.out.println(i + ". " + c.toString() + ": " + c.getRules().getManaCost().toString());
|
||||
}
|
||||
i = 0;
|
||||
System.out.println("NOT PICKED");
|
||||
for (final PaperCard c : aiPlayables) {
|
||||
i++;
|
||||
System.out.println(i + ". " + c.toString() + ": " + c.getRules().getManaCost().toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the deck does not have 40 cards, fix it. This method should not be
|
||||
* called if the stuff above it is working correctly.
|
||||
*
|
||||
*/
|
||||
private void fixDeckSize() {
|
||||
while (deckList.size() > targetSize) {
|
||||
if (logToConsole) {
|
||||
System.out.println("WARNING: Fixing deck size, currently " + deckList.size() + " cards.");
|
||||
}
|
||||
final PaperCard c = deckList.get(MyRandom.getRandom().nextInt(deckList.size() - 1));
|
||||
deckList.remove(c);
|
||||
aiPlayables.add(c);
|
||||
if (logToConsole) {
|
||||
System.out.println(" - Removed " + c.getName() + " randomly.");
|
||||
}
|
||||
}
|
||||
if(deckList.size()==targetSize){
|
||||
return;
|
||||
}
|
||||
|
||||
Predicate<PaperCard> possibleFromFullPool = new Predicate<PaperCard>() {
|
||||
@Override
|
||||
public boolean apply(PaperCard card) {
|
||||
return format.isLegalCard(card)
|
||||
&&!card.getRules().getManaCost().isPureGeneric()
|
||||
&& colors.containsAllColorsFrom(card.getRules().getColorIdentity().getColor())
|
||||
&& !deckList.contains(card)
|
||||
&&!card.getRules().getAiHints().getRemAIDecks()
|
||||
&&!card.getRules().getAiHints().getRemRandomDecks()
|
||||
&&!card.getRules().getMainPart().getType().isLand();
|
||||
}
|
||||
};
|
||||
List<PaperCard> randomPool = Lists.newArrayList(pool.getAllCards(possibleFromFullPool));
|
||||
Collections.shuffle(randomPool,new Random());
|
||||
Iterator<PaperCard> iRandomPool=randomPool.iterator();
|
||||
while (deckList.size() < targetSize) {
|
||||
if (logToConsole) {
|
||||
System.out.println("WARNING: Fixing deck size, currently " + deckList.size() + " cards.");
|
||||
}
|
||||
PaperCard randomCard = iRandomPool.next();
|
||||
deckList.add(randomCard);
|
||||
if (logToConsole) {
|
||||
System.out.println(" - Added " + randomCard.getName() + " randomly.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the sets that have basic lands for the available cards.
|
||||
*/
|
||||
private void findBasicLandSets() {
|
||||
final Set<String> sets = new HashSet<>();
|
||||
for (final PaperCard cp : aiPlayables) {
|
||||
final CardEdition ee = FModel.getMagicDb().getEditions().get(cp.getEdition());
|
||||
if( !sets.contains(cp.getEdition()) && CardEdition.Predicates.hasBasicLands.apply(ee)) {
|
||||
sets.add(cp.getEdition());
|
||||
}
|
||||
}
|
||||
setsWithBasicLands.addAll(sets);
|
||||
if (setsWithBasicLands.isEmpty()) {
|
||||
setsWithBasicLands.add("BFZ");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add lands to fulfill the given color counts.
|
||||
*
|
||||
* @param clrCnts
|
||||
* counts of lands needed, by color
|
||||
*/
|
||||
private void addLands(final int[] clrCnts) {
|
||||
// basic lands that are available in the deck
|
||||
final Iterable<PaperCard> basicLands = Iterables.filter(aiPlayables, Predicates.compose(CardRulesPredicates.Presets.IS_BASIC_LAND, PaperCard.FN_GET_RULES));
|
||||
final Set<PaperCard> snowLands = new HashSet<PaperCard>();
|
||||
|
||||
// total of all ClrCnts
|
||||
int totalColor = 0;
|
||||
int numColors = 0;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
totalColor += clrCnts[i];
|
||||
if (clrCnts[i] > 0) {
|
||||
numColors++;
|
||||
}
|
||||
}
|
||||
/*if (totalColor == 0) {
|
||||
for (int j = 0; j < nLand; j++) {
|
||||
deckList.add(getBasicLand(i));
|
||||
}
|
||||
}*/
|
||||
|
||||
// do not update landsNeeded until after the loop, because the
|
||||
// calculation involves landsNeeded
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (clrCnts[i] > 0) {
|
||||
// calculate number of lands for each color
|
||||
float p = (float) clrCnts[i] / (float) totalColor;
|
||||
if (numColors == 2) {
|
||||
// In the normal two-color case, constrain to within 40% and 60% so that the AI
|
||||
// doesn't put too few lands of the lesser color, risking getting screwed on that color.
|
||||
// Don't do this for the odd case where a third color had to be added to the deck.
|
||||
p = Math.min(Math.max(p, 0.4f), 0.6f);
|
||||
}
|
||||
int nLand = Math.round(landsNeeded * p); // desired truncation to int
|
||||
if (logToConsole) {
|
||||
System.out.printf("Basics[%s]: %d/%d = %f%% = %d cards%n", MagicColor.Constant.BASIC_LANDS.get(i), clrCnts[i], totalColor, 100*p, nLand);
|
||||
}
|
||||
|
||||
// if appropriate snow-covered lands are available, add them
|
||||
for (final PaperCard cp : basicLands) {
|
||||
if (cp.getName().equals(MagicColor.Constant.SNOW_LANDS.get(i))) {
|
||||
snowLands.add(cp);
|
||||
nLand--;
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < nLand; j++) {
|
||||
deckList.add(getBasicLand(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A common problem at this point is that p in the above loop was exactly 1/2,
|
||||
// and nLand rounded up for both colors, so that one too many lands was added.
|
||||
// So if the deck size is > 60, remove the last land added.
|
||||
// Otherwise, the fixDeckSize() method would remove random cards.
|
||||
while (deckList.size() > targetSize) {
|
||||
deckList.remove(deckList.size() - 1);
|
||||
}
|
||||
|
||||
deckList.addAll(snowLands);
|
||||
aiPlayables.removeAll(snowLands);
|
||||
rankedColorList.remove(snowLands);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get basic land.
|
||||
*
|
||||
* @param basicLand
|
||||
* the set to take basic lands from (pass 'null' for random).
|
||||
* @return card
|
||||
*/
|
||||
private PaperCard getBasicLand(final int basicLand) {
|
||||
String set;
|
||||
if (setsWithBasicLands.size() > 1) {
|
||||
set = setsWithBasicLands.get(MyRandom.getRandom().nextInt(setsWithBasicLands.size() - 1));
|
||||
} else {
|
||||
set = setsWithBasicLands.get(0);
|
||||
}
|
||||
return FModel.getMagicDb().getCommonCards().getCard(MagicColor.Constant.BASIC_LANDS.get(basicLand), set);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only adds wastes if present in the card pool but if present adds them all
|
||||
*/
|
||||
private void addWastesIfRequired(){
|
||||
Iterable<PaperCard> wastes = Iterables.filter(aiPlayables,PaperCard.Predicates.name("Wastes"));
|
||||
if(wastes.iterator().hasNext()){
|
||||
PaperCard waste = wastes.iterator().next();
|
||||
while(landsNeeded>0) {
|
||||
deckList.add(waste);
|
||||
landsNeeded--;
|
||||
}
|
||||
aiPlayables.remove(waste);
|
||||
rankedColorList.remove(waste);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to optimize basic land counts according to color representation.
|
||||
* Only consider colors that are supposed to be in the deck. It's not worth
|
||||
* putting one land in for that random off-color card we had to stick in at
|
||||
* the end...
|
||||
*
|
||||
* @return CCnt
|
||||
*/
|
||||
private int[] calculateLandNeeds() {
|
||||
final int[] clrCnts = { 0,0,0,0,0 };
|
||||
// count each card color using mana costs
|
||||
for (final PaperCard cp : deckList) {
|
||||
final ManaCost mc = cp.getRules().getManaCost();
|
||||
|
||||
// count each mana symbol in the mana cost
|
||||
for (final ManaCostShard shard : mc) {
|
||||
for ( int i = 0 ; i < MagicColor.WUBRG.length; i++ ) {
|
||||
final byte c = MagicColor.WUBRG[i];
|
||||
|
||||
if ( shard.canBePaidWithManaOfColor(c) && colors.hasAnyColor(c)) {
|
||||
clrCnts[i]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return clrCnts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add non-basic lands to the deck.
|
||||
*/
|
||||
private void addNonBasicLands() {
|
||||
final Iterable<PaperCard> lands = Iterables.filter(aiPlayables,
|
||||
Predicates.compose(CardRulesPredicates.Presets.IS_NONBASIC_LAND, PaperCard.FN_GET_RULES));
|
||||
List<PaperCard> landsToAdd = new ArrayList<>();
|
||||
int minBasics;//Keep a minimum number of basics to ensure playable decks
|
||||
if(colors.isMonoColor()){
|
||||
minBasics=Math.round((r.nextInt(15)+6)*targetSize/60);
|
||||
}else{
|
||||
minBasics=Math.round((r.nextInt(8)+6)*targetSize/60);
|
||||
}
|
||||
|
||||
|
||||
for (final PaperCard card : lands) {
|
||||
if (landsNeeded > minBasics) {
|
||||
// Throw out any dual-lands for the wrong colors. Assume
|
||||
// everything else is either
|
||||
// (a) dual-land of the correct two colors, or
|
||||
// (b) a land that generates colorless mana and has some other
|
||||
// beneficial effect.
|
||||
if (!card.getRules().getColorIdentity().isColorless() && card.getRules().getColorIdentity().getSharedColors(colors).countColors()==0){
|
||||
//skip as does not match colours
|
||||
if (logToConsole) {
|
||||
System.out.println("Excluding NonBasicLand: " + card.getName());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!inverseDLands.contains(card.getName())&&!dLands.contains(card.getName())&&r.nextInt(100)<90) {
|
||||
landsToAdd.add(card);
|
||||
landsNeeded--;
|
||||
if (logToConsole) {
|
||||
System.out.println("NonBasicLand[" + landsNeeded + "]:" + card.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
deckList.addAll(landsToAdd);
|
||||
aiPlayables.removeAll(landsToAdd);
|
||||
rankedColorList.removeAll(landsToAdd);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add random cards to the deck.
|
||||
*
|
||||
* @param num
|
||||
* number to add
|
||||
*/
|
||||
private void addRandomCards(int num) {
|
||||
Predicate<PaperCard> possibleFromFullPool = new Predicate<PaperCard>() {
|
||||
@Override
|
||||
public boolean apply(PaperCard card) {
|
||||
return format.isLegalCard(card)
|
||||
&&!card.getRules().getManaCost().isPureGeneric()
|
||||
&& colors.containsAllColorsFrom(card.getRules().getColorIdentity().getColor())
|
||||
&& !deckList.contains(card)
|
||||
&&!card.getRules().getAiHints().getRemAIDecks()
|
||||
&&!card.getRules().getAiHints().getRemRandomDecks()
|
||||
&&!card.getRules().getMainPart().getType().isLand();
|
||||
}
|
||||
};
|
||||
List<PaperCard> randomPool = Lists.newArrayList(pool.getAllCards(possibleFromFullPool));
|
||||
Collections.shuffle(randomPool,new Random());
|
||||
Iterator<PaperCard> iRandomPool=randomPool.iterator();
|
||||
for(int i=0;i<num;++i){
|
||||
PaperCard randomCard=iRandomPool.next();
|
||||
deckList.add(randomCard);
|
||||
if(logToConsole) {
|
||||
System.out.println("Random Card[" + i + "]:" + randomCard.getName() + " (" + randomCard.getRules().getManaCost() + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add creatures to the deck.
|
||||
*
|
||||
* @param cards
|
||||
* cards to choose from
|
||||
* @param num
|
||||
* number to add
|
||||
*/
|
||||
private void addCards(final Iterable<PaperCard> cards, int num) {
|
||||
List<PaperCard> cardsToAdd = new ArrayList<>();
|
||||
for (final PaperCard card : cards) {
|
||||
if(card.getRules().getMainPart().getType().isLand()){
|
||||
continue;
|
||||
}
|
||||
if (num +1 > 0) {
|
||||
cardsToAdd.add(card);
|
||||
if (logToConsole) {
|
||||
System.out.println("Extra needed[" + num + "]:" + card.getName() + " (" + card.getRules().getManaCost() + ")");
|
||||
}
|
||||
num--;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
deckList.addAll(cardsToAdd);
|
||||
aiPlayables.removeAll(cardsToAdd);
|
||||
rankedColorList.removeAll(cardsToAdd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add cards to the deck, trying to follow some mana curve. Trying to
|
||||
* have generous limits at each cost, but perhaps still too strict. But
|
||||
* we're trying to prevent the AI from adding everything at a single cost.
|
||||
*
|
||||
* @param creatures
|
||||
* cards to choose from
|
||||
* @param num
|
||||
* number to add
|
||||
*/
|
||||
private void addManaCurveCards(final Iterable<PaperCard> creatures, int num, String nameForLog) {
|
||||
/* // Add the deck card
|
||||
if(commanderCard.getRules().getMainPart().getType().isCreature()) {
|
||||
keyCards = Iterables.filter(aiPlayables,PaperCard.Predicates.name(commanderCard.getName()));
|
||||
final List<PaperCard> keyCardList = Lists.newArrayList(keyCards);
|
||||
deckList.addAll(keyCardList);
|
||||
aiPlayables.removeAll(keyCardList);
|
||||
rankedColorList.removeAll(keyCardList);
|
||||
}*/
|
||||
final Map<Integer,Integer> targetCMCs = new HashMap<>();
|
||||
targetCMCs.put(1,Math.round((r.nextInt(4)+2)*targetSize/60));//2
|
||||
targetCMCs.put(2,Math.round((r.nextInt(5)+5)*targetSize/60));//6
|
||||
targetCMCs.put(3,Math.round((r.nextInt(5)+6)*targetSize/60));//7
|
||||
targetCMCs.put(4,Math.round((r.nextInt(3)+3)*targetSize/60));//4
|
||||
targetCMCs.put(5,Math.round((r.nextInt(3)+3)*targetSize/60));//3
|
||||
targetCMCs.put(6,Math.round((r.nextInt(3)+1)*targetSize/60));//2
|
||||
|
||||
|
||||
final Map<Integer, Integer> creatureCosts = new HashMap<Integer, Integer>();
|
||||
for (int i = 1; i < 7; i++) {
|
||||
creatureCosts.put(i, 0);
|
||||
}
|
||||
final Predicate<PaperCard> filter = Predicates.compose(CardRulesPredicates.Presets.IS_CREATURE,
|
||||
PaperCard.FN_GET_RULES);
|
||||
for (final IPaperCard creature : Iterables.filter(deckList, filter)) {
|
||||
int cmc = creature.getRules().getManaCost().getCMC();
|
||||
if (cmc < 1) {
|
||||
cmc = 1;
|
||||
} else if (cmc > 6) {
|
||||
cmc = 6;
|
||||
}
|
||||
creatureCosts.put(cmc, creatureCosts.get(cmc) + 1);
|
||||
}
|
||||
|
||||
List<PaperCard> creaturesToAdd = new ArrayList<>();
|
||||
for (final PaperCard card : creatures) {
|
||||
int cmc = card.getRules().getManaCost().getCMC();
|
||||
if (cmc < 1) {
|
||||
cmc = 1;
|
||||
} else if (cmc > 6) {
|
||||
cmc = 6;
|
||||
}
|
||||
final Integer currentAtCmc = creatureCosts.get(cmc);
|
||||
boolean willAddCreature = false;
|
||||
if (cmc <= 1 && currentAtCmc < targetCMCs.get(1)) {
|
||||
willAddCreature = true;
|
||||
} else if (cmc == 2 && currentAtCmc < targetCMCs.get(2)) {
|
||||
willAddCreature = true;
|
||||
} else if (cmc == 3 && currentAtCmc < targetCMCs.get(3)) {
|
||||
willAddCreature = true;
|
||||
} else if (cmc == 4 && currentAtCmc < targetCMCs.get(4)) {
|
||||
willAddCreature = true;
|
||||
} else if (cmc == 5 && currentAtCmc < targetCMCs.get(5)) {
|
||||
willAddCreature = true;
|
||||
} else if (cmc >= 6 && currentAtCmc < targetCMCs.get(6)) {
|
||||
willAddCreature = true;
|
||||
}
|
||||
|
||||
if (willAddCreature) {
|
||||
creaturesToAdd.add(card);
|
||||
num--;
|
||||
creatureCosts.put(cmc, creatureCosts.get(cmc) + 1);
|
||||
if (logToConsole) {
|
||||
System.out.println(nameForLog+"[" + num + "]:" + card.getName() + " (" + card.getRules().getManaCost() + ")");
|
||||
}
|
||||
} else {
|
||||
if (logToConsole) {
|
||||
System.out.println(card.getName() + " not added because CMC " + card.getRules().getManaCost().getCMC()
|
||||
+ " has " + currentAtCmc + " already.");
|
||||
}
|
||||
}
|
||||
if (num <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
deckList.addAll(creaturesToAdd);
|
||||
aiPlayables.removeAll(creaturesToAdd);
|
||||
rankedColorList.removeAll(creaturesToAdd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate average CMC.
|
||||
*
|
||||
* @param cards
|
||||
* cards to choose from
|
||||
* @return the average
|
||||
*/
|
||||
private static double getAverageCMC(final List<PaperCard> cards) {
|
||||
double sum = 0.0;
|
||||
for (final IPaperCard cardPrinted : cards) {
|
||||
sum += cardPrinted.getRules().getManaCost().getCMC();
|
||||
}
|
||||
return sum / cards.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate max CMC.
|
||||
*
|
||||
* @param cards
|
||||
* cards to choose from
|
||||
* @return the average
|
||||
*/
|
||||
private static int getMaxCMC(final List<PaperCard> cards) {
|
||||
int max = 0;
|
||||
for (final IPaperCard cardPrinted : cards) {
|
||||
if(cardPrinted.getRules().getManaCost().getCMC()>max) {
|
||||
max = cardPrinted.getRules().getManaCost().getCMC();
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the colors
|
||||
*/
|
||||
public ColorSet getColors() {
|
||||
return colors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param colors0
|
||||
* the colors to set
|
||||
*/
|
||||
public void setColors(final ColorSet colors0) {
|
||||
colors = colors0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the aiPlayables
|
||||
*/
|
||||
public List<PaperCard> getAiPlayables() {
|
||||
return aiPlayables;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -190,7 +190,7 @@ public class CardThemedDeckBuilder extends DeckGeneratorBase {
|
||||
// extras.
|
||||
double avCMC=getAverageCMC(deckList);
|
||||
int maxCMC=getMaxCMC(deckList);
|
||||
if (deckList.size() == numSpellsNeeded && avCMC < 4) {
|
||||
if (deckList.size() >= numSpellsNeeded && avCMC < 4) {
|
||||
addLowCMCCard();
|
||||
}
|
||||
if (deckList.size() >= numSpellsNeeded && avCMC < 3 && maxCMC<6) {
|
||||
|
||||
@@ -27,6 +27,8 @@ import forge.ai.AiProfileUtil;
|
||||
import forge.card.CardPreferences;
|
||||
import forge.card.CardType;
|
||||
import forge.deck.CardRelationMatrixGenerator;
|
||||
import forge.deck.DeckFormat;
|
||||
import forge.deck.io.CardThemedMatrixIO;
|
||||
import forge.deck.io.DeckPreferences;
|
||||
import forge.game.GameFormat;
|
||||
import forge.game.GameType;
|
||||
@@ -218,11 +220,18 @@ public final class FModel {
|
||||
AiProfileUtil.loadAllProfiles(ForgeConstants.AI_PROFILE_DIR);
|
||||
|
||||
//generate Deck Gen matrix
|
||||
if(!FModel.getPreferences().getPrefBoolean(FPref.LOAD_CARD_SCRIPTS_LAZILY)) {
|
||||
CardRelationMatrixGenerator.initialize();
|
||||
if(!FModel.getPreferences().getPrefBoolean(FPref.LOAD_CARD_SCRIPTS_LAZILY)
|
||||
&&FModel.getPreferences().getPrefBoolean(FPref.DECKGEN_CARDBASED)) {
|
||||
deckGenMatrixLoaded=CardRelationMatrixGenerator.initialize();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean deckGenMatrixLoaded=false;
|
||||
|
||||
public static boolean isdeckGenMatrixLoaded(){
|
||||
return deckGenMatrixLoaded;
|
||||
}
|
||||
|
||||
public static QuestController getQuest() {
|
||||
return quest;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,22 @@ public class ForgePreferences extends PreferencesStore<ForgePreferences.FPref> {
|
||||
CONSTRUCTED_P6_DECK_STATE(""),
|
||||
CONSTRUCTED_P7_DECK_STATE(""),
|
||||
CONSTRUCTED_P8_DECK_STATE(""),
|
||||
COMMANDER_P1_DECK_STATE(""),
|
||||
COMMANDER_P2_DECK_STATE(""),
|
||||
COMMANDER_P3_DECK_STATE(""),
|
||||
COMMANDER_P4_DECK_STATE(""),
|
||||
COMMANDER_P5_DECK_STATE(""),
|
||||
COMMANDER_P6_DECK_STATE(""),
|
||||
COMMANDER_P7_DECK_STATE(""),
|
||||
COMMANDER_P8_DECK_STATE(""),
|
||||
TINY_LEADER_P1_DECK_STATE(""),
|
||||
TINY_LEADER_P2_DECK_STATE(""),
|
||||
TINY_LEADER_P3_DECK_STATE(""),
|
||||
TINY_LEADER_P4_DECK_STATE(""),
|
||||
TINY_LEADER_P5_DECK_STATE(""),
|
||||
TINY_LEADER_P6_DECK_STATE(""),
|
||||
TINY_LEADER_P7_DECK_STATE(""),
|
||||
TINY_LEADER_P8_DECK_STATE(""),
|
||||
UI_LANDSCAPE_MODE ("false"),
|
||||
UI_COMPACT_MAIN_MENU ("false"),
|
||||
UI_USE_OLD ("false"),
|
||||
@@ -140,6 +156,7 @@ public class ForgePreferences extends PreferencesStore<ForgePreferences.FPref> {
|
||||
DECKGEN_SINGLETONS ("false"),
|
||||
DECKGEN_ARTIFACTS ("false"),
|
||||
DECKGEN_NOSMALL ("false"),
|
||||
DECKGEN_CARDBASED ("true"),
|
||||
|
||||
PHASE_AI_UPKEEP ("false"),
|
||||
PHASE_AI_DRAW ("false"),
|
||||
@@ -210,6 +227,19 @@ public class ForgePreferences extends PreferencesStore<ForgePreferences.FPref> {
|
||||
CONSTRUCTED_P3_DECK_STATE, CONSTRUCTED_P4_DECK_STATE,
|
||||
CONSTRUCTED_P5_DECK_STATE, CONSTRUCTED_P6_DECK_STATE,
|
||||
CONSTRUCTED_P7_DECK_STATE, CONSTRUCTED_P8_DECK_STATE };
|
||||
|
||||
public static FPref[] COMMANDER_DECK_STATES = {
|
||||
COMMANDER_P1_DECK_STATE, COMMANDER_P2_DECK_STATE,
|
||||
COMMANDER_P3_DECK_STATE, COMMANDER_P4_DECK_STATE,
|
||||
COMMANDER_P5_DECK_STATE, COMMANDER_P6_DECK_STATE,
|
||||
COMMANDER_P7_DECK_STATE, COMMANDER_P8_DECK_STATE };
|
||||
|
||||
public static FPref[] TINY_LEADER_DECK_STATES = {
|
||||
TINY_LEADER_P1_DECK_STATE, TINY_LEADER_P2_DECK_STATE,
|
||||
TINY_LEADER_P3_DECK_STATE, TINY_LEADER_P4_DECK_STATE,
|
||||
TINY_LEADER_P5_DECK_STATE, TINY_LEADER_P6_DECK_STATE,
|
||||
TINY_LEADER_P7_DECK_STATE, TINY_LEADER_P8_DECK_STATE };
|
||||
|
||||
}
|
||||
|
||||
/** Instantiates a ForgePreferences object. */
|
||||
|
||||
Reference in New Issue
Block a user