Skip to content

Commit

Permalink
Merge pull request #4304 from cwisniew/ffg-dice
Browse files Browse the repository at this point in the history
Add Genesys Narrative Dice (incl StarWars)
  • Loading branch information
cwisniew authored Sep 30, 2023
2 parents e479bb1 + 368d241 commit 043c840
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 2 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,14 @@ dependencies {

// Built In Add-on Libraries
implementation 'com.github.RPTools:maptool-builtin-addons:1.3'

// For advanced dice roller
implementation 'com.github.RPTools:advanced-dice-roller:1.0.3'





}


Expand Down
29 changes: 29 additions & 0 deletions src/main/java/net/rptools/dicelib/expression/ExpressionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package net.rptools.dicelib.expression;

import java.util.List;
import java.util.regex.Pattern;
import net.rptools.dicelib.expression.function.ArsMagicaStress;
import net.rptools.dicelib.expression.function.CountSuccessDice;
import net.rptools.dicelib.expression.function.DropHighestRoll;
Expand All @@ -36,9 +38,11 @@
import net.rptools.dicelib.expression.function.ShadowRun5Dice;
import net.rptools.dicelib.expression.function.ShadowRun5ExplodeDice;
import net.rptools.dicelib.expression.function.UbiquityRoll;
import net.rptools.dicelib.expression.function.advanced.AdvancedDiceRolls;
import net.rptools.parser.*;
import net.rptools.parser.transform.RegexpStringTransformer;
import net.rptools.parser.transform.StringLiteralTransformer;
import org.javatuples.Pair;

public class ExpressionParser {
private static String[][] DICE_PATTERNS =
Expand Down Expand Up @@ -209,6 +213,11 @@ public class ExpressionParser {

private final Parser parser;

private final List<Pair<Pattern, String>> preprocessPatterns =
List.of(
new Pair<>(Pattern.compile("([A-z]+)!\"([^\"]*)\""), "advancedRoll('$1', " + "'$2')"),
new Pair<>(Pattern.compile("([A-z]+)!'([^']*)'"), "advancedRoll('$1', " + "'$2')"));

public ExpressionParser() {
this(DICE_PATTERNS);
}
Expand Down Expand Up @@ -238,6 +247,7 @@ public ExpressionParser(String[][] regexpTransforms) {
parser.addFunction(new DropHighestRoll());
parser.addFunction(new KeepLowestRoll());
parser.addFunction(new ArsMagicaStress());
parser.addFunction(new AdvancedDiceRolls());

parser.addFunction(new If());

Expand Down Expand Up @@ -277,6 +287,9 @@ public Result evaluate(String expression, VariableResolver resolver, boolean mak
}
RunData.setCurrent(newRunData);

// Some patterns need pre-processing before the parser is called otherwise the parser
// creation will fail
expression = preProcess(expression);
synchronized (parser) {
final Expression xp =
makeDeterministic
Expand All @@ -292,4 +305,20 @@ public Result evaluate(String expression, VariableResolver resolver, boolean mak

return ret;
}

/**
* Pre-process the expression before it is parsed. This is used to convert some patterns into
* function calls that the parser can handle.
*
* @param expression The expression to pre-process
* @return The pre-processed expression
*/
private String preProcess(String expression) {
for (Pair<Pattern, String> p : preprocessPatterns) {
if (p.getValue0().matcher(expression).find()) {
return p.getValue0().matcher(expression).replaceAll(p.getValue1());
}
}
return expression;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code 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.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.dicelib.expression.function.advanced;

import java.util.List;
import net.rptools.dicelib.expression.function.advanced.GenesysDiceRolls.DiceType;
import net.rptools.maptool.language.I18N;
import net.rptools.parser.Parser;
import net.rptools.parser.ParserException;
import net.rptools.parser.VariableResolver;
import net.rptools.parser.function.AbstractFunction;

/** Function to roll dice using the advanced dice roller. */
public class AdvancedDiceRolls extends AbstractFunction {

/** Constructor. */
public AdvancedDiceRolls() {
super(2, 2, false, "advancedRoll");
}

@Override
public Object childEvaluate(
Parser parser, VariableResolver resolver, String functionName, List<Object> parameters)
throws ParserException {
String diceName = parameters.get(0).toString().toLowerCase();
String diceExpression = parameters.get(1).toString();

try {
return switch (diceName) {
case "sw" -> new GenesysDiceRolls().roll(DiceType.StarWars, diceExpression, resolver);
case "gs" -> new GenesysDiceRolls().roll(DiceType.Genesys, diceExpression, resolver);
default -> throw new ParserException(
I18N.getText("advanced.roll.unknownDiceType", diceName));
};
} catch (IllegalArgumentException e) {
throw new ParserException(e.getMessage());
}
}
}
Loading

0 comments on commit 043c840

Please sign in to comment.