Match on prompt lines with no newline

Posted by Nick Gammon on Thu 24 Oct 2002 03:09 AM — 5 posts, 23,424 views.

Australia Forum Administrator #0
Every now and again someone wants to make a trigger that matches on the prompt line, which MUSHclient doesn't do directly, because its trigger matching is deferred until a newline is received.

This can be annoying if you want to make triggers to answer the initial questions in a MUD (eg. "What race do you want ..."), or to have a prompt line immediately update a hit point status line.

There is a way, using the fairly recent GetLineInfo script function. Basically the method is to use a timer, the timer calls a script, the script gets the very last line in the output buffer, checks to see if it has a newline, and if not, runs a regular expression on it to see if it matches whatever you are trying to match on.

Depending on how quickly you want the information to be processed you might make a 1-second timer (of course, this would have a bit of overhead), or maybe a 5 or 10-second timer would do.

An example of doing this is below, it updates the status line based on a Dawn Of Time prompt line. To use it, just make a simple timer that calls "CheckPrompt" script every few seconds.




sub CheckPrompt (sName)
Dim regEx, Matches, Match, sLine, iNumber
Dim hp, m, xp, mv

'
'  Find line number of last line in buffer
'
  iNumber = world.GetLinesInBufferCount

' ignore lines with a newline at the end

  if world.GetLineInfo (iNumber, 3) then exit sub

' ignore world.note lines

  if world.GetLineInfo (iNumber, 4) then exit sub

' ignore player input lines

  if world.GetLineInfo (iNumber, 5) then exit sub

'
' So far we have a line that is MUD output (not note or command)
'  and it doesn't have a newline, so it is probably a prompt
'

' get text of line

  sLine = world.GetLineInfo (iNumber, 1)

'
' Make a regular expression to match on a line like this:
'
'  [EW 10:10pm 5/5hp 100/100m 94mv 996xp> 
'  
  Set regEx = New RegExp
  regEx.Pattern = "(\d+)/\d+hp (\d+)/\d+m (\d+)mv (\d+)xp"

'
'  Execute regular expression
'

  Set Matches = regEx.Execute (sLine)

'
'  Exit if no match

'
  if Matches.Count = 0 then  exit sub


  Set Match = Matches.Item (0)

  Set regEx = Nothing
  Set Matches = Nothing

'
'  Pull out the sub-matches (individual numbers)
'

  hp = CInt (Match.SubMatches (0))
  m = CInt (Match.SubMatches (1))
  mv = CInt (Match.SubMatches (2))
  xp = CInt (Match.SubMatches (3))

  Set Match = Nothing

'
'  Update the status line
'

  world.SetStatus "hp = " & hp & " m = " & m &  _
                  " mv = " & mv & " xp = " & xp

end sub
Amended on Mon 03 Feb 2003 10:46 PM by Nick Gammon
#1
There's an even SIMPLER way...

Just put *'s on either end of your trigger.

Done.
USA #2
Sorry Togras, but that doesn't in fact work, since the line is not processed until a newline happens. I.e.>

Enter your password: _

can't be matched, using '*Enter your pasword:*' because until you actually type a password and hit enter, the client won't process that line through triggers. Nicks suggestion on the other hand takes advantage of the fact that the line is being displayed, even though the client assumes it is still incomplete. Thus you can 'read' the line through scripting and react to it, even though triggers won't.
USA #3
In the example above where do you define 'mv'. Also is there a way to gag the promp from the output?
Australia Forum Administrator #4
I accidentally omitted "mv" from the "dim" line - I have edited the post to add it.

You could gag the prompts by omitting them from output, however the gagging only takes effect when the newline is received - which is the problem that this whole topic addresses.

It won't be too bad - it means the prompt will appear - then the script kicks in a second later and updates the status line, and then when more output arrives from the MUD the prompt line is omitted, so you only ever see the bottom one. This is good in a way, as the most recent one is really the only relevant one.

You can work around that on some MUDs which let you customise the prompt, by making your own prompt with a newline in it, that way the prompt is omitted immediately (and there is no need for this script in the first place).