summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBotond Hende <nettingman@gmail.com>2025-05-03 00:32:30 +0200
committerBotond Hende <nettingman@gmail.com>2025-05-03 00:32:30 +0200
commit50a2465d4274aa82480198e32715883005411068 (patch)
tree01c90d645f3726ab1ae760319d760906f19edd13
parent05bde0603cda5d91712615d0e2706b0e2347aa40 (diff)
advanced sortingHEADmaster
-rw-r--r--inv_utils.lua39
-rw-r--r--sorter.lua20
2 files changed, 50 insertions, 9 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 }
diff --git a/sorter.lua b/sorter.lua
index c5ac286..f2ff4f2 100644
--- a/sorter.lua
+++ b/sorter.lua
@@ -4,6 +4,7 @@ local str_utils = require("modules.str_utils")
local nav = require("modules.nav")
local DATA_PATH = "/data/sorter_data.txt"
+local CAT_PATH = "/data/sorter_cat.txt"
local function runDiagnostics()
@@ -23,6 +24,8 @@ local function runDiagnostics()
print(string.format("Data path exists: %s", dataExists and "yes" or "no"))
+ print(string.format("Category path exists: %s", fs.exists(CAT_PATH) and "yes" or "no"))
+
local hasItemsInUnsorted = inv_utils.hasItemsInStorage(peripheral.wrap("top"))
if not hasItemsInUnsorted then
@@ -88,6 +91,21 @@ local function getState()
table.remove(state.coords, 1)
end
+ state.categories = { }
+ if fs.exists(CAT_PATH) then
+ local file2 = fs.open(CAT_PATH, "r")
+ local cat_content = file2.readAll()
+ file2.close()
+
+ for _, cat in pairs(str_utils.split(cat_content, "---")) do
+ local categ = {}
+ for _, entry in pairs(str_utils.split(cat)) do
+ table.insert(categ, entry)
+ end
+ table.insert(state.categories, categ)
+ end
+ end
+
return state
end
@@ -112,7 +130,7 @@ local function runErrands()
for _, next_pos in pairs(state.coords) do
move(current_pos, next_pos, state.safe_y)
current_pos = next_pos
- inv_utils.storeIdenticalItemsInStorage()
+ inv_utils.storeIdenticalItemsInStorage(state)
if not inv_utils.hasItemsInTurtle() then
break