mIRC Homepage
Posted By: Sphyrwa Random Encounter script - 26/02/15 11:03 PM
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!
Posted By: Newbie Re: Random Encounter script - 26/02/15 11:09 PM
Hm... it's funny because I've never seen an event inside a script. I don't think you can do that.

Maybe making the on text commands different commands?
Posted By: Sakana Re: Random Encounter script - 26/02/15 11:18 PM
You can't have events inside other events like that.

If an encounter happens, you'll need to set a variable or something, and then have the ON TEXT events check for that variable
Posted By: Sphyrwa Re: Random Encounter script - 26/02/15 11:19 PM
Is there any way such a thing could be accomplished? I thought the any way for mIRC to read user input in streams was via ON TEXT events (granted, I began scripting two days ago).

Edit: Does the rest of the script still function while it's in the loop?
Posted By: Sakana Re: Random Encounter script - 26/02/15 11:47 PM
Right now your TEXT events are under the { }'s of your JOIN event. You need to make them stand alone.

You also have a bunch of syntax that looks incorrect, but I'm not sure all the things I see as mistakes are actually mistakes :-P

Then you need to find a way to only have your text events trigger if there's an encounter going on
Posted By: Nillen Re: Random Encounter script - 27/02/15 07:55 AM
To further explain what Sakana is saying:
You will need to create the first on text event, when that occurs you want to set a %variable as Active (you can use the %monster_name) variable. By randoming between 1/50 this is chosen, I suggest moving to Ini files instead of Txt files which can allow loading the data dynamically a lot simpler in this case.

Then, when the variables are set, you can proceed to the next step. You already have it written, but it's inside your event right now, which isn't how it works. You need to create a new on text event which will only work if the variable %monster_name is set. So for example:
Code:
on *:join:#: {
var %rand $rand(1,50)
set %monster_name $readini(D&D.ini,%rand,name)
;You can use /writeini D&D.ini 1 name MONSTERNAME to get the value set
;...
}
on *:text:!attack:#: { 
if (!%monster_name) return
;If the variable %monster_name doesn't exist or == $null / 0 it will return (do nothing)
var %attack $rand(1,3) 
dec %monster_hp %attack
if (%monster_hp < 1) { 
;Monster is dead!
;Time to give exp
}
else msg # $nick attacked %monster_name for %attack damage
}
This will be a good enough skeleton for you to follow =)
Posted By: Sphyrwa Re: Random Encounter script - 10/03/15 09:16 PM
Thank you for the help. I practiced more scripting and have now created my own mIRC RPG script (at least the very backbone of it).

Thank you again!
Posted By: Nillen Re: Random Encounter script - 10/03/15 11:11 PM
You're very welcome good sir =)
© mIRC Discussion Forums