I'm trying to write a custom explode function, which has a limit on the number of rerolls available.
A basic limit of limiting the number of rerolls per die is easy enough. The problem is trying to model a total number of rerolls across all dice.
So, if I have a limit of 3 rerolls, and roll 3d6, exploding on a 6, then if I get:
6 > 6 > 6 > 4
6
6
That first 6 uses up the 3 available rerolls, and the remaining 6's don't get any.
The way I'm implementing it is by using a recursive function to handle the explosion mechanics, with a limit on how many times it can recurse. Unfortunately, this limit isn't something I can track outside the function since the function is being called with all possible die values, and the overall logic just doesn't work.
I've tried several different approaches, but the results range from no better to much worse.
It mostly seems to revolve around not being able to generate all possible die result sequences while inside a function (where I can control the LIMIT calculation) because that seems to be reserved to function calls (which can't return meaningful modified parameter values since they collate multiple calls).
Here's my current code:
YES: 1
NO: 0
EXPLODES: 0
EXTRA_EMPOWERS: 0
function: explode ROLL:n LIMIT:n ISREROLL {
if LIMIT > 0 {
if ROLL = 6 {
EXPLODES: LIMIT - 1
result: 6 + [explode d6 EXPLODES NO]
}
if ROLL < 4 & EXTRA_EMPOWERS > 0 & !ISREROLL {
EXTRA_EMPOWERS: EXTRA_EMPOWERS - 1
result: [explode d6 LIMIT YES]
}
}
result: ROLL
}
function: burst ROLL:s cha CHA empowered EMPOWERED {
if EMPOWERED {
USED: [count {1,2,3} in ROLL]
EXTRA_EMPOWERS: [highest of CHA - USED and 0]
}
else {
EXTRA_EMPOWERS: 0
}
EXPLODES: CHA
TOTAL: 0
loop N over {1..#ROLL} {
if N@ROLL = 6 {
TOTAL: TOTAL + [explode 6 EXPLODES NO]
}
else if N@ROLL < 4 & EMPOWERED {
TOTAL: TOTAL + [explode 1d6 EXPLODES YES]
}
else {
TOTAL: TOTAL + N@ROLL
}
}
result: TOTAL
}
output [burst 1d6 cha 3 empowered NO] named "Level 1"
output [burst 2d6 cha 4 empowered NO] named "Level 5"
\
output [burst 3d6 cha 5 empowered NO] named "Level 11"
output [burst 4d6 cha 5 empowered NO] named "Level 17"
output [burst 4d6 cha 6 empowered NO] named "Level 20"
output [burst 1d6 cha 3 empowered YES] named "Level 1 empowered"
output [burst 2d6 cha 4 empowered YES] named "Level 5 empowered"
output [burst 3d6 cha 5 empowered YES] named "Level 11 empowered"
output [burst 4d6 cha 5 empowered YES] named "Level 17 empowered"
output [burst 4d6 cha 6 empowered YES] named "Level 20 empowered"
\
The issue applies to both EXTRA_EMPOWERS and EXPLODES/LIMIT variables, but fixing either should allow fixing both.
Is there a way to do this in AnyDice?