Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Tips and tricks ➜ Forcing newlines on prompt lines

Forcing newlines on prompt lines

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


Pages: 1 2  3  4  

Posted by Nick Gammon   Australia  (23,072 posts)  Bio   Forum Administrator
Date Mon 13 Dec 2004 12:37 AM (UTC)

Amended on Sat 21 Oct 2006 10:30 PM (UTC) by Nick Gammon

Message
With the new functionality of OnPluginPacketReceived it is now possible to "fool" MUSHclient into thinking prompt lines have a newline after them, even if they don't.

This can be useful for situations where you want a trigger to match a prompt line the moment it arrives, which it won't normally because trigger matching happens when a newline is received.

The way it is done is this:


  • The plugin below receives all incoming text from the MUD.

  • It batches the packets into lines, as sometimes lines are split over packets

  • All of the lines, except the last one, are just reassembled into normal lines with a newline between them.

  • The final line (one with no newline after it), is tested against a regular expression which should be set up to match your prompt.

    In my test case the prompt was:


    (ESC)[0m<1000/1000hp 100/100m 110/110mv 2000/315816750xp>


    The very first character is the "Escape" character (hex 1B) which is used in this case to reset colours to the default. (ESC[0m). Since we are expecting that we build it into the regular expression.

    Note that in the regular expression we have to use two backslashes, as the first is used by Lua to indicate that the second is a "literal" backslash. This gets passed to the regular expression engine as \x1B to indicate the "escape" character.

    If the regular expression matches, then the final (partial) line is also returned to MUSHclient (with a newline after it) thus effectively adding the newline to the prompt line.

  • The lines (saved into table t) are concatenated back into a single string with a newline between each table entry, and returned as the function result.



To use this, just copy below the line, and paste into a text window (notepad window). Change the regular expression (line 3) to match your prompt line, save as Add_Newline_To_Prompt.xml and then install it as a plugin.

All being well, your prompt lines should now automatically end with a newline, and thus match triggers immediately.


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient[
  <!ENTITY prompt_regexp "^\\x1B\\[0m&lt;.+hp .+m .+mv .+xp&gt; $" > 
]>
<!-- MuClient version 3.59 -->

<!-- Plugin "Add_Newline_To_Prompt" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Add_Newline_To_Prompt"
   author="Nick Gammon"
   id="8316c19c35a9ebdb46055874"
   language="Lua"
   purpose="Adds a newline to prompt lines"
   date_written="2004-12-13 12:08:00"
   requires="3.59"
   version="1.0"
   >
<description trim="y">
Detects prompt lines without a newline, and if found, adds one.

Change ENTITY line on 3rd line of plugin to be a regular expression 
that matches your prompt.

</description>

</plugin>

<!--  Script  -->

<script>

  re = rex.new ("&prompt_regexp;")

<![CDATA[

partial = ""  -- partial line from last time through

function OnPluginPacketReceived (s)

  -- add packet to what we already have (excluding carriage-returns)
  
  partial = partial .. string.gsub (s, "\r", "")
  
  t = {}  -- table of lines to be returned
  
  -- iterate over each line
  
  partial = string.gsub (partial, "(.-)\n", 
    function (line) 
     table.insert (t, line) 
     return "" -- added for MUSHclient 3.80+
    end)
  
  -- look for prompt

  if (re:match (partial)) then
    table.insert (t, partial)
    partial = ""
  end -- if found prompt

  if table.getn (t) > 0 then
    table.insert (t, "")  -- to get final linefeed
  end -- if
    
  -- return table of lines, concatenated with newlines between each one
  return table.concat (t, "\n")
  
end -- function OnPluginPacketReceived
]]>
</script>

</muclient>



[EDIT] Amended on 22 October 2006 to make a change so that this plugins works with MUSHclient 3.80 onwards. The amended line is in bold (return "") and the surrounding lines have been reformatted a bit. See this forum post for a discussion:

http://www.gammon.com.au/forum/?id=7430&page=999

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,072 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 13 Dec 2004 02:39 AM (UTC)

Amended on Mon 13 Dec 2004 02:41 AM (UTC) by Nick Gammon

Message
Below is a simpler approach. The first one presented above, which breaks the packet into lines, is probably overkill for this application, although it is useful to illustrate how you would do that.

That version could be extended to omit certain lines, since it already processes each line individually.

The one below simply tests each packet against a regular expression, like:

(blah blah)<prompt line>

This uses a multi-line regular expression (hence the (?m) at the start) and if it find a prompt at the very end, adds a newline to it.


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient[
  <!ENTITY prompt_regexp "(?m)^.*&lt;.+hp .+m .+mv .+xp&gt; \\z" > 
]>
<!-- MuClient version 3.59 -->

<!-- Plugin "Add_Newline_To_Prompt_2" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Add_Newline_To_Prompt_2"
   author="Nick Gammon"
   id="a14e9768c3ad5ce50d202ee0"
   language="Lua"
   purpose="Adds a newline to prompt lines"
   date_written="2004-12-13 14:27"
   requires="3.59"
   version="1.0"
   >
<description trim="y">
Detects prompt lines without a newline, and if found, adds one.

Change ENTITY line on 3rd line of plugin to be a regular expression 
that matches your prompt.

</description>

</plugin>

<!--  Script  -->

<script>

re = rex.new ("&prompt_regexp;")

<![CDATA[

function OnPluginPacketReceived (s)

  if (re:match (s)) then
    return s .. "\n"	
  else
    return s
  end -- if 
  
end -- function OnPluginPacketReceived
]]>
</script>

</muclient>

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Flyto   (12 posts)  Bio
Date Reply #2 on Sun 02 Jan 2005 04:45 PM (UTC)
Message
Hmmm, how can I use that in Achaea? I have a trigger firing great and the regexp looks like
^(\d+)h, (\d+)m (?:\d+[we] |)(?:\d+[we] |)(\D{0,7})\-

If I try to use that in the plugin nothing happens. Mush keeps waiting for an Enter/newLine before he continues. I have no idea what I am doing wrong. Can i use the regular trigger in the GUI and use the Plugin too, or do they interfere in a way?

Oh, and it works great on a USB-stick, including scite-editor...so the same thing at work and on the linux-box at home.

Flyto


... Flyto ...
Top

Posted by Flannel   USA  (1,230 posts)  Bio
Date Reply #3 on Sun 02 Jan 2005 08:22 PM (UTC)

Amended on Sun 02 Jan 2005 08:24 PM (UTC) by Flannel

Message
You need to escape your backslashes (\\) in the entity.

And yes, you do use normal triggers.
The trigger in the plugin only tells MC what to add a newline after. So make sure it is your full prompt.
You then use your normal triggers to actually do stuff with that prompt.

~Flannel

Messiah of Rose
Eternity's Trials.

Clones are people two.
Top

Posted by Nick Gammon   Australia  (23,072 posts)  Bio   Forum Administrator
Date Reply #4 on Sun 02 Jan 2005 10:15 PM (UTC)
Message
Flannel is right. The backslashes in the regular expression are swallowed up by the Lua interpreter, so you need to double them when putting them into the plugin.

eg. In Lua (and Jscript) if you write this:

a = "\n"

It puts a newline into a. If you want to put in the backslash literally (which you do for a regular expression) you need to put another backslash, eg.

a = "\\n"

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Flyto   (12 posts)  Bio
Date Reply #5 on Mon 03 Jan 2005 04:35 PM (UTC)
Message
*scratch*, really trying hard, but it simply doesnt sip the vials when nothing else happens. I made the regexp more simple....from perl...
qr/(\d+)h,.+m\s(\d+)m(.+)-/o
I made in simple LUA-plugin (because I dont need the values):
<!ENTITY prompt_regexp ".+h, .+m(.+)-" >
or did I forget something. BTW the original Prompt looks like
3340h, 3796m ex-
Do I have to strip all Ansi-Codes too? And...*shrug*. Any Ideas? Thank you for the help.

Flyto

... Flyto ...
Top

Posted by Flyto   (12 posts)  Bio
Date Reply #6 on Mon 03 Jan 2005 06:23 PM (UTC)
Message
Finally I found a Regexp firing..the entity is

<!ENTITY prompt_regexp "(\\d+)h,.+m\\s(\\d+)m(.+)-" >

This is about the only thing that matches the prompt. But the problem I have now that there is on effect by adding a "\n", still needing a manual Enter. The rest of the Plugin is exactly the same as in the forum.

This is my function

function OnPluginPacketReceived (s)
if (re:match(s)) then
return s .. "\n"
else
return s .. "not matched"
end -- if
end -- function OnPluginPacketReceived

I have no idea how OnPluginPacketReceived works so i lay my fate in your hands *chuckle*

...Flyto...

... Flyto ...
Top

Posted by Nick Gammon   Australia  (23,072 posts)  Bio   Forum Administrator
Date Reply #7 on Mon 03 Jan 2005 08:25 PM (UTC)
Message
Quote:

This is about the only thing that matches the prompt. But the problem I have now that there is on effect by adding a "\n", still needing a manual Enter.


What do you mean? It works, but it doesn't work?

Are you seeing "not matched" popping up all the time in your output window?

What the plugin is supposed to do is modify the incoming packet, so MUSHclient thinks the <enter> is there all along.

If the prompt has ANSI codes you will need to detect those, after all the trigger is matching literally. Try using Edit -> Debug Packets to see exactly what is arriving.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Flyto   (12 posts)  Bio
Date Reply #8 on Tue 04 Jan 2005 09:05 AM (UTC)
Message
The prompt is detected but I dont see any difference in behavior of my script.

I dont see any "not matched" in my window. But I have another script catching the health and mana-values and if it falls below x, I should sip some elixirs.

The bad thing is that I only drink if something else happens in the mud or ... if I press Enter manually, so the plugin doesnt affect that. The Autosipper is NO plugin or should it be one?

...Flyto...

... Flyto ...
Top

Posted by Nick Gammon   Australia  (23,072 posts)  Bio   Forum Administrator
Date Reply #9 on Wed 05 Jan 2005 03:14 AM (UTC)
Message
If the plugin is installed you should see the newline added to it, or "not matched" appearing for every packet.

What version of MUSHclient do you have? It was only recently changed to allow packet changing.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Flyto   (12 posts)  Bio
Date Reply #10 on Wed 05 Jan 2005 05:14 AM (UTC)
Message
I use 3.65, and the regexp matches the prompt.....if I do

function OnPluginPacketReceived (s)
if (re:match(s)) then
return s .. "Eureka!!\n"
else
return s .. "not matched"
end -- if
end -- function OnPluginPacketReceived

I see lots of Eurekas after every prompt, but no autosip until i hit Enter. Maybe I should check the Autosipper, I am very new to Mush and never heard of Lua before.



... Flyto ...
Top

Posted by Nick Gammon   Australia  (23,072 posts)  Bio   Forum Administrator
Date Reply #11 on Wed 05 Jan 2005 07:09 PM (UTC)
Message
If you are seeking Eurekas then clearly the plugin is working. Now you need to see what the autosipper is doing. Are there some tests in it that stop it working immediately?

Also, some of those were written before the days when you could force newlines on prompts, and detected a line *without* a newline.

Now, you just need a simple trigger to detect the incoming prompt.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Flyto   (12 posts)  Bio
Date Reply #12 on Wed 05 Jan 2005 09:24 PM (UTC)
Message
It took long, but after lots of debugging (thanks to Notes) I found the error. It wasnt the plugin, it was indeed the Sipper. This is what the problem caused...

DoAfterSpecial (5, 'SetVariable ("elBalance", 1)', 12)

elBalance is a variable checking if I can drink another sip - time-based - got a trigger-based balanced as backoff.

the Timer-part is being processed after an Enter (manually or by an input from the mud)

... Flyto ...
Top

Posted by Flyto   (12 posts)  Bio
Date Reply #13 on Thu 06 Jan 2005 08:33 AM (UTC)
Message
Is there a possibility to solve the Timer/SetVariable-problem?

... Flyto ...
Top

Posted by Nick Gammon   Australia  (23,072 posts)  Bio   Forum Administrator
Date Reply #14 on Fri 07 Jan 2005 05:02 AM (UTC)
Message
What problem is that exactly?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


129,233 views.

This is page 1, subject is 4 pages long: 1 2  3  4  [Next page]

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

Go to topic:           Search the forum


[Go to top] top

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