Advertisement
towwey

roomdig

Jan 14th, 2025 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. -----------------------------------
  2. -- SINGLE-LAYER SPIRAL DIG
  3. -- by "Outside→In" Snail Method
  4. -----------------------------------
  5.  
  6. -- We'll keep track of a local (x, y) position,
  7. --   x increasing "forward," y increasing "to the left" if we turn left
  8. -- We'll also track an orientation integer 0..3:
  9. --   0 = facing +x, 1 = +y, 2 = -x, 3 = -y
  10.  
  11. local xPos, yPos = 0, 0
  12. local orient = 0   -- 0 = +x, 1 = +y, 2 = -x, 3 = -y
  13.  
  14. -- We'll define lookup tables so when we move forward,
  15. -- we know how xPos,yPos changes for each orientation.
  16. local dx = { [0] =  1, [1] =  0, [2] = -1, [3] =  0 }
  17. local dy = { [0] =  0, [1] =  1, [2] =  0, [3] = -1 }
  18.  
  19. -----------------------------------
  20. -- 1) Prompt for length & width
  21. -----------------------------------
  22. print("SINGLE-LAYER SPIRAL DIG")
  23. print("(No fancy features, just a snail path on one layer)")
  24. io.write("Enter the room LENGTH (forward): ")
  25. local length = tonumber(io.read())
  26. io.write("Enter the room WIDTH (perpendicular): ")
  27. local width  = tonumber(io.read())
  28.  
  29. print("OK, digging a spiral across a " .. length .. "x" .. width .. " area...")
  30.  
  31. -----------------------------------
  32. -- 2) Basic digForward function
  33. -----------------------------------
  34. local function digForward()
  35.     -- Keep breaking blocks in front until clear
  36.     while turtle.detect() do
  37.         turtle.dig()
  38.         sleep(0.2)  -- let gravel/sand settle
  39.     end
  40.     turtle.forward()
  41.  
  42.     -- Update our local xPos, yPos based on orientation
  43.     xPos = xPos + dx[orient]
  44.     yPos = yPos + dy[orient]
  45. end
  46.  
  47. -----------------------------------
  48. -- 3) Turn left / right
  49. -----------------------------------
  50. local function turnLeft()
  51.     turtle.turnLeft()
  52.     orient = (orient + 1) % 4
  53. end
  54.  
  55. local function turnRight()
  56.     turtle.turnRight()
  57.     orient = (orient + 3) % 4
  58. end
  59.  
  60. -----------------------------------
  61. -- 4) Move n steps forward, digging as we go
  62. -----------------------------------
  63. local function moveSteps(n)
  64.     for i=1, n do
  65.         digForward()
  66.     end
  67. end
  68.  
  69. -----------------------------------
  70. -- 5) The SPIRAL function (for 1 layer)
  71. --    This is the classic snail algorithm:
  72. --       while (length>0 and width>0) do
  73. --         1) move (length-1), turnLeft
  74. --         2) move (width -1), turnLeft
  75. --         length--, width--
  76. --         [repeat again if still length>0 & width>0]
  77. -----------------------------------
  78. local function spiralSingleLayer(L, W)
  79.     local horiz = L
  80.     local vert  = W
  81.  
  82.     while (horiz > 0 and vert > 0) do
  83.         -- Pass 1
  84.         moveSteps(horiz - 1)
  85.         turnLeft()
  86.         moveSteps(vert - 1)
  87.         turnLeft()
  88.  
  89.         horiz = horiz - 1
  90.         vert  = vert - 1
  91.  
  92.         -- if after shrinking, we've run out, break
  93.         if horiz <= 0 or vert <= 0 then
  94.             break
  95.         end
  96.  
  97.         -- Pass 2
  98.         moveSteps(horiz - 1)
  99.         turnLeft()
  100.         moveSteps(vert - 1)
  101.         turnLeft()
  102.  
  103.         horiz = horiz - 1
  104.         vert  = vert - 1
  105.     end
  106. end
  107.  
  108. -----------------------------------
  109. -- 6) Run the spiral dig
  110. -----------------------------------
  111. spiralSingleLayer(length, width)
  112.  
  113. print("Done! The turtle is near the center of that layer.")
  114. print("xPos="..xPos..", yPos="..yPos.." in local coords, orientation="..orient)
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement