From d9ded9775395b5ff617f91a0de06f11ca449fdde Mon Sep 17 00:00:00 2001 From: kane-t Date: Fri, 26 Jun 2015 15:26:51 -0300 Subject: [PATCH] Add support for specifying stockpile by id --- combine_drinks.lua | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/combine_drinks.lua b/combine_drinks.lua index 2927837..8e0e43b 100644 --- a/combine_drinks.lua +++ b/combine_drinks.lua @@ -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 @@ -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 @@ -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 @@ -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 @@ -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