ID:1698874
 
Just thought I'd whip up something quick. This was fairly easy to create and works pretty well when combined with each other. All of these functions return numbers and work off of the rand() proc in the backend.

There are a few slight differences amongst a couple of these functions. For example, Coin() will return 0 or 1, when D2() will return 1 or 2. D100() returns intervals of 10 and not 1-100. This is intended to be used with a D10() which returns 1-10 (e.g. D100() + D10()). This is common in the real-world where a D100 is actually a D10 with intervals of 10s and not 100 sides.

Each proc takes a number as an argument which tells the proc how many dice to use, and returns the total of the roll.

/////////////////
// DICE MODULE //
/////////////////



proc/Coin(N=1)
var/d = 0
for (var/i = 1 to N)
d += rand(0,1)
return d

proc/D2(N=1)
var/d = 0
for (var/i = 1 to N)
d += rand(1,2)
return d

proc/D4(N=1)
var/d = 0
for (var/i = 1 to N)
d += rand(1,4)
return d

proc/D6(N=1)
var/d = 0
for (var/i = 1 to N)
d += rand(1,6)
return d

proc/D8(N=1)
var/d = 0
for (var/i = 1 to N)
d += rand(1,8)
return d

proc/D10(N=1)
var/d = 0
for (var/i = 1 to N)
d += rand(1,10)
return d

proc/D12(N=1)
var/d = 0
for (var/i = 1 to N)
d += rand(1,12)
return d

proc/D16(N=1)
var/d = 0
for (var/i = 1 to N)
d += rand(1,16)
return d

proc/D20(N=1)
var/d = 0
for (var/i = 1 to N)
d += rand(1,20)
return d

proc/D100(N=1)
var/d = 0
for (var/i = 1 to N)
d += (rand(1,10)*10)
return d


An example of using the dice can be something like:
proc/CalcDamage()
return D20(1) + D6(3)

proc/InflictDamage(client/C)
var/d = CalcDamage()
world << "[C] inflicts [d] damage!"


Calculating a percentage:
proc/DicePercentage()
return D100() + D10() // 1-100
...why wouldn't you just use roll()?
That's fine too.
Someone wrote something in regard to the roll() function but appears to be deleted?

the built-in roll() function takes both number of dice and sides as arguments, except the number of dice comes first, then the sides.

An example of this function might look like this:
proc/Roll(S=6,N=1)
var/d = 0
for (var/i = 1 to N)
d += rand(1,S)
return d


If you wanted to make a wrapper function for the built-in roll(), you could do it this way:
proc/Roll(S=6,N=1)
return roll(N,S)


or even a text macro:
#define Roll(S,N) roll(N,S)

In response to Mr_Goober
Or, you could use the built-in roll() function and pretty-much-standard dice notation, with 50+ less lines of code.
roll("1d6")
roll(6)

roll("2d6")
roll(2, 6)

It's okay to remake built-in features just to see if you can, but it's definitely better to use the built-in version. Putting effort into rearranging the arguments is just stubborn.
In response to Mr_Goober
I lol'd