Advertisement
Guest User

Computercraft - Turtle Cylinder Digger

a guest
Jan 29th, 2013
3,404
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1. args = {...}
  2.  
  3. radius = 15
  4. height = 10
  5.  
  6. if tonumber(args[1]) ~= nil and tonumber(args[2]) ~= nil then
  7.     radius = args[1]
  8.     height = args[2]
  9. end
  10.  
  11. xCoord = 0
  12. zCoord = 0
  13. yCoord = 0
  14.  
  15. xStartRing = 0
  16. zStartRing = 0
  17.  
  18. orientation = 4
  19. orientations = {"north", "east", "south", "west"}
  20.  
  21. zDiff = {-1, 0, 1, 0}
  22. xDiff = {0, 1, 0, -1}
  23.  
  24.  
  25. function left()
  26.     orientation = orientation - 1
  27.     orientation = (orientation - 1) % 4
  28.     orientation = orientation + 1
  29.     turtle.turnLeft()
  30. end
  31.  
  32.  
  33.  
  34. function right()
  35.     orientation = orientation - 1
  36.     orientation = (orientation + 1) % 4
  37.     orientation = orientation + 1
  38.  
  39.     turtle.turnRight()
  40. end
  41.  
  42.  
  43.  
  44. function moveForward()
  45.     xCoord = xCoord + xDiff[orientation]  
  46.     zCoord = zCoord + zDiff[orientation]
  47.  
  48.     moved = false
  49.     while not(moved) do
  50.         turtle.dig()
  51.         moved = turtle.forward()
  52.     end
  53. end
  54.  
  55.  
  56.  
  57. function moveUp()
  58.     yCoord = yCoord + 1
  59.  
  60.     turtle.digUp()
  61.  
  62.     moved = false
  63.     while not(moved) do
  64.         moved = turtle.up()
  65.     end  
  66. end
  67.  
  68. function moveDown()
  69.  
  70.     yCoord = yCoord - 1
  71.     turtle.digDown()
  72.    
  73.     moved = false
  74.  
  75.     while not(moved) do
  76.         moved = turtle.down()
  77.     end
  78.  
  79. end
  80.  
  81. function look(direction)
  82.     while direction ~= orientations[orientation] do
  83.         right()
  84.     end
  85. end
  86.  
  87.  
  88. function round(n)
  89.     decimal = n - math.floor(n)
  90.     if decimal >= 0.5 then
  91.         return math.ceil(n)
  92.     else
  93.         return math.floor(n)
  94.     end
  95. end
  96.  
  97. function withinRadius(x, z)
  98.     return round( math.sqrt((x * x) + (z * z)) ) <= radius
  99. end
  100.  
  101.  
  102. function forwardWithinRadius()
  103.  
  104.     xNext = xCoord + xDiff[orientation]  
  105.     zNext = zCoord + zDiff[orientation]
  106.    
  107.     return withinRadius(xNext, zNext)
  108. end
  109.  
  110. function gotoEdge()
  111.     while forwardWithinRadius() do
  112.         moveForward()
  113.     end
  114.    
  115.     left()
  116.    
  117.     xStartRing = xCoord
  118.     zStartRing = zCoord
  119. end
  120.  
  121. function digRing()
  122.  
  123.     repeat
  124.        
  125.         while forwardWithinRadius() do
  126.             moveForward()
  127.         end
  128.         while not(forwardWithinRadius()) do
  129.             left()
  130.         end
  131.         moveForward()
  132.         right()
  133.    
  134.     until xCoord == xStartRing and zCoord == zStartRing
  135.    
  136.     left()
  137.     left()
  138.     for i = 1,radius do
  139.         moveForward()
  140.     end
  141.     left()
  142.     left()
  143. end
  144.  
  145. function digLayer()
  146.     r = radius
  147.     for i = 1,r do
  148.         radius = i
  149.         gotoEdge()
  150.         digRing()
  151.     end
  152. end
  153.  
  154. function digCylinder()
  155.     for i = 1,height do
  156.         digLayer()
  157.         moveDown()
  158.     end
  159. end
  160.  
  161. digCylinder()
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement