Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get 6fYKgMYH StorageDataManager
- FM = require("FM")
- local itemInfo = {
- name = "minecraft:cobblestone",
- dName = "Cobblestone",
- stackSize = 64,
- number = 100
- }
- local DataStorageSettings = {
- maxSize_Items = 90000,
- maxSize_Chests = 90000
- }
- local lastChestIndex = 0
- local function getDefaultDisplayName(itemName)
- if not itemName then return nil end
- -- Extrahiere den Teil nach dem Doppelpunkt (z. B. "stone_axe")
- local id = itemName:match("^[^:]+:(.+)$")
- if not id then return nil end
- -- Ersetze Unterstriche durch Leerzeichen und kapitalisiere jedes Wort
- local words = {}
- for word in string.gmatch(id, "[^_]+") do
- table.insert(words, word:sub(1,1):upper() .. word:sub(2))
- end
- return table.concat(words, " ")
- end
- local function getChestListPath(listNum)
- local diskList = SDM.getStorageDisks("chest")
- local diskPath = diskList[listNum].path
- local filename = "Chestlist_" .. tostring(listNum)
- return fs.combine(diskPath, filename)
- end
- local function getItemListPath(listNum)
- local diskList = SDM.getStorageDisks("item")
- local diskPath = diskList[listNum].path
- local filename = "Itemlist_" .. tostring(listNum)
- return fs.combine(diskPath, filename)
- end
- local function getNextEmptyChest()
- local disks = SDM.getStorageDisks("chest")
- for i, disk in ipairs(disks) do
- local path = getChestListPath(i)
- local chestList = FM.loadTable(path)
- if chestList and chestList["nextUnused"] <= chestList[#chestList].id then
- local cId = chestList["nextUnused"]
- SDM.changeChestStatus(cId, true)
- return {id = cId, num = 0, storingNum = 0, extractingNum = 0}
- end
- end
- print("Error: Can't find unused chest.")
- return nil
- end
- local function getLastChestId()
- local disks = SDM.getStorageDisks("chest")
- if #disks == 0 then
- _, disks = SDM.addStorageDisk("chest")
- return 0
- end
- local cnt = #disks
- while cnt > 0 do
- local path = getChestListPath(cnt)
- local chestList = FM.loadTable(path)
- if #chestList > 0 then return chestList[#chestList].id end
- cnt = cnt - 1
- end
- return 0
- end
- local function getLastChestList()
- local disks = SDM.getStorageDisks("chest")
- if #disks == 0 then
- _, disks = SDM.addStorageDisk("chest")
- return {}, 1
- end
- local cnt = #disks
- while cnt > 0 do
- local path = getChestListPath(cnt)
- local chestList = FM.loadTable(path)
- if #chestList > 0 then
- return chestList, cnt
- end
- cnt = cnt - 1
- end
- return {}, 1
- end
- local function createChest(id, coords)
- local chest = {
- id = id,
- coords = coords,
- used = false
- }
- return chest
- end
- local function createItem(name, dName, stackSize)
- if type(dName) == "number" then
- stackSize = dName
- dName = getDefaultDisplayName(name)
- elseif not stackSize then
- stackSize = 64
- end
- local chest1 = getNextEmptyChest()
- local NewItem = {
- name = name,
- dName = dName,
- stackSize = stackSize,
- storage = {
- chest1,
- }
- }
- return NewItem
- end
- local function isNameOfItem(name, item)
- if item.name == name or item.dName == name then return true end
- if item.name == "minecraft:" .. name then return true end
- if item.name == "minecraft:" .. name:lower():gsub(" ", "_") then return true end
- if item.name == name:lower():gsub(" ", "_") then return true end
- if item.dName:lower():gsub(" ", "_") == name:lower():gsub(" ", "_") then return true end
- return false
- end
- SDM = {}
- -----------------------------------------------------------------------------------
- -- getStorageDisks - Returns list of all Storage disks of requested type
- -- addStorageDisk - Adds a new Storage Disk for requested type
- -- getDisk - Returns any disk of requested type
- -- addDisk - Adds any disk of requested type(limited to 1 for most types)
- -----------------------------------------------------------------------------------
- function SDM.getStorageDisks(dataStorageType)
- local STORAGE_LABEL_BASE = "Item_Storage"
- if dataStorageType == "Chest" or dataStorageType == "chest" then
- STORAGE_LABEL_BASE = "Chest_Storage"
- end
- local disks = {}
- local index = 1
- while true do
- local label = STORAGE_LABEL_BASE .. (index > 1 and (" " .. tostring(index)) or "")
- local _, path = FM.getDiskByLabel(label)
- if not path then break end
- table.insert(disks, {label = label, path = path})
- index = index + 1
- end
- return disks
- end
- function SDM.addStorageDisk(dataStorageType)
- local STORAGE_LABEL_BASE = "Item_Storage"
- if dataStorageType == "Chest" or dataStorageType == "chest" then
- STORAGE_LABEL_BASE = "Chest_Storage"
- end
- local disks = SDM.getStorageDisks(dataStorageType)
- local index = #disks + 1
- local sDisk, sPath = FM.getDiskByLabel()
- if not sDisk then
- return nil, disks
- end
- local label = STORAGE_LABEL_BASE .. (index > 1 and (" " .. tostring(index)) or "")
- sDisk.setDiskLabel(label)
- table.insert(disks, {label = label, path = sPath})
- return sDisk, disks
- end
- function SDM.getDisk(diskType)
- local isStorageDisk = diskType == "Chest" or diskType == "chest" or diskType == "Item" or diskType == "item"
- if isStorageDisk then
- return SDM.getStorageDisks(diskType)
- end
- local DISK_LABEL = ""
- local isSettings = diskType == "Settings" or diskType == "settings" or diskType == "settings Task" or diskType == "Settings task"
- local isUser = diskType == "user" or diskType == "User" or diskType == "user Task" or diskType == "User task"
- local isIncoming = diskType == "incoming" or diskType == "Incoming" or diskType == "incoming Task" or diskType == "Incoming task"
- local isOutgoing = diskType == "outgoing" or diskType == "Outgoing" or diskType == "outgoing Task" or diskType == "Outgoing Task"
- local isFinished = diskType == "finished" or diskType == "Finished" or diskType == "finished Task" or diskType == "Finished Task"
- local isTurtInit = diskType == "init" or diskType == "Init" or diskType == "turtle init" or diskType == "Turtle Init"
- local isTaskProt = diskType == "protocoll" or diskType == "Protocoll" or diskType == "task protocoll" or diskType == "Task Protocoll"
- if isSettings then
- DISK_LABEL = "Settings"
- elseif isUser then
- DISK_LABEL = "User Task"
- elseif isIncoming then
- DISK_LABEL = "Incoming Task"
- elseif isOutgoing then
- DISK_LABEL = "Outgoing Task"
- elseif isFinished then
- DISK_LABEL = "Finished Task"
- elseif isTurtInit then
- DISK_LABEL = "Turtle Initializing"
- elseif isTaskProt then
- DISK_LABEL = "Task Protocoll"
- else
- return nil
- end
- local sDisk, sPath = FM.getDiskByLabel(DISK_LABEL)
- if sDisk then
- return sDisk, {label = DISK_LABEL, path = sPath}
- end
- return nil
- end
- function SDM.addDisk(diskType)
- local isStorageDisk = diskType == "Chest" or diskType == "chest" or diskType == "Item" or diskType == "item"
- if isStorageDisk then
- return SDM.addStorageDisk(diskType)
- end
- if SDM.getDisk(diskType) then
- print("Warning: Disk already exists. Use getDisk() to get it")
- return nil
- end
- local DISK_LABEL = ""
- local isSettings = diskType == "Settings" or diskType == "settings"
- local isUser = diskType == "user" or diskType == "User" or diskType == "User Task" or diskType == "user task"
- local isIncoming = diskType == "incoming" or diskType == "Incoming" or diskType == "Incoming Task" or diskType == "incoming task"
- local isTaskProt = diskType == "protocoll" or diskType == "Protocoll" or diskType == "task protocoll" or diskType == "Task Protocoll"
- if isSettings then
- DISK_LABEL = "Settings"
- elseif isUser then
- DISK_LABEL = "User Task"
- elseif isIncoming then
- DISK_LABEL = "Incoming Task"
- elseif isTaskProt then
- DISK_LABEL = "Task Protocoll"
- else
- return nil
- end
- local sDisk, sPath = FM.getDiskByLabel()
- sDisk.setDiskLabel(DISK_LABEL)
- return sDisk, {label = DISK_LABEL, path = sPath}
- end
- -----------------------------------------------------------------------------------
- -- findItem - Finds an item in the savefiles by name or itemInfo
- -- findItemByChestId - Finds an item with given chestId
- -----------------------------------------------------------------------------------
- function SDM.findItem(param)
- local name = param
- if type(param) == "table" then
- name = param.name
- end
- local storageDisks = SDM.getStorageDisks("item")
- for i, diskInfo in ipairs(storageDisks) do
- local path = getItemListPath(i)
- local itemList = FM.loadTable(path)
- for _, item in ipairs(itemList) do
- if isNameOfItem(name, item) then
- return item
- end
- end
- end
- return nil
- end
- function SDM.findItemByChestId(chestId)
- local storageDisks = SDM.getStorageDisks("item")
- for i, diskInfo in ipairs(storageDisks) do
- local path = getItemListPath(i)
- local itemList = FM.loadTable(path)
- for _, item in ipairs(itemList) do
- local itemStorage = item.storage
- for _, chest in ipairs(item.storage) do
- if chest.id == chestId then
- return item
- end
- end
- end
- end
- return nil
- end
- -----------------------------------------------------------------------------------
- -- saveItem - Item replaces its old data in the savefiles
- -- or is added at the end
- -- saveChestList - Saves chestlist on correct disk
- -----------------------------------------------------------------------------------
- function SDM.saveItem(item)
- local storageDisks = SDM.getStorageDisks("item")
- local bItemReplaced = false
- local bufferItems = {}
- for i, diskInfo in ipairs(storageDisks) do
- local diskPath = diskInfo.path
- local filename = "Itemlist_" .. tostring(i)
- local path = fs.combine(diskPath, filename)
- local itemList = FM.loadTable(path)
- if not bItemReplaced then
- for j, oldItem in ipairs(itemList) do
- if isNameOfItem(item.name, oldItem) then
- itemList[j] = item
- bItemReplaced = true
- end
- end
- end
- if bItemReplaced then
- itemList = FM.combineTables(bufferItems, itemList)
- bufferItems = {}
- while FM.getTableSize(itemList) > DataStorageSettings.maxSize_Items do
- table.insert(bufferItems, 1, table.remove(itemList))
- end
- FM.saveTable(itemList, path)
- if #bufferItems == 0 then return end
- end
- end
- if not bItemReplaced and #storageDisks > 0 then
- local diskInfo = storageDisks[#storageDisks]
- local filename = "Itemlist_" .. tostring(#storageDisks)
- local path = fs.combine(diskInfo.path, filename)
- local itemList = FM.loadTable(path)
- table.insert(itemList, item)
- if not (FM.getTableSize(itemList) > DataStorageSettings.maxSize_Items) then
- FM.saveTable(itemList, path)
- return
- end
- table.insert(bufferItems, item)
- end
- if #storageDisks == 0 then
- table.insert(bufferItems, item)
- end
- _, storageDisks = SDM.addStorageDisk("item")
- local diskInfo = storageDisks[#storageDisks]
- local filename = "Itemlist_" .. tostring(#storageDisks)
- local path = fs.combine(diskInfo.path, filename)
- FM.saveTable(bufferItems, path)
- return
- end
- function SDM.saveChestList(chestList)
- local listNum = chestList["num"]
- local path = getChestListPath(listNum)
- FM.saveTable(chestList, path)
- end
- -- SDM.createChestBlock({x=0, y=0, z=0}, {x=1, y=0, z=0})
- -----------------------------------------------------------------------------------
- -- createChestBlock - Registers a 5 high and 10 Block long block of chests
- -- createSingleChest - Registers a single chest at given coordinates
- -----------------------------------------------------------------------------------
- function SDM.createChestBlock(startCoords, dirCoords)
- local chestList, listNum = getLastChestList()
- local diskList = SDM.getStorageDisks("chest")
- local lastId = getLastChestId()
- if #chestList == 0 then
- chestList["num"] = listNum
- chestList["numChests"] = #chestList
- chestList["nextUnused"] = lastId + 1
- end
- local nextId = lastId + 1
- for dy = 0, 4 do
- for s = 0, 9 do
- local newCoords = {
- x = startCoords.x + dirCoords.x * s,
- y = startCoords.y + dy,
- z = startCoords.z + dirCoords.z * s
- }
- local newChest = createChest(nextId, newCoords)
- local newListSize = #textutils.serialize(chestList, {compact = true}) + #textutils.serialize(newChest, {compact = true}) + 1
- if newListSize < DataStorageSettings.maxSize_Chests then
- table.insert(chestList, newChest)
- else
- chestList["numChests"] = #chestList
- SDM.saveChestList(chestList)
- if listNum == #diskList then
- _, diskList = SDM.addStorageDisk("chest")
- end
- listNum = listNum + 1
- local path = getChestListPath(listNum)
- chestList = FM.loadTable(path)
- chestList["num"] = listNum
- chestList["numChests"] = #chestList
- chestList["nextUnused"] = nextId
- table.insert(chestList, newChest)
- end
- nextId = nextId + 1
- end
- end
- chestList["numChests"] = #chestList
- SDM.saveChestList(chestList)
- end
- function SDM.createSingleChest(coords)
- local chestList, listNum = getLastChestList()
- local diskList = SDM.getStorageDisks("chest")
- local lastId = getLastChestId()
- if #chestList == 0 then
- chestList["num"] = listNum
- chestList["numChests"] = #chestList
- chestList["nextUnused"] = lastId + 1
- end
- local nextId = lastId + 1
- local newChest = createChest(nextId, coords)
- local newListSize = #textutils.serialize(chestList, {compact = true}) + #textutils.serialize(newChest, {compact = true}) + 1
- if newListSize < DataStorageSettings.maxSize_Chests then
- table.insert(chestList, newChest)
- else
- if listNum == #diskList then
- _, diskList = SDM.addStorageDisk("chest")
- end
- listNum = listNum + 1
- local path = getChestListPath(listNum)
- chestList = FM.loadTable(path)
- chestList["num"] = listNum
- chestList["numChests"] = #chestList
- chestList["nextUnused"] = nextId
- table.insert(chestList, newChest)
- end
- chestList["numChests"] = #chestList
- SDM.saveChestList(chestList)
- return newChest
- end
- -----------------------------------------------------------------------------------
- -- changeChestStatus - Sets chest.used to status if given
- -- - Changes chest.used otherwise
- -- - correctly sets the 'nextUnused' id for the chest list
- -----------------------------------------------------------------------------------
- function SDM.changeChestStatus(cId, status)
- local diskList = SDM.getStorageDisks("chest")
- local chestList = {}
- local path = ""
- for i, disk in ipairs(diskList) do
- local path1 = getChestListPath(i)
- local chestList1 = FM.loadTable(path1)
- if #chestList1 > 0 then
- if chestList1[1].id <= cId and chestList1[#chestList1].id >= cId then
- path = path1
- chestList = chestList1
- break
- end
- end
- end
- if path ~= "" then
- local listId = cId - chestList[1].id + 1
- if type(status) == "boolean" then
- chestList[listId].used = status
- else
- local oldStatus = chestList[listId].used
- chestList[listId].used = not oldStatus
- end
- if chestList[listId].used and cId == chestList["nextUnused"] then
- for i = listId, #chestList do
- if not chestList[i].used then
- chestList["nextUnused"] = chestList[i].id
- break
- elseif i == #chestList then
- chestList["nextUnused"] = chestList[i].id + 1
- end
- end
- elseif not chestList[listId].used and cId <= chestList["nextUnused"] then
- chestList["nextUnused"] = cId
- end
- SDM.saveChestList(chestList)
- return true
- end
- print("Error: Chest with id " .. tostring(cId) .. " could not be found.")
- return false
- end
- -----------------------------------------------------------------------------------
- -- removeChestFromItem - Removes Chest with id cId from item
- -- addChestToItem - Adds chest to given item
- -----------------------------------------------------------------------------------
- function SDM.removeChestFromItem(cId, param)
- local item = param
- if type(param) ~= "table" then
- item = SDM.findItem(param)
- end
- local idx = 0
- for i, itemChest in ipairs(item.storage) do
- if itemChest.id == cId then
- idx = i
- break
- end
- end
- if idx > 0 then
- table.remove(item.storage, idx)
- SDM.saveItem(item)
- SDM.changeChestStatus(cId, false)
- return true
- end
- return false
- end
- function SDM.addChestToItem(chest, param)
- local item = param
- if type(param) ~= "table" then
- item = SDM.findItem(param)
- end
- table.insert(item.storage, chest)
- SDM.saveItem(item)
- SDM.changeChestStatus(chest.id, true)
- end
- -----------------------------------------------------------------------------------
- -- getChestCoords - Searches for chest by id and returns its coords
- -- getChestId - Searches for chest by coords and returns its id
- -- chestAvailable - Searches for unused chest. Returns true if found
- -----------------------------------------------------------------------------------
- function SDM.getChestCoords(cId)
- local diskList = SDM.getStorageDisks("chest")
- for i, disk in ipairs(diskList) do
- local path = getChestListPath(i)
- local chestList = FM.loadTable(path)
- if #chestList > 0 then
- if chestList[1].id <= cId and chestList[#chestList].id >= cId then
- local idx = cId - chestList[1].id + 1
- local chest = chestList[idx]
- return chest.coords
- end
- end
- end
- return nil
- end
- function SDM.getChestId(coords)
- local diskList = SDM.getStorageDisks("chest")
- for i, disk in ipairs(diskList) do
- local path = getChestListPath(i)
- local chestList = FM.loadTable(path)
- if #chestList > 0 then
- for j, chest in ipairs(chestList) do
- if chest.coords.x == coords.x and chest.coords.y == coords.y and chest.coords.z == coords.z then
- return chest.id
- end
- end
- end
- end
- return nil
- end
- function SDM.chestAvailable()
- local disks = SDM.getStorageDisks("chest")
- for i, disk in ipairs(disks) do
- local path = getChestListPath(i)
- local chestList = FM.loadTable(path)
- local bCondition = chestList and #textutils.serialize(chestList) > 2
- if bCondition and chestList["nextUnused"] <= chestList[#chestList].id then
- return true
- end
- end
- return false
- end
- -----------------------------------------------------------------------------------
- -- checkChestIdContinuity - Returns true if chest ids are continuous
- -----------------------------------------------------------------------------------
- function SDM.checkChestIdContinuity()
- local disks = SDM.getStorageDisks("chest")
- local lastId = 0
- for i, disk in ipairs(disks) do
- local path = getChestListPath(i)
- local chestList = FM.loadTable(path)
- for j, chest in ipairs(chestList) do
- if chest.id == lastId + 1 then
- lastId = lastId + 1
- else
- return false
- end
- end
- end
- return true
- end
- -----------------------------------------------------------------------------------
- -- addInputItem - Item on the way to the chest is added as such
- -- addOutputItem - Item requested is added as such for the chest
- -- addStoredItem - Inputed Item reached chest and is counted as stored
- -- subStoredItem - Requested Item is delivered and removed from the system
- -----------------------------------------------------------------------------------
- function SDM.addInputItem(itemData)
- local item = SDM.findItem(itemData)
- if not item then
- item = createItem(itemData.name, itemData.dName, itemData.stackSize)
- end
- local chestLimit = item.stackSize * 54
- local targetChests = {}
- local numToStore = itemData.number
- for _, chest in ipairs(item.storage) do
- local remainingCapacity = chestLimit - chest.num - chest.storingNum
- if remainingCapacity > 0 then
- local num = math.min(numToStore, remainingCapacity)
- table.insert(targetChests, {id = chest.id, num = num})
- chest.storingNum = chest.storingNum + num
- numToStore = numToStore - num
- if numToStore == 0 then
- break
- end
- end
- end
- if numToStore > 0 then
- local newChest = getNextEmptyChest()
- newChest.storingNum = numToStore
- table.insert(item.storage, newChest)
- table.insert(targetChests, {id = newChest.id, num = numToStore})
- end
- SDM.saveItem(item)
- return targetChests
- end
- function SDM.addOutputItem(itemName, num)
- local item = SDM.findItem(itemName)
- if not item then
- return nil
- end
- local chestLimit = item.stackSize * 54
- local targetChests = {}
- local numToExtract = num
- for _, chest in ipairs(item.storage) do
- local remainingItems = chest.num - chest.extractingNum
- if remainingItems > 0 then
- local amount = math.min(numToExtract, remainingItems)
- table.insert(targetChests, {id = chest.id, num = amount})
- chest.extractingNum = chest.extractingNum + amount
- numToExtract = numToExtract - amount
- if numToExtract == 0 then
- break
- end
- end
- end
- SDM.saveItem(item)
- return targetChests
- end
- function SDM.addStoredItem(itemName, cId, num)
- local item = SDM.findItem(itemName)
- if not item then
- return false
- end
- local chestLimit = item.stackSize * 54
- local bSuccess = false
- for _, chest in ipairs(item.storage) do
- if chest.id == cId then
- chest.num = chest.num + num
- chest.storingNum = chest.storingNum - num
- bSuccess = true
- break
- end
- end
- if bSuccess then
- SDM.saveItem(item)
- end
- return bSuccess
- end
- function SDM.subStoredItem(itemName, cId, num)
- local item = SDM.findItem(itemName)
- if not item then
- return false
- end
- local chestLimit = item.stackSize * 54
- local removedChests = {}
- local bSuccess = false
- for i, chest in ipairs(item.storage) do
- if chest.id == cId then
- chest.num = chest.num - num
- chest.extractingNum = chest.extractingNum - num
- bSuccess = true
- if chest.num == 0 and chest.extractingNum == 0 and chest.storingNum == 0 then
- table.insert(removedChests, 1, i)
- SDM.changeChestStatus(chest.id, false)
- end
- break
- end
- end
- for _, indx in ipairs(removedChests) do
- table.remove(item.storage, indx)
- end
- if bSuccess then
- SDM.saveItem(item)
- end
- return bSuccess
- end
- -----------------------------------------------------------------------------------
- -- getStoredItemNumbers - Returns number of items currently stored
- -- + Detailed table about incoming, outgoing and total
- -- amount in System
- -----------------------------------------------------------------------------------
- function SDM.getStoredItemNumbers(itemName)
- local item = SDM.findItem(itemName)
- if not item then
- return 0
- end
- local av = 0
- local tot = 0
- local inc = 0
- local out = 0
- for i, chest in ipairs(item.storage) do
- av = av + chest.num - chest.extractingNum
- tot = tot + chest.num + chest.storingNum
- inc = inc + chest.storingNum
- out = out + chest.extractingNum
- end
- return av, {tot = tot, inc = inc, out = out}
- end
- -----------------------------------------------------------------------------------
- -- isFullyInitialized - Checks if system is already initialized
- -----------------------------------------------------------------------------------
- function SDM.isFullyInitialized()
- local initData = SDM.getInitData()
- if initData and #textutils.serialize(initData) == 2 then return false end
- local varList = {
- "dirSection", "dirTopBelt", "dirBottomBelt", "initChestCoord", "yModulo",
- "chestRowMod", "chestColMod", "elevPos", "sectionMod", "leftSecMod",
- "rightSecMod", "lowBeltY", "upBeltY", "diskOutCoord", "diskFinishCoord",
- "entryModulo", "exitModulo", "pickaxeChestCoord", "chestOutCoord",
- "bLeftStart", "colBlockNum", "blockHeight", "beltPos", "chestRefuelCoord"
- }
- for _, var in ipairs(varList) do
- if initData[var] == nil then return false end
- end
- return true
- end
- -----------------------------------------------------------------------------------
- -- getInitData - Returns initialized data
- -- setInitData - Saves (overwrites) initialized data
- -----------------------------------------------------------------------------------
- function SDM.getInitData()
- local disk, diskInfo = SDM.getDisk("init")
- if not disk then return nil end
- local path = fs.combine(diskInfo.path, "Init_Data")
- local initData = FM.loadTable(path)
- return initData
- end
- function SDM.setInitData(initData)
- local disk, diskInfo = SDM.getDisk("init")
- if not disk then return nil end
- local path = fs.combine(diskInfo.path, "Init_Data")
- FM.saveTable(initData, path, true)
- return true
- end
- function SDM.getIncomingItem()
- local disk, diskInfo = SDM.getDisk("incoming")
- if not disk then
- disk, diskInfo = SDM.addDisk("incoming")
- end
- local path = fs.combine(diskInfo.path, "Incoming_Task")
- local incomingItem = FM.loadTable(path)
- return incomingItem
- end
- function SDM.setIncomingItem(itemInfo)
- local disk, diskInfo = SDM.getDisk("incoming")
- if not disk then
- disk, diskInfo = SDM.addDisk("incoming")
- end
- local path = fs.combine(diskInfo.path, "Incoming_Task")
- FM.saveTable(itemInfo, path, true)
- return true
- end
- function SDM.getOutgoingTasks()
- local disk, diskInfo = SDM.getDisk("outgoing")
- if not disk then return nil end
- local path = fs.combine(diskInfo.path, "Outgoing_Tasks")
- local outgoingTaskList = FM.loadTable(path)
- return outgoingTaskList
- end
- function SDM.setOutgoingTasks(tasks)
- local disk, diskInfo = SDM.getDisk("outgoing")
- if not disk then return nil end
- local path = fs.combine(diskInfo.path, "Outgoing_Tasks")
- FM.saveTable(tasks, path, true)
- return true
- end
- function SDM.getFinishedTask()
- local disk, diskInfo = SDM.getDisk("finished")
- if not disk then return nil end
- local path = fs.combine(diskInfo.path, "Finished_Tasks")
- local finishedTaskList = FM.loadTable(path)
- return finishedTaskList
- end
- function SDM.setFinishedTask(task)
- task.status = nil
- local disk, diskInfo = SDM.getDisk("finished")
- if not disk then return nil end
- local path = fs.combine(diskInfo.path, "Finished_Tasks")
- FM.saveTable(task, path, true)
- return true
- end
- function SDM.setRequestedItem(itemInfo)
- local disk, diskInfo = SDM.getDisk("user")
- if not disk then
- disk, diskInfo = SDM.addDisk("user")
- end
- local path = fs.combine(diskInfo.path, "Request_Task")
- FM.saveTable(itemInfo, path, true)
- return true
- end
- function SDM.getRequestedItem()
- local disk, diskInfo = SDM.getDisk("user")
- if not disk then
- disk, diskInfo = SDM.addDisk("user")
- end
- local path = fs.combine(diskInfo.path, "Request_Task")
- local requestedItem = FM.loadTable(path)
- return requestedItem
- end
- function SDM.setChestPreTask(taskInfo)
- local disk, diskInfo = SDM.getDisk("settings")
- if not disk then
- disk, diskInfo = SDM.addDisk("settings")
- end
- local SAVE_FILE = "chest_init"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- FM.saveTable(taskInfo, path, true)
- end
- function SDM.getChestPreTask()
- local disk, diskInfo = SDM.getDisk("settings")
- if not disk then
- disk, diskInfo = SDM.addDisk("settings")
- end
- local SAVE_FILE = "chest_init"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- local taskInfo = FM.loadTable(path)
- return taskInfo
- end
- function SDM.setChestBlockCoords(coords)
- local disk, diskInfo = SDM.getDisk("settings")
- if not disk then
- disk, diskInfo = SDM.addDisk("settings")
- end
- local SAVE_FILE = "ChestBlockCoords"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- FM.saveTable(coords, path, true)
- end
- function SDM.getChestBlockCoords()
- local disk, diskInfo = SDM.getDisk("settings")
- if not disk then
- disk, diskInfo = SDM.addDisk("settings")
- end
- local SAVE_FILE = "ChestBlockCoords"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- return FM.loadTable(path)
- end
- function SDM.setInvCheckPreTask(taskInfo)
- local disk, diskInfo = SDM.getDisk("settings")
- if not disk then
- disk, diskInfo = SDM.addDisk("settings")
- end
- local SAVE_FILE = "inv_check"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- FM.saveTable(taskInfo, path, true)
- end
- function SDM.getInvCheckPreTask()
- local disk, diskInfo = SDM.getDisk("settings")
- if not disk then
- disk, diskInfo = SDM.addDisk("settings")
- end
- local SAVE_FILE = "inv_check"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- return FM.loadTable(path)
- end
- function SDM.getCompletedTasks()
- local disk, diskInfo = SDM.getDisk("protocoll")
- if not disk then
- disk, diskInfo = SDM.addDisk("protocoll")
- end
- local SAVE_FILE = "Completed_Tasks"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- return FM.loadTable(path)
- end
- function SDM.setCompletedTasks(taskInfo)
- local disk, diskInfo = SDM.getDisk("protocoll")
- if not disk then
- disk, diskInfo = SDM.addDisk("protocoll")
- end
- local SAVE_FILE = "Completed_Tasks"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- FM.saveTable(taskInfo, path, true)
- end
- function SDM.getCurrentTasks()
- local disk, diskInfo = SDM.getDisk("protocoll")
- if not disk then
- disk, diskInfo = SDM.addDisk("protocoll")
- end
- local SAVE_FILE = "Current_Tasks"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- return FM.loadTable(path)
- end
- function SDM.setCurrentTasks(taskInfo)
- local disk, diskInfo = SDM.getDisk("protocoll")
- if not disk then
- disk, diskInfo = SDM.addDisk("protocoll")
- end
- local SAVE_FILE = "Current_Tasks"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- FM.saveTable(taskInfo, path, true)
- end
- function SDM.saveCurrentTask(taskInfo)
- local disk, diskInfo = SDM.getDisk("protocoll")
- if not disk then
- disk, diskInfo = SDM.addDisk("protocoll")
- end
- local SAVE_FILE = "Current_Tasks"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- local taskData = SDM.getCurrentTasks()
- table.insert(taskData, taskInfo)
- FM.saveTable(taskData, path, true)
- end
- function SDM.saveTaskId(id)
- local disk, diskInfo = SDM.getDisk("settings")
- if not disk then
- disk, diskInfo = SDM.addDisk("settings")
- end
- local SAVE_FILE = "Last_Task_ID"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- FM.saveTable({id}, path)
- end
- function SDM.loadTaskId()
- local disk, diskInfo = SDM.getDisk("settings")
- if not disk then
- disk, diskInfo = SDM.addDisk("settings")
- end
- local SAVE_FILE = "Last_Task_ID"
- local path = fs.combine(diskInfo.path, SAVE_FILE)
- local idList = FM.loadTable(path)
- if idList[1] then return idList[1] end
- return 0
- end
- function SDM.moveToOutgoing_SE()
- filename = "SE"
- local SE_Data = FM.loadFile(filename)
- local disk, diskInfo = SDM.getDisk("outgoing")
- if not disk then return nil end
- local path = fs.combine(diskInfo.path, filename)
- FM.saveFile(SE_Data, path)
- end
- SDM.TURTLE = {}
- function SDM.TURTLE.update_SE()
- filename = "SE"
- local SE_old = FM.loadFile(filename)
- local disk, diskInfo = SDM.getDisk("outgoing")
- if not disk then return nil end
- local path = fs.combine(diskInfo.path, filename)
- if not disk then return false end
- local SE_new = FM.loadFile(path)
- if not SE_new then return false end
- if SE_new ~= SE_old then
- FM.saveFile(SE_new, filename)
- return true
- end
- return false
- end
- -----------------------------------------------------------------------------------
- -- loadTurtleTask - Turtle loads actual task
- -- saveTurtleTask - Turtle saves task
- -----------------------------------------------------------------------------------
- function SDM.TURTLE.loadTurtleTask()
- return FM.loadTable("Turtle_Task")
- end
- function SDM.TURTLE.saveTurtleTask(task)
- if task and #textutils.serialize(task) > 2 and not task.status then
- task.status = 1
- end
- FM.saveTable(task, "Turtle_Task", true)
- end
- -----------------------------------------------------------------------------------
- -- loadInitData - Loads initData from local file or disk file
- -- - Saves it locally if loaded from disk
- -----------------------------------------------------------------------------------
- function SDM.TURTLE.loadInitData()
- local SAVE_FILE = "Init_Data"
- local initData = FM.loadTable(SAVE_FILE)
- if initData and #textutils.serialize(initData) == 2 then
- initData = SDM.getInitData()
- FM.saveTable(initData, SAVE_FILE, true)
- end
- if initData and #textutils.serialize(initData) == 2 then
- return nil
- end
- return initData
- end
- -----------------------------------------------------------------------------------
- -- getNextOutgoingTask - Turtle takes first Task
- -----------------------------------------------------------------------------------
- function SDM.TURTLE.getNextOutgoingTask()
- local taskList = SDM.getOutgoingTasks()
- local task = table.remove(taskList, 1)
- SDM.TURTLE.saveTurtleTask(task)
- SDM.setOutgoingTasks(taskList)
- end
- function SDM.TURTLE.setQueueState(bQueue)
- local SAVE_FILE = "Queue_State"
- FM.saveTable({bQueue}, SAVE_FILE)
- end
- function SDM.TURTLE.getQueueState()
- local SAVE_FILE = "Queue_State"
- local loadedList = FM.loadTable(SAVE_FILE)
- if type(loadedList) == "table" and loadedList[1] and type(loadedList[1] == "boolean") then
- return loadedList[1]
- end
- return false
- end
- return SDM
Advertisement
Add Comment
Please, Sign In to add comment