Accurate openlegend dice roll function for anydice.com

An accurate openlegend function for anydice.com: http://anydice.com/program/cde0

The outputs there default to showing Disadvantage 1 for all attribute levels, but you can change the outputs at the bottom of the script to compare any rolls you want. Click the [Graph] and [At Least] buttons for a good comparative view.

SYNTAX: [openlegend attribute__level {advantage}]
where attribute__level is a value 0 - 9
and advantage is the number for level of advantage or disadvantage (use negative values for disadvantage)

It was tricky to get the dice rules exactly right since you have to select the dice results before exploding, and if you try something like [explode [highest 1 of 2d20]], then on a 20 the explode function rolls another [highest 1 of 2d20] instead of just another 1d20.

It took me a while to figure out how to pass a sequence of dice results to a function as a parameter. The key is that if the function is expecting a parameter of type Sequence and you pass it a set of dice (eg 3d6), the anydice engine will call your function with every possible sequence of results for those dice, sorted highest to lowest. Then it’s easy to iterate across the correct subset of dice, check for explosions, and sum it all up.

function: openlegendhelper SEQ:s D:n START:n END:n {
 RES: 0
 loop I over {START..END} {
  RES: RES + I@SEQ
  if I@SEQ = D { RES: RES + [explode 1dD] }
 }
 result: RES
}

function: openlegend LEV:n ADV:n {
 N: 1
 if LEV = 0 {
  D: 20
  RES: 0
 }
 else {
  RES: [explode 1d20]
  if LEV > 4 {
   if LEV > 7 { 
    N: 3
    D: (LEV*2)-8
   }
   else { 
    N: 2
    D: (LEV*2)-4
   }
  }
  else { D: (LEV*2)+2 }
 }
 if ADV < 0 {
  N: N-ADV
  START: 1-ADV
  END: N
 }
 else {
  START: 1
  END: N
  N: N+ADV
 }
 result: RES + [openlegendhelper NdD D START END]
}
4 Likes

Interesting, I know the biggest problem in the past was it didn’t iterate very far in explosion. Like it would only go 4 or 5 deep and so exploding to save resources.

That is admittedly still the case. I’ll have to see if there’s a way to improve on that, but it already hits the 5 second time limit just trying to do 3d10 advantage 5. The odds of six explosions in a row should be small enough not to move these numbers much.

This is much better than my function set. Good stuff!

1 Like