Advertisement
KelvinBouma

TurtleDig.lua

Aug 19th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.30 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local length = args[1]
  4. local depth = args[2]
  5. local width = args[3]
  6.  
  7. if width == nil then
  8.     error("Usage: dig <length> <depth> <width>")
  9. end
  10.  
  11. local direction = 0
  12. local goRight = false
  13.  
  14. local x, y, z = 1, 1, 1
  15.  
  16. local function move()
  17.     if turtle.forward() then
  18.         if direction == 0 then
  19.             x = x + 1
  20.         elseif direction == 1 then
  21.             z = z + 1
  22.         elseif direction == 2 then
  23.             x = x - 1
  24.         elseif direction == 3 then
  25.             z = z - 1
  26.         else
  27.             error("Direction value out of bounds")
  28.         end
  29.     else
  30.         turtle.dig()
  31.         return move()
  32.     end
  33. end
  34.  
  35. local function down()
  36.     if turtle.down() then
  37.         y = y + 1
  38.     else
  39.         turtle.digDown()
  40.         return down()
  41.     end
  42. end
  43.  
  44. local function left()
  45.     turtle.turnLeft()
  46.     direction = (direction + 1) % 4
  47. end
  48.  
  49. local function right()
  50.     turtle.turnRight()
  51.     direction = (direction - 1) % 4
  52. end
  53.  
  54. -- CC might stop loops from functioning w/o the inclusion of events
  55. local function unlock()
  56.     os.queueEvent("hai")
  57.     os.pullEvent()
  58. end
  59.  
  60. for i = 1, depth-1 do
  61.     for j = 1, width-1 do
  62.         for k = 1, length-1 do
  63.             move()
  64.  
  65.             unlock()
  66.         end
  67.  
  68.         if goRight then
  69.             right()
  70.             move()
  71.             right()
  72.         else
  73.             left()
  74.             move()
  75.             left()
  76.         end
  77.         goRight = not goRight
  78.  
  79.         unlock()
  80.     end
  81.  
  82.     down()
  83.     left()
  84.     left()
  85.     goRight = not goRight
  86.  
  87.     unlock()
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement