Helping a friend

Posted by Shou on Fri 31 Dec 2010 11:19 PM — 8 posts, 30,267 views.

USA #0
Ok, like the subject says, I made some triggers and aliases for a friend to help him with some targeting and attacking issues. Thing is, I script in VBscript, so it would help a lot if someone could help convert these (three or four) items to Lua.

(kinda long..)


<triggers>
  <trigger
   group="automaul"
   lines_to_match="2"
   keep_evaluating="y"
   match="You spring forwards and maul *\'s *\.\n* parries the attack with a deft maneuver\.\Z"
   multi_line="y"
   regexp="y"
   send_to="12"
   sequence="10"
  >
  <send>if "%2" = "left leg" then
world.setvariable "limb", "right leg"
end if
if "%2" = "right leg" then
world.setvariable "limb", "head"
end if
if "%2" = "head" then
world.setvariable "limb", "left leg"
end if
if "%2" = "right arm" then
world.setvariable "limb", "left leg"
end if
if "%2" = "torso" then
world.setvariable "limb", "left leg"
end if
if "%2" = "left arm" then
world.setvariable "limb", "left leg"
end if</send>
  </trigger>
</triggers>



<triggers>
  <trigger
   expand_variables="y"
   group="automaul"
   match="You have recovered balance."
   sequence="10"
  >
  <send>target @limb
maul @target</send>
  </trigger>
</triggers>



<aliases>
  <alias
   match="automaul on"
   enabled="y"
   send_to="12"
   sequence="10"
  >
  <send>world.enablegroup "automaul", 1
world.note "Will automatically maul on balance"</send>
  </alias>
</aliases>



<aliases>
  <alias
   match="automaul off"
   enabled="y"
   send_to="12"
   sequence="10"
  >
  <send>world.enablegroup "automaul", 0
world.note "Will not automatically maul on balance"</send>
  </alias>
</aliases>


Please and Thank You in advance.
Australia Forum Administrator #1
  • You need to get the function name capitalization right. This post describes how to do it with a simple find-and-replace:

    http://www.gammon.com.au/forum/?id=6211

    For example "world.setvariable" should be "world.SetVariable".
  • You can omit "world." anyway, from both VBscript and Lua, to save typing.

    For example "world.setvariable" could be "SetVariable".
  • The IF statement looks a bit different.

    VBscript

    
    if "%2" = "left leg" then
      world.setvariable "limb", "right leg"
    end if
    


    Lua

    
    if "%2" == "left leg" then
      SetVariable ("limb", "right leg")
    end -- if
    


    It is "end" rather than "end if" but I usually do a find-and-replace to change "end if" to "end -- if" (the "--" starts a comment).

    Also note that tests for "equals" are "==" rather than "=".
  • Function arguments should be in brackets as illustrated above. That is:

    Instead of:

    
      world.setvariable "limb", "right leg"
    


    Use:

    
      SetVariable ("limb", "right leg")
    


You should be able to do a quick fixup in a few minutes based on the above. By the way, this looks wrong:


if "%2" = "head" then
  world.setvariable "limb", "left leg"
end if


Do you mean:


if "%2" = "head" then
  world.setvariable "limb", "head"
end if


Or why not just replace the whole lot of if-tests with:


SetVariable ("limb", "%2")

Amended on Sat 01 Jan 2011 01:09 AM by Nick Gammon
USA #2
Ok, thanks a lot for the help! What I did for the 'if' statement was supposed to be like that, but since I discussed it in the game, I wouldn't expect you to know why.
______________________________

hmm, actually I need a bit of help on one of those triggers.

I have this as the original lines:


You spring forwards and maul Shou's left leg.
Shou parries the attack with a deft maneuver. 


So I made it into a multi-line trigger and did this:


<triggers>
  <trigger
   group="automaul"
   lines_to_match="2"
   keep_evaluating="y"
   match="You spring forwards and maul *\'s *\.\n* parries the attack with a deft maneuver\.\Z"
   multi_line="y"
   regexp="y"
   send_to="12"
   sequence="10"
  >
  <send>if "%2" == "left leg" then
SetVariable ("limb", "right leg")
end
if "%2" == "right leg" then
SetVariable ("limb", "head")
end
if "%2" == "head" then
SetVariable ("limb", "left leg")
end
if "%2" == "right arm" then
SetVariable ("limb", "left leg")
end
if "%2" == "torso" then
SetVariable ("limb", "left leg")
end
if "%2" == "left arm" then
SetVariable ("limb", "left leg")
end</send>
  </trigger>
</triggers>


It always switches to the left leg, even when %2 is the right leg(which is supposed to switch to head) did I do something wrong or is it something with the variables?
Amended on Sat 01 Jan 2011 08:41 PM by Shou
Australia Forum Administrator #3
Now that you have a regular expression that isn't quite the right syntax for matching wildcards.

Template:regexp
Regular expressions
  • Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
  • Also see how Lua string matching patterns work, as documented on the Lua string.find page.


In particular, wildcards need to be in round brackets, and use ".*" instead of "*".

So something like "(.*?)" in a regular expression is the same as "*" not in a regular expression (the final ? makes it non-greedy).

So, without actually testing, it would be something like:


match="^You spring forwards and maul (.*?)'s (.*?)\.\n(.*?) parries the attack with a deft maneuver\.\Z"

USA #4
Ok awesome. That worked like a charm. Thanks a lot!
USA #5
here ya go This_guy!


<aliases>
  <alias
   match="rbs"
   enabled="y"
   expand_variables="y"
   send_to="12"
   ignore_case="y"
   sequence="8"
  >
  <send>if getvariable "direction" == "nw" then
SetVariable ("direction"), ("se")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "ne" then
SetVariable ("direction"), ("sw")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "se" then
SetVariable ("direction"), ("nw")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "sw" then
SetVariable ("direction"), ("ne")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "n" then
SetVariable ("direction"), ("s")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "w" then
SetVariable ("direction"), ("e")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "e" then
SetVariable ("direction"), ("w")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "s" then
SetVariable ("direction"), ("n")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "d" then
SetVariable ("direction"), ("u")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "u" then
SetVariable ("direction"), ("d")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "in" then
SetVariable ("direction"), ("out")
Execute ("ridebyshot @target @direction")
end
if getvariable "direction" == "out" then
SetVariable ("direction"), ("in")
Execute ("ridebyshot @target @direction")
end</send>
  </alias>
</aliases>


enjoy
Australia Forum Administrator #6
Two comments on the above code:

  1. In Lua, the functions are case-sensitive, so instead of getvariable you should have GetVariable.
  2. The replacement of things like @target and @direction are done by the client before the script is executed, so the line:

    
    Execute ("ridebyshot @target @direction")
    


    ... would refer to the original value of the direction variable, not the one which was set a line above. This is probably not what you want. Better would be:

    
    Execute (string.format ("ridebyshot %s %s",
             GetVariable "target",
             GetVariable "direction"))
    


Also I can't help thinking it would be neater if you used a table, eg.


local rev_dir =  {
  n = "s",
  s = "n",
  e = "w",
  w = "e",
  u = "d",
  d = "u",
  ne = "sw",
  sw = "ne",
  nw = "se",
  se = "nw",
  ["in"] = "out",
  out = "in",
  }  -- end of rev_dir

-- find reverse direction
local dir = rev_dir [GetVariable "direction"]

-- if found, swap to opposite
if dir then
  SetVariable ("direction", dir)
  Execute (string.format ("ridebyshot %s %s",
         GetVariable "target",
         dir))
end -- if

Amended on Sat 08 Jan 2011 03:06 AM by Nick Gammon
USA #7
Oh ok, thanks a lot for fixing it. Don't really script in Lua that much (or at all really). Think your version will actually work