-
Notifications
You must be signed in to change notification settings - Fork 209
Add zprospectanalyzer #1465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add zprospectanalyzer #1465
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
zprospectanalyzer | ||
================= | ||
|
||
**Goal**: Filter and print stones that are worth **3 points**. | ||
|
||
Features | ||
-------- | ||
|
||
- **Output Parsing**: Automatically runs ``prospect all`` and parses the text output. | ||
- **Section Filtering**: Optionally filters materials by specific sections like ores or gems. | ||
- **Presets**: Running ``zprospectanalyzer`` **without parameters** will only run the preset of **3-point stones**. | ||
- **Missing Materials Reporting**: Displays ``<not found>`` next to any requested material that doesn't appear in the output. | ||
|
||
Usage | ||
----- | ||
|
||
.. code-block:: bash | ||
|
||
zprospectanalyzer [material1] [material2] ... | ||
|
||
Example | ||
------- | ||
|
||
.. code-block:: bash | ||
|
||
zprospectanalyzer claystone granite ruby tetrahedrite |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
-- scripts/zprospectanalyzer.lua | ||
|
||
local zprospectanalyzer = {} | ||
|
||
--- Scans the world using the `prospect` command and returns a table of materials. | ||
-- @return table data[section][material_key] = { count=number, minElev=number?, maxElev=number? } | ||
function zprospectanalyzer.scanProspect(section) | ||
local filterSection = section and section:lower():gsub("%s+","_") or "all" | ||
local showMap = { | ||
base_materials = "base", | ||
liquids = "liquids", | ||
layer_materials = "layers", | ||
features = "features", | ||
ores = "ores", | ||
gems = "gems", | ||
other_vein_stone = "veins", | ||
shrubs = "shrubs", | ||
wood_in_trees = "trees", | ||
} | ||
local cmd = { "prospect", "all" } | ||
if filterSection ~= "all" and showMap[filterSection] then | ||
table.insert(cmd, "--show") | ||
table.insert(cmd, showMap[filterSection]) | ||
end | ||
local output, status = dfhack.run_command_silent(table.unpack(cmd)) | ||
if status ~= CR_OK then | ||
error(("prospect failed (code %d): %s"):format(status, tostring(output))) | ||
end | ||
local data = {} | ||
local current | ||
-- split output into lines without interpreting escape sequences | ||
for line in output:gmatch("([^\n]+)") do | ||
local header = line:match("^%s*([%a%s_]+)%s*:%s*$") | ||
if header then | ||
current = header:lower():gsub("%s+","_") | ||
data[current] = {} | ||
elseif current then | ||
local name, count, elev = line:match( | ||
"^%s*([%u_]+)%s*:%s*(%d+)%s+Elev:?%s*([-%d%.]+)" | ||
) | ||
if not name then | ||
name, count = line:match("^%s*([%u_]+)%s*:%s*(%d+)") | ||
end | ||
if name and count then | ||
local key = name:lower() | ||
local entry = { count = tonumber(count) } | ||
if elev then | ||
local minE, maxE = elev:match("([-%d]+)%.%.([-%d]+)") | ||
entry.minElev = tonumber(minE) | ||
entry.maxElev = tonumber(maxE) | ||
end | ||
data[current][key] = entry | ||
end | ||
end | ||
end | ||
return data | ||
end | ||
|
||
--- Sorts an array of material entries by min elevation then quantity. | ||
-- @return sorted list | ||
function zprospectanalyzer.sortMaterials(list) | ||
table.sort(list, function(a, b) | ||
local am = a.entry.minElev or 0 | ||
local bm = b.entry.minElev or 0 | ||
if am ~= bm then | ||
return am > bm | ||
end | ||
return a.entry.count > b.entry.count | ||
end) | ||
return list | ||
end | ||
|
||
--- Prints material entries in aligned columns with a header. | ||
function zprospectanalyzer.printMaterials(list) | ||
-- Header line | ||
print(string.format(" %-15s %8s %10s %10s", "Material", "Quantity", "Min Elev", "Max Elev")) | ||
for _, item in ipairs(list) do | ||
print(string.format( | ||
" %-15s %8d %10s %10s", | ||
item.key:upper(), | ||
item.entry.count, | ||
item.entry.minElev or "?", | ||
item.entry.maxElev or "?" | ||
)) | ||
end | ||
end | ||
|
||
--- Main entry point for CLI. | ||
-- Supports "blocks" preset or custom section/material arguments. | ||
-- Not-found entries are printed last. | ||
-- blocks preset includes stones that are worth 3 pts. | ||
function zprospectanalyzer.main(...) | ||
local args = { ... } | ||
if #args == 0 then args = { "blocks" } end | ||
local presets = { | ||
blocks = { | ||
"Alabaster", "Alunite", "Andesite", "Anhydrite", "Basalt", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be hardcoding - it should fetch the actual stone IDs from the current world's inorganic material definitions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realized I didn’t put the initial purpose of the program. The goal is to output stone that is worth 3 points in the embark shop menu. I’ll review the program today. I can clean it up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no guarantee even that any of those stones exist in any given world, or if they do that they're worth 3 points in the embark menu. For that you need to pull the material definitions out of the world DFHack tools should, to the extent practical, work even with radically modified raws There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yesterday I tried looking for the skill point value of items and failed. I don’t like the way I’m doing it now but can’t seem to find way to achieve a preset of blocks worth 3 points. I’ll try searching again today. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should (a) make the target value or values a script argument (what exactly is magical about 3 points?) i'm restating this to add it here as a review blocker |
||
"Bauxite", "Bismuthinite", "Borax", "Brimstone", "Chert", | ||
"Chromite", "Cinnabar", "Claystone", "Cobaltite", "Conglomerate", | ||
"Cryolite", "Dacite", "Diorite", "Gabbro", "Gneiss", | ||
"Granite", "Graphite", "Gypsum", "Hornblende", "Ilmenite", | ||
"Jet", "Kaolinite", "Kimberlite", "Marcasite", "Mica", | ||
"Microcline", "Olivine", "Orpiment", "Orthoclase", "Periclase", | ||
"Petrified_wood", "Phyllite", "Pitchblende", "Puddingstone", | ||
"Pyrolusite", "Quartzite", "Realgar", "Rhyolite", "Rock_salt", | ||
"Rutile", "Saltpeter", "Sandstone", "Satinspar", "Schist", | ||
"Selenite", "Serpentine", "Shale", "Siltstone", "Slate", | ||
"Stibnite", "Sylvite", "Talc", | ||
} | ||
} | ||
local first = args[1]:lower():gsub("%s+","_") | ||
local materials = {} | ||
local section | ||
if presets[first] then | ||
materials = presets[first] | ||
else | ||
local validSections = { | ||
base_materials=true, liquids=true, layer_materials=true, | ||
features=true, ores=true, gems=true, | ||
other_vein_stone=true, shrubs=true, wood_in_trees=true | ||
} | ||
local startIndex = 1 | ||
if validSections[first] then section = first; startIndex = 2 end | ||
for i = startIndex, #args do materials[#materials+1] = args[i] end | ||
end | ||
local data = zprospectanalyzer.scanProspect(section) | ||
local foundEntries = {} | ||
local missingEntries = {} | ||
for _, mat in ipairs(materials) do | ||
local key = mat:lower():gsub("%s+","_") | ||
local entryFound = false | ||
if section then | ||
local e = (data[section] or {})[key] | ||
if e then | ||
foundEntries[#foundEntries+1] = { key = key, entry = e } | ||
entryFound = true | ||
end | ||
else | ||
for _, items in pairs(data) do | ||
local e = items[key] | ||
if e then | ||
foundEntries[#foundEntries+1] = { key = key, entry = e } | ||
entryFound = true | ||
end | ||
end | ||
end | ||
if not entryFound then | ||
missingEntries[#missingEntries+1] = mat | ||
end | ||
end | ||
zprospectanalyzer.printMaterials(zprospectanalyzer.sortMaterials(foundEntries)) | ||
for _, mat in ipairs(missingEntries) do | ||
print(string.format( | ||
" %-15s : %7s <not found>", | ||
mat:upper(), "-" | ||
)) | ||
end | ||
end | ||
|
||
-- Execute main when run as script | ||
zprospectanalyzer.main(...) | ||
|
||
return zprospectanalyzer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of running the
prospect
command and grubbing through its output, you should add (or requested added) an API to theprospect
plugin that will return the requested datascraping is always a fragile approach and creates undocumented dependencies - if someone later changes the
prospect
tool's output, even in the slightest way, this tool will breakThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hadn't thought about prospect updating.
I haven't been able to figure out where the block point values are defined, and I haven’t had any luck locating them in gui/gm-editor either. What’s special about 3-point blocks? It’s helpful to know on the embark screen which blocks are worth 3 points, so I (the user) can make informed decisions and aim for a consistent material when making rock blocks. I usually bring around 180 stones to turn into blocks, so it's frustrating to start a large project with, say, rock salt, only to discover that chert is more abundant and at a higher tighter elevation.
I do want to switch to using the actual stone IDs in my presets, but without first uncovering where the point values are defined, I can't justify moving away from using the names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope you're able to test what I'm referring to. If you run prospect, it takes a while to make an informed decision about which stones to bring. But if you run my program, you can significantly cut down the time needed to decide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how to search for the point values of items anymore. I feel I've exhausted all avenues through gui/gm-editor and using lua.