Advertisement
Blackhome

Main Storage Management

May 2nd, 2025 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.69 KB | Gaming | 0 0
  1. -- Storage System Manager
  2. -- By ChatGPT, structured for modular expansion
  3.  
  4. -- === CONFIG ===
  5. local DISK_NAMES = {
  6.     incoming = "Incoming Task",
  7.     user = "User Task",
  8.     outgoing = "Outgoing Task",
  9.     finished = "Finished Task",
  10.     settings = "Settings",
  11.     storage = "Data Storage",
  12.     init = "Turtle Initializing"
  13. }
  14.  
  15. local TASK_DATA = {
  16.     currentTasks = {},
  17.     emptyChests = {},
  18.     storedItems = {},
  19.     completedTasks = {},
  20.     totalChestCount = 0,
  21.     outgoingTask = nil
  22. }
  23.  
  24. local SYSTEM_STATE = {
  25.     chestInitialized = false,
  26.     waitingForChestPlacement = false,
  27.     orientation = nil,
  28.     orientationDirection = 1,
  29.     startCoords = nil,
  30.     endCoords = nil,
  31.     modulo = nil
  32. }
  33.  
  34. local taskIdCounter = 1
  35. local currentTaskSourceIndex = 1
  36. local TASK_SOURCES = {"incoming", "user", "settings"}  -- disk name keys
  37.  
  38. -- === UTILITIES ===
  39. function getDiskByLabel(label)
  40.     for _, side in ipairs(peripheral.getNames()) do
  41.         if peripheral.getType(side) == "drive" then
  42.             local disk = peripheral.wrap(side)
  43.             if disk.getDiskLabel() == label then
  44.                 return disk, disk.getMountPath()
  45.             end
  46.         end
  47.     end
  48.     return nil, nil
  49. end
  50.  
  51. function saveTableToDisk(tableData, filename)
  52.     local disk, path = getDiskByLabel(DISK_NAMES.storage)
  53.     if not path then error("Data Storage disk not found") end
  54.     local fullPath = fs.combine(path, filename)
  55.     local file = fs.open(fullPath, "w")
  56.     file.write(textutils.serialize(tableData))
  57.     file.close()
  58. end
  59.  
  60. function loadTableFromDisk(filename)
  61.     local disk, path = getDiskByLabel(DISK_NAMES.storage)
  62.     if not path then return {} end
  63.     local fullPath = fs.combine(path, filename)
  64.     if not fs.exists(fullPath) then return {} end
  65.     local file = fs.open(fullPath, "r")
  66.     local data = textutils.unserialize(file.readAll())
  67.     file.close()
  68.     return data or {}
  69. end
  70.  
  71. function loadTaskIdCounter()
  72.     local disk, path = getDiskByLabel(DISK_NAMES.storage)
  73.     if not path then return 1 end
  74.     local fullPath = fs.combine(path, "taskIdCounter.txt")
  75.     if fs.exists(fullPath) then
  76.         local file = fs.open(fullPath, "r")
  77.         local id = tonumber(file.readLine())
  78.         file.close()
  79.         return id or 1
  80.     end
  81.     return 1
  82. end
  83.  
  84. function saveTaskIdCounter()
  85.     local disk, path = getDiskByLabel(DISK_NAMES.storage)
  86.     if not path then error("Data Storage disk not found for saving taskIdCounter") end
  87.     local fullPath = fs.combine(path, "taskIdCounter.txt")
  88.     local file = fs.open(fullPath, "w")
  89.     file.writeLine(tostring(taskIdCounter))
  90.     file.close()
  91. end
  92.  
  93.  
  94. function saveTaskToDisk(taskType, contentTable)
  95.     local disk, path = getDiskByLabel(DISK_NAMES.outgoing)
  96.     if not path then error("Outgoing Task disk not found") end
  97.  
  98.     local tasksPath = fs.combine(path, "tasks")
  99.     if not fs.exists(tasksPath) then fs.makeDir(tasksPath) end
  100.  
  101.     local filename = taskType .. "_" .. tostring(taskIdCounter)
  102.     local fullPath = fs.combine(tasksPath, filename .. ".txt")
  103.     local file = fs.open(fullPath, "w")
  104.     for k, v in pairs(contentTable) do
  105.         file.writeLine(k .. "=" .. tostring(v))
  106.     end
  107.     file.writeLine("taskType=" .. taskType)
  108.     file.writeLine("taskId=" .. tostring(taskIdCounter))
  109.     file.close()
  110.  
  111.     contentTable.taskId = taskIdCounter
  112.     contentTable.taskType = taskType
  113.     table.insert(TASK_DATA.currentTasks, contentTable)
  114.     saveTableToDisk(TASK_DATA.currentTasks, "currentTasks.txt")
  115.     taskIdCounter = taskIdCounter + 1
  116.     saveTaskIdCounter()
  117. end
  118.  
  119.  
  120.  
  121. -- === INITIALIZATION ===
  122. function waitForInitialSettings()
  123.     print("Waiting for settings...")
  124.     local _, path = getDiskByLabel(DISK_NAMES.settings)
  125.     while not path do sleep(1) path = select(2, getDiskByLabel(DISK_NAMES.settings)) end
  126.  
  127.     local settingsPath = fs.combine(path, "initial_coords.txt")
  128.     while not fs.exists(settingsPath) do sleep(1) end
  129.  
  130.     local file = fs.open(settingsPath, "r")
  131.     local x1, y1, z1 = file.readLine():match("(-?%d+),(-?%d+),(-?%d+)")
  132.     local x2, y2, z2 = file.readLine():match("(-?%d+),(-?%d+),(-?%d+)")
  133.     file.close()
  134.  
  135.     SYSTEM_STATE.startCoords = {x=tonumber(x1), y=tonumber(y1), z=tonumber(z1)}
  136.     SYSTEM_STATE.endCoords = {x=tonumber(x2), y=tonumber(y2), z=tonumber(z2)}
  137.  
  138.     determineOrientation(SYSTEM_STATE.startCoords, SYSTEM_STATE.endCoords)
  139. end
  140.  
  141. function determineOrientation(startPos, endPos)
  142.     if startPos.x ~= endPos.x then
  143.         SYSTEM_STATE.orientation = "x"
  144.         SYSTEM_STATE.orientationDirection = (endPos.x - startPos.x > 0) and 1 or -1
  145.         SYSTEM_STATE.modulo = (startPos.z + 2 * SYSTEM_STATE.orientationDirection) % 8
  146.     elseif startPos.z ~= endPos.z then
  147.         SYSTEM_STATE.orientation = "z"
  148.         SYSTEM_STATE.orientationDirection = (endPos.z - startPos.z > 0) and 1 or -1
  149.         SYSTEM_STATE.modulo = (startPos.x + 2 * SYSTEM_STATE.orientationDirection) % 8
  150.     else
  151.         SYSTEM_STATE.orientation = "unknown"
  152.         SYSTEM_STATE.orientationDirection = 1
  153.         SYSTEM_STATE.modulo = 0
  154.     end
  155.  
  156.     local disk, path = getDiskByLabel(DISK_NAMES.init)
  157.     if path then
  158.         local file = fs.open(fs.combine(path, "init.txt"), "w")
  159.         file.writeLine("orientation=" .. SYSTEM_STATE.orientation)
  160.         file.writeLine("direction=" .. tostring(SYSTEM_STATE.orientationDirection))
  161.         file.writeLine("modulo=" .. tostring(SYSTEM_STATE.modulo))
  162.         file.close()
  163.     end
  164. end
  165.  
  166. function createChestInitializationTask(coords)
  167.     if #TASK_DATA.emptyChests == 0 then
  168.         SYSTEM_STATE.waitingForChestPlacement = true
  169.     end
  170.     local taskContent = {task="create_chests", x=coords.x, y=coords.y, z=coords.z}
  171.     saveTaskToDisk("create_chests", taskContent)
  172. end
  173.  
  174. function initializeChestList(coords)
  175.     print("Initializing chest list...")
  176.     for row = 0, 4 do
  177.         for col = 0, 9 do
  178.             local offsetX = SYSTEM_STATE.orientation == "x" and col * SYSTEM_STATE.orientationDirection or 0
  179.             local offsetZ = SYSTEM_STATE.orientation == "z" and col * SYSTEM_STATE.orientationDirection or 0
  180.             local chestPos = {
  181.                 x = coords.x + offsetX,
  182.                 y = coords.y + row,
  183.                 z = coords.z + offsetZ
  184.             }
  185.             TASK_DATA.totalChestCount = TASK_DATA.totalChestCount + 1
  186.             table.insert(TASK_DATA.emptyChests, {id = TASK_DATA.totalChestCount, pos = chestPos})
  187.         end
  188.     end
  189.     SYSTEM_STATE.chestInitialized = true
  190.     SYSTEM_STATE.waitingForChestPlacement = false
  191.     saveTableToDisk(TASK_DATA.emptyChests, "emptyChests.txt")
  192.     saveTableToDisk({count = TASK_DATA.totalChestCount}, "totalChestCount.txt")
  193. end
  194.  
  195. function checkForCompletedTasks()
  196.     local _, path = getDiskByLabel(DISK_NAMES.finished)
  197.     if not path then return end
  198.     local finishedPath = fs.combine(path, "tasks")
  199.     if not fs.exists(finishedPath) then return end
  200.  
  201.     for _, file in ipairs(fs.list(finishedPath)) do
  202.         local taskType, taskIdStr = file:match("^(create_chests)_(%d+)%.txt$")
  203.         local taskId = tonumber(taskIdStr)
  204.  
  205.         if taskType and taskId then
  206.             fs.delete(fs.combine(finishedPath, file))
  207.  
  208.             local matchedTask = nil
  209.             local matchedIndex = nil
  210.             for i, task in ipairs(TASK_DATA.currentTasks) do
  211.                 if task.taskType == taskType and task.taskId == taskId then
  212.                     matchedTask = task
  213.                     matchedIndex = i
  214.                     break
  215.                 end
  216.             end
  217.  
  218.             if matchedTask then
  219.                 initializeChestList({x=tonumber(matchedTask.x), y=tonumber(matchedTask.y), z=tonumber(matchedTask.z)})
  220.                 table.insert(TASK_DATA.completedTasks, matchedTask)
  221.                 table.remove(TASK_DATA.currentTasks, matchedIndex)
  222.                 saveTableToDisk(TASK_DATA.currentTasks, "currentTasks.txt")
  223.                 saveTableToDisk(TASK_DATA.completedTasks, "completedTasks.txt")
  224.             end
  225.         end
  226.     end
  227. end
  228.  
  229.  
  230. function handleIncomingTask() end
  231. function handleUserTask() end
  232. function handleSettingsTask() end
  233. function handleChestCreationTask() end
  234.  
  235. local handlerMap = {
  236.     incoming = handleIncomingTask,
  237.     user = handleUserTask,
  238.     settings = handleSettingsTask
  239. }
  240.  
  241. -- === START ===
  242. TASK_DATA.currentTasks = loadTableFromDisk("currentTasks.txt")
  243. TASK_DATA.emptyChests = loadTableFromDisk("emptyChests.txt")
  244. TASK_DATA.storedItems = loadTableFromDisk("storedItems.txt")
  245. TASK_DATA.completedTasks = loadTableFromDisk("completedTasks.txt")
  246. local chestCountData = loadTableFromDisk("totalChestCount.txt")
  247. TASK_DATA.totalChestCount = chestCountData.count or 0
  248. taskIdCounter = loadTaskIdCounter()
  249.  
  250. if #TASK_DATA.emptyChests == 0 and not SYSTEM_STATE.chestInitialized then
  251.     waitForInitialSettings()
  252.     local _, settingsPath = getDiskByLabel(DISK_NAMES.settings)
  253.     local settingsTaskFile = fs.combine(settingsPath, "task.txt")
  254.     if fs.exists(settingsTaskFile) then
  255.         createChestInitializationTask(SYSTEM_STATE.startCoords)
  256.     else
  257.         initializeChestList(SYSTEM_STATE.startCoords)
  258.     end
  259. end
  260.  
  261. -- === MAIN LOOP ===
  262. while true do
  263.     checkForCompletedTasks()
  264.  
  265.     if SYSTEM_STATE.waitingForChestPlacement then
  266.         -- actively check settings disk for new create_chests task
  267.         handlerMap.settings()
  268.     elseif #TASK_DATA.emptyChests == 0 then
  269.         SYSTEM_STATE.waitingForChestPlacement = true
  270.     else
  271.         local sourceKey = TASK_SOURCES[currentTaskSourceIndex]
  272.         handlerMap[sourceKey]()
  273.  
  274.         currentTaskSourceIndex = currentTaskSourceIndex + 1
  275.         if currentTaskSourceIndex > #TASK_SOURCES then
  276.             currentTaskSourceIndex = 1
  277.         end
  278.     end
  279.  
  280.     sleep(1)
  281. end
  282.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement