Skip to content

Commit

Permalink
Add support for chaos dice
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan19 committed May 14, 2021
1 parent a0a4069 commit a70a00c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'java'
apply plugin: 'maven'

group = 'main'
version = '6.1.11'
version = '6.2.0'

description = "A bot for the Facets tabletop RPG"

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/dicerolling/DicePool.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class DicePool {
private List<Integer> plotDice = new ArrayList<>();
private List<Integer> keptDice = new ArrayList<>();
private List<Integer> flatBonuses = new ArrayList<>();
private List<Integer> chaosDice = new ArrayList<>();
//Configs
private int keepHowMany = 2;
private boolean enableEnhancementEmojis = true;
Expand Down Expand Up @@ -37,6 +38,10 @@ public List<Integer> getFlatBonuses() {
return flatBonuses;
}

public List<Integer> getChaosDice() {
return chaosDice;
}

public int getNumberOfKeptDice() {
return keepHowMany;
}
Expand Down Expand Up @@ -71,6 +76,11 @@ public boolean enableOpportunities() {
return enableOpportunities;
}

public DicePool addChaosDie(int dice) {
chaosDice.add(dice);
return this;
}

public DicePool addDice(int dice) {
if (dice > minFacets) {
regularDice.add(dice);
Expand Down Expand Up @@ -104,6 +114,9 @@ public DicePool addDice(String diceType, int dice) {
case "kd":
addKeptDice(dice);
break;
case "cd":
addChaosDie(dice);
break;
default:
throw new IllegalStateException("Unexpected value: " + diceType);
}
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/dicerolling/DiceRoller.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ private String createDicePoolString(DicePool dicePool) {
dicePool.getRegularDice().forEach(integer -> dicePoolString.append(" d").append(integer));
dicePool.getPlotDice().forEach(integer -> dicePoolString.append(" pd").append(integer));
dicePool.getKeptDice().forEach(integer -> dicePoolString.append(" kd").append(integer));
dicePool.getFlatBonuses().stream().map(integer -> (integer > 0) ? ("+" + integer) : integer).forEach(dicePoolString::append);
dicePool.getChaosDice().forEach(integer -> dicePoolString.append(" cd").append(integer));
dicePool.getFlatBonuses().stream().map(integer -> (integer > 0) ? (" +" + integer) : integer).forEach(dicePoolString::append);
return dicePoolString.toString();
}

Expand All @@ -101,6 +102,7 @@ private void rollDice(Random random, DicePool dicePool) {
rollDie(random, dicePool.getRegularDice());
rollKeptDie(random, dicePool.getKeptDice());
rollPlotDice(random, dicePool.getPlotDice());
rollChaosDice(random, dicePool.getChaosDice());
addFlatBonus(dicePool.getFlatBonuses());
}

Expand All @@ -125,6 +127,18 @@ private void rollPlotDice(Random random, List<Integer> plotDice) {
.forEach(rollResult::addPlotDice);
}

/**
* Rolls all chaos die. Chaos die are effectively kept dice, but instead inflict a penalty.
*
* @param random The random number generator
* @param chaosDice The list of chaos die
*/
private void rollChaosDice(Random random, List<Integer> chaosDice) {
chaosDice.stream()
.mapToInt(chaosDie -> (random.nextInt(chaosDie) + 1) * -1)
.forEach(rollResult::addKeptDice);
}

/**
* Rolls all regular dice
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dicerolling/PoolProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ else if (param.matches("-enh=(true|false)")) {
else if (param.matches("-opp=(true|false)")) {
dicePool.setOpportunities(Boolean.parseBoolean(param.substring(5)));
}
else if (param.matches("-nd=(d|kd|pd)")) {
else if (param.matches("-nd=(d|kd|pd|cd)")) {
nextDiceType = param.substring(4);
}
//Any type of dice
else if (param.matches("\\d*([kp])?d\\d+")) {
else if (param.matches("\\d*([kpc])?d\\d+")) {
addDiceToPool(nextDiceFacetMod, param);
//Reset facet modifier
nextDiceFacetMod = 0;
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/statistics/GenerateStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ private HashMap<PoolResult, Long> generateResultsMap() {
rollResultOccurrences = processNormalDice(rollResultOccurrences);
rollResultOccurrences = processPlotDice(rollResultOccurrences);
rollResultOccurrences = processKeptDice(rollResultOccurrences);
rollResultOccurrences = processChaosDice(rollResultOccurrences);
rollResultOccurrences = processFlatBonus(rollResultOccurrences);
return rollResultOccurrences;
}
Expand Down Expand Up @@ -155,6 +156,30 @@ private HashMap<PoolResult, Long> processKeptDice(HashMap<PoolResult, Long> roll
return newMap;
}

private HashMap<PoolResult, Long> processChaosDice(HashMap<PoolResult, Long> rollResultOccurrences) {
HashMap<PoolResult, Long> newMap = new HashMap<>(rollResultOccurrences);
// Loop through all of the kept dice
for (Integer keptDice : dicePool.getChaosDice()) {
HashMap<PoolResult, Long> tempMap = new HashMap<>();
// Create n PoolResult Objects with each possible outcomes of the dice
// Add the occurrences to the new map if that result already exists in the new HashMap, else set the value of that result as the number of occurrences
if (newMap.isEmpty()) {
IntStream.rangeClosed(1, keptDice)
.map(operand -> operand * -1)
.mapToObj(i -> new FastRollResult(dicePool.getNumberOfKeptDice()).addKeptDice(i))
.forEach(fastRollResult -> tempMap.compute(fastRollResult, (result, occurrenceCount) -> occurrenceCount != null ? occurrenceCount + 1 : 1));
}
else {
newMap.forEach((key, value) -> IntStream.rangeClosed(1, keptDice)
.map(operand -> operand * -1)
.mapToObj(key::addKeptDice)
.forEach(fastRollResult -> tempMap.compute(fastRollResult, (result, occurrenceCount) -> occurrenceCount != null ? occurrenceCount + value : value)));
}
newMap = tempMap;
}
return newMap;
}

private HashMap<PoolResult, Long> processPlotDice(HashMap<PoolResult, Long> rollResultOccurrences) {
HashMap<PoolResult, Long> newMap = new HashMap<>(rollResultOccurrences);
// Loop through all of the kept dice
Expand Down

0 comments on commit a70a00c

Please sign in to comment.