Skip to content
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

empty-bin command options (recursive and liquid) #1244

Merged
merged 10 commits into from
Aug 1, 2024
10 changes: 9 additions & 1 deletion docs/empty-bin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ Usage

::

empty-bin
empty-bin [<options>]

foxxelias marked this conversation as resolved.
Show resolved Hide resolved
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.
foxxelias marked this conversation as resolved.
Show resolved Hide resolved
70 changes: 61 additions & 9 deletions empty-bin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
foxxelias marked this conversation as resolved.
Show resolved Hide resolved
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
},
foxxelias marked this conversation as resolved.
Show resolved Hide resolved
{
"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
Expand All @@ -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
foxxelias marked this conversation as resolved.
Show resolved Hide resolved
return
end
for _, contained in ipairs(selectedBuilding.contained_items) do
if contained.use_mode == df.building_item_role_type.TEMP then
emptyContainer(contained.item)
Expand Down