2022-06-21 05:53:59 -06:00
|
|
|
.. _modding-guide:
|
|
|
|
|
2022-06-20 12:38:23 -06:00
|
|
|
DFHack modding guide
|
|
|
|
====================
|
|
|
|
|
2022-09-14 14:19:10 -06:00
|
|
|
.. highlight:: lua
|
|
|
|
|
2022-09-12 01:14:03 -06:00
|
|
|
What is the difference between a script and a mod?
|
|
|
|
--------------------------------------------------
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2023-03-17 22:11:18 -06:00
|
|
|
Well, sometimes there is no difference. A mod is anything you add to the game,
|
2023-03-24 19:42:42 -06:00
|
|
|
which can be graphics overrides, content in the raws, DFHack scripts, any, or
|
|
|
|
all. There are already resources out there for
|
2023-03-17 22:11:18 -06:00
|
|
|
`raws modding <https://dwarffortresswiki.org/index.php/Modding>`__, so this
|
|
|
|
guide will focus more on scripts, both standalone and as an extension to
|
2023-03-24 19:42:42 -06:00
|
|
|
raws-based mods.
|
2023-03-17 22:11:18 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
A DFHack script is a Lua file that can be run as a command in
|
|
|
|
DFHack. Scripts can do pretty much anything, from displaying information to
|
|
|
|
enforcing new game mechanics. If you don't already know Lua, there's a great
|
|
|
|
primer at `lua.org <https://www.lua.org/pil/contents.html>`__.
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2022-09-12 01:14:03 -06:00
|
|
|
Why not just mod the raws?
|
|
|
|
--------------------------
|
2022-06-21 14:07:35 -06:00
|
|
|
|
2022-09-14 11:56:58 -06:00
|
|
|
It depends on what you want to do. Some mods *are* better to do in just the
|
2023-03-17 22:11:18 -06:00
|
|
|
raws. You don't need DFHack to add a new race or modify attributes. However,
|
|
|
|
DFHack scripts can do many things that you just can't do in the raws, like make
|
2023-03-24 19:42:42 -06:00
|
|
|
a creature that trails smoke or launch a unit into the air when they are hit
|
|
|
|
with a certain type of projectile. Some things *could* be done in the raws, but
|
|
|
|
a script is better (e.g. easier to maintain, easier to extend, and/or not prone
|
|
|
|
to side-effects). A great example is adding a syndrome when a reaction
|
|
|
|
is performed. If done in the raws, you have to create an exploding boulder as
|
|
|
|
an intermediary to apply the syndrome. DFHack scripts can add the syndrome
|
|
|
|
directly and with much more flexibility. In the end, complex mods will likely
|
|
|
|
require a mix of raw modding and DFHack scripting.
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2023-03-17 22:11:18 -06:00
|
|
|
The structure of a mod
|
|
|
|
----------------------
|
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
In the example below, we'll use a mod name of ``example-mod``. I'm sure your
|
|
|
|
mods will have more creative names! Mods have a basic structure that looks like
|
|
|
|
this::
|
2023-03-17 22:11:18 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
info.txt
|
|
|
|
graphics/...
|
|
|
|
objects/...
|
2023-07-20 18:44:09 -06:00
|
|
|
blueprints/...
|
2023-03-24 19:42:42 -06:00
|
|
|
scripts_modactive/example-mod.lua
|
|
|
|
scripts_modactive/internal/example-mod/...
|
|
|
|
scripts_modinstalled/...
|
|
|
|
README.md (optional)
|
2023-03-17 22:11:18 -06:00
|
|
|
|
|
|
|
Let's go through that line by line.
|
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
- The :file:`info.txt` file contains metadata about your mod that DF will
|
|
|
|
display in-game. You can read more about this file in the
|
|
|
|
`Official DF Modding Guide <https://bay12games.com/dwarves/modding_guide.html>`__.
|
|
|
|
- Modifications to the game raws (potentially with custom raw tokens) go in
|
|
|
|
the :file:`graphics/` and :file:`objects/` folders. You can read more about
|
|
|
|
the files that go in these directories on the :wiki:`Modding` wiki page.
|
2023-07-20 18:44:09 -06:00
|
|
|
- Any `quickfort` blueprints included with your mod go in the
|
|
|
|
:file:`blueprints` folder. Note that your mod can *just* be blueprints and
|
|
|
|
nothing else if you like.
|
2023-03-24 19:42:42 -06:00
|
|
|
- A control script in :file:`scripts_modactive/` directory that handles
|
|
|
|
system-level event hooks (e.g. reloading state when a world is loaded),
|
|
|
|
registering `overlays <overlay-dev-guide>`, and
|
|
|
|
`enabling/disabling <script-enable-api>` your mod. You can put other
|
|
|
|
scripts in this directory as well if you want them to appear as runnable
|
|
|
|
DFHack commands when your mod is active for the current world. Lua modules
|
|
|
|
that your main scripts use, but which don't need to be directly runnable by
|
|
|
|
the player, should go in a subdirectory under
|
|
|
|
:file:`scripts_modactive/internal/` so they don't show up in the DFHack
|
|
|
|
`launcher <gui/launcher>` command autocomplete lists.
|
|
|
|
- Scripts that you want to be available before a world is loaded (i.e. on the
|
|
|
|
DF title screen) or that you want to be runnable in any world, regardless
|
|
|
|
of whether your mod is active, should go in the
|
|
|
|
:file:`scripts_modinstalled/` folder. You can also have an :file:`internal/`
|
|
|
|
subfolder in here for private modules if you like.
|
|
|
|
- Finally, a :file:`README.md` file that has more information about your mod.
|
|
|
|
If you develop your mod using version control (recommended!), that
|
|
|
|
:file:`README.md` file can also serve as your git repository documentation.
|
|
|
|
|
2023-04-03 16:59:42 -06:00
|
|
|
These files end up in a subdirectory under :file:`mods/` when players copy them
|
|
|
|
in or install them from the
|
|
|
|
`Steam Workshop <https://steamcommunity.com/app/975370/workshop/>`__, and in
|
|
|
|
:file:`data/installed_mods/` when the mod is selected as "active" for the first
|
|
|
|
time.
|
2023-03-24 19:42:42 -06:00
|
|
|
|
|
|
|
What if I just want to distribute a simple script?
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
If your mod is just a script with no raws modifications, things get a bit
|
|
|
|
simpler. All you need is::
|
2023-03-17 22:11:18 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
info.txt
|
|
|
|
scripts_modinstalled/yourscript.lua
|
|
|
|
README.md (optional)
|
2023-03-17 22:11:18 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Adding your script to the :file:`scripts_modinstalled/` folder will allow
|
|
|
|
DFHack to find it and add your mod to the `script-paths`. Your script will be
|
|
|
|
runnable from the title screen and in any loaded world, regardless of whether
|
|
|
|
your mod is explicitly "active".
|
2023-03-17 22:11:18 -06:00
|
|
|
|
2022-06-21 05:58:36 -06:00
|
|
|
A mod-maker's development environment
|
2022-06-21 06:02:10 -06:00
|
|
|
-------------------------------------
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Create a folder for development somewhere outside your Dwarf Fortress
|
|
|
|
installation directory (e.g. ``/path/to/mymods/``). If you work on multiple
|
|
|
|
mods, you might want to make a subdirectory for each mod.
|
2022-06-21 14:07:35 -06:00
|
|
|
|
2023-04-03 16:59:42 -06:00
|
|
|
If you have changes to the raws, you'll have to copy them into DF's
|
|
|
|
``data/installed_mods/`` folder to have them take effect, but you can set
|
|
|
|
things up so that scripts are run directly from your dev directory. This way,
|
|
|
|
you can edit your scripts and have the changes available in the game
|
|
|
|
immediately: no copying, no restarting.
|
2022-06-21 14:07:35 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
How does this magic work? Just add a line like this to your
|
|
|
|
``dfhack-config/script-paths.txt`` file::
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
+/path/to/mymods/example-mod/scripts_modinstalled
|
2022-09-12 01:14:03 -06:00
|
|
|
|
2022-09-14 11:56:58 -06:00
|
|
|
Then that directory will be searched when you run DFHack commands from inside
|
|
|
|
the game. The ``+`` at the front of the path means to search that directory
|
2023-03-24 19:42:42 -06:00
|
|
|
first, before any other script directory (like :file:`hack/scripts` or other
|
2023-04-03 16:59:42 -06:00
|
|
|
versions of your mod in the DF mod folders).
|
2022-06-21 14:35:07 -06:00
|
|
|
|
2022-06-20 12:38:23 -06:00
|
|
|
The structure of the game
|
|
|
|
-------------------------
|
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
"The game" is in the global variable `df <lua-df>`. Most of the information
|
|
|
|
relevant to a script is found in ``df.global.world``, which contains things
|
|
|
|
like the list of all items, whether to reindex pathfinding, et cetera. Also
|
|
|
|
relevant to us are the various data types found in the game, e.g.
|
|
|
|
``df.pronoun_type`` which we will be using in this guide. We'll explore more of
|
|
|
|
the game structures below.
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2022-06-21 05:54:45 -06:00
|
|
|
Your first script
|
2022-06-21 06:02:10 -06:00
|
|
|
-----------------
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
So! It's time to write your first script. This section will walk you through how
|
|
|
|
to make a script that will get the pronoun type of the currently selected unit.
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
First line, we get the unit::
|
2022-06-20 12:38:23 -06:00
|
|
|
|
|
|
|
local unit = dfhack.gui.getSelectedUnit()
|
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
If no unit is selected, ``unit`` will be ``nil`` and an error message will be
|
|
|
|
printed (which can be silenced by passing ``true`` to ``getSelectedUnit``).
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
If ``unit`` is ``nil``, we don't want the script to run anymore::
|
2022-06-20 12:38:23 -06:00
|
|
|
|
|
|
|
if not unit then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
Now, the field ``sex`` in a unit is an integer, but each integer corresponds to
|
2022-09-14 11:56:58 -06:00
|
|
|
a string value ("it", "she", or "he"). We get this value by indexing the
|
2023-03-24 19:42:42 -06:00
|
|
|
bidirectional map ``df.pronoun_type``. Indexing the other way, with one of the
|
|
|
|
strings, will yield its corresponding number. So::
|
2022-06-20 12:38:23 -06:00
|
|
|
|
|
|
|
local pronounTypeString = df.pronoun_type[unit.sex]
|
|
|
|
print(pronounTypeString)
|
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Simple. Save this as a Lua file in your own scripts directory and run it from
|
|
|
|
`gui/launcher` when a unit is selected in the Dwarf Fortress UI.
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Exploring DF state
|
|
|
|
------------------
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
So how could you have known about the field and type we just used? Well, there
|
|
|
|
are two main tools for discovering the various fields in the game's data
|
2022-09-12 15:51:26 -06:00
|
|
|
structures. The first is the ``df-structures``
|
|
|
|
`repository <https://github.com/DFHack/df-structures>`__ that contains XML files
|
2023-03-24 19:42:42 -06:00
|
|
|
describing the layouts of the game's structures. These are complete, but
|
2022-09-14 11:56:58 -06:00
|
|
|
difficult to read (for a human). The second option is the `gui/gm-editor`
|
2023-03-24 19:42:42 -06:00
|
|
|
interface, an interactive data explorer. You can run the script while objects
|
|
|
|
like units are selected to view the data within them. Press :kbd:`?` while the
|
|
|
|
script is active to view help.
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
Familiarising yourself with the many structs of the game will help with ideas
|
2022-09-12 15:51:26 -06:00
|
|
|
immensely, and you can always ask for help in the `right places <support>`.
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Reacting to events
|
2022-06-21 14:07:35 -06:00
|
|
|
------------------
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
The common method for injecting new behaviour into the game is to define a
|
|
|
|
callback function and get it called when something interesting happens. DFHack
|
|
|
|
provides two libraries for this, ``repeat-util`` and `eventful <eventful-api>`.
|
2022-09-14 11:56:58 -06:00
|
|
|
``repeat-util`` is used to run a function once per a configurable number of
|
|
|
|
frames (paused or unpaused), ticks (unpaused), in-game days, months, or years.
|
|
|
|
If you need to be aware the instant something happens, you'll need to run a
|
2022-09-28 15:49:03 -06:00
|
|
|
check once a tick. Be careful not to do this gratuitously, though, since
|
2023-03-24 19:42:42 -06:00
|
|
|
running callbacks too often can slow down the game!
|
2022-07-03 08:59:01 -06:00
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
``eventful``, on the other hand, is much more performance-friendly since it will
|
2022-09-14 11:56:58 -06:00
|
|
|
only call your callback when a relevant event happens, like a reaction or job
|
|
|
|
being completed or a projectile moving.
|
2022-07-03 08:59:01 -06:00
|
|
|
|
2022-09-14 11:56:58 -06:00
|
|
|
To get something to run once per tick, we can call
|
|
|
|
``repeat-util.scheduleEvery()``. First, we load the module::
|
2022-07-03 08:59:01 -06:00
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
local repeatUtil = require('repeat-util')
|
2022-07-03 08:59:01 -06:00
|
|
|
|
2022-09-14 11:56:58 -06:00
|
|
|
Both ``repeat-util`` and ``eventful`` require keys for registered callbacks. You
|
|
|
|
should use something unique, like your mod name::
|
2022-07-03 08:59:01 -06:00
|
|
|
|
2022-07-14 10:46:12 -06:00
|
|
|
local modId = "callback-example-mod"
|
2022-07-03 08:59:01 -06:00
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
Then, we pass the key, amount of time units between function calls, what the
|
2022-09-12 15:51:26 -06:00
|
|
|
time units are, and finally the callback function itself::
|
2022-07-03 08:59:01 -06:00
|
|
|
|
|
|
|
repeatUtil.scheduleEvery(modId, 1, "ticks", function()
|
2022-09-14 11:56:58 -06:00
|
|
|
-- Do something like iterating over all active units and
|
|
|
|
-- check for something interesting
|
2022-07-14 10:46:12 -06:00
|
|
|
for _, unit in ipairs(df.global.world.units.active) do
|
2022-09-12 15:51:26 -06:00
|
|
|
...
|
2022-07-03 08:59:01 -06:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
``eventful`` is slightly more involved. First get the module::
|
2022-07-03 08:59:01 -06:00
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
local eventful = require('plugins.eventful')
|
2022-07-03 10:33:36 -06:00
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
``eventful`` contains a table for each event which you populate with functions.
|
|
|
|
Each function in the table is then called with the appropriate arguments when
|
|
|
|
the event occurs. So, for example, to print the position of a moving (item)
|
2022-09-12 15:51:26 -06:00
|
|
|
projectile::
|
2022-07-04 10:11:21 -06:00
|
|
|
|
|
|
|
eventful.onProjItemCheckMovement[modId] = function(projectile)
|
2022-09-14 11:56:58 -06:00
|
|
|
print(projectile.cur_pos.x, projectile.cur_pos.y,
|
|
|
|
projectile.cur_pos.z)
|
2022-07-04 10:11:21 -06:00
|
|
|
end
|
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
Check out the `full list of supported events <eventful-api>` to see what else
|
|
|
|
you can react to with ``eventful``.
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2022-09-14 00:16:54 -06:00
|
|
|
Now, you may have noticed that you won't be able to register multiple callbacks
|
2022-09-14 11:56:58 -06:00
|
|
|
with a single key named after your mod. You can, of course, call all the
|
2022-09-28 15:49:03 -06:00
|
|
|
functions you want from a single registered callback. Alternately, you can
|
|
|
|
create multiple callbacks using different keys, using your mod ID as a key name
|
|
|
|
prefix. If you do register multiple callbacks, though, there are no guarantees
|
|
|
|
about the call order.
|
2022-09-14 00:16:54 -06:00
|
|
|
|
2022-07-07 12:23:10 -06:00
|
|
|
Custom raw tokens
|
|
|
|
-----------------
|
2022-06-20 12:38:23 -06:00
|
|
|
|
2022-09-14 14:19:10 -06:00
|
|
|
.. highlight:: none
|
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
In this section, we are going to use `custom raw tokens <custom-raw-tokens>`
|
|
|
|
applied to a reaction to transfer the material of a reagent to a product as a
|
|
|
|
handle improvement (like on artifact buckets), and then we are going to see how
|
2022-09-12 15:51:26 -06:00
|
|
|
you could make boots that make units go faster when worn.
|
2022-07-07 12:23:10 -06:00
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
First, let's define a custom crossbow with its own custom reaction. The
|
2022-09-12 15:51:26 -06:00
|
|
|
crossbow::
|
2022-07-07 12:23:10 -06:00
|
|
|
|
|
|
|
[ITEM_WEAPON:ITEM_WEAPON_CROSSBOW_SIEGE]
|
|
|
|
[NAME:crossbow:crossbows]
|
|
|
|
[SIZE:600]
|
|
|
|
[SKILL:HAMMER]
|
|
|
|
[RANGED:CROSSBOW:BOLT]
|
|
|
|
[SHOOT_FORCE:4000]
|
|
|
|
[SHOOT_MAXVEL:800]
|
|
|
|
[TWO_HANDED:0]
|
|
|
|
[MINIMUM_SIZE:17500]
|
|
|
|
[MATERIAL_SIZE:4]
|
|
|
|
[ATTACK:BLUNT:10000:4000:bash:bashes:NO_SUB:1250]
|
|
|
|
[ATTACK_PREPARE_AND_RECOVER:3:3]
|
2022-07-14 10:46:12 -06:00
|
|
|
[SIEGE_CROSSBOW_MOD_FIRE_RATE_MULTIPLIER:2] custom token (you'll see)
|
2022-07-07 12:23:10 -06:00
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
The reaction to make it (you would add the reaction and not the weapon to an
|
2022-09-12 15:51:26 -06:00
|
|
|
entity raw)::
|
2022-07-07 12:23:10 -06:00
|
|
|
|
|
|
|
[REACTION:MAKE_SIEGE_CROSSBOW]
|
|
|
|
[NAME:make siege crossbow]
|
|
|
|
[BUILDING:BOWYER:NONE]
|
|
|
|
[SKILL:BOWYER]
|
|
|
|
[REAGENT:mechanism 1:2:TRAPPARTS:NONE:NONE:NONE]
|
|
|
|
[REAGENT:bar:150:BAR:NONE:NONE:NONE]
|
|
|
|
[METAL_ITEM_MATERIAL]
|
|
|
|
[REAGENT:handle 1:1:BLOCKS:NONE:NONE:NONE] wooden handles
|
|
|
|
[ANY_PLANT_MATERIAL]
|
|
|
|
[REAGENT:handle 2:1:BLOCKS:NONE:NONE:NONE]
|
|
|
|
[ANY_PLANT_MATERIAL]
|
2022-07-15 12:43:48 -06:00
|
|
|
[SIEGE_CROSSBOW_MOD_TRANSFER_HANDLE_MATERIAL_TO_PRODUCT_IMPROVEMENT:1]
|
|
|
|
another custom token
|
2022-07-07 12:23:10 -06:00
|
|
|
[PRODUCT:100:1:WEAPON:ITEM_WEAPON_CROSSBOW_SIEGE:GET_MATERIAL_FROM_REAGENT:bar:NONE]
|
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
So, we are going to use the ``eventful`` module to make it so that (after the
|
|
|
|
script is run) when this crossbow is crafted, it will have two handles, each
|
|
|
|
with the material given by the block reagents.
|
2022-07-07 12:23:10 -06:00
|
|
|
|
2022-09-14 14:19:10 -06:00
|
|
|
.. highlight:: lua
|
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
First, require the modules we are going to use::
|
2022-07-04 10:11:21 -06:00
|
|
|
|
2022-07-07 12:23:10 -06:00
|
|
|
local eventful = require("plugins.eventful")
|
|
|
|
local customRawTokens = require("custom-raw-tokens")
|
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
Now, let's make a callback (we'll be defining the body of this function soon)::
|
2022-07-07 12:23:10 -06:00
|
|
|
|
2022-07-14 10:46:12 -06:00
|
|
|
local modId = "siege-crossbow-mod"
|
2022-09-14 11:56:58 -06:00
|
|
|
eventful.onReactionComplete[modId] = function(reaction,
|
|
|
|
reactionProduct, unit, inputItems, inputReagents,
|
|
|
|
outputItems)
|
2022-07-07 12:23:10 -06:00
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
First, we check to see if it the reaction that just happened is relevant to this
|
2022-09-12 15:51:26 -06:00
|
|
|
callback::
|
2022-07-07 12:23:10 -06:00
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
if not customRawTokens.getToken(reaction,
|
|
|
|
"SIEGE_CROSSBOW_MOD_TRANSFER_HANDLE_MATERIAL_TO_PRODUCT_IMPROVEMENT")
|
|
|
|
then
|
2022-07-11 11:24:07 -06:00
|
|
|
return
|
|
|
|
end
|
2022-07-07 12:23:10 -06:00
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
Then, we get the product number listed. Next, for every reagent, if the reagent
|
2022-09-12 15:51:26 -06:00
|
|
|
name starts with "handle" then we get the corresponding item, and...
|
|
|
|
|
|
|
|
::
|
2022-07-07 12:23:10 -06:00
|
|
|
|
|
|
|
for i, reagent in ipairs(inputReagents) do
|
2022-09-12 15:51:26 -06:00
|
|
|
if reagent.code:startswith('handle') then
|
2022-07-07 12:23:10 -06:00
|
|
|
-- Found handle reagent
|
2022-09-12 15:51:26 -06:00
|
|
|
local item = inputItems[i]
|
2022-07-07 12:23:10 -06:00
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
...We then add a handle improvement to the listed product within our loop::
|
2022-07-07 12:23:10 -06:00
|
|
|
|
|
|
|
local new = df.itemimprovement_itemspecificst:new()
|
|
|
|
new.mat_type, new.mat_index = item.mat_type, item.mat_index
|
|
|
|
new.type = df.itemimprovement_specific_type.HANDLE
|
2022-09-12 15:51:26 -06:00
|
|
|
outputItems[productNumber - 1].improvements:insert('#', new)
|
2022-07-07 12:23:10 -06:00
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
This works well as long as you don't have multiple stacks filling up one
|
|
|
|
reagent.
|
2022-07-07 12:23:10 -06:00
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
Let's also make some code to modify the fire rate of our siege crossbow::
|
2022-07-09 10:04:20 -06:00
|
|
|
|
|
|
|
eventful.onProjItemCheckMovement[modId] = function(projectile)
|
2022-09-12 15:51:26 -06:00
|
|
|
if projectile.distance_flown > 0 then
|
|
|
|
-- don't make this adjustment more than once
|
2022-07-09 10:04:20 -06:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local firer = projectile.firer
|
|
|
|
if not firer then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local weapon = df.item.find(projectile.bow_id)
|
|
|
|
if not weapon then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-09-14 11:56:58 -06:00
|
|
|
local multiplier = tonumber(customRawTokens.getToken(
|
|
|
|
weapon.subtype,
|
|
|
|
"SIEGE_CROSSBOW_MOD_FIRE_RATE_MULTIPLIER")) or 1
|
|
|
|
firer.counters.think_counter = math.floor(
|
|
|
|
firer.counters.think_counter * multiplier)
|
2022-07-09 10:04:20 -06:00
|
|
|
end
|
|
|
|
|
2022-09-14 14:19:10 -06:00
|
|
|
.. highlight:: none
|
|
|
|
|
2022-07-15 12:43:48 -06:00
|
|
|
Now, let's see how we could make some "pegasus boots". First, let's define the
|
2022-09-12 15:51:26 -06:00
|
|
|
item in the raws::
|
2022-07-11 11:56:28 -06:00
|
|
|
|
2022-07-12 04:26:49 -06:00
|
|
|
[ITEM_SHOES:ITEM_SHOES_BOOTS_PEGASUS]
|
|
|
|
[NAME:pegasus boot:pegasus boots]
|
|
|
|
[ARMORLEVEL:1]
|
|
|
|
[UPSTEP:1]
|
|
|
|
[METAL_ARMOR_LEVELS]
|
|
|
|
[LAYER:OVER]
|
|
|
|
[COVERAGE:100]
|
|
|
|
[LAYER_SIZE:25]
|
|
|
|
[LAYER_PERMIT:15]
|
|
|
|
[MATERIAL_SIZE:2]
|
|
|
|
[METAL]
|
|
|
|
[LEATHER]
|
|
|
|
[HARD]
|
2022-12-01 16:52:00 -07:00
|
|
|
[PEGASUS_BOOTS_MOD_FOOT_MOVEMENT_TIMER_REDUCTION_PER_TICK:2] custom raw token
|
2022-09-14 11:56:58 -06:00
|
|
|
(you don't have to comment the custom token every time,
|
|
|
|
but it does clarify what it is)
|
2022-07-11 11:56:28 -06:00
|
|
|
|
2022-09-14 14:19:10 -06:00
|
|
|
.. highlight:: lua
|
|
|
|
|
2022-09-12 15:51:26 -06:00
|
|
|
Then, let's make a ``repeat-util`` callback for once a tick::
|
2022-07-11 11:56:28 -06:00
|
|
|
|
|
|
|
repeatUtil.scheduleEvery(modId, 1, "ticks", function()
|
|
|
|
|
2022-09-14 11:56:58 -06:00
|
|
|
Let's iterate over every active unit, and for every unit, iterate over their
|
2023-03-24 19:42:42 -06:00
|
|
|
worn items to calculate how much we are going to take from their on-foot
|
|
|
|
movement timers::
|
2022-07-11 11:56:28 -06:00
|
|
|
|
|
|
|
for _, unit in ipairs(df.global.world.units.active) do
|
|
|
|
local amount = 0
|
|
|
|
for _, entry in ipairs(unit.inventory) do
|
2022-09-14 11:56:58 -06:00
|
|
|
if entry.mode == df.unit_inventory_item.T_mode.Worn then
|
|
|
|
local reduction = customRawTokens.getToken(
|
|
|
|
entry.item,
|
2022-12-01 16:52:00 -07:00
|
|
|
'PEGASUS_BOOTS_MOD_FOOT_MOVEMENT_TIMER_REDUCTION_PER_TICK')
|
2022-09-14 11:56:58 -06:00
|
|
|
amount = amount + (tonumber(reduction) or 0)
|
|
|
|
end
|
2022-07-11 11:56:28 -06:00
|
|
|
end
|
2022-12-01 16:52:00 -07:00
|
|
|
-- Subtract amount from on-foot movement timers if not on ground
|
|
|
|
if not unit.flags1.on_ground then
|
2023-03-24 19:42:42 -06:00
|
|
|
dfhack.units.subtractActionTimers(unit, amount,
|
|
|
|
df.unit_action_type_group.MovementFeet)
|
2022-12-01 16:52:00 -07:00
|
|
|
end
|
2022-07-11 11:56:28 -06:00
|
|
|
end
|
2022-09-14 11:56:58 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Putting it all together
|
|
|
|
-----------------------
|
2022-09-14 00:16:54 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Ok, you're all set up! Now, let's take a look at an example
|
|
|
|
``scripts_modinstalled/example-mod.lua`` file::
|
2022-09-14 00:16:54 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
-- main file for example-mod
|
2022-09-14 00:16:54 -06:00
|
|
|
|
2023-03-24 19:47:18 -06:00
|
|
|
-- these lines indicate that the script supports the "enable"
|
|
|
|
-- API so you can start it by running "enable example-mod" and
|
|
|
|
-- stop it by running "disable example-mod"
|
2023-03-24 19:42:42 -06:00
|
|
|
--@module = true
|
|
|
|
--@enable = true
|
2022-09-14 00:16:54 -06:00
|
|
|
|
2023-03-24 19:47:18 -06:00
|
|
|
-- this is the help text that will appear in `help` and
|
|
|
|
-- `gui/launcher`. see possible tags here:
|
|
|
|
-- https://docs.dfhack.org/en/latest/docs/Tags.html
|
2023-03-24 19:42:42 -06:00
|
|
|
--[====[
|
|
|
|
example-mod
|
|
|
|
===========
|
2022-09-14 00:16:54 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Tags: fort | gameplay
|
2022-09-14 00:16:54 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Short one-sentence description ...
|
2022-09-14 11:33:27 -06:00
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Longer description ...
|
2022-09-14 12:00:26 -06:00
|
|
|
|
2022-09-14 11:33:27 -06:00
|
|
|
Usage
|
|
|
|
-----
|
2022-09-14 12:00:26 -06:00
|
|
|
|
2022-09-14 11:33:27 -06:00
|
|
|
enable example-mod
|
|
|
|
disable example-mod
|
2023-03-24 19:42:42 -06:00
|
|
|
]====]
|
|
|
|
|
2022-09-14 00:16:54 -06:00
|
|
|
local repeatUtil = require('repeat-util')
|
|
|
|
local eventful = require('plugins.eventful')
|
2022-07-15 12:28:59 -06:00
|
|
|
|
2022-09-14 11:56:58 -06:00
|
|
|
-- you can reference global values or functions declared in any of
|
|
|
|
-- your internal scripts
|
2023-03-24 19:42:42 -06:00
|
|
|
local moduleA = reqscript('internal/example-mod/module-a')
|
|
|
|
local moduleB = reqscript('internal/example-mod/module-b')
|
|
|
|
local moduleC = reqscript('internal/example-mod/module-c')
|
|
|
|
local moduleD = reqscript('internal/example-mod/module-d')
|
|
|
|
|
|
|
|
local GLOBAL_KEY = 'example-mod'
|
2022-09-14 12:00:26 -06:00
|
|
|
|
2022-09-14 11:33:27 -06:00
|
|
|
enabled = enabled or false
|
2023-03-24 19:42:42 -06:00
|
|
|
|
|
|
|
function isEnabled()
|
2023-03-27 15:05:06 -06:00
|
|
|
-- this function is for the enabled API, the script won't show up on the
|
|
|
|
-- control panel without it
|
2023-03-24 19:42:42 -06:00
|
|
|
return enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
|
|
|
|
if sc == SC_MAP_UNLOADED then
|
|
|
|
dfhack.run_command('disable', 'example-mod')
|
2023-04-07 01:48:04 -06:00
|
|
|
|
|
|
|
-- ensure our mod doesn't try to enable itself when a different
|
|
|
|
-- world is loaded where we are *not* active
|
|
|
|
dfhack.onStateChange[GLOBAL_KEY] = nil
|
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if sc ~= SC_MAP_LOADED or df.global.gamemode ~= df.game_mode.DWARF then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
dfhack.run_command('enable', 'example-mod')
|
|
|
|
end
|
2022-07-15 12:28:59 -06:00
|
|
|
|
2023-03-27 15:05:06 -06:00
|
|
|
if dfhack_flags.module then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-09-14 11:33:27 -06:00
|
|
|
if not dfhack_flags.enable then
|
2023-03-24 19:42:42 -06:00
|
|
|
print(dfhack.script_help())
|
2022-09-14 11:33:27 -06:00
|
|
|
print()
|
2022-09-14 11:56:58 -06:00
|
|
|
print(('Example mod is currently '):format(
|
|
|
|
enabled and 'enabled' or 'disabled'))
|
2022-09-14 11:33:27 -06:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if dfhack_flags.enable_state then
|
|
|
|
-- do any initialization your internal scripts might require
|
2022-07-15 12:28:59 -06:00
|
|
|
moduleA.onLoad()
|
|
|
|
moduleB.onLoad()
|
|
|
|
|
2022-09-22 13:23:56 -06:00
|
|
|
-- multiple functions in the same repeat callback
|
|
|
|
repeatUtil.scheduleEvery(modId .. ' every tick', 1, 'ticks', function()
|
|
|
|
moduleA.every1Tick()
|
|
|
|
moduleB.every1Tick()
|
|
|
|
end)
|
2022-09-22 13:31:43 -06:00
|
|
|
|
2022-09-22 13:23:56 -06:00
|
|
|
-- one function per repeat callback (you can put them in the
|
|
|
|
-- above format if you prefer)
|
2022-09-14 11:56:58 -06:00
|
|
|
repeatUtil.scheduleEvery(modId .. ' 100 frames', 1, 'frames',
|
|
|
|
moduleD.every100Frames)
|
|
|
|
|
2022-09-22 13:23:56 -06:00
|
|
|
-- multiple functions in the same eventful callback
|
2022-09-14 11:56:58 -06:00
|
|
|
eventful.onReactionComplete[modId] = function(reaction,
|
|
|
|
reaction_product, unit, input_items, input_reagents,
|
|
|
|
output_items)
|
2022-09-14 11:33:27 -06:00
|
|
|
-- pass the event's parameters to the listeners
|
2022-09-14 11:56:58 -06:00
|
|
|
moduleB.onReactionComplete(reaction, reaction_product,
|
|
|
|
unit, input_items, input_reagents, output_items)
|
|
|
|
moduleC.onReactionComplete(reaction, reaction_product,
|
|
|
|
unit, input_items, input_reagents, output_items)
|
2022-07-15 12:28:59 -06:00
|
|
|
end
|
|
|
|
|
2022-09-22 13:23:56 -06:00
|
|
|
-- one function per eventful callback (you can put them in the
|
2022-09-14 13:04:50 -06:00
|
|
|
-- above format if you prefer)
|
2022-09-14 11:33:27 -06:00
|
|
|
eventful.onProjItemCheckMovement[modId] = moduleD.onProjItemCheckMovement
|
|
|
|
eventful.onProjUnitCheckMovement[modId] = moduleD.onProjUnitCheckMovement
|
2022-07-15 12:28:59 -06:00
|
|
|
|
2022-09-14 11:33:27 -06:00
|
|
|
print('Example mod enabled')
|
|
|
|
enabled = true
|
|
|
|
else
|
|
|
|
-- call any shutdown functions your internal scripts might require
|
2022-07-15 12:28:59 -06:00
|
|
|
moduleA.onUnload()
|
|
|
|
|
2022-09-14 11:33:27 -06:00
|
|
|
repeatUtil.cancel(modId .. ' every ticks')
|
|
|
|
repeatUtil.cancel(modId .. ' 100 frames')
|
2022-07-15 12:28:59 -06:00
|
|
|
|
|
|
|
eventful.onReactionComplete[modId] = nil
|
|
|
|
eventful.onProjItemCheckMovement[modId] = nil
|
|
|
|
eventful.onProjUnitCheckMovement[modId] = nil
|
|
|
|
|
2022-09-14 11:33:27 -06:00
|
|
|
print('Example mod disabled')
|
|
|
|
enabled = false
|
2022-07-15 12:28:59 -06:00
|
|
|
end
|
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
Inside ``scripts_modinstalled/internal/example-mod/module-a.lua`` you could
|
|
|
|
have code like this::
|
2022-07-15 12:28:59 -06:00
|
|
|
|
|
|
|
--@ module = true
|
|
|
|
|
|
|
|
function onLoad() -- global variables are exported
|
2022-09-14 11:33:27 -06:00
|
|
|
-- do initialization here
|
2022-07-15 12:28:59 -06:00
|
|
|
end
|
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
-- this is a local function: local functions/variables
|
|
|
|
-- are not accessible to other scripts.
|
2022-09-14 11:56:58 -06:00
|
|
|
local function usedByOnTick(unit)
|
|
|
|
-- ...
|
2022-07-15 12:28:59 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function onTick() -- exported
|
2022-09-14 11:33:27 -06:00
|
|
|
for _,unit in ipairs(df.global.world.units.all) do
|
|
|
|
usedByOnTick(unit)
|
2022-07-15 12:28:59 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-24 19:42:42 -06:00
|
|
|
The `reqscript <reqscript>` function reloads scripts that have changed, so you
|
|
|
|
can modify your scripts while DF is running and just disable/enable your mod to
|
|
|
|
load the changes into your ongoing game!
|