morganamilo

Turtle Tunnel v1.0

May 1st, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local width = tonumber(args[1])
  4. local height = tonumber(args[2])
  5. local depth = tonumber(args [3])
  6.  
  7. if table.getn(args) < 3 then
  8.   print('Usage: ' .. shell.getRunningProgram() .. ' <width> <height> <depth>')
  9.   error()
  10. end
  11.  
  12. if width == nil or height == nil or depth == nil then
  13.   print('Dimensions must be numbers')
  14.   error()
  15. end
  16.  
  17. if width ~= math.floor(width) or height ~= math.floor(height) or depth ~= math.floor(depth) then
  18.   print('Dimensions must be whole numbers')
  19.   error()
  20. end
  21.  
  22. if width < 1 or height < 1 or depth < 1 then
  23.   print('All dimensions must be at least 1')
  24.   error()
  25. end
  26.  
  27. local x = 0
  28. local y = 0 --in minecraft y is really z but the turtles coords are relative to itself so it doesnt matter
  29. local z = 0
  30. local orient = 1
  31.  
  32. local xDiff = {0, 1, 0, -1}
  33. local yDiff = {1, 0, -1, 0}
  34.  
  35. function forward()
  36.   x = x + xDiff[orient + 1]
  37.   y = y + yDiff[orient + 1]
  38.   repeat
  39.     moved = turtle.forward()
  40.     if moved == false then
  41.       turtle.dig()
  42.     end
  43.   until moved == true
  44. end
  45.  
  46. function left()
  47.   orient = orient - 1
  48.   orient = (orient % 4)
  49.   turtle.turnLeft()    
  50. end
  51.  
  52. function right()
  53.   orient = orient + 1
  54.   orient = (orient % 4)
  55.   turtle.turnRight()
  56. end
  57.  
  58. function up()
  59.   turtle.up()
  60.   z = z + 1
  61. end
  62.  
  63. function down()
  64.   turtle.down()
  65.   z = z - 1
  66. end
  67.  
  68. function getCorner() -- 1, 2, 3, 4 = topL, topR, bottomL, bottomR
  69.   if y ~= 0 and z ~= 0 then return 1 end
  70.   if y == 0 and z ~= 0 then return 2 end
  71.   if y ~= 0 and z == 0 then return 3 end
  72.   if y == 0 and z == 0 then return 4 end
  73. end
  74.  
  75.  
  76. function digRow()
  77.   if width > 1 then
  78.     repeat
  79.         turtle.dig()
  80.          print(z ~= depth)
  81.          print(getcorner ~= 4)
  82.          if z ~= depth then forward() end
  83.      until y == width - 1 or y == 0
  84.   end
  85. end
  86.  
  87. function digLayer()
  88.  if height > 1 then
  89.   corner = getCorner()
  90.  
  91.   if corner == 1 or corner == 3 then
  92.     right()
  93.   else
  94.     left()
  95.   end
  96.  
  97.   repeat
  98.     digRow()
  99.     left()
  100.     left()
  101.  
  102.     if corner > 2 then
  103.       if z ~= height then
  104.         turtle.digUp()
  105.         up()
  106.       end
  107.     end
  108.    
  109.     if corner < 3 then      
  110.       if z ~= 0 then
  111.         turtle.digDown()
  112.         down()
  113.       end  
  114.     end
  115.   until z == 0 or z == height -1
  116.  
  117.   digRow()
  118.  
  119.   while orient ~= 1 do
  120.     if orient == 2 then left()
  121.     else right()
  122.     end
  123.   end
  124. end
  125. end
  126.  
  127. function digTunnel()
  128.  for n = 1, depth do
  129.    digLayer()
  130.  end
  131. end
  132.      
  133. digTunnel()
Advertisement
Add Comment
Please, Sign In to add comment