ILua scripting

by Unknown

Back to Project L.

Unknown2007-06-07 17:16:14
QUOTE(Theomar @ Jun 7 2007, 05:30 PM) 415552
Looks like I won't be needing multi-line triggers anymore, but thanks for the help. smile.gif

Pardon my curiosity but, how come?
Unknown2007-06-07 17:29:18
Lusternia now has the option for wrapwidth 0, meaning everything can be on one line now and wrapped locally instead.
thumup.gif
Unknown2007-06-07 19:31:38
Oh gawd.. Makes me semi-regret moving back to Impy. sad.gif
Rothmar2007-10-04 10:27:35
I can't get the lrex to work still freaked.gif But the questions I have are as follows:

-Is this the same as the regex_match, regex_compile features on Whyte's original ilua modules?

-How would these (lrex/regex_match) fair with string.match in terms of speed?
Unknown2007-10-04 10:40:56
QUOTE(rothmar @ Oct 4 2007, 12:27 PM) 446623
I can't get the lrex to work still freaked.gif But the questions I have are as follows:

-Is this the same as the regex_match, regex_compile features on Whyte's original ilua modules?

-How would these (lrex/regex_match) fair with string.match in terms of speed?

Yes, they work the same way. Whyte didn't implement the PCRE functionality until just recently though. But now that they're in, you can pretty much skip loading an external lib for it.

Regexp is slower than Lua pattern matching. Period. My tests shows that string.match() can be up to x30 times faster, depending on the pattern ofcourse. Personally I have abondoned regexp and is now exclusively using Lua patterns throughout my system. It should be mentioned that this is at a loss of some minor functionality though, such as (?:grouping|groups) whereas Lua patterns only supports single character grouping with .
Forren2007-10-04 10:43:34
Hmm.. what I'd really like to do is have mudbot change the - from blackout automatically to <>-. How would I do this with Lua?
Unknown2007-10-04 10:56:12
I posted a prompt script a while back that will substitute your prompt with ILua. I've since made a few minor tweaks to it (because it goofed up the composer when I forgot to unload it first), but what I originally posted should be enough for you to go on.
Forren2007-10-04 11:02:21
QUOTE(Zarquan @ Oct 4 2007, 06:56 AM) 446630
I posted a prompt script a while back that will substitute your prompt with ILua. I've since made a few minor tweaks to it (because it goofed up the composer when I forgot to unload it first), but what I originally posted should be enough for you to go on.


Saw it - I don't want my prompt modified in any way, other than during blackout when my prompt is just ^-$ if no gem exists. A simulated gem for blackout is all I need.
Unknown2007-10-04 11:12:16
So... take my code and make it do what you want. You're asking a fairly simple thing, so just remove my complex stuff with the stats and do your gem thing instead. tongue.gif
Rothmar2007-10-12 14:08:09
QUOTE(CroX @ Oct 4 2007, 10:40 AM) 446626
....It should be mentioned that this is at a loss of some minor functionality though, such as (?:grouping|groups) whereas Lua patterns only supports single character grouping with .


-If I decide to make a trigger to highlight names e.g. (tim|tom|jill|jack), does it mean I should be using regex instead of lua matching?
If so, can anyone please show me an example for using regex_compile and regex_match (the newest implementations for ilua by whyte)?

-following whyte's website examples, if I make aliases: for example
function aliasone( input )
if input == "hello" then
echo("Hello world!")
end
mb.client_aliases = aliasone

function aliastwo( input )
if input == "bye" then
echo("Bye world!")
end
mb.client_aliases = aliastwo

The last alias will always be the only one that works. Something to do with being the only variable stored in mb.client_aliases? This is also the case for triggers, so I'm not sure how to make multiple triggers/aliases work.

Is there a way around this? Or would you recommend making aliases/triggers differently?
============================================================
Edit. Thanks Xinael, I understand about the aliases/triggers now.

Also, instead of gagging the old prompt and echoing a new one, how can I use the replace() to do this?
Replace(oldprompt, oldprompt, newprompt, true)
Like that? Am guessing the use of this function from the mushclient site.
Xinael2007-10-12 14:39:26
Have a look at this. The examples there aren't ideal, but should give you an idea.
Unknown2007-10-14 10:09:42
QUOTE(rothmar @ Oct 12 2007, 04:08 PM) 449174
Also, instead of gagging the old prompt and echoing a new one, how can I use the replace() to do this?
Replace(oldprompt, oldprompt, newprompt, true)
Like that? Am guessing the use of this function from the mushclient site.

No. Your safest bet is to do something like this:

CODE
newprompt = atcp.health .. "(" .. math.floor(atcp.health / atcp.max_health * 100) .. "%)h "
replace(-1, newprompt)
Rothmar2007-10-19 14:16:24
I am very dumb, so my ilua 'system' has barely started.
My list of questions, please bear with me!

1)
CODE
prompt = {}
function prompt:process(line)
--lots of code here
end
return prompt

I'm trying to understand how Zarquan's prompt script works, I'm trying to write out my own.I see 'prompt:process(mb.line)' ... what do these xx:yy() functions actually mean? I saw that the prompt = {} was made at the start, so I guess it's something to do with tables. Also, what is the purpose for 'return prompt' at the end?

2)
CODE
alias = {test1 = 'ponder', test2 = tryfunction}

function mb.client_aliases (input)
    local i, params = 0, {}
    for match in string.gmatch(input,"%S+") do
    params = match
    i = i+1
    end
    
    if alias] then
        params = input
        send (alias])      
        return true
    else
        return false
    end
end

This is an alias parser that I'm trying to use. How can I call functions from an alias e.g. tryfunction = function() echo('works'!) end. confused.gif ?

3) Please forgive my lack of knowledge but is it possible to provide me an example of a trigger parser too? I can't come to grasp how I can track my triggers through this one main parser. I've read over the previous example scripts but still confused.
Can you make an example of :
-triggering from a word in a line (e.g. highlight) and
-triggering from a whole line?
To be run in a single trigger parser?

Sorry to be pounding the forums with questions.
Unknown2007-10-19 18:48:16
QUOTE(rothmar @ Oct 19 2007, 04:16 PM) 451336
1)

What that does is that it puts the function yy() inside the xx table. So you'll end up with something like:
CODE
xx = { function yy(xx) code here end }

When you call the function, using the xx:yy() convention, you're also secretly passing along a parameter called self, which can be used to access the table in which you have placed the function. You generally use this to group together stuff and mimic some OOP-behaviour. It's very neat in some cases. An example of it's usage (for Ilua, since it uses echo() instead of print()):
CODE
thing = { val = "test" }
function thing:show()
    echo(self.val)
end

That snippet would print: "test"

I do not know why Zarquan would use "return prompt" with require though. Care to unveil it for us, Zarquan?


QUOTE(rothmar @ Oct 19 2007, 04:16 PM) 451336
2)

Try this:
CODE
alias = { test1 = "send('ponder')", test2 = "echo('pondering!') mb.client_aliases('test1')" }
function mb.client_aliases(input)
    params = { string.match(input, "%S+") }
    if alias] then
        local func, err = loadstring(alias])
        if func then

            func, err = pcall(func)

        else

            echo(C.R .. "\\n\\nError:\\n" .. C.x .. err .. "\\n")

        end
    end
end

Some syntax error is bound to have sneaked into that code, since I'm writing it as I go. But perhaps you get the geist of it?


QUOTE(rothmar @ Oct 19 2007, 04:16 PM) 451336
2)

Consider this simple setup:
CODE
trigs = { { pattern = "You code some Lua.", script = "echo('This is kickass')" }, { pattern = "You code in some other language.", script = "send('scold me')" } }
function mb.server_lines()
    for k in ipairs(mb.lines) do
        set_line(k)
        for _, v2 in pairs(trigs) do
            if v2.pattern == mb.line do
                local func, err = loadstring(alias])
                if func then
                    func, err = pcall(func)
                else
                    echo(C.R .. "\\n\\nError:\\n" .. C.x .. err .. "\\n")
                end
            end
        end
    end
end
Unknown2007-10-23 01:25:45
I tried putting in the prompt script from the beginning of this thread just to start playing with ilua stuff. For some reason, it's not actually gagging my original prompt. For every prompt line, it's showing the original prompt and echoing the new prompt. I would think that mb.gag_line would gag every prompt, but somehow it's still getting through.

Any ideas what could be missing from that script that would be keeping it from gagging correctly?
Unknown2007-10-23 02:18:05
1. I use "return prompt" at the end of my script because it's a module and that helps group all the functions and stuff together. It's an ILua thing, or so the docs said...

2. Your prompt may just not match the pattern in the script for the gagging and/or you need to add/subtract the newline on your prompts. Just a guess, though.
Unknown2007-10-23 06:32:34
QUOTE(mitbulls @ Oct 23 2007, 03:25 AM) 452616
I tried putting in the prompt script from the beginning of this thread just to start playing with ilua stuff. For some reason, it's not actually gagging my original prompt. For every prompt line, it's showing the original prompt and echoing the new prompt. I would think that mb.gag_line would gag every prompt, but somehow it's still getting through.

Any ideas what could be missing from that script that would be keeping it from gagging correctly?

Depends on which version you're using. If you got the current from the CVS, you'll want to either replace(-1, "New prompt> ") or hide_line(-1) echo(-1, "New prompt>"). If you're using the old one you could do the same but with mb.gag_line = true and replace("New prompt> ") instead.

The manual covers the newer version and I'd strongly recommend upgrading to it, even though it'll require some hands on to get your old system working with it.
Unknown2007-10-23 15:55:55
This will all be a lot easier once I actually learn some Lua. Does Lua have the equivalent of a 'contains' operator? I can't find any mention of anything that would work in the manual or a quick google search. For example, for trigger execution how do you match part of the line instead of the whole line? I could use the LMTS cmp function to do something like cmp(*trigger line*), but that seems clunky. Is there a lua library function that would accomplish something similar?
Unknown2007-10-23 17:20:00
QUOTE(mitbulls @ Oct 23 2007, 05:55 PM) 452739
This will all be a lot easier once I actually learn some Lua. Does Lua have the equivalent of a 'contains' operator? I can't find any mention of anything that would work in the manual or a quick google search. For example, for trigger execution how do you match part of the line instead of the whole line? I could use the LMTS cmp function to do something like cmp(*trigger line*), but that seems clunky. Is there a lua library function that would accomplish something similar?

Definitely!

In ILua you have three real choices when it comes to pattern matching. First you have regexp, which is very powerful and since I'm sure you know about it I wont explain it further. It's power comes at a cost though, and that's in speed. Lua's native patterns remedies that as they're very fast (in my tests up to 30 times that of regexp, depending on complexity of the pattern) and allows for most anything regexp does. There's some features Lua patterns doesn't give but I have yet to run into a situation where I couldn't use them. Third and last choice is the cmp() function, which is very limited but also very fast. My tests showed me that Lua patterns are just as fast though, and even faster in some cases.

So my recommendation is that you go for Lua patterns. You can find more info on them in the reference manual.
Unknown2007-10-23 22:46:06
QUOTE(CroX @ Oct 23 2007, 01:32 AM) 452697
Depends on which version you're using. If you got the current from the CVS, you'll want to either replace(-1, "New prompt> ") or hide_line(-1) echo(-1, "New prompt>"). If you're using the old one you could do the same but with mb.gag_line = true and replace("New prompt> ") instead.

The manual covers the newer version and I'd strongly recommend upgrading to it, even though it'll require some hands on to get your old system working with it.


I'm still missing something here. I started with the code that Zarquan has in the first post, which echoed the new prompt nicely but didn't gag the old prompt. If I change it to just replace (-1, new prompt), it does not display the new prompt at all, just the old prompt. If I try to use hide_line, I get an error about trying to use a global function (which makes me assume that I'm on the old version and hide_line doesn't exist yet). If I'm on the new version, it seems like hide_line(-1) should do it. If I'm on the old version, it seems like mb.gag_line=true should do it, but for some reason neither one seems to be working.

Any other ideas I might be missing?

I might try to get the files off of the SF SVN. Is that what you were talking about, or is there another CVS somewhere?
.
EDIT: I take that back, I can't connect to the SF SVN; should have seen that coming