Skip to content

Commit 7574e08

Browse files
authored
Merge pull request #3 from kane-t/combine-plants-fix
Fix combine-plants recursion bug
2 parents b0c5d0e + 0253756 commit 7574e08

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

combine-plants.lua

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,34 @@ if args.stockpile then stockpile = df.building.find(tonumber(args.stockpile)) en
2020
local container = nil
2121
if args.container then container = df.item.find(tonumber(args.container)) end
2222

23-
local function itemsCompatible(item0, item1)
23+
function itemsCompatible(item0, item1)
2424
return item0:getType() == item1:getType()
2525
and item0.mat_type == item1.mat_type
2626
and item0.mat_index == item1.mat_index
2727
end
2828

29-
local function getPlants(items, plants, index)
30-
for i,p in ipairs(items) do
31-
-- Skip items currently tasked
32-
if #p.specific_refs == 0 then
33-
if p:getType() == 53 or p:getType() == 55 or p:getType() == 70 then
34-
plants[index] = p
35-
index = index + 1
36-
37-
else
38-
local containedItems = dfhack.items.getContainedItems(p)
39-
if #containedItems > 0 then
40-
index = getPlants(containedItems, plants, index)
29+
function getPlants(items, plants, index)
30+
repeat
31+
local nextBatch = {}
32+
for _,v in pairs(items) do
33+
-- Skip items currently tasked
34+
if #v.specific_refs == 0 then
35+
if v:getType() == 53 or v:getType() == 55 or v:getType() == 70 then
36+
plants[index] = v
37+
index = index + 1
38+
39+
else
40+
local containedItems = dfhack.items.getContainedItems(v)
41+
if #containedItems > 0 then
42+
for _,w in pairs(containedItems) do
43+
table.insert(nextBatch, w)
44+
end
45+
end
4146
end
4247
end
4348
end
44-
end
49+
items = nextBatch
50+
until #items == 0
4551

4652
return index
4753
end
@@ -66,6 +72,7 @@ else
6672
else
6773
local plants = { }
6874
local plantCount = getPlants(rootItems, plants, 0)
75+
print("found " .. plantCount .. " plants")
6976

7077
local removedPlants = { }
7178

@@ -74,7 +81,9 @@ else
7481
local itemsNeeded = max - currentPlant.stack_size
7582

7683
if removedPlants[currentPlant.id] == nil and itemsNeeded > 0 then
77-
for j=(i+1),(plantCount-1) do
84+
local j = i+1
85+
local last = plantCount
86+
repeat
7887
local sourcePlant = plants[j]
7988

8089
if removedPlants[sourcePlant.id] == nil and itemsCompatible(currentPlant, sourcePlant) then
@@ -88,16 +97,22 @@ else
8897
else
8998
sourcePlant.stack_size = sourcePlant.stack_size - amountToMove
9099
end
100+
-- else print("failed")
91101
end
92-
end
102+
103+
j = j + 1
104+
until j == plantCount or itemsNeeded == 0
93105
end
94106
end
95107

108+
local removedCount = 0
96109
for id,removed in pairs(removedPlants) do
97110
if removed then
111+
removedCount = removedCount + 1
98112
local removedPlant = df.item.find(id)
99113
dfhack.items.remove(removedPlant)
100114
end
101115
end
116+
print("removed " .. removedCount .. " plants")
102117
end
103118
end

0 commit comments

Comments
 (0)