Skip to content

Commit 0936c53

Browse files
authored
Merge pull request #1511 from amade-w/item-qualityfix
`item`: add new `--total-quality` option to filter items according to their total quality
2 parents a1607ef + 478e606 commit 0936c53

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Template for new versions:
2929
## New Tools
3030

3131
## New Features
32+
- `item`: new ``--total-quality`` option for use in conjunction with ``--min-quality`` or ``--max-quality`` to filter items according to their total quality
3233

3334
## Fixes
3435

docs/item.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Examples
4949
flood-fill to create a burrow covering an entire cavern layer).
5050

5151
``item melt -t weapon -m steel --max-quality 3``
52-
Designate all steel weapons whose quality is at most superior for melting.
52+
Designate all steel weapons whose core quality is at most superior for
53+
melting.
5354

5455
``item hide -t boulder --scattered``
5556
Hide all scattered boulders, i.e. those that are not in stockpiles.
@@ -121,6 +122,11 @@ Options
121122
Only include items whose quality level is at most ``integer``. Useful
122123
values are 0 (ordinary) to 5 (masterwork).
123124

125+
``--total-quality``
126+
Only applies to ``--min-quality`` and ``--max-quality`` options. Filter items
127+
according to their total quality (to include improvements) of instead of
128+
their core quality.
129+
124130
``--stockpiled``
125131
Only include items that are in stockpiles. Does not include empty bins,
126132
barrels, and wheelbarrows assigned as storage and transport for stockpiles.
@@ -201,8 +207,12 @@ the filter is described.
201207
see above).
202208

203209
* ``condition_quality(tab, lower, upper, negate)``
204-
Selects items with quality between ``lower`` and ``upper`` (Range 0-5, see
205-
above).
210+
Selects items with core quality between ``lower`` and ``upper`` (Range 0-5,
211+
see above).
212+
213+
* ``condition_overall_quality(tab, lower, upper, negate)``
214+
Selects items with total quality between ``lower`` and ``upper`` (Range 0-5,
215+
see above).
206216

207217
* ``condition_stockpiled(tab, negate)``
208218
Corresponds to ``--stockpiled``.

item.lua

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ function condition_quality(tab, lower, upper, negate)
145145
addPositiveOrNegative(tab, pred, negate)
146146
end
147147

148+
--- @param tab conditions
149+
--- @param lower number # range: 0 (standard) to 5 (masterwork)
150+
--- @param upper number # range: 0 (standard) to 5 (masterwork)
151+
--- @param negate { negate : boolean }|nil
152+
function condition_overall_quality(tab, lower, upper, negate)
153+
local pred = function(item) return lower <= item:getOverallQuality() and item:getOverallQuality() <= upper end
154+
addPositiveOrNegative(tab, pred, negate)
155+
end
156+
148157
--- @param tab conditions
149158
--- @param negate { negate : boolean }|nil
150159
function condition_forbid(tab, negate)
@@ -346,11 +355,15 @@ local options = {
346355
owned = false,
347356
nowebs = false,
348357
verbose = false,
358+
filterquality = false,
359+
totalquality = false,
349360
}
350361

351362
--- @type (fun(item:item):boolean)[]
352363
local conditions = {}
353364

365+
local minQuality, maxQuality = 0, 5
366+
354367
local function flagsFilter(args, negate)
355368
local flags = argparse.stringList(args, "flag list")
356369
for _,flag in ipairs(flags) do
@@ -375,6 +388,7 @@ local positionals = argparse.processArgsGetopt({ ... }, {
375388
{ nil, 'ignore-webs', handler = function() options.nowebs = true end },
376389
{ 'n', 'dry-run', handler = function() options.dryrun = true end },
377390
{ nil, 'by-type', handler = function() options.bytype = true end },
391+
{ nil, 'total-quality', handler = function() options.totalquality = true end },
378392
{ 'i', 'inside', hasArg = true,
379393
handler = function (name)
380394
local burrow = dfhack.burrows.findByName(name,true)
@@ -407,14 +421,18 @@ local positionals = argparse.processArgsGetopt({ ... }, {
407421
handler = function(levelst)
408422
local level = argparse.nonnegativeInt(levelst, 'max-wear')
409423
condition_wear(conditions, 0, level) end },
424+
-- Need to process total-quality argument before processing min/max-quality arguments,
425+
-- since there's no guarantee the user will call total-quality first in the command line.
410426
{ 'q', 'min-quality', hasArg = true,
411427
handler = function(levelst)
412428
local level = argparse.nonnegativeInt(levelst, 'min-quality')
413-
condition_quality(conditions, level, 5) end },
429+
minQuality = level options.filterquality = true end },
430+
-- condition_quality(conditions, level, 5) end },
414431
{ 'Q', 'max-quality', hasArg = true,
415432
handler = function(levelst)
416433
local level = argparse.nonnegativeInt(levelst, 'max-quality')
417-
condition_quality(conditions, 0, level) end },
434+
maxQuality = level options.filterquality = true end },
435+
-- condition_quality(conditions, 0, level) end },
418436
{ nil, 'stockpiled',
419437
handler = function () condition_stockpiled(conditions) end },
420438
{ nil, 'scattered',
@@ -432,6 +450,14 @@ if options.help or positionals[1] == 'help' then
432450
return
433451
end
434452

453+
if options.filterquality then
454+
if options.totalquality then
455+
condition_overall_quality(conditions, minQuality, maxQuality)
456+
else
457+
condition_quality(conditions, minQuality, maxQuality)
458+
end
459+
end
460+
435461
for i=2,#positionals do
436462
condition_description(conditions, positionals[i])
437463
end

0 commit comments

Comments
 (0)