diff --git a/.gitignore b/.gitignore index 471958e..94690a6 100755 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,3 @@ -.gradle -build/ -maven-repo/ - # OS generated files ################################################################################ .DS_Store @@ -16,11 +12,21 @@ Thumbs.db # Others ################################################################################ -*.log -*~ -target/ +.gradle build/ +/bin/ +target/ out/ +/releases/ +*.log +*~ +\${sys:appHome}/ + + +# Automatically Generated +################################################################################ +package/windows/MapTool.iss +src/main/resources/sentry.properties # IDEs @@ -33,3 +39,7 @@ out/ .settings/ .classpath .history +workbench.xmi + +# Keystore +build-resources/rptools-keystore \ No newline at end of file diff --git a/README.md b/README.md index e70536e..199cbd8 100755 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![](https://jitpack.io/v/RPTools/dicelib.svg)](https://jitpack.io/#RPTools/dicelib) + # dicelib Dice Roll Library used by RPTools programs. diff --git a/build.gradle b/build.gradle index d4a5e8a..6b4d2c9 100755 --- a/build.gradle +++ b/build.gradle @@ -1,41 +1,97 @@ -import org.apache.tools.ant.filters.ReplaceTokens import java.text.SimpleDateFormat -import org.ajoberstar.grgit.* +buildscript { + dependencies { + classpath "com.diffplug.spotless:spotless-plugin-gradle:3.13.0" + } + + repositories { + mavenCentral() + } +} +// Access Git info from build script +plugins { + id "org.ajoberstar.grgit" version "3.0.0" +} -apply plugin: 'java' -apply plugin: 'findbugs' -apply plugin: 'pmd' -apply plugin: 'application' -apply plugin: 'maven' + +apply plugin: 'java-library' apply plugin: 'com.diffplug.gradle.spotless' +apply plugin: 'maven' + + + +sourceCompatibility = 10 +targetCompatibility = 10 + + +// In this section you declare where to find the dependencies of your project +repositories { + mavenCentral() + mavenLocal() + maven { url = 'http://maptool.craigs-stuff.net/repo/' } + maven { url = 'https://jitpack.io' } +} -sourceCompatibility = 1.7 -targetCompatibility = 1.7 -if (version == 'unspecified') { - version = getVersionName() +dependencies { + compile 'rhino:js:1.6R5' + compile 'antlr:antlr:2.7.6' +// compile 'net.rptools.parser:parser:1.1.b24' + implementation 'com.github.RPTools:parser:1.5.5' + testCompile group: 'junit', name: 'junit', version: '4.11' } -ext.repo = Grgit.open(project.file('.')) +// Custom properties +ext { + // Get tag and commit info from Git to use for version numbering + def repo = org.ajoberstar.grgit.Grgit.open(currentDir: file('.')) + def head = repo.head() + def tags = repo.tag.list().find { + it.commit == head + } + if (tags) { + tagVersion = tags.getName() + project.version = tagVersion + } + + revision = head.abbreviatedId + revisionFull = head.id + +// println 'Configuring for ' + project.name + " " + tagVersion + " by " + vendor +} +group = "net.rptools.dicelib" spotless { java { - eclipseFormatFile 'build-resources/eclipse.prefs.formatter.xml' + licenseHeaderFile 'spotless.license.java' + // Now using the Google Java style guide + //eclipse().configFile('build-resources/eclipse.prefs.formatter.xml') + googleJavaFormat() + // If you get exceptions thrown by spotlessApply, this might + // help. Enable it here if the problem is with a Java file, and + // below if it is not. Don't leave it enabled, as nothing will + // actually be updated if you do. + // https://github.com/diffplug/spotless/blob/master/PADDEDCELL.md + //paddedCell() } -} + format 'misc', { + target '**/*.gradle', '**/.gitignore' -configurations { - deployerJars + // spotless has built-in rules for most basic formatting tasks + trimTrailingWhitespace() + // or spaces. Takes an integer argument if you don't like 4 + indentWithSpaces(4) + //paddedCell() + } } - -task wrapper(type: Wrapper) { - gradleVersion='2.1' +configurations { + deployerJars } install { @@ -57,75 +113,6 @@ uploadArchives { } } -buildscript { - repositories { - mavenCentral() - jcenter() - maven { - url 'https://plugins.gradle.org/m2/' - } - } - dependencies { - classpath 'org.ajoberstar:gradle-git:0.11.2' - classpath 'com.diffplug.gradle.spotless:spotless:1.3.0-SNAPSHOT' - } -} - - -dependencies { - compile 'rhino:js:1.6R5' - compile 'antlr:antlr:2.7.6' - compile 'net.rptools.parser:parser:1.1.b24' - testCompile group: 'junit', name: 'junit', version: '4.11' - deployerJars 'org.apache.maven.wagon:wagon-ssh:2.2' -} - -ext.compileDate = new Date(); -ext.yyyymmdd = (new SimpleDateFormat('yyyyMMDD')).format(ext.compileDate); - - -repositories { - mavenCentral() - mavenLocal() - maven { - url = 'http://maptool.craigs-stuff.net/repo/' - } -} - - -/* - * Gets the version name from the latest Git tag - */ - -def getVersionName() { - - - if (project.hasProperty('buildVersion')) { - return buildVersion - } else { - String vtxtVersionNo = new File('build-resources/version.txt').text.trim() - - return vtxtVersionNo - } -} - -findbugs { - ignoreFailures = true - toolVersion = '3.0.0' - effort = 'max' - sourceSets = [] // Empty source set so it wont run during build/check -} - -pmd { - ignoreFailures = true - sourceSets = [] // Empty source set so it wont run during tebuild/check -} - - - -task showBuildVersion() << { - println 'Build Version Number = ' + project.version -} jar { manifest { diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar old mode 100755 new mode 100644 index b761216..87b738c Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties old mode 100755 new mode 100644 index c9c27e8..ea13fdf --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Sun May 03 01:01:06 CST 2015 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.3.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-bin.zip diff --git a/gradlew b/gradlew index 91a7e26..af6708f 100755 --- a/gradlew +++ b/gradlew @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh ############################################################################## ## @@ -6,20 +6,38 @@ ## ############################################################################## -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" -warn ( ) { +warn () { echo "$*" } -die ( ) { +die () { echo echo "$*" echo @@ -30,6 +48,7 @@ die ( ) { cygwin=false msys=false darwin=false +nonstop=false case "`uname`" in CYGWIN* ) cygwin=true @@ -40,31 +59,11 @@ case "`uname`" in MINGW* ) msys=true ;; + NONSTOP* ) + nonstop=true + ;; esac -# For Cygwin, ensure paths are in UNIX format before anything is touched. -if $cygwin ; then - [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` -fi - -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >&- -APP_HOME="`pwd -P`" -cd "$SAVED" >&- - CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar # Determine the Java command to use to start the JVM. @@ -90,7 +89,7 @@ location of your Java installation." fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then MAX_FD_LIMIT=`ulimit -H -n` if [ $? -eq 0 ] ; then if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then @@ -114,6 +113,7 @@ fi if $cygwin ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` # We build the pattern for arguments to be converted via cygpath ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` @@ -154,11 +154,19 @@ if $cygwin ; then esac fi -# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules -function splitJvmOpts() { - JVM_OPTS=("$@") +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " } -eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS -JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi -exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat old mode 100755 new mode 100644 index aec9973..6d57edc --- a/gradlew.bat +++ b/gradlew.bat @@ -1,90 +1,84 @@ -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windowz variants - -if not "%OS%" == "Windows_NT" goto win9xME_args -if "%@eval[2+2]" == "4" goto 4NT_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* -goto execute - -:4NT_args -@rem Get arguments from the 4NT Shell from JP Software -set CMD_LINE_ARGS=%$ - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/jitpack.yml b/jitpack.yml new file mode 100644 index 0000000..246d32f --- /dev/null +++ b/jitpack.yml @@ -0,0 +1,2 @@ +jdk: + - oraclejdk10 diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..4719553 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,10 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * + * Detailed information about configuring a multi-project build in Gradle can be found + * in the user manual at https://docs.gradle.org/5.2.1/userguide/multi_project_builds.html + */ + +rootProject.name = 'dicelib' diff --git a/spotless.license.java b/spotless.license.java new file mode 100644 index 0000000..25bfe7b --- /dev/null +++ b/spotless.license.java @@ -0,0 +1,14 @@ +/* + * 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 + * and specifically the Affero license + * text at . + */ diff --git a/src/main/java/net/rptools/common/expression/ExpressionParser.java b/src/main/java/net/rptools/common/expression/ExpressionParser.java index e3a95fe..89aa69b 100755 --- a/src/main/java/net/rptools/common/expression/ExpressionParser.java +++ b/src/main/java/net/rptools/common/expression/ExpressionParser.java @@ -1,15 +1,16 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression; @@ -22,159 +23,204 @@ import net.rptools.parser.transform.StringLiteralTransformer; public class ExpressionParser { - private static String[][] DICE_PATTERNS = new String[][] { - // Comments - new String[] { "//.*", "" }, - - // Color hex strings #FFF or #FFFFFF or #FFFFFFFF (with alpha) - new String[] { "(? and specifically the Affero license + * text at . */ package net.rptools.common.expression; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; public class Result { - private final String expression; - private String detailExpression; - private Object value; - private String description; - - private final Map properties = new HashMap(); - - public Result(String expression) { - this.expression = expression; - } - - public String getExpression() { - return expression; - } - - public String getDetailExpression() { - return detailExpression; - } - - public void setDetailExpression(String detailExpression) { - this.detailExpression = detailExpression; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Object getValue() { - return value; - } - - public void setValue(Object value) { - this.value = value; - } - - public Map getProperties() { - return this.properties; - } - - public String format() { - StringBuilder sb = new StringBuilder(64); - sb.append(expression).append(" = "); - - if (detailExpression != null && !detailExpression.equals(value.toString())) { - sb.append("(").append(detailExpression).append(") = "); - } - - sb.append(value); - if (description != null) { - sb.append(" // ").append(description); - } - return sb.toString(); - } + private final String expression; + private String detailExpression; + private Object value; + private String description; + private List rolled; + + private final Map properties = new HashMap(); + + public Result(String expression) { + this.expression = expression; + } + + public String getExpression() { + return expression; + } + + public String getDetailExpression() { + return detailExpression; + } + + public void setDetailExpression(String detailExpression) { + this.detailExpression = detailExpression; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + + public Map getProperties() { + return this.properties; + } + + public void setRolled(List rolls) { + rolled = new ArrayList<>(rolls); + } + + public List getRolled() { + return Collections.unmodifiableList(rolled); + } + + public String format() { + StringBuilder sb = new StringBuilder(64); + sb.append(expression).append(" = "); + + if (detailExpression != null && !detailExpression.equals(value.toString())) { + sb.append("(").append(detailExpression).append(") = "); + } + + sb.append(value); + if (description != null) { + sb.append(" // ").append(description); + } + return sb.toString(); + } } diff --git a/src/main/java/net/rptools/common/expression/RunData.java b/src/main/java/net/rptools/common/expression/RunData.java index f112246..3a4b93e 100755 --- a/src/main/java/net/rptools/common/expression/RunData.java +++ b/src/main/java/net/rptools/common/expression/RunData.java @@ -1,100 +1,107 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression; import java.security.SecureRandom; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; import java.util.Random; public class RunData { - private static ThreadLocal current = new ThreadLocal(); - public static Random RANDOM = new SecureRandom(); - - private final Result result; - - private long randomValue; - private long randomMax; - private long randomMin; - - public RunData(Result result) { - this.result = result; - } - - /** - * Returns a random integer between 1 and maxValue - */ - public int randomInt(int maxValue) { - return randomInt(1, maxValue); - } - - /** - * Returns a list of random integers between 1 and maxValue - */ - public int[] randomInts(int num, int maxValue) { - int[] ret = new int[num]; - for (int i = 0; i < num; i++) - ret[i] = randomInt(maxValue); - return ret; - } - - /** - * Returns a random integer between minValue and maxValue - */ - public int randomInt(int minValue, int maxValue) { - randomMin += minValue; - randomMax += maxValue; - - int result = RANDOM.nextInt(maxValue - minValue + 1) + minValue; - - randomValue += result; - - return result; - } - - /** - * Returns a list of random integers between minValue and maxValue - * @return - */ - public int[] randomInts(int num, int minValue, int maxValue) { - int[] ret = new int[num]; - for (int i = 0; i < num; i++) - ret[i] = randomInt(minValue, maxValue); - return ret; - } - - public Result getResult() { - return result; - } - - public static boolean hasCurrent() { - return current.get() != null; - } - - public static RunData getCurrent() { - RunData data = current.get(); - if (data == null) { - throw new NullPointerException("data cannot be null"); - } - return data; - } - - public static void setCurrent(RunData data) { - current.set(data); - } - - // If a seed is set we need to switch from SecureRandom to - // random. - public static void setSeed(long seed) { - RANDOM = new Random(seed); - } + private static ThreadLocal current = new ThreadLocal(); + public static Random RANDOM = new SecureRandom(); + + private final Result result; + + private long randomValue; + private long randomMax; + private long randomMin; + + private List rolled = new LinkedList<>(); + + public RunData(Result result) { + this.result = result; + } + + /** Returns a random integer between 1 and maxValue */ + public int randomInt(int maxValue) { + return randomInt(1, maxValue); + } + + /** Returns a list of random integers between 1 and maxValue */ + public int[] randomInts(int num, int maxValue) { + int[] ret = new int[num]; + for (int i = 0; i < num; i++) { + ret[i] = randomInt(maxValue); + } + return ret; + } + + /** Returns a random integer between minValue and maxValue */ + public int randomInt(int minValue, int maxValue) { + randomMin += minValue; + randomMax += maxValue; + + int result = RANDOM.nextInt(maxValue - minValue + 1) + minValue; + + rolled.add(result); + + randomValue += result; + + return result; + } + + /** + * Returns a list of random integers between minValue and maxValue + * + * @return + */ + public int[] randomInts(int num, int minValue, int maxValue) { + int[] ret = new int[num]; + for (int i = 0; i < num; i++) ret[i] = randomInt(minValue, maxValue); + return ret; + } + + public Result getResult() { + return result; + } + + public static boolean hasCurrent() { + return current.get() != null; + } + + public static RunData getCurrent() { + RunData data = current.get(); + if (data == null) { + throw new NullPointerException("data cannot be null"); + } + return data; + } + + public static void setCurrent(RunData data) { + current.set(data); + } + + // If a seed is set we need to switch from SecureRandom to + // random. + public static void setSeed(long seed) { + RANDOM = new Random(seed); + } + + public List getRolled() { + return Collections.unmodifiableList(rolled); + } } diff --git a/src/main/java/net/rptools/common/expression/function/CountSuccessDice.java b/src/main/java/net/rptools/common/expression/function/CountSuccessDice.java index ac741d7..7d194d5 100755 --- a/src/main/java/net/rptools/common/expression/function/CountSuccessDice.java +++ b/src/main/java/net/rptools/common/expression/function/CountSuccessDice.java @@ -1,39 +1,39 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; import net.rptools.parser.function.EvaluationException; public class CountSuccessDice extends AbstractNumberFunction { - public CountSuccessDice() { - super(3, 3, false, "countsuccess", "success"); - } - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws EvaluationException { - int n = 0; - int times = ((BigDecimal) parameters.get(n++)).intValue(); - int sides = ((BigDecimal) parameters.get(n++)).intValue(); - int success = ((BigDecimal) parameters.get(n++)).intValue(); + public CountSuccessDice() { + super(3, 3, false, "countsuccess", "success"); + } - return new BigDecimal(DiceHelper.countSuccessDice(times, sides, success)); - } + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { + int n = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + int success = ((BigDecimal) parameters.get(n++)).intValue(); + return new BigDecimal(DiceHelper.countSuccessDice(times, sides, success)); + } } diff --git a/src/main/java/net/rptools/common/expression/function/DiceHelper.java b/src/main/java/net/rptools/common/expression/function/DiceHelper.java index 47e2ac9..039cbfd 100755 --- a/src/main/java/net/rptools/common/expression/function/DiceHelper.java +++ b/src/main/java/net/rptools/common/expression/function/DiceHelper.java @@ -1,183 +1,213 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.util.Arrays; - +import java.util.Comparator; import net.rptools.common.expression.RunData; import net.rptools.parser.function.*; public class DiceHelper { - public static int rollDice(int times, int sides) { - int result = 0; + public static int rollDice(int times, int sides) { + int result = 0; + + RunData runData = RunData.getCurrent(); + + for (int i = 0; i < times; i++) { + result += runData.randomInt(sides); + } + + return result; + } + + public static String explodingSuccessDice(int times, int sides, int target) + throws EvaluationException { + String rolls = "Dice: "; + int successes = 0; + + for (int i = 0; i < times; i++) { + int currentRoll = explodeDice(1, sides); + rolls += currentRoll + ", "; + if (currentRoll >= target) successes++; + } + return rolls + "Successes: " + successes; + } + + public static String openTestDice(int times, int sides) throws EvaluationException { + String rolls = "Dice: "; + int max = 0; + + for (int i = 0; i < times; i++) { + int currentRoll = explodeDice(1, sides); + rolls += currentRoll + ", "; + if (currentRoll > max) max = currentRoll; + } + return rolls + "Maximum: " + max; + } + + public static int fudgeDice(int times) { + return rollDice(times, 3) - (2 * times); + } + + public static int ubiquityDice(int times) { + return rollDice(times, 2) - times; + } + + public static int keepDice(int times, int sides, int keep) throws EvaluationException { + return dropDice(times, sides, times - keep); + } - RunData runData = RunData.getCurrent(); + public static int keepLowestDice(int times, int sides, int keep) throws EvaluationException { + return dropDiceHighest(times, sides, times - keep); + } - for (int i = 0; i < times; i++) { - result += runData.randomInt(sides); - } + public static int dropDice(int times, int sides, int drop) throws EvaluationException { + if (times - drop <= 0) throw new EvaluationException("You cannot drop more dice than you roll"); - return result; - } + RunData runData = RunData.getCurrent(); - public static String explodingSuccessDice(int times, int sides, int target) throws EvaluationException { - String rolls = "Dice: "; - int successes = 0; + int[] values = runData.randomInts(times, sides); - for (int i = 0; i < times; i++) { - int currentRoll = explodeDice(1, sides); - rolls += currentRoll + ", "; - if (currentRoll >= target) - successes++; - } - return rolls + "Successes: " + successes; - } + Arrays.sort(values); - public static String openTestDice(int times, int sides) throws EvaluationException { - String rolls = "Dice: "; - int max = 0; + int result = 0; + for (int i = drop; i < times; i++) { + result += values[i]; + } - for (int i = 0; i < times; i++) { - int currentRoll = explodeDice(1, sides); - rolls += currentRoll + ", "; - if (currentRoll > max) - max = currentRoll; - } - return rolls + "Maximum: " + max; - } + return result; + } - public static int fudgeDice(int times) { - return rollDice(times, 3) - (2 * times); - } + public static int dropDiceHighest(int times, int sides, int drop) throws EvaluationException { + if (times - drop <= 0) throw new EvaluationException("You cannot drop more dice than you roll"); - public static int ubiquityDice(int times) { - return rollDice(times, 2) - times; - } + RunData runData = RunData.getCurrent(); - public static int keepDice(int times, int sides, int keep) throws EvaluationException { - return dropDice(times, sides, times - keep); - } + int[] values = runData.randomInts(times, sides); - public static int dropDice(int times, int sides, int drop) throws EvaluationException { - if (times - drop <= 0) - throw new EvaluationException("You cannot drop more dice than you roll"); + int[] descValues = + Arrays.stream(values) + .boxed() + .sorted(Comparator.reverseOrder()) + .mapToInt(Integer::intValue) + .toArray(); - RunData runData = RunData.getCurrent(); + int result = 0; + for (int i = drop; i < times; i++) { + result += descValues[i]; + } - int[] values = runData.randomInts(times, sides); + return result; + } - Arrays.sort(values); + public static int rerollDice(int times, int sides, int lowerBound) throws EvaluationException { + RunData runData = RunData.getCurrent(); - int result = 0; - for (int i = drop; i < times; i++) { - result += values[i]; - } + if (lowerBound > sides) + throw new EvaluationException( + "When rerolling, the lowerbound must be smaller than the number of sides on the rolling dice."); - return result; - } + int[] values = new int[times]; - public static int rerollDice(int times, int sides, int lowerBound) throws EvaluationException { - RunData runData = RunData.getCurrent(); + for (int i = 0; i < values.length; i++) { + int roll; + while ((roll = runData.randomInt(sides)) < lowerBound) ; - if (lowerBound > sides) - throw new EvaluationException( - "When rerolling, the lowerbound must be smaller than the number of sides on the rolling dice."); + values[i] = roll; + } - int[] values = new int[times]; + int result = 0; + for (int i = 0; i < values.length; i++) { + result += values[i]; + } - for (int i = 0; i < values.length; i++) { - int roll; - while ((roll = runData.randomInt(sides)) < lowerBound) - ; + return result; + } - values[i] = roll; - } + public static int explodeDice(int times, int sides) throws EvaluationException { + int result = 0; - int result = 0; - for (int i = 0; i < values.length; i++) { - result += values[i]; - } + if (sides == 0 || sides == 1) throw new EvaluationException("Number of sides must be > 1"); - return result; - } + RunData runData = RunData.getCurrent(); - public static int explodeDice(int times, int sides) throws EvaluationException { - int result = 0; + for (int i = 0; i < times; i++) { + int roll = runData.randomInt(sides); + if (roll == sides) times++; + result += roll; + } - if (sides == 0 || sides == 1) - throw new EvaluationException("Number of sides must be > 1"); + return result; + } - RunData runData = RunData.getCurrent(); + public static int countSuccessDice(int times, int sides, int success) { + RunData runData = RunData.getCurrent(); - for (int i = 0; i < times; i++) { - int roll = runData.randomInt(sides); - if (roll == sides) - times++; - result += roll; - } + int result = 0; + for (int value : runData.randomInts(times, sides)) { + if (value >= success) result++; + } - return result; - } + return result; + } - public static int countSuccessDice(int times, int sides, int success) { - RunData runData = RunData.getCurrent(); + public static String countShadowRun4(int times, int gremlins, boolean explode) { + RunData runData = RunData.getCurrent(); - int result = 0; - for (int value : runData.randomInts(times, sides)) { - if (value >= success) - result++; - } + int hitCount = 0; + int oneCount = 0; + int sides = 6; + int success = 5; + String actual = ""; + String glitch = ""; - return result; - } + for (int i = 0; i < times; i++) { + int value = runData.randomInt(sides); - public static String countShadowRun4(int times, int gremlins, boolean explode) { - RunData runData = RunData.getCurrent(); + if (value >= success) hitCount++; - int hitCount = 0; - int oneCount = 0; - int sides = 6; - int success = 5; - String actual = ""; - String glitch = ""; + if (value == 1) oneCount++; - for (int i = 0; i < times; i++) { - int value = runData.randomInt(sides); + if (value == 6 && explode) times++; - if (value >= success) - hitCount++; + actual = actual + value + " "; + } - if (value == 1) - oneCount++; + // Check for Glitchs + if (oneCount != 0) { + if ((hitCount == 0) && ((double) times / 2 - gremlins) <= (double) oneCount) { + glitch = " *Critical Glitch*"; + } else if ((double) (times / 2 - gremlins) <= (double) oneCount) { + glitch = " *Glitch*"; + } + } - if (value == 6 && explode) - times++; + String result = "Hits: " + hitCount + " Ones: " + oneCount + glitch + " Results: " + actual; - actual = actual + value + " "; - } + return result; + } - // Check for Glitchs - if (oneCount != 0) { - if ((hitCount == 0) && ((double) times / 2 - gremlins) <= (double) oneCount) { - glitch = " *Critical Glitch*"; - } else if ((double) (times / 2 - gremlins) <= (double) oneCount) { - glitch = " *Glitch*"; - } - } + public static int rollModWithBounds(int times, int sides, int sub, int lower, int upper) { + int result = 0; - String result = "Hits: " + hitCount + " Ones: " + oneCount + glitch + " Results: " + actual; + for (int i = 0; i < times; i++) { + int roll = rollDice(1, sides); + int val = Math.min(Math.max(roll + sub, lower), upper); + result += val; + } - return result; - } + return result; + } } diff --git a/src/main/java/net/rptools/common/expression/function/DropHighestRoll.java b/src/main/java/net/rptools/common/expression/function/DropHighestRoll.java new file mode 100755 index 0000000..2838cd5 --- /dev/null +++ b/src/main/java/net/rptools/common/expression/function/DropHighestRoll.java @@ -0,0 +1,39 @@ +/* + * 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 + * and specifically the Affero license + * text at . + */ +package net.rptools.common.expression.function; + +import java.math.BigDecimal; +import java.util.List; +import net.rptools.parser.Parser; +import net.rptools.parser.function.AbstractNumberFunction; +import net.rptools.parser.function.EvaluationException; + +public class DropHighestRoll extends AbstractNumberFunction { + + public DropHighestRoll() { + super(3, 3, false, "dropHighest"); + } + + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { + int n = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + int drop = ((BigDecimal) parameters.get(n++)).intValue(); + + return new BigDecimal(DiceHelper.dropDiceHighest(times, sides, drop)); + } +} diff --git a/src/main/java/net/rptools/common/expression/function/DropRoll.java b/src/main/java/net/rptools/common/expression/function/DropRoll.java index aaddeff..507e1f8 100755 --- a/src/main/java/net/rptools/common/expression/function/DropRoll.java +++ b/src/main/java/net/rptools/common/expression/function/DropRoll.java @@ -1,39 +1,39 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; import net.rptools.parser.function.EvaluationException; public class DropRoll extends AbstractNumberFunction { - public DropRoll() { - super(3, 3, false, "drop"); - } - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws EvaluationException { - int n = 0; - int times = ((BigDecimal) parameters.get(n++)).intValue(); - int sides = ((BigDecimal) parameters.get(n++)).intValue(); - int drop = ((BigDecimal) parameters.get(n++)).intValue(); + public DropRoll() { + super(3, 3, false, "drop"); + } - return new BigDecimal(DiceHelper.dropDice(times, sides, drop)); - } + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { + int n = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + int drop = ((BigDecimal) parameters.get(n++)).intValue(); + return new BigDecimal(DiceHelper.dropDice(times, sides, drop)); + } } diff --git a/src/main/java/net/rptools/common/expression/function/ExplodeDice.java b/src/main/java/net/rptools/common/expression/function/ExplodeDice.java index 318a8c3..9f7dfad 100755 --- a/src/main/java/net/rptools/common/expression/function/ExplodeDice.java +++ b/src/main/java/net/rptools/common/expression/function/ExplodeDice.java @@ -1,38 +1,38 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; import net.rptools.parser.function.EvaluationException; public class ExplodeDice extends AbstractNumberFunction { - public ExplodeDice() { - super(2, 2, false, "explode"); - } - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws EvaluationException { - int n = 0; - int times = ((BigDecimal) parameters.get(n++)).intValue(); - int sides = ((BigDecimal) parameters.get(n++)).intValue(); + public ExplodeDice() { + super(2, 2, false, "explode"); + } - return new BigDecimal(DiceHelper.explodeDice(times, sides)); - } + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { + int n = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + return new BigDecimal(DiceHelper.explodeDice(times, sides)); + } } diff --git a/src/main/java/net/rptools/common/expression/function/ExplodingSuccessDice.java b/src/main/java/net/rptools/common/expression/function/ExplodingSuccessDice.java index b8b3239..b7d03c0 100755 --- a/src/main/java/net/rptools/common/expression/function/ExplodingSuccessDice.java +++ b/src/main/java/net/rptools/common/expression/function/ExplodingSuccessDice.java @@ -1,39 +1,39 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; import net.rptools.parser.function.EvaluationException; public class ExplodingSuccessDice extends AbstractNumberFunction { - public ExplodingSuccessDice() { - super(3, 3, true, "explodingSuccess"); - } - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws EvaluationException { - int n = 0; - int times = ((BigDecimal) parameters.get(n++)).intValue(); - int sides = ((BigDecimal) parameters.get(n++)).intValue(); - int target = ((BigDecimal) parameters.get(n++)).intValue(); + public ExplodingSuccessDice() { + super(3, 3, true, "explodingSuccess"); + } - return DiceHelper.explodingSuccessDice(times, sides, target); - } + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { + int n = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + int target = ((BigDecimal) parameters.get(n++)).intValue(); + return DiceHelper.explodingSuccessDice(times, sides, target); + } } diff --git a/src/main/java/net/rptools/common/expression/function/FudgeRoll.java b/src/main/java/net/rptools/common/expression/function/FudgeRoll.java index dfe19de..a2f9ea1 100755 --- a/src/main/java/net/rptools/common/expression/function/FudgeRoll.java +++ b/src/main/java/net/rptools/common/expression/function/FudgeRoll.java @@ -1,46 +1,45 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; -/** fudge(range) - * - * Generate a random number form 1 to sides, times - * number of times. If times is not supplied it defaults to 1. - * - * Example: - * fudge(4) = 4dF (-4..4) +/** + * fudge(range) + * + *

Generate a random number form 1 to sides, times number of times. If + * times is not supplied it defaults to 1. + * + *

Example: fudge(4) = 4dF (-4..4) */ public class FudgeRoll extends AbstractNumberFunction { - public FudgeRoll() { - super(1, 1, false, "f", "fudge"); - } + public FudgeRoll() { + super(1, 1, false, "f", "fudge"); + } - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) { - int n = 0; + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) { + int n = 0; - int times = 1; - if (parameters.size() == 1) - times = ((BigDecimal) parameters.get(n++)).intValue(); + int times = 1; + if (parameters.size() == 1) times = ((BigDecimal) parameters.get(n++)).intValue(); - return new BigDecimal(DiceHelper.fudgeDice(times)); - } + return new BigDecimal(DiceHelper.fudgeDice(times)); + } } diff --git a/src/main/java/net/rptools/common/expression/function/HeroKillingRoll.java b/src/main/java/net/rptools/common/expression/function/HeroKillingRoll.java index b55d170..589cc51 100755 --- a/src/main/java/net/rptools/common/expression/function/HeroKillingRoll.java +++ b/src/main/java/net/rptools/common/expression/function/HeroKillingRoll.java @@ -1,21 +1,21 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.common.expression.RunData; import net.rptools.parser.Parser; import net.rptools.parser.ParserException; @@ -24,89 +24,87 @@ /* * Hero System Dice - * + * * Used to get both the stun & body of an attack roll. - * + * */ public class HeroKillingRoll extends AbstractNumberFunction { - public HeroKillingRoll() { - super(2, 3, false, "herokilling", "herokilling2", "killing", "heromultiplier", "multiplier"); - } + public HeroKillingRoll() { + super(2, 3, false, "herokilling", "herokilling2", "killing", "heromultiplier", "multiplier"); + } - // Use variable names with illegal character to minimize chances of variable overlap - private static String lastKillingBodyVar = "#Hero-LastKillingBodyVar"; + // Use variable names with illegal character to minimize chances of variable overlap + private static String lastKillingBodyVar = "#Hero-LastKillingBodyVar"; - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws ParserException { - int n = 0; + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws ParserException { + int n = 0; - double times = ((BigDecimal) parameters.get(n++)).doubleValue(); - int sides = ((BigDecimal) parameters.get(n++)).intValue(); - double half = times - Math.floor(times); - int extra = 0; - if (parameters.size() > 2) - extra = ((BigDecimal) parameters.get(n++)).intValue(); + double times = ((BigDecimal) parameters.get(n++)).doubleValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + double half = times - Math.floor(times); + int extra = 0; + if (parameters.size() > 2) extra = ((BigDecimal) parameters.get(n++)).intValue(); - VariableResolver vr = parser.getVariableResolver(); - RunData runData = RunData.getCurrent(); + VariableResolver vr = parser.getVariableResolver(); + RunData runData = RunData.getCurrent(); - if (functionName.equalsIgnoreCase("herokilling")) { + if (functionName.equalsIgnoreCase("herokilling")) { - int body = DiceHelper.rollDice((int) times, sides); - body = body + extra; - /* - * If value half or more roll a half die - */ - if (half >= 0.5) { - /* - * Roll a half dice. - */ - int die = runData.randomInt(sides); - body += (die + 1) / 2; - } else if (half >= 0.2) { - /* - * Add a single pip - */ - body++; - } + int body = DiceHelper.rollDice((int) times, sides); + body = body + extra; + /* + * If value half or more roll a half die + */ + if (half >= 0.5) { + /* + * Roll a half dice. + */ + int die = runData.randomInt(sides); + body += (die + 1) / 2; + } else if (half >= 0.2) { + /* + * Add a single pip + */ + body++; + } - parser.setVariable(lastKillingBodyVar, new BigDecimal(body)); - return new BigDecimal(body); - } else if (functionName.equalsIgnoreCase("herokilling2")) { + parser.setVariable(lastKillingBodyVar, new BigDecimal(body)); + return new BigDecimal(body); + } else if (functionName.equalsIgnoreCase("herokilling2")) { - int body = DiceHelper.rollDice((int) times, sides); - body = body + extra; - /* - * If value half or more roll a die -1. minimum value of 1. - */ - if (half >= 0.5) { - /* - * Roll a half dice. - */ - int die = runData.randomInt(sides); - if (die > 1) - die = die - 1; - body += die; - } else if (half >= 0.2) { - /* - * Add a single pip - */ - body++; - } + int body = DiceHelper.rollDice((int) times, sides); + body = body + extra; + /* + * If value half or more roll a die -1. minimum value of 1. + */ + if (half >= 0.5) { + /* + * Roll a half dice. + */ + int die = runData.randomInt(sides); + if (die > 1) die = die - 1; + body += die; + } else if (half >= 0.2) { + /* + * Add a single pip + */ + body++; + } - parser.setVariable(lastKillingBodyVar, new BigDecimal(body)); - return new BigDecimal(body); - } else { - int multi = DiceHelper.rollDice((int) times, sides); - multi = multi + extra; - if (multi < 1) - multi = 1; + parser.setVariable(lastKillingBodyVar, new BigDecimal(body)); + return new BigDecimal(body); + } else { + int multi = DiceHelper.rollDice((int) times, sides); + multi = multi + extra; + if (multi < 1) multi = 1; - int lastBody = 0; - if (vr.containsVariable(lastKillingBodyVar)) - lastBody = ((BigDecimal) vr.getVariable(lastKillingBodyVar)).intValue(); + int lastBody = 0; + if (vr.containsVariable(lastKillingBodyVar)) + lastBody = ((BigDecimal) vr.getVariable(lastKillingBodyVar)).intValue(); - return new BigDecimal(lastBody * multi); - } - } + return new BigDecimal(lastBody * multi); + } + } } diff --git a/src/main/java/net/rptools/common/expression/function/HeroRoll.java b/src/main/java/net/rptools/common/expression/function/HeroRoll.java index c42ee42..923262f 100755 --- a/src/main/java/net/rptools/common/expression/function/HeroRoll.java +++ b/src/main/java/net/rptools/common/expression/function/HeroRoll.java @@ -1,21 +1,21 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.common.expression.RunData; import net.rptools.parser.Parser; import net.rptools.parser.ParserException; @@ -24,94 +24,91 @@ /* * Hero System Dice - * + * * Used to get both the stun & body of an attack roll. - * + * */ public class HeroRoll extends AbstractNumberFunction { - public HeroRoll() { - super(2, 2, false, "hero", "herostun", "herobody"); - } - - // Use variable names with illegal character to minimize chances of variable overlap - private static String lastTimesVar = "#Hero-LastTimesVar"; - private static String lastSidesVar = "#Hero-LastSidesVar"; - private static String lastBodyVar = "#Hero-LastBodyVar"; - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws ParserException { - int n = 0; - - double times = ((BigDecimal) parameters.get(n++)).doubleValue(); - int sides = ((BigDecimal) parameters.get(n++)).intValue(); - - VariableResolver vr = parser.getVariableResolver(); - - if (functionName.equalsIgnoreCase("herobody")) { - double lastTimes = 0; - if (vr.containsVariable(lastTimesVar)) - lastTimes = ((BigDecimal) vr.getVariable(lastTimesVar)).doubleValue(); - - int lastSides = 0; - if (vr.containsVariable(lastSidesVar)) - lastSides = ((BigDecimal) vr.getVariable(lastSidesVar)).intValue(); - - int lastBody = 0; - if (vr.containsVariable(lastBodyVar)) - lastBody = ((BigDecimal) vr.getVariable(lastBodyVar)).intValue(); - - if (times == lastTimes && sides == lastSides) - return new BigDecimal(lastBody); - - return new BigDecimal(-1); // Should this be -1? Perhaps it should return null. - } else { - // assume stun - - double lastTimes = times; - int lastSides = sides; - int lastBody = 0; - - RunData runData = RunData.getCurrent(); - - int stun = 0; - double half = times - Math.floor(times); - for (int i = 0; i < Math.floor(times); i++) { - int die = runData.randomInt(sides); - /* - * Keep track of the body generated. In theory - * Hero System only uses 6-sided where a 1 is - * 0 body, 2-5 is 1 body and 6 is 2 body but I - * left the sides unbounded just in case. - */ - if (die > 1) - lastBody++; - if (die == sides) - lastBody++; - - stun += die; - } - - if (half >= 0.5) { - /* - * Roll a half dice. In theory Hero System - * only uses 6-sided and for half dice - * 1 & 2 = 1 Stun 0 body - * 3 = 2 stun 0 body - * 4 = 2 stun 1 body - * 5 & 6 = 3 stun 1 body - */ - int die = runData.randomInt(sides); - if (die * 2 > sides) - lastBody++; - - stun += (die + 1) / 2; - } - - parser.setVariable(lastTimesVar, new BigDecimal(lastTimes)); - parser.setVariable(lastSidesVar, new BigDecimal(lastSides)); - parser.setVariable(lastBodyVar, new BigDecimal(lastBody)); - - return new BigDecimal(stun); - } - } + public HeroRoll() { + super(2, 2, false, "hero", "herostun", "herobody"); + } + + // Use variable names with illegal character to minimize chances of variable overlap + private static String lastTimesVar = "#Hero-LastTimesVar"; + private static String lastSidesVar = "#Hero-LastSidesVar"; + private static String lastBodyVar = "#Hero-LastBodyVar"; + + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws ParserException { + int n = 0; + + double times = ((BigDecimal) parameters.get(n++)).doubleValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + + VariableResolver vr = parser.getVariableResolver(); + + if (functionName.equalsIgnoreCase("herobody")) { + double lastTimes = 0; + if (vr.containsVariable(lastTimesVar)) + lastTimes = ((BigDecimal) vr.getVariable(lastTimesVar)).doubleValue(); + + int lastSides = 0; + if (vr.containsVariable(lastSidesVar)) + lastSides = ((BigDecimal) vr.getVariable(lastSidesVar)).intValue(); + + int lastBody = 0; + if (vr.containsVariable(lastBodyVar)) + lastBody = ((BigDecimal) vr.getVariable(lastBodyVar)).intValue(); + + if (times == lastTimes && sides == lastSides) return new BigDecimal(lastBody); + + return new BigDecimal(-1); // Should this be -1? Perhaps it should return null. + } else { + // assume stun + + double lastTimes = times; + int lastSides = sides; + int lastBody = 0; + + RunData runData = RunData.getCurrent(); + + int stun = 0; + double half = times - Math.floor(times); + for (int i = 0; i < Math.floor(times); i++) { + int die = runData.randomInt(sides); + /* + * Keep track of the body generated. In theory + * Hero System only uses 6-sided where a 1 is + * 0 body, 2-5 is 1 body and 6 is 2 body but I + * left the sides unbounded just in case. + */ + if (die > 1) lastBody++; + if (die == sides) lastBody++; + + stun += die; + } + + if (half >= 0.5) { + /* + * Roll a half dice. In theory Hero System + * only uses 6-sided and for half dice + * 1 & 2 = 1 Stun 0 body + * 3 = 2 stun 0 body + * 4 = 2 stun 1 body + * 5 & 6 = 3 stun 1 body + */ + int die = runData.randomInt(sides); + if (die * 2 > sides) lastBody++; + + stun += (die + 1) / 2; + } + + parser.setVariable(lastTimesVar, new BigDecimal(lastTimes)); + parser.setVariable(lastSidesVar, new BigDecimal(lastSides)); + parser.setVariable(lastBodyVar, new BigDecimal(lastBody)); + + return new BigDecimal(stun); + } + } } diff --git a/src/main/java/net/rptools/common/expression/function/If.java b/src/main/java/net/rptools/common/expression/function/If.java index 30a2019..4405633 100755 --- a/src/main/java/net/rptools/common/expression/function/If.java +++ b/src/main/java/net/rptools/common/expression/function/If.java @@ -1,37 +1,36 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractFunction; import net.rptools.parser.function.EvaluationException; public class If extends AbstractFunction { - public If() { - super(3, 3, false, "if"); - } - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws EvaluationException { - if (BigDecimal.ZERO.equals((BigDecimal) parameters.get(0))) - return parameters.get(2); + public If() { + super(3, 3, false, "if"); + } - return parameters.get(1); - } + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { + if (BigDecimal.ZERO.equals((BigDecimal) parameters.get(0))) return parameters.get(2); + return parameters.get(1); + } } diff --git a/src/main/java/net/rptools/common/expression/function/KeepLowestRoll.java b/src/main/java/net/rptools/common/expression/function/KeepLowestRoll.java new file mode 100755 index 0000000..361db5f --- /dev/null +++ b/src/main/java/net/rptools/common/expression/function/KeepLowestRoll.java @@ -0,0 +1,39 @@ +/* + * 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 + * and specifically the Affero license + * text at . + */ +package net.rptools.common.expression.function; + +import java.math.BigDecimal; +import java.util.List; +import net.rptools.parser.Parser; +import net.rptools.parser.function.AbstractNumberFunction; +import net.rptools.parser.function.EvaluationException; + +public class KeepLowestRoll extends AbstractNumberFunction { + + public KeepLowestRoll() { + super(3, 3, false, "keepLowest"); + } + + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { + int n = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + int keep = ((BigDecimal) parameters.get(n++)).intValue(); + + return new BigDecimal(DiceHelper.keepLowestDice(times, sides, keep)); + } +} diff --git a/src/main/java/net/rptools/common/expression/function/KeepRoll.java b/src/main/java/net/rptools/common/expression/function/KeepRoll.java index 9ad65ad..9a064f9 100755 --- a/src/main/java/net/rptools/common/expression/function/KeepRoll.java +++ b/src/main/java/net/rptools/common/expression/function/KeepRoll.java @@ -1,39 +1,39 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; import net.rptools.parser.function.EvaluationException; public class KeepRoll extends AbstractNumberFunction { - public KeepRoll() { - super(3, 3, false, "keep"); - } - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws EvaluationException { - int n = 0; - int times = ((BigDecimal) parameters.get(n++)).intValue(); - int sides = ((BigDecimal) parameters.get(n++)).intValue(); - int keep = ((BigDecimal) parameters.get(n++)).intValue(); + public KeepRoll() { + super(3, 3, false, "keep"); + } - return new BigDecimal(DiceHelper.keepDice(times, sides, keep)); - } + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { + int n = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + int keep = ((BigDecimal) parameters.get(n++)).intValue(); + return new BigDecimal(DiceHelper.keepDice(times, sides, keep)); + } } diff --git a/src/main/java/net/rptools/common/expression/function/OpenTestDice.java b/src/main/java/net/rptools/common/expression/function/OpenTestDice.java index b832e8e..7e5b62a 100755 --- a/src/main/java/net/rptools/common/expression/function/OpenTestDice.java +++ b/src/main/java/net/rptools/common/expression/function/OpenTestDice.java @@ -1,38 +1,38 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; import net.rptools.parser.function.EvaluationException; public class OpenTestDice extends AbstractNumberFunction { - public OpenTestDice() { - super(2, 2, true, "openTest"); - } - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws EvaluationException { - int n = 0; - int times = ((BigDecimal) parameters.get(n++)).intValue(); - int sides = ((BigDecimal) parameters.get(n++)).intValue(); + public OpenTestDice() { + super(2, 2, true, "openTest"); + } - return DiceHelper.openTestDice(times, sides); - } + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { + int n = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + return DiceHelper.openTestDice(times, sides); + } } diff --git a/src/main/java/net/rptools/common/expression/function/RerollDice.java b/src/main/java/net/rptools/common/expression/function/RerollDice.java index 4f07c70..4225471 100755 --- a/src/main/java/net/rptools/common/expression/function/RerollDice.java +++ b/src/main/java/net/rptools/common/expression/function/RerollDice.java @@ -1,39 +1,39 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; import net.rptools.parser.function.EvaluationException; public class RerollDice extends AbstractNumberFunction { - public RerollDice() { - super(3, 3, false, "reroll"); - } - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws EvaluationException { - int n = 0; - int times = ((BigDecimal) parameters.get(n++)).intValue(); - int sides = ((BigDecimal) parameters.get(n++)).intValue(); - int lowerBound = ((BigDecimal) parameters.get(n++)).intValue(); + public RerollDice() { + super(3, 3, false, "reroll"); + } - return new BigDecimal(DiceHelper.rerollDice(times, sides, lowerBound)); - } + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { + int n = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); + int lowerBound = ((BigDecimal) parameters.get(n++)).intValue(); + return new BigDecimal(DiceHelper.rerollDice(times, sides, lowerBound)); + } } diff --git a/src/main/java/net/rptools/common/expression/function/Roll.java b/src/main/java/net/rptools/common/expression/function/Roll.java index fc2521b..bb3aaae 100755 --- a/src/main/java/net/rptools/common/expression/function/Roll.java +++ b/src/main/java/net/rptools/common/expression/function/Roll.java @@ -1,48 +1,47 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; -/** roll(range) or roll(times, range) - * - * Generate a random number form 1 to sides, times - * number of times. If times is not supplied it defaults to 1. - * - * Example: - * roll(4, 6) = 4d6 +/** + * roll(range) or roll(times, range) + * + *

Generate a random number form 1 to sides, times number of times. If + * times is not supplied it defaults to 1. + * + *

Example: roll(4, 6) = 4d6 */ public class Roll extends AbstractNumberFunction { - public Roll() { - super(1, 2, false, "d", "roll", "dice"); - } + public Roll() { + super(1, 2, false, "d", "roll", "dice"); + } - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) { - int n = 0; + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) { + int n = 0; - int times = 1; - if (parameters.size() == 2) - times = ((BigDecimal) parameters.get(n++)).intValue(); + int times = 1; + if (parameters.size() == 2) times = ((BigDecimal) parameters.get(n++)).intValue(); - int sides = ((BigDecimal) parameters.get(n++)).intValue(); + int sides = ((BigDecimal) parameters.get(n++)).intValue(); - return new BigDecimal(DiceHelper.rollDice(times, sides)); - } + return new BigDecimal(DiceHelper.rollDice(times, sides)); + } } diff --git a/src/main/java/net/rptools/common/expression/function/RollWithBounds.java b/src/main/java/net/rptools/common/expression/function/RollWithBounds.java new file mode 100644 index 0000000..57ef212 --- /dev/null +++ b/src/main/java/net/rptools/common/expression/function/RollWithBounds.java @@ -0,0 +1,78 @@ +/* + * 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 + * and specifically the Affero license + * text at . + */ +package net.rptools.common.expression.function; + +import java.math.BigDecimal; +import java.util.List; +import net.rptools.parser.Parser; +import net.rptools.parser.ParserException; +import net.rptools.parser.function.AbstractNumberFunction; + +public class RollWithBounds extends AbstractNumberFunction { + + public RollWithBounds() { + super( + 3, + 4, + false, + "rollSubWithLower", + "rollWithLower", + "rollAddWithUpper", + "rollWithUpper", + "rollAddWithLower"); + } + + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws ParserException { + int times = 0; + int sides = 0; + int mod = 0; + int lower = Integer.MIN_VALUE; + int upper = Integer.MAX_VALUE; + + switch (functionName) { + case "rollSubWithLower": + times = ((BigDecimal) parameters.get(0)).intValue(); + sides = ((BigDecimal) parameters.get(1)).intValue(); + mod = -((BigDecimal) parameters.get(2)).intValue(); + lower = ((BigDecimal) parameters.get(3)).intValue(); + break; + case "rollWithLower": + times = ((BigDecimal) parameters.get(0)).intValue(); + sides = ((BigDecimal) parameters.get(1)).intValue(); + lower = ((BigDecimal) parameters.get(2)).intValue(); + break; + case "rollAddWithUpper": + times = ((BigDecimal) parameters.get(0)).intValue(); + sides = ((BigDecimal) parameters.get(1)).intValue(); + mod = ((BigDecimal) parameters.get(2)).intValue(); + upper = ((BigDecimal) parameters.get(3)).intValue(); + break; + case "rollWithUpper": + times = ((BigDecimal) parameters.get(0)).intValue(); + sides = ((BigDecimal) parameters.get(1)).intValue(); + upper = ((BigDecimal) parameters.get(2)).intValue(); + break; + case "rollAddWithLower": + times = ((BigDecimal) parameters.get(0)).intValue(); + sides = ((BigDecimal) parameters.get(1)).intValue(); + mod = ((BigDecimal) parameters.get(2)).intValue(); + lower = ((BigDecimal) parameters.get(3)).intValue(); + break; + } + return DiceHelper.rollModWithBounds(times, sides, mod, lower, upper); + } +} diff --git a/src/main/java/net/rptools/common/expression/function/ShadowRun4Dice.java b/src/main/java/net/rptools/common/expression/function/ShadowRun4Dice.java index 4ec012c..5595df4 100755 --- a/src/main/java/net/rptools/common/expression/function/ShadowRun4Dice.java +++ b/src/main/java/net/rptools/common/expression/function/ShadowRun4Dice.java @@ -1,41 +1,40 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; import net.rptools.parser.function.EvaluationException; public class ShadowRun4Dice extends AbstractNumberFunction { - public ShadowRun4Dice() { - super(1, 2, true, "sr4"); - } - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws EvaluationException { + public ShadowRun4Dice() { + super(1, 2, true, "sr4"); + } - int n = 0; - int gremlins = 0; - int times = ((BigDecimal) parameters.get(n++)).intValue(); - if (parameters.size() == 2) - gremlins = ((BigDecimal) parameters.get(n++)).intValue(); + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { - return DiceHelper.countShadowRun4(times, gremlins, false); - } + int n = 0; + int gremlins = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + if (parameters.size() == 2) gremlins = ((BigDecimal) parameters.get(n++)).intValue(); + return DiceHelper.countShadowRun4(times, gremlins, false); + } } diff --git a/src/main/java/net/rptools/common/expression/function/ShadowRun4ExplodeDice.java b/src/main/java/net/rptools/common/expression/function/ShadowRun4ExplodeDice.java index 3ffb76c..b5c0811 100755 --- a/src/main/java/net/rptools/common/expression/function/ShadowRun4ExplodeDice.java +++ b/src/main/java/net/rptools/common/expression/function/ShadowRun4ExplodeDice.java @@ -1,41 +1,40 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; import net.rptools.parser.function.EvaluationException; public class ShadowRun4ExplodeDice extends AbstractNumberFunction { - public ShadowRun4ExplodeDice() { - super(1, 2, true, "sr4e"); - } - - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) throws EvaluationException { + public ShadowRun4ExplodeDice() { + super(1, 2, true, "sr4e"); + } - int n = 0; - int gremlins = 0; - int times = ((BigDecimal) parameters.get(n++)).intValue(); - if (parameters.size() == 2) - gremlins = ((BigDecimal) parameters.get(n++)).intValue(); + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) + throws EvaluationException { - return DiceHelper.countShadowRun4(times, gremlins, true); - } + int n = 0; + int gremlins = 0; + int times = ((BigDecimal) parameters.get(n++)).intValue(); + if (parameters.size() == 2) gremlins = ((BigDecimal) parameters.get(n++)).intValue(); + return DiceHelper.countShadowRun4(times, gremlins, true); + } } diff --git a/src/main/java/net/rptools/common/expression/function/UbiquityRoll.java b/src/main/java/net/rptools/common/expression/function/UbiquityRoll.java index 5a26355..2d21f31 100755 --- a/src/main/java/net/rptools/common/expression/function/UbiquityRoll.java +++ b/src/main/java/net/rptools/common/expression/function/UbiquityRoll.java @@ -1,46 +1,45 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; import java.util.List; - import net.rptools.parser.Parser; import net.rptools.parser.function.AbstractNumberFunction; -/** ubiquity(range) - * - * Generate a random number form 1 to sides, times - * number of times. If times is not supplied it defaults to 1. - * - * Example: - * ubiquity(4) = 4dU (0..4 successes) +/** + * ubiquity(range) + * + *

Generate a random number form 1 to sides, times number of times. If + * times is not supplied it defaults to 1. + * + *

Example: ubiquity(4) = 4dU (0..4 successes) */ public class UbiquityRoll extends AbstractNumberFunction { - public UbiquityRoll() { - super(1, 1, false, "u", "ubiquity"); - } + public UbiquityRoll() { + super(1, 1, false, "u", "ubiquity"); + } - @Override - public Object childEvaluate(Parser parser, String functionName, List parameters) { - int n = 0; + @Override + public Object childEvaluate(Parser parser, String functionName, List parameters) { + int n = 0; - int times = 1; - if (parameters.size() == 1) - times = ((BigDecimal) parameters.get(n++)).intValue(); + int times = 1; + if (parameters.size() == 1) times = ((BigDecimal) parameters.get(n++)).intValue(); - return new BigDecimal(DiceHelper.ubiquityDice(times)); - } + return new BigDecimal(DiceHelper.ubiquityDice(times)); + } } diff --git a/src/test/java/net/rptools/common/expression/ExpressionParserTest.java b/src/test/java/net/rptools/common/expression/ExpressionParserTest.java index 617be58..0c1ad05 100755 --- a/src/test/java/net/rptools/common/expression/ExpressionParserTest.java +++ b/src/test/java/net/rptools/common/expression/ExpressionParserTest.java @@ -1,15 +1,16 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression; @@ -17,236 +18,235 @@ import java.math.BigInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; - import junit.framework.TestCase; -import net.rptools.parser.Parser; import net.rptools.parser.ParserException; -import net.rptools.parser.function.EvaluationException; -import net.rptools.parser.function.ParameterException; public class ExpressionParserTest extends TestCase { - public void testEvaluate() throws ParserException { - Result result = new ExpressionParser().evaluate("100+4d1*10"); - - assertNotNull(result); - assertEquals("100+4d1*10", result.getExpression()); - assertEquals("100 + 4 * 10", result.getDetailExpression()); - assertEquals(new BigDecimal(140), (BigDecimal) result.getValue()); - } - - public void testEvaluate_Explode() throws ParserException { - RunData.setSeed(10423L); - Result result = new ExpressionParser().evaluate("100+10d6e+1"); - - assertEquals(new BigDecimal(164), result.getValue()); - } - - public void testEvaluate_Drop() throws ParserException { - RunData.setSeed(10423L); - Result result = new ExpressionParser().evaluate("100+10d6d2+1"); - - assertEquals(new BigDecimal(138), result.getValue()); - } - - public void testEvaluate_Keep() throws ParserException { - RunData.setSeed(10423L); - Result result = new ExpressionParser().evaluate("100+10d6k8+1"); - - assertEquals(new BigDecimal(138), result.getValue()); - } - - public void testEvaluate_CountSuccess() throws ParserException { - RunData.setSeed(10423L); - Result result = new ExpressionParser().evaluate("100+10d6s4+1"); - - assertEquals(new BigDecimal(109), result.getValue()); - } - - public void testEvaluate_ExplodingSuccess() throws ParserException { - RunData.setSeed(10423L); - Result result = new ExpressionParser().evaluate("10d4es6"); - assertEquals("10d4es6", result.getExpression()); - assertEquals("explodingSuccess(10, 4, 6)", result.getDetailExpression()); - assertEquals("Dice: 1, 2, 2, 1, 2, 7, 1, 7, 2, 3, Successes: 2", result.getValue()); - RunData.setSeed(10423L); - - result = new ExpressionParser().evaluate("10es9"); - assertEquals("10es9", result.getExpression()); - assertEquals("explodingSuccess(10, 6, 9)", result.getDetailExpression()); - assertEquals("Dice: 4, 4, 4, 3, 16, 5, 1, 4, 14, 8, Successes: 2", result.getValue()); - } - - public void testEvaluate_OpenTest() throws ParserException { - RunData.setSeed(10423L); - Result result = new ExpressionParser().evaluate("10d4o"); - assertEquals("10d4o", result.getExpression()); - assertEquals("openTest(10, 4)", result.getDetailExpression()); - assertEquals("Dice: 1, 2, 2, 1, 2, 7, 1, 7, 2, 3, Maximum: 7", result.getValue()); - - RunData.setSeed(10423L); - result = new ExpressionParser().evaluate("10o"); - assertEquals("10o", result.getExpression()); - assertEquals("openTest(10, 6)", result.getDetailExpression()); - assertEquals("Dice: 4, 4, 4, 3, 16, 5, 1, 4, 14, 8, Maximum: 16", result.getValue()); - } - - public void testEvaluate_SR4Success() throws ParserException { - RunData.setSeed(10523L); - Result result = new ExpressionParser().evaluate("5sr4"); - assertEquals("5sr4", result.getExpression()); - assertEquals("sr4(5)", result.getDetailExpression()); - assertEquals("Hits: 1 Ones: 1 Results: 3 1 4 6 3 ", result.getValue()); - } - - public void testEvaluate_SR4GremlinSuccess() throws ParserException { - RunData.setSeed(10523L); - Result result = new ExpressionParser().evaluate("5sr4g2"); - assertEquals("5sr4g2", result.getExpression()); - assertEquals("sr4(5, 2)", result.getDetailExpression()); - assertEquals("Hits: 1 Ones: 1 *Glitch* Results: 3 1 4 6 3 ", result.getValue()); - } - - public void testEvaluate_SR4ExplodingSuccess() throws ParserException { - RunData.setSeed(10523L); - Result result = new ExpressionParser().evaluate("5sr4e"); - assertEquals("5sr4e", result.getExpression()); - assertEquals("sr4e(5)", result.getDetailExpression()); - assertEquals("Hits: 1 Ones: 2 Results: 3 1 4 6 3 1 ", result.getValue()); - } - - public void testEvaluate_SR4ExplodingGremlinSuccess() throws ParserException { - RunData.setSeed(10523L); - Result result = new ExpressionParser().evaluate("5sr4eg2"); - assertEquals("5sr4eg2", result.getExpression()); - assertEquals("sr4e(5, 2)", result.getDetailExpression()); - assertEquals("Hits: 1 Ones: 2 *Glitch* Results: 3 1 4 6 3 1 ", result.getValue()); - } - - public void testEvaluate_HeroRoll() throws ParserException { - RunData.setSeed(10423L); - ExpressionParser parser = new ExpressionParser(); - - Result result = parser.evaluate("4.5d6h"); - assertEquals(new BigDecimal(18), result.getValue()); - - result = parser.evaluate("4.5d6b"); - assertEquals(new BigDecimal(5), result.getValue()); - - RunData.setSeed(10423L); - parser = new ExpressionParser(); - - result = parser.evaluate("4d6h"); - assertEquals(new BigDecimal(15), result.getValue()); - - result = parser.evaluate("4d6b"); - assertEquals(new BigDecimal(4), result.getValue()); - } - - public void testEvaluate_FudgeRoll() throws ParserException { - RunData.setSeed(10423L); - ExpressionParser parser = new ExpressionParser(); - - Result result = parser.evaluate("dF"); - assertEquals(new BigDecimal(-1), result.getValue()); - - result = parser.evaluate("4df"); - assertEquals(new BigDecimal(0), result.getValue()); - - // Don't parse df in the middle of things - parser.getParser().getVariableResolver().setVariable("asdfg", new BigDecimal(10)); - result = parser.evaluate("asdfg"); - assertEquals(new BigDecimal(10), result.getValue()); - } - - public void testEvaluate_UbiquityRoll() throws ParserException { - RunData.setSeed(10423L); - ExpressionParser parser = new ExpressionParser(); - - Result result = parser.evaluate("dU"); - assertEquals(new BigDecimal(0), result.getValue()); - - result = parser.evaluate("10du"); - assertEquals(new BigDecimal(4), result.getValue()); - - // Don't parse a uf in the middle of other things - parser.getParser().getVariableResolver().setVariable("asufg", new BigDecimal(10)); - result = parser.evaluate("asufg"); - assertEquals(new BigDecimal(10), result.getValue()); - } - - public void testEvaluate_ColorHex() throws ParserException { - RunData.setSeed(10423L); - ExpressionParser parser = new ExpressionParser(); - - Result result = parser.evaluate("#FF0000"); - assertEquals(new BigDecimal(new BigInteger("FF0000", 16)), result.getValue()); - - result = parser.evaluate("#00FF0000"); - assertEquals(new BigDecimal(new BigInteger("FF0000", 16)), result.getValue()); - - result = parser.evaluate("#FF0"); - assertEquals(new BigDecimal(new BigInteger("FFFF00", 16)), result.getValue()); - } - - public void testEvaluate_If() throws ParserException { - ExpressionParser parser = new ExpressionParser(); - - evaluateExpression(parser, "if(10 > 2, 10, 2)", new BigDecimal(10)); - evaluateExpression(parser, "if(10 < 2, 10, 2)", new BigDecimal(2)); - evaluateStringExpression(parser, "if(10 < 2, 's1', 's2')", "s2"); - evaluateStringExpression(parser, "if(10 > 2, 's1', 's2')", "s1"); - } + public void testEvaluate() throws ParserException { + Result result = new ExpressionParser().evaluate("100+4d1*10"); + + assertNotNull(result); + assertEquals("100+4d1*10", result.getExpression()); + assertEquals("100 + 4 * 10", result.getDetailExpression()); + assertEquals(new BigDecimal(140), (BigDecimal) result.getValue()); + } + + public void testEvaluate_Explode() throws ParserException { + RunData.setSeed(10423L); + Result result = new ExpressionParser().evaluate("100+10d6e+1"); + + assertEquals(new BigDecimal(164), result.getValue()); + } + + public void testEvaluate_Drop() throws ParserException { + RunData.setSeed(10423L); + Result result = new ExpressionParser().evaluate("100+10d6d2+1"); + + assertEquals(new BigDecimal(138), result.getValue()); + } + + public void testEvaluate_Keep() throws ParserException { + RunData.setSeed(10423L); + Result result = new ExpressionParser().evaluate("100+10d6k8+1"); + + assertEquals(new BigDecimal(138), result.getValue()); + } + + public void testEvaluate_CountSuccess() throws ParserException { + RunData.setSeed(10423L); + Result result = new ExpressionParser().evaluate("100+10d6s4+1"); + + assertEquals(new BigDecimal(109), result.getValue()); + } + + public void testEvaluate_ExplodingSuccess() throws ParserException { + RunData.setSeed(10423L); + Result result = new ExpressionParser().evaluate("10d4es6"); + assertEquals("10d4es6", result.getExpression()); + assertEquals("explodingSuccess(10, 4, 6)", result.getDetailExpression()); + assertEquals("Dice: 1, 2, 2, 1, 2, 7, 1, 7, 2, 3, Successes: 2", result.getValue()); + RunData.setSeed(10423L); + + result = new ExpressionParser().evaluate("10es9"); + assertEquals("10es9", result.getExpression()); + assertEquals("explodingSuccess(10, 6, 9)", result.getDetailExpression()); + assertEquals("Dice: 4, 4, 4, 3, 16, 5, 1, 4, 14, 8, Successes: 2", result.getValue()); + } + + public void testEvaluate_OpenTest() throws ParserException { + RunData.setSeed(10423L); + Result result = new ExpressionParser().evaluate("10d4o"); + assertEquals("10d4o", result.getExpression()); + assertEquals("openTest(10, 4)", result.getDetailExpression()); + assertEquals("Dice: 1, 2, 2, 1, 2, 7, 1, 7, 2, 3, Maximum: 7", result.getValue()); + + RunData.setSeed(10423L); + result = new ExpressionParser().evaluate("10o"); + assertEquals("10o", result.getExpression()); + assertEquals("openTest(10, 6)", result.getDetailExpression()); + assertEquals("Dice: 4, 4, 4, 3, 16, 5, 1, 4, 14, 8, Maximum: 16", result.getValue()); + } + + public void testEvaluate_SR4Success() throws ParserException { + RunData.setSeed(10523L); + Result result = new ExpressionParser().evaluate("5sr4"); + assertEquals("5sr4", result.getExpression()); + assertEquals("sr4(5)", result.getDetailExpression()); + assertEquals("Hits: 1 Ones: 1 Results: 3 1 4 6 3 ", result.getValue()); + } + + public void testEvaluate_SR4GremlinSuccess() throws ParserException { + RunData.setSeed(10523L); + Result result = new ExpressionParser().evaluate("5sr4g2"); + assertEquals("5sr4g2", result.getExpression()); + assertEquals("sr4(5, 2)", result.getDetailExpression()); + assertEquals("Hits: 1 Ones: 1 *Glitch* Results: 3 1 4 6 3 ", result.getValue()); + } + + public void testEvaluate_SR4ExplodingSuccess() throws ParserException { + RunData.setSeed(10523L); + Result result = new ExpressionParser().evaluate("5sr4e"); + assertEquals("5sr4e", result.getExpression()); + assertEquals("sr4e(5)", result.getDetailExpression()); + assertEquals("Hits: 1 Ones: 2 Results: 3 1 4 6 3 1 ", result.getValue()); + } + + public void testEvaluate_SR4ExplodingGremlinSuccess() throws ParserException { + RunData.setSeed(10523L); + Result result = new ExpressionParser().evaluate("5sr4eg2"); + assertEquals("5sr4eg2", result.getExpression()); + assertEquals("sr4e(5, 2)", result.getDetailExpression()); + assertEquals("Hits: 1 Ones: 2 *Glitch* Results: 3 1 4 6 3 1 ", result.getValue()); + } + + public void testEvaluate_HeroRoll() throws ParserException { + RunData.setSeed(10423L); + ExpressionParser parser = new ExpressionParser(); + + Result result = parser.evaluate("4.5d6h"); + assertEquals(new BigDecimal(18), result.getValue()); + + result = parser.evaluate("4.5d6b"); + assertEquals(new BigDecimal(5), result.getValue()); + + RunData.setSeed(10423L); + parser = new ExpressionParser(); + + result = parser.evaluate("4d6h"); + assertEquals(new BigDecimal(15), result.getValue()); + + result = parser.evaluate("4d6b"); + assertEquals(new BigDecimal(4), result.getValue()); + } + + public void testEvaluate_FudgeRoll() throws ParserException { + RunData.setSeed(10423L); + ExpressionParser parser = new ExpressionParser(); + + Result result = parser.evaluate("dF"); + assertEquals(new BigDecimal(-1), result.getValue()); + + result = parser.evaluate("4df"); + assertEquals(new BigDecimal(0), result.getValue()); + + // Don't parse df in the middle of things + parser.getParser().getVariableResolver().setVariable("asdfg", new BigDecimal(10)); + result = parser.evaluate("asdfg"); + assertEquals(new BigDecimal(10), result.getValue()); + } + + public void testEvaluate_UbiquityRoll() throws ParserException { + RunData.setSeed(10423L); + ExpressionParser parser = new ExpressionParser(); + + Result result = parser.evaluate("dU"); + assertEquals(new BigDecimal(0), result.getValue()); + + result = parser.evaluate("10du"); + assertEquals(new BigDecimal(4), result.getValue()); + + // Don't parse a uf in the middle of other things + parser.getParser().getVariableResolver().setVariable("asufg", new BigDecimal(10)); + result = parser.evaluate("asufg"); + assertEquals(new BigDecimal(10), result.getValue()); + } + + public void testEvaluate_ColorHex() throws ParserException { + RunData.setSeed(10423L); + ExpressionParser parser = new ExpressionParser(); + + Result result = parser.evaluate("#FF0000"); + assertEquals(new BigDecimal(new BigInteger("FF0000", 16)), result.getValue()); + + result = parser.evaluate("#00FF0000"); + assertEquals(new BigDecimal(new BigInteger("FF0000", 16)), result.getValue()); + + result = parser.evaluate("#FF0"); + assertEquals(new BigDecimal(new BigInteger("FFFF00", 16)), result.getValue()); + } + + public void testEvaluate_If() throws ParserException { + ExpressionParser parser = new ExpressionParser(); + + evaluateExpression(parser, "if(10 > 2, 10, 2)", new BigDecimal(10)); + evaluateExpression(parser, "if(10 < 2, 10, 2)", new BigDecimal(2)); + evaluateStringExpression(parser, "if(10 < 2, 's1', 's2')", "s2"); + evaluateStringExpression(parser, "if(10 > 2, 's1', 's2')", "s1"); + } - public void testEvaluate_Multiline() throws ParserException { - RunData.setSeed(10423L); - ExpressionParser parser = new ExpressionParser(); + public void testEvaluate_Multiline() throws ParserException { + RunData.setSeed(10423L); + ExpressionParser parser = new ExpressionParser(); - evaluateExpression(parser, "10 + \r\n d6 + \n 2", new BigDecimal(16)); + evaluateExpression(parser, "10 + \r\n d6 + \n 2", new BigDecimal(16)); - String s = "10 + // Constant expression\n" + - "2 + // Another bit\n" + - "d20 // The roll\n"; + String s = "10 + // Constant expression\n" + "2 + // Another bit\n" + "d20 // The roll\n"; - evaluateExpression(parser, s, new BigDecimal(26)); - } + evaluateExpression(parser, s, new BigDecimal(26)); + } - public void testMultilineRegex() { - String str1 = "one two three"; - String str2 = "one two\nthree"; + public void testMultilineRegex() { + String str1 = "one two three"; + String str2 = "one two\nthree"; - Pattern p1 = Pattern.compile("^one(.*)three$"); - Pattern p2 = Pattern.compile("one(.*)three", Pattern.MULTILINE); + Pattern p1 = Pattern.compile("^one(.*)three$"); + Pattern p2 = Pattern.compile("one(.*)three", Pattern.MULTILINE); - Matcher m1 = p1.matcher(str1); - Matcher m2 = p2.matcher(str2); + Matcher m1 = p1.matcher(str1); + Matcher m2 = p2.matcher(str2); - System.out.println(m1.matches()); - System.out.println(m2.matches()); - } + System.out.println(m1.matches()); + System.out.println(m2.matches()); + } - public void testNoTransformInStrings() throws ParserException { - ExpressionParser parser = new ExpressionParser(); + public void testNoTransformInStrings() throws ParserException { + ExpressionParser parser = new ExpressionParser(); - evaluateStringExpression(parser, "'10' + 'd10'", "10d10"); - } + evaluateStringExpression(parser, "'10' + 'd10'", "10d10"); + } - public void testVariableRegexOverlaps() throws ParserException { - ExpressionParser parser = new ExpressionParser(); - parser.getParser().setVariable("food10", new BigDecimal(10)); + public void testVariableRegexOverlaps() throws ParserException { + ExpressionParser parser = new ExpressionParser(); + parser.getParser().setVariable("food10", new BigDecimal(10)); - evaluateExpression(parser, "food10 + 10", new BigDecimal(20)); - } + evaluateExpression(parser, "food10 + 10", new BigDecimal(20)); + } - private void evaluateExpression(ExpressionParser p, String expression, BigDecimal answer) throws ParserException { - Result result = p.evaluate(expression); - assertTrue(String.format("%s evaluated incorrectly expected <%s> but was <%s>", expression, answer, result.getValue()), - answer.compareTo((BigDecimal) result.getValue()) == 0); - } + private void evaluateExpression(ExpressionParser p, String expression, BigDecimal answer) + throws ParserException { + Result result = p.evaluate(expression); + assertTrue( + String.format( + "%s evaluated incorrectly expected <%s> but was <%s>", + expression, answer, result.getValue()), + answer.compareTo((BigDecimal) result.getValue()) == 0); + } - private void evaluateStringExpression(ExpressionParser p, String expression, String answer) throws ParserException { - Result result = p.evaluate(expression); + private void evaluateStringExpression(ExpressionParser p, String expression, String answer) + throws ParserException { + Result result = p.evaluate(expression); - assertEquals(answer, result.getValue()); - } + assertEquals(answer, result.getValue()); + } } diff --git a/src/test/java/net/rptools/common/expression/RunDataTest.java b/src/test/java/net/rptools/common/expression/RunDataTest.java index b5d202d..d42d365 100755 --- a/src/test/java/net/rptools/common/expression/RunDataTest.java +++ b/src/test/java/net/rptools/common/expression/RunDataTest.java @@ -1,15 +1,16 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression; @@ -17,21 +18,21 @@ public class RunDataTest extends TestCase { - public void testRandomIntInt() { - RunData runData = new RunData(null); + public void testRandomIntInt() { + RunData runData = new RunData(null); - for (int i = 0; i < 10000; i++) { - int value = runData.randomInt(10); - assertTrue(1 <= value && value <= 10); - } - } + for (int i = 0; i < 10000; i++) { + int value = runData.randomInt(10); + assertTrue(1 <= value && value <= 10); + } + } - public void testRandomIntIntInt() { - RunData runData = new RunData(null); + public void testRandomIntIntInt() { + RunData runData = new RunData(null); - for (int i = 0; i < 10000; i++) { - int value = runData.randomInt(10, 20); - assertTrue(String.format("Value outside range: %s", value), 10 <= value && value <= 20); - } - } + for (int i = 0; i < 10000; i++) { + int value = runData.randomInt(10, 20); + assertTrue(String.format("Value outside range: %s", value), 10 <= value && value <= 20); + } + } } diff --git a/src/test/java/net/rptools/common/expression/function/DiceHelperTest.java b/src/test/java/net/rptools/common/expression/function/DiceHelperTest.java index 7dadc20..da10b2e 100755 --- a/src/test/java/net/rptools/common/expression/function/DiceHelperTest.java +++ b/src/test/java/net/rptools/common/expression/function/DiceHelperTest.java @@ -1,67 +1,67 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; +import junit.framework.TestCase; import net.rptools.common.expression.RunData; import net.rptools.parser.function.EvaluationException; -import junit.framework.TestCase; public class DiceHelperTest extends TestCase { - public void testRollDice() throws Exception { - RunData.setCurrent(new RunData(null)); - RunData.setSeed(102312L); - - assertEquals(42, DiceHelper.rollDice(10, 6)); - } + public void testRollDice() throws Exception { + RunData.setCurrent(new RunData(null)); + RunData.setSeed(102312L); - public void testKeepDice() throws Exception { - RunData.setCurrent(new RunData(null)); - RunData.setSeed(102312L); + assertEquals(42, DiceHelper.rollDice(10, 6)); + } - assertEquals(28, DiceHelper.keepDice(10, 6, 5)); - } + public void testKeepDice() throws Exception { + RunData.setCurrent(new RunData(null)); + RunData.setSeed(102312L); - public void testDropDice() throws Exception { - RunData.setCurrent(new RunData(null)); - RunData.setSeed(102312L); + assertEquals(28, DiceHelper.keepDice(10, 6, 5)); + } - assertEquals(28, DiceHelper.dropDice(10, 6, 5)); - } + public void testDropDice() throws Exception { + RunData.setCurrent(new RunData(null)); + RunData.setSeed(102312L); - public void testRerollDice() throws Exception { - RunData.setCurrent(new RunData(null)); - RunData.setSeed(102312L); + assertEquals(28, DiceHelper.dropDice(10, 6, 5)); + } - assertEquals(50, DiceHelper.rerollDice(10, 6, 2)); - } + public void testRerollDice() throws Exception { + RunData.setCurrent(new RunData(null)); + RunData.setSeed(102312L); - public void testExplodeDice() throws Exception { - RunData.setCurrent(new RunData(null)); - RunData.setSeed(102312L); + assertEquals(50, DiceHelper.rerollDice(10, 6, 2)); + } - assertEquals(23, DiceHelper.explodeDice(4, 6)); - } + public void testExplodeDice() throws Exception { + RunData.setCurrent(new RunData(null)); + RunData.setSeed(102312L); - public void testExplodeDice_Exception() throws Exception { - try { - RunData.setCurrent(new RunData(null)); - RunData.setSeed(102312L); + assertEquals(23, DiceHelper.explodeDice(4, 6)); + } - assertEquals(23, DiceHelper.explodeDice(4, 1)); - fail(); - } catch (EvaluationException e) { - } + public void testExplodeDice_Exception() throws Exception { + try { + RunData.setCurrent(new RunData(null)); + RunData.setSeed(102312L); - } + assertEquals(23, DiceHelper.explodeDice(4, 1)); + fail(); + } catch (EvaluationException e) { + } + } } diff --git a/src/test/java/net/rptools/common/expression/function/DropRollTest.java b/src/test/java/net/rptools/common/expression/function/DropRollTest.java index d99ba7f..7f4dc8a 100755 --- a/src/test/java/net/rptools/common/expression/function/DropRollTest.java +++ b/src/test/java/net/rptools/common/expression/function/DropRollTest.java @@ -1,46 +1,45 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; - +import junit.framework.TestCase; import net.rptools.common.expression.RunData; import net.rptools.parser.Expression; import net.rptools.parser.Parser; import net.rptools.parser.ParserException; import net.rptools.parser.function.EvaluationException; import net.rptools.parser.function.ParameterException; -import junit.framework.TestCase; public class DropRollTest extends TestCase { - public void testEvaluateRoll() throws ParserException, EvaluationException, - ParameterException { + public void testEvaluateRoll() throws ParserException, EvaluationException, ParameterException { - Parser p = new Parser(); - p.addFunction(new DropRoll()); + Parser p = new Parser(); + p.addFunction(new DropRoll()); - try { - RunData.setCurrent(new RunData(null)); - RunData.setSeed(10423L); + try { + RunData.setCurrent(new RunData(null)); + RunData.setSeed(10423L); - Expression xp = p.parseExpression("drop(4,6,1)"); + Expression xp = p.parseExpression("drop(4,6,1)"); - long result = ((BigDecimal) xp.evaluate()).longValueExact(); + long result = ((BigDecimal) xp.evaluate()).longValueExact(); - assertEquals(12L, result); - } finally { - RunData.setCurrent(null); - } - } + assertEquals(12L, result); + } finally { + RunData.setCurrent(null); + } + } } diff --git a/src/test/java/net/rptools/common/expression/function/RollTest.java b/src/test/java/net/rptools/common/expression/function/RollTest.java index 784aeb6..8e7cacc 100755 --- a/src/test/java/net/rptools/common/expression/function/RollTest.java +++ b/src/test/java/net/rptools/common/expression/function/RollTest.java @@ -1,63 +1,62 @@ /* - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at + * This software Copyright by the RPTools.net development team, and + * licensed under the Affero GPL Version 3 or, at your option, any later + * version. * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * You should have received a copy of the GNU Affero General Public + * License * along with this source Code. If not, please visit + * and specifically the Affero license + * text at . */ package net.rptools.common.expression.function; import java.math.BigDecimal; - +import junit.framework.TestCase; import net.rptools.common.expression.RunData; import net.rptools.parser.Expression; -import net.rptools.parser.ParserException; import net.rptools.parser.Parser; +import net.rptools.parser.ParserException; import net.rptools.parser.function.EvaluationException; import net.rptools.parser.function.ParameterException; -import junit.framework.TestCase; public class RollTest extends TestCase { - public void testEvaluateRoll() throws ParserException, EvaluationException, - ParameterException { - Parser p = new Parser(); - p.addFunction(new Roll()); - - try { - RunData.setCurrent(new RunData(null)); - - Expression xp = p.parseExpression("roll(6)"); - - for (int i = 0; i < 1000; i++) { - long result = ((BigDecimal) xp.evaluate()).longValueExact(); - - assertTrue(result >= 1 && result <= 6); - } - } finally { - RunData.setCurrent(null); - } - } - - public void testIsNonDeterministic() throws ParserException, - EvaluationException, ParameterException { - Parser p = new Parser(); - p.addFunction(new Roll()); - - try { - RunData.setCurrent(new RunData(null)); - - Expression xp = p.parseExpression("roll(10, 6) + 10"); - Expression dxp = xp.getDeterministicExpression(); - - assertTrue(dxp.format().matches("\\d+ \\+ 10")); - } finally { - RunData.setCurrent(null); - } - } + public void testEvaluateRoll() throws ParserException, EvaluationException, ParameterException { + Parser p = new Parser(); + p.addFunction(new Roll()); + + try { + RunData.setCurrent(new RunData(null)); + + Expression xp = p.parseExpression("roll(6)"); + + for (int i = 0; i < 1000; i++) { + long result = ((BigDecimal) xp.evaluate()).longValueExact(); + + assertTrue(result >= 1 && result <= 6); + } + } finally { + RunData.setCurrent(null); + } + } + + public void testIsNonDeterministic() + throws ParserException, EvaluationException, ParameterException { + Parser p = new Parser(); + p.addFunction(new Roll()); + + try { + RunData.setCurrent(new RunData(null)); + + Expression xp = p.parseExpression("roll(10, 6) + 10"); + Expression dxp = xp.getDeterministicExpression(); + + assertTrue(dxp.format().matches("\\d+ \\+ 10")); + } finally { + RunData.setCurrent(null); + } + } }