Blackhome

StorageTurtle

Jun 5th, 2025 (edited)
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.94 KB | Gaming | 0 0
  1.     -- pastebin get bSBgdCDF StorageTurtle
  2.  
  3.     TT = require("TT")
  4.     TURT = {}
  5.  
  6.     local function isTurtleBlock(data)
  7.         return data and type(data.name) == "string" and data.name:match("computercraft:turtle")
  8.     end
  9.  
  10.     local function getDirectionInQueue()
  11.         local initData = SDM.TURTLE.loadInitData()
  12.  
  13.         local posQueueLeft = CVF.sub(initData.diskFinishCoord, initData.dirSection)
  14.         local posQueueRight = CVF.add(posQueueLeft, CVF.mult(4, initData.dirTopBelt))
  15.  
  16.         posQueueLeft.y = POS.position.y
  17.         posQueueRight.y = POS.position.y
  18.  
  19.         if CVF.equalCoords(posQueueLeft, POS.position) then
  20.             local strBlocks = ""
  21.             for i = 1, 4 do
  22.                 local success, data = turtle.inspect()
  23.  
  24.                 if success and not isTurtleBlock(data) then
  25.                     if i < 4 or strBlocks ~= "1" then
  26.                         strBlocks = strBlocks .. tostring(i)
  27.                         if #strBlocks == 2 then
  28.                             POS.direction = {x=initData.dirBottomBelt.x, z=initData.dirBottomBelt.z}
  29.                             return true
  30.                         end
  31.                     else
  32.                         strBlocks = tostring(i) .. strBlocks
  33.                         if #strBlocks == 2 then
  34.                             POS.direction = {x = -initData.dirSection.x, z = -initData.dirSection.z}
  35.                             return true
  36.                         end
  37.                     end
  38.                 end
  39.                 BM.turnRight()
  40.             end
  41.             return false
  42.         elseif CVF.equalCoords(posQueueRight, POS.position) and not (posQueueLeft.y == initData.diskFinishCoord.y + 1) then
  43.             local strBlocks = ""
  44.             for i = 1, 4 do
  45.                 local success, data = turtle.inspect()
  46.  
  47.                 if success and not isTurtleBlock(data) then
  48.                     if i < 4 or strBlocks ~= "1" then
  49.                         strBlocks = strBlocks .. tostring(i)
  50.                         if #strBlocks == 2 then
  51.                             POS.direction = {x = -initData.dirSection.x, z = -initData.dirSection.z}
  52.                             return true
  53.                         end
  54.                     else
  55.                         strBlocks = tostring(i) .. strBlocks
  56.                         if #strBlocks == 2 then
  57.                             POS.direction = {x=initData.dirTopBelt.x, z=initData.dirTopBelt.z}
  58.                             return true
  59.                         end
  60.                     end
  61.                 end
  62.                 BM.turnRight()
  63.             end
  64.             return false
  65.         else
  66.             for i = 1, 4 do
  67.                 local success, data = turtle.inspect()
  68.                 if success and not isTurtleBlock(data) then
  69.                     POS.direction = {x = -initData.dirSection.x, z = -initData.dirSection.z}
  70.                     return true
  71.                 end
  72.                 BM.turnRight()
  73.             end
  74.         end
  75.     end
  76.  
  77.     local function getDirectionPerMovement()
  78.         local initData = SDM.TURTLE.loadInitData()
  79.         local bQueue = SDM.TURTLE.getQueueState()
  80.        
  81.         if bQueue then
  82.             if getDirectionInQueue() then return true end
  83.         end
  84.         if POS.isOnElevatorDown() then
  85.             local bModY = initData.lowBeltY % initData.blockHeight
  86.             while POS.position.y % initData.blockHeight ~= bModY do BM.down() end
  87.         elseif POS.isOnElevatorUp() then
  88.             local bModY = initData.lowBeltY % initData.blockHeight
  89.             while POS.position.y % initData.blockHeight ~= bModY do BM.up() end
  90.         end
  91.  
  92.         local cnt = 0
  93.         while not BM.forward() do
  94.             if cnt == 4 then
  95.                 return false
  96.             end
  97.             BM.turnLeft()
  98.             cnt = cnt + 1
  99.         end
  100.         if not POS.isInSystem() then
  101.             while not BM.back() do sleep(0.1) end
  102.         end
  103.         return true
  104.     end
  105.  
  106.  
  107.     function TURT.init()
  108.         local bInit, num = POS.init()
  109.         if not bInit and num == 3 then
  110.             if not getDirectionPerMovement() then
  111.                 print("Can't get direction")
  112.                 return false
  113.             end
  114.         elseif not bInit then
  115.             return false
  116.         end
  117.         return true
  118.     end
  119.  
  120.     function TURT.main()
  121.         local minFuelLevel = 2000
  122.         local initData = SDM.TURTLE.loadInitData()
  123.         local taskOutputCoord = initData.diskOutCoord
  124.         local finishCoord = initData.diskFinishCoord
  125.         local refuelCoord = initData.chestRefuelCoord
  126.  
  127.         taskOutputCoord.y = taskOutputCoord.y + 1
  128.         finishCoord.y = initData.diskFinishCoord.y + 1
  129.         refuelCoord.y = refuelCoord.y - 1
  130.  
  131.         while not TURT.init() do sleep(5) end
  132.        
  133.         refuelCoord[POS.sectionDir] = refuelCoord[POS.sectionDir] + initData.dirSection[POS.sectionDir]
  134.  
  135.         while true do
  136.             if CVF.equalCoords(taskOutputCoord, POS.position) then
  137.                 SDM.TURTLE.setQueueState(false)
  138.                 if SDM.TURTLE.update_SE() then
  139.                     os.reboot()
  140.                 end
  141.                 SDM.TURTLE.setQueueState(false)
  142.                 if turtle.getFuelLevel() < minFuelLevel then
  143.                     while not AM.goToCoord(refuelCoord) do
  144.                         while not POS.get_GPS_Position() do sleep(1) end
  145.                     end
  146.                     return true
  147.                 end
  148.             end
  149.             if SDM.TURTLE.getQueueState() then
  150.                 AM.queueForTask()
  151.             elseif CVF.equalCoords(taskOutputCoord, POS.position) then
  152.                 if SDM.TURTLE.update_SE() then
  153.                     os.reboot()
  154.                 end
  155.                 SDM.TURTLE.setQueueState(false)
  156.                 if turtle.getFuelLevel() < minFuelLevel then
  157.                     while not AM.goToCoord(refuelCoord) do
  158.                         while not POS.get_GPS_Position() do sleep(1) end
  159.                     end
  160.                     return true
  161.                 end
  162.                 while not TT.getTask() do sleep(0.5) end
  163.                 if SDM.TURTLE.update_SE() then
  164.                     os.reboot()
  165.                 end
  166.             elseif not TT.getTask() then
  167.                 while not AM.goToCoord(finishCoord) do
  168.                     while not POS.get_GPS_Position() do sleep(1) end
  169.                 end
  170.                 AM.joinQueue()
  171.             end
  172.  
  173.             if TT.getTask() then
  174.                 if TT.Task.taskType == "incoming" then
  175.                     TT.doIncomingTask()
  176.                 elseif TT.Task.taskType == "requested" then
  177.                     TT.doOutgoingTask()
  178.                 elseif TT.Task.taskType == "create chests" then
  179.                     TT.doCreateChest()
  180.                 elseif TT.Task.taskType == "check chests" then
  181.                     TT.doCheckChests()
  182.                 else
  183.  
  184.                 end
  185.             end
  186.  
  187.         end
  188.     end
  189.  
  190.     return TURT
  191.    
Advertisement
Add Comment
Please, Sign In to add comment