A few questions about clua


Problems running or configuring the software, commands & options, compiling, different platforms, using the interface, documentation, etc.

Slime Squisher

Posts: 352

Joined: Monday, 14th December 2015, 00:43

Post Wednesday, 13th July 2016, 18:23

A few questions about clua

Not sure if this is the right forum, feel free to move the thread if necessary. I'm also not particularly good at this whole code diving thing so it'd be cool if you could keep your answers relatively simple.

1. Is there any even partial documentation of functions exposed through clua? I remember seeing a very incomplete one somewhere but now I can't find even that.
2. Is player's MR value exposed in any way? l_you.cc has all other resistances but no MR unless I'm blind.
3. How do lua scripts in online .rc files even work, are they executed every player action? I can copy stuff from other players' rc files but it wouldn't hurt to have some basic knowledge on how things work.

Mines Malingerer

Posts: 48

Joined: Tuesday, 21st June 2016, 19:08

Post Wednesday, 13th July 2016, 23:37

Re: A few questions about clua

I've been messing with it recently, so I'll try to give the important stuff that I actually know about. I'm going to include stuff that you obviously already know, just for the sake of completeness. I would like to get enough basic info so we can at least create a tiny 'quick start guide' or some such; even if it only has a few things, like ready() and choose_stat_gain() it'll still have been extremely helpful for me when I was trying to figure this stuff out.

Firstly, bind something to CMD_LUA_CONSOLE.. You'll probably want to debug and mess with stuff in the console while the game is running to figure it out faster at times.

A lot of the code you're looking for is in the source folder, https://github.com/crawl/crawl/tree/mas ... ref/source , and most of the functions are in the files in the format: l_(blah).cc

You'll see at the bottom of a lot of these pages something like 'luaL_openlib' with some functions being pushed through. Some of it is clua, and some is dlua. For example, on https://github.com/crawl/crawl/blob/mas ... e/l_you.cc find "you_clib", there should be a table definition full of good stuff, and then the table pushed through that function

  Code:
luaL_openlib(ls, "you", you_clib, 0)


And in our rc we can access that you_clib table of functions using 'you.(table key)'. So you.turns() (or maybe you.turns) will give you turn number.

Another method I used to figure out exactly what I had access to was using the console:

  Code:
for x, c in pairs(_G) do
  crawl.mpr(x)
end


_G is a table with all your globals, so it'll list lua functions, crawl functions, everything. And use crawl.mpr(type(blah)) to figure out if it's a function or whatever. Luckily, most of the clua functions are in neat little tables (inventory, you, crawl, etc), which you can loop through just like above.

General lua warnings.. Lua arrays are actually tables, I think? And they tend to start at 1, unless you manually started it at 0. So the keys can be anything, just like a table/dict. I guess that's the only thing I can think of atm. I like Lua. :)

-------------------------------------------------------

Some of those functions I feel are worth mentioning:

crawl.mpr() prints to the screen. I suggest aliasing it for quicker access.

crawl.messages([num of messages]) is, as far as I know, the only way to get the chat log info to trigger events on messages from the chat log. For pattern matching, watch that crawl.mpr automatically capitalizes the first letter even if it's actually lowercase, made a bug from that.

crawl.sendkeys(), crawl.process_command(), crawl.process_keys(), crawl.do_commands(), crawl.runmacro() send commands to the game. They are all slightly different, so look through them.

-------------------------------------------------------

Some special functions you can define(Edit: found a couple more):

ready() is actually called by the game, so for actual lua automation I believe this is all you can use. It is called after every action completes. The autoattack code errors (pressing tab with nobody in view) do not trigger a ready(). Luckily you can just copy the whole tab code into your rc and add them there :p.

choose_stat_gain() is called when you level up. You can define it to sendkeys 's', 'i', or 'd', or even just have the function return the letter you want, and it'll pick that stat for you.

c_answer_prompt(prompt) is called when a prompt comes up, return true or false

c_trap_is_safe(trap) Qw has an example

auto_experience() "Calling a user lua function here to let players automatically accept the given skill distribution for a potion of experience.", not sure of the syntax

ch_force_autopickup() looks like it's a way to force an item onto your auto-pickup

-------------------------------------------------------

My 'bot'. It's much smaller and simpler than a real bot like qw, but it has examples of everything above. Most of it is just the copied over autoattack code, slightly modified. (The real autoattack code is here: https://github.com/crawl/crawl/blob/fca ... ofight.lua )

http://crawl.akrasiac.org/rcfiles/crawl-git/Dracbot.rc

As you can see, all the hard work is done by the auto explore and auto fight code that other people wrote. This really wasn't meant for public consumption, so if you want examples of good (or at least better) code, look at the other bots. :D

-------------------------------------------------------

Edit: I forgot to mention this, which wasn't initially obvious to me, although it might be in the docs.

But you can use functions and conditionals to directly set rc settings:

  Code:
: if you.race() == "Troll" then
autopickup = $%
: end


Another random thing, stolen from qw: string.char(27) is the 'escape' character, I think? It's the only way I could find to use sendkeys to exit out of a shop, probably good for some other menus as well.
Last edited by Dracunos on Thursday, 14th July 2016, 03:56, edited 3 times in total.

Mines Malingerer

Posts: 48

Joined: Tuesday, 21st June 2016, 19:08

Post Wednesday, 13th July 2016, 23:45

Re: A few questions about clua

Leszczynek wrote:Not sure if this is the right forum, feel free to move the thread if necessary. I'm also not particularly good at this whole code diving thing so it'd be cool if you could keep your answers relatively simple.

1. Is there any even partial documentation of functions exposed through clua? I remember seeing a very incomplete one somewhere but now I can't find even that.
2. Is player's MR value exposed in any way? l_you.cc has all other resistances but no MR unless I'm blind.
3. How do lua scripts in online .rc files even work, are they executed every player action? I can copy stuff from other players' rc files but it wouldn't hurt to have some basic knowledge on how things work.



For the sake of being less annoying, I'll just answer your questions as best I can, directly, so you don't have to read through the above.

1. I couldn't find any documentation with a solid list of these functions, sadly.

2. I'm surprised there's no simple you.mr() with the string of how resistant you are based on %, or number of '+'s. I couldn't find anything about mr.
EDIT: I can think of two solutions, barring a simple command. Use '@', which will send your magic resistance flavor text to the buffer. Then use crawl.messages(20) and find the text. The other solution is probably better.. MR can be calculated. You'll need your race, xl, and the enchantments on all your equipment, as well as any status effects that can affect MR, and if there's any special equipment that grants an irregular amount of mr, you'll want to catch that.. So probably pretty annoying. http://crawl.chaosforge.org/Magic_resis ... scriptions The first method is probably much easier, but might be able to copy from qw if he uses the second method.

3. ready() and choose_stat_gain() are called by the game, so you can use them for actual automation. The lua in your rc, and whatever you have in included files, is read when you start the game. But there are definitely people who can answer that better than me.

Hopefully some of my info helped you a bit, though.

Return to Technical Support

Who is online

Users browsing this forum: No registered users and 15 guests

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.