Blackhome

TaskCreatorEvaluator

Jan 29th, 2026 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.00 KB | Gaming | 0 0
  1.     -- pastebin get Q4y1iUEd TaskCreatorEvaluator
  2.     SDM = require("SDM")
  3.     CVF = require("CVF")
  4.  
  5.     TCE = {}
  6.  
  7.  
  8.     local function createBlockRowCheck(cBottom)
  9.         local taskId = SDM.loadTaskId()
  10.         local task = {
  11.                 id = taskId + 1,
  12.                 taskType = "check chests",
  13.                 chests = {}
  14.             }
  15.         for dy = 0, 4 do
  16.             local newCoords = {x = cBottom.x,  y = cBottom.y +  dy, z = cBottom.z}
  17.             cId = SDM.getChestId(newCoords)
  18.             if not cId then
  19.                 cId = SDM.createSingleChest(newCoords)
  20.             end
  21.  
  22.             local chest = {id = cId, coords = newCoords, item = {}, checkState=0}
  23.             table.insert(task.chests, chest)
  24.         end
  25.         SDM.saveTaskId(taskId + 1)
  26.  
  27.         return task
  28.     end
  29.  
  30.     local function protocollFinishedTask(finTask)
  31.         local currentTasks = SDM.getCurrentTasks()
  32.         local idx = 0
  33.         for i, curTask in ipairs(currentTasks) do
  34.             if curTask.id == finTask.id then
  35.                 idx = i
  36.             end
  37.         end
  38.         if idx > 0 then
  39.             table.remove(currentTasks, idx)
  40.             SDM.setCurrentTasks(currentTasks)
  41.         end
  42.  
  43.         local completedTasks = SDM.getCompletedTasks()
  44.         if #completedTasks >= 50 then
  45.             table.remove(completedTasks, 1)
  46.         end
  47.         table.insert(completedTasks, finTask)
  48.         SDM.setCompletedTasks(completedTasks)
  49.     end
  50.  
  51.     local function evaluateChestCreation(task)
  52.         local initData = SDM.getInitData()
  53.         SDM.createChestBlock(task.coords, initData.dirSection)
  54.  
  55.         protocollFinishedTask(task)
  56.     end
  57.     local function evaluateIncomingTask(task)
  58.         SDM.addStoredItem(task.info.name, task.chestId, task.info.number)
  59.         protocollFinishedTask(task)
  60.     end
  61.     local function evaluateRequestTask(task)
  62.         SDM.subStoredItem(task.info.name, task.chestId, task.info.number)
  63.         protocollFinishedTask(task)
  64.     end
  65.     local function evaluateChestCheckTask(task)
  66.         for _, chest in ipairs(task.chests) do
  67.             -- 1. Global sicherstellen, dass diese Chest-ID nirgendwo mehr existiert
  68.             local oldItem = SDM.findItemByChestId(chest.id)
  69.             if oldItem then
  70.                 SDM.removeChestFromItem(chest.id, oldItem)
  71.             end
  72.  
  73.             -- 2. Nur fortfahren, wenn ein gültiges Item in der Chest liegt
  74.             if chest.item and #textutils.serialize(chest.item) > 2 and chest.item.number > 0 then
  75.                 local newItem = SDM.findItem(chest.item)
  76.                 local newChest = {id = chest.id, num = chest.item.number, storingNum = 0, extractingNum = 0}
  77.  
  78.                 if newItem then
  79.                     -- Sicherstellen, dass die Chest-ID auch im "neuen" Item-Slot nicht doppelt ist
  80.                     SDM.removeChestFromItem(chest.id, newItem)
  81.                     SDM.addChestToItem(newChest, newItem)
  82.                 else
  83.                     -- Neues Item registrieren
  84.                     local itemData = {
  85.                         name = chest.item.name,
  86.                         dName = chest.item.dName,
  87.                         stackSize = chest.item.stackSize,
  88.                         storage = { newChest }
  89.                     }
  90.                     SDM.saveItem(itemData)
  91.                     SDM.changeChestStatus(chest.id, true)
  92.                 end
  93.             end
  94.         end
  95.         protocollFinishedTask(task)
  96.     end
  97.  
  98.  
  99.     function TCE.createFromRequest()
  100.         local itemInfo = SDM.getRequestedItem()
  101.         if itemInfo and #textutils.serialize(itemInfo) == 2 then return false end
  102.  
  103.         local chests = SDM.addOutputItem(itemInfo.name, itemInfo.number)
  104.         local taskList = {}
  105.  
  106.         for _, chest in ipairs(chests) do
  107.             local maxAmount = itemInfo.stackSize * 16
  108.            
  109.             local numRemaining = chest.num
  110.             while numRemaining > 0 do
  111.                 local chestCoords = SDM.getChestCoords(chest.id)
  112.                 local taskId = SDM.loadTaskId()
  113.                 local numTransport = math.min(numRemaining, maxAmount)
  114.                 local info = {
  115.                     name = itemInfo.name, dName = itemInfo.dName,
  116.                     stackSize = itemInfo.stackSize, number = numTransport
  117.                 }
  118.                 numRemaining = numRemaining - numTransport
  119.                 local task = {
  120.                     id = taskId + 1,
  121.                     taskType = "requested",
  122.                     info = info,
  123.                     chestId = chest.id,
  124.                     coords = chestCoords,
  125.                 }
  126.                 SDM.saveTaskId(taskId + 1)
  127.                 table.insert(taskList, task)
  128.             end
  129.         end
  130.         SDM.setOutgoingTasks(taskList)
  131.         SDM.setRequestedItem({})
  132.         for _, task in ipairs(taskList) do
  133.             SDM.saveCurrentTask(task)
  134.         end
  135.         return true
  136.     end
  137.  
  138.     function TCE.createFromIncoming()
  139.         local itemInfo = SDM.getIncomingItem()
  140.         if itemInfo and #textutils.serialize(itemInfo) == 2 then return false end
  141.  
  142.         local chests = SDM.addInputItem(itemInfo)
  143.         local taskList = {}
  144.  
  145.         for _, chest in ipairs(chests) do
  146.             local taskId = SDM.loadTaskId()
  147.             local chestCoords = SDM.getChestCoords(chest.id)
  148.             local info = {
  149.                 name = itemInfo.name, dName = itemInfo.dName,
  150.                 stackSize = itemInfo.stackSize, number = chest.num
  151.             }
  152.             local task = {
  153.                 id = taskId + 1,
  154.                 taskType = "incoming",
  155.                 info = info,
  156.                 chestId = chest.id,
  157.                 coords = chestCoords,
  158.             }
  159.             SDM.saveTaskId(taskId + 1)
  160.             table.insert(taskList, task)
  161.         end
  162.         SDM.setOutgoingTasks(taskList)
  163.         SDM.setIncomingItem({})
  164.         for _, task in ipairs(taskList) do
  165.             SDM.saveCurrentTask(task)
  166.         end
  167.         return true
  168.     end
  169.  
  170.     function TCE.createChest()
  171.         if not SDM.isFullyInitialized() then print("System not Initialized") return false end
  172.         local preTask = SDM.getChestPreTask()
  173.         if preTask and #textutils.serialize(preTask) == 2 then return false end
  174.  
  175.         local taskType = preTask.taskType
  176.  
  177.         local initData = SDM.getInitData()
  178.  
  179.         local taskId = SDM.loadTaskId()
  180.  
  181.         local task = {
  182.             id = taskId + 1,
  183.             taskType = taskType,
  184.             coords = preTask.coords,
  185.             bLeft = preTask.bLeft
  186.         }
  187.  
  188.         if taskType == "init chests" then
  189.             evaluateChestCreation(task)
  190.  
  191.             SDM.setChestPreTask({})
  192.             SDM.saveTaskId(taskId + 1)
  193.  
  194.             return true
  195.         elseif taskType ~= "create chests" then
  196.             return false
  197.         end
  198.  
  199.         SDM.saveTaskId(taskId + 1)
  200.  
  201.         local taskList = {}
  202.         table.insert(taskList, task)
  203.  
  204.         SDM.setOutgoingTasks(taskList)
  205.         SDM.setChestPreTask({})
  206.         SDM.saveCurrentTask(task)
  207.  
  208.         return true
  209.     end
  210.    
  211.     function TCE.createChestCheck()
  212.         if not SDM.isFullyInitialized() then print("System not Initialized") return false end
  213.         local preTask = SDM.getInvCheckPreTask()
  214.         if preTask and #textutils.serialize(preTask) == 2 then return false end
  215.  
  216.         local taskType = preTask.taskType
  217.  
  218.         local blockList = preTask.blockList
  219.  
  220.         if preTask.bAllBlocks then
  221.             blockList = SDM.getChestBlockCoords()
  222.         end
  223.  
  224.         local initData = SDM.getInitData()
  225.  
  226.         for strBlock, Coords in pairs(blockList) do
  227.             local taskOut = SDM.getOutgoingTasks()
  228.             while not (taskOut and #textutils.serialize(taskOut) == 2) do
  229.                 TCE.evaluateTasks()
  230.                 sleep(0.5)
  231.                 taskOut = SDM.getOutgoingTasks()
  232.             end
  233.             local taskList = {}
  234.             for df = 0, 9 do
  235.                 local bCoord = CVF.add(Coords, CVF.mult(df, initData.dirSection))
  236.                 local task = createBlockRowCheck(bCoord)
  237.                 table.insert(taskList, task)
  238.             end
  239.             SDM.setOutgoingTasks(taskList)
  240.             for _, task in ipairs(taskList) do
  241.                 SDM.saveCurrentTask(task)
  242.             end
  243.         end
  244.  
  245.         SDM.setInvCheckPreTask({})
  246.  
  247.         return true
  248.     end
  249.  
  250.     function TCE.evaluateTasks()
  251.         local finishedTask = SDM.getFinishedTask()
  252.         if finishedTask and #textutils.serialize(finishedTask) == 2 then return false end
  253.  
  254.         if finishedTask.taskType == "create chests" then
  255.             evaluateChestCreation(finishedTask)
  256.         elseif finishedTask.taskType == "incoming" then
  257.             evaluateIncomingTask(finishedTask)
  258.         elseif finishedTask.taskType == "requested" then
  259.             evaluateRequestTask(finishedTask)
  260.         elseif finishedTask.taskType == "check chests" then
  261.             evaluateChestCheckTask(finishedTask)
  262.         end
  263.  
  264.         SDM.setFinishedTask({})
  265.     end
  266.  
  267.     return TCE
Advertisement
Add Comment
Please, Sign In to add comment