Advertisement
jaypar

Dig

Aug 20th, 2019
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.83 KB | None | 0 0
  1. curPos = {
  2.     x = 0,
  3.     y = 0,
  4.     z = 0,
  5.     rot = 0
  6. }
  7.  
  8. function Forward(num)
  9.     local i = 0
  10.    
  11.     while i < num do
  12.         if curPos.rot == 0 then
  13.             if turtle.forward() then
  14.                 curPos.z = curPos.z + 1
  15.             end
  16.         elseif curPos.rot == 180 then
  17.             if turtle.forward() then
  18.                 curPos.z = curPos.z - 1
  19.             end
  20.         elseif curPos.rot == -90 then
  21.             if turtle.forward() then
  22.                 curPos.x = curPos.x - 1
  23.             end
  24.         elseif curPos.rot == 90 then
  25.             if turtle.forward() then
  26.                 curPos.x = curPos.x + 1
  27.             end
  28.         end
  29.         i = i + 1
  30.     end
  31. end
  32.  
  33. function Up(num)
  34.     local i = 0
  35.    
  36.     while i < num do
  37.         if turtle.up() then
  38.             curPos.y = curPos.y + 1
  39.         end
  40.         i = i + 1
  41.     end
  42. end
  43.  
  44. function Down(num)
  45.     local i = 0
  46.    
  47.     while i < num do
  48.         if turtle.down() then
  49.             curPos.y = curPos.y - 1
  50.         end
  51.         i = i + 1
  52.     end
  53. end
  54.  
  55. function RotateLeft(num)
  56.     local i = 0
  57.    
  58.     while i < num do
  59.         if turtle.turnLeft() then
  60.             curPos.rot = curPos.rot - 90
  61.         end
  62.         if curPos.rot == -180 then
  63.             curPos.rot = 180
  64.         end
  65.         i = i + 1
  66.     end
  67. end
  68.  
  69. function RotateRight(num)
  70.     local i = 0
  71.    
  72.     while i < num do
  73.         if turtle.turnRight() then
  74.             curPos.rot = curPos.rot + 90
  75.         end
  76.         if curPos.rot > 180 then
  77.             curPos.rot = -90
  78.         end
  79.         i = i + 1
  80.     end
  81. end
  82.  
  83. function ReturnHome()
  84.     while curPos.x ~= 0 do
  85.         if curPos.x > 0 then
  86.             if curPos.rot ~= -90 then
  87.                 RotateLeft(1)
  88.             else
  89.                 Forward(curPos.x)
  90.             end
  91.         elseif curPos.x < 0 then
  92.             if curPos.rot ~= 90 then
  93.                 RotateRight(1)
  94.             else
  95.                 Forward(curPos.x)
  96.             end
  97.         end
  98.     end
  99.    
  100.     while curPos.y ~= 0 do
  101.         if curPos.y > 0 then
  102.             Down(curPos.y)
  103.         elseif curPos.y < 0 then
  104.             Up(curPos.y)
  105.         end
  106.     end
  107.    
  108.     while curPos.z ~= 0 do
  109.         if curPos.z > 0 then
  110.             if curPos.rot ~= 180 then
  111.                 RotateLeft(1)
  112.             else
  113.                 Forward(curPos.z)
  114.             end
  115.         elseif curPos.z < 0 then
  116.             if curPos.rot ~= 0 then
  117.                 RotateRight(1)
  118.             else
  119.                 Forward(curPos.z)
  120.             end
  121.         end
  122.     end
  123.    
  124.     RotateLeft(2)
  125. end
  126.  
  127. function Dig(UpDownOnly)
  128.     turtle.digUp()
  129.     turtle.digDown()
  130.     if not UpDownOnly then
  131.         turtle.dig()
  132.     end
  133. end
  134.  
  135. function FuelCheck(x, y, z)
  136.     if turtle.getFuelLevel() < x*y*z then
  137.         print("Turtle needs atleast " .. x*y*z .. " more fuel")
  138.         while turtle.getFuelLevel() < x*y*z do
  139.             turtle.refuel()
  140.             sleep(1)
  141.         end
  142.     end
  143. end
  144.  
  145. function Clear(x, y, z)
  146.     FuelCheck(x, y, z)
  147.  
  148.     Up(1)
  149.     Dig()
  150.     Digging = true
  151.     while Digging do
  152.         Forward(1)
  153.         Dig(false)
  154.        
  155.         if curPos.x == x and curPos.z == z and CurPos.y == y then
  156.             ReturnHome()
  157.         elseif curPos.x == x and curPos.z == z then
  158.             ReturnHome()
  159.             RotateLeft(2)
  160.             Up(1)
  161.             Dig(true)
  162.             Up(1)
  163.             Dig(false)
  164.         elseif curPos.x == x then
  165.             RotateLeft(1)
  166.             Dig(false)
  167.             Forward(1)
  168.             RotateLeft(1)
  169.             Dig(false)
  170.         elseif curPos.x == 0 then
  171.             RotateRight(1)
  172.             Dig(false)
  173.             Forward(1)
  174.             RotateRight(1)
  175.             Dig(false)
  176.         end
  177.     end
  178. end
  179.  
  180. Clear(60, 20, 60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement