|
| 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