v50 Steam/Premium information for editors
  • v50 information can now be added to pages in the main namespace. v0.47 information can still be found in the DF2014 namespace. See here for more details on the new versioning policy.
  • Use this page to report any issues related to the migration.
This notice may be cached—the current version can be found here.

Difference between revisions of "v0.34:Macros and keymaps"

From Dwarf Fortress Wiki
Jump to navigation Jump to search
(importing from v0.31)
Line 1: Line 1:
 
{{av}}
 
{{av}}
 +
{{Quality|Exceptional|14:18, 4 April 2011 (UTC)}}
 +
Playing Dwarf Fortress means lots of typing. The game has an internal macro/keymap system. Using it or any external program can save you a great deal of time when dumping, rewalling, designating, and so forth.
  
=== Macros and Keymaps ===
+
== DF macros ==
 +
=== Creating macros ===
 +
The controls for creating macros within DF are as follows:
  
In this context the macro is mean a set of keystrokes / input from the player. Keymap is a set of special key combinations to get special function, or shorten input.
+
*{{k|Ctrl}}+{{k|r}} = record
 +
*{{k|Ctrl}}+{{k|p}} = play
 +
*{{k|Ctrl}}+{{k|s}} = save
 +
*{{k|Ctrl}}+{{k|l}} = load
  
What this is good for?
+
To create a macro press {{k|Ctrl}}+{{k|r}} to begin recording your actions. When you have recorded all the action you want stop recording by hitting {{k|Ctrl}}+{{k|r}} again and save ({{k|Ctrl}}+{{k|s}}) the macro. The macro is then added to your macro list. To load a macro from the list just press {{k|Ctrl}}+{{k|l}}. You can then play the macro using {{k|Ctrl}}+{{k|p}}.
Did you had a migrants of 20 or more, and you were in trouble of make enough accommodations for them, before they got unhappy thoughts? Were you bored, by the repetitive creation of of a big bedroom? Not to worry anymore, it's possible to break down the task into much less interaction, and to speed up things.
 
  
What you need:
+
There is a directory data/init/macros for them. The macros are saved in .mak format. Even a recorded file for a simple macro - for example to create a 3 tiles wide ramp - may already consist of up to 50 commands listed. This is because every possible [[Key_bindings|binding of the key pressed]] is included in the macro and put in a block (and {{k|r}} for ramp has many by default).
under linux:
+
<pre>
* fluxbox (or your steady window manager, or shortcut editor)
+
pressing_enter_recorded
* xvkbd (or anything, that allows you to type something into an active window / program, as came from stdin)
+
SELECT
winder windows:
+
CLOSE_MEGA_ANNOUNCEMENT
*a good (free) program, that can do the same, and supports special characters
+
WORLD_PARAM_ENTER_VALUE
 +
SETUPGAME_SAVE_PROFILE_GO
 +
D_BURROWS_DEFINE
 +
D_MILITARY_ALERTS_SET
 +
End of group
 +
CUSTOM_CTRL_R
 +
End of group
 +
End of macro
 +
</pre>
 +
For this example the recording was started, enter was pressed and the recording was stopped. When using this macro every underlying command in the file will be called, if possible. If you are in the designation menu, it will react as a select, the other commands will be ignored. If you are in the burrow menu, it will work like pressing enter there. The macro alway ends with a block containing the end of it's recording. But executing macros seems to ignore this command. If you have changed your key bindings you'll get another result, because the underlying commands are recorded, not the keys pressed.<br />
 +
When creating or editing your own macros it is a good idea to use only those commands you really want.
 +
<pre>
 +
ramping_created
 +
DESIGNATE_RAMP
 +
End of group
 +
CURSOR_DOWN_Z
 +
End of group
 +
SELECT
 +
End of group
 +
CURSOR_RIGHT
 +
End of group
 +
CURSOR_RIGHT
 +
End of group
 +
SELECT
 +
End of group
 +
CURSOR_LEFT
 +
End of group
 +
CURSOR_LEFT
 +
End of group
 +
CURSOR_UP
 +
End of group
 +
End of macro
 +
</pre>
 +
This selfmade example will designate a 3 tiles wide ramp one z-level below and place the cursor to make the next execution of the macro continue the way down. The first line has to be the name of the file. You can see that there are grouping tags for every single keypress. These are important for a working macro.<br />
 +
It is unknown if there is the possibility of creationg loops/iterations, other programming features or comments.<br />
 +
There seem to be problems with changing removing and adding macros while the game is running. Save, quit and restart the game when you want to change something.
  
=== Fluxbox ===
+
=== Tuning macros ===
 +
The fewer commands a macro consists of, the faster it runs. This means you should avoid unnecessary steps by optimizing the "path" of your designations.
  
Fluxbox is a tiny window manager, that allows a lot of powerful customization. If you don't want to install fluxbox just for this, you should look for other solutions in your distro (KDE / Gnome). If you use / successfully setup fluxbox [[http://fluxbox.org/help]] you only need to edit .fluxbox/keys files. This contains the keyboard shortcuts.
+
The second and most effective way to increase speed is to remove all unnecessary commands DF recorded with an external texteditor. While the extra commands are ignored by the game, however they take time to be processed. To move a cursor 3 (up/down) or 4 (right/left) commands are recorded, most other keys are bound to more commands. Pressing d for example records more than 30 commands. Depending on what you do, you can increase the speed by 4 or more by reducing the number of commands in every group to the one you need. ''If you edit a macro, you'll have to restart DF afterwards.''
  
Mod1 Mod4 m None d b :ExecCommand xvkbd -text "\D2r\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\r"
+
The third way is to change settings in the init-files. In the base init file (data/init/init.txt) you will find the follow lines:  
 +
<pre>
 +
If you set KEY_REPEAT_ACCEL_LIMIT above one, then after KEY_REPEAT_ACCEL_START repetitions
 +
the repetition delay will smoothly decrease until repetition is this number of times faster
 +
than at the start.
  
Mod1 means windows / command funciton key (If you use keychains, the order of the modificators seems important :( )
+
[KEY_REPEAT_ACCEL_LIMIT:8]
Mod4 means alt
+
[KEY_REPEAT_ACCEL_START:10]
m is for the key m (so this will be windows + alt + m (special enough not to used in any other application
 
(this is important, because if fluxbox is intercepting a hotkey, it not pass along to the active application!))
 
None the following combinations are not using modifications keys (Important: use your last key in keychain as
 
non-modified key, or a big enough delay, or else your macro input will be applied to the modifier, you currently
 
pressing (see below))
 
d is for key D (like Dwarf Fortress)
 
b is for key B (as bedroom)
 
: means, that this is the end of key combinations, and from here the command to apply.
 
  
So, to start this particular keychain (hotkey), '''(windows + alt + m) d b'''
+
This controls the number of milliseconds between macro instructions.
  
:ExecCommand  tells fluxbox, to execute the followings
+
[MACRO_MS:15]
  
xvkbd this the virtual keyboard we will use
+
</pre>
-text tells xvkbd to don't display the virtual keyboard, just get the input from command line
+
This means that the speed between macro commands will gradually increase until it hits the limit. The secondary command is when the gradual increase in speed increases.
(there's file input is available, but it does not support special keys so easily)
 
"\D2r\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\r" This is the input, we will pass to
 
Dwarf Fortress (which will press r, and gives 12 + and a final enter)
 
The begining '''\D2''' commands xvkbd to wait 2*100ms. You might want to play with this, but I found,
 
that I need this, because the first input key is not passed always otherwise.
 
  
The + is used on keypad under DF, we access this by \[]. The enter is also a special character, used by \r. You might want to take a peek into the xvkbd's man page for some special keys.
+
== External utilities ==
  
So what this macro does at all:
+
=== [http://sun2design.com/quickfort/ Quickfort] ===
if you press: if you have large bedroom, and you don't want to make the boring task to create a large bedroom for all the beds, you can manage the bed by '''q''' (task and preferences in DF), and then press '''(windows + alt + m) d b''', which will give the order of '''r''' ++++++++++++ and an enter.
+
A spreadsheet-driven construction tool for Dwarf Fortress. Converts CSV files containing a "graphical" (or at least two-dimensional) representation of what you want to build into efficient DF macros. Comes with a number of scripts to get you started, some of them quite complex.
  
=== Trouble shooting ===
+
=== AutoHotKey ===
 +
#  Go to [[Utilities#AutoHotKey]] and download AutoHotKey.  Installation is simple and the program uses few system resources.
 +
#  Write macro scripts (file type .ahk), which may contain any number of commands.  You activate scripts by double-clicking .ahk files and deactivate them by right-clicking the AutoHotKey icon on the task bar.  Both of these can be done at any time - even right in the middle of a game.  AutoHotKey also allows for automated activation of scripts.
  
* My first characters are missing from the input
+
:'''Please see also the [[40d:Macros and Keymaps|40d macros page]], as much of what is there works perfectly fine. If you can verify it works, please move it to this page.'''
** - You have most probably insufficient delay at the start of the text. Try a bigger number, or a delay at all ('''\D2''')
 
* My macro fails / I cannot get it work at, and not even prints into a textbox.
 
** - You might have a last keychain key with a modifier.
 
  '''Mod1 Mod4 b :ExecCommand xvkbd -text "Hello World"'''
 
  This will most probably not give you any output, or even worst, start a few other applications. This is because the  
 
  keyboard interface is behaving additively. This means, if you plug 2 keyboards, and press a key on one of them, and  
 
  press another on the other one, the OS will see as both of them pressed. So, if you press on your keyboard Mod1 + Mod4,  
 
  and sending a stream of keys on the other, they will add up, and you will end up, as you typed the '''Hellow World'''
 
  with Mod1 + Mod4 pressed (Windows + Alt).
 
  Solutions:
 
  **Set a big enough delay at the beginning of your string ('''\D5Hello World'''), so you can release them.
 
  '''Mod1 Mod4 b :ExecCommand xvkbd -text "\D5Hello World"'''
 
  **Set the hotkey to a keychain that ends with a non-modified key
 
  '''Mod1 Mod4 m None b :ExecCommand xvkbd -text "Hello World"'''
 
  
=== Some useful macros with a short description ===
+
==== Troubleshooting Scripts ====
 +
Users may experience some issues in getting scripts to work, particularly when using looping scripts when experiencing low frame-rates.
 +
* If experiencing low frame-rates, try adding delays ("Sleep 100" to pause for 100 milliseconds for example) within loops to allow the interface to keep up. If there are nested loops, sometimes adding a pause at the end of an inner loop is all that is needed to flush the keyboard buffer
 +
* Another way to add delay during and after each simulated key press is to put <B>SetKeyDelay, 40, 40</B> at the start of the macro.
 +
* Make sure that Dwarf Fortress maintains focus. IM windows are the enemy! Who needs friends anyhow? You've got Dwarf Fortress.
 +
* This may go without saying, but most macros assume standard key-mappings. If you're using non-standard ones, you may have to edit the macro to get it to work.
 +
* Visiting liaisons can bring up screens that eat keystrokes, throwing a long-looping script out-of-phase with where it expects the game to be.  Wait for the farewell screen before running a long script.
 +
* The <B>SendPlay</B> function supports keys that the <B>Send</B> function does not, for example {{key|Shift-Enter}}.  According to the AutoHotKey documentation, <B>SendPlay</B> may also be better at preventing dropped keystrokes.
  
* Quick resizes a (bed)room (from the 'q' and set into cursored the bed)
+
==== General Fortress Mode Hotkeys Script ====
'''Mod1 Mod4 m None d b :ExecCommand xvkbd -text "\D2r\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\r"'''
+
An attempt at speeding up various designations. Includes an up/down stair builder, a fast move up/down, and some select-and-advance keys. Please see [[user:DDR#Dwarf_Fortress_General_AHK_Script]].
'''(Windows + Alt + m) d b'''
 
 
 
* Quick resizes 6 (bed)room, that are inline to the right with a distance of 2 title.
 
'''Mod1 Mod4 m None d v :ExecCommand xvkbd -text "\D2r\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\r\[Right]\[Right]\[Right]r\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\r\[Right]\[Right]\[Right]r\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\r\[Right]\[Right]\[Right]r\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\r\[Right]\[Right]\[Right]r\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\r\[Right]\[Right]\[Right]r\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\[+]\r\[Right]\[Right]\[Right]"'''
 
'''(Windows + Alt + m) d v'''
 
b..b..b..b..b..b..x (and the cursor will be stopped at the next item)
 
 
 
* Mass selector. It selects a lot of items from a list ((enter then down) * 39) (this is about 3 page down)) I use this particular macro to sell Junk to traders, without selling the bins. ''(If you select the bin, and then select any item for trading, the bin will be deselected.)''
 
'''Mod1 Mod4 m None d t :ExecCommand xvkbd -text "\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]\r\[Down]"'''
 
'''(Windows + Alt + m) d t'''
 
Warning, if you sell ~20 finished goods bins' contents, then you may expect that
 
the merchants will pack up for a while (probably for a few months).
 
 
 
* Quick macro to build a repeating pattern. For example, you want to build a bedroom, where the dwarf's items are 3 titles apart (2 title between them, and 6 of them). Will select the first available item, so if there's something, you don't want to used, forbid them (coffers / bags).
 
'''Mod1 Mod4 m None d 3 :ExecCommand xvkbd -text "\r\r\r\[Right]\[Right]\[Right]\r\r\r\[Right]\[Right]\[Right]\r\r\r\[Right]\[Right]\[Right]\r\r\r\[Right]\[Right]\[Right]\r\r\r\[Right]\[Right]\[Right]"'''
 
Build menu, and select the item, you want to build (with /*-+, not the keycode!, otherwise you end up
 
one wanted item, and 5 other armor stands) '''(Windows + Alt + m) d 3'''
 
b..b..b..b..b..x (and the cursor will be stopped at the next item)
 
 
 
Feel free, to add your own macros.
 

Revision as of 23:18, 1 March 2012

This article is about an older version of DF.

Playing Dwarf Fortress means lots of typing. The game has an internal macro/keymap system. Using it or any external program can save you a great deal of time when dumping, rewalling, designating, and so forth.

DF macros

Creating macros

The controls for creating macros within DF are as follows:

  • Ctrl+r = record
  • Ctrl+p = play
  • Ctrl+s = save
  • Ctrl+l = load

To create a macro press Ctrl+r to begin recording your actions. When you have recorded all the action you want stop recording by hitting Ctrl+r again and save (Ctrl+s) the macro. The macro is then added to your macro list. To load a macro from the list just press Ctrl+l. You can then play the macro using Ctrl+p.

There is a directory data/init/macros for them. The macros are saved in .mak format. Even a recorded file for a simple macro - for example to create a 3 tiles wide ramp - may already consist of up to 50 commands listed. This is because every possible binding of the key pressed is included in the macro and put in a block (and r for ramp has many by default).

pressing_enter_recorded
		SELECT
		CLOSE_MEGA_ANNOUNCEMENT
		WORLD_PARAM_ENTER_VALUE
		SETUPGAME_SAVE_PROFILE_GO
		D_BURROWS_DEFINE
		D_MILITARY_ALERTS_SET
	End of group
		CUSTOM_CTRL_R
	End of group
End of macro

For this example the recording was started, enter was pressed and the recording was stopped. When using this macro every underlying command in the file will be called, if possible. If you are in the designation menu, it will react as a select, the other commands will be ignored. If you are in the burrow menu, it will work like pressing enter there. The macro alway ends with a block containing the end of it's recording. But executing macros seems to ignore this command. If you have changed your key bindings you'll get another result, because the underlying commands are recorded, not the keys pressed.
When creating or editing your own macros it is a good idea to use only those commands you really want.

ramping_created
		DESIGNATE_RAMP
	End of group
		CURSOR_DOWN_Z
	End of group
		SELECT
	End of group
		CURSOR_RIGHT
	End of group
		CURSOR_RIGHT
	End of group
		SELECT
	End of group
		CURSOR_LEFT
	End of group
		CURSOR_LEFT
	End of group
		CURSOR_UP
	End of group
End of macro

This selfmade example will designate a 3 tiles wide ramp one z-level below and place the cursor to make the next execution of the macro continue the way down. The first line has to be the name of the file. You can see that there are grouping tags for every single keypress. These are important for a working macro.
It is unknown if there is the possibility of creationg loops/iterations, other programming features or comments.
There seem to be problems with changing removing and adding macros while the game is running. Save, quit and restart the game when you want to change something.

Tuning macros

The fewer commands a macro consists of, the faster it runs. This means you should avoid unnecessary steps by optimizing the "path" of your designations.

The second and most effective way to increase speed is to remove all unnecessary commands DF recorded with an external texteditor. While the extra commands are ignored by the game, however they take time to be processed. To move a cursor 3 (up/down) or 4 (right/left) commands are recorded, most other keys are bound to more commands. Pressing d for example records more than 30 commands. Depending on what you do, you can increase the speed by 4 or more by reducing the number of commands in every group to the one you need. If you edit a macro, you'll have to restart DF afterwards.

The third way is to change settings in the init-files. In the base init file (data/init/init.txt) you will find the follow lines:

If you set KEY_REPEAT_ACCEL_LIMIT above one, then after KEY_REPEAT_ACCEL_START repetitions 
the repetition delay will smoothly decrease until repetition is this number of times faster 
than at the start.

[KEY_REPEAT_ACCEL_LIMIT:8]
[KEY_REPEAT_ACCEL_START:10]

This controls the number of milliseconds between macro instructions.

[MACRO_MS:15]

This means that the speed between macro commands will gradually increase until it hits the limit. The secondary command is when the gradual increase in speed increases.

External utilities

Quickfort

A spreadsheet-driven construction tool for Dwarf Fortress. Converts CSV files containing a "graphical" (or at least two-dimensional) representation of what you want to build into efficient DF macros. Comes with a number of scripts to get you started, some of them quite complex.

AutoHotKey

  1. Go to Utilities#AutoHotKey and download AutoHotKey. Installation is simple and the program uses few system resources.
  2. Write macro scripts (file type .ahk), which may contain any number of commands. You activate scripts by double-clicking .ahk files and deactivate them by right-clicking the AutoHotKey icon on the task bar. Both of these can be done at any time - even right in the middle of a game. AutoHotKey also allows for automated activation of scripts.
Please see also the 40d macros page, as much of what is there works perfectly fine. If you can verify it works, please move it to this page.

Troubleshooting Scripts

Users may experience some issues in getting scripts to work, particularly when using looping scripts when experiencing low frame-rates.

  • If experiencing low frame-rates, try adding delays ("Sleep 100" to pause for 100 milliseconds for example) within loops to allow the interface to keep up. If there are nested loops, sometimes adding a pause at the end of an inner loop is all that is needed to flush the keyboard buffer
  • Another way to add delay during and after each simulated key press is to put SetKeyDelay, 40, 40 at the start of the macro.
  • Make sure that Dwarf Fortress maintains focus. IM windows are the enemy! Who needs friends anyhow? You've got Dwarf Fortress.
  • This may go without saying, but most macros assume standard key-mappings. If you're using non-standard ones, you may have to edit the macro to get it to work.
  • Visiting liaisons can bring up screens that eat keystrokes, throwing a long-looping script out-of-phase with where it expects the game to be. Wait for the farewell screen before running a long script.
  • The SendPlay function supports keys that the Send function does not, for example Shift-Enter. According to the AutoHotKey documentation, SendPlay may also be better at preventing dropped keystrokes.

General Fortress Mode Hotkeys Script

An attempt at speeding up various designations. Includes an up/down stair builder, a fast move up/down, and some select-and-advance keys. Please see user:DDR#Dwarf_Fortress_General_AHK_Script.