HitPoint Trigger

Post Reply
shanglei
Posts: 2
Joined: Fri Jul 13, 2018 9:52 pm

HitPoint Trigger

Post by shanglei » Fri Jul 13, 2018 9:59 pm

Hey:

I am new to Mudlet, thanks in advance for any help that you can lend. I am trying to create a trigger off the Hp displayed after every "round". I am able to get the trigger to recognize "Hp: " but after that space I can't get it to acknowledge the actual hit points displayed. The Trigger that I am trying to make would basically check to see if Hp: is less than X, and if it is then I will send a "drink flask". What I tried to set up for the trigger pattern was something like:

Hp: %d

then I would try to set up something like:

if %1 < XXXX then
send("drink flask")
end

But for some reason I am not able to get the trigger to recognize the numbers after the Hp:.

Thanks again for any thoughts/ideas.

Shang

User avatar
SlySven
Posts: 918
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: HitPoint Trigger

Post by SlySven » Sat Jul 14, 2018 12:15 pm

Right sort of idea - wrong sort of regular expression...! :geek:

You will want:

Code: Select all

Hp: (\d+)
instead because we use Perl Compatible Regular Expressions - pairs of '('...')' mark things that are to be collected out of the string ("Capture Group") and the "\d" means capture a numeric digit and the '+' following is a quantifier meaning "one or more" of the preceding item. Things that get captured are loading into a global lua table called "matches" (unless it is a multi-line capture and that is something that we can probably ignore for the purpose of this "lesson"). Lua table members can be accessed with a numeric index which for lua starts at 1 (not the 0 that C and related programming languages use btw). Anyhow, should a line match a trigger item then (unless some other factors come into play which I won't mention here) the first "matches" entry: matches[1] will contain the whole line and then successive indexed value hold the capture group items in order of their opening '(' - so matches[2] will contain the string you want. So your code will be:

Code: Select all

if matches[2] and tonumber(matches[2]) < XXXX then
  send("drink flask")
end
the first term just checks that there is a captured value (if not trying to get the value for a non-existent table member will return a lua nil value which is treated as a false in logical tests {sort of!}) then the second test forces the match value to be treated as a number (with the "tonumber" built-in function (as opposed to a string) so that the less than comparison works as you might expected and want.

I am not the lua expert around here so if I have anything wrong I expect someone else to chip in. To examine PCREs in more detail and to test things I can suggest you try experimenting/testing on https://regex101.com/ - the default config is quite close to the setup we have...

shanglei
Posts: 2
Joined: Fri Jul 13, 2018 9:52 pm

Re: HitPoint Trigger

Post by shanglei » Tue Jul 17, 2018 3:24 pm

SlySven:

Thanks so much for your reply, it was spot on and the code you posted worked perfectly. I really appreciate the level of explanation that you went into as I feel it really helped me understand somethings (like the matches table) that I will hopefully be able to use in the future. I used Zmud before so my brain was thinking expressions from that client, it will take me a bit to get used to Mudlet and Lua, thanks again for your help with this as I think it will help me get a good foothold to start from.

Shang

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests