Advertisement
SimplyRaven

Untitled

Jan 16th, 2021
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.92 KB | None | 0 0
  1. -- Usage (program name) <x> <y> <z>
  2.  
  3.  
  4. local tArgs = {...}
  5.  
  6. xHome, yHome, zHome = gps.locate(3)
  7.  
  8. xStart = tonumber( tArgs[1] )
  9. yStart = tonumber( tArgs[2] )
  10. zStart = tonumber( tArgs[3] )
  11.  
  12. xCount = 0
  13. yCount = 0
  14. zCount = 0
  15.  
  16. function checkIfFuel()
  17.     return turtle.refuel(0)
  18.   end
  19.  
  20. function smartRefuel()
  21.     local fuelLimit = turtle.getFuelLimit()
  22.     if turtle.getFuelLevel() < fuelLimit / 4 then
  23.         for i = 1, 16 do
  24.             turtle.select(i)
  25.             if checkIfFuel() then
  26.                 turtle.refuel()
  27.             end
  28.         end
  29.     end
  30. end
  31.  
  32. function inventoryDump()
  33.     turtle.select(16)
  34.     turtle.placeUp()
  35.     for i = 15, 2, -1 do
  36.        turtle.select(i)
  37.        if turtle.getItemCount( turtle.getSelectedSlot() ) > 0 then
  38.             turtle.dropUp()
  39.         end
  40.     end
  41.     turtle.select(16)
  42.     turtle.select(1)
  43.     smartRefuel()
  44. end
  45.  
  46. -- 4 for North, 1 for East, 2 for South, 3 for West
  47. function getDirection()
  48.     local stX, stY, stZ
  49.     local nwX, nwY, nwZ
  50.     local clcX, clcZ
  51.     local facing
  52.  
  53.     stX, stY, stZ = gps.locate(3)
  54.     turtle.forward()
  55.     sleep(1)
  56.     nwX, nwY, nwZ = gps.locate(3)
  57.    
  58.     clcX = stX - nwX
  59.     clcZ = stZ - nwZ
  60.  
  61.     if tonumber(clcX) ~= 0 then
  62.         if tonumber(clcX) < 0 then
  63.             facing = 1
  64.         end
  65.         if tonumber(clcX) > 0 then
  66.             facing = 3
  67.         end
  68.     end
  69.     if tonumber(clcZ) ~= 0 then
  70.         if tonumber(clcZ) < 0 then
  71.             facing = 2
  72.         end
  73.         if tonumber(clcZ) > 0 then
  74.             facing = 4
  75.         end
  76.     end
  77.     turtle.back()
  78.     return facing
  79. end
  80.  
  81. function faceNorth()
  82.     local direction = getDirection()
  83.     if tonumber(direction) == 3 then
  84.         turtle.turnLeft()
  85.     elseif tonumber(direction) == 4 then
  86.         turtle.turnLeft()
  87.         turtle.turnLeft()
  88.     elseif tonumber(direction) == 1 then
  89.         turtle.turnRight()
  90.     end
  91. end
  92.  
  93. function safeBreakDown()
  94.     if turtle.detectDown then
  95.         turtle.digDown()
  96.     end
  97. end
  98.  
  99. function smartTurn(counter)
  100.     if counter % 2 == 0 then
  101.         turtle.turnRight()
  102.     else
  103.         turtle.turnLeft()
  104.     end
  105. end
  106.  
  107. function goToX()
  108.     local homeCopy = xHome
  109.     if homeCopy < xStart then
  110.         turtle.turnLeft()
  111.         while homeCopy ~= xStart do
  112.             turtle.forward()
  113.             homeCopy = homeCopy + 1
  114.             xCount = xCount + 1
  115.         end
  116.         turtle.turnLeft()
  117.     elseif homeCopy > xStart then
  118.         turtle.turnRight()
  119.         while homeCopy ~= xStart do
  120.             turtle.forward()
  121.             homeCopy = homeCopy - 1
  122.             xCount = xCount - 1
  123.         end
  124.     end
  125. end
  126. function goToZ()
  127.     local homeCopy = zHome
  128.     if homeCopy < zStart then
  129.         while homeCopy ~= zStart do
  130.             turtle.forward()
  131.             homeCopy = homeCopy + 1
  132.             zCount = zCount + 1
  133.         end
  134.     elseif homeCopy > zStart then
  135.         turtle.turnLeft()
  136.         turtle.turnLeft()
  137.         while homeCopy > zStart do
  138.             turtle.forward()
  139.             homeCopy = homeCopy - 1
  140.             zCount = zCount - 1
  141.         end
  142.     end
  143. end
  144. function goToY()
  145.     local homeCopy = yHome
  146.     if homeCopy < yStart then
  147.         while homeCopy ~= yStart do
  148.             turtle.moveUp()
  149.             homeCopy = homeCopy + 1
  150.             yCount = yCount + 1
  151.         end
  152.     elseif homeCopy > yStart then
  153.         while homeCopy > yStart do
  154.             safeBreakDown()
  155.             turtle.down()
  156.             homeCopy = homeCopy - 1
  157.             yCount = yCount - 1
  158.         end
  159.     end
  160. end
  161.  
  162. function goToCoords()
  163.     faceNorth()
  164.     goToX()
  165.     faceNorth()
  166.     goToZ()
  167.     goToY()
  168.     smartRefuel()
  169. end
  170.  
  171. function quarry()
  172.     for i = 1, 3 do
  173.         turtle.digDown()
  174.         turtle.down()
  175.         for j = 1, 20 do
  176.             for k = 1, 19 do
  177.                 turtle.dig()
  178.                 turtle.forward()
  179.             end
  180.             smartRefuel()
  181.             if j < 20 then
  182.                 smartTurn(j)
  183.                 turtle.dig()
  184.                 turtle.forward()
  185.                 smartTurn(j)
  186.             end
  187.         end
  188.         turtle.turnLeft()
  189.         for j = 1, 19 do
  190.             turtle.forward()
  191.         end
  192.         turtle.turnLeft()
  193.     end
  194. end
  195.  
  196. function returnHome()
  197.     for i = 1, 3 do
  198.         turtle.up()
  199.     end
  200.     xMove = math.abs(xCount)
  201.     yMove = math.abs(yCount)
  202.     zMove = math.abs(zCount)
  203.  
  204.     for i = 1, math.abs(yMove) do
  205.         turtle.up()
  206.     end
  207.    
  208.     if xCount < 0 then
  209.         turtle.turnLeft()
  210.     else
  211.         turtle.turnRight()
  212.     end
  213.     for i = 1, math.abs(xMove) do
  214.         turtle.forward()
  215.     end
  216.     faceNorth()
  217.  
  218.     if zCount > 0 then
  219.         turtle.turnLeft()
  220.         turtle.turnLeft()
  221.     end
  222.     for i = 1, math.abs(zMove) do
  223.         turtle.forward()
  224.     end
  225. end
  226.  
  227. function miner()
  228.     goToCoords()
  229.     faceNorth()
  230.     quarry()
  231.     returnHome()
  232.     inventoryDump()
  233. end
  234.  
  235. miner()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement