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, confirm your email, resolve issues, 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.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Suggestions ➜ Getting ANSI colour codes

Getting ANSI colour codes

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


Posted by Prattler   Lithuania  (15 posts)  Bio
Date Sat 22 Oct 2005 06:31 PM (UTC)
Message
Currently the only way to get the colours is to make a trigger that would remove a line and just before that would call a script which cycled the style runs. If I'm wrong, I will be very very happy hear about alternative methods!

Please, there should be a faster way of doing this.. The lag of such script is unbearable. Could a way to catch the ANSI codes before displaying them be implemented?
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 22 Oct 2005 11:21 PM (UTC)
Message
Why do you want the ANSI codes exactly? For logging? There is a plugin that does that. To omit the colours? Just set the colour scheme to white and black, or similar. Some other reason?

In any case, a simple script like that shouldn't lag unbearably. It would help to know why you want to do it.

- Nick Gammon

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

Posted by Prattler   Lithuania  (15 posts)  Bio
Date Reply #2 on Fri 28 Oct 2005 02:23 PM (UTC)
Message
Sorry for not replying for so long..

I ran through my script and noticed that the huge lag was caused by another thing. But still, let's say I'd want to replace

Your cleave <-==-><-==-> SHATTERS <-==-><-==-> a gateguard! [305]
Your flaming bite <->*<=> WASTES <=>*<-> a gateguard! [219]
Your cleave <--*--><--*--> IMPLODES <--*--><--*--> a gateguard! [269]

with

Your cleave (slash) <-==-><-==-> SHATTERS <-==-><-==-> a gateguard! [305]
Your flaming bite (fire) <->*<=> WASTES <=>*<-> a gateguard! [219]
Your cleave (slash) <--*--><--*--> IMPLODES <--*--><--*--> a gateguard! [269]

and keep all the colours. The damage messages have quite a few of them. The current way of checking styles and then removing the line is noticeably slow. Maybe there could be a kind of triggers (maybe negative sequence?) that fired before lines were actually displayed?

Anyway, thank You for making mushclient so great!
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #3 on Fri 28 Oct 2005 10:11 PM (UTC)
Message
It shouldn't be slow, I wonder which method you are using? The method I describe below has been tested 11 times and claims to have taken under 1/1000 of a second to execute the script.

My technique, which you probably need to modify a bit depending on your circumstances, is to use the Lua trigger handling. This is because when a trigger is passed to a Lua script it also gets a copy of the style runs. This greatly simplies re-echoing the line in its original colours. This is the script:


function mytrigger (name, line, wildcards, styles)

  for k, v in ipairs (styles) do
   
    -- do substitutions 

    v.text = string.gsub (v.text, "cleave", "cleave (slash)")
    v.text = string.gsub (v.text, "flaming bite", "flaming bite (fire)")

    ColourTell ( RGBColourToName (v.textcolour),
                 RGBColourToName (v.backcolour),
                 v.text)
 
  end -- for

  Note ""  -- force a new line

end -- function




You can see it is quite short, and the work is done by iterating through the "styles" argument, which has a copy of the original style runs in the line. A style run is a sequence of text that is all the same colour. We are using ColourTell here to resend the same colours we originally got (turning them back from RGB numbers to names), and then the text of the style.

However to achieve what you wanted we preprocess the text and substitute the words where required.

There are a couple of ways this could be made faster and more efficient.

If you know, for example, that the word you want to change is always in style run 2 (ie. the 2nd colour change) and is always on its own, then a simple table lookup would be better. This is an example illustrating that method:


damage_fixup = 
{ -- start of table

  ["cleave"] = "(slash)",
  ["flaming bite"] = "(fire)",
  ["pummel"] = "(pound)",

}  -- end of table
 
function mytrigger (name, line, wildcards, styles)

  -- look up attack type
 
  local extra = damage_fixup [styles [2].text]

  -- found? append attack type

  if extra then
    styles [2].text = styles [2].text .. " " .. extra 
  end -- if found

  for k, v in ipairs (styles) do
   
    ColourTell ( RGBColourToName (v.textcolour),
                 RGBColourToName (v.backcolour),
                 v.text)
 
  end -- for

  Note ""  -- force a new line

end -- function




Now in this method we assume the attack type (eg. cleave) is in style run 2. So we only test that. Also we assume that it is the exact style run. This lets us do a simple table lookup, which is faster than heaps of substitutions.

The trigger I used for these tests was:


<triggers>
  <trigger
   enabled="y"
   match="Your * &lt;-==-&gt;&lt;-==-&gt; * &lt;-==-&gt;&lt;-==-&gt; *! [*]"
   omit_from_output="y"
   script="mytrigger"
   sequence="100"
  >
  </trigger>
</triggers>


- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #4 on Fri 28 Oct 2005 10:52 PM (UTC)

Amended on Fri 28 Oct 2005 10:56 PM (UTC) by Nick Gammon

Message
The only problem with the above technique is, you probably have more than one attack type that maps to a single word. That means you will eventually get something messy like this:


  ["cleave"] = "(slash)",
  ["slice"] = "(slash)",
  ["cut"] = "(slash)",
  ["flaming bite"] = "(fire)",
  ["pummel"] = "(pound)",


Here the word "(slash)" appears multiple times. What might be nicer is to group the different types of attack together. The script below, whilst lengthier, does that.


damage_fixup_init = 
{ -- start of table

 slice = {
        "miss", "barely scratch", "scratch", "nick", "cut", "hit", "tear",
        "rip", "gash", "lacerate", "hack", "maul", "rend", "decimate",
         },  -- end of slice
 pound = {
        "miss", "barely scuff", "scuff", "pelt", "bruise", "strike", "thrash",
        "batter", "flog", "pummel", "smash", "maul", "bludgeon", "decimate",
         },  -- end of pound

 hit = {
        "misses", "brushes", "scratches", "grazes", "nicks", "jolts", "wounds",
        "injures", "hits", "jars", "thrashes", "mauls", "decimates", "_traumatizes_",
       },  -- end of hit

}  -- end of table
 

-- convert table into one where the word we are looking for is the key

damage_fixup = {}

for k, v in pairs (damage_fixup_init) do
  t = {}
  for _, msg in ipairs (v) do
    t [msg] = true
  end -- inner loop  
  damage_fixup [k] = t
end -- for

 
function mytrigger (name, line, wildcards, styles)

  -- look up attack type
 
  for k, v in pairs (damage_fixup) do
    if v [styles [2].text] then
      styles [2].text = styles [2].text .. " (" .. k .. ")"
      break
    end -- if
  end -- for

  for k, v in ipairs (styles) do
   
    ColourTell ( RGBColourToName (v.textcolour),
                 RGBColourToName (v.backcolour),
                 v.text)
 
  end -- for

  Note ""  -- force a new line

end -- function



The first part is a table of tables (nested tables are a great part of the Lua language!). The outer table is the different types of attack (slice, hit, pound etc.) and for each one we have an inner table which is all the words that match that attack type.

Now the only problem here is that this syntax stores the words as the value of the table, not the key.

If I use tprint to show that table you will see what I mean:


/tprint (damage_fixup_init)

slice:
  1=miss
  2=barely scratch
  3=scratch
  4=nick
  5=cut
  6=hit
  7=tear
  8=rip
  9=gash
  10=lacerate
  11=hack
  12=maul
  13=rend
  14=decimate
hit:
  1=misses
  2=brushes
  3=scratches
  4=grazes
  5=nicks
  6=jolts
  7=wounds
  8=injures
  9=hits
  10=jars
  11=thrashes
  12=mauls
  13=decimates
  14=_traumatizes_
pound:
  1=miss
  2=barely scuff
  3=scuff
  4=pelt
  5=bruise
  6=strike
  7=thrash
  8=batter
  9=flog
  10=pummel
  11=smash
  12=maul
  13=bludgeon
  14=decimate


What we really want is the words like "slice" to be the key. So, as a once-off action we make a second table, that rearranges the tables, so that the keyword is the key. This code is outside a function, so it is run when the script first initialises. After that is run we have this:


/tprint (damage_fixup)

slice:
  tear=true
  miss=true
  lacerate=true
  rend=true
  decimate=true
  cut=true
  gash=true
  hack=true
  scratch=true
  barely scratch=true
  maul=true
  rip=true
  nick=true
  hit=true
pound:
  strike=true
  bruise=true
  bludgeon=true
  decimate=true
  maul=true
  flog=true
  smash=true
  scuff=true
  barely scuff=true
  pummel=true
  thrash=true
  pelt=true
  miss=true
  batter=true
hit:
  thrashes=true
  _traumatizes_=true
  decimates=true
  jars=true
  brushes=true
  jolts=true
  misses=true
  injures=true
  scratches=true
  nicks=true
  wounds=true
  mauls=true
  grazes=true
  hits=true


Now you can see that a word like "hits" is the key of the "hit" table, with its value simply being true.

Now all we have to do is process each table, and for each one look up to see if the word we have is in it, and if so, use the key of the outer table as the action word.

This is what the loop in mytrigger does. Once it finds one it uses a break statement to drop out of the loop, and we now have our word to substitute into the message. This still should be pretty fast to execute.

- 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.


14,426 views.

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.