From b773d05177755a6d4ff55fad3437422714a05a0d Mon Sep 17 00:00:00 2001 From: Elias Date: Mon, 29 Jul 2024 16:51:12 +0300 Subject: [PATCH 1/8] empty-bin recursive usage and liquid include flag --- docs/empty-bin.rst | 10 ++++++- empty-bin.lua | 72 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/docs/empty-bin.rst b/docs/empty-bin.rst index 1cd6b9bb29..1e0a8d124d 100644 --- a/docs/empty-bin.rst +++ b/docs/empty-bin.rst @@ -22,4 +22,12 @@ Usage :: - empty-bin + empty-bin [] + +Options +-------------- + +``-r|--recursive`` + Whether to apply the command recursively. +``-l|--liquids`` + Include liquids (DRINK and LIQUID_MISC) in the items list to be moved to the ground. \ No newline at end of file diff --git a/empty-bin.lua b/empty-bin.lua index 3f10225e65..62280ad26c 100644 --- a/empty-bin.lua +++ b/empty-bin.lua @@ -3,28 +3,78 @@ -- https://gist.github.com/stonetoad/11129025 -- http://dwarffortresswiki.org/index.php/DF2014_Talk:Bin -local function moveItem(item, to_pos) - print(' ' .. dfhack.items.getReadableDescription(item)) - dfhack.items.moveToGround(item, to_pos) -end +local argparse = require("argparse") + +local options, args = { + help = false, + recursive = false, + liquids = false + }, + {...} local function emptyContainer(container) local items = dfhack.items.getContainedItems(container) - if #items > 0 then - print('Emptying ' .. dfhack.items.getReadableDescription(container)) + print("Emptying " .. dfhack.items.getReadableDescription(container)) local pos = xyz2pos(dfhack.items.getPosition(container)) for _, item in ipairs(items) do - moveItem(item, pos) + local skip_liquid = + item:getType() == df.item_type.LIQUID_MISC or + item:getType() == df.item_type.DRINK and not options.liquids + if skip_liquid then + print( + " " .. + dfhack.items.getReadableDescription(item) .. + " was skipped because the liquid flag was not provided" + ) + else + print(" " .. dfhack.items.getReadableDescription(item)) + dfhack.items.moveToGround(item, pos) + if options.recursive then + emptyContainer(item) + end + end end end end -local viewsheets = df.global.game.main_interface.view_sheets +argparse.processArgsGetopt( + args, + { + { + "h", + "help", + handler = function() + options.help = true + end + }, + { + "r", + "recursive", + handler = function() + options.recursive = true + end + }, + { + "l", + "liquids", + handler = function() + options.liquids = true + end + } + } +) + +if options.help then + print(dfhack.script_help()) + return +end +local viewsheets = df.global.game.main_interface.view_sheets local stockpile = dfhack.gui.getSelectedStockpile(true) local selectedItem = dfhack.gui.getSelectedItem(true) local selectedBuilding = dfhack.gui.getSelectedBuilding(true) + if stockpile then local contents = dfhack.buildings.getStockpileContents(stockpile) for _, container in ipairs(contents) do @@ -33,7 +83,9 @@ if stockpile then elseif selectedItem then emptyContainer(selectedItem) elseif selectedBuilding then - if not df.building_actual:is_instance(selectedBuilding) then return end + if not selectedBuilding:isActual() then + return + end for _, contained in ipairs(selectedBuilding.contained_items) do if contained.use_mode == df.building_item_role_type.TEMP then emptyContainer(contained.item) @@ -46,4 +98,4 @@ elseif viewsheets.open then end else qerror("Please select a container, building, stockpile, or tile with a list of items.") -end +end \ No newline at end of file From c0bade1a3015e8c802616a6fa79e2961dc6cef3d Mon Sep 17 00:00:00 2001 From: Elias Date: Mon, 29 Jul 2024 19:14:40 +0300 Subject: [PATCH 2/8] append missing additional empty line in end of files --- docs/empty-bin.rst | 2 +- empty-bin.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/empty-bin.rst b/docs/empty-bin.rst index 1e0a8d124d..3f33bff683 100644 --- a/docs/empty-bin.rst +++ b/docs/empty-bin.rst @@ -30,4 +30,4 @@ Options ``-r|--recursive`` Whether to apply the command recursively. ``-l|--liquids`` - Include liquids (DRINK and LIQUID_MISC) in the items list to be moved to the ground. \ No newline at end of file + Include liquids (DRINK and LIQUID_MISC) in the items list to be moved to the ground. diff --git a/empty-bin.lua b/empty-bin.lua index 62280ad26c..c2636e31be 100644 --- a/empty-bin.lua +++ b/empty-bin.lua @@ -98,4 +98,4 @@ elseif viewsheets.open then end else qerror("Please select a container, building, stockpile, or tile with a list of items.") -end \ No newline at end of file +end From cbc384296a46829bb83f144efb53b4a76ecfbd36 Mon Sep 17 00:00:00 2001 From: Elias Date: Tue, 30 Jul 2024 00:38:23 +0300 Subject: [PATCH 3/8] single quotes for strings --- empty-bin.lua | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/empty-bin.lua b/empty-bin.lua index c2636e31be..853563b8c3 100644 --- a/empty-bin.lua +++ b/empty-bin.lua @@ -1,9 +1,9 @@ -- Empty a bin onto the floor --- Based on "emptybin" by StoneToad +-- Based on 'emptybin' by StoneToad -- https://gist.github.com/stonetoad/11129025 -- http://dwarffortresswiki.org/index.php/DF2014_Talk:Bin -local argparse = require("argparse") +local argparse = require('argparse') local options, args = { help = false, @@ -15,7 +15,7 @@ local options, args = { local function emptyContainer(container) local items = dfhack.items.getContainedItems(container) if #items > 0 then - print("Emptying " .. dfhack.items.getReadableDescription(container)) + print('Emptying ' .. dfhack.items.getReadableDescription(container)) local pos = xyz2pos(dfhack.items.getPosition(container)) for _, item in ipairs(items) do local skip_liquid = @@ -23,12 +23,12 @@ local function emptyContainer(container) item:getType() == df.item_type.DRINK and not options.liquids if skip_liquid then print( - " " .. + ' ' .. dfhack.items.getReadableDescription(item) .. - " was skipped because the liquid flag was not provided" + ' was skipped because the liquid flag was not provided' ) else - print(" " .. dfhack.items.getReadableDescription(item)) + print(' ' .. dfhack.items.getReadableDescription(item)) dfhack.items.moveToGround(item, pos) if options.recursive then emptyContainer(item) @@ -42,22 +42,22 @@ argparse.processArgsGetopt( args, { { - "h", - "help", + 'h', + 'help', handler = function() options.help = true end }, { - "r", - "recursive", + 'r', + 'recursive', handler = function() options.recursive = true end }, { - "l", - "liquids", + 'l', + 'liquids', handler = function() options.liquids = true end @@ -97,5 +97,5 @@ elseif viewsheets.open then emptyContainer(item) end else - qerror("Please select a container, building, stockpile, or tile with a list of items.") + qerror('Please select a container, building, stockpile, or tile with a list of items.') end From a5a62b7b55823664b5e9376351c0e343f2587d9e Mon Sep 17 00:00:00 2001 From: Elias Date: Tue, 30 Jul 2024 00:42:38 +0300 Subject: [PATCH 4/8] Removed the warning about emptying liquid onto the floor --- docs/empty-bin.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/docs/empty-bin.rst b/docs/empty-bin.rst index 3f33bff683..3be4a2b674 100644 --- a/docs/empty-bin.rst +++ b/docs/empty-bin.rst @@ -9,9 +9,6 @@ This tool can quickly empty the contents of the selected container (bin, barrel, pot, wineskin, quiver, etc.) onto the floor, allowing you to access individual items that might otherwise be hard to get to. -Note that if there are liquids in the container, they will empty onto the floor -and become unusable. - If you instead select a stockpile or building, running `empty-bin` will empty *all* containers in the stockpile or building. Likewise, if you select a tile that has many items and the UI is showing the list of items, all containers on From a4990367a816f60d2c55414c4af9758378c9a8a9 Mon Sep 17 00:00:00 2001 From: Elias Date: Tue, 30 Jul 2024 00:46:36 +0300 Subject: [PATCH 5/8] fixed command options formatting and descriptions --- docs/empty-bin.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/empty-bin.rst b/docs/empty-bin.rst index 3be4a2b674..1f21c5249b 100644 --- a/docs/empty-bin.rst +++ b/docs/empty-bin.rst @@ -24,7 +24,7 @@ Usage Options -------------- -``-r|--recursive`` - Whether to apply the command recursively. -``-l|--liquids`` - Include liquids (DRINK and LIQUID_MISC) in the items list to be moved to the ground. +``-r``, ``--recursive`` + Recursively empty containers. +``-l``, ``--liquids`` + Move contained liquids (DRINK and LIQUID_MISC) to the floor, making them unusable. From 88024936773feb52d70eafc547cf25bc81d13375 Mon Sep 17 00:00:00 2001 From: Elias Date: Tue, 30 Jul 2024 00:56:29 +0300 Subject: [PATCH 6/8] short option handlers and badly autoformatted code rows now is oneliners --- empty-bin.lua | 41 +++++++---------------------------------- 1 file changed, 7 insertions(+), 34 deletions(-) diff --git a/empty-bin.lua b/empty-bin.lua index 853563b8c3..9647333a69 100644 --- a/empty-bin.lua +++ b/empty-bin.lua @@ -18,15 +18,9 @@ local function emptyContainer(container) print('Emptying ' .. dfhack.items.getReadableDescription(container)) local pos = xyz2pos(dfhack.items.getPosition(container)) for _, item in ipairs(items) do - local skip_liquid = - item:getType() == df.item_type.LIQUID_MISC or - item:getType() == df.item_type.DRINK and not options.liquids + local skip_liquid = item:getType() == df.item_type.LIQUID_MISC or item:getType() == df.item_type.DRINK and not options.liquids if skip_liquid then - print( - ' ' .. - dfhack.items.getReadableDescription(item) .. - ' was skipped because the liquid flag was not provided' - ) + print(' ' .. dfhack.items.getReadableDescription(item) .. ' was skipped because the liquid flag was not provided') else print(' ' .. dfhack.items.getReadableDescription(item)) dfhack.items.moveToGround(item, pos) @@ -38,32 +32,11 @@ local function emptyContainer(container) end end -argparse.processArgsGetopt( - args, - { - { - 'h', - 'help', - handler = function() - options.help = true - end - }, - { - 'r', - 'recursive', - handler = function() - options.recursive = true - end - }, - { - 'l', - 'liquids', - handler = function() - options.liquids = true - end - } - } -) +argparse.processArgsGetopt(args,{ + { 'h', 'help', handler = function() options.help = true end }, + { 'r', 'recursive', handler = function() options.recursive = true end }, + { 'l', 'liquids', handler = function() options.liquids = true end } + }) if options.help then print(dfhack.script_help()) From a01304388145ec1ad2d219e113f9398d6991e108 Mon Sep 17 00:00:00 2001 From: Elias Date: Wed, 31 Jul 2024 20:59:21 +0300 Subject: [PATCH 7/8] changed script message about missing --liquids option --- empty-bin.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/empty-bin.lua b/empty-bin.lua index 9647333a69..f94a140816 100644 --- a/empty-bin.lua +++ b/empty-bin.lua @@ -20,7 +20,7 @@ local function emptyContainer(container) for _, item in ipairs(items) do local skip_liquid = item:getType() == df.item_type.LIQUID_MISC or item:getType() == df.item_type.DRINK and not options.liquids if skip_liquid then - print(' ' .. dfhack.items.getReadableDescription(item) .. ' was skipped because the liquid flag was not provided') + print(' ' .. dfhack.items.getReadableDescription(item) .. ' was skipped because the --liquids flag was not provided') else print(' ' .. dfhack.items.getReadableDescription(item)) dfhack.items.moveToGround(item, pos) From 4617ab166619b8ce3608221d5943b9a191af0885 Mon Sep 17 00:00:00 2001 From: Elias Date: Wed, 31 Jul 2024 21:00:19 +0300 Subject: [PATCH 8/8] added example section --- docs/empty-bin.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/empty-bin.rst b/docs/empty-bin.rst index 1f21c5249b..1d45eb81aa 100644 --- a/docs/empty-bin.rst +++ b/docs/empty-bin.rst @@ -21,6 +21,18 @@ Usage empty-bin [] +Examples +-------- + +``empty-bin`` + Empty the contents of selected containers or all containers in the selected stockpile or building, except containers with liquids, onto the floor. + +``empty-bin --liquids`` + Empty the contents of selected containers or all containers in the selected stockpile or building, including containers with liquids, onto the floor. + +``empty-bin --recursive --liquids`` + Empty the contents of selected containers or all containers in the selected stockpile or building, including containers with liquids and containers contents that are containers, such as a bags of seeds or filled waterskins, onto the floor. + Options --------------