summaryrefslogtreecommitdiff
path: root/inv_utils.lua
diff options
context:
space:
mode:
Diffstat (limited to 'inv_utils.lua')
-rw-r--r--inv_utils.lua39
1 files changed, 31 insertions, 8 deletions
diff --git a/inv_utils.lua b/inv_utils.lua
index 3a3feed..19d73e2 100644
--- a/inv_utils.lua
+++ b/inv_utils.lua
@@ -31,25 +31,48 @@ local function emptyTurtle()
end
end
-local function hasIdenticalItemsInStorage(storage, slot)
- local detail = turtle.getItemDetail(slot)
- if detail == nil then
+local function getSpecialCategoryIndex(state, item_name)
+ for idx, categ in pairs(state.categories) do
+ for _, pattern in pairs(categ) do
+ if string.match(item_name, pattern) then
+ return idx
+ end
+ end
+ end
+
+ return 0
+end
+
+local function hasIdenticalItemsInStorage(state, storage, slot)
+ local item = turtle.getItemDetail(slot)
+ if item == nil then
return false
end
- for _, item in pairs(storage.list()) do
- if item.name == detail.name then
+ local itemSpecialCat = getSpecialCategoryIndex(state, item.name)
+ local chestSpecialCat = 0
+
+ for _, other in pairs(storage.list()) do
+ if other.name == item.name then
return true
end
+
+ if itemSpecialCat > 0 and chestSpecialCat == 0 then
+ chestSpecialCat = getSpecialCategoryIndex(state, other.name)
+
+ if itemSpecialCat == chestSpecialCat then
+ return true
+ end
+ end
end
return false
end
-local function storeIdenticalItemsInStorage()
+local function storeIdenticalItemsInStorage(state)
local storage = peripheral.wrap("front")
for ii = 1, 16 do
- if hasIdenticalItemsInStorage(storage, ii) then
+ if hasIdenticalItemsInStorage(state, storage, ii) then
turtle.select(ii)
turtle.drop()
end
@@ -68,4 +91,4 @@ local function swapSlotsInStorage(storage, slotA, slotB)
end
-return { hasItemsInStorage = hasItemsInStorage, hasItemsInTurtle = hasItemsInTurtle, fillTurtle = fillTurtle, emptyTurtle = emptyTurtle, hadIdenticalItemsInStorage = hasIdenticalItemsInStorage, storeIdenticalItemsInStorage = storeIdenticalItemsInStorage, swapSlotsInStorage = swapSlotsInStorage }
+return { getSpecialCategoryIndex = getSpecialCategoryIndex, hasItemsInStorage = hasItemsInStorage, hasItemsInTurtle = hasItemsInTurtle, fillTurtle = fillTurtle, emptyTurtle = emptyTurtle, hadIdenticalItemsInStorage = hasIdenticalItemsInStorage, storeIdenticalItemsInStorage = storeIdenticalItemsInStorage, swapSlotsInStorage = swapSlotsInStorage }