Hello! I'm in the progress of scripting a D&D style bot for a channel.

I'm having trouble of creating "random encounters".

Code:
on *:JOIN:#:{
  var %r = $rand(1,50)
  if (%r == 1) {
    set %monster_txt $read(mon_list.txt, n)
    set %monster_name $read(%monster_txt, n, 1)
    var %monster_hp $read(%monster_txt, n, 2)
    var %monster_xp $read(%monster_txt, n, 4)
    set %monster_looks $read(%monster_txt, n, 3)
    msg $chan A %monster_name has appeared!
    var %EXP $read($+($chan,_rpg.txt), n, 1)
    while (%monster_hp > 0) {
      on *:TEXT:!attack:#:{
        var %attack $rand(1,3)
        dec %monster_hp %attack
        msg $chan $nick attacked %monster_name for %attack damage!
      }

      on *:TEXT:!examine:#:{
        msg $chan %monster_looks
        msg $chan It has $calc(%monster_hp - %damage) HP left!
      }
    }
    inc %EXP %monster_xp
    write $+(-l,1) $+($chan,_rpg.txt) %EXP
    $msg $chan %monster_name has been defeated! $chan EXP is now %EXP
    else {
    }
  }
}


How it should work:

If anybody joins the active channel, there is a 1 in a 50 chance of starting an encounter.

Monster choice is read from a txt file called "mon_list.txt", which contains the filenames for separate monster encounters (In this case "mon_SkeletonBrawler.txt on line 1 and "mon_Slime.txt" on line 2)

The choice of the monster file is in the value %monster_txt

Monster name, hp, description and XP reward is written in the monster txt file (mon_Slime.txt or mon_SkeletonBrawler).

An example for mon_SkeletonBrawler.txt:
Code:
Skeleton Brawler
15
-- Suddenly, a wrestler career wasn't such a good idea --
3


Line 1 is name
Line 2 is max HP
Line 3 is description
Line 4 is XP reward.

Having the random number line up (1/50 chance) should load all that information from the text files and start a while loop.

In that loop, users should be able to use commands like:

!attack which rolls a random value between 1 and 3 and uses it as an attack value. That attack value is then deduced from the HP value.

!examine which reads the flavor text and shows the remaining HP.

After the while loop has ended (%monster_hp is equal or less than 0), XP will be awarded.

XP is awarded as follows:
There is a pre-existing file with the syntax of $chan_rpg.txt, let's say #test_rpg.txt.
On line 1 there is a numerical value, 0 (channel starts at 0 experience).
After the loop has ended, the current value from #test_rpg.txt will be read. Then the %EXP (XP value) will be added with a $calc function and the resulting sum will be saved back to #test_rpg.txt

Otherwise if the random value does not equal to the preset value, nothing happens (which is why the ELSE section is empty)


This is how my logic would follow this. Here is my problem:

Every part of the script works as it should, because the monster loses HP gradually and dies when it's HP reaches 0 or lower. Experience is also awarded properly because the value in the text file increases properly after each encounter.

Except that the fight commences automatically without waiting for input from users. It automatically goes through the ON TEXT sections of the script without waiting for said text.

The final "monster has been defeated" message does not load either.

Here is an example of how one encounter looks like:

Code:
[00:41] <FateBot> A Skeleton Brawler has appeared!
[00:41] <FateBot> Fate attacked Skeleton Brawler for 3 damage!
[00:41] <FateBot> -- Suddenly, a wrestler career wasn't such a good idea --
[00:41] <FateBot> It has 12 HP left!
[00:41] <FateBot> Fate attacked Skeleton Brawler for 3 damage!
[00:41] <FateBot> -- Suddenly, a wrestler career wasn't such a good idea --
[00:41] <FateBot> It has 9 HP left!
[00:41] <FateBot> Fate attacked Skeleton Brawler for 1 damage!
[00:41] <FateBot> -- Suddenly, a wrestler career wasn't such a good idea --
[00:41] <FateBot> It has 8 HP left!
[00:41] <FateBot> Fate attacked Skeleton Brawler for 3 damage!
[00:41] <FateBot> -- Suddenly, a wrestler career wasn't such a good idea --
[00:41] <FateBot> It has 5 HP left!
[00:41] <FateBot> Fate attacked Skeleton Brawler for 3 damage!
[00:41] <FateBot> -- Suddenly, a wrestler career wasn't such a good idea --
[00:41] <FateBot> It has 2 HP left!
[00:41] <FateBot> Fate attacked Skeleton Brawler for 2 damage!
[00:41] <FateBot> -- Suddenly, a wrestler career wasn't such a good idea --
[00:41] <FateBot> It has 0 HP left!


I would not like the script to be fixed for me, but rather be pointed in the right direction on what errors I seem to have made. Thank you!

Last edited by Sphyrwa; 26/02/15 11:05 PM.