Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions forge-gui/src/main/java/forge/deck/DeckgenUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,7 @@ public static Deck generateRandomCommanderDeck(PaperCard commander, DeckFormat f
}else {
String matrixKey = (format.equals(DeckFormat.TinyLeaders) ? DeckFormat.Commander : format).toString(); //use Commander for Tiny Leaders
List<Map.Entry<PaperCard, Integer>> potentialCards = new ArrayList<>(CardRelationMatrixGenerator.cardPools.get(matrixKey).get(commander.getName()));
Collections.shuffle(potentialCards, MyRandom.getRandom());
for(Map.Entry<PaperCard,Integer> pair:potentialCards){
for(Map.Entry<PaperCard,Integer> pair:getWeightedRandomizedCardPool(potentialCards)){
if(format.isLegalCard(pair.getKey())) {
preSelectedCards.add(pair.getKey());
}
Expand Down Expand Up @@ -819,6 +818,20 @@ else if (commander.getRules().canBePartnerCommander()) {
return deck;
}

private static List<Map.Entry<PaperCard, Integer>> getWeightedRandomizedCardPool(final List<Map.Entry<PaperCard, Integer>> potentialCards) {
final Map<Map.Entry<PaperCard, Integer>, Double> sortKeys = new IdentityHashMap<>();
for (final Map.Entry<PaperCard, Integer> cardEntry : potentialCards) {
sortKeys.put(cardEntry, getWeightedRandomSortKey(cardEntry));
}
potentialCards.sort(Comparator.comparingDouble(sortKeys::get).reversed());
Comment thread
Madwand99 marked this conversation as resolved.
return potentialCards;
}

private static double getWeightedRandomSortKey(final Map.Entry<PaperCard, Integer> cardEntry) {
final int weight = Math.max(1, cardEntry.getValue());
return Math.log(MyRandom.getRandom().nextDouble()) / weight;
}

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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package forge.gamemodes.limited;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
Expand All @@ -20,7 +23,7 @@ public class CardThemedCommanderDeckBuilder extends CardThemedDeckBuilder {

public CardThemedCommanderDeckBuilder(PaperCard commanderCard0, PaperCard partner0, final List<PaperCard> dList, boolean isForAI, DeckFormat format) {
super(new DeckGenPool(FModel.getMagicDb().getCommonCards().getUniqueCards()), format);
this.availableList = dList;
this.availableList = new ArrayList<>(dList);
keyCard = commanderCard0;
secondKeyCard = partner0;
// remove Unplayables
Expand All @@ -32,6 +35,8 @@ public CardThemedCommanderDeckBuilder(PaperCard commanderCard0, PaperCard partne
this.aiPlayables = Lists.newArrayList(availableList);
}
this.availableList.removeAll(aiPlayables);
this.aiPlayables = uniqueCardNamesForSingletonDeck(aiPlayables);
this.availableList = uniqueCardNamesForSingletonDeck(availableList);
targetSize=format.getMainRange().getMinimum();
colors = keyCard.getRules().getColorIdentity();
colors = ColorSet.combine(colors, keyCard.getRules().getColorIdentity());
Expand Down Expand Up @@ -59,6 +64,13 @@ protected void addLandKeyCards(){
//do nothing as keycards are commander/partner and are added by the DeckGenUtils
}

@Override
protected void extendPlaysets(int numSpellsNeeded) {
// Commander-family formats are singleton except for basic lands and cards
// with explicit deckbuilding exceptions. Do not fill gaps by duplicating
// cards already selected for the main deck.
}

@Override
protected void addThirdColorCards(int num) {
//do nothing as we cannot add extra colours beyond commanders
Expand All @@ -78,4 +90,25 @@ protected String generateName() {
return keyCard.getName() +" based commander deck";
}

private List<PaperCard> uniqueCardNamesForSingletonDeck(final List<PaperCard> cards) {
final List<PaperCard> result = new ArrayList<>();
final Map<String, Integer> countsByName = new HashMap<>();
countsByName.put(keyCard.getName(), 1);
if (secondKeyCard != null) {
countsByName.put(secondKeyCard.getName(), 1);
}

for (final PaperCard card : cards) {
final int maxCopies = format.getMaxCardCopies(card);
final String name = card.getName();
final int currentCount = countsByName.getOrDefault(name, 0);
if (currentCount < maxCopies) {
result.add(card);
countsByName.put(name, currentCount + 1);
}
}

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ public boolean test(PaperCard card) {
&& !card.getRules().getMainPart().getType().isLand();
}
};
List<PaperCard> possibleList = Lists.newArrayList(pool.getAllCards(possibleFromFullPool));
List<PaperCard> possibleList = limitCopiesForFormat(Lists.newArrayList(pool.getAllCards(possibleFromFullPool)));
Comment thread
Madwand99 marked this conversation as resolved.
Outdated
//ensure we do not add more keycards in case they are commanders
if (keyCard != null) {
possibleList.removeAll(StaticData.instance().getCommonCards().getAllCards(keyCard));
Expand Down Expand Up @@ -830,7 +830,7 @@ private void addRandomCards(int num) {
&& !card.getRules().getAiHints().getRemRandomDecks()
&& !card.getRules().getMainPart().getType().isLand();

List<PaperCard> possibleList = Lists.newArrayList(pool.getAllCards(possibleFromFullPool));
List<PaperCard> possibleList = limitCopiesForFormat(Lists.newArrayList(pool.getAllCards(possibleFromFullPool)));
//ensure we do not add more keycards in case they are commanders
if (keyCard != null) {
possibleList.removeAll(StaticData.instance().getCommonCards().getAllCards(keyCard));
Expand All @@ -844,6 +844,24 @@ private void addRandomCards(int num) {
addManaCurveCards(possibleList, num, "Random Card");
}

protected List<PaperCard> limitCopiesForFormat(final List<PaperCard> cards) {
final Map<String, Integer> countsByName = new HashMap<>();
for (final PaperCard card : deckList) {
countsByName.merge(card.getName(), 1, Integer::sum);
}

final List<PaperCard> result = new ArrayList<>();
for (final PaperCard card : cards) {
final String name = card.getName();
final int currentCount = countsByName.getOrDefault(name, 0);
if (currentCount < format.getMaxCardCopies(card)) {
result.add(card);
countsByName.put(name, currentCount + 1);
}
}
return result;
}

/**
* Add creatures to the deck.
*
Expand Down