Blackhome

StorageDataManager

Jan 26th, 2026 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 40.15 KB | Gaming | 0 0
  1.     -- pastebin get 6fYKgMYH StorageDataManager
  2.  
  3.     FM = require("FM")
  4.  
  5.     local itemInfo = {
  6.         name = "minecraft:cobblestone",
  7.         dName = "Cobblestone",
  8.         stackSize = 64,
  9.         number = 100
  10.     }
  11.  
  12.     local DataStorageSettings = {
  13.         maxSize_Items = 90000,
  14.         maxSize_Chests = 90000
  15.     }
  16.  
  17.     local lastChestIndex = 0
  18.  
  19.     local function getDefaultDisplayName(itemName)
  20.         if not itemName then return nil end
  21.  
  22.         -- Extrahiere den Teil nach dem Doppelpunkt (z. B. "stone_axe")
  23.         local id = itemName:match("^[^:]+:(.+)$")
  24.         if not id then return nil end
  25.  
  26.         -- Ersetze Unterstriche durch Leerzeichen und kapitalisiere jedes Wort
  27.         local words = {}
  28.         for word in string.gmatch(id, "[^_]+") do
  29.             table.insert(words, word:sub(1,1):upper() .. word:sub(2))
  30.         end
  31.  
  32.         return table.concat(words, " ")
  33.     end
  34.  
  35.     local function getChestListPath(listNum)
  36.         local diskList = SDM.getStorageDisks("chest")
  37.         local diskPath = diskList[listNum].path
  38.         local filename = "Chestlist_" .. tostring(listNum)
  39.         return  fs.combine(diskPath, filename)
  40.     end
  41.     local function getItemListPath(listNum)
  42.         local diskList = SDM.getStorageDisks("item")
  43.         local diskPath = diskList[listNum].path
  44.         local filename = "Itemlist_" .. tostring(listNum)
  45.         return  fs.combine(diskPath, filename)
  46.     end
  47.  
  48.     local function getNextEmptyChest()
  49.         local disks = SDM.getStorageDisks("chest")
  50.         for i, disk in ipairs(disks) do
  51.             local path = getChestListPath(i)
  52.             local chestList = FM.loadTable(path)
  53.             if chestList and chestList["nextUnused"] <= chestList[#chestList].id then
  54.                 local cId = chestList["nextUnused"]
  55.                 SDM.changeChestStatus(cId, true)
  56.                 return {id = cId, num = 0, storingNum = 0, extractingNum = 0}
  57.             end
  58.         end
  59.         print("Error: Can't find unused chest.")
  60.         return nil
  61.     end
  62.  
  63.     local function getLastChestId()
  64.         local disks = SDM.getStorageDisks("chest")
  65.  
  66.         if #disks == 0 then
  67.             _, disks = SDM.addStorageDisk("chest")
  68.             return 0
  69.         end
  70.  
  71.         local cnt = #disks
  72.         while cnt > 0 do
  73.             local path = getChestListPath(cnt)
  74.             local chestList = FM.loadTable(path)
  75.  
  76.             if #chestList > 0 then return chestList[#chestList].id end
  77.             cnt = cnt - 1
  78.         end
  79.         return 0
  80.     end
  81.  
  82.     local function getLastChestList()
  83.         local disks = SDM.getStorageDisks("chest")
  84.         if #disks == 0 then
  85.             _, disks = SDM.addStorageDisk("chest")
  86.             return {}, 1
  87.         end
  88.  
  89.         local cnt = #disks
  90.         while cnt > 0 do
  91.             local path = getChestListPath(cnt)
  92.             local chestList = FM.loadTable(path)
  93.  
  94.             if #chestList > 0 then
  95.                 return chestList, cnt
  96.             end
  97.             cnt = cnt - 1
  98.         end
  99.         return {}, 1
  100.  
  101.     end
  102.  
  103.  
  104.     local function createChest(id, coords)
  105.         local chest = {
  106.             id = id,
  107.             coords = coords,
  108.             used = false
  109.         }
  110.         return chest
  111.     end
  112.  
  113.     local function createItem(name, dName, stackSize)
  114.         if type(dName) == "number" then
  115.             stackSize = dName
  116.             dName = getDefaultDisplayName(name)
  117.         elseif not stackSize then
  118.             stackSize = 64
  119.         end
  120.         local chest1 = getNextEmptyChest()
  121.  
  122.         local NewItem = {
  123.             name = name,
  124.             dName = dName,
  125.             stackSize = stackSize,
  126.             storage = {
  127.                         chest1,
  128.                     }
  129.         }
  130.         return NewItem
  131.     end
  132.  
  133.     local function isNameOfItem(name, item)
  134.         if item.name == name or item.dName == name then return true end
  135.         if item.name == "minecraft:" .. name then return true end
  136.         if item.name == "minecraft:" .. name:lower():gsub(" ", "_") then return true end
  137.         if item.name == name:lower():gsub(" ", "_") then return true end
  138.         if item.dName:lower():gsub(" ", "_") == name:lower():gsub(" ", "_") then return true end
  139.         return false
  140.     end
  141.  
  142.    
  143.  
  144.     SDM = {}
  145.     -----------------------------------------------------------------------------------
  146.     -- getStorageDisks  - Returns list of all Storage disks of requested type
  147.     -- addStorageDisk   - Adds a new Storage Disk for requested type
  148.     -- getDisk          - Returns any disk of requested type
  149.     -- addDisk          - Adds any disk of requested type(limited to 1 for most types)
  150.     -----------------------------------------------------------------------------------
  151.     function SDM.getStorageDisks(dataStorageType)
  152.         local STORAGE_LABEL_BASE = "Item_Storage"
  153.         if dataStorageType == "Chest" or dataStorageType == "chest" then
  154.             STORAGE_LABEL_BASE = "Chest_Storage"
  155.         end
  156.         local disks = {}
  157.         local index = 1
  158.         while true do
  159.             local label = STORAGE_LABEL_BASE .. (index > 1 and (" " .. tostring(index)) or "")
  160.             local _, path = FM.getDiskByLabel(label)
  161.             if not path then break end
  162.             table.insert(disks, {label = label, path = path})
  163.             index = index + 1
  164.         end
  165.         return disks
  166.     end
  167.     function SDM.addStorageDisk(dataStorageType)
  168.         local STORAGE_LABEL_BASE = "Item_Storage"
  169.         if dataStorageType == "Chest" or dataStorageType == "chest" then
  170.             STORAGE_LABEL_BASE = "Chest_Storage"
  171.         end
  172.         local disks = SDM.getStorageDisks(dataStorageType)
  173.         local index = #disks + 1
  174.  
  175.         local sDisk, sPath = FM.getDiskByLabel()
  176.         if not sDisk then
  177.             return nil, disks
  178.         end
  179.         local label = STORAGE_LABEL_BASE .. (index > 1 and (" " .. tostring(index)) or "")
  180.  
  181.         sDisk.setDiskLabel(label)
  182.         table.insert(disks, {label = label, path = sPath})
  183.         return sDisk, disks
  184.     end
  185.     function SDM.getDisk(diskType)
  186.         local isStorageDisk = diskType == "Chest" or diskType == "chest" or  diskType == "Item" or diskType == "item"
  187.         if isStorageDisk then
  188.             return SDM.getStorageDisks(diskType)
  189.         end
  190.  
  191.         local DISK_LABEL = ""
  192.  
  193.         local isSettings = diskType == "Settings" or diskType == "settings" or diskType == "settings Task" or diskType == "Settings task"
  194.         local isUser = diskType == "user" or diskType == "User" or diskType == "user Task" or diskType == "User task"
  195.         local isIncoming = diskType == "incoming" or diskType == "Incoming" or diskType == "incoming Task" or diskType == "Incoming task"
  196.         local isOutgoing = diskType == "outgoing" or diskType == "Outgoing" or diskType == "outgoing Task" or diskType == "Outgoing Task"
  197.         local isFinished = diskType == "finished" or diskType == "Finished" or diskType == "finished Task" or diskType == "Finished Task"
  198.         local isTurtInit = diskType == "init" or diskType == "Init" or diskType == "turtle init" or diskType == "Turtle Init"
  199.         local isTaskProt = diskType == "protocoll" or diskType == "Protocoll" or diskType == "task protocoll" or diskType == "Task Protocoll"
  200.  
  201.         if isSettings then
  202.             DISK_LABEL = "Settings"
  203.         elseif isUser then
  204.             DISK_LABEL = "User Task"
  205.         elseif isIncoming then
  206.             DISK_LABEL = "Incoming Task"
  207.         elseif isOutgoing then
  208.             DISK_LABEL = "Outgoing Task"
  209.         elseif isFinished then
  210.             DISK_LABEL = "Finished Task"
  211.         elseif isTurtInit then
  212.             DISK_LABEL = "Turtle Initializing"
  213.         elseif isTaskProt then
  214.             DISK_LABEL = "Task Protocoll"
  215.         else
  216.             return nil
  217.         end
  218.         local sDisk, sPath = FM.getDiskByLabel(DISK_LABEL)
  219.  
  220.         if sDisk then
  221.             return sDisk, {label = DISK_LABEL, path = sPath}
  222.         end
  223.  
  224.         return nil
  225.     end
  226.     function SDM.addDisk(diskType)
  227.         local isStorageDisk = diskType == "Chest" or diskType == "chest" or  diskType == "Item" or diskType == "item"
  228.         if isStorageDisk then
  229.             return SDM.addStorageDisk(diskType)
  230.         end
  231.  
  232.         if SDM.getDisk(diskType) then
  233.             print("Warning: Disk already exists. Use getDisk() to get it")
  234.             return nil
  235.         end
  236.  
  237.         local DISK_LABEL = ""
  238.  
  239.         local isSettings = diskType == "Settings" or diskType == "settings"
  240.         local isUser = diskType == "user" or diskType == "User" or diskType == "User Task" or diskType == "user task"
  241.         local isIncoming = diskType == "incoming" or diskType == "Incoming" or diskType == "Incoming Task" or diskType == "incoming task"
  242.         local isTaskProt = diskType == "protocoll" or diskType == "Protocoll" or diskType == "task protocoll" or diskType == "Task Protocoll"
  243.  
  244.         if isSettings then
  245.             DISK_LABEL = "Settings"
  246.         elseif isUser then
  247.             DISK_LABEL = "User Task"
  248.         elseif isIncoming then
  249.             DISK_LABEL = "Incoming Task"
  250.         elseif isTaskProt then
  251.             DISK_LABEL = "Task Protocoll"
  252.         else
  253.             return nil
  254.         end
  255.         local sDisk, sPath = FM.getDiskByLabel()
  256.         sDisk.setDiskLabel(DISK_LABEL)
  257.  
  258.         return sDisk, {label = DISK_LABEL, path = sPath}
  259.  
  260.     end
  261.  
  262.     -----------------------------------------------------------------------------------
  263.     -- findItem             - Finds an item in the savefiles by name or itemInfo
  264.     -- findItemByChestId    - Finds an item with given chestId
  265.     -----------------------------------------------------------------------------------
  266.     function SDM.findItem(param)
  267.         local name = param
  268.         if type(param) == "table" then
  269.             name = param.name
  270.         end
  271.  
  272.         local storageDisks = SDM.getStorageDisks("item")
  273.  
  274.         for i, diskInfo in ipairs(storageDisks) do
  275.             local path = getItemListPath(i)
  276.             local itemList = FM.loadTable(path)
  277.  
  278.             for _, item in ipairs(itemList) do
  279.                 if isNameOfItem(name, item) then
  280.                     return item
  281.                 end
  282.             end
  283.         end
  284.         return nil
  285.     end
  286.     function SDM.findItemByChestId(chestId)
  287.         local storageDisks = SDM.getStorageDisks("item")
  288.  
  289.         for i, diskInfo in ipairs(storageDisks) do
  290.             local path = getItemListPath(i)
  291.             local itemList = FM.loadTable(path)
  292.  
  293.             for _, item in ipairs(itemList) do
  294.                 local itemStorage = item.storage
  295.  
  296.                 for _, chest in ipairs(item.storage) do
  297.                     if chest.id == chestId then
  298.                         return item
  299.                     end
  300.                 end
  301.             end
  302.         end
  303.         return nil
  304.     end
  305.  
  306.     -----------------------------------------------------------------------------------
  307.     -- saveItem         - Item replaces its old data in the savefiles
  308.     --                    or is added at the end
  309.     -- saveChestList    - Saves chestlist on correct disk
  310.     -----------------------------------------------------------------------------------
  311.     function SDM.saveItem(item)
  312.         local storageDisks = SDM.getStorageDisks("item")
  313.  
  314.         local bItemReplaced = false
  315.  
  316.         local bufferItems = {}
  317.         for i, diskInfo in ipairs(storageDisks) do
  318.                 local diskPath = diskInfo.path
  319.  
  320.                 local filename = "Itemlist_" .. tostring(i)
  321.  
  322.                 local path = fs.combine(diskPath, filename)
  323.  
  324.                 local itemList = FM.loadTable(path)
  325.  
  326.             if not bItemReplaced then
  327.                 for j, oldItem in ipairs(itemList) do
  328.                     if isNameOfItem(item.name, oldItem) then
  329.                         itemList[j] = item
  330.                         bItemReplaced = true
  331.                     end
  332.                 end
  333.             end
  334.             if bItemReplaced then
  335.                 itemList = FM.combineTables(bufferItems, itemList)
  336.                 bufferItems = {}
  337.                 while FM.getTableSize(itemList) > DataStorageSettings.maxSize_Items do
  338.                     table.insert(bufferItems, 1, table.remove(itemList))
  339.                 end
  340.                 FM.saveTable(itemList, path)
  341.                 if #bufferItems == 0 then return end
  342.             end
  343.         end
  344.        
  345.         if not bItemReplaced  and #storageDisks > 0 then
  346.             local diskInfo = storageDisks[#storageDisks]
  347.             local filename = "Itemlist_" .. tostring(#storageDisks)
  348.             local path = fs.combine(diskInfo.path, filename)
  349.             local itemList = FM.loadTable(path)
  350.  
  351.             table.insert(itemList, item)
  352.             if not (FM.getTableSize(itemList) > DataStorageSettings.maxSize_Items) then
  353.                 FM.saveTable(itemList, path)
  354.                 return
  355.             end
  356.             table.insert(bufferItems, item)
  357.         end
  358.         if #storageDisks == 0 then
  359.             table.insert(bufferItems, item)
  360.         end
  361.  
  362.         _, storageDisks = SDM.addStorageDisk("item")
  363.  
  364.         local diskInfo = storageDisks[#storageDisks]
  365.         local filename = "Itemlist_" .. tostring(#storageDisks)
  366.         local path = fs.combine(diskInfo.path, filename)
  367.  
  368.         FM.saveTable(bufferItems, path)
  369.         return
  370.     end
  371.     function SDM.saveChestList(chestList)
  372.         local listNum = chestList["num"]
  373.         local path = getChestListPath(listNum)
  374.  
  375.         FM.saveTable(chestList, path)
  376.     end
  377.     -- SDM.createChestBlock({x=0, y=0, z=0}, {x=1, y=0, z=0})
  378.     -----------------------------------------------------------------------------------
  379.     -- createChestBlock     - Registers a 5 high and 10 Block long block of chests
  380.     -- createSingleChest    - Registers a single chest at given coordinates
  381.     -----------------------------------------------------------------------------------
  382.     function SDM.createChestBlock(startCoords, dirCoords)
  383.         local chestList, listNum = getLastChestList()
  384.         local diskList = SDM.getStorageDisks("chest")
  385.  
  386.         local lastId = getLastChestId()
  387.         if #chestList == 0 then
  388.             chestList["num"] = listNum
  389.             chestList["numChests"] = #chestList
  390.             chestList["nextUnused"] = lastId + 1
  391.         end
  392.  
  393.         local nextId = lastId + 1
  394.         for dy = 0, 4 do
  395.             for s = 0, 9 do
  396.                 local newCoords = {
  397.                                     x = startCoords.x +  dirCoords.x * s,
  398.                                     y = startCoords.y +  dy,
  399.                                     z = startCoords.z +  dirCoords.z * s
  400.                                 }
  401.                 local newChest = createChest(nextId, newCoords)
  402.                 local newListSize = #textutils.serialize(chestList, {compact = true}) + #textutils.serialize(newChest, {compact = true}) + 1
  403.                 if newListSize < DataStorageSettings.maxSize_Chests then
  404.                     table.insert(chestList, newChest)
  405.                 else
  406.                     chestList["numChests"] = #chestList
  407.                     SDM.saveChestList(chestList)
  408.  
  409.                     if listNum == #diskList then
  410.                         _, diskList = SDM.addStorageDisk("chest")
  411.                     end
  412.                     listNum = listNum + 1
  413.                    
  414.                     local path = getChestListPath(listNum)
  415.                     chestList = FM.loadTable(path)
  416.  
  417.                     chestList["num"] = listNum
  418.                     chestList["numChests"] = #chestList
  419.                     chestList["nextUnused"] = nextId
  420.  
  421.                     table.insert(chestList, newChest)
  422.                 end
  423.                 nextId = nextId + 1
  424.             end
  425.         end
  426.         chestList["numChests"] = #chestList
  427.         SDM.saveChestList(chestList)
  428.     end
  429.     function SDM.createSingleChest(coords)
  430.         local chestList, listNum = getLastChestList()
  431.         local diskList = SDM.getStorageDisks("chest")
  432.  
  433.         local lastId = getLastChestId()
  434.         if #chestList == 0 then
  435.             chestList["num"] = listNum
  436.             chestList["numChests"] = #chestList
  437.             chestList["nextUnused"] = lastId + 1
  438.         end
  439.  
  440.         local nextId = lastId + 1
  441.         local newChest = createChest(nextId, coords)
  442.         local newListSize = #textutils.serialize(chestList, {compact = true}) + #textutils.serialize(newChest, {compact = true}) + 1
  443.         if newListSize < DataStorageSettings.maxSize_Chests then
  444.             table.insert(chestList, newChest)
  445.         else
  446.             if listNum == #diskList then
  447.                 _, diskList = SDM.addStorageDisk("chest")
  448.             end
  449.             listNum = listNum + 1
  450.             local path = getChestListPath(listNum)
  451.             chestList = FM.loadTable(path)
  452.  
  453.             chestList["num"] = listNum
  454.             chestList["numChests"] = #chestList
  455.             chestList["nextUnused"] = nextId
  456.  
  457.             table.insert(chestList, newChest)
  458.         end
  459.        
  460.         chestList["numChests"] = #chestList
  461.         SDM.saveChestList(chestList)
  462.  
  463.         return newChest
  464.     end
  465.     -----------------------------------------------------------------------------------
  466.     -- changeChestStatus    - Sets chest.used to status if given
  467.     --                      - Changes chest.used otherwise
  468.     --                      - correctly sets the 'nextUnused' id for the chest list
  469.     -----------------------------------------------------------------------------------
  470.     function SDM.changeChestStatus(cId, status)
  471.         local diskList = SDM.getStorageDisks("chest")
  472.         local chestList = {}
  473.         local path = ""
  474.         for i, disk in ipairs(diskList) do
  475.             local path1 = getChestListPath(i)
  476.             local chestList1 = FM.loadTable(path1)
  477.             if #chestList1 > 0 then
  478.                 if chestList1[1].id <= cId and chestList1[#chestList1].id >= cId then
  479.                     path = path1
  480.                     chestList = chestList1
  481.                     break
  482.                 end
  483.             end
  484.         end
  485.         if path ~= "" then
  486.             local listId = cId - chestList[1].id + 1
  487.             if type(status) == "boolean" then
  488.                 chestList[listId].used = status
  489.             else
  490.                 local oldStatus = chestList[listId].used
  491.                 chestList[listId].used = not oldStatus
  492.             end
  493.            
  494.             if chestList[listId].used and cId == chestList["nextUnused"] then
  495.                 for i = listId, #chestList do
  496.                     if not chestList[i].used then
  497.                         chestList["nextUnused"] = chestList[i].id
  498.                         break
  499.                     elseif i == #chestList then
  500.                         chestList["nextUnused"] = chestList[i].id + 1
  501.                     end
  502.                 end
  503.             elseif not chestList[listId].used and cId <= chestList["nextUnused"] then
  504.                 chestList["nextUnused"] = cId
  505.             end
  506.             SDM.saveChestList(chestList)
  507.             return true
  508.         end
  509.         print("Error: Chest with id " .. tostring(cId) .. " could not be found.")
  510.         return false
  511.     end
  512.     -----------------------------------------------------------------------------------
  513.     -- removeChestFromItem      - Removes Chest with id cId from item
  514.     -- addChestToItem           - Adds chest to given item
  515.     -----------------------------------------------------------------------------------
  516.     function SDM.removeChestFromItem(cId, param)
  517.         local item = param
  518.         if type(param) ~= "table" then
  519.             item = SDM.findItem(param)
  520.         end
  521.         local idx = 0
  522.         for i, itemChest in ipairs(item.storage) do
  523.             if itemChest.id == cId then
  524.                 idx = i
  525.                 break
  526.             end
  527.         end
  528.         if idx > 0 then
  529.             table.remove(item.storage, idx)
  530.             SDM.saveItem(item)
  531.             SDM.changeChestStatus(cId, false)
  532.             return true
  533.         end
  534.         return false
  535.     end
  536.     function SDM.addChestToItem(chest, param)
  537.         local item = param
  538.         if type(param) ~= "table" then
  539.             item = SDM.findItem(param)
  540.         end
  541.         table.insert(item.storage, chest)
  542.         SDM.saveItem(item)
  543.         SDM.changeChestStatus(chest.id, true)
  544.     end
  545.     -----------------------------------------------------------------------------------
  546.     -- getChestCoords       - Searches for chest by id and returns its coords
  547.     -- getChestId           - Searches for chest by coords and returns its id
  548.     -- chestAvailable       - Searches for unused chest. Returns true if found
  549.     -----------------------------------------------------------------------------------
  550.     function SDM.getChestCoords(cId)
  551.         local diskList = SDM.getStorageDisks("chest")
  552.         for i, disk in ipairs(diskList) do
  553.             local path = getChestListPath(i)
  554.             local chestList = FM.loadTable(path)
  555.             if #chestList > 0 then
  556.                 if chestList[1].id <= cId and chestList[#chestList].id >= cId then
  557.                     local idx = cId - chestList[1].id + 1
  558.                     local chest = chestList[idx]
  559.                     return chest.coords
  560.                 end
  561.             end
  562.         end
  563.         return nil
  564.     end
  565.     function SDM.getChestId(coords)
  566.         local diskList = SDM.getStorageDisks("chest")
  567.         for i, disk in ipairs(diskList) do
  568.             local path = getChestListPath(i)
  569.             local chestList = FM.loadTable(path)
  570.             if #chestList > 0 then
  571.                 for j, chest in ipairs(chestList) do
  572.                     if chest.coords.x == coords.x and chest.coords.y == coords.y and chest.coords.z == coords.z then
  573.                         return chest.id
  574.                     end
  575.                 end
  576.             end
  577.         end
  578.         return nil
  579.     end
  580.     function SDM.chestAvailable()
  581.         local disks = SDM.getStorageDisks("chest")
  582.         for i, disk in ipairs(disks) do
  583.             local path = getChestListPath(i)
  584.             local chestList = FM.loadTable(path)
  585.             local bCondition = chestList and #textutils.serialize(chestList) > 2
  586.             if bCondition and chestList["nextUnused"] <= chestList[#chestList].id then
  587.                 return true
  588.             end
  589.         end
  590.         return false
  591.     end
  592.     -----------------------------------------------------------------------------------
  593.     -- checkChestIdContinuity       - Returns true if chest ids are continuous
  594.     -----------------------------------------------------------------------------------
  595.     function SDM.checkChestIdContinuity()
  596.         local disks = SDM.getStorageDisks("chest")
  597.         local lastId = 0
  598.         for i, disk in ipairs(disks) do
  599.             local path = getChestListPath(i)
  600.             local chestList = FM.loadTable(path)
  601.  
  602.             for j, chest in ipairs(chestList) do
  603.                 if chest.id == lastId + 1 then
  604.                     lastId = lastId + 1
  605.                 else
  606.                     return false
  607.                 end
  608.             end
  609.         end
  610.         return true
  611.     end
  612.     -----------------------------------------------------------------------------------
  613.     -- addInputItem     - Item on the way to the chest is added as such
  614.     -- addOutputItem    - Item requested is added as such for the chest
  615.     -- addStoredItem    - Inputed Item reached chest and is counted as stored
  616.     -- subStoredItem    - Requested Item is delivered and removed from the system
  617.     -----------------------------------------------------------------------------------
  618.     function SDM.addInputItem(itemData)
  619.         local item = SDM.findItem(itemData)
  620.  
  621.         if not item then
  622.             item = createItem(itemData.name, itemData.dName, itemData.stackSize)
  623.         end
  624.  
  625.         local chestLimit = item.stackSize * 54
  626.  
  627.         local targetChests = {}
  628.  
  629.         local numToStore = itemData.number
  630.         for _, chest in ipairs(item.storage) do
  631.             local remainingCapacity = chestLimit - chest.num - chest.storingNum
  632.             if remainingCapacity > 0 then
  633.                 local num = math.min(numToStore, remainingCapacity)
  634.                 table.insert(targetChests, {id = chest.id, num = num})
  635.                 chest.storingNum = chest.storingNum + num
  636.                 numToStore = numToStore - num
  637.                 if numToStore == 0 then
  638.                     break
  639.                 end
  640.             end
  641.         end
  642.  
  643.         if numToStore > 0 then
  644.             local newChest = getNextEmptyChest()
  645.             newChest.storingNum = numToStore
  646.             table.insert(item.storage, newChest)
  647.             table.insert(targetChests, {id = newChest.id, num = numToStore})
  648.         end
  649.         SDM.saveItem(item)
  650.  
  651.         return targetChests
  652.     end
  653.     function SDM.addOutputItem(itemName, num)
  654.         local item = SDM.findItem(itemName)
  655.  
  656.         if not item then
  657.             return nil
  658.         end
  659.  
  660.         local chestLimit = item.stackSize * 54
  661.  
  662.         local targetChests = {}
  663.  
  664.         local numToExtract = num
  665.         for _, chest in ipairs(item.storage) do
  666.             local remainingItems = chest.num - chest.extractingNum
  667.             if remainingItems > 0 then
  668.                 local amount = math.min(numToExtract, remainingItems)
  669.                 table.insert(targetChests, {id = chest.id, num = amount})
  670.                 chest.extractingNum = chest.extractingNum + amount
  671.                 numToExtract = numToExtract - amount
  672.                 if numToExtract == 0 then
  673.                     break
  674.                 end
  675.             end
  676.         end
  677.  
  678.         SDM.saveItem(item)
  679.        
  680.         return targetChests
  681.     end
  682.     function SDM.addStoredItem(itemName, cId, num)
  683.         local item = SDM.findItem(itemName)
  684.         if not item then
  685.             return false
  686.         end
  687.  
  688.         local chestLimit = item.stackSize * 54
  689.  
  690.         local bSuccess = false
  691.         for _, chest in ipairs(item.storage) do
  692.             if chest.id == cId then
  693.                 chest.num = chest.num + num
  694.                 chest.storingNum = chest.storingNum - num
  695.                 bSuccess = true
  696.                 break
  697.             end
  698.         end
  699.  
  700.         if bSuccess then
  701.             SDM.saveItem(item)
  702.         end
  703.  
  704.         return bSuccess
  705.     end
  706.     function SDM.subStoredItem(itemName, cId, num)
  707.         local item = SDM.findItem(itemName)
  708.  
  709.         if not item then
  710.             return false
  711.         end
  712.  
  713.         local chestLimit = item.stackSize * 54
  714.  
  715.         local removedChests = {}
  716.         local bSuccess = false
  717.         for i, chest in ipairs(item.storage) do
  718.             if chest.id == cId then
  719.                 chest.num = chest.num - num
  720.                 chest.extractingNum = chest.extractingNum - num
  721.                 bSuccess = true
  722.                 if chest.num == 0 and chest.extractingNum == 0 and chest.storingNum == 0 then
  723.                     table.insert(removedChests, 1, i)
  724.                     SDM.changeChestStatus(chest.id, false)
  725.                 end
  726.                 break
  727.             end
  728.         end
  729.  
  730.         for _, indx in ipairs(removedChests) do
  731.             table.remove(item.storage, indx)
  732.         end
  733.  
  734.         if bSuccess then
  735.             SDM.saveItem(item)
  736.         end
  737.        
  738.         return bSuccess
  739.     end
  740.  
  741.     -----------------------------------------------------------------------------------
  742.     -- getStoredItemNumbers     - Returns number of items currently stored
  743.     --                            + Detailed table about incoming, outgoing and total
  744.     --                            amount in System
  745.     -----------------------------------------------------------------------------------
  746.     function SDM.getStoredItemNumbers(itemName)
  747.         local item = SDM.findItem(itemName)
  748.         if not item then
  749.             return 0
  750.         end
  751.  
  752.         local av = 0
  753.         local tot = 0
  754.         local inc = 0
  755.         local out = 0
  756.  
  757.         for i, chest in ipairs(item.storage) do
  758.             av = av + chest.num - chest.extractingNum
  759.             tot = tot + chest.num + chest.storingNum
  760.             inc = inc + chest.storingNum
  761.             out = out + chest.extractingNum
  762.         end
  763.  
  764.         return av, {tot = tot, inc = inc, out = out}
  765.     end
  766.  
  767.     -----------------------------------------------------------------------------------
  768.     -- isFullyInitialized       - Checks if system is already initialized
  769.     -----------------------------------------------------------------------------------
  770.     function SDM.isFullyInitialized()
  771.         local initData = SDM.getInitData()
  772.         if initData and #textutils.serialize(initData) == 2 then return false end
  773.  
  774.         local varList = {
  775.                 "dirSection", "dirTopBelt", "dirBottomBelt", "initChestCoord", "yModulo",
  776.                 "chestRowMod", "chestColMod", "elevPos", "sectionMod", "leftSecMod",
  777.                 "rightSecMod", "lowBeltY", "upBeltY", "diskOutCoord", "diskFinishCoord",
  778.                 "entryModulo", "exitModulo", "pickaxeChestCoord", "chestOutCoord",
  779.                 "bLeftStart", "colBlockNum", "blockHeight", "beltPos", "chestRefuelCoord"
  780.         }
  781.  
  782.         for _, var in ipairs(varList) do
  783.             if initData[var] == nil then return false end
  784.         end
  785.         return true
  786.     end
  787.  
  788.     -----------------------------------------------------------------------------------
  789.     -- getInitData              - Returns initialized data
  790.     -- setInitData              - Saves (overwrites) initialized data
  791.     -----------------------------------------------------------------------------------
  792.     function SDM.getInitData()
  793.         local disk, diskInfo = SDM.getDisk("init")
  794.  
  795.         if not disk then return nil end
  796.  
  797.         local path = fs.combine(diskInfo.path, "Init_Data")
  798.         local initData = FM.loadTable(path)
  799.         return initData
  800.     end
  801.     function SDM.setInitData(initData)
  802.         local disk, diskInfo = SDM.getDisk("init")
  803.  
  804.         if not disk then return nil end
  805.  
  806.         local path = fs.combine(diskInfo.path, "Init_Data")
  807.         FM.saveTable(initData, path, true)
  808.         return true
  809.     end
  810.  
  811.     function SDM.getIncomingItem()
  812.         local disk, diskInfo = SDM.getDisk("incoming")
  813.  
  814.         if not disk then
  815.             disk, diskInfo = SDM.addDisk("incoming")
  816.         end
  817.         local path = fs.combine(diskInfo.path, "Incoming_Task")
  818.         local incomingItem = FM.loadTable(path)
  819.  
  820.         return incomingItem
  821.     end
  822.     function SDM.setIncomingItem(itemInfo)
  823.         local disk, diskInfo = SDM.getDisk("incoming")
  824.  
  825.         if not disk then
  826.             disk, diskInfo = SDM.addDisk("incoming")
  827.         end
  828.         local path = fs.combine(diskInfo.path, "Incoming_Task")
  829.         FM.saveTable(itemInfo, path, true)
  830.         return true
  831.     end
  832.  
  833.     function SDM.getOutgoingTasks()
  834.         local disk, diskInfo = SDM.getDisk("outgoing")
  835.  
  836.         if not disk then return nil end
  837.  
  838.         local path = fs.combine(diskInfo.path, "Outgoing_Tasks")
  839.         local outgoingTaskList = FM.loadTable(path)
  840.  
  841.         return outgoingTaskList
  842.     end
  843.     function SDM.setOutgoingTasks(tasks)
  844.         local disk, diskInfo = SDM.getDisk("outgoing")
  845.  
  846.         if not disk then return nil end
  847.  
  848.         local path = fs.combine(diskInfo.path, "Outgoing_Tasks")
  849.  
  850.         FM.saveTable(tasks, path, true)
  851.         return true
  852.     end
  853.  
  854.     function SDM.getFinishedTask()
  855.         local disk, diskInfo = SDM.getDisk("finished")
  856.  
  857.         if not disk then return nil end
  858.  
  859.         local path = fs.combine(diskInfo.path, "Finished_Tasks")
  860.         local finishedTaskList = FM.loadTable(path)
  861.  
  862.         return finishedTaskList
  863.     end
  864.     function SDM.setFinishedTask(task)
  865.         task.status = nil
  866.         local disk, diskInfo = SDM.getDisk("finished")
  867.  
  868.         if not disk then return nil end
  869.  
  870.         local path = fs.combine(diskInfo.path, "Finished_Tasks")
  871.  
  872.         FM.saveTable(task, path, true)
  873.         return true
  874.     end
  875.  
  876.     function SDM.setRequestedItem(itemInfo)
  877.         local disk, diskInfo = SDM.getDisk("user")
  878.  
  879.         if not disk then
  880.             disk, diskInfo = SDM.addDisk("user")
  881.         end
  882.         local path = fs.combine(diskInfo.path, "Request_Task")
  883.         FM.saveTable(itemInfo, path, true)
  884.         return true
  885.     end
  886.     function SDM.getRequestedItem()
  887.         local disk, diskInfo = SDM.getDisk("user")
  888.  
  889.         if not disk then
  890.             disk, diskInfo = SDM.addDisk("user")
  891.         end
  892.         local path = fs.combine(diskInfo.path, "Request_Task")
  893.         local requestedItem = FM.loadTable(path)
  894.  
  895.         return requestedItem
  896.     end
  897.  
  898.     function SDM.setChestPreTask(taskInfo)
  899.         local disk, diskInfo = SDM.getDisk("settings")
  900.         if not disk then
  901.             disk, diskInfo = SDM.addDisk("settings")
  902.         end
  903.         local SAVE_FILE = "chest_init"
  904.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  905.  
  906.         FM.saveTable(taskInfo, path, true)
  907.     end
  908.     function SDM.getChestPreTask()
  909.         local disk, diskInfo = SDM.getDisk("settings")
  910.         if not disk then
  911.             disk, diskInfo = SDM.addDisk("settings")
  912.         end
  913.         local SAVE_FILE = "chest_init"
  914.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  915.  
  916.         local taskInfo = FM.loadTable(path)
  917.  
  918.         return taskInfo
  919.     end
  920.  
  921.     function SDM.setChestBlockCoords(coords)
  922.         local disk, diskInfo = SDM.getDisk("settings")
  923.         if not disk then
  924.             disk, diskInfo = SDM.addDisk("settings")
  925.         end
  926.         local SAVE_FILE = "ChestBlockCoords"
  927.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  928.  
  929.         FM.saveTable(coords, path, true)
  930.     end
  931.     function SDM.getChestBlockCoords()
  932.         local disk, diskInfo = SDM.getDisk("settings")
  933.         if not disk then
  934.             disk, diskInfo = SDM.addDisk("settings")
  935.         end
  936.         local SAVE_FILE = "ChestBlockCoords"
  937.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  938.         return FM.loadTable(path)
  939.     end
  940.  
  941.     function SDM.setInvCheckPreTask(taskInfo)
  942.         local disk, diskInfo = SDM.getDisk("settings")
  943.         if not disk then
  944.             disk, diskInfo = SDM.addDisk("settings")
  945.         end
  946.         local SAVE_FILE = "inv_check"
  947.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  948.  
  949.         FM.saveTable(taskInfo, path, true)
  950.     end
  951.     function SDM.getInvCheckPreTask()
  952.         local disk, diskInfo = SDM.getDisk("settings")
  953.         if not disk then
  954.             disk, diskInfo = SDM.addDisk("settings")
  955.         end
  956.         local SAVE_FILE = "inv_check"
  957.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  958.  
  959.         return FM.loadTable(path)
  960.     end
  961.  
  962.     function SDM.getCompletedTasks()
  963.         local disk, diskInfo = SDM.getDisk("protocoll")
  964.         if not disk then
  965.             disk, diskInfo = SDM.addDisk("protocoll")
  966.         end
  967.         local SAVE_FILE = "Completed_Tasks"
  968.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  969.         return FM.loadTable(path)
  970.     end
  971.     function SDM.setCompletedTasks(taskInfo)
  972.         local disk, diskInfo = SDM.getDisk("protocoll")
  973.         if not disk then
  974.             disk, diskInfo = SDM.addDisk("protocoll")
  975.         end
  976.         local SAVE_FILE = "Completed_Tasks"
  977.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  978.  
  979.         FM.saveTable(taskInfo, path, true)
  980.     end
  981.  
  982.     function SDM.getCurrentTasks()
  983.         local disk, diskInfo = SDM.getDisk("protocoll")
  984.         if not disk then
  985.             disk, diskInfo = SDM.addDisk("protocoll")
  986.         end
  987.         local SAVE_FILE = "Current_Tasks"
  988.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  989.         return FM.loadTable(path)
  990.     end
  991.     function SDM.setCurrentTasks(taskInfo)
  992.         local disk, diskInfo = SDM.getDisk("protocoll")
  993.         if not disk then
  994.             disk, diskInfo = SDM.addDisk("protocoll")
  995.         end
  996.         local SAVE_FILE = "Current_Tasks"
  997.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  998.  
  999.         FM.saveTable(taskInfo, path, true)
  1000.     end
  1001.     function SDM.saveCurrentTask(taskInfo)
  1002.         local disk, diskInfo = SDM.getDisk("protocoll")
  1003.         if not disk then
  1004.             disk, diskInfo = SDM.addDisk("protocoll")
  1005.         end
  1006.         local SAVE_FILE = "Current_Tasks"
  1007.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  1008.  
  1009.         local taskData = SDM.getCurrentTasks()
  1010.         table.insert(taskData, taskInfo)
  1011.  
  1012.         FM.saveTable(taskData, path, true)
  1013.     end
  1014.  
  1015.     function SDM.saveTaskId(id)
  1016.         local disk, diskInfo = SDM.getDisk("settings")
  1017.         if not disk then
  1018.             disk, diskInfo = SDM.addDisk("settings")
  1019.         end
  1020.         local SAVE_FILE = "Last_Task_ID"
  1021.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  1022.  
  1023.         FM.saveTable({id}, path)
  1024.     end
  1025.     function SDM.loadTaskId()
  1026.         local disk, diskInfo = SDM.getDisk("settings")
  1027.         if not disk then
  1028.             disk, diskInfo = SDM.addDisk("settings")
  1029.         end
  1030.         local SAVE_FILE = "Last_Task_ID"
  1031.         local path = fs.combine(diskInfo.path, SAVE_FILE)
  1032.  
  1033.         local idList = FM.loadTable(path)
  1034.         if idList[1] then return idList[1] end
  1035.         return 0
  1036.     end
  1037.  
  1038.  
  1039.     function SDM.moveToOutgoing_SE()
  1040.         filename = "SE"
  1041.         local SE_Data = FM.loadFile(filename)
  1042.  
  1043.         local disk, diskInfo = SDM.getDisk("outgoing")
  1044.         if not disk then return nil end
  1045.         local path = fs.combine(diskInfo.path, filename)
  1046.  
  1047.         FM.saveFile(SE_Data, path)
  1048.     end
  1049.  
  1050.    
  1051.  
  1052.     SDM.TURTLE = {}
  1053.  
  1054.     function SDM.TURTLE.update_SE()
  1055.         filename = "SE"
  1056.         local SE_old = FM.loadFile(filename)
  1057.        
  1058.         local disk, diskInfo = SDM.getDisk("outgoing")
  1059.         if not disk then return nil end
  1060.         local path = fs.combine(diskInfo.path, filename)
  1061.  
  1062.         if not disk then return false end
  1063.  
  1064.         local SE_new = FM.loadFile(path)
  1065.  
  1066.         if not SE_new then return false end
  1067.  
  1068.         if SE_new ~= SE_old then
  1069.             FM.saveFile(SE_new, filename)
  1070.             return true
  1071.         end
  1072.  
  1073.         return false
  1074.     end
  1075.     -----------------------------------------------------------------------------------
  1076.     -- loadTurtleTask       - Turtle loads actual task
  1077.     -- saveTurtleTask       - Turtle saves task
  1078.     -----------------------------------------------------------------------------------
  1079.     function SDM.TURTLE.loadTurtleTask()
  1080.         return FM.loadTable("Turtle_Task")
  1081.     end
  1082.     function SDM.TURTLE.saveTurtleTask(task)
  1083.         if task and #textutils.serialize(task) > 2 and not task.status then
  1084.             task.status = 1
  1085.         end
  1086.         FM.saveTable(task, "Turtle_Task", true)
  1087.     end
  1088.  
  1089.     -----------------------------------------------------------------------------------
  1090.     -- loadInitData         - Loads initData from local file or disk file
  1091.     --                      - Saves it locally if loaded from disk
  1092.     -----------------------------------------------------------------------------------
  1093.     function SDM.TURTLE.loadInitData()
  1094.         local SAVE_FILE = "Init_Data"
  1095.         local initData = FM.loadTable(SAVE_FILE)
  1096.         if initData and #textutils.serialize(initData) == 2 then
  1097.             initData = SDM.getInitData()
  1098.             FM.saveTable(initData, SAVE_FILE, true)
  1099.         end
  1100.         if initData and #textutils.serialize(initData) == 2 then
  1101.             return nil
  1102.         end
  1103.         return initData
  1104.     end
  1105.  
  1106.     -----------------------------------------------------------------------------------
  1107.     -- getNextOutgoingTask      - Turtle takes first Task
  1108.     -----------------------------------------------------------------------------------
  1109.     function SDM.TURTLE.getNextOutgoingTask()
  1110.         local taskList = SDM.getOutgoingTasks()
  1111.         local task = table.remove(taskList, 1)
  1112.         SDM.TURTLE.saveTurtleTask(task)
  1113.         SDM.setOutgoingTasks(taskList)
  1114.     end
  1115.  
  1116.     function SDM.TURTLE.setQueueState(bQueue)
  1117.         local SAVE_FILE = "Queue_State"
  1118.         FM.saveTable({bQueue}, SAVE_FILE)
  1119.     end
  1120.     function SDM.TURTLE.getQueueState()
  1121.         local SAVE_FILE = "Queue_State"
  1122.         local loadedList = FM.loadTable(SAVE_FILE)
  1123.         if type(loadedList) == "table" and loadedList[1] and type(loadedList[1] == "boolean") then
  1124.             return loadedList[1]
  1125.         end
  1126.         return false
  1127.     end
  1128.  
  1129.     return SDM
Advertisement
Add Comment
Please, Sign In to add comment