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.

40d:Macro design

From Dwarf Fortress Wiki
Revision as of 17:09, 12 November 2009 by HebaruSan (talk | contribs) (moved Macro Design to Macro design: same upper/lower case as Trap design)
Jump to navigation Jump to search

Playing Dwarf Fortress means lots of typing. Macros can reduce this typing, but they must be planned and designed carefully. This page discusses how to design a macro and avoid common problems. The specific operation or structure for which you are creating a macro is beyond the scope of this page.

Macro Delay

Forum and wiki participants have observed that some macros can take hours to run. This happens because there is a built-in delay between each macro command; since the game as shipped includes no preset macros, this delay is set to a value that allows the human player to see each move and debug any mistakes.

The solution is to change MACRO_MS in init.txt from 150 to 0. Most macros on any system commonly run better with some delay between commands[Verify]; a MACRO_MS:1 seems to be a good run-time setting.

Planning

There are thousands of ways to do anything, but we would like to pick the best or near-best solution. In the case of DF's macros we want to see our macro run in the fewest steps possible, getting the most out of each command.

Start by planning out your design as a series of steps to create the final result. Then code each step individually, while taking note of the before and after steps. Like the complete macro, each step has many possible different coding solutions. Try to pick one that is simple (short) but also goes best with the step before it, and will end somewhere that's advantageous for the next step. Try to keep a step to a single type of operation; this will save the time of continuously switching between modes.

Example: Fields (boxes or area-selections) have four corners. Your code traverses from one corner to the opposite corner to define the field. It doesn't matter which corner you start at. So pick a start and stop point of your field that's convenient for the next and previous steps. This saves a lot of time spent walking the cursor around.

Repeats

The macro syntax allows commands to be repeated using a numeric parameter. The following scripts take the same time to execute.

[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:1]
[MACRO:CURSOR_UP:20]

The condensed version is more readable, but these work the same way.

Movement

Most of the time a macro takes to run is spent moving the cursor around. This can be sped up through the use of the "_FAST" versions of the move commands and by moving diagonally.

Standard Movement

When you hold the Shift key in the game UI and press an arrow key, the cursor moves ten squares instead of one. Similarly, each cursor command has a "_FAST" version that jumps ten squares in the same time a normal movement command takes to travel on square.

The following code,

[MACRO:CURSOR_UP:33]

is the same as 33 moving commands however,

[MACRO:CURSOR_UP_FAST:3]
[MACRO:CURSOR_UP:3]

is the exact same action in only 4 commands.

Backpedaling

When the 1's digit is higher than 5, backing the cursor up can achieve faster run times.

[MACRO:CURSOR_UP:38]

Can be sped up with _FAST commands:

[MACRO:CURSOR_UP_FAST:3]
[MACRO:CURSOR_UP:8]

This brings 38 commands down to just 11. However this can be better optimized by moving past the intended destination and backing up.

[MACRO:CURSOR_UP_FAST:4]
[MACRO:CURSOR_Down:2]

Is the exact same cursor position change but in only 6 moves. This can have side effects however (covered below) and must be done carefully.


Diagonal Movement

Most people using modern keyboards are familiar with the T-shaped arrow key block. The numeric keypad has arrows too, and on some keyboards the 1 3 7 9 keys have arrows as well. Regardless of whether your numeric keypad has arrows on it, you can move with the 1 3 7 9 keys. Diagonal movement is also possible in macros, and it can be used to reduce the number of commands required.

[MACRO:CURSOR_UP:8]
[MACRO:CURSOR_RIGHT:8]

can be replaced by

[MACRO:CURSOR_UPRIGHT:8]

This dropped the command count from 16 to 8. It also works for non-squares too.

[MACRO:CURSOR_UP:12]
[MACRO:CURSOR_RIGHT:16]

can be replaced by

[MACRO:CURSOR_UPRIGHT:12]
[MACRO:CURSOR_RIGHT:4]

This saved us 12 unneeded commands.

The "_FAST" works in the game and in your scripts on diagonals too. Further bringing down the the previous example command count to just,

[MACRO:CURSOR_UPRIGHT_FAST:1]
[MACRO:CURSOR_UPRIGHT:2]
[MACRO:CURSOR_RIGHT:4]

So from 38 commands to just 7, with a little planning.

Diagonal movement commands are written as vertical then horizontal with no separating "_"

Cursor Return

When designing scripts, it's a good idea to plan where to leave off. The two common ideas are:

  • Back where the script started, which is less disorienting to the user. This is best for macros that are intended to be used once.
  • In position for an instant repeat of the macro, for macros that are likely to be used several times in a row.

If your macro makes a 3 square wide section of hallway to the left, then put the cursor in a position that allows for a repeat call of the macro to add a second section.

Both are better than just stopping the cursor wherever it happens to be.


Problems to avoid

A step to far

When your script doesn't run as planned, check if you went too far on a movement action. To make a 5x5 box the code is

//written for ease of reading
[MACRO:SELECT:1]
[MACRO:CURSOR_LEFT:4]   
[MACRO:CURSOR_UP:4]
[MACRO:SELECT:1]

Those are 4's not 5's. The cursors current position is always 1, since it's really movement your defining not a size. Its an easy concept but also easy to forget/mess up.

Edge Collision

Edge Collision can be helpful in several situations. When you hit a edge you stop there, no matter how far your script wants to travel that way. This is per command and if your not expecting to hit a wall, it will corrupt that exeicution of the script. There is no way to avoid this. And in most cases, it's the script-runner's fault not the script-designer's.

However as a designer you can run into trouble with backpedaling optimizations as mentioned above. Backpedaling can jump you out of the working area of your script and then return, as long as there is enough extra space around your script area to do so. If an edge happens to be there, instant problem.

If your script is made inside a 17x4 rectangle, getting across it with the following code,
[MACRO:CURSOR_RIGHT_FAST:2]
[MACRO:CURSOR_LEFT:3]

is fine, as long as an edge wasn't 19 squares away.

It's recommend to only use backpedaling that does not go outside of your script's work area.

With backpedaling movements, that are larger than 10 spaces, a simple change of order of operations can fix this issue. Here is the above example, with an order changes

[MACRO:CURSOR_RIGHT_FAST:2]
[MACRO:CURSOR_LEFT:3]

is really just

[MACRO:CURSOR_RIGHT_FAST:1]
[MACRO:CURSOR_RIGHT_FAST:1]
[MACRO:CURSOR_LEFT:3]

so we can reorder it to

[MACRO:CURSOR_RIGHT_FAST:1]
[MACRO:CURSOR_LEFT:3]
[MACRO:CURSOR_RIGHT_FAST:1]

This is the same run time and number of commands, yet the cursor says inside the script area.

For script areas of 6, 7, 8, or 9 your just out of luck and I would suggest not backpedaling, but with a script area so small, speed isn't that much of an issue.
The backpedaling "LEFT:3" command, travels over the same area the previous "_FAST" command had covered. If it had been placed first, the cursor would have backpedaled out the other side.