A trigger that attacks when they see another character attack.

Posted by MythicalPlatypus on Mon 28 May 2018 02:01 PM — 4 posts, 16,591 views.

#0
Im trying to work on a trigger that will input

"attack <name of creature"

whenever it sees

"Jinx just attacked the <imp|huge rat|any creature>"

My problem is that sometimes it is attacked "the imp" sometimes "the huge rat" "the female hobgoblin" "the lizard man", Etc... aswell as "the cyclops" (which works fine)

I can't quite figure out how to have it only select "rat", "lizard".

as I said it works with one worded creatures (aka Cyclops)

<triggers>
  <trigger
   enabled="y"
   group="auto attacks"
   keep_evaluating="y"
   match="^Jinx just attacked (?:the )?(.*?) with a (.*?)$"
   regexp="y"
   sequence="100"
  >
  <send>a %1
a %1
c dobuza %1</send>
  </trigger>
</triggers>

Thankyou for any input
Amended on Mon 28 May 2018 02:03 PM by MythicalPlatypus
Australia Forum Administrator #1
The easiest thing in this case is to post-process the matching name to grab the last word, like this:


<triggers>
  <trigger
   enabled="y"
   group="auto attacks"
   keep_evaluating="y"
   match="^Jinx just attacked (?:the )?(.*?) with a (.*?)$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

who = string.match ("%1", "%%S+$")

Send ("a " .. who)
Send ("a " .. who)
Send ("c dobuza " .. who)

</send>
  </trigger>
</triggers>



The string.match there finds one or more characters which are not a space, at the end of the string. This will effectively be the last (or only) word.

Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.


The "send_to" being 12 means "send to script" (this will work if you follow the instructions above to paste it back into the client).

You need %% in the string.match, like I showed, to "escape" the meaning of a % in the send field. If you used a script file it would be done slightly differently, eg:


function attach_trigger (name, line, wildcards)
  who = string.match (wildcards [1], "%S+$")

  Send ("a " .. who)
  Send ("a " .. who)
  Send ("c dobuza " .. who)
end -- attach_trigger
Amended on Mon 28 May 2018 09:13 PM by Nick Gammon
#2
Hey thank-you so much for your help! It was very informative, Only one thing I have to ask is what does %S+$ do? I know "+" is one or more of the previous characters (be+d, Beeed etc..) and $ is end of string. But unsure as to what %S does? is it just code for not space? Because I would have assumed would would have been (! +$). Thank-you again.

Also do you mind if I ask more scripting questions in here or are new topics better?
Australia Forum Administrator #3
The page for string.find (which encompasses the syntax for string.match) explains it:

http://www.gammon.com.au/scripts/doc.php?lua=string.find

%S is indeed code for "not space". %s is the code for space.

So, %S+ is one or more of anything but a space.

The Lua pattern matching is a little different to regular expressions used in triggers.

Quote:

Also do you mind if I ask more scripting questions in here or are new topics better?


New topics are better, they are more easily searchable by other people.