ax1m

quarry_v1.1

Dec 23rd, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.91 KB | None | 0 0
  1. -- pastebin get 11ifxreH quarry
  2.  
  3. -- These variables are continually updated
  4. local xx, yy, zz = 0, 0, 0
  5. local xDir, zDir = 0, 1
  6. local blocksMined = 0
  7.  
  8. -- These constants will never change once set
  9. local debugMode
  10. local enderChest
  11. local fuelLimit = turtle.getFuelLimit()
  12. local fuelValue
  13. local logInterval
  14. local quarryLeft
  15. local quarryUpwd
  16. local width, length
  17.  
  18.  
  19. local args = {...}
  20. local nArgs = #args
  21. local optns = 0
  22.  
  23.  
  24. -- Parsing program options
  25. if string.sub(args[1], 1, 1) == "-"  then
  26.     optns = 1
  27.     local ops = string.lower(string.sub(args[1], 2))
  28.     debugMode = string.match(ops, "d")
  29.     enderChest = string.match(ops, "e")
  30.     quarryLeft = string.match(ops, "l")
  31.     quarryUpwd = string.match(ops, "u")
  32. end
  33.  
  34. if debugMode then logInterval = 1
  35. else logInterval = 100 end
  36.  
  37. -- Parsing program arguments
  38. width = tonumber(args[optns+1])
  39. if (nArgs - optns == 1) then
  40.     length = width
  41. elseif (nArgs - optns == 2) then
  42.     length = tonumber(args[optns+2])
  43. end
  44.  
  45. -- Log message to the console, but only if debugMode is enabled
  46. function dLog(message) if debugMode then print(message) end end
  47.  
  48.  
  49. -- Increment and periodically log # of blocks mined
  50. function iBlock()
  51.     dLog("iBlock() called")
  52.     blocksMined = blocksMined + 1
  53.     if math.fmod(blocksMined, logInterval) == 0 then
  54.         print(blocksMined.." Blocks mined")
  55.     end
  56. end
  57.  
  58. -- Checks if there is any space left in the turtle's inventory
  59. function spaceLeft()
  60.     dLog("spaceLeft() called")
  61.     if enderChest then return turtle.getItemCount(15)==0
  62.     else return turtle.getItemCount(16)==0 end
  63. end
  64.  
  65. -- Checks if the turtle can safely return home after one more move
  66. function isFueled(over)
  67.     local over = over or 0
  68.     dLog("isFueled("..over..") called")
  69.     local dist = math.abs(xx) + math.abs(yy) + math.abs(zz)
  70.     return ( turtle.getFuelLevel() >= ( dist + over ) )
  71. end
  72.  
  73.  
  74. -- Turn left
  75. function left()
  76.     dLog("left() called")
  77.     local swap = xDir
  78.     turtle.turnLeft()
  79.     xDir = zDir
  80.     zDir = -swap
  81. end
  82.  
  83. -- Turn right
  84. function right()
  85.     dLog("right() called")
  86.     local swap = xDir
  87.     turtle.turnRight()
  88.     xDir = -zDir
  89.     zDir = swap
  90. end
  91.  
  92. function iLeft() if quarryLeft then right() else left() end end
  93. function iRight() if quarryLeft then left() else right() end end
  94.  
  95. function oneEighty() left() left() end
  96.  
  97.  
  98.  
  99. -- Move forward if possible and confirm success/failure
  100. function forward(dist)
  101.     local dist = dist or 1
  102.     local rev = (dist < 0)
  103.     dLog("forward("..dist..") called")
  104.     if rev then oneEighty() end
  105.     for iter = 1, dist do
  106.         while not turtle.forward() do
  107.             if turtle.dig() then
  108.                 iBlock()
  109.             elseif turtle.detect() then
  110.                 return false
  111.             elseif turtle.attack() then
  112.                 turtle.suck()
  113.             else return false end
  114.         end
  115.         xx = xx + xDir
  116.         zz = zz + zDir
  117.     end
  118.     if rev then oneEighty() end
  119.     return true
  120. end
  121.  
  122. -- Move up if possible and confirm success/failure
  123. function up(dist)
  124.     local dist = dist or 1
  125.     local rev = (dist < 0)
  126.     dLog("up("..dist..") called")
  127.     if rev then return down(-dist) end
  128.     for iter = 1, dist do
  129.         while not turtle.up() do
  130.             if turtle.digUp() then
  131.                 iBlock()
  132.             elseif turtle.detectUp() then
  133.                 return false
  134.             elseif turtle.attackUp() then
  135.                 turtle.suckUp()
  136.             else return false end
  137.         end
  138.         yy = yy - 1
  139.     end
  140.     return true
  141. end
  142.  
  143. -- Move down if possible and confirm success/failure
  144. function down(dist)
  145.     local dist = dist or 1
  146.     local rev = (dist < 0)
  147.     dLog("down("..dist..") called")
  148.     if rev then return up(-dist) end
  149.     for iter = 1, dist do
  150.         while not turtle.down() do
  151.             if turtle.digDown() then
  152.                 iBlock()
  153.             elseif turtle.detectDown() then
  154.                 return false
  155.             elseif turtle.attackDown() then
  156.                 turtle.suckDown()
  157.             else return false end
  158.         end
  159.         yy = yy + 1
  160.     end
  161.     return true
  162. end
  163.  
  164.  
  165. -- Goes to a position passed as coordinates and direction
  166. function goTo(xX, yY, zZ, xXDir, zZDir)
  167.     dLog("goTo("..xX..", "..yY..", "..zZ..", "..xXDir..", "..zZDir..") called")
  168.     local dist = math.abs(xX) + math.abs(yY) + math.abs(zZ)
  169.     refuel((2 * dist) + 2)
  170.     if (yY < yy) then up(2)
  171.     elseif (yY > yy) then down(2) end
  172.     if (xX > xx) then
  173.         while (xDir ~= 1) do
  174.             left()
  175.         end
  176.     elseif (xX < xx) then
  177.         while (xDir ~= -1) do
  178.             right()
  179.         end
  180.     end
  181.     forward(math.abs(xX - xx))
  182.     if (zZ > zz) then
  183.         while (zDir ~= 1) do
  184.             right()
  185.         end
  186.     elseif (zZ < zz) then
  187.         while (zDir ~= -1) do
  188.             left()
  189.         end
  190.     end
  191.     forward(math.abs(zZ - zz))
  192.     if (yY < yy) then up(yy - yY)
  193.     elseif (yY > yy) then down(yY - yy) end
  194.     while not ((xXDir == xDir) and (zZDir == zDir)) do
  195.         right()
  196.     end
  197. end
  198.  
  199.  
  200. -- Dig one block forward, as well as above and below
  201. function mineFwd()
  202.     dLog("mineFwd() called")
  203.     refuel()
  204.     if not spaceLeft() then dropLoot() end
  205.     while (not forward()) do
  206.         if turtle.dig() then
  207.             iBlock()
  208.         elseif turtle.detect() then
  209.             return false
  210.         end
  211.         if not spaceLeft() then dropLoot() end
  212.         turtle.suck()
  213.     end
  214.     while turtle.digUp() do
  215.         iBlock()
  216.         sleep(0.05)
  217.     end
  218.     if not spaceLeft() then dropLoot() end
  219.     if turtle.digDown() then iBlock() end
  220.     turtle.suckUp()
  221.     turtle.suckDown()
  222.     return true
  223. end
  224.  
  225.  
  226.  
  227. -- Refuels the turtle if necessary
  228. function refuel(over)
  229.     local over = over or 5
  230.     dLog("refuel("..over..") called")
  231.     if not (fuelLimit == 0) then
  232.         while not isFueled(over) do
  233.             if enderChest then
  234.                 turtle.select(1)
  235.                 turtle.placeUp()
  236.                 if (fuelValue == nil) then
  237.                     fuel = turtle.getFuelLevel()
  238.                     turtle.suckUp(1)
  239.                     turtle.refuel(1)
  240.                     fuelValue = turtle.getFuelLevel() - fuel
  241.                 end
  242.                 local num = 64
  243.                 if ((fuelLimit / fuelValue) < num) then
  244.                     num = math.floor(fuelLimit / fuelValue)
  245.                 end
  246.                 turtle.suckUp(num)
  247.                 turtle.refuel(num)             
  248.                 turtle.digUp()
  249.             elseif turtle.refuel(0) then
  250.                 turtle.select(1)
  251.                 turtle.refuel(1)
  252.             else
  253.                 local pos = {xx, yy, zz, xDir, zDir}
  254.                 goTo(0, 0, 0, 0, -1)
  255.                 dropLoot()
  256.                 print("Waiting for Fuel...")
  257.                 while not turtle.refuel(0) do
  258.                     if (turtle.getItemCount() ~= 0) then turtle.drop() end
  259.                     os.pullEvent("turtle_inventory")
  260.                 end
  261.                 print("Resuming mining")
  262.                 goTo(unpack(pos))
  263.             end
  264.         end
  265.     end
  266. end
  267.  
  268.  
  269.  
  270. -- Drops all loot in the output chest. If done is true the turtle will return to the position it was started from
  271. function dropLoot(done)
  272.     local done = done or false
  273.     dLog("dropLoot("..tostring(done)..") called")
  274.     local pos = {0, 0, 0, 0, 1}
  275.     if not done then pos = {xx, yy, zz, xDir, zDir} end
  276.     if enderChest then
  277.         turtle.select(16)
  278.         turtle.placeUp()
  279.         for slot = 2, 15 do
  280.             if (turtle.getItemCount(slot) > 0) then
  281.                 turtle.select(slot)
  282.                 while not turtle.dropUp() do
  283.                     sleep(10)
  284.                 end
  285.             end
  286.         end
  287.         turtle.select(16)
  288.         turtle.digUp()
  289.     else
  290.         goTo(0, 0, 0, 0, -1)
  291.         for slot = 2, 16 do
  292.             if (turtle.getItemCount(slot) > 0) then
  293.                 turtle.select(slot)
  294.                 while not turtle.drop() do
  295.                     sleep(10)
  296.                 end
  297.             end
  298.         end
  299.     end
  300.     turtle.select(1)
  301.     goTo(unpack(pos))
  302. end
  303.  
  304. -- Digs a width * length sized hole and returns true when done
  305. function quarry(width, length)
  306.     local alternate = 0
  307.     local square = width == length
  308.     local swapSides = width > length
  309.     local iWidth, iLength = width, length
  310.    
  311.     if swapSides then
  312.         iWidth, iLength = length, width
  313.         iRight()
  314.         quarryLeft = not quarryLeft
  315.     end
  316.    
  317.     turtle.digUp()
  318.     turtle.digDown()
  319.     while true do
  320.         for ii = 1, iWidth do
  321.             for jj = 1, iLength-1 do
  322.                 if (not mineFwd()) then return true end
  323.             end
  324.             if ii < iWidth then
  325.                 if math.fmod(ii + alternate, 2) == 0 then
  326.                     iLeft()
  327.                     if (not mineFwd()) then return true end
  328.                     iLeft()
  329.                 else
  330.                     iRight()
  331.                     if (not mineFwd()) then return true end
  332.                     iRight()
  333.                 end
  334.             end
  335.         end
  336.         if (iLength > 1) then oneEighty() end
  337.         for n = 1, 3 do
  338.             refuel()
  339.             if quarryUpwd then
  340.                 if (not up()) and (n < 3) then return true end
  341.                 turtle.digUp()
  342.             else
  343.                 if (not down()) and (n < 3) then return true end
  344.                 turtle.digDown()
  345.             end
  346.         end
  347.         if math.fmod(iWidth, 2) == 0 then
  348.             alternate = 1 - alternate
  349.         end
  350.     end
  351. end
  352.  
  353.  
  354. dropLoot(quarry(width, length))
Advertisement
Add Comment
Please, Sign In to add comment