From 221163d226384105b73e763ca2f6219a143e1777 Mon Sep 17 00:00:00 2001 From: kane-t Date: Mon, 4 Jul 2016 17:41:19 -0300 Subject: [PATCH 1/2] fix recursion not working --- combine-plants.lua | 53 +++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/combine-plants.lua b/combine-plants.lua index 42d40891d0..9f6626ba97 100644 --- a/combine-plants.lua +++ b/combine-plants.lua @@ -16,32 +16,38 @@ 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 container = nil if args.container then container = df.item.find(tonumber(args.container)) end -local function itemsCompatible(item0, item1) +function itemsCompatible(item0, item1) return item0:getType() == item1:getType() and item0.mat_type == item1.mat_type and item0.mat_index == item1.mat_index end -local function getPlants(items, plants, index) - for i,p in ipairs(items) do - -- Skip items currently tasked - if #p.specific_refs == 0 then - if p:getType() == 53 or p:getType() == 55 or p:getType() == 70 then - plants[index] = p - index = index + 1 - - else - local containedItems = dfhack.items.getContainedItems(p) - if #containedItems > 0 then - index = getPlants(containedItems, plants, index) +function getPlants(items, plants, index) + repeat + local nextBatch = {} + for _,v in pairs(items) do + -- Skip items currently tasked + if #v.specific_refs == 0 then + if v:getType() == 53 or v:getType() == 55 or v:getType() == 70 then + plants[index] = v + index = index + 1 + + else + local containedItems = dfhack.items.getContainedItems(v) + if #containedItems > 0 then + for _,w in pairs(containedItems) do + table.insert(nextBatch, w) + end + end end - end + end end - end + items = nextBatch + until #items == 0 return index end @@ -66,15 +72,18 @@ else else local plants = { } local plantCount = getPlants(rootItems, plants, 0) + print("found " .. plantCount .. " plants") local removedPlants = { } - + for i=0,(plantCount-2) do local currentPlant = plants[i] local itemsNeeded = max - currentPlant.stack_size if removedPlants[currentPlant.id] == nil and itemsNeeded > 0 then - for j=(i+1),(plantCount-1) do + local j = i+1 + local last = plantCount + repeat local sourcePlant = plants[j] if removedPlants[sourcePlant.id] == nil and itemsCompatible(currentPlant, sourcePlant) then @@ -88,16 +97,22 @@ else else sourcePlant.stack_size = sourcePlant.stack_size - amountToMove end +-- else print("failed") end - end + + j = j + 1 + until j == plantCount or itemsNeeded == 0 end end + local removedCount = 0 for id,removed in pairs(removedPlants) do if removed then + removedCount = removedCount + 1 local removedPlant = df.item.find(id) dfhack.items.remove(removedPlant) end end + print("removed " .. removedCount .. " plants") end end From 02537567453192ee5e965e8b92099d46a0d7bdd8 Mon Sep 17 00:00:00 2001 From: kane-t Date: Mon, 4 Jul 2016 19:45:17 -0300 Subject: [PATCH 2/2] strip trailing whitespace --- combine-plants.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/combine-plants.lua b/combine-plants.lua index 9f6626ba97..d972f52b92 100644 --- a/combine-plants.lua +++ b/combine-plants.lua @@ -16,7 +16,7 @@ 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 container = nil if args.container then container = df.item.find(tonumber(args.container)) end @@ -44,7 +44,7 @@ function getPlants(items, plants, index) end end end - end + end end items = nextBatch until #items == 0 @@ -75,7 +75,7 @@ else print("found " .. plantCount .. " plants") local removedPlants = { } - + for i=0,(plantCount-2) do local currentPlant = plants[i] local itemsNeeded = max - currentPlant.stack_size