Advertisement
karelvysinka

quarry_v0.6

Nov 15th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.44 KB | None | 0 0
  1. local width, length
  2.  
  3. local blocksMined = 0
  4. local xDir, zDir = 0, 1
  5. local xx, yy, zz = 0, 0, 0
  6.  
  7. local enderChest = false
  8. local quarryLeft = false
  9. local quarryUpwd = false
  10. local fuelLimit = turtle.getFuelLimit()
  11.  
  12.  
  13. local args = {...}
  14. local nArgs = #args
  15. local optns = 0
  16.  
  17.  
  18.  
  19.  
  20. if string.sub(args[1], 1, 1) == "-"  then
  21.     optns = 1
  22.     local ops = string.lower(string.sub(args[1], 2))
  23.     if string.match(ops, "e") then enderChest = true end
  24.     if string.match(ops, "l") then quarryLeft = true end
  25.     if string.match(ops, "u") then quarryUpwd = true end
  26. end
  27.  
  28. width = tonumber(args[optns+1])
  29. if (nArgs - optns == 1) then
  30.     length = width
  31. elseif (nArgs - optns == 2) then
  32.     length = tonumber(args[optns+2])
  33. end
  34.  
  35.  
  36.  
  37. function spaceLeft()
  38.     if enderChest then return turtle.getItemCount(15)==0
  39.     else return turtle.getItemCount(16)==0 end
  40. end
  41.  
  42. function isFueled(over)
  43.     local over = over or 0
  44.     return ( turtle.getFuelLevel() > ( xx + yy + zz + over ) )
  45. end
  46.  
  47.  
  48.  
  49. function left()
  50.     local swap = xDir
  51.     turtle.turnLeft()
  52.     xDir = zDir
  53.     zDir = -swap
  54. end
  55.  
  56. function right()
  57.     local swap = xDir
  58.     turtle.turnRight()
  59.     xDir = -zDir
  60.     zDir = swap
  61. end
  62.  
  63. function iLeft() if quarryLeft then right() else left() end end
  64. function iRight() if quarryLeft then left() else right() end end
  65.  
  66.  
  67.  
  68. function forward(dist)
  69.     local dist = dist or 1
  70.     dist = math.abs(dist)
  71.     for iter = 1, dist do
  72.         while not turtle.forward() do
  73.             refuel()
  74.             if turtle.detect() and (not turtle.dig()) then
  75.                 return false end
  76.             turtle.attack()
  77.             turtle.suck()
  78.         end
  79.         xx = xx + xDir
  80.         zz = zz + zDir
  81.     end
  82. end
  83.  
  84. function up(dist)
  85.     local dist = dist or 1
  86.     dist = math.abs(dist)
  87.     for iter = 1, dist do
  88.         while not turtle.up() do
  89.             refuel()
  90.             if turtle.detectUp() and (not turtle.digUp()) then
  91.                 return false end
  92.             turtle.attackUp()
  93.             turtle.suckUp()
  94.         end
  95.         yy = yy - 1
  96.     end
  97. end
  98.  
  99. function down(dist)
  100.     local dist = dist or 1
  101.     dist = math.abs(dist)
  102.     for iter = 1, dist do
  103.         while not turtle.down() do
  104.             refuel()
  105.             if turtle.detectDown() and (not turtle.digDown()) then
  106.                 return false end
  107.             turtle.attackDown()
  108.             turtle.suckDown()
  109.         end
  110.         yy = yy + 1
  111.     end
  112. end
  113.  
  114.  
  115.  
  116. function goTo(pos)
  117.     local xX, yY, zZ, xXDir, zZDir = pos[1], pos[2], pos[3], pos[4], pos[5]
  118.     if (yY < yy) then up(2)
  119.     elseif (yY > yy) then down(2) end
  120.     if (xX > xx) then
  121.         while (xDir ~= 1) do
  122.             right()
  123.         end
  124.     elseif (xX < xx) then
  125.         while (xDir ~= -1) do
  126.             left()
  127.         end
  128.     end
  129.     forward(xX - xx)
  130.     if (zZ > zz) then
  131.         while (zDir ~= 1) do
  132.             left()
  133.         end
  134.     elseif (zZ < zz) then
  135.         while (zDir ~= -1) do
  136.             right()
  137.         end
  138.     end
  139.     forward(zZ - zz)
  140.     if (yY < yy) then up(yY - yy)
  141.     elseif (yY > yy) then down(yY - yy) end
  142.     while not ((xXDir == xDir) and (zZDir == zDir)) do
  143.         right()
  144.     end
  145. end
  146.  
  147.  
  148.  
  149. function mineFwd()
  150.     if not spaceLeft() then dropLoot() end
  151.     while turtle.dig() do
  152.         blocksMined = blocksMined + 1
  153.         sleep(0.05)
  154.     end
  155.     refuel()
  156.     if not spaceLeft() then dropLoot() end
  157.     forward()
  158.     while turtle.digUp() do
  159.         blocksMined = blocksMined + 1
  160.         sleep(0.1)
  161.     end
  162.     if not spaceLeft() then dropLoot() end
  163.     if turtle.digDown() then blocksMined = blocksMined + 1 end
  164.     turtle.suckUp()
  165.     turtle.suckDown()
  166. end
  167.  
  168.  
  169.  
  170. function refuel(over)
  171.     local over = over or 5
  172.     if not ((fuelLimit==0) or isFueled(over)) then
  173.         if enderChest then
  174.             turtle.select(1)
  175.             turtle.placeUp()
  176.             turtle.suckUp(1)
  177.             turtle.refuel(1)
  178.             turtle.digUp()
  179.         elseif (turtle.getItemCount(1) > 0) then
  180.             turtle.select(1)
  181.             turtle.refuel(1)
  182.         else
  183.             local pos = {xx, yy, zz, xDir, zDir}
  184.             goTo({0, 0, 0, 0, -1})
  185.             print("Waiting for Fuel...")
  186.             while (turtle.getItemCount(1) == 0) do
  187.                 os.pullEvent("turtle_inventory")
  188.             end
  189.             turtle.select(1)
  190.             turtle.refuel()
  191.             goTo(pos)
  192.         end
  193.     end
  194. end
  195.  
  196.  
  197.  
  198. function dropLoot(done)
  199.     local done = done or false
  200.     local pos = {xx, yy, zz, xDir, zDir}
  201.     if enderChest then
  202.         turtle.select(16)
  203.         turtle.placeUp()
  204.         for slot = 2, 15 do
  205.             turtle.select(slot)
  206.             while not turtle.dropUp() do
  207.                 sleep(10)
  208.             end
  209.         end
  210.         turtle.select(16)
  211.         turtle.digUp()
  212.     else
  213.         refuel(2)
  214.         goTo({0, 0, 0, 0, -1})
  215.         for slot = 2, 16 do
  216.             turtle.select(slot)
  217.             while not turtle.drop() do
  218.                 sleep(10)
  219.             end
  220.         end
  221.         if not done then
  222.             turtle.refuel(pos[1] + pos[2] + pos[3])
  223.             goTo(pos)
  224.         else goTo({0, 0, 0, 0, 1}) end
  225.     end
  226.     turtle.select(1)
  227. end
  228.  
  229.  
  230.  
  231. local alternate = 0
  232. local square = width == length
  233. local swapSides = false
  234. local done = turtle.detectDown() and (not turtle.digDown())
  235.  
  236. turtle.digUp()
  237. while not done do
  238.     local iWidth, iLength
  239.     if (not square) and swapSides then
  240.         iWidth, iLength = length, width
  241.     else
  242.         iWidth, iLength = width, length
  243.     end
  244.     for ii = 1, iWidth do
  245.         for jj = 1, iLength-1 do
  246.             mineFwd()
  247.         end
  248.         if ii < iWidth then
  249.             if math.fmod(ii + alternate, 2) == 0 then
  250.                 iLeft()
  251.                 mineFwd()
  252.                 iLeft()
  253.             else
  254.                 iRight()
  255.                 mineFwd()
  256.                 iRight()
  257.             end
  258.         end
  259.     end
  260.     if (width > 1) and (length > 1) then
  261.         if math.fmod(iWidth, 2) == 0 then
  262.             if (alternate == 0) or math.fmod(iLength, 2) == 0 then
  263.                 iRight()
  264.             else iLeft() end
  265.         else  
  266.             if alternate == 0 then
  267.                 iLeft()
  268.             else
  269.                 iRight()
  270.             end
  271.             alternate = 1 - alternate
  272.         end
  273.     end
  274.     for n = 1, 3 do
  275.         if quarryUpwd then
  276.             up()
  277.             turtle.digUp()
  278.         else
  279.             turtle.digDown()
  280.             down()
  281.         end
  282.     end
  283.     swapSides = not swapSides
  284.     done = (turtle.detectDown() and (not turtle.digDown())) and (not turtle.detect())
  285. end
  286. dropLoot(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement