I was wondering if it’s possible to use Lua to control my character in the same I would as a player.
I want to be able to do a simple script where my character would train in mechanics by prying open a door, then closing it if it opens. The script would stop when my character is hungry, thirsty, or tired.
At the moment no.
Lua support is currently limited to a rather narrow set of functions.
Would it be difficult for me to add?
I’m not all that familiar with the codebase, but know some C++. I wouldn’t know where to even look, though. I just need to simulate user input.
[quote=“deadmoose, post:3, topic:9130”]Would it be difficult for me to add?
I’m not all that familiar with the codebase, but know some C++. I wouldn’t know where to even look, though. I just need to simulate user input.[/quote]
May be doable.
Input is handled in a big loop in game.cpp in function game::handle_action(). At the top of the function (the if-else block), you could add own function that would check if Lua “wants” to force some input.
You would probably need to create some object that would hold the state of this Lua-driven input. This could possibly be just a single bool variable, say “lua_input” in game class.
If this check returns true, then instead of asking player for input, you’d run a script to generate it instead.
Since actions are strings, it should be rather easy to pass them from Lua to C++ - just have the function run a Lua script that returns a string, then return that string back to handle_action().
Most (all?) Lua code is in catalua.cpp. Probably the best example is the use_function::call function there, because it is invoked from C++, calls Lua scripts and gets a return value from them.
The biggest problem here is understanding how C++ and Lua connect, but catalua.cpp has some good examples. It has loading lua script from file, setting up environment for the script and finally calling it.
You may need to define some extra wrapper functions, such as furn_at(x,y) (to open/close doors). Those are in src/lua/catalua.cpp. They have rather simple (copy+paste) structure.
tl;dr You want to make game::handle_action() get a string from a script and the script to be able to “see” furniture.
Thank you for the in-depth reply. I have my work cut out for me, but this is as much as I could of asked for to get started.
Just FYI, for basic things that only require global keybinding inputs like walking around and melee combat, this would do it for you, but for any menu interaction you’re going to have trouble (by which I mean you’d have to add hooks into the code all over the place).
Depending on what you’re doing a much bigger problem is getting output from the game, any kind of AI is going to require extensive work to make game state visible to your code. If you’re looking at plugging in a macro system or something similar though, that wouldn’t be a problem.