mIRC Home    About    Download    Register    News    Help

Print Thread
#242044 13/06/13 07:09 AM
Joined: Mar 2013
Posts: 8
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
Joined: Mar 2013
Posts: 8
I'm looking for a way to make this code more efficient and faster. The gist of it is, there's a \characters\ folder where files are copied into for each round of my game (the reason for that being that the \monsters\ and \npcs\ folders act as templates, it copies the file over into \characters\ and then gives them adjusted stats and whatever). At the end of the round it goes into \characters\ and erases "monster" and "npc" files, leaving only the players. Now, as the script stands it works. But on slower machines it can take up to 45 seconds or more to finish cleaning the folder out. This is problematic since there's other stuff going on via timers and and this particular function taking so long interferes (keeps players from being able to use other commands that might be necessary).

Anyway, I have it set to run the cleaning thing twice because if there's a lot of npcs & monsters in the \characters\ it can sometimes miss a few. If anyone has any suggestions on how to improve this so that it only has to run once and can possibly speed it up, I'd love to see them.

Code:
alias clear_battle { 



  ; Erase any stray monsters/bosses..

  if (($lines(battle.txt) != 0) && ($lines(battle.txt) != $null)) {
    var %battletxt.lines $lines(battle.txt) | var %battletxt.current.line 1 
    while (%battletxt.current.line <= %battletxt.lines) { 
      var %who.battle $read -l $+ %battletxt.current.line battle.txt
      var %clear.flag $readini($char(%who.battle), info, flag)

      if ((%clear.flag = monster) || (%clear.flag = npc)) { .remove $char(%who.battle) }
      if ($file($char(%who.battle)).size = 0) { $zap_char(%who.battle)  }

      inc %battletxt.current.line
    }
  }


  ; Full everyone that was in battle.
  unset %clear.flag

  var %value 1
  while ($findfile( $char_path , *.char, %value , 0) != $null) {
    set %file $nopath($findfile($char_path ,*.char,%value)) 
    set %name $remove(%file,.char)

    if ($lines(status $+ %name $+ .txt) != $null) {   .remove status $+ %name $+ .txt }
    if ((%name = new_chr) || (%name = $null)) { inc %value 1 } 
    else { 
      var %clear.flag $readini($char(%name), Info, Flag)

      ; It may seem silly to check for monsters twice on this, after we just did it, but I've found that if there's too many files in the folder it
      ; may not get them all.  This is a double check to get rid of them.

      if ((%clear.flag = monster) || (%clear.flag = npc)) { .remove $char(%name) }
      if ((%clear.flag = $null) && ($readini($char(%name), basestats, hp) = $null)) { .remove $char(%name) }
      if ($file($char(%name)).size = 0) { $zap_char(%name) }

      ; If the person is a player, let's refill their hp/mp/stats to max.
      if ((%clear.flag = $null) && ($readini($char(%name), basestats, hp) != $null)) { writeini $char(%name) DCCchat Room Lobby |  $fulls(%name)  }

      inc %value 1  
    }
  }


}


(I've cut out some stuff it does before and after these two checks, as this block of code is what I'd really like help in improving and the other parts are mostly running other aliases or erasing the files that it generates for battles)

Last edited by Iyouboushi; 13/06/13 07:12 AM.
Joined: Jul 2006
Posts: 4,211
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,211
The $findfile function already makes the loop for you, it's really slow for you because you're using a while loop for each file, but you call $findfile each time, which itself already is browsing all files, and this for each iteration.

while ($findfile(path,ext,%N)) { mycommand $v1 | inc %N }
is in fact
noop $findfile(path,ext,0,mycommand $1-)
read /help $findfile, for each file matching, it will call /mycommand where $1- is the filename, you should be able to improve your loop on $findfile from that (which will literraly takes 3secs now :))


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Mar 2013
Posts: 8
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
Joined: Mar 2013
Posts: 8
Oh wow! Thanks. This definitely helps. I can't believe I never realized that $findfile was a loop on its own.


Link Copied to Clipboard