Advertisement
Blackhome

StorageTurtle

Jun 5th, 2025 (edited)
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.57 KB | None | 0 0
  1. -- === Turtle Navigation for Warehouse System ===
  2. -- Assumes turtle starts on init disk, facing toward the system
  3. -- Uses GPS for position, init.txt for orientation info
  4.  
  5. -- === Utility ===
  6. local function readInitData()
  7.     local initDiskPath
  8.     for _, side in ipairs(peripheral.getNames()) do
  9.         if peripheral.getType(side) == "drive" then
  10.             local disk = peripheral.wrap(side)
  11.             if disk.getDiskLabel() == "Turtle Initializing" then
  12.                 initDiskPath = disk.getMountPath()
  13.                 break
  14.             end
  15.         end
  16.     end
  17.     if not initDiskPath then error("Init disk not found") end
  18.  
  19.     local initFile = fs.open(fs.combine(initDiskPath, "init.txt"), "r")
  20.     local orientation = initFile.readLine():match("orientation=(%a+)")
  21.     local direction = tonumber(initFile.readLine():match("direction=(%-?%d+)"))
  22.     local modulo = tonumber(initFile.readLine():match("modulo=(%d+)"))
  23.     initFile.close()
  24.  
  25.     return orientation, direction, modulo
  26. end
  27.  
  28. local function getDirectionVectors(orientation, direction)
  29.     if orientation == "x" then
  30.         return {
  31.             forward = {x = direction, y = 0, z = 0},
  32.             right   = {x = 0, y = 0, z = direction},
  33.             up      = {x = 0, y = 1, z = 0}
  34.         }
  35.     elseif orientation == "z" then
  36.         return {
  37.             forward = {x = 0, y = 0, z = direction},
  38.             right   = {x = -direction, y = 0, z = 0},
  39.             up      = {x = 0, y = 1, z = 0}
  40.         }
  41.     else
  42.         error("Invalid orientation")
  43.     end
  44. end
  45.  
  46. local function getStartPosition()
  47.     local x, y, z = gps.locate()
  48.     if not x then error("GPS not available") end
  49.     return {x = math.floor(x), y = math.floor(y), z = math.floor(z)}
  50. end
  51.  
  52. local function add(pos, delta)
  53.     return {
  54.         x = pos.x + delta.x,
  55.         y = pos.y + delta.y,
  56.         z = pos.z + delta.z
  57.     }
  58. end
  59.  
  60. -- === Facing and Rotation Management ===
  61. local facingVec
  62. local function updateFacingVec(vec)
  63.     facingVec = vec
  64. end
  65.  
  66. local function turnAndUpdate(dir)
  67.     if dir == "left" then
  68.         turtle.turnLeft()
  69.         facingVec = {x = facingVec.z, y = 0, z = -facingVec.x}
  70.     elseif dir == "right" then
  71.         turtle.turnRight()
  72.         facingVec = {x = -facingVec.z, y = 0, z = facingVec.x}
  73.     elseif dir == "around" then
  74.         turtle.turnLeft()
  75.         turtle.turnLeft()
  76.         facingVec = {x = -facingVec.x, y = 0, z = -facingVec.z}
  77.     end
  78. end
  79.  
  80. local function vectorsEqual(a, b)
  81.     return a.x == b.x and a.z == b.z
  82. end
  83.  
  84. local function getTurnDirection(from, to)
  85.     if vectorsEqual(from, to) then return nil end
  86.     local left  = {x = from.z, y = 0, z = -from.x}
  87.     local right = {x = -from.z, y = 0, z = from.x}
  88.     local back  = {x = -from.x, y = 0, z = -from.z}
  89.     if vectorsEqual(to, left) then
  90.         return "left"
  91.     elseif vectorsEqual(to, right) then
  92.         return "right"
  93.     elseif vectorsEqual(to, back) then
  94.         return "around"
  95.     else
  96.         error("Invalid target direction")
  97.     end
  98. end
  99.  
  100. -- === Movement ===
  101. local function move(directionVec)
  102.     if directionVec.y > 0 then
  103.         while not turtle.up() do sleep(0.5) end
  104.     elseif directionVec.y < 0 then
  105.         while not turtle.down() do sleep(0.5) end
  106.     elseif directionVec.x ~= 0 or directionVec.z ~= 0 then
  107.         local turnDir = getTurnDirection(facingVec, directionVec)
  108.         if turnDir then
  109.             turnAndUpdate(turnDir)
  110.         end
  111.         while not turtle.forward() do sleep(0.5) end
  112.     end
  113. end
  114.  
  115. local function moveMultiple(vec, amount)
  116.     local step = (amount > 0 and 1 or -1)
  117.     for _ = 1, math.abs(amount) do
  118.         move({x = vec.x * step, y = vec.y * step, z = vec.z * step})
  119.     end
  120. end
  121.  
  122. -- === Initialization ===
  123. local orientation, direction, modulo = readInitData()
  124. local dirs = getDirectionVectors(orientation, direction)
  125. local relativeDirs = {
  126.     forward = dirs.forward,
  127.     right   = dirs.right,
  128.     up      = dirs.up,
  129.     down    = {x = -dirs.up.x, y = -dirs.up.y, z = -dirs.up.z},
  130.     left    = {x = -dirs.right.x, y = 0, z = -dirs.right.z},
  131.     back    = {x = -dirs.forward.x, y = 0, z = -dirs.forward.z},
  132. }
  133. updateFacingVec(relativeDirs.forward)
  134.  
  135. local startPos = getStartPosition()
  136. local finishPos = add(startPos, {
  137.     x = 2 * dirs.forward.x - 5 * dirs.right.x,
  138.     y = -1,
  139.     z = 2 * dirs.forward.z - 5 * dirs.right.z
  140. })
  141.  
  142. -- === Core Functions ===
  143. local function initialMoveToFinish()
  144.     moveMultiple(dirs.forward, 3)
  145.     moveMultiple(relativeDirs.left, 5)
  146.     moveMultiple(relativeDirs.down, 1)
  147.     moveMultiple(relativeDirs.back, 1)
  148.     turnAndUpdate("left")
  149. end
  150.  
  151. local function acceptTask()
  152.     print("[TASK] Accepting task...")
  153.     turnAndUpdate("left")
  154.     move(facingVec)
  155.     moveMultiple(relativeDirs.up, 1)
  156.     move(facingVec)
  157.     -- TODO: actual task logic
  158. end
  159.  
  160. local function travelToTask()
  161.     print("[STATE] Traveling to task...")
  162.     -- TODO
  163. end
  164.  
  165. local function performTask()
  166.     print("[STATE] Performing task...")
  167.     -- TODO
  168. end
  169.  
  170. local function returnToFinish()
  171.     print("[STATE] Returning to finish block...")
  172.     -- TODO
  173. end
  174.  
  175. -- === Queue Handling ===
  176. local function isTurtleBlock(data)
  177.     return data and type(data.name) == "string" and data.name:match("computercraft:turtle")
  178. end
  179.  
  180. local function enterQueue()
  181.     print("[QUEUE] Entering queue...")
  182.     moveMultiple(relativeDirs.left, 6)
  183.     local level = 0
  184.     while true do
  185.         turnAndUpdate("left")
  186.         local success, data = turtle.inspect()
  187.         turnAndUpdate("right")
  188.         if not (success and isTurtleBlock(data)) then
  189.             turnAndUpdate("left")
  190.             move(facingVec)
  191.             if level % 2 == 0 then
  192.                 turnAndUpdate("left")
  193.             else
  194.                 turnAndUpdate("right")
  195.             end
  196.             break
  197.         else
  198.             moveMultiple(relativeDirs.up, 1)
  199.             level = level + 1
  200.         end
  201.     end
  202.  
  203.     if level > 0 and level % 2 == 0 then
  204.         while turtle.forward() do
  205.             local belowSuccess, belowData = turtle.inspectDown()
  206.             if not (belowSuccess and isTurtleBlock(belowData)) then
  207.                 moveMultiple(relativeDirs.down, 1)
  208.                 turnAndUpdate("around")
  209.                 break
  210.             elseif turtle.detect() then
  211.                 break
  212.             end
  213.         end
  214.     end
  215. end
  216.  
  217. local function queueForTask()
  218.     print("[QUEUE] Advancing in queue...")
  219.     local lastWasDescent = false
  220.     while true do
  221.         local downSuccess, downData = turtle.inspectDown()
  222.         if downSuccess and downData.name == "computercraft:disk_drive" then
  223.             break
  224.         elseif not turtle.detect() then
  225.             while not turtle.forward() do sleep(0.5) end
  226.             lastWasDescent = false
  227.         else
  228.             local frontSuccess, frontData = turtle.inspect()
  229.             if frontSuccess and not isTurtleBlock(frontData) then
  230.                 local downAgain, downInfo = turtle.inspectDown()
  231.                 if not lastWasDescent and not (downAgain and isTurtleBlock(downInfo)) then
  232.                     moveMultiple(relativeDirs.down, 1)
  233.                     turnAndUpdate("around")
  234.                     lastWasDescent = true
  235.                 else
  236.                     sleep(0.5)
  237.                 end
  238.             else
  239.                 sleep(0.5)
  240.             end
  241.         end
  242.     end
  243.     print("Leaving Queue")
  244. end
  245.  
  246. -- === Runtime ===
  247. initialMoveToFinish()
  248.  
  249. while true do
  250.     enterQueue()
  251.     queueForTask()
  252.     acceptTask()
  253.     travelToTask()
  254.     performTask()
  255.     returnToFinish()
  256.     break           -- zum Testen
  257. end
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement