ILua scripting

by Unknown

Back to Project L.

Unknown2007-05-21 21:58:13
Step 1. Download the LMTS files from the SourceForge project page. This includes ILua as a plugin module with the mapper for Lusternia. (You can edit the config to remove the LMapper, if you just want Lua support.)

Step 2. Extract the archive anywhere you like.

Step 3. Edit config.ilua.txt to include the following two lines:
CODE
module "cool"
load "triggers.lua"


Step 4. Create the file triggers.lua in the same directory as the files you extracted and put the following in it:
CODE
require "prompt"

function prompt_handler()
    if prompt:process(mb.line) then
        return true
    end

    return false
end

mb.server_prompt = prompt_handler


Step 5. Create the file prompt.lua in the same directory as the files you extracted and put the following in it:
CODE
prompt = {}

-- Mimic prompt colors similar to the way Lusternia does them
function show_gradient(v, mv)
    local s

    if (v == nil or mv == nil) then
        return "?"
    end

    if (v < 1) then
        s = C.D
    elseif (v <= mv * 0.25) then
        s = C.R
    elseif (v <= mv * 0.75) then
        s = C.Y
    else
        s = C.g
    end

    return s .. v .. C.x
end

function prompt:process(line)
    -- No gagging of the character name/password entry prompts!
    if (line == "Enter an option or enter your character's name. " or
        line == "What is your password? ") then
        return false
    end

    -- TODO: check for prompts in the editor (news, journal, etc)

    -- When ATCP is enabled, attempt to display a cooler prompt
    if (atcp) then
        local xp = C.y .. atcp.exp .. "%" .. C.x
        local hp = show_gradient(atcp.health, atcp.max_health)
        local mp = show_gradient(atcp.mana, atcp.max_mana)
        local ep = show_gradient(atcp.ego, atcp.max_ego)
        local en = show_gradient(atcp.endurance, atcp.max_endurance)
        local wp = show_gradient(atcp.willpower, atcp.max_willpower)
        local pow = show_gradient(atcp.power, 10)
        local fl = ""
        for k in string.gmatch(mb.line, "%d+h, %d+m.- (*)%-") do
            fl = k
        end

        -- Hide all prompts, substituting our own instead
        mb.gag_line = true
        mb.gag_ending = true

        echo(hp .. "h, " .. mp .. "m, " .. ep .. "e, " .. pow .. "p, " .. en .. "en, " .. wp .. "w " .. xp .. " " .. tostring(fl) .. "-")
    end

    return true
end

return prompt


Step 6. Run mb-core.exe to load the plugins and Lua source code into LMTS. (If there are any errors, verify files, paths, code, etc and try again.)

Step 7. Connect your favorite client to localhost on port 123 instead of lusternia.com on port 23. LMTS is a proxy server that handles text to and from the game, so it makes the connection to the game for you. (Don't worry about speed. It's far faster than most MUD clients.)


If everything is done right, you should see a nifty new prompt, complete with your percent-to-next-level! (Note: the prompt will have a linebreak after it, but with fewer blank lines than Lusternia's linebreak due to the nature of the gagging in LMTS. You can remove the linebreak by replacing the echo with echop.)

The triggers.lua file is simple, but it allows you to add processing of prompts (or other lines) with a single handler that calls other handlers. I tried setting up multiple modules, each with it's own trigger/alias handler, but it just doesn't seem to want to work for me.

Feel free to post comments, questions, etc.
Forren2007-05-22 01:58:25
Alright, so I switched over to the LMTS version of mudbot and LMapper, renamed my map to an LMap, and added those files for Lua. However, they don't seem to be doing anything. My prompt remains the same. I even made an alt to see if it was a Demigod oddity, and it's not doing anything either. I also copy-pasted the IMTS setup, not modifying anything except the config.lua.text.

The file names are prompt.lua and triggers.lua (not .txt too), correct? I even tried changing that - still nothing.
Unknown2007-05-22 05:51:51
QUOTE(Forren @ May 22 2007, 03:58 AM) 410568
Alright, so I switched over to the LMTS version of mudbot and LMapper, renamed my map to an LMap, and added those files for Lua. However, they don't seem to be doing anything. My prompt remains the same. I even made an alt to see if it was a Demigod oddity, and it's not doing anything either. I also copy-pasted the IMTS setup, not modifying anything except the config.lua.text.

The file names are prompt.lua and triggers.lua (not .txt too), correct? I even tried changing that - still nothing.

First, make sure that the ILua module is loaded. The file config.txt should contain:

CODE
dll "./i_lua.dll"

You can also try this by entering the command `mods and see if ILua is listed there. If it is then you can try `il mods to see if your module is loaded. It should show the name you put in config.ilua.txt, "cool" in Zarquan's case. Try these methods to see where it breaks.

Also, remember that the config files of IMTS and LMTS should contain two blank spaces at the end. Another undocumented "feature" for ILua is that you can specify where you have your LUA-files by doing this:

CODE
module "cooler" c:\\cooler\\
load "triggers.lua"


EDIT: Instead of doing the gagging and then the echo, Zarquan, you can use the replace() function.
Unknown2007-05-22 10:52:21
The LMTS archive on SourceForge has a valid config.txt and config.ilua.txt already. You just need to add the module and load triggers.lua in the config.ilua.txt as described.

Thanks for the tip on the undocumented feature, CroX. I actually just used load "Lua\\triggers.lua" in my file, and that worked, too. Heh.
Unknown2007-05-22 14:14:26
QUOTE(Zarquan @ May 22 2007, 12:52 PM) 410698
The LMTS archive on SourceForge has a valid config.txt and config.ilua.txt already. You just need to add the module and load triggers.lua in the config.ilua.txt as described.

Thanks for the tip on the undocumented feature, CroX. I actually just used load "Lua\\triggers.lua" in my file, and that worked, too. Heh.

What it does, I think, it that it adds your own dir to the LUA path, which is used by require. I do believe it contains the path "./lua/*.lua" but I'm not sure. Perhaps some small module could be included, which would do something trivial as add visuals for health/mana changes to the prompt. That could make it easier for people to get started toying with this wonderful module.

If you're seriously considering writing a system with ILua, which I'd really recommend, you might want to check out Lrexlib, which adds true regexp to LUA. String matching in LUA is fast but with this library added you achieve even greater speed and much more useful pattern matching. You can find it at http://luaforge.net/projects/lrexlib/.
Unknown2007-05-22 14:47:59
I had made myself a two-line prompt that showed current/max for everything and power and XP on the line above. It was a bit much, so I dialed it back down to a more basic prompt with just colors tweaked and XP added instead. Heh.

Thanks for the regex link. It'll really help me do this matching stuff, especially for capturing parts of the lines to use (names, limbs, etc). Lua patterns are nice, but not really something I want to convert to. I just hadn't gotten around to doing a serious number of triggers and aliases yet, but regex was on my list. You saved me a bit of time with this.
Unknown2007-05-22 15:10:46
Where's the best place to go for reference/documentation materials for ILua and LMTS? I'm interested in the concept, but significantly fuzzy on the implementation.
Unknown2007-05-22 15:20:46
This thread is pretty much it for now.

You can go to http://imts.sf.net to read some of Whyte's documentation on the original IMTS (on which this LMTS code is based), but it will differ in a few small ways.

CroX, how does one install and use that lrexlib stuff? I put the DLLs in my LMTS directory, but I can't seem to load the rex stuff in my source files. sad.gif
Unknown2007-05-22 15:39:12
QUOTE(Zarquan @ May 22 2007, 10:20 AM) 410771
This thread is pretty much it for now.

You can go to http://imts.sf.net to read some of Whyte's documentation on the original IMTS (on which this LMTS code is based), but it will differ in a few small ways.


Thanks!

So, if I'm reading it correctly, you can register delegates for things like receiving a line from the server, sending text to the server, etc?

I guess my question then becomes: what would you say the benefits are of managing, say, triggers and aliases in this way as opposed to the standard MUSHClient way of managing triggers and aliases? Is it because of the flexibility of the conditions? I can, for example, have the same alias do two different things depending on what conditions are true?

I may have just answered my own question.
Unknown2007-05-22 15:44:24
You register delegates for processing lines to/from the server, yes.

You can do a lot of the same stuff in MUSHclient, especially with its excellent support for Lua scripting. One of the advantages of ILua is that you could use almost any other client (no, not Nexus) to connect to Lusternia through LMTS and your system aliases and triggers would still work just the same. It also allows you to change the text before it ever reaches the server or your client, being the middle man, so you can completely customize your prompt without any messing gagging in your client and what-not.
Unknown2007-05-22 15:48:29
QUOTE(Zarquan @ May 22 2007, 10:44 AM) 410781
You register delegates for processing lines to/from the server, yes.

You can do a lot of the same stuff in MUSHclient, especially with its excellent support for Lua scripting. One of the advantages of ILua is that you could use almost any other client (no, not Nexus) to connect to Lusternia through LMTS and your system aliases and triggers would still work just the same. It also allows you to change the text before it ever reaches the server or your client, being the middle man, so you can completely customize your prompt without any messing gagging in your client and what-not.


Very nice.

How does this change the offline trigger testing process?
Unknown2007-05-22 15:55:33
Well, so far I don't know of any way to test offline with MudBot. You might get away with setting up a dummy server and using that for testing. I had one I found that let me feed lines as I wished and could even play back entire logs just as though it happened on the MUD itself.
Unknown2007-05-22 16:17:04
QUOTE(Zarquan @ May 22 2007, 10:55 AM) 410787
Well, so far I don't know of any way to test offline with MudBot. You might get away with setting up a dummy server and using that for testing. I had one I found that let me feed lines as I wished and could even play back entire logs just as though it happened on the MUD itself.


Does the server have to be IRE-specific?

This is starting to give me ideas of constructing an automated test suite for systems.
Unknown2007-05-22 16:25:00
You can use whatever server you like, especially if you just use ILua and not the mapper, voter, etc.
Unknown2007-05-22 18:50:25
QUOTE(Zarquan @ May 22 2007, 05:20 PM) 410771
CroX, how does one install and use that lrexlib stuff? I put the DLLs in my LMTS directory, but I can't seem to load the rex stuff in my source files. sad.gif

The documentation for lrexlib is somewhat outdated if I remember it correctly. With the DLLs in your LMTS folder, you load the library with:
CODE
rex = require("rex_pcre")

To use it, here's an example which'll precompile the pattern and then match it (for überspeed):
CODE
pattern = rex.newPCRE("(\\\\w+)")
_, _, match = pattern:match("world")
if match then
    echo("Hello " .. match .. "!")
end

Also, to easily run text through ILua as if it came from Lusternia you can use the `echo commando.
CODE
`echo Your head begins throbbing to the beat of the ominous noise.
Unknown2007-05-22 19:17:28
It looks for lua5.1.dll instead of lua51.dll, but I think I've got it installed and running now. Thanks for the help!
Forren2007-05-22 22:06:37
Fixed my issue - you need the two blank lines after #module etc.

I don't think I'll be using this script - the way I have my new system set up, I use the blackout prompt "<>-" to determine blackout - unless there's a way to trigger this only while blacked out? That would be quite useful.
Theomar2007-05-22 23:50:09
Actually, I've been looking at this, and it seems easier for me to use for a system than Whyte's (mainly cause I don't know C++), but what I would like to know is how would I go about making my own triggers for it?

I know Whyte's had some wacky thing to make them, so does this have the same thing, or have you not included that functionality yet?
Unknown2007-05-22 23:59:33
QUOTE(Forren @ May 22 2007, 06:06 PM) 410903
Fixed my issue - you need the two blank lines after #module etc.

I don't think I'll be using this script - the way I have my new system set up, I use the blackout prompt "<>-" to determine blackout - unless there's a way to trigger this only while blacked out? That would be quite useful.



You can substitute your regular prompt and/or your blackout prompt. Not only this, but you can have your full stats when blacked out, so you still know when to sip what. You can just add a new flag to your prompt flags to indicate that you're blacked out.
Unknown2007-05-23 00:02:48
QUOTE(Theomar @ May 22 2007, 07:50 PM) 410942
Actually, I've been looking at this, and it seems easier for me to use for a system than Whyte's (mainly cause I don't know C++), but what I would like to know is how would I go about making my own triggers for it?

I know Whyte's had some wacky thing to make them, so does this have the same thing, or have you not included that functionality yet?



This is Whyte's code adapted to Lusternia (mostly the mapper). The MudBot program is written in C, but that's not what you use to write a system for it. You use Lua with the ILua module, as I've demonstrated here. Whyte's IMTS site has a wiki with a couple simple examples on how to code for ILua. It's not exactly the way I use ILua, but it might give you an idea on how to make your own aliases and triggers with it.

http://imts.sourceforge.net/doku.php?id=il...a962247a6c2912d