Compare commits
4 Commits
3ce527b07f
...
20be086198
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20be086198 | ||
|
|
ac5aec6cb4 | ||
|
|
8892f684e8 | ||
|
|
b918960040 |
@ -1,7 +0,0 @@
|
||||
import game_logic.runescape.RunescapeRoutines
|
||||
|
||||
fun main() {
|
||||
RunescapeRoutines.fullRunIncense( 0, 0, 0, 1839)
|
||||
// RunescapeRoutines.processInventoryAtFurnace(2500)
|
||||
}
|
||||
|
||||
36
src/main/kotlin/entries/BGSimulation.kt
Normal file
36
src/main/kotlin/entries/BGSimulation.kt
Normal file
@ -0,0 +1,36 @@
|
||||
package entries
|
||||
|
||||
import simulation.*
|
||||
|
||||
fun main(){
|
||||
val start = System.currentTimeMillis()
|
||||
doSimulation()
|
||||
val finish = System.currentTimeMillis()
|
||||
println("Simulation finished in: ${finish-start}ms")
|
||||
}
|
||||
|
||||
fun doSimulation(){
|
||||
val itt = 10_000_000
|
||||
val simulator = Simulator.getInstance<AttackResult>(Runtime.getRuntime().availableProcessors())
|
||||
|
||||
val attackModifiers = listOf(FlatModifier(4), DiceBonusModifier("1d4"))
|
||||
|
||||
RollType.entries.parallelStream()
|
||||
.forEach{
|
||||
val normalAttack = SimpleMeleeAttack(
|
||||
actionRoll = AttackDice("1d20", it, attackModifiers),
|
||||
damageRoll = Dice.makeDice("1d8"),
|
||||
19
|
||||
)
|
||||
val normalAttackModel = AttackSimulatorModel(itt, normalAttack)
|
||||
val normalResults = simulator.doSimulation(normalAttackModel)
|
||||
|
||||
|
||||
AttackResult.printSimulationStatistics(normalResults, "Normal Attack (${it.name})")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
8
src/main/kotlin/entries/FurnaceEntry.kt
Normal file
8
src/main/kotlin/entries/FurnaceEntry.kt
Normal file
@ -0,0 +1,8 @@
|
||||
package entries
|
||||
|
||||
import game_logic.runescape.RunescapeRoutines
|
||||
|
||||
|
||||
fun main() {
|
||||
RunescapeRoutines.processInventoryAtFurnace(1839)
|
||||
}
|
||||
12
src/main/kotlin/entries/IncenceEntry.kt
Normal file
12
src/main/kotlin/entries/IncenceEntry.kt
Normal file
@ -0,0 +1,12 @@
|
||||
package entries
|
||||
|
||||
import game_logic.runescape.RunescapeRoutines
|
||||
|
||||
|
||||
fun main() {
|
||||
RunescapeRoutines.fullRunIncense(0, 0, 0, 1839)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -2,11 +2,33 @@ package simulation
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Attack defines the interface for performing an attack action.
|
||||
* It handles rolling attack dice, calculating modifiers, checking for hits/crits, and triggering damage calculation.
|
||||
*/
|
||||
interface Attack {
|
||||
|
||||
val actionRoll: Dice
|
||||
val passValue: Int
|
||||
/**
|
||||
* We can't call this an 'attack' roll because it could be the case that we're attacking by forcing a DC. So whoever
|
||||
* is taking the positive action makes this roll. For melee attacks, this is a normal attack. For spell checks this
|
||||
* would be the save roll.
|
||||
*/
|
||||
val actionRoll: AttackDice
|
||||
|
||||
/**
|
||||
* Similar to [actionRoll], we cannot call this 'defense', because it might be a spell DC. This is the value of the
|
||||
* responder to the action. In a melee attack, this is AC. For a spell check, this would be the DC.
|
||||
*/
|
||||
val responseValue: Int
|
||||
|
||||
/**
|
||||
* calculateDamage calculates the damage for an attack by:
|
||||
* - Rolling the relevant action
|
||||
* - Evaluating action modifiers
|
||||
* - Checking if action hits by comparing roll + modifiers to response value
|
||||
* - Calling onCriticalHit, onNormalHit or onMiss based on hit result
|
||||
* - Returning the AttackResult
|
||||
*/
|
||||
fun calculateDamage(r: Random): AttackResult {
|
||||
|
||||
val attackResult = actionRoll.roll(r)
|
||||
@ -14,7 +36,7 @@ interface Attack {
|
||||
|
||||
|
||||
return if (isHit(attackResult, attackBonus)) {
|
||||
if (isCrit(attackResult)) {
|
||||
if (actionRoll.isCrit(attackResult)) {
|
||||
onCriticalHit(r)
|
||||
} else {
|
||||
onNormalHit(r)
|
||||
@ -24,14 +46,9 @@ interface Attack {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun isCrit(roll: RollResult): Boolean {
|
||||
return roll.max == roll.result
|
||||
}
|
||||
|
||||
private fun isHit(roll: RollResult, attackBonus: Int): Boolean {
|
||||
private fun isHit(roll: RollResult, actionBonus: Int): Boolean {
|
||||
//ties go to the roller
|
||||
return (roll.result + attackBonus) >= passValue
|
||||
return (roll.result + actionBonus) >= responseValue
|
||||
}
|
||||
|
||||
fun onNormalHit(r: Random): AttackResult
|
||||
@ -42,37 +59,10 @@ interface Attack {
|
||||
|
||||
}
|
||||
|
||||
private fun ArrayList<AttackResult>.sumBools(fn: (AttackResult) -> Boolean): Int {
|
||||
return sumOf { boolToInt(fn(it)) }
|
||||
}
|
||||
|
||||
private fun boolToInt(b: Boolean): Int = if (b) 1 else 0
|
||||
|
||||
data class AttackResult(val attackWasHit: Boolean, val attackWasCrit: Boolean, val resultingDamage: Int) {
|
||||
companion object {
|
||||
fun printSimulationStatistics(results: ArrayList<AttackResult>, label: String = "") {
|
||||
val totalHits = results.sumBools { it.attackWasHit }
|
||||
val hitRate = (totalHits.toDouble() / results.size.toDouble()) * 100.0
|
||||
|
||||
val totalCriticals = results.sumBools { it.attackWasCrit }
|
||||
val critRate = (totalCriticals.toDouble() / results.size.toDouble()) * 100.0
|
||||
|
||||
val sumDamage = results.sumOf { it.resultingDamage }
|
||||
val avgDamage = (sumDamage.toDouble() / results.size.toDouble()) * 100.0
|
||||
|
||||
val reportString = "Hit Rate: %.2f\tCrit Rate: %.2f\tAvg Dmg: %.2f".format(hitRate, critRate, avgDamage)
|
||||
|
||||
if(label.isNotBlank()){
|
||||
println("[$label] $reportString")
|
||||
}else{
|
||||
println(reportString)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* AttackSimulatorModel simulates attacks by running [sampleSize] simulations using the provided [attack] instance.
|
||||
* It implements [SimulationModel] to run the simulations and return [AttackResult].
|
||||
*/
|
||||
class AttackSimulatorModel(override val sampleSize: Int, private val attack: Attack) : SimulationModel<AttackResult> {
|
||||
override fun simulate(r: Random): AttackResult {
|
||||
return attack.calculateDamage(r)
|
||||
|
||||
61
src/main/kotlin/simulation/AttackDice.kt
Normal file
61
src/main/kotlin/simulation/AttackDice.kt
Normal file
@ -0,0 +1,61 @@
|
||||
package simulation
|
||||
|
||||
/**
|
||||
* AttackDice class represents dice used for attack rolls.
|
||||
* If the roll string contains a 'c' modifier, the value after 'c' is used as the crit threshold.
|
||||
* Otherwise, the crit threshold defaults to the max possible roll (number of dice * die size).
|
||||
*
|
||||
*
|
||||
* Example rollStrings:
|
||||
*
|
||||
* "1d20"
|
||||
*
|
||||
* "1d20c18"
|
||||
*
|
||||
* "3d8c15"
|
||||
*
|
||||
* The isCrit() method checks if a roll result meets the crit threshold.
|
||||
*/
|
||||
class AttackDice(
|
||||
override val rollString: String,
|
||||
override val rollType: RollType = RollType.Normal,
|
||||
override val modifiers: List<Modifier<Int>> = ArrayList()
|
||||
) : Dice {
|
||||
override val nDice: Int
|
||||
override val dieSize: Int
|
||||
|
||||
private val critThreshold: Int
|
||||
|
||||
init {
|
||||
val cleanRollString = rollString.lowercase()
|
||||
|
||||
if (cleanRollString.contains('c')) {
|
||||
val critModifierParts = cleanRollString.split("c")
|
||||
val parts = critModifierParts[0].split('d')
|
||||
nDice = parts[0].toInt()
|
||||
dieSize = parts[1].toInt()
|
||||
critThreshold = critModifierParts[1].toInt()
|
||||
} else {
|
||||
val parts = cleanRollString.split('d')
|
||||
nDice = parts[0].toInt()
|
||||
dieSize = parts[1].toInt()
|
||||
critThreshold = dieSize * nDice
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the given [RollResult] meets or exceeds the critical hit threshold
|
||||
* for these attack dice.
|
||||
*
|
||||
* The critical hit threshold is determined based on the roll string used to construct
|
||||
* this [AttackDice] instance.
|
||||
*
|
||||
* @param result The [RollResult] to check for crit.
|
||||
* @return True if result meets or exceeds the crit threshold.
|
||||
*/
|
||||
fun isCrit(result: RollResult): Boolean {
|
||||
return result.result >= critThreshold
|
||||
}
|
||||
|
||||
}
|
||||
37
src/main/kotlin/simulation/AttackResult.kt
Normal file
37
src/main/kotlin/simulation/AttackResult.kt
Normal file
@ -0,0 +1,37 @@
|
||||
package simulation
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
|
||||
private fun ArrayList<AttackResult>.sumBools(fn: (AttackResult) -> Boolean): Int {
|
||||
return sumOf { boolToInt(fn(it)) }
|
||||
}
|
||||
|
||||
private fun boolToInt(b: Boolean): Int = if (b) 1 else 0
|
||||
|
||||
data class AttackResult(val attackWasHit: Boolean, val attackWasCrit: Boolean, val resultingDamage: Int) {
|
||||
companion object {
|
||||
fun printSimulationStatistics(results: ArrayList<AttackResult>, label: String = "") {
|
||||
val sampleSize = results.size.toDouble()
|
||||
|
||||
val totalHits = results.sumBools { it.attackWasHit }
|
||||
val hitRate = (totalHits.toDouble() / sampleSize) * 100.0
|
||||
|
||||
val totalCriticals = results.sumBools { it.attackWasCrit }
|
||||
val critRate = (totalCriticals.toDouble() / sampleSize) * 100.0
|
||||
|
||||
val sumDamage = results.sumOf { it.resultingDamage.toLong() }
|
||||
val avgDamage = sumDamage.toDouble() / sampleSize
|
||||
|
||||
val reportString = "Hit Rate: %.2f\tCrit Rate: %.2f\tAvg Dmg: %.2f".format(hitRate, critRate, avgDamage)
|
||||
|
||||
if(label.isNotBlank()){
|
||||
println("[$label] $reportString")
|
||||
}else{
|
||||
println(reportString)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,6 +1,8 @@
|
||||
package simulation
|
||||
|
||||
import simulation.RollType.*
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
@ -31,25 +33,18 @@ enum class RollType {
|
||||
data class RollResult(val min: Int, val max: Int, val result: Int)
|
||||
|
||||
|
||||
/**
|
||||
* Represents dice that can be rolled with different roll types and modifiers.
|
||||
*
|
||||
* @param rollString The dice roll notation, e.g. "2d6"
|
||||
* @param rollType The roll type to use, which determines how the dice are rolled
|
||||
* @param modifiers Optional modifier functions that add a bonus to the roll result
|
||||
*/
|
||||
class Dice(
|
||||
rollString: String,
|
||||
private val rollType: RollType = RollType.Normal,
|
||||
private vararg val modifiers: Modifier<Int>
|
||||
) {
|
||||
private val nDice: Int
|
||||
private val dieSize: Int
|
||||
interface Dice {
|
||||
val rollString: String
|
||||
val rollType: RollType
|
||||
val modifiers: List<Modifier<Int>>
|
||||
val nDice: Int
|
||||
val dieSize: Int
|
||||
|
||||
init {
|
||||
val parts = rollString.lowercase().split("d")
|
||||
nDice = parts[0].toInt()
|
||||
dieSize = parts[1].toInt()
|
||||
|
||||
companion object {
|
||||
fun makeDice(rollString: String, rollType: RollType = Normal, modifiers: List<Modifier<Int>> = ArrayList()): Dice {
|
||||
return SimpleDice(rollString, rollType, modifiers)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -64,33 +59,32 @@ class Dice(
|
||||
* @see RollType
|
||||
*/
|
||||
fun roll(r: Random): RollResult {
|
||||
val range = (dieSize * nDice) - nDice
|
||||
val result = when (rollType) {
|
||||
RollType.Advantage -> onAdvantage(r, nDice, dieSize)
|
||||
RollType.Disadvantage -> onDisadvantage(r, nDice, dieSize)
|
||||
else -> onNormalRoll(r, nDice, dieSize)
|
||||
Advantage -> advantageRoll(r, nDice, range)
|
||||
Disadvantage -> disadvantageRoll(r, nDice, range)
|
||||
else -> normalRoll(r, nDice, range)
|
||||
}
|
||||
return RollResult(nDice, dieSize * nDice, result)
|
||||
}
|
||||
|
||||
private fun onAdvantage(r: Random, nDice: Int, dieSize: Int): Int {
|
||||
val range = (dieSize * nDice)
|
||||
val roll1 = r.nextInt(range) + nDice
|
||||
val roll2 = r.nextInt(range) + nDice
|
||||
|
||||
return max(roll1, roll2)
|
||||
private fun advantageRoll(r: Random, nDice: Int, range: Int): Int {
|
||||
val roll1 = r.nextInt(range + 1)
|
||||
val roll2 = r.nextInt(range + 1)
|
||||
|
||||
return max(roll1, roll2) + nDice
|
||||
}
|
||||
|
||||
private fun onDisadvantage(r: Random, nDice: Int, dieSize: Int): Int {
|
||||
val range = (dieSize * nDice)
|
||||
val roll1 = r.nextInt(range) + nDice
|
||||
val roll2 = r.nextInt(range) + nDice
|
||||
private fun disadvantageRoll(r: Random, nDice: Int, range: Int): Int {
|
||||
val roll1 = r.nextInt(range + 1)
|
||||
val roll2 = r.nextInt(range + 1)
|
||||
|
||||
return min(roll1, roll2)
|
||||
return min(roll1, roll2) + nDice
|
||||
}
|
||||
|
||||
private fun onNormalRoll(r: Random, nDice: Int, dieSize: Int): Int {
|
||||
val range = (dieSize * nDice)
|
||||
return r.nextInt(range) + nDice
|
||||
private fun normalRoll(r: Random, nDice: Int, range: Int): Int {
|
||||
return r.nextInt(range + 1) + nDice
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,3 +97,19 @@ class Dice(
|
||||
return modifiers.sumOf { it.getBonus(r, crit) }
|
||||
}
|
||||
}
|
||||
|
||||
private class SimpleDice(
|
||||
override val rollString: String,
|
||||
override val rollType: RollType = Normal,
|
||||
override val modifiers: List<Modifier<Int>> = ArrayList()
|
||||
) : Dice {
|
||||
override val nDice: Int
|
||||
override val dieSize: Int
|
||||
|
||||
init {
|
||||
val cleanRollString = rollString.lowercase()
|
||||
val parts = cleanRollString.split('d')
|
||||
nDice = parts[0].toInt()
|
||||
dieSize = parts[1].toInt()
|
||||
}
|
||||
}
|
||||
@ -7,12 +7,12 @@ import java.util.*
|
||||
*
|
||||
* @param actionRoll The dice roll used to determine if an attack hits.
|
||||
* @param damageRoll The dice roll used to determine damage if attack hits.
|
||||
* @param passValue The defense value the attack must exceed to hit.
|
||||
* @param responseValue The defense value the attack must exceed to hit.
|
||||
*/
|
||||
class SimpleMeleeAttack(
|
||||
override val actionRoll: Dice,
|
||||
override val actionRoll: AttackDice,
|
||||
val damageRoll: Dice,
|
||||
override val passValue: Int
|
||||
override val responseValue: Int
|
||||
) : Attack {
|
||||
|
||||
override fun onNormalHit(r: Random): AttackResult {
|
||||
|
||||
@ -28,7 +28,10 @@ interface Modifier<T> {
|
||||
*
|
||||
* @param dice The [Dice] instance to use for generating bonus values.
|
||||
*/
|
||||
class DiceBonus(private val dice: Dice) : Modifier<Int> {
|
||||
class DiceBonusModifier(private val dice: Dice) : Modifier<Int> {
|
||||
|
||||
constructor(diceString: String):this(Dice.makeDice(diceString))
|
||||
|
||||
override fun getBonus(r: Random, crit: Boolean): Int {
|
||||
return if (crit){
|
||||
dice.roll(r).result + dice.roll(r).result
|
||||
@ -47,7 +50,7 @@ class DiceBonus(private val dice: Dice) : Modifier<Int> {
|
||||
*
|
||||
* @param dice The [Dice] to use for generating penalty values.
|
||||
*/
|
||||
class DicePenalty(private val dice: Dice): Modifier<Int>{
|
||||
class DicePenaltyModifier(private val dice: Dice): Modifier<Int>{
|
||||
override fun getBonus(r: Random, crit: Boolean): Int {//can penalties ever crit?
|
||||
return -dice.roll(r).result
|
||||
}
|
||||
|
||||
57
src/test/kotlin/simulation/AttackDiceTest.kt
Normal file
57
src/test/kotlin/simulation/AttackDiceTest.kt
Normal file
@ -0,0 +1,57 @@
|
||||
package simulation
|
||||
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import java.util.*
|
||||
import kotlin.test.Test
|
||||
|
||||
class AttackDiceTest {
|
||||
|
||||
@Test
|
||||
fun testAttackDiceImplementation() {
|
||||
val r = Random()
|
||||
// Test no crit below threshold
|
||||
val dice = AttackDice("1d20")
|
||||
val result = dice.roll(r)
|
||||
if (result.result < 20) {
|
||||
Assertions.assertFalse(dice.isCrit(result))
|
||||
} else {
|
||||
Assertions.assertTrue(dice.isCrit(result))
|
||||
}
|
||||
// Test no crit below threshold
|
||||
val dice2 = AttackDice("1d20c19")
|
||||
val result2 = dice2.roll(r)
|
||||
if (result2.result < 10) {
|
||||
Assertions.assertFalse(dice2.isCrit(result2))
|
||||
} else {
|
||||
Assertions.assertTrue(dice2.isCrit(result2))
|
||||
}
|
||||
|
||||
// Test crit threshold other than max
|
||||
val dice3 = AttackDice("2d10c8")
|
||||
val result3 = dice3.roll(r)
|
||||
if (result3.result >= 8) {
|
||||
Assertions.assertTrue(dice3.isCrit(result3))
|
||||
} else {
|
||||
Assertions.assertFalse(dice3.isCrit(result3))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun validateCritFunctionality() {
|
||||
val trueCritResult = RollResult(1, 20, 20)
|
||||
val fakeCritResult = RollResult(1, 20, 19)
|
||||
|
||||
val defaultRoll = AttackDice("1d20")
|
||||
val verboseDefaultCrit = AttackDice("1d20c20")
|
||||
val normalModifiedCrit = AttackDice("1d20c19")
|
||||
|
||||
Assertions.assertFalse(defaultRoll.isCrit(fakeCritResult))
|
||||
Assertions.assertFalse(verboseDefaultCrit.isCrit(fakeCritResult))
|
||||
Assertions.assertTrue(normalModifiedCrit.isCrit(fakeCritResult))
|
||||
|
||||
Assertions.assertTrue(defaultRoll.isCrit(trueCritResult))
|
||||
Assertions.assertTrue(verboseDefaultCrit.isCrit(trueCritResult))
|
||||
Assertions.assertTrue(normalModifiedCrit.isCrit(trueCritResult))
|
||||
}
|
||||
|
||||
}
|
||||
@ -6,11 +6,13 @@ import org.junit.jupiter.api.Test
|
||||
import java.util.*
|
||||
|
||||
internal class DiceTests {
|
||||
private val random = Random(1)
|
||||
private val d20 = Dice.makeDice("1d20")
|
||||
|
||||
|
||||
@Test
|
||||
fun roll_normal() {
|
||||
val dice = Dice("2d6")
|
||||
val random = Random(1)
|
||||
val dice = Dice.makeDice("2d6")
|
||||
val result = dice.roll(random)
|
||||
|
||||
assertEquals(2, result.min)
|
||||
@ -20,8 +22,7 @@ internal class DiceTests {
|
||||
|
||||
@Test
|
||||
fun roll_advantage() {
|
||||
val dice = Dice("2d6", RollType.Advantage)
|
||||
val random = Random(1)
|
||||
val dice = Dice.makeDice("2d6", RollType.Advantage)
|
||||
val result = dice.roll(random)
|
||||
|
||||
assertEquals(2, result.min)
|
||||
@ -32,8 +33,7 @@ internal class DiceTests {
|
||||
|
||||
@Test
|
||||
fun roll_disadvantage() {
|
||||
val dice = Dice("2d6", RollType.Disadvantage)
|
||||
val random = Random(1)
|
||||
val dice = Dice.makeDice("2d6", RollType.Disadvantage)
|
||||
val result = dice.roll(random)
|
||||
|
||||
assertEquals(2, result.min)
|
||||
@ -45,12 +45,168 @@ internal class DiceTests {
|
||||
fun evaluate_modifiers() {
|
||||
val mod1 = FlatModifier(1)
|
||||
val mod2 = FlatModifier(2)
|
||||
val dice = Dice("1d20", RollType.Normal, mod1, mod2)
|
||||
val dice = Dice.makeDice("1d20", RollType.Normal, arrayListOf(mod1, mod2))
|
||||
|
||||
val random = Random(1)
|
||||
val bonus = dice.evaluateModifiers(random)
|
||||
|
||||
assertEquals(3, bonus)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun verifyRollRangeForTypes() {
|
||||
val rollString = "2d6"
|
||||
val iterations = 10_000_000
|
||||
|
||||
RollType.entries.parallelStream()
|
||||
.forEach {
|
||||
val dice = Dice.makeDice(rollString, it)
|
||||
val r = Random(1)
|
||||
repeat(iterations) {
|
||||
val res = dice.roll(r)
|
||||
Assertions.assertTrue(res.min <= res.result)
|
||||
Assertions.assertTrue(res.result <= res.max)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun verifyBoundariesForTypes() {
|
||||
verifyBoundariesForTypes(1, 20)
|
||||
verifyBoundariesForTypes(2, 6)
|
||||
verifyBoundariesForTypes(5, 8)
|
||||
verifyBoundariesForTypes(1000, 6)
|
||||
verifyBoundariesForTypes(6, 1000)
|
||||
}
|
||||
|
||||
|
||||
private fun verifyBoundariesForTypes(nDice: Int, dieSize: Int) {
|
||||
val rollString = "${nDice}d${dieSize}"
|
||||
val iterations = 10_000_000
|
||||
val max = nDice * dieSize
|
||||
|
||||
var observedMin = false
|
||||
var observedMax = false
|
||||
|
||||
|
||||
RollType.entries.parallelStream()
|
||||
.forEach {
|
||||
val dice = Dice.makeDice(rollString, it)
|
||||
val r = Random(1)
|
||||
for (i in 0..<iterations) {
|
||||
val res = dice.roll(r)
|
||||
if (!observedMin && res.result == nDice) {
|
||||
observedMin = true
|
||||
}
|
||||
if (!observedMax && res.result == max) {
|
||||
observedMax = true
|
||||
}
|
||||
if (observedMin && observedMax) {
|
||||
break
|
||||
}
|
||||
}
|
||||
Assertions.assertTrue(observedMin)
|
||||
Assertions.assertTrue(observedMax)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun verifyNormalRollWithinExpectedBounds() {
|
||||
val n = 2
|
||||
val max = 100
|
||||
val rollString = "${n}d${max}"
|
||||
val tolerance = 0.05 //we expect more than a 25% improvement
|
||||
val iterations = 100_000_000
|
||||
|
||||
val expectedAverageLowerBound = ((n + (n * max)) / 2) * (1 - tolerance)
|
||||
val expectedAverageUpperBound = ((n + (n * max)) / 2) * (1 + tolerance)
|
||||
|
||||
val dice = Dice.makeDice(rollString, RollType.Normal)
|
||||
var total = 0L
|
||||
repeat(iterations) {
|
||||
total += dice.roll(random).result.toLong()
|
||||
}
|
||||
|
||||
val avg = total.toDouble() / iterations.toDouble()
|
||||
|
||||
//assert that the observed average is greater than the expected lower bound of the normal roll scaled by the
|
||||
//tolerance
|
||||
Assertions.assertTrue(avg > expectedAverageLowerBound)
|
||||
Assertions.assertTrue(avg < expectedAverageUpperBound)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun verifyAdvantageSkew() {
|
||||
val n = 2
|
||||
val max = 100
|
||||
val rollString = "${n}d${max}"
|
||||
val tolerance = 1.25 //we expect more than a 25% improvement
|
||||
val iterations = 10_000_000
|
||||
|
||||
val expectedAverageUpperBound = ((n + (n * max)) / 2) * tolerance
|
||||
|
||||
val dice = Dice.makeDice(rollString, RollType.Advantage)
|
||||
|
||||
var total = 0L
|
||||
repeat(iterations) {
|
||||
total += dice.roll(random).result.toLong()
|
||||
}
|
||||
|
||||
val avg = total.toDouble() / iterations.toDouble()
|
||||
|
||||
//assert that the observed average is greater than the expected upper bound of the normal roll scaled by the
|
||||
//tolerance
|
||||
Assertions.assertTrue(avg > expectedAverageUpperBound)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun verifyDisadvantageSkew() {
|
||||
val n = 2
|
||||
val max = 100
|
||||
val rollString = "${n}d${max}"
|
||||
val tolerance = 0.75 //we expect more than a 25% penalty
|
||||
val iterations = 10_000_000
|
||||
|
||||
val expectedAverageLowerBound = ((n + (n * max)) / 2) * tolerance
|
||||
|
||||
val dice = Dice.makeDice(rollString, RollType.Disadvantage)
|
||||
|
||||
|
||||
var total = 0L
|
||||
|
||||
repeat(iterations) {
|
||||
total += dice.roll(random).result.toLong()
|
||||
}
|
||||
|
||||
val avg = total.toDouble() / iterations.toDouble()
|
||||
|
||||
//assert that the observed average is less than the expected lower bound of the normal roll scaled by the
|
||||
//tolerance
|
||||
Assertions.assertTrue(avg < expectedAverageLowerBound)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun verifyDistribution() {
|
||||
val size = 20
|
||||
val dice = Dice.makeDice("1d20")
|
||||
val iterations = 10_000_000
|
||||
val tolerance = 0.05 //5% wiggle on distribution
|
||||
|
||||
val m = HashMap<Int, Int>()
|
||||
|
||||
repeat(iterations) {
|
||||
val rollResult = dice.roll(random)
|
||||
val total = m.getOrDefault(rollResult.result, 0)
|
||||
m[rollResult.result] = (total + 1)
|
||||
}
|
||||
|
||||
val expected = iterations.toDouble() / size.toDouble()
|
||||
|
||||
m.forEach { (_, count) ->
|
||||
val dCount = count.toDouble()
|
||||
Assertions.assertTrue(dCount > (expected * (1 - tolerance)))
|
||||
Assertions.assertTrue(dCount < (expected * (1 + tolerance)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
27
src/test/kotlin/simulation/MeleeAttackTest.kt
Normal file
27
src/test/kotlin/simulation/MeleeAttackTest.kt
Normal file
@ -0,0 +1,27 @@
|
||||
package simulation
|
||||
|
||||
import kotlin.test.Test
|
||||
|
||||
class MeleeAttackTest {
|
||||
|
||||
@Test
|
||||
fun testAttack() {
|
||||
val itt = 1_000_000
|
||||
val simulator = Simulator.getInstance<AttackResult>(Runtime.getRuntime().availableProcessors())
|
||||
|
||||
val critAttack = SimpleMeleeAttack(
|
||||
actionRoll = AttackDice("1d20c19"),
|
||||
damageRoll = Dice.makeDice("1d8"),
|
||||
10
|
||||
)
|
||||
|
||||
|
||||
|
||||
val normalAttackModel = AttackSimulatorModel(itt, critAttack)
|
||||
val normalResults = simulator.doSimulation(normalAttackModel)
|
||||
|
||||
|
||||
AttackResult.printSimulationStatistics(normalResults, "Normal Attack")
|
||||
|
||||
}
|
||||
}
|
||||
@ -14,34 +14,6 @@ class SimulatorTest {
|
||||
val finish = System.nanoTime()
|
||||
println("${results.size} simulations performed in ${finish - start}ns (${(finish - start) / results.size}ns/simulation)")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAttack() {
|
||||
val itt = 1_000_000
|
||||
val simulator = Simulator.getInstance<AttackResult>(Runtime.getRuntime().availableProcessors())
|
||||
|
||||
val attack = SimpleMeleeAttack(
|
||||
Dice("1d20", RollType.Normal, FlatModifier(5)),
|
||||
Dice("2d6", RollType.Normal, FlatModifier(5)),
|
||||
15
|
||||
)
|
||||
|
||||
val attackWithAdvantageAndBless = SimpleMeleeAttack(
|
||||
Dice("1d20", RollType.Advantage, FlatModifier(5), DiceBonus(Dice("1d4"))),
|
||||
Dice("2d6", RollType.Normal, FlatModifier(5)),
|
||||
15
|
||||
)
|
||||
|
||||
val normalAttackModel = AttackSimulatorModel(itt, attack)
|
||||
val normalResults = simulator.doSimulation(normalAttackModel)
|
||||
|
||||
val buffedAttackModel = AttackSimulatorModel(itt, attackWithAdvantageAndBless)
|
||||
val buffedResults = simulator.doSimulation(buffedAttackModel)
|
||||
|
||||
|
||||
AttackResult.printSimulationStatistics(normalResults, "Normal Attack")
|
||||
AttackResult.printSimulationStatistics(buffedResults, "Buffed Attack")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user