mIRC Homepage

AH, so close. Please Help.

Posted By: _DuDe_

AH, so close. Please Help. - 07/06/05 03:11 AM

Ok, so I made a cool script that is like a mini KingsOfChaos (Online RPG Game). Alright, I made a !buy command, and you can buy UP (Unit Production).

Code:
   if ($2- == all UP) {
    if ( $readini( $nick $+ .ini, THINGS, gold) <= 10000) { msg $nick Not enough gold. | .halt }
    writeini $nick $+ .ini THINGS UP $calc( $readini( $nick $+ .ini, THINGS, UP) + $calc( $readini( $nick $+ .ini, THINGS, gold) / 10000 ))
    writeini $nick $+ .ini THINGS gold $calc( $readini( $nick $+ .ini, THINGS, gold) - $readini( $nick $+ .ini, THINGS, gold))
    msg $nick Purchase successfull. Your UP is now:  $readini( $nick $+ .ini, THINGS, UP) 
  }  


That is the UP command. I need to know how to make it get rid of decimals after it does it's calculations.

If you need anymore info, please feel free to contact me at either mikefosh@gmail.com or on here. Thank For Any Help You Can Give.

~mike~
Posted By: MikeChat

Re: AH, so close. Please Help. - 07/06/05 04:03 AM

you could use $int(readini( $nick $+ .ini, THINGS, UP))
or
$round(your calc,0)

/help $int
/help $round
Posted By: DaveC

Re: AH, so close. Please Help. - 07/06/05 05:54 AM

Code:
   if ($2- == all UP) {
    if ( $readini( $nick $+ .ini, THINGS, gold) <= 10000) { msg $nick Not enough gold. | .halt }
    writeini $nick $+ .ini THINGS UP $calc( $readini( $nick $+ .ini, THINGS, UP) + $calc( $readini( $nick $+ .ini, THINGS, gold) / 10000 ))
    ;
    writeini $nick $+ .ini THINGS UP $calc($readini( $nick $+ .ini, THINGS, UP) + $int($calc($readini( $nick $+ .ini, THINGS, gold) / 10000 + .5)))
    ; ^ the +.5 allows for rounding  4.000001 to 4.499999 becomes 4 and 4.5 to 4.999999 becomes 5
    ;
    writeini $nick $+ .ini THINGS gold $calc( $readini( $nick $+ .ini, THINGS, gold) - $readini( $nick $+ .ini, THINGS, gold))
    msg $nick Purchase successfull. Your UP is now:  $readini( $nick $+ .ini, THINGS, UP) 
  }
Posted By: _DuDe_

Re: AH, so close. Please Help. - 07/06/05 05:41 PM

Thanks So Much you guys. I went with
Code:
   if ($2- == all UP) {
    if ( $readini( $nick $+ .ini, THINGS, gold) <= 10000) { msg $nick Not enough gold. | .halt }
    set %ups $calc( $readini( $nick $+ .ini, THINGS, UP) + $calc( $readini( $nick $+ .ini, THINGS, gold) / 10000 ))
    writeini $nick $+ .ini THINGS UP $round( %ups , 0 )
    writeini $nick $+ .ini THINGS gold $calc( $readini( $nick $+ .ini, THINGS, gold) - $round( %ups , 0 ) * 10000 )
    msg $nick Purchase successfull. Your UP is now:  $readini( $nick $+ .ini, THINGS, UP) 
  }  


Thanks again to both of you.
Posted By: DaveC

Re: AH, so close. Please Help. - 07/06/05 11:20 PM

Can i suggest you variablise some things to reduce file access / increase performace / make it easier to read (also made me see while rounding .5+ up was not a good idea!)

(example below, as you may find better spots to define the variables within the whole script)

Code:
   if ($2- == all UP) {
    var %nickini = $nick $+ .ini
    var %gold = $readini( %nickini, THINGS, gold)
    if ( %gold <= 10000) { msg $nick You only have %gold gold, Thats not enough!. | .halt }
    ;
    var %up = $readini( %nickini, THINGS, UP)
    var %ups = $round( $calc( %up + ( %gold / 10000 ) ) ,0 )
    writeini %nickini THINGS UP %ups
    writeini %nickini THINGS gold $calc( %gold - ( %ups * 10000 ) )
    msg $nick Purchase successfull. Your UP is now:  %ups 
  } 


**** POSSABLE LOGIC BUG IN YOUR CODE ****
ok once i did that above it became easier to read and i noticed, that when you create the %UPS value u use the original UP value plus 1/10000th of the GOLD value, which is correct, but you then subtract from the GOLD the %UPS value * 10000
UP = 4
GOLD = 25000
%UPS = ( 4 + ( 25000 / 10000) ) = 6.5 = rounded = 6
GOLD = 25000 - ( 6 * 10000 ) = *** -35000 *** hmmmmmm that dont look right!


I would suggest this.

Code:
   if ($2- == all UP) {
    var %nickini = $nick $+ .ini
    var %gold = $readini( %nickini, THINGS, gold)
    if ( %gold <= 10000) { msg $nick You only have %gold gold, Thats not enough!. | .halt }
    ;
    var %ups = $round( $calc( %gold / 10000 ) ,0 )
    var %up = $calc( $readini( %nickini, THINGS, UP) +  %ups )
    writeini %nickini THINGS UP %up
    writeini %nickini THINGS gold $calc( %gold - ( %ups * 10000 ) )
    msg $nick Purchase successfull. Your UP is now:  %up 
  }


PS: using VAR = in replace of SET as var's exist only tell the script completes.
© 2020 mIRC Discussion Forums