Unknown2005-05-09 04:37:21
One of the questions I get asked the most is how to do multiline triggers in TF. TF does not have any built-in commands to support multiline triggers. However, there are many approaches and techniques to get it working. The ones I've always seen involve multiple /def's that set variables, or trigger on expressions and whatnot. I've always found such methods to be more of a pain than they're really worth.
I don't like dealing with a lot of extra variables and things that don't have anything to do with what my goal is. Although I may know what all those extra variables/expressions are for, anyone else looking at my code will be boggled beyond belief. Chances are, a few weeks down the road I would be too!
I had seen a few posts on the TF mailing list concerning it, and decided to try forming a few templates to make it easy to create multiline triggers. In the process of creating my defences script I came up with working code for 2, 3, 4, and 5-line triggers. This code does NOT rely on any extra variables or expressions. It also keeps the entire trigger together in 1 /def, which improves performance significantly (1 def, as opposed to x def's for an x-line multiline trigger).
Here's how it works. You create a trigger for the first line you wish to match. This trigger will always be present (-n0). The triggers for all other lines are created by their predecessor (line 5's trigger is created by line 4, line 4's by line 3, etc). All of the triggers for lines > 1 are single-fire triggers (-n1), that way they disapear after they fire.
Every trigger also creates a "catch-all" trigger, at a lower priority, that will remove the trigger just created at that level. So the trigger for line 1 will create the trigger for line 2, and a catch-all trigger that deletes the line 2 trigger. Line 2's trigger body will create a trigger for line 3, and a catch-all that deletes the line 3 trigger. And so on. This way if your clients sees the text for line 1, it won't fire the trigger unless it sees all of the other lines in the right order.
Let's say we want to trigger on the following two lines:
Your client receives the first line from lusternia, it creates a trigger for the second line and a catch-all trigger. If you receive the second line next, the code in the second line's trigger is executed. This code deletes the catch-all trigger, and then the commands you tell it to (since the second line trigger was a one-fire trigger, it's deleted and we're back at where we started with just a trigger for the first line). If the second line was NOT received next, the catch-all trigger fires and deletes the 2nd line's trigger. Now all that exists is the first line's trigger (we're back at where we started before any lines were received).
It may sound a bit confusing at first, but if you look at the code for it I'm sure it will be more clear. (What can I say, I'm a programmer...not a writer )
The following code snippets are the templates I'm using to create my defences script:
Here is an example of two multiline triggers for deathsense. One triggers what happens when you put up the defense, the other when you take it down.
I don't like dealing with a lot of extra variables and things that don't have anything to do with what my goal is. Although I may know what all those extra variables/expressions are for, anyone else looking at my code will be boggled beyond belief. Chances are, a few weeks down the road I would be too!
I had seen a few posts on the TF mailing list concerning it, and decided to try forming a few templates to make it easy to create multiline triggers. In the process of creating my defences script I came up with working code for 2, 3, 4, and 5-line triggers. This code does NOT rely on any extra variables or expressions. It also keeps the entire trigger together in 1 /def, which improves performance significantly (1 def, as opposed to x def's for an x-line multiline trigger).
Here's how it works. You create a trigger for the first line you wish to match. This trigger will always be present (-n0). The triggers for all other lines are created by their predecessor (line 5's trigger is created by line 4, line 4's by line 3, etc). All of the triggers for lines > 1 are single-fire triggers (-n1), that way they disapear after they fire.
Every trigger also creates a "catch-all" trigger, at a lower priority, that will remove the trigger just created at that level. So the trigger for line 1 will create the trigger for line 2, and a catch-all trigger that deletes the line 2 trigger. Line 2's trigger body will create a trigger for line 3, and a catch-all that deletes the line 3 trigger. And so on. This way if your clients sees the text for line 1, it won't fire the trigger unless it sees all of the other lines in the right order.
Let's say we want to trigger on the following two lines:
QUOTE
This is the first line of the trigger.
Following the first line is this line, the second line.
Following the first line is this line, the second line.
Your client receives the first line from lusternia, it creates a trigger for the second line and a catch-all trigger. If you receive the second line next, the code in the second line's trigger is executed. This code deletes the catch-all trigger, and then the commands you tell it to (since the second line trigger was a one-fire trigger, it's deleted and we're back at where we started with just a trigger for the first line). If the second line was NOT received next, the catch-all trigger fires and deletes the 2nd line's trigger. Now all that exists is the first line's trigger (we're back at where we started before any lines were received).
It may sound a bit confusing at first, but if you look at the code for it I'm sure it will be more clear. (What can I say, I'm a programmer...not a writer )
The following code snippets are the templates I'm using to create my defences script:
CODE
;;; 5-line template
/def -mregexp -n0 -p285 -F -aCyellow -t"^line one$" highlight_defences_DEF_up = \\
  /def -mregexp -n1 -p287 -F -aCyellow -t"^line two\\$" highlight_defences_DEF_up2 = \\
    /undef highlight_defences_DEF_catch_all %%; \\
    /def -mregexp -n1 -p287 -F -aCyellow -t"^line three\\\\\\$" highlight_defences_DEF_up3 = \\
      /undef highlight_defences_DEF_catch_all %%%; \\
      /def -mregexp -n1 -p287 -F -aCyellow -t"^line four\\\\\\\\\\\\\\$" highlight_defences_DEF_up4 = \\
        /undef highlight_defences_DEF_catch_all %%%%; \\
        /def -mregexp -n1 -p287 -F -aCyellow -t"^line five\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\$" highlight_defences_DEF_up5 = \\
          /undef highlight_defences_DEF_catch_all %%%%%; \\
          /set defence_DEF=1 %%%%; \\
        /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up5 %%%; \\
      /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up4 %%; \\
    /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up3 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up2
;;; 4-line template
/def -mregexp -n0 -p285 -F -aCyellow -t"^line one$" highlight_defences_DEF_up = \\
  /def -mregexp -n1 -p287 -F -aCyellow -t"^line two\\$" highlight_defences_DEF_up2 = \\
    /undef highlight_defences_DEF_catch_all %%; \\
    /def -mregexp -n1 -p287 -F -aCyellow -t"^line three\\\\\\$" highlight_defences_DEF_up3 = \\
      /undef highlight_defences_DEF_catch_all %%%; \\
      /def -mregexp -n1 -p287 -F -aCyellow -t"^line four\\\\\\\\\\\\\\$" highlight_defences_DEF_up4 = \\
        /undef highlight_defences_DEF_catch_all %%%%; \\
        /set defence_DEF=1 %%%; \\
      /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up4 %%; \\
    /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up3 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up2
;;; 3-line template
/def -mregexp -n0 -p285 -F -aCyellow -t"^line one$" highlight_defences_DEF_up = \\
  /def -mregexp -n1 -p287 -F -aCyellow -t"^line two\\$" highlight_defences_DEF_up2 = \\
    /undef highlight_defences_DEF_catch_all %%; \\
    /def -mregexp -n1 -p287 -F -aCyellow -t"^line three\\\\\\$" highlight_defences_DEF_up3 = \\
      /undef highlight_defences_DEF_catch_all %%%; \\
      /set defence_DEF=1 %%; \\
    /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up3 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up2
;;; 2-line template
/def -mregexp -n0 -p285 -F -aCyellow -t"^line one$" highlight_defences_DEF_up = \\
  /def -mregexp -n1 -p287 -F -aCyellow -t"^line two\\$" highlight_defences_DEF_up2 = \\
    /undef highlight_defences_DEF_catch_all %%; \\
    /set defence_DEF=1 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up2
;;; 1-line template
/def -mregexp -n0 -p285 -F -aCyellow -t"^trigger text$" highlight_defences_DEF_up = \\
  /set defence_DEF=1
/def -mregexp -n0 -p285 -F -aCyellow -t"^line one$" highlight_defences_DEF_up = \\
  /def -mregexp -n1 -p287 -F -aCyellow -t"^line two\\$" highlight_defences_DEF_up2 = \\
    /undef highlight_defences_DEF_catch_all %%; \\
    /def -mregexp -n1 -p287 -F -aCyellow -t"^line three\\\\\\$" highlight_defences_DEF_up3 = \\
      /undef highlight_defences_DEF_catch_all %%%; \\
      /def -mregexp -n1 -p287 -F -aCyellow -t"^line four\\\\\\\\\\\\\\$" highlight_defences_DEF_up4 = \\
        /undef highlight_defences_DEF_catch_all %%%%; \\
        /def -mregexp -n1 -p287 -F -aCyellow -t"^line five\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\$" highlight_defences_DEF_up5 = \\
          /undef highlight_defences_DEF_catch_all %%%%%; \\
          /set defence_DEF=1 %%%%; \\
        /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up5 %%%; \\
      /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up4 %%; \\
    /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up3 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up2
;;; 4-line template
/def -mregexp -n0 -p285 -F -aCyellow -t"^line one$" highlight_defences_DEF_up = \\
  /def -mregexp -n1 -p287 -F -aCyellow -t"^line two\\$" highlight_defences_DEF_up2 = \\
    /undef highlight_defences_DEF_catch_all %%; \\
    /def -mregexp -n1 -p287 -F -aCyellow -t"^line three\\\\\\$" highlight_defences_DEF_up3 = \\
      /undef highlight_defences_DEF_catch_all %%%; \\
      /def -mregexp -n1 -p287 -F -aCyellow -t"^line four\\\\\\\\\\\\\\$" highlight_defences_DEF_up4 = \\
        /undef highlight_defences_DEF_catch_all %%%%; \\
        /set defence_DEF=1 %%%; \\
      /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up4 %%; \\
    /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up3 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up2
;;; 3-line template
/def -mregexp -n0 -p285 -F -aCyellow -t"^line one$" highlight_defences_DEF_up = \\
  /def -mregexp -n1 -p287 -F -aCyellow -t"^line two\\$" highlight_defences_DEF_up2 = \\
    /undef highlight_defences_DEF_catch_all %%; \\
    /def -mregexp -n1 -p287 -F -aCyellow -t"^line three\\\\\\$" highlight_defences_DEF_up3 = \\
      /undef highlight_defences_DEF_catch_all %%%; \\
      /set defence_DEF=1 %%; \\
    /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up3 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up2
;;; 2-line template
/def -mregexp -n0 -p285 -F -aCyellow -t"^line one$" highlight_defences_DEF_up = \\
  /def -mregexp -n1 -p287 -F -aCyellow -t"^line two\\$" highlight_defences_DEF_up2 = \\
    /undef highlight_defences_DEF_catch_all %%; \\
    /set defence_DEF=1 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_DEF_catch_all = /undef highlight_defences_DEF_up2
;;; 1-line template
/def -mregexp -n0 -p285 -F -aCyellow -t"^trigger text$" highlight_defences_DEF_up = \\
  /set defence_DEF=1
Here is an example of two multiline triggers for deathsense. One triggers what happens when you put up the defense, the other when you take it down.
CODE
;;; deathsense
;;; up
/def -mregexp -n0 -p285 -F -aCyellow -t"^You shut your eyes and concentrate on the end of life. A moment later, you feel$" highlight_defences_deathsense_up = \\
  /def -mregexp -n1 -p287 -F -aCyellow -t"^inextricably linked with the strings of fate\\.\\$" highlight_defences_deathsense_up2 = \\
    /undef highlight_defences_deathsense_catch_all %%; \\
    /set defence_deathsense=1 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_deathsense_catch_all = /undef highlight_defences_deathsense_up2
;;; down
/def -mregexp -n0 -p285 -F -aBCyellow -t"^You shut your eyes and concentrate on life. A moment later, you feel your link $" highlight_defences_deathsense_down = \\
  /def -mregexp -n1 -p287 -F -aBCyellow -t"^with the strings of fate sever\\.\\$" highlight_defences_deathsense_down2 = \\
    /undef highlight_defences_deathsense_catch_all %%; \\
    /set defence_deathsense=0 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_deathsense_catch_all = /undef highlight_defences_deathsense_down2
;;; up
/def -mregexp -n0 -p285 -F -aCyellow -t"^You shut your eyes and concentrate on the end of life. A moment later, you feel$" highlight_defences_deathsense_up = \\
  /def -mregexp -n1 -p287 -F -aCyellow -t"^inextricably linked with the strings of fate\\.\\$" highlight_defences_deathsense_up2 = \\
    /undef highlight_defences_deathsense_catch_all %%; \\
    /set defence_deathsense=1 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_deathsense_catch_all = /undef highlight_defences_deathsense_up2
;;; down
/def -mregexp -n0 -p285 -F -aBCyellow -t"^You shut your eyes and concentrate on life. A moment later, you feel your link $" highlight_defences_deathsense_down = \\
  /def -mregexp -n1 -p287 -F -aBCyellow -t"^with the strings of fate sever\\.\\$" highlight_defences_deathsense_down2 = \\
    /undef highlight_defences_deathsense_catch_all %%; \\
    /set defence_deathsense=0 %; \\
  /def -mregexp -n1 -p286 -F -t".*" highlight_defences_deathsense_catch_all = /undef highlight_defences_deathsense_down2