Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Storage System Manager
- -- By ChatGPT, structured for modular expansion
- -- === CONFIG ===
- local DISK_NAMES = {
- incoming = "Incoming Task",
- user = "User Task",
- outgoing = "Outgoing Task",
- finished = "Finished Task",
- settings = "Settings",
- storage = "Data Storage",
- init = "Turtle Initializing"
- }
- local TASK_DATA = {
- currentTasks = {},
- emptyChests = {},
- storedItems = {},
- completedTasks = {},
- totalChestCount = 0,
- outgoingTask = nil
- }
- local SYSTEM_STATE = {
- chestInitialized = false,
- waitingForChestPlacement = false,
- orientation = nil,
- orientationDirection = 1,
- startCoords = nil,
- endCoords = nil,
- modulo = nil
- }
- local taskIdCounter = 1
- local currentTaskSourceIndex = 1
- local TASK_SOURCES = {"incoming", "user", "settings"} -- disk name keys
- -- === UTILITIES ===
- function getDiskByLabel(label)
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "drive" then
- local disk = peripheral.wrap(side)
- if disk.getDiskLabel() == label then
- return disk, disk.getMountPath()
- end
- end
- end
- return nil, nil
- end
- function saveTableToDisk(tableData, filename)
- local disk, path = getDiskByLabel(DISK_NAMES.storage)
- if not path then error("Data Storage disk not found") end
- local fullPath = fs.combine(path, filename)
- local file = fs.open(fullPath, "w")
- file.write(textutils.serialize(tableData))
- file.close()
- end
- function loadTableFromDisk(filename)
- local disk, path = getDiskByLabel(DISK_NAMES.storage)
- if not path then return {} end
- local fullPath = fs.combine(path, filename)
- if not fs.exists(fullPath) then return {} end
- local file = fs.open(fullPath, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data or {}
- end
- function loadTaskIdCounter()
- local disk, path = getDiskByLabel(DISK_NAMES.storage)
- if not path then return 1 end
- local fullPath = fs.combine(path, "taskIdCounter.txt")
- if fs.exists(fullPath) then
- local file = fs.open(fullPath, "r")
- local id = tonumber(file.readLine())
- file.close()
- return id or 1
- end
- return 1
- end
- function saveTaskIdCounter()
- local disk, path = getDiskByLabel(DISK_NAMES.storage)
- if not path then error("Data Storage disk not found for saving taskIdCounter") end
- local fullPath = fs.combine(path, "taskIdCounter.txt")
- local file = fs.open(fullPath, "w")
- file.writeLine(tostring(taskIdCounter))
- file.close()
- end
- function saveTaskToDisk(taskType, contentTable)
- local disk, path = getDiskByLabel(DISK_NAMES.outgoing)
- if not path then error("Outgoing Task disk not found") end
- local tasksPath = fs.combine(path, "tasks")
- if not fs.exists(tasksPath) then fs.makeDir(tasksPath) end
- local filename = taskType .. "_" .. tostring(taskIdCounter)
- local fullPath = fs.combine(tasksPath, filename .. ".txt")
- local file = fs.open(fullPath, "w")
- for k, v in pairs(contentTable) do
- file.writeLine(k .. "=" .. tostring(v))
- end
- file.writeLine("taskType=" .. taskType)
- file.writeLine("taskId=" .. tostring(taskIdCounter))
- file.close()
- contentTable.taskId = taskIdCounter
- contentTable.taskType = taskType
- table.insert(TASK_DATA.currentTasks, contentTable)
- saveTableToDisk(TASK_DATA.currentTasks, "currentTasks.txt")
- taskIdCounter = taskIdCounter + 1
- saveTaskIdCounter()
- end
- -- === INITIALIZATION ===
- function waitForInitialSettings()
- print("Waiting for settings...")
- local _, path = getDiskByLabel(DISK_NAMES.settings)
- while not path do sleep(1) path = select(2, getDiskByLabel(DISK_NAMES.settings)) end
- local settingsPath = fs.combine(path, "initial_coords.txt")
- while not fs.exists(settingsPath) do sleep(1) end
- local file = fs.open(settingsPath, "r")
- local x1, y1, z1 = file.readLine():match("(-?%d+),(-?%d+),(-?%d+)")
- local x2, y2, z2 = file.readLine():match("(-?%d+),(-?%d+),(-?%d+)")
- file.close()
- SYSTEM_STATE.startCoords = {x=tonumber(x1), y=tonumber(y1), z=tonumber(z1)}
- SYSTEM_STATE.endCoords = {x=tonumber(x2), y=tonumber(y2), z=tonumber(z2)}
- determineOrientation(SYSTEM_STATE.startCoords, SYSTEM_STATE.endCoords)
- end
- function determineOrientation(startPos, endPos)
- if startPos.x ~= endPos.x then
- SYSTEM_STATE.orientation = "x"
- SYSTEM_STATE.orientationDirection = (endPos.x - startPos.x > 0) and 1 or -1
- SYSTEM_STATE.modulo = (startPos.z + 2 * SYSTEM_STATE.orientationDirection) % 8
- elseif startPos.z ~= endPos.z then
- SYSTEM_STATE.orientation = "z"
- SYSTEM_STATE.orientationDirection = (endPos.z - startPos.z > 0) and 1 or -1
- SYSTEM_STATE.modulo = (startPos.x + 2 * SYSTEM_STATE.orientationDirection) % 8
- else
- SYSTEM_STATE.orientation = "unknown"
- SYSTEM_STATE.orientationDirection = 1
- SYSTEM_STATE.modulo = 0
- end
- local disk, path = getDiskByLabel(DISK_NAMES.init)
- if path then
- local file = fs.open(fs.combine(path, "init.txt"), "w")
- file.writeLine("orientation=" .. SYSTEM_STATE.orientation)
- file.writeLine("direction=" .. tostring(SYSTEM_STATE.orientationDirection))
- file.writeLine("modulo=" .. tostring(SYSTEM_STATE.modulo))
- file.close()
- end
- end
- function createChestInitializationTask(coords)
- if #TASK_DATA.emptyChests == 0 then
- SYSTEM_STATE.waitingForChestPlacement = true
- end
- local taskContent = {task="create_chests", x=coords.x, y=coords.y, z=coords.z}
- saveTaskToDisk("create_chests", taskContent)
- end
- function initializeChestList(coords)
- print("Initializing chest list...")
- for row = 0, 4 do
- for col = 0, 9 do
- local offsetX = SYSTEM_STATE.orientation == "x" and col * SYSTEM_STATE.orientationDirection or 0
- local offsetZ = SYSTEM_STATE.orientation == "z" and col * SYSTEM_STATE.orientationDirection or 0
- local chestPos = {
- x = coords.x + offsetX,
- y = coords.y + row,
- z = coords.z + offsetZ
- }
- TASK_DATA.totalChestCount = TASK_DATA.totalChestCount + 1
- table.insert(TASK_DATA.emptyChests, {id = TASK_DATA.totalChestCount, pos = chestPos})
- end
- end
- SYSTEM_STATE.chestInitialized = true
- SYSTEM_STATE.waitingForChestPlacement = false
- saveTableToDisk(TASK_DATA.emptyChests, "emptyChests.txt")
- saveTableToDisk({count = TASK_DATA.totalChestCount}, "totalChestCount.txt")
- end
- function checkForCompletedTasks()
- local _, path = getDiskByLabel(DISK_NAMES.finished)
- if not path then return end
- local finishedPath = fs.combine(path, "tasks")
- if not fs.exists(finishedPath) then return end
- for _, file in ipairs(fs.list(finishedPath)) do
- local taskType, taskIdStr = file:match("^(create_chests)_(%d+)%.txt$")
- local taskId = tonumber(taskIdStr)
- if taskType and taskId then
- fs.delete(fs.combine(finishedPath, file))
- local matchedTask = nil
- local matchedIndex = nil
- for i, task in ipairs(TASK_DATA.currentTasks) do
- if task.taskType == taskType and task.taskId == taskId then
- matchedTask = task
- matchedIndex = i
- break
- end
- end
- if matchedTask then
- initializeChestList({x=tonumber(matchedTask.x), y=tonumber(matchedTask.y), z=tonumber(matchedTask.z)})
- table.insert(TASK_DATA.completedTasks, matchedTask)
- table.remove(TASK_DATA.currentTasks, matchedIndex)
- saveTableToDisk(TASK_DATA.currentTasks, "currentTasks.txt")
- saveTableToDisk(TASK_DATA.completedTasks, "completedTasks.txt")
- end
- end
- end
- end
- function handleIncomingTask() end
- function handleUserTask() end
- function handleSettingsTask() end
- function handleChestCreationTask() end
- local handlerMap = {
- incoming = handleIncomingTask,
- user = handleUserTask,
- settings = handleSettingsTask
- }
- -- === START ===
- TASK_DATA.currentTasks = loadTableFromDisk("currentTasks.txt")
- TASK_DATA.emptyChests = loadTableFromDisk("emptyChests.txt")
- TASK_DATA.storedItems = loadTableFromDisk("storedItems.txt")
- TASK_DATA.completedTasks = loadTableFromDisk("completedTasks.txt")
- local chestCountData = loadTableFromDisk("totalChestCount.txt")
- TASK_DATA.totalChestCount = chestCountData.count or 0
- taskIdCounter = loadTaskIdCounter()
- if #TASK_DATA.emptyChests == 0 and not SYSTEM_STATE.chestInitialized then
- waitForInitialSettings()
- local _, settingsPath = getDiskByLabel(DISK_NAMES.settings)
- local settingsTaskFile = fs.combine(settingsPath, "task.txt")
- if fs.exists(settingsTaskFile) then
- createChestInitializationTask(SYSTEM_STATE.startCoords)
- else
- initializeChestList(SYSTEM_STATE.startCoords)
- end
- end
- -- === MAIN LOOP ===
- while true do
- checkForCompletedTasks()
- if SYSTEM_STATE.waitingForChestPlacement then
- -- actively check settings disk for new create_chests task
- handlerMap.settings()
- elseif #TASK_DATA.emptyChests == 0 then
- SYSTEM_STATE.waitingForChestPlacement = true
- else
- local sourceKey = TASK_SOURCES[currentTaskSourceIndex]
- handlerMap[sourceKey]()
- currentTaskSourceIndex = currentTaskSourceIndex + 1
- if currentTaskSourceIndex > #TASK_SOURCES then
- currentTaskSourceIndex = 1
- end
- end
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement