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 TASK_TYPES = {
- createChests = "create_chests",
- storeItems = "store_items",
- getItems = "get_items",
- checkInventory = "check_inventory",
- initializeChests = "initialize_chests"
- }
- 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 getTaskList(path)
- if not fs.exists(path) then return {} end
- local file = fs.open(path, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data or {}
- end
- function saveTaskList(path, taskList)
- local file = fs.open(path, "w")
- file.write(textutils.serialize(taskList))
- file.close()
- end
- function addTaskToList(path, task)
- local tasks = getTaskList(path)
- table.insert(tasks, task)
- saveTaskList(path, tasks)
- end
- function saveTaskToDisk(contentTable)
- contentTable.taskId = taskIdCounter
- taskIdCounter = taskIdCounter + 1
- saveTaskIdCounter()
- if contentTable.taskType == TASK_TYPES.initializeChests then
- local finishedDisk, finishedPath = getDiskByLabel(DISK_NAMES.finished)
- if not finishedPath then error("Finished disk not found") end
- local fullFinishedPath = fs.combine(finishedPath, "task_list.txt")
- addTaskToList(fullFinishedPath, contentTable)
- else
- local outgoingDisk, outgoingPath = getDiskByLabel(DISK_NAMES.outgoing)
- if not outgoingPath then error("Outgoing Task disk not found") end
- local fullOutgoingPath = fs.combine(outgoingPath, "task_list.txt")
- addTaskToList(fullOutgoingPath, contentTable)
- end
- local storageDisk, storagePath = getDiskByLabel(DISK_NAMES.storage)
- if not storagePath then error("Data Storage disk not found") end
- local fullStoragePath = fs.combine(storagePath, "currentTasks.txt")
- addTaskToList(fullStoragePath, contentTable)
- table.insert(TASK_DATA.currentTasks, contentTable)
- end
- function hasOutgoingTasks()
- local _, path = getDiskByLabel(DISK_NAMES.outgoing)
- if not path then return false end
- local tasks = getTaskList(fs.combine(path, "task_list.txt"))
- return #tasks > 0
- 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 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 _, finishedPath = getDiskByLabel(DISK_NAMES.finished)
- if not finishedPath then error("Finished disk not found") end
- local finishedTasks = getTaskList(fs.combine(finishedPath, "task_list.txt"))
- for _, task in ipairs(finishedTasks) do
- if task.taskId then
- local handled = false
- if task.taskType == TASK_TYPES.createChests then
- print("Abgeschlossener Task: " .. tostring(task.taskType) .." --- Initialisiere Kistenliste")
- initializeChestList({x = task.x, y = task.y, z = task.z})
- handled = true
- elseif task.taskType == TASK_TYPES.storeItems then
- print("Abgeschlossener Task: " .. tostring(task.taskType))
- handled = true
- elseif task.taskType == TASK_TYPES.getItems then
- print("Abgeschlossener Task: " .. tostring(task.taskType))
- handled = true
- elseif task.taskType == TASK_TYPES.checkInventory then
- print("Abgeschlossener Task: " .. tostring(task.taskType))
- handled = true
- elseif task.taskType == TASK_TYPES.initializeChests then
- print("Abgeschlossener Task: " .. tostring(task.taskType) .." --- Initialisiere Kistenliste")
- initializeChestList({x = task.x, y = task.y, z = task.z})
- handled = true
- else
- print("Unbekannter Task-Typ: " .. tostring(task.taskType))
- end
- if handled then
- -- Suche Task in den aktuellen Tasks
- local matchedIndex = nil
- for index, current in ipairs(TASK_DATA.currentTasks) do
- if current.taskId == task.taskId then
- matchedIndex = index
- break
- end
- end
- if matchedIndex then
- local matchedTask = TASK_DATA.currentTasks[matchedIndex]
- table.insert(TASK_DATA.completedTasks, matchedTask)
- table.remove(TASK_DATA.currentTasks, matchedIndex)
- local disk, storagePath = getDiskByLabel(DISK_NAMES.storage)
- if not storagePath then error("Data Storage disk not found") end
- -- Aktuelle Tasks speichern
- local currentPath = fs.combine(storagePath, "currentTasks.txt")
- saveTaskList(currentPath, TASK_DATA.currentTasks)
- -- Abgeschlossene Tasks speichern
- local completedPath = fs.combine(storagePath, "completedTasks.txt")
- addTaskToList(completedPath, matchedTask)
- else
- print("Warnung: Task nicht in den aktuellen Tasks gefunden: " .. tostring(task.id))
- end
- end
- else
- print("Warnung: Task ohne ID gefunden, wird übersprungen")
- print(task.taskType)
- end
- end
- saveTaskList(fs.combine(finishedPath, "task_list.txt"), {})
- end
- function processNextTaskFromDisk(diskLabelKey)
- local disk, path = getDiskByLabel(DISK_NAMES[diskLabelKey])
- if not path then return end
- local tasks = getTaskList(fs.combine(path, "task_list.txt"))
- if #tasks == 0 then return end
- local task = table.remove(tasks, 1)
- -- Save remaining tasks back
- local fullPath = fs.combine(path, "task_list.txt")
- local file = fs.open(fullPath, "w")
- file.write(textutils.serialize(tasks))
- file.close()
- -- Save to outgoing
- saveTaskToDisk(task)
- -- Do something with the task (expand as needed)
- print("Processed " .. diskLabelKey .. " task ID: " .. tostring(taskIdCounter - 1))
- end
- function handleIncomingTask()
- processNextTaskFromDisk("incoming")
- end
- function handleUserTask()
- processNextTaskFromDisk("user")
- end
- function handleSettingsTask()
- processNextTaskFromDisk("settings")
- 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)
- if not settingsPath then error("Settings disk not found") end
- local taskList = getTaskList(fs.combine(settingsPath, "task_list.txt"))
- local hasCreateTask = false
- local createTaskIndex = nil
- local createTask = nil
- for index, task in ipairs(taskList) do
- if task.taskType == TASK_TYPES.createChests then
- hasCreateTask = true
- createTaskIndex = index
- createTask = task
- break
- end
- end
- if hasCreateTask then
- -- Task abspeichern auf Daten-Disk (als wäre er gerade ausgeführt worden)
- local disk, path = getDiskByLabel(DISK_NAMES.storage)
- if not path then error("Data Storage disk not found") end
- local currentPath = fs.combine(path, "currentTasks.txt")
- addTaskToList(currentPath, createTask)
- saveTaskToDisk(createTask)
- -- Task aus taskList entfernen und aktualisieren
- table.remove(taskList, createTaskIndex)
- saveTaskList(fs.combine(settingsPath, "task_list.txt"), taskList)
- else
- initializeChestList(SYSTEM_STATE.startCoords)
- end
- end
- -- === MAIN LOOP ===
- while true do
- checkForCompletedTasks()
- if not hasOutgoingTasks() then
- if SYSTEM_STATE.waitingForChestPlacement then
- 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
- end
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement