Die Roll 3d6+2

By keaven on Jul 21, 2011

This is my first posting, so please be gentle ;P hehe

This is a die roller that takes in very simple parameters: number of dice, sides of die, operation, and modifier .. example, 3d6+2, 4d4*5, 2d20 .. etc.

There is only the very basics of error checking - it makes sure there are arguments after the !roll command, and it filters output based on if there was an Operation/Modifier combination or not.

sample usage:

!roll 5d6*2 [ROLL] Rolling 5d6*2 [ROLL] Die 1: 4 [ROLL] Die 2: 6 [ROLL] Die 3: 3 [ROLL] Die 4: 2 [ROLL] Die 5: 5 [ROLL] Total: 20*2 = 40 [EDIT] Added a 'max' variable. If number of dice is larger than this, it will not print out individual roll results. Also, added final results output as a notice to a channel (currently just $chan for my own purposes). [EDIT] Added %goodDice to limit rolls to valid die types. Added checks for natural 20's (when rolling d20s). Added comments to better break up what's going on. ```mirc on *:text:*:*: { if ($1 == !roll) { if ($len($2) == 0) { msg $nick 14--> 3[ROLL]14 Usage: !roll #d#[+-*/#] (ex: !roll 1d20+5) } else { ; Variables ; %goodDice -- the acceptable die type list ; %max -- the maximum number of dice to print per-die output for ; %op -- the operation to perform (the +, in the +2 of 1d20+2) ; %mod -- the modifier to the roll total (the 2, in the +2 of 1d20+2) ; %qtyOfDice -- the first value, the number of dice to roll ; %typeOfDice -- the number of sides to the die being rolled, which must be in %goodDice ; %thisRoll -- the result of a single die roll ; %total -- running total of the dice roll ; %natural20s -- total number of natural 20's rolled var %goodDice = 4.6.8.10.12.20.100 var %max = 5 if (* isin $2) { var %op = * } elseif (+ isin $2) { var %op = + } elseif (- isin $2) { var %op = - } elseif (/ isin $2) { var %op = / } else { var %op = NONE } var %qtyOfDice = $gettok($2,1,100) if (%op != NONE) { var %typeOfDice = $gettok($gettok($2,-1,100),1,$asc(%op)) } else { var %typeOfDice = $gettok($2,-1,100) } var %mod = $gettok($2,-1,$asc(%op)) var %total = 0 var %natural20s = 0 ; Check for valid die type. If %typeOfDice is not in %goodDice, script ends if ($matchtok(%goodDice,%typeOfDice,0,46) == 0) { msg $nick 3[ROLL]14 That is not a valid type of die. halt } ; Confirmation output to the user as a PM if (%op != NONE) { msg $nick 3[ROLL]14 Rolling %qtyOfDice $+ d $+ %typeOfDice $+ %op $+ %mod } else { msg $nick 3[ROLL]14 Rolling %qtyOfDice $+ d $+ %typeOfDice } ; While-loop to cycle through the number of dice, running sum added to %total ; Individual die rolls suppressed if number of die used is greater than %max, except for Natural 20s which always get printed var %x = 1 if (%qtyOfDice > %max ) { msg $nick 3[ROLL]14 %qtyOfDice > %max - Suppressing individual roll results... } while ( %x <= %qtyOfDice ) { var %thisRoll = $rand(1,%typeOfDice) if (%thisRoll == 20) && (%typeOfDice == 20) { msg $nick 3[ROLL]14 Die %x $+ : %thisRoll -- 4NATURAL 20!! inc %natural20s } elseif (%qtyOfDice <= %max ) { msg $nick 3[ROLL]14 Die %x $+ : %thisRoll } %total = $calc( %total + %thisRoll ) inc %x } ; Output totals to both the user as a PM and to the originiating channel if (%op != NONE ) { msg $nick 3[ROLL]14 Total: %total $+ %op $+ %mod = $calc($int(%total) $(%op) $int(%mod)) notice $chan 3[ROLL]14 $nick rolled %qtyOfDice $+ d $+ %typeOfDice $+ %op $+ %mod = %total $+ %op $+ %mod = $calc($int(%total) $(%op) $int(%mod)) } else { msg $nick 3[ROLL]14 Total: %total notice $chan 3[ROLL]14 $nick rolled %qtyOfDice $+ d $+ %typeOfDice = %total } if (%natural20s > 0) { if (%natural20s == 1) { msg $nick 3[ROLL]14 You got %natural20s Natural 20! notice $chan 3[ROLL]14 $nick got %natural20s natural 20 in that roll! } else { msg $nick 3[ROLL]14 You got %natural20s Natural 20s! notice $chan 3[ROLL]14 $nick got %natural20s natural 20s in that roll! } } } } ;end !roll } ;end onText ```

Comments

Sign in to comment.
SReject   -  Jul 23, 2011

None-the-less, good script. [Ignore the one's complaining about flood protection, for the length of the script it's WELL wrote]

 Respond  
keaven   -  Jul 23, 2011

Multiple people issuing commands to the bot, all at the same time, is something I've been thinking about. Since this particular snippet is just one of many possible commands, I didn't embed trigger flood protection here specifically. It is my hope I'll be able to work out a better solution at a higher level. But I do appreciate it being mentioned :) keep me honest guys ;D

@SReject: I actually did start off with something similar .. not nearly as elegant as what you posted here, but along the same lines. I ended up expanding it mostly because of people wanting to see individual rolls, and to identify natural 20's

 Respond  
SReject   -  Jul 23, 2011

Not to inturpt, and I understand some people like to see the result of each roll, but here's a quick roll calculater:

alias dice return $calc($regsubex($1-,/([^\Q{}[]()-+/*d\E]+)d([^\Q{}[]()-+/*d\E]+)/gi,$r( \1 ,$calc( \1 * \2 ))))

Handles things like (5d10+4-3d6)*9

To use: //echo -a $dice()

 Respond  
Jethro   -  Jul 23, 2011

I truly hope so too, jaytea, and those who have read jaytea's heartfelt post above will do the same and spread the word. After all, we can't do it alone! Until then, let's keep our fingers crossed, shall we? :$

 Respond  
jaytea   -  Jul 23, 2011

thanks for your understanding ;P i think we both hope that the site has not yet seen its best days. let's make it so!

 Respond  
Jethro   -  Jul 23, 2011

Well at least you know how a broken record works, don't you? ;)

 Respond  
jaytea   -  Jul 23, 2011

it's not me that's suffering, but the quality of the site :P

but people have to find something to say about a snippet, whether it's productive or not. :o

i don't think people should be forcing themselves to contribute unproductively to the site. this isn't something that can, nor should, be sustained.

All comments are welcome or Hawkee is as dead as a ghost town.

you're twisting the issue in order to present a conclusion that we can all agree upon. i think the site would be improved with fewer comments such as the ones i quoted earlier, it certainly wouldn't become a ghost town ;D

all i'm saying is, if we find ourselves constantly having to reiterate the same message over and over, perhaps it's time to take a step back and re-examine the issue.

 Respond  
Jethro   -  Jul 22, 2011

I can relate to your suffering, jaytea, but people have to find something to say about a snippet, whether it's productive or not. :o All comments are welcome or Hawkee is as dead as a ghost town.

 Respond  
jaytea   -  Jul 22, 2011

You should add a flood protection to this.

why is this always brought up whenever somebody posts a snippet involving any number of outgoing messages? it really does nothing to improve the signal to noise ratio of the comments section.

the submission is a 75 line script, of a reasonable standard and complete with comments. to point out something so mundane as the absence of a couple of basic lines of code is a little insulting to the poster, don't you think? if the comment were made with at least a casual reference to the idea or the main parts of the script in question, i, as the submitter, might be more inclined to appreciate the suggestion.

also, have you guys noticed that there are far fewer complaints about a script causing problematic flooding than there are suggestions to add flood protection?

yes, ensuring your scripts do not cause you to be disconnected from the server is worth considering. but posting the same comment to every single snippet on hawkee is a bit much, in my opinion. if there was an FAQ section on the site, this would be a good place to discuss the issue. since different servers/networks have different flood thresholds, the advice should be directed to those who are experiencing problems with causing flood, not script authors whose function is simply to introduce an idea.

keep in mind mIRC has its own native outgoing flood protection through the queuing and potential discarding of messages (mIRC options -> IRC -> Flood). the problem, if there is one, should be tackled globally, or on a per server basis, rather than per script.

 Respond  
Jethro   -  Jul 22, 2011

To be precise, a trigger flood protection.

 Respond  
PyThOn   -  Jul 22, 2011

You should add a flood protection to this.

 Respond  
keaven   -  Jul 22, 2011

[EDIT] Added %goodDice to limit rolls to valid die types. Added checks for natural 20's (when rolling d20s). Added comments to better break up what's going on.

 Respond  
PyThOn   -  Jul 22, 2011

Nice.

 Respond  
keaven   -  Jul 21, 2011

Added event. This is how i'm using it.. inline with a slew of other !commands, thus the [if ($1 == !roll)] around the whole thing.

If !roll is called from a PM, all output is limited to that window, regardless of other channels you may be in. If !roll is called from within a channel containing the bot, rolls are reported in a PM to the user who called it, with final output being sent both to the PM as well as a notice to the channel where the command originated.

 Respond  
Jethro   -  Jul 21, 2011

Your event is missing, keaven, which doesn't give me a clue as to this snippet is for active channel or PM.

 Respond  
keaven   -  Jul 21, 2011

I added a max to the script. If the number of dice is greater than the max, it now suppresses individual roll results.

 Respond  
keaven   -  Jul 21, 2011

@Aha2Y: Thanks :)

@Jethro_: Yeah, i saw on some other dice roller they had a limiter for the maximum number of dice allowed. I should probably throw that in there. Also, the output is in a PM only, so as long as you're not doing something outlandish it should be okay. Granted, you don't want to go doing something like 500d12 .. cause, yeah.. that would flood you :P

 Respond  
Jethro   -  Jul 21, 2011

There's a strong likelihood that your snippet will result in a ban or flooding because of the consecutive, vertical output in a scrolling fashion, as shown in your instruction. A timed delay will help.

 Respond  
Jordyk19   -  Jul 21, 2011

Not bad for your first script.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.