Advertisement
hiphopcanine

Circular Excavate

Nov 9th, 2024
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 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.         SmartRefuel()
  52.         moved = turtle.forward()
  53.     end
  54. end
  55.  
  56.  
  57.  
  58. function moveUp()
  59.     yCoord = yCoord + 1
  60.  
  61.     turtle.digUp()
  62.  
  63.     moved = false
  64.     while not(moved) do
  65.         moved = turtle.up()
  66.     end  
  67. end
  68.  
  69. function moveDown()
  70.  
  71.     yCoord = yCoord - 1
  72.     turtle.digDown()
  73.    
  74.     moved = false
  75.  
  76.     while not(moved) do
  77.         moved = turtle.down()
  78.     end
  79.  
  80. end
  81.  
  82. function look(direction)
  83.     while direction ~= orientations[orientation] do
  84.         right()
  85.     end
  86. end
  87.  
  88.  
  89. function round(n)
  90.     decimal = n - math.floor(n)
  91.     if decimal >= 0.5 then
  92.         return math.ceil(n)
  93.     else
  94.         return math.floor(n)
  95.     end
  96. end
  97.  
  98. function withinRadius(x, z)
  99.     return round( math.sqrt((x * x) + (z * z)) ) <= radius
  100. end
  101.  
  102.  
  103. function forwardWithinRadius()
  104.  
  105.     xNext = xCoord + xDiff[orientation]  
  106.     zNext = zCoord + zDiff[orientation]
  107.    
  108.     return withinRadius(xNext, zNext)
  109. end
  110.  
  111. function gotoEdge()
  112.     while forwardWithinRadius() do
  113.         moveForward()
  114.     end
  115.    
  116.     left()
  117.    
  118.     xStartRing = xCoord
  119.     zStartRing = zCoord
  120. end
  121.  
  122. function digRing()
  123.  
  124.     repeat
  125.        
  126.         while forwardWithinRadius() do
  127.             moveForward()
  128.         end
  129.         while not(forwardWithinRadius()) do
  130.             left()
  131.         end
  132.         moveForward()
  133.         right()
  134.    
  135.     until xCoord == xStartRing and zCoord == zStartRing
  136.    
  137.     left()
  138.     left()
  139.     for i = 1,radius do
  140.         moveForward()
  141.     end
  142.     left()
  143.     left()
  144. end
  145.  
  146. function digLayer()
  147.     r = radius
  148.     for i = 1,r do
  149.         radius = i
  150.         gotoEdge()
  151.         digRing()
  152.     end
  153. end
  154.  
  155. function digCylinder()
  156.     for i = 1,height do
  157.         digLayer()
  158.         moveDown()
  159.     end
  160. end
  161.  
  162. --[[ Calculate fuel:
  163. Calculate how much fuel we have left and refuel if needed
  164. returns true if we can get home on our current fuel level
  165. returns false if we need to go back now
  166. ]]--
  167. function SmartRefuel()
  168.     -- refuel if we need to
  169.     local cur_fuel = turtle.getFuelLevel() -- current fuel
  170.     if cur_fuel == 0 then
  171.         for i = 1,16,1 do
  172.             if not (turtle.getItemCount(i) == nil) then
  173.                 if turtle.getItemDetail(i)["name"] == "minecraft:coal" then
  174.                     turtle.select(i)
  175.                     turtle.refuel()
  176.                     turtle.select(1)
  177.                     print("Refuelled.")
  178.                     break
  179.                 end
  180.             end
  181.         end
  182.     end
  183. end
  184.  
  185. digCylinder()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement