Skip to content

Commit 1806f0d

Browse files
committed
Add z_instrument_all script and documentation
1 parent e50474e commit 1806f0d

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

docs/z_instrument_all.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
z_instrument_all.lua
2+
=====================
3+
4+
**Automatically queue work orders for all discovered instruments in Dwarf Fortress using DFHack.**
5+
6+
Features
7+
--------
8+
9+
- Detects all valid, craftable instruments via `instruments` command
10+
- Normalizes and filters instrument names for compatibility
11+
- Automatically places a work order for each instrument without having to manually input order and name
12+
13+
Usage
14+
-----
15+
16+
Run with a number to place **that many** orders per instrument (defaults to 1 if omitted):
17+
18+
::
19+
20+
z_instrument_all 5
21+
22+
This example queues 5 of each discovered instrument.
23+
24+
Acknowledgments
25+
---------------
26+
27+
This script utilizes the DFHack `instruments` command to retrieve and process instrument definitions.

z_instrument_all.lua

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
-- ztest.lua
2+
-- Automatically places work orders for all discovered instruments using: 'instruments order <name> <count>'
3+
-- Accepts an optional numeric argument to control how many of each to order (default is 1)
4+
5+
-- Retrieves and returns a list of unique instrument names from the DFHack 'instruments' command.
6+
-- Strips parenthetical info and filters out crafting steps like 'make' or 'forge'.
7+
local function collect_unique_instrument_names()
8+
local success, raw_output = pcall(function()
9+
return dfhack.run_command_silent("instruments")
10+
end)
11+
12+
if not success or not raw_output then
13+
qerror("Failed to run 'instruments': " .. tostring(raw_output))
14+
end
15+
16+
local instrument_names = {}
17+
local seen_names = {}
18+
19+
for line in raw_output:gmatch("[^\r\n]+") do
20+
local normalized_line = dfhack.toSearchNormalized(line)
21+
22+
-- Skip crafting steps
23+
if not normalized_line:match("^%s*make") and not normalized_line:match("^%s*forge") then
24+
-- Remove content in parentheses and trim whitespace
25+
local name = normalized_line:gsub("%s*%b()", ""):gsub("^%s+", ""):gsub("%s+$", "")
26+
if name ~= "" and not seen_names[name] then
27+
seen_names[name] = true
28+
table.insert(instrument_names, name)
29+
end
30+
end
31+
end
32+
33+
return instrument_names
34+
end
35+
36+
-- Submits DFHack instrument work orders using the list of instrument names and specified count.
37+
local function place_instrument_work_orders(order_count)
38+
local names = collect_unique_instrument_names()
39+
40+
for _, instrument in ipairs(names) do
41+
print("------------------------------\n")
42+
43+
print("Placed order for: " .. instrument .. " (x" .. order_count .. ")\n")
44+
local success, err = pcall(function()
45+
dfhack.run_command("instruments", "order", instrument, tostring(order_count))
46+
end)
47+
48+
if not success then
49+
dfhack.printerr("Failed to place order for '" .. instrument .. "': " .. tostring(err))
50+
end
51+
print("------------------------------\n")
52+
end
53+
end
54+
55+
-- Main entry point: processes optional count argument and triggers order placement.
56+
local args = {...}
57+
local quantity = 1 -- Default number of orders per instrument
58+
59+
if #args == 1 then
60+
local parsed = tonumber(args[1])
61+
if parsed and parsed > 0 then
62+
quantity = math.floor(parsed)
63+
else
64+
qerror("Invalid argument. Usage: ztest [number_of_orders_per_instrument]")
65+
end
66+
elseif #args > 1 then
67+
qerror("Too many arguments. Usage: ztest [number_of_orders_per_instrument]")
68+
end
69+
70+
local ok, err = pcall(function()
71+
place_instrument_work_orders(quantity)
72+
end)
73+
if not ok then
74+
qerror("Script failed: " .. tostring(err))
75+
end

0 commit comments

Comments
 (0)