Help w/lua scripting

Posted by Salaz on Sat 20 Sep 2008 12:04 AM — 7 posts, 29,308 views.

#0
I'm relatively new to mush and lua but i am able to create simple aliases,triggers and timers.
I'd like to learn more, and i find that i learn best when i have examples i can look at. so basically i'm asking if anyone has simple scripts they've written in lua that i can i take a look at and learn from. (any simple script will do, but i play achaea and will appreciate any related material)

Secondly, i need help converting this zmud alias to mush:
#ALIAS i_info {#ECHO "";#say %ansi( red)~[%ansi( gray)SCS%ansi( red)~]%ansi( gray): %ansi( white)%expand( %-1)%ansi( gray)}

what it does is similar to world.AnsiNote but instead of just displaying the note, it adds "SCS:" to it with all that coloring.

And finally, i wana write a script that gags/highlights/notes certain text from the output. But i dont know how to go about doing that. I mean i cant figure out whether to make triggers that call a certain function from my script file or just to make separate triggers and omit them from output.
This is an example in zmud that Daes wrote for achaea (just a part of it)

Quote:
#CLASS {Combat|Highlights, Echos, Gags, Warnings}
#REGEX {^You secrete (.+) and} {#gag;i_info ENVENOMED: %upper( %1)}
#REGEX {^There are no venoms on that item at present\.$} {#gag} "" {case}
#REGEX {^You sink your fangs into (.+), injecting just the proper amount of (.+)\.$} {#gag;i_info BITE: %upper( %1) with %upper( %2)}
#REGEX {^The chaos hound explodes into a fury of claws and teeth, rending your flesh\.$} {#gag} "" {case}
.


Hope i'm not asking too much and/or bothering ya'll
Appreciate the help in advance!
USA #1
To answer the second question in your script file (ctrl+shift+h):

function SCSnote ( passedtext )
    ColourNote ( "red", "black" , "[" ,
                 "gray", "black", "SCS",
                 "red", "black", "]",
                 "gray", "black", ":",
                 "white", "black", passedtext)
end

The ColourNote function needs 3 parameters; foreground, background, and text. It will also take those 3 parameters multiple times if you're using Lua.

For samples, I'd recommend looking at the plugins that Nick provides.

For gags, I'd recommend multiple triggers.
i.e.
trigger pattern: ^You secrete (.+) and .++$
trigger field value for Script: Secrete

in your script file again:

function Secrete (trig, line, wildcards, styleruns)
   SCSNote ("ENVENOMED: " .. wildcards[1]:upper() )
end


Or if you prefer,
trigger pattern: ^You secrete (.+) and .++$
trigger field value for Send To: Script After Omit
trigger field value for send:

SCSNote ("ENVENOMED: " .. string.upper("%1")) 


Here's a small 'gotcha!' for the people that use the send field of a triger or alias.
Mushclient needs to do a literal replacement of the %1, %2, etc before sending the text to the Lua compiler. Keep in mind if you want your code to be 'string.upper(poison)' (a variable named poison), or 'string.upper("poison")' (convert the literal string to upper case) when using the Send field in the trigger dialog. If you have any familiarity with C/C++, it's easier to think that '%1' isn't a variable that's passed into your script, it's a token that the precompiler replaces.





Amended on Sat 20 Sep 2008 01:17 AM by WillFa
#2
could you please explain what the following are and how they work:

wildcards[1]:upper() & string.upper("%1")

PS: does it make a difference which of the gag methods i use? i wana assume that the first method using my script file would be best cause it doesn't create too much clutter?
Amended on Sat 20 Sep 2008 01:47 AM by Salaz
#3
wildcards[1]:upper() is another way to write string.upper(wildcards[1]). It's what we call 'syntactic sugar' and you can use either method you choose.
USA #4
in the line:

function Secrete (trig, line, wildcards, styleruns)


those 4 parameters are passed into the function by Mushclient when the script is called. wildacards ans styleruns are tables, which are a data type in Lua. http://www.lua.org/pil/index.html (Programming in Lua, PiL) is a useful reference for learning the language and the general syntax. It's a book, and not just a reference manual; it will have some of the generic examples to learn the language syntax that you're looking for.

'syntactic sugar' is a concept that's explained in the PiL. It's just a more concise way of writing the same thing.

Tables are fun, and a bit more complex than I want to get in here when PiL says the same thing, only better. Tables are arrays, dictionaries, hashes, and maps found in other languages all rolled into one.
#5
Hi, me again and i've got a rather large project on my hands
trying to convert this :http://www.freewebs.com/heroica/Sabiru_Delusion.TXT
to mush.

Now i know its mostly just a bunch of aliases i have to make but there a few things i cant do. like these
Quote:
#ALIAS itog {#if (@Illusioning) {illusioning = 0;report Illusioning OFF} {illusioning = 1;report Illusioning ON}} "Delusion|Illusions"
#ALIAS it {#if (@illusion_targetting) {illusion_targetting=0;report Not targetting illusions} {illusion_targetting=1;report Targetting illusions}} "Delusion|Illusions"
#ALIAS ill {#if {@Illusioning=1} {#if (@illusion_targetting) {conjure @target illusion %-1} {conjure illusion %-1}}} "Delusion|Illusions"
#ALIAS ilt {conjure @target illusion} "Delusion|Illusions"
#ALIAS ilw {illusioning_what=%proper(%-1)} "Delusion|Illusions"


And i dont understand what the functions #Case and #if (%1="") in the following alias do or how they are used in mush

Quote:
ALIAS bxen {screte xentio;#CASE %random {ibcur} {iscur} {isscy} {isdar} {iseup} {ibscy} {ibdar} {ibeup} {ginsengillusion} {bloodrootillusion};#if (%1="") {bite @target} {bite %1}} "Delusion|Venom"


And finally, if all this can be a placed in a neat script like that on the site. hope i'm not asking too much and thanks in advance
Australia Forum Administrator #6
The illusions one could look like this:


<aliases>
  <alias
   match="itog"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>

illusioning = not illusioning -- toggle it

if illusioning then
  Note "Illusioning ON"
  Send "Delusion"
else
  Note "Illusioning OFF"
  Send "Illusions"
end -- if

</send>
  </alias>
</aliases>


This uses Lua variables, not MUSHclient variables, but that would be OK for a single session.

The bexn alias will be something like this:


<aliases>
  <alias
   match="^bxen( .+)?$"
   enabled="y"
   expand_variables="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

Send "screte xentio"

do
  local t = {
  "ibcur",
  "iscur",
  "isscy",
  "isdar",
  "iseup",
  "ibscy", 
  "ibdar",
  "ibeup",
  "ginsengillusion",
  "bloodrootillusion",
  }

  Send (t [math.random (#t)])  -- send a random item
end -- do

if "%1" == "" then
  Send ("bite @target")
else
  Send ("bite%1")
end -- if

Send "Delusion|Venom"
    </send>
  </alias>
</aliases>



I am guessing a bit at what the original does, but this should help you get started. The case is selecting from a group based on a random number, so I made a table of those words, and used math.random to pick an item from it.