I'm trying to model my RPG system in Anydice. The rules are:
Roll 3d20. A die that rolls under your stat is a success. If you have advantage, you may reroll a failed die. If you have disadvantage, you must reroll a success, if there were any. Harder tasks require more successes.
I've been working on this for an hour and can't figure out how to make it work. I tried modeling this literally in the code, but it came back with up to 60 successes instead of 3, so I clearly did something wrong. link to program
ABILITY: 10
function: set element I:n in SEQ:s to N:n {
NEW: {}
loop J over {1 .. #SEQ} {
if I = J { NEW: {NEW, N} }
else { NEW: {NEW, J@SEQ} }
}
result: NEW
}
function: reroll greatest of ROLL:s as REROLL:n if greater than TARGET:n {
GREATEST: 1@ROLL \ the GREATEST die is sorted first \
if GREATEST < TARGET {
result: ROLL
} else {
result: [set element 1 in ROLL to REROLL]
}
}
output [reroll greatest of 3d20 as 1d20 if greater than ABILITY]
After working on it for some time, I think I should be able to model advantage as 4d20 take the three highest, and disadvantage as 4d20 take the three lowest. But since the highest
function sums the dice sequences, I don't know how to tell Anydice to do this.