Post Sunday, 14th September 2014, 15:14

Using built in commands instead of crawl.processkeys

I've been working on writing lua scripts for this game to automate some of the simpler things you can do, and as reference I've been looking at the work of some of the other users on this forum who have written things like lua "bots" or autopray.lua. It seems to be convention that when your script needs to input a command to the game you use either the function crawl.sendkeys('key') or the function crawl.processkeys('key').

A good example of this is the delta_to_vi function that can be found in some variation in scripts that enable automated movement.
  Code:
local function delta_to_vi(dx, dy)
  local d2v = {
    [-1] = { [-1] = 'y', [0] = 'h', [1] = 'b'},
    [0]  = { [-1] = 'k',            [1] = 'j'},
    [1]  = { [-1] = 'u', [0] = 'l', [1] = 'n'},
  }
  return d2v[dx][dy]
end

Whenever the script needs to provide movement commands to the game it uses delta_to_vi to convert the idea of a desired direction of movement into the key on the keyboard that the user would press to move in that direction. It then uses the crawl.processkeys function to simulate pressing that key and send the command to the game.

I am curious if there is a way to use the commands that keys map to instead of simulating pressing the key that corresponds to that command. Keybind.txt has a list of the commands bound to each key within the game:
  Code:
b         CMD_MOVE_DOWN_LEFT
h         CMD_MOVE_LEFT
j         CMD_MOVE_DOWN
k         CMD_MOVE_UP
l         CMD_MOVE_RIGHT
n         CMD_MOVE_DOWN_RIGHT
u         CMD_MOVE_UP_RIGHT
y         CMD_MOVE_UP_LEFT

Is there a way that I can have my script call the command CMD_MOVE_LEFT to move my character left rather than having to use crawl.processkeys('h')? This would allow me to avoid problems that might be caused by different users having different keybindings, because while the 'h' key can be rebound to a different command it is unlikely anyone will change the behavior of the CMD_MOVE_LEFT command. I just don't know how to access or "call" the CMD_MOVE_LEFT command from within my script.

Does anyone know if there is an easy way to do this? Or should I just stick to using crawl.processkeys?

Thanks in advance.