[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Delaying until 'You|you' comes on prompt line

Delaying until 'You|you' comes on prompt line

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Tiopon   USA  (71 posts)  [Biography] bio
Date Tue 23 Mar 2010 04:00 AM (UTC)
Message
So what I'm trying to do is a form of http://www.gammon.com.au/forum/?id=4957 but the specifics are confusing me... Not sure what to put into the regexp line. Here's what the prompt looks like normally:
Quote:
<1095/10100CN 100/100SP 0ST 0MT 0DT 0LG 1516XP 1:19 AM (north)>

I'm trying to pause re-enabling my trigger until it triggers the last command that has been sent, so it doesn't trigger 5 times in a row... My old 'solution' was to do a wait.time for several seconds and hope it would work, but that's not really reliable, so... Here's what a sample prompt line might look like with a command. It will always have either You or you in it, but there's no real guarantee where in the statement the 'You' will come. Also, other messages that happen and people who chat will often use 'You' and I'm trying to avoid having it trigger on those.
Quote:
<1055/10100CN 100/100SP 0ST 0MT 0DT 0LG 1516XP 12:38 AM (south)> You maneuver down the stairs carefully.


I only want to do the command once, and wait for it to trigger, so I'm not sure if I'm actually doing it right with the repeat and so on. What I'm thinking is that it should be something like this...
Quote:
require "wait"

function cr ()

repeat
Send "maneuver stairs"
line, wildcards =
wait.regexp ("^(\<.*CN.*SP.*ST.*MT.*DT.*LG.*XP.*[Yy]ou.*)$")

until string.find (line, "[Yy]ou")

-- wait a second for luck
wait.time (1)

Note ("maneuvering done!")

end -- of function cr

wait.make (cr) -- start coroutine up


Where I'm really confused is the combination of the regexp in wait.regexp and the until string.find part. Do I need both? What's the purpose of each? If I understand properly, your example is looking until it doesn't match because of the caret, right? I haven't used the "repeat" function much and it doesn't appear to be documented in the helpfiles... Should my example skip any line that doesn't match the regexp with no result, and if it does match to look if it has that value in string.find? Does it repeat the command, or only if it does match the regexp and doesn't succeed in the string.find?

This seems like a truly awesome ability, and I'm just wanting to do things properly. Much better than trying to set global variables and have other triggers enabling and disabling themselves all over the place...

Also, the maneuver stairs example is quite a bit shorter than what I'm actually doing... the actual maneuvering bit is about 70 lines long, but that whole thing goes where the send is, and will only fire once, right?

I'd be testing this online, but my connection is too flaky right now for mudding (sad that as my connection has becoming faster, the stability has gone down) but I can work through a plan for the various triggers once it cooperates again.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Tue 23 Mar 2010 05:09 AM (UTC)
Message
I'm a bit confused by the question, but will try to answer parts of it. For a start, "repeat" is a Lua keyword and Lua keywords are not in the documentation, only inbuilt functions. For example, "if" isn't in my help files.

However repeat is very simple, it is basically:


repeat
  
-- do things

until some_condition_is_true


So you might think of it as something like: "repeat" "drink water" "until not thirsty".

In your case you don't need the regexp *and* the string.match. I only did that in some cases where you were matching something like "*" (anything), so the string.match was needed to see if the "anything" line had a certain thing on it.

So for example, this would be unnecessary:


repeat
  line = wait.match ("You fall down.")
until string.find (line, "You fall down.")


The only way you pass the wait.match is if you get "You fall down." so you don't need to test for that.

However, in this case:


repeat
  line = wait.match ("*")
until string.find (line, "You fall down.")


Then it makes sense to do the string.find because the wait line matches anything.

Wouldn't it be simpler to do this ...?


Send "maneuver stairs"
wait.regexp ("You maneuver down the stairs carefully")

-- then do something else


Being a regexp it will check for "You maneuver down the stairs carefully" anywhere in the line (ie. after a prompt).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Tiopon   USA  (71 posts)  [Biography] bio
Date Reply #2 on Tue 23 Mar 2010 05:17 AM (UTC)
Message
Ah... didn't see that described in the description, the example just had it in the repeat section. :)

So instead of doing the whole function thing, all I need to do is do:
Quote:
wait.regexp ("^(\<.*CN.*SP.*ST.*MT.*DT.*LG.*XP.*[Yy]ou.*)$", 10)
to have it search for the prompt and time out after 10 seconds, instead of doing the 4 second wait.time I'm currently doing, or remove the ", 10" and have it not time out... Right?
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Tue 23 Mar 2010 05:24 AM (UTC)

Amended on Tue 23 Mar 2010 06:43 AM (UTC) by Nick Gammon

Message
Hmm - you have to get the coroutine going or it won't work. This is a minimal version:


require "wait"

wait.make (function ()  --- coroutine below here

  line = wait.regexp ("^(\<.*CN.*SP.*ST.*MT.*DT.*LG.*XP.*[Yy]ou.*)", 10)
 
  if line then
    -- got: You ...
  else
    -- timed out
  end -- if

end)  -- end of coroutine


[EDIT] Got rid of trailing $ in regexp.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Tiopon   USA  (71 posts)  [Biography] bio
Date Reply #4 on Tue 23 Mar 2010 06:26 AM (UTC)

Amended on Tue 23 Mar 2010 06:40 AM (UTC) by Tiopon

Message
Ah yes... already had that in from my wait.time, just forgot to re-mention it. And because about half of my triggers or aliases involve some sort of delay, I just have that included in the main script file, along with convert, serialize, tprint, var, and reloading my serialized table. :)

It's being a bit twiggy at the moment... sometimes it triggers, sometimes it doesn't. In both cases, it begins, it just doesn't always match. Will see if it works better without the edge parenthesis. Thanks much for the explanation, I'm fighting through figuring out what I'm supposed to be doing here to make it 'good'.

Edit: Removed the "match to end of line" part and it seems to be triggering properly now... Here's what I'm using as my regexp:
Quote:
wait.regexp ("^\<.*CN.*SP.*ST.*MT.*DT.*LG.*XP.*[Yy]ou")


Edit2: And... it still fails sometimes. Seems to fail anytime You isn't capitalized. Blah. [Yy]ou should match either, as I understand it... or would (You|you) work better?
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Tue 23 Mar 2010 06:45 AM (UTC)
Message
The [Yy]ou should work. I would have to see the whole line that arrives to comment more.

I'm a bit worried about all the .* you have in there - sometimes regexps can consume too much there (since you have asked for any number of any character).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Tiopon   USA  (71 posts)  [Biography] bio
Date Reply #6 on Tue 23 Mar 2010 06:54 AM (UTC)

Amended on Tue 23 Mar 2010 07:15 AM (UTC) by Tiopon

Message
Hmm... so this should work better then? This is the regexp I'm using for the prompt matching...
Quote:
wait.regexp ("^\<([0-9]*)\/([0-9]*)CN ([0-9]*)\/([0-9]*)SP ([0-9]*)ST ([0-9]*)MT ([0-9]*)DT ([0-9]*)LG ([0-9]*)XP ([A-Z0-9\: ]*) \(([a-z]*)\)\>.*[Yy]ou")
Same as the prompt matching, except instead of just having it do .*$ I changed it to .*[Yy]ou...

Hmm... odd. It appears to be stripping the \ from the trigger before passing it along... here's the way the 'proper' trigger parses the prompt:
Quote:
^\<([0-9]*)\/([0-9]*)CN ([0-9]*)\/([0-9]*)SP ([0-9]*)ST ([0-9]*)MT ([0-9]*)DT ([0-9]*)LG ([0-9]*)XP ([A-Z0-9\: ]*) \(([a-z]*)\)\>.*
and this is what the automatic wait.regexp trigger is looking for
Quote:
^<([0-9]*)/([0-9]*)CN ([0-9]*)/([0-9]*)SP ([0-9]*)ST ([0-9]*)MT ([0-9]*)DT ([0-9]*)LG ([0-9]*)XP ([A-Z0-9: ]*) (([a-z]*))>.*(You|you).*$
from
Quote:
wait.regexp ("^\<([0-9]*)\/([0-9]*)CN ([0-9]*)\/([0-9]*)SP ([0-9]*)ST ([0-9]*)MT ([0-9]*)DT ([0-9]*)LG ([0-9]*)XP ([A-Z0-9\: ]*) \(([a-z]*)\)\>.*(You|you).*$")


Somewhere it appears that the wait.regexp trigger automatically removes the \ when it's creating new triggers... do I need to double up on them maybe? Would explain why the triggers were flaky, since I was always matching for the <> bordering the prompt and generally more of the symbols...
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Tue 23 Mar 2010 07:21 AM (UTC)
Message
In "send to script" you do need to double the backslashes, yes.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Worstje   Netherlands  (899 posts)  [Biography] bio
Date Reply #8 on Tue 23 Mar 2010 09:38 AM (UTC)
Message
Looking at that example, I can't help but think turning on the 'Convert IAC/EOR GA to new line' option might help you out a lot, since presumably that is how your mud signals the end of a prompt. That way, the 'You maneuver through stuff and stuff' or whatever line would come on its own line, making for far easier triggering.
[Go to top] top

Posted by Tiopon   USA  (71 posts)  [Biography] bio
Date Reply #9 on Tue 23 Mar 2010 01:58 PM (UTC)

Amended on Tue 23 Mar 2010 02:12 PM (UTC) by Tiopon

Message
Checked and the 'Convert' option didn't make it go on a new line. Also, doubling (and even tripling, on the basis that for \< to work properly, you might need to do \ and then \< again) didn't make a single \ show up in the automatic regexp trigger...

Is this a case where I should/can have a premade trigger that it activates and deactivates instead of trying to create its own that never puts in the proper backslashes? Or would it be better to go through wait.lua and try to have it check regexp strings for either directly posting what it was sent, or re-triggering the escape character...?

I knew there was some sort of issue I'd experienced starting this thought last time. The regexp-thumping must have been it.

Edit: Think I might be hitting a 'doh!' moment. Checking now, but think that it might be the way I was enabling and disabling the trigger so that it doesn't activate itself multiple times... I'd worked that in manually before when I was doing wait.time (does wait.regexp automatically disable the trigger so it won't trigger again, or should I still do that?) but when I converted from wait.time to wait.regexp, there's no guarantee that my in-game lag will have reduced to the point where the triggering command actually happens... so there's no guarantee that the triggering regexp will EVER be thrown.

Moved the wait.regexp to the spot in the if/elseif/end chain where the actual Execute happens, and <coughs> seems to be working more properly now, though I'm doing it as a .*? chain instead of anything that involves escape characters. Here's what I'm trying now (in the Execute sequence):
Quote:
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Tue 23 Mar 2010 07:34 PM (UTC)

Amended on Tue 23 Mar 2010 07:35 PM (UTC) by Nick Gammon

Message
Hang on, what trigger? I think you need to show us the whole thing and not just excerpts that you think are relevant.

Template:copying For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.


Potentially if you don't disable the trigger it will fire multiple times, and this script might be running multiple times, one per trigger. Or if you do disable it, and never re-enable it, it will only ever run the first time.

Bear in mind the forum converts two backslashes to one backslash as well, that might be part of the confusion. Post the whole thing, reading the instructions here for fixing up stuff pasted to the forum:

Template:codetag To make your code more readable please use [code] tags as described here.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Tiopon   USA  (71 posts)  [Biography] bio
Date Reply #11 on Tue 23 Mar 2010 08:41 PM (UTC)
Message
Okay, here's what I've got currently. Thinking I'll probably scrap the lag check (nowlg) because the waiting on a disabled trigger should hopefully make that irrelevant, but I'll backup the trigger before I do any changes like that... Anyways, here's the trigger in a way that seems to work, now that I've moved the wait.regexp into the if/elseif/end Execution line.
<triggers>
  <trigger
   lines_to_match="2"
   keep_evaluating="y"
   match="([a-zA-Z\ ]*) is ([0-9]*)\%([a-z\ ]*)\.   Shield\: ([0-9]*)\Z"
   multi_line="y"
   name="job_line"
   regexp="y"
   send_to="12"
   sequence="80"
  >
  <send>wait.make (function ()

EnableTrigger ("job_line", false)

table.foreach (work_table, function (k, v)
       if (v.name == GetVariable ("Current_Job")) then JobNumber = k
       end -- if 
      end -- function
      )

local job_completion = %2
local job_shield = %4

if (nowlg &lt; 6 and job_shield == 0 and work_table[JobNumber].shield3) then
Execute("work " .. work_table[JobNumber].shield3)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and ((job_shield == 0 and work_table[JobNumber].shield2) or (job_shield == 20 and work_table[JobNumber].shield2 and job_completion &gt; 0))) then
Execute("work " .. work_table[JobNumber].shield2)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and ((job_shield == 0 and work_table[JobNumber].shield1) or (job_shield == 50 and work_table[JobNumber].shield1 and job_completion &gt; 33))) then
Execute("work " .. work_table[JobNumber].shield1)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and job_completion &gt; 100 and work_table[JobNumber].finish1) then
Execute("work " .. work_table[JobNumber].finish1)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and job_completion &gt; 85 and work_table[JobNumber].finish3) then
Execute("work " .. work_table[JobNumber].finish3)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and job_completion &gt; 50 and work_table[JobNumber].finish4) then
Execute("work " .. work_table[JobNumber].finish4)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and job_completion &gt; 75 and work_table[JobNumber].finish2) then
Execute("work " .. work_table[JobNumber].finish2)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and work_table[JobNumber].attack3 and job_completion == 0 and job_shield == 20) then
Execute("work " .. work_table[JobNumber].attack3)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and work_table[JobNumber].attack5 and job_completion &lt; 50) then
Execute("work " .. work_table[JobNumber].attack5)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and work_table[JobNumber].attack4 and job_completion &lt; 60) then
Execute("work " .. work_table[JobNumber].attack4)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and work_table[JobNumber].attack3 and job_completion &lt; 75) then
Execute("work " .. work_table[JobNumber].attack3)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and work_table[JobNumber].attack2 and job_completion &lt; 92) then
Execute("work " .. work_table[JobNumber].attack2)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
elseif (nowlg &lt; 6 and work_table[JobNumber].attack1) then
Execute("work " .. work_table[JobNumber].attack1)
wait.regexp (".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou")
end

EnableTrigger ("job_line", true)
end)</send>
  </trigger>
</triggers>


Again, think it's working now... the issue it had before was that if nowlg > 5, it didn't fire the Execute but would still pause the trigger waiting for '[Yy]ou' to show up on the prompt line, forever...
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #12 on Tue 23 Mar 2010 10:27 PM (UTC)
Message
Glad it's working. You could make it a bit easier to read if you set up the prompt match once, eg.


prompt = ".*?CN.*?SP.*?ST.*?MT.*?DT.*?LG.*?XP.*?[Yy]ou"

-- do something
wait.regexp (prompt)
-- do something else
wait.regexp (prompt)

... and so on



- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Tiopon   USA  (71 posts)  [Biography] bio
Date Reply #13 on Tue 23 Mar 2010 10:31 PM (UTC)
Message
True... think I'll toss a "local matchline" in there. Thanks so much for your help. I was beating my head into a wall there. :)
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


35,474 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]