Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get Q4y1iUEd TaskCreatorEvaluator
- SDM = require("SDM")
- CVF = require("CVF")
- TCE = {}
- local function createBlockRowCheck(cBottom)
- local taskId = SDM.loadTaskId()
- local task = {
- id = taskId + 1,
- taskType = "check chests",
- chests = {}
- }
- for dy = 0, 4 do
- local newCoords = {x = cBottom.x, y = cBottom.y + dy, z = cBottom.z}
- cId = SDM.getChestId(newCoords)
- if not cId then
- cId = SDM.createSingleChest(newCoords)
- end
- local chest = {id = cId, coords = newCoords, item = {}, checkState=0}
- table.insert(task.chests, chest)
- end
- SDM.saveTaskId(taskId + 1)
- return task
- end
- local function protocollFinishedTask(finTask)
- local currentTasks = SDM.getCurrentTasks()
- local idx = 0
- for i, curTask in ipairs(currentTasks) do
- if curTask.id == finTask.id then
- idx = i
- end
- end
- if idx > 0 then
- table.remove(currentTasks, idx)
- SDM.setCurrentTasks(currentTasks)
- end
- local completedTasks = SDM.getCompletedTasks()
- if #completedTasks >= 50 then
- table.remove(completedTasks, 1)
- end
- table.insert(completedTasks, finTask)
- SDM.setCompletedTasks(completedTasks)
- end
- local function evaluateChestCreation(task)
- local initData = SDM.getInitData()
- SDM.createChestBlock(task.coords, initData.dirSection)
- protocollFinishedTask(task)
- end
- local function evaluateIncomingTask(task)
- SDM.addStoredItem(task.info.name, task.chestId, task.info.number)
- protocollFinishedTask(task)
- end
- local function evaluateRequestTask(task)
- SDM.subStoredItem(task.info.name, task.chestId, task.info.number)
- protocollFinishedTask(task)
- end
- local function evaluateChestCheckTask(task)
- for _, chest in ipairs(task.chests) do
- -- 1. Global sicherstellen, dass diese Chest-ID nirgendwo mehr existiert
- local oldItem = SDM.findItemByChestId(chest.id)
- if oldItem then
- SDM.removeChestFromItem(chest.id, oldItem)
- end
- -- 2. Nur fortfahren, wenn ein gültiges Item in der Chest liegt
- if chest.item and #textutils.serialize(chest.item) > 2 and chest.item.number > 0 then
- local newItem = SDM.findItem(chest.item)
- local newChest = {id = chest.id, num = chest.item.number, storingNum = 0, extractingNum = 0}
- if newItem then
- -- Sicherstellen, dass die Chest-ID auch im "neuen" Item-Slot nicht doppelt ist
- SDM.removeChestFromItem(chest.id, newItem)
- SDM.addChestToItem(newChest, newItem)
- else
- -- Neues Item registrieren
- local itemData = {
- name = chest.item.name,
- dName = chest.item.dName,
- stackSize = chest.item.stackSize,
- storage = { newChest }
- }
- SDM.saveItem(itemData)
- SDM.changeChestStatus(chest.id, true)
- end
- end
- end
- protocollFinishedTask(task)
- end
- function TCE.createFromRequest()
- local itemInfo = SDM.getRequestedItem()
- if itemInfo and #textutils.serialize(itemInfo) == 2 then return false end
- local chests = SDM.addOutputItem(itemInfo.name, itemInfo.number)
- local taskList = {}
- for _, chest in ipairs(chests) do
- local maxAmount = itemInfo.stackSize * 16
- local numRemaining = chest.num
- while numRemaining > 0 do
- local chestCoords = SDM.getChestCoords(chest.id)
- local taskId = SDM.loadTaskId()
- local numTransport = math.min(numRemaining, maxAmount)
- local info = {
- name = itemInfo.name, dName = itemInfo.dName,
- stackSize = itemInfo.stackSize, number = numTransport
- }
- numRemaining = numRemaining - numTransport
- local task = {
- id = taskId + 1,
- taskType = "requested",
- info = info,
- chestId = chest.id,
- coords = chestCoords,
- }
- SDM.saveTaskId(taskId + 1)
- table.insert(taskList, task)
- end
- end
- SDM.setOutgoingTasks(taskList)
- SDM.setRequestedItem({})
- for _, task in ipairs(taskList) do
- SDM.saveCurrentTask(task)
- end
- return true
- end
- function TCE.createFromIncoming()
- local itemInfo = SDM.getIncomingItem()
- if itemInfo and #textutils.serialize(itemInfo) == 2 then return false end
- local chests = SDM.addInputItem(itemInfo)
- local taskList = {}
- for _, chest in ipairs(chests) do
- local taskId = SDM.loadTaskId()
- local chestCoords = SDM.getChestCoords(chest.id)
- local info = {
- name = itemInfo.name, dName = itemInfo.dName,
- stackSize = itemInfo.stackSize, number = chest.num
- }
- local task = {
- id = taskId + 1,
- taskType = "incoming",
- info = info,
- chestId = chest.id,
- coords = chestCoords,
- }
- SDM.saveTaskId(taskId + 1)
- table.insert(taskList, task)
- end
- SDM.setOutgoingTasks(taskList)
- SDM.setIncomingItem({})
- for _, task in ipairs(taskList) do
- SDM.saveCurrentTask(task)
- end
- return true
- end
- function TCE.createChest()
- if not SDM.isFullyInitialized() then print("System not Initialized") return false end
- local preTask = SDM.getChestPreTask()
- if preTask and #textutils.serialize(preTask) == 2 then return false end
- local taskType = preTask.taskType
- local initData = SDM.getInitData()
- local taskId = SDM.loadTaskId()
- local task = {
- id = taskId + 1,
- taskType = taskType,
- coords = preTask.coords,
- bLeft = preTask.bLeft
- }
- if taskType == "init chests" then
- evaluateChestCreation(task)
- SDM.setChestPreTask({})
- SDM.saveTaskId(taskId + 1)
- return true
- elseif taskType ~= "create chests" then
- return false
- end
- SDM.saveTaskId(taskId + 1)
- local taskList = {}
- table.insert(taskList, task)
- SDM.setOutgoingTasks(taskList)
- SDM.setChestPreTask({})
- SDM.saveCurrentTask(task)
- return true
- end
- function TCE.createChestCheck()
- if not SDM.isFullyInitialized() then print("System not Initialized") return false end
- local preTask = SDM.getInvCheckPreTask()
- if preTask and #textutils.serialize(preTask) == 2 then return false end
- local taskType = preTask.taskType
- local blockList = preTask.blockList
- if preTask.bAllBlocks then
- blockList = SDM.getChestBlockCoords()
- end
- local initData = SDM.getInitData()
- for strBlock, Coords in pairs(blockList) do
- local taskOut = SDM.getOutgoingTasks()
- while not (taskOut and #textutils.serialize(taskOut) == 2) do
- TCE.evaluateTasks()
- sleep(0.5)
- taskOut = SDM.getOutgoingTasks()
- end
- local taskList = {}
- for df = 0, 9 do
- local bCoord = CVF.add(Coords, CVF.mult(df, initData.dirSection))
- local task = createBlockRowCheck(bCoord)
- table.insert(taskList, task)
- end
- SDM.setOutgoingTasks(taskList)
- for _, task in ipairs(taskList) do
- SDM.saveCurrentTask(task)
- end
- end
- SDM.setInvCheckPreTask({})
- return true
- end
- function TCE.evaluateTasks()
- local finishedTask = SDM.getFinishedTask()
- if finishedTask and #textutils.serialize(finishedTask) == 2 then return false end
- if finishedTask.taskType == "create chests" then
- evaluateChestCreation(finishedTask)
- elseif finishedTask.taskType == "incoming" then
- evaluateIncomingTask(finishedTask)
- elseif finishedTask.taskType == "requested" then
- evaluateRequestTask(finishedTask)
- elseif finishedTask.taskType == "check chests" then
- evaluateChestCheckTask(finishedTask)
- end
- SDM.setFinishedTask({})
- end
- return TCE
Advertisement
Add Comment
Please, Sign In to add comment