Advertisement
Guest User

clearCube.lua

a guest
Apr 4th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. local depth, width, height = ...
  2. if not (width and depth and height) then
  3.     error("Not enough arguments; should be depth, width, height")
  4. end
  5. width = tonumber(width)
  6. depth = tonumber(depth)
  7. height = tonumber(height)
  8.  
  9. abs = math.abs
  10. floor = math.floor
  11. local heading = 0
  12. local x, y, z = 0, 0, 0
  13.  
  14. local normalPasses = floor(height/3)
  15. local remainingLayers = height % 3
  16. local layers = 3
  17.  
  18. function isOdd(num)
  19.     if num % 2 == 0 then
  20.         return false
  21.     else
  22.         return true
  23.     end
  24. end
  25.  
  26. function changeHeading(amount)
  27.     heading = (heading + amount) % 4
  28. end
  29.  
  30. function turnRight()
  31.     turtle.turnRight()
  32.     changeHeading(1)
  33. end
  34.  
  35. function turnLeft()
  36.     turtle.turnLeft()
  37.     changeHeading(-1)
  38. end
  39.  
  40. function xIncrement(heading)
  41.     if heading == 1 or heading == 3 then
  42.         return 0
  43.     elseif heading == 0 then
  44.         return 1
  45.     else return -1
  46.     end
  47. end
  48.  
  49. function zIncrement(heading)
  50.     if heading == 0 or heading == 2 then
  51.         return 0
  52.     elseif heading == 1 then
  53.         return 1
  54.     else return -1
  55.     end
  56. end
  57.  
  58. function turnTo(newHeading)
  59.     local rightTurns = (newHeading-heading)%4
  60.     if rightTurns <= 2 then
  61.         for i = 1, rightTurns do
  62.             turnRight()
  63.         end
  64.     elseif rightTurns == 3 then
  65.         turnLeft()
  66.     end
  67. end
  68.  
  69. function up()
  70.     turtle.up()
  71.     y = y+1
  72. end
  73.  
  74. function down()
  75.     turtle.down()
  76.     y = y-1
  77. end
  78.  
  79. function moveVert(signedDist)
  80.     if signedDist < 0 then
  81.         for i = signedDist, -1 do
  82.             down()
  83.         end        
  84.     else
  85.         for i = 1, signedDist do
  86.             up()
  87.         end
  88.     end
  89. end
  90.  
  91. function moveHor(distance, direction)
  92.     turnTo(direction)
  93.     for i = 1, distance do
  94.         forw()
  95.     end
  96. end
  97.  
  98. oddWidth = isOdd(width)
  99. oddDepth = isOdd(depth)
  100. oddHeight = isOdd(height)
  101.  
  102. function checkRefuel()
  103.     if turtle.getFuelLevel() < 10 then
  104.         turtle.refuel()
  105.     end
  106. end
  107.  
  108. function moveTo(destX, destZ, destY)
  109.     if destY then
  110.         moveVert(destY-y)        
  111.     end
  112.     if destZ then
  113.         local diffZ = destZ-z
  114.         local distZ = abs(diffZ)
  115.         moveHor(distZ, diffZ>0 and 1 or 3)
  116.     end
  117.     if destX then
  118.         local diffX = destX-x
  119.         local distX = abs(diffX)
  120.         moveHor(distX, diffX>0 and 0 or 2)
  121.     end
  122. end
  123.  
  124. function forw()
  125.     turtle.forward()
  126.     x = x + xIncrement(heading)
  127.     z = z + zIncrement(heading)
  128.     --print("x"..x)
  129.     --print("z"..z)
  130. end
  131.  
  132. function fullDig()
  133.     turtle.dig()
  134.     if layers > 2 then turtle.digDown() end
  135.     if layers > 1 then turtle.digUp() end
  136. end
  137.  
  138. function moveX()
  139.     fullDig()
  140.     forw()
  141. end
  142.  
  143. function moveZ()
  144.     checkRefuel()
  145.     if isOdd(z) then
  146.         while x > 0 do
  147.             moveX()
  148.         end
  149.     else
  150.         while x < depth-1 do
  151.             moveX()
  152.         end
  153.     end        
  154.     print("ferdigz "..z)
  155. end
  156.  
  157. function moveY()
  158.     while z < width-1 do
  159.        moveZ()
  160.        if not isOdd(z) then
  161.            turnRight()
  162.            fullDig()
  163.            forw()
  164.            turnRight()
  165.        else
  166.            turnLeft()
  167.            fullDig()
  168.            forw()
  169.            turnLeft()
  170.        end
  171.     end
  172.     moveZ()
  173.     if layers > 1 then turtle.digUp() end
  174.     if layers > 2 then turtle.digDown() end
  175. end
  176.  
  177. function clearCube()
  178.     turtle.dig()
  179.     turtle.forward()
  180.     while y < height-1 do
  181.         moveY()
  182.         moveTo(0,0,nil)
  183.         turtle.digUp()
  184.         up()
  185.         turnTo(0)
  186.     end
  187.     moveY()
  188.     moveTo(-1,0,0)
  189. end
  190.  
  191. --clearCube()        
  192.  
  193. function clearCubeEfficient()
  194.     turtle.dig()
  195.     turtle.forward()
  196.     for pass = 1, normalPasses do
  197.         if pass > 1 then
  198.             turtle.digUp()
  199.             up()
  200.             turtle.digUp()
  201.             up()
  202.         end
  203.         turtle.digUp()
  204.         up()
  205.        
  206.         moveY()
  207.         moveTo(0, 0, nil)
  208.         turnTo(0)
  209.     end
  210.     layers = remainingLayers
  211.     if remainingLayers > 0 then
  212.         turtle.digUp()
  213.         up()
  214.         turtle.digUp()
  215.         up()
  216.         moveY()
  217.     end
  218.     moveTo(-1, 0, 0)
  219. end
  220.  
  221. clearCubeEfficient()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement