Skip to content

Commit 04664ee

Browse files
authored
Update z_instrument_all.lua
1 parent e161965 commit 04664ee

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

z_instrument_all.lua

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
-- ztest.lua
1+
-- z_instrument_all.lua
22
-- 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)
3+
-- Optionally accepts a numeric argument to specify the number of each instrument to order (default is 1)
44

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'.
75
local function collect_unique_instrument_names()
86
local success, raw_output = pcall(function()
97
return dfhack.run_command_silent("instruments")
@@ -17,54 +15,73 @@ local function collect_unique_instrument_names()
1715
local seen_names = {}
1816

1917
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)
18+
local normalized = dfhack.toSearchNormalized(line)
19+
if not normalized:match("^%s*make") and not normalized:match("^%s*forge") then
20+
local raw_name = line:gsub("%s*%b()", ""):gsub("^%s+", ""):gsub("%s+$", "")
21+
local normalized_name = dfhack.toSearchNormalized(raw_name)
22+
if raw_name ~= "" and not seen_names[normalized_name] then
23+
seen_names[normalized_name] = true
24+
table.insert(instrument_names, { name = raw_name, original_line = line })
2925
end
3026
end
3127
end
3228

3329
return instrument_names
3430
end
3531

36-
-- Submits DFHack instrument work orders using the list of instrument names and specified count.
3732
local function place_instrument_work_orders(order_count)
38-
local names = collect_unique_instrument_names()
33+
local instruments = collect_unique_instrument_names()
34+
local counts = { building = 0, handheld = 0, total = 0 }
35+
36+
for _, instrument in ipairs(instruments) do
37+
local name = instrument.name
38+
local type = "unknown"
3939

40-
for _, instrument in ipairs(names) do
41-
print("------------------------------\n")
40+
-- Determine type based on line description
41+
local line_lower = instrument.original_line:lower()
42+
if line_lower:find("building") then
43+
type = "building"
44+
elseif line_lower:find("handheld") then
45+
type = "handheld"
46+
end
47+
48+
print("------------------------------")
49+
print("Placed order for: " .. name .. " (x" .. order_count .. ") [" .. type .. "]")
50+
print("------------------------------")
4251

43-
print("Placed order for: " .. instrument .. " (x" .. order_count .. ")\n")
4452
local success, err = pcall(function()
45-
dfhack.run_command("instruments", "order", instrument, tostring(order_count))
53+
dfhack.run_command("instruments", "order", name, tostring(order_count))
4654
end)
4755

4856
if not success then
49-
dfhack.printerr("Failed to place order for '" .. instrument .. "': " .. tostring(err))
57+
dfhack.printerr("Failed to place order for '" .. name .. "': " .. tostring(err))
58+
else
59+
counts[type] = counts[type] + order_count
60+
counts.total = counts.total + order_count
5061
end
51-
print("------------------------------\n")
5262
end
63+
64+
-- Summary
65+
print("\n==== Instrument Order Summary ====")
66+
print("Total instruments ordered: " .. counts.total)
67+
print(" Handheld: " .. counts.handheld)
68+
print(" Building: " .. counts.building)
69+
print("==================================\n")
5370
end
5471

55-
-- Main entry point: processes optional count argument and triggers order placement.
72+
-- Main execution
5673
local args = {...}
57-
local quantity = 1 -- Default number of orders per instrument
74+
local quantity = 1
5875

5976
if #args == 1 then
6077
local parsed = tonumber(args[1])
6178
if parsed and parsed > 0 then
6279
quantity = math.floor(parsed)
6380
else
64-
qerror("Invalid argument. Usage: ztest [number_of_orders_per_instrument]")
81+
qerror("Invalid argument. Usage: z_instrument_all [number_of_orders_per_instrument]")
6582
end
6683
elseif #args > 1 then
67-
qerror("Too many arguments. Usage: ztest [number_of_orders_per_instrument]")
84+
qerror("Too many arguments. Usage: z_instrument_all [number_of_orders_per_instrument]")
6885
end
6986

7087
local ok, err = pcall(function()

0 commit comments

Comments
 (0)