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

Combine stacks of coins, make combine work in adventure mode #1227

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions combine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local opts, args = {
help = false,
all = nil,
here = nil,
item = nil,
dry_run = false,
types = nil,
quiet = false,
Expand Down Expand Up @@ -44,6 +45,7 @@ local valid_types_map = {
plant = {[df.item_type.PLANT] ={type_id=df.item_type.PLANT, max_stack_qty=5, max_mat_amt=1},
[df.item_type.PLANT_GROWTH]={type_id=df.item_type.PLANT_GROWTH, max_stack_qty=5, max_mat_amt=1}},
powder= {[df.item_type.POWDER_MISC] ={type_id=df.item_type.POWDER_MISC, max_stack_qty=10, max_mat_amt=1}},
coin = {[df.item_type.COIN] ={type_id=df.item_type.COIN, max_stack_qty=500, max_mat_amt=1}},
-- seed = {[df.item_type.SEEDS] ={type_id=df.item_type.SEEDS, max_stack_qty=1, max_mat_amt=1}},
}

Expand Down Expand Up @@ -122,7 +124,11 @@ local function comp_item_add_item(stockpile, stack_type, comp_item, item, contai
new_item.before_size = item.stack_size

new_item.stockpile_id = stockpile.id
new_item.stockpile_name = stockpile.name
if df.item:is_instance(stockpile) then
new_item.stockpile_name = dfhack.items.getReadableDescription(stockpile)
else
new_item.stockpile_name = stockpile.name
end

-- material amount info
new_item.before_mat_amt = {}
Expand Down Expand Up @@ -437,7 +443,7 @@ local function stacks_add_items(stockpile, stacks, items, container, ind)

-- item type in list of included types?
if stack_type and not item:isSand() and not item:isPlaster() and isValidPart(item) then
if not isRestrictedItem(item) and item.stack_size <= stack_type.max_stack_qty then
if not isRestrictedItem(item) and (item.stack_size <= stack_type.max_stack_qty) then

stacks_add_item(stockpile, stacks, stack_type, item, container)

Expand Down Expand Up @@ -505,10 +511,16 @@ local function populate_stacks(stacks, stockpiles, types)
-- iterate across the stockpiles, get the list of items and call the add function to check/add as needed
log(4, ('stockpiles'))
for _, stockpile in ipairs(stockpiles) do

local items = dfhack.buildings.getStockpileContents(stockpile)
log(4, (' stockpile:%30s <%6d> pos:(%3d,%3d,%3d) #items:%5d'):format(
stockpile.name, stockpile.id, stockpile.centerx, stockpile.centery, stockpile.z, #items))
local items = nil
if df.building_stockpilest:is_instance(stockpile) then
items = dfhack.buildings.getStockpileContents(stockpile)
log(4, (' stockpile:%30s <%6d> pos:(%3d,%3d,%3d) #items:%5d'):format(
stockpile.name, stockpile.id, stockpile.centerx, stockpile.centery, stockpile.z, #items))
elseif df.item:is_instance(stockpile) then
items = dfhack.items.getContainedItems(stockpile)
log(4, (' container:%30s <%6d> pos:(%3d,%3d,%3d) #items:%5d'):format(
dfhack.items.getReadableDescription(stockpile), stockpile.id, stockpile.pos.x, stockpile.pos.y, stockpile.pos.z, #items))
end

if #items > 0 then
stacks_add_items(stockpile, stacks, items)
Expand Down Expand Up @@ -689,6 +701,10 @@ local function merge_stacks(stacks)
if item.after_size == 0 then
log(4, ' removing')
dfhack.items.remove(item.item)
if dfhack.world.isAdventureMode() and df.global.game.main_interface.adventure.inventory.open then
-- refresh the inventory to prevent stale item references
df.global.game.main_interface.adventure.inventory.open = false
Comment on lines +704 to +706
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user should be notified at least in command console output if their inventory was closed automatically to prevent bugs. Running a tool that does not ostensibly claim it will manipulate UI and having it, surprisingly, manipulate UI, can be a distressing and disorienting experience for users.

end

-- some items left in stack
elseif not typesThatUseMaterial[df.item_type[stack_type.type_id]] and item.before_size ~= item.after_size then
Expand Down Expand Up @@ -792,6 +808,8 @@ local function parse_commandline(opts, args)
opts.all=get_stockpile_all()
elseif positionals[1] == 'here' then
opts.here=get_stockpile_here()
elseif positionals[1] == 'item' then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs docs

opts.item={dfhack.gui.getSelectedItem(true)}
else
opts.help = true
end
Expand All @@ -805,7 +823,7 @@ end
-- main program starts here
local function main()

if df.global.gamemode ~= df.game_mode.DWARF or not dfhack.isMapLoaded() then
if not dfhack.isMapLoaded() then
qerror('combine needs a loaded fortress map to work')
Comment on lines +826 to 827
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using combine from the main menu now gives a disingenuous error message, because it works in adventure mode. Perhaps change this to combine can only be used in-game.

end

Expand All @@ -818,7 +836,7 @@ local function main()

local stacks = stacks_new()

populate_stacks(stacks, opts.all or opts.here, opts.types)
populate_stacks(stacks, opts.all or opts.here or opts.item, opts.types)

preview_stacks(stacks)

Expand Down