Page 1 of 1

basic lua instructions

PostPosted: Wednesday, 10th October 2012, 20:31
by splat
I'm an experienced programmer trying to learn LUA through DCSS. The apparent lack of documentation doesn't bother me, but it does mean I need to ask a few questions and check a few statements.

1. I'm making a lot of code changes while figuring out what properties and functions work. Each time I make a change, I have to exit and restart the game to use the new code. Is there a way to reload my .rc file from inside the game?

2. Examples are helpful for learning, even if they're out of date. I'm working with ZotBot and autopray2. I've seen references to other bots (autorobin, auto7hm) but the links are broken. Working links/files would be appreciated if anybody has some.

3. The parts of DCSS that attempt to call custom code use "clua.callfn". I'm pretty sure that's correct, but is it complete? This gives me potential hooks in: c_interrupt_activity, c_assign_invletter, c_kill_list, ready, hit_closest, hit_adjacent, ch_mon_is_safe, choose_stat_gain, and whatever par.section.c_str() is.

Thanks :)

Re: basic lua instructions

PostPosted: Thursday, 11th October 2012, 01:15
by neil
splat wrote:1. I'm making a lot of code changes while figuring out what properties and functions work. Each time I make a change, I have to exit and restart the game to use the new code. Is there a way to reload my .rc file from inside the game?


I think the best you can do is enter arbitratry lua code with & Ctrl-T in wizard mode; that is treated as dlua, though, so it's not perfect. There isn't a way to re-read a config file (and it wouldn't work right anyway, since many options are not idempotent).

splat wrote:2. Examples are helpful for learning, even if they're out of date. I'm working with ZotBot and autopray2. I've seen references to other bots (autorobin, auto7hm) but the links are broken. Working links/files would be appreciated if anybody has some.


A few bots:

The dat/clua directory in the crawl source (http://gitorious.org/crawl/crawl/trees/master/crawl-ref/source/dat/clua) might also be useful to look at.

splat wrote:3. The parts of DCSS that attempt to call custom code use "clua.callfn". I'm pretty sure that's correct, but is it complete? This gives me potential hooks in: c_interrupt_activity, c_assign_invletter, c_kill_list, ready, hit_closest, hit_adjacent, ch_mon_is_safe, choose_stat_gain, and whatever par.section.c_str() is.


Someone else will have to verify this, but I'm pretty sure all calls to user-supplied Lua from C++ do go through clua.callfn(); you did miss one call to c_save in clua.cc. Also, note that built-in Lua code can call user code as well; usually in that case you register your function in a list of hooks (usually named chk_something) rather than providing a function with a specific name. See for dat/dlua/userbase.lua for the dispatchers, and dat/clua/runrest.lua and gearset.lua for examples of using some of them.

Re: basic lua instructions

PostPosted: Tuesday, 10th June 2014, 17:38
by HilariousDeathArtist
I have some rcfiles which I hope can be of some use for those looking for examples.

  • HDAtravel - Creates a lua function which is used as a autotravel macro, which attempts to automate some out of combat tactics, such as resting, finding and eating food, channeling MP, and casting regen, animate skeleton, and sublimation of blood on chunks. This has lots of function definitions and clua calls.
  • SpoilerAlerts - Creates a function which runs every turn, parses the monsters in vision, checks player resists and level, and prints warning messages. This is starting to get a bit outdated and I don't really use or maintain it anymore, but it does contain some interesting lua.
  • HDamage - Tracks your hp and mp and prints messages detailing the amount lost or gained in a turn. Uses global variables to store values and uses a couple clua functions.
  • HilariousDeathArtist - My main rcfile which has some lua, includes the other files, and also has plenty of comments and examples of how to use the standard options.

Re: basic lua instructions

PostPosted: Friday, 19th December 2014, 20:40
by Slap and stab
neil wrote:I think the best you can do is enter arbitratry lua code with & Ctrl-T in wizard mode; that is treated as dlua, though, so it's not perfect. There isn't a way to re-read a config file (and it wouldn't work right anyway, since many options are not idempotent).


Actually, the best you can do is store your lua code in a separate file and reload the file in the runtime with the dofile function. It will run you lua file again and redefine everything in it. If you want to include more files from that lua, include them with dofile instead of require. Make sure you don't overwrite anything important this way. (like global variables storing your state)