mIRC Homepage
Posted By: Makoko A script crashing my IRC - 28/07/10 07:50 PM
Basically it's a simple die roller, only recognizing a d6. But it locks up and freezes my IRC window whenever it runs. I already know that there's some problem in it somewhere preventing it from running properly (it doesn't repeat properly in the while loop), but that still shouldn't be enough to crash my IRC.

Code:
on *:TEXT:!r*:#:{
  if $me == %bot {
    if ($$2 isnum) {
      if $$3 == d6 {
        set %i 1
        while %i <= $$2 {
          timer 1 2 set %outcome $rand(1,6)
          timer 1 3 msg #$chan %outcome
          timer 1 4 inc %i 1
        }
      }
    }
  }
}


I think it has to do with the loop running infinitely, I don't know. Maybe I'm just not seeing the obvious. Any ideas?
Posted By: 5618 Re: A script crashing my IRC - 28/07/10 08:21 PM
Yes, your timers are the whole problem.
Code:
on *:TEXT:!r*:#:{
  if ($me == %bot) && ($2 isnum) && ($3 == d6) {
    var %i = 1
    while (%i <= $2) {
      var %outcome = %outcome $rand(1,6)
      inc %i
    }
    msg $chan %outcome
  }
}

You don't need your $$ in this case since you explicitly state they need to be a certain value.

By the way, as described in the help file, you can always break out of infinite loops by pressing Ctrl+Break/Pause
Posted By: Riamus2 Re: A script crashing my IRC - 28/07/10 10:30 PM
Here's a basic die roller that accepts any sided dice and any number of dice:

Code:
on *:text:!roll *:#: {
  ; $2 should be the format of 2d6.  If you want another format, this can easily be adjusted.
  var %numdice = $gettok($2-,1,100), %dicesides = $gettok($2-,2,100)
  if (%numdice !isnum || %dicesides !isnum) { msg $chan ERROR: Wrong format.  Example:  !roll 3d6 }
  while (%numdice) {
    var %rolls = %rolls $rand(1,%dicesides)
    dec %numdice
  }
  msg $chan %rolls
}


Example Uses:
!roll 3d6
!roll 2d12
!roll 4 d 5
Posted By: Wims Re: A script crashing my IRC - 28/07/10 10:43 PM
little note, you need to use $2- instead of $2 in your $gettok if you want the '!roll 4 d 5' case to work
Posted By: Riamus2 Re: A script crashing my IRC - 28/07/10 11:42 PM
Oops. That's what I get for showing another way that would work and not testing it first. Fixed. laugh
Posted By: chacha Re: A script crashing my IRC - 29/07/10 10:59 AM
use $lower bcz of "D" mabe that won't work with chr 100
and u have to stop the code after the check of numbers by adding halt, return, or if/else, orelse the loop will continue the execution

Code:
on *:text:!roll *:#:{
  tokenize 100 $lower($2-)
  if ($1-2 !isnum) msg # ERROR: Wrong format.  Example: !roll 3d6
  else {
    var %n $1,%d $2
    while (%n) var %r %r $r(1,%d),%n %n - 1
    msg # %r
  }
}
Posted By: jaytea Re: A script crashing my IRC - 29/07/10 11:54 AM
Originally Posted By: chacha
use $lower bcz of "D" mabe that won't work with chr 100
and u have to stop the code after the check of numbers by adding halt, return, or if/else, orelse the loop will continue the execution

Code:
on *:text:!roll *:#:{
  tokenize 100 $lower($2-)
  if ($1-2 !isnum) msg # ERROR: Wrong format.  Example: !roll 3d6
  else {
    var %n $1,%d $2
    while (%n) var %r %r $r(1,%d),%n %n - 1
    msg # %r
  }
}


if ($1-2 !isnum) is always satisfied if $2 exists. reason being, $1-2 will contain a space which automatically renders it non-numerical. you would have to expand that to if ($1 !isnum) || ($2 !isnum) to have it behave the way you expect, but you'll find that also lets through input such as !roll -1d1 (which causes an infinite loop), !roll 1d1.5 (which, though rounded by $r(), should probably be considered invalid) etc.

to check if $1 and $2 are both strings of digits (that is to say, $2 exists and $1 $+ $2 is a string of digits) you can use the following:

Code:
if ($+(+, $1, $$2.) isnum) {


in general, if ($+(+, N, .) isnum) is true if and only if N is composed entirely of (1 or more) digits
Posted By: Riamus2 Re: A script crashing my IRC - 29/07/10 12:42 PM
Originally Posted By: chacha
use $lower bcz of "D" mabe that won't work with chr 100
and u have to stop the code after the check of numbers by adding halt, return, or if/else, orelse the loop will continue the execution


Yeah, I considered putting support in for capital D, but that's not really considered a valid format for D&D's dice, which is really what you're talking about when using 2d4 for similar format. I could add in a variety of other checks, such as making it only work if the d4 is a valid dice size. I just did a very basic one to show how it can be done. We could also strip out control codes to avoid those possible problems and a variety of other tweaks depending on how it's being used. I just left it as a basic script as mentioned in the post. It's just a template of how you can do dice before adding in any extras. I probably should have noted all of the potential "options" you can add to it to make it work better depending on the user's situation.

Here are some things that can be added if needed by the user:

*Support for capital D in 2d4
*Different error messages depending on input
*Limit the sides of dice to valid numbers (valid being whatever the rules say for the game the dice are being used in)
*Different output format to allow for a better display
*Set it up in an alias so it can return the dice in a way that can easily be used either with echo/msg/notice/etc or used in a script that makes use of the dice values
*Sort the rolls in a way that makes it easier to look at (for example, Yahtzee would benefit from sorting because you're looking for "pairs" and "runs")

And, yes, I forgot to put in halt/return/else in the check.
Posted By: chacha Re: A script crashing my IRC - 29/07/10 02:34 PM
ty for the info smile
Posted By: FroggieDaFrog Re: A script crashing my IRC - 29/07/10 02:56 PM
Why use a while loop, when $str() can be used?

Code:
on *:text:!roll *:#: {
  tokenize 32 $lower($2-)
  if (!$regex($1-,^(\d+)\x20?d\x20?(\d+)$)) { 
    msg $chan ERROR: Wrong format.  Example:  !roll 3d6 
  }
  else { 
    msg $chan $calc($($str($!r(1,$regml(2)) + $chr(32),$regml(1)),2)) 
  }
}



...maybe I am just showing off :\
though this would limit the amount of rolls able to be preformed b/c of mIRC's line-to-long thing
© mIRC Discussion Forums