Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Commit

Permalink
Add support for specifying stockpile by id
Browse files Browse the repository at this point in the history
  • Loading branch information
kane-t committed Jun 26, 2015
1 parent 5a936ff commit d9ded97
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions combine_drinks.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
-- Merge stacks of drinks in the selected stockpile
local utils = require 'utils'

validArgs = validArgs or utils.invert({ 'max' })
validArgs = validArgs or utils.invert({ 'max', 'stockpile' })
local args = utils.processArgs({...}, validArgs)

local max = 30;
if args.max then max = tonumber(args.max) end

local stockpile = nil
if args.stockpile then stockpile = df.building.find(tonumber(args.stockpile)) end

local function itemsCompatible(item0, item1)
return item0:getType() == item1:getType()
and item0.mat_type == item1.mat_type
Expand All @@ -24,6 +26,7 @@ local function getDrinks(items, drinks, index)
foundDrink = d

else
--print(d.id)
local containedItems = dfhack.items.getContainedItems(d)
-- Drink containers only contain one item
if #containedItems == 1 then
Expand All @@ -45,7 +48,7 @@ local function getDrinks(items, drinks, index)
return index
end

local building = dfhack.gui.getSelectedBuilding(true)
local building = stockpile or dfhack.gui.getSelectedBuilding(true)
if building ~= nil and building:getType() ~= 29 then building = nil end

if building == nil then
Expand All @@ -55,30 +58,36 @@ else
local rootItems = dfhack.buildings.getStockpileContents(building);

if #rootItems == 0 then
error("Stockpile is empty")
error("Select a non-empty stockpile")

else
local drinks = { }
local drinkCount = getDrinks(rootItems, drinks, 0)

for i,p in ipairs(drinks) do
print(i .. ': ' .. dfhack.items.getDescription(p, p:getType()))
end

local removedDrinks = { }

for i=0,(drinkCount-2) do
local currentDrink = drinks[i]
local itemsNeeded = max - currentDrink.stack_size

--print('processing ' .. dfhack.items.getDescription(currentDrink, currentDrink:getType()) .. ' needs ' .. itemsNeeded)
if removedDrinks[currentDrink.id] == nil and itemsNeeded > 0 then
for j=(i+1),(drinkCount-1) do
local sourceDrink = drinks[j]
--print('\ttrying ' .. dfhack.items.getDescription(sourceDrink, sourceDrink:getType()))

if removedDrinks[sourceDrink.id] == nil and itemsCompatible(currentDrink, sourceDrink) then
local amountToMove = math.min(itemsNeeded, sourceDrink.stack_size)
--print('\tmoving ' .. amountToMove)
itemsNeeded = itemsNeeded - amountToMove
currentDrink.stack_size = currentDrink.stack_size + amountToMove

if sourceDrink.stack_size == amountToMove then
--print('\tadding remove id ' .. sourceDrink.id)
removedDrinks[sourceDrink.id] = true
sourceDrink.stack_size = 1
else
sourceDrink.stack_size = sourceDrink.stack_size - amountToMove
end
Expand All @@ -90,8 +99,13 @@ else
for id,removed in pairs(removedDrinks) do
if removed then
local removedDrink = df.item.find(id)
--print('remove id=' .. id .. ' drink=' .. dfhack.items.getDescription(removedDrink, removedDrink:getType()))
dfhack.items.remove(removedDrink)
end
end
end
--elseif item:getType() == 68 then
--handleDrink(item)
--else
-- error("Select a drink or a drink.")
end

0 comments on commit d9ded97

Please sign in to comment.