Advertisement
dracoix

Turtle Excavate 1.7a

Jul 20th, 2019
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs = { ... }
  2. if #tArgs < 1 then
  3.     print( "Usage: diggit <diameter> <0 or 1 for blacklist enabled>" )
  4.     return
  5. end
  6.  
  7. -- Mine in a quarry pattern until we hit something we can't dig
  8. local size = tonumber( tArgs[1] )
  9. if size < 1 then
  10.     print( "Excavate diameter must be positive" )
  11.     return
  12. end
  13.  
  14. local black = false
  15.  
  16. if (#tArgs >= 2) then
  17.     black = (tArgs[2] == "1")
  18. else
  19.     tArgs[2] = "0"
  20. end
  21.  
  22.  
  23. local uhand = fs.open("update", "w")
  24. local ustr = "shell.run(\"rm posdata\")\n" .. "shell.run(\"rm diggit\")\n" .. "shell.run(\"pastebin get L5c3MGas diggit\")"
  25. uhand.write(ustr)
  26. uhand.close()
  27.  
  28. local shand = fs.open("startup", "w")
  29. shand.write("shell.run(\"diggit " .. tArgs[1] .. " " .. tArgs[2] .. "\")")
  30. shand.close()
  31.  
  32. local posinfo = {
  33.   ["xPos"] = 0,
  34.   ["zPos"] = 0,
  35.   ["xDir"] = 0,
  36.   ["zDir"] = 1,
  37.   ["depth"] = 0,
  38.   ["done"] = false,
  39.   ["maxdepth"] = 0,
  40.   ["dxPos"] = 0,
  41.   ["dzPos"] = 0,
  42.   ["dxDir"] = 0,
  43.   ["dzDir"] = 1,
  44.   ["ddepth"] = 0
  45. }
  46.  
  47. --local depth = 0
  48. local unloaded = 0
  49. local collected = 0
  50.  
  51. --local xPos,zPos = 0,0
  52. --local xDir,zDir = 0,1
  53.  
  54. local hRead
  55. local hWrite
  56.  
  57. local goTo -- Filled in further down
  58. local refuel -- Filled in further down
  59.  
  60. local function printPos()
  61.     print(posinfo.xPos .. "," .. posinfo.depth .. "," .. posinfo.zPos .. " : " .. posinfo.xDir .. "," .. posinfo.zDir)
  62. end
  63.  
  64. local function wasDestroyed()
  65.     count = 0
  66.     for i = 1,16 do
  67.         count = count + turtle.getItemCount(i)
  68.     end
  69.     return (count == 0)
  70. end
  71.  
  72. local function savePos()
  73.     hWrite = fs.open("posdata", "w")
  74.     hWrite.write(textutils.serialize(posinfo))
  75.     hWrite.close()
  76.     --printPos()
  77. end
  78.  
  79. local function loadPos()
  80.     hRead = fs.open("posdata", "r")
  81.     posinfo = textutils.unserialize(hRead.readAll())
  82.     hRead.close()
  83. end
  84.  
  85. local function turnLeft()
  86.     xd,zd = posinfo.xDir, posinfo.zDir
  87.     posinfo.dxDir = -zd
  88.     posinfo.dzDir = xd
  89.     savePos()
  90.     turtle.turnLeft()
  91.     posinfo.xDir = -zd
  92.     posinfo.zDir = xd
  93.     savePos()
  94. end
  95.  
  96. local function turnRight()
  97.     xd,zd = posinfo.xDir, posinfo.zDir
  98.     posinfo.dxDir = zd
  99.     posinfo.dzDir = -xd
  100.     savePos()
  101.     turtle.turnRight()
  102.     posinfo.xDir = zd
  103.     posinfo.zDir = -xd
  104.     savePos()
  105. end
  106.  
  107. local function trackForward()
  108.     if turtle.forward() then
  109.         posinfo.xPos = posinfo.xPos + posinfo.xDir
  110.         posinfo.zPos = posinfo.zPos + posinfo.zDir
  111.         savePos()
  112.         return true
  113.     else
  114.         return false
  115.     end
  116. end
  117.  
  118. local function trackDown()
  119.     if turtle.down() then
  120.         posinfo.depth = posinfo.depth + 1
  121.         savePos()
  122.         if (posinfo.maxdepth < posinfo.depth) then
  123.             posinfo.maxdepth = posinfo.depth
  124.             savePos()
  125.         end
  126.         return true
  127.     else
  128.         return false
  129.     end
  130. end
  131.  
  132. local function trackUp()
  133.     if turtle.up() then
  134.         posinfo.depth = posinfo.depth - 1
  135.         savePos()
  136.         return true
  137.     else
  138.         return false
  139.     end
  140. end
  141.  
  142. local function unload( _bKeepOneFuelStack )
  143.     print( "Unloading items..." )
  144.     for n=1,16 do
  145.         local nCount = turtle.getItemCount(n)
  146.         if nCount > 0 then
  147.             turtle.select(n)           
  148.             local bDrop = true
  149.             if _bKeepOneFuelStack and turtle.refuel(0) then
  150.                 bDrop = false
  151.                 _bKeepOneFuelStack = false
  152.             end        
  153.             if bDrop then
  154.                 turtle.drop()
  155.                 unloaded = unloaded + nCount
  156.             end
  157.         end
  158.     end
  159.     collected = 0
  160.     turtle.select(1)
  161. end
  162.  
  163. local function lookingAtChest()
  164.     s,d = turtle.inspect()
  165.     if (s) then
  166.         return string.find(d.name, "chest")
  167.     end
  168.     return false
  169. end
  170.  
  171. local function chestUp()
  172.     s,d = turtle.inspectUp()
  173.     if (s) then
  174.         return string.find(d.name, "chest")
  175.     end
  176.     return false
  177. end
  178.  
  179. local function chestDown()
  180.     s,d = turtle.inspectDown()
  181.     if (s) then
  182.         return string.find(d.name, "chest")
  183.     end
  184.     return false
  185. end
  186.  
  187. local function panic()
  188.     local x,y,z,xd,zd = posinfo.xPos,posinfo.depth,posinfo.zPos,posinfo.xDir,posinfo.zDir
  189.     print( "Panic to surface..." )
  190.     if goCareTo( 0,0,0,0,-1) then
  191.    
  192.     else
  193.         return false
  194.     end
  195.     if lookingAtChest() then
  196.         print("Alignment seems correct...")
  197.     else
  198.         return false
  199.     end
  200.     local fuelNeeded = 2*(x+y+z) + 1
  201.     if not refuel( fuelNeeded ) then
  202.         unload( true )
  203.         print( "Waiting for fuel" )
  204.         while not refuel( fuelNeeded ) do
  205.             os.pullEvent( "turtle_inventory" )
  206.         end
  207.     else
  208.         unload( true ) 
  209.     end
  210.     goTo( 0,0,0,0,1)
  211.     print( "I think I'm home..." )
  212.     return true
  213. end
  214.  
  215. local function returnSupplies()
  216.     local x,y,z,xd,zd = posinfo.xPos,posinfo.depth,posinfo.zPos,posinfo.xDir,posinfo.zDir
  217.     print( "Returning to surface..." )
  218.     goTo( 0,0,0,0,-1 )
  219.    
  220.     local fuelNeeded = 2*(x+y+z) + 1
  221.     if not refuel( fuelNeeded ) then
  222.         unload( true )
  223.         print( "Waiting for fuel" )
  224.         while not refuel( fuelNeeded ) do
  225.             os.pullEvent( "turtle_inventory" )
  226.         end
  227.     else
  228.         unload( true ) 
  229.     end
  230.    
  231.     print( "Resuming mining..." )
  232.     goTo( x,y,z,xd,zd )
  233. end
  234.  
  235. local function collect()   
  236.     local bFull = true
  237.     local nTotalItems = 0
  238.     for n=1,16 do
  239.         local nCount = turtle.getItemCount(n)
  240.         if nCount == 0 then
  241.             bFull = false
  242.         end
  243.         nTotalItems = nTotalItems + nCount
  244.     end
  245.    
  246.     if nTotalItems > collected then
  247.         collected = nTotalItems
  248.         if math.fmod(collected + unloaded, 50) == 0 then
  249.             print( "Mined "..(collected + unloaded).." items." )
  250.         end
  251.     end
  252.    
  253.     if bFull then
  254.         print( "No empty slots left." )
  255.         return false
  256.     end
  257.     return true
  258. end
  259.  
  260. function refuel( ammount )
  261.     local fuelLevel = turtle.getFuelLevel()
  262.     if fuelLevel == "unlimited" then
  263.         return true
  264.     end
  265.    
  266.     local needed = ammount or (posinfo.xPos + posinfo.zPos + posinfo.depth + 2)
  267.     if turtle.getFuelLevel() < needed then
  268.         local fueled = false
  269.         for n=1,16 do
  270.             if turtle.getItemCount(n) > 0 then
  271.                 turtle.select(n)
  272.                 if turtle.refuel(1) then
  273.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  274.                         turtle.refuel(1)
  275.                     end
  276.                     if turtle.getFuelLevel() >= needed then
  277.                         turtle.select(1)
  278.                         return true
  279.                     end
  280.                 end
  281.             end
  282.         end
  283.         turtle.select(1)
  284.         return false
  285.     end
  286.    
  287.     return true
  288. end
  289.  
  290. local iup
  291. local idown
  292. local dup
  293. local ddown
  294.  
  295. local function isBlacklist(name)
  296.     if not black then
  297.         return false
  298.     end
  299.     if (name == "minecraft:stone") then
  300.         return true
  301.     end
  302.     if (name == "minecraft:netherrack") then
  303.         return true
  304.     end
  305.     if (name == "minecraft:dirt") then
  306.         return true
  307.     end
  308.     if (name == "minecraft:water") then
  309.         return true
  310.     end
  311.     if (name == "minecraft:lava") then
  312.         return true
  313.     end
  314.     if (name == "minecraft:flowing_water") then
  315.         return true
  316.     end
  317.     print ("Ooo piece of candy: " .. name)
  318.     return false
  319. end
  320.  
  321. local function tryForwards()
  322.     if not refuel() then
  323.         print( "Not enough Fuel" )
  324.         returnSupplies()
  325.     end
  326.  
  327.     while not trackForward() do
  328.         if turtle.detect() then
  329.             if turtle.dig() then
  330.                 if not collect() then
  331.                     returnSupplies()
  332.                 end
  333.             else
  334.                 return false
  335.             end
  336.         elseif turtle.attack() then
  337.             if not collect() then
  338.                 returnSupplies()
  339.             end
  340.         else
  341.             sleep( 0.5 )
  342.         end
  343.     end
  344.  
  345.     --xPos = xPos + xDir
  346.     --zPos = zPos + zDir
  347.    
  348.     iup, dup = turtle.inspectUp()
  349.     idown, ddown = turtle.inspectDown()
  350.  
  351.     if iup then
  352.         if not (isBlacklist(dup.name)) then
  353.             if turtle.digUp() then
  354.                 if not collect() then
  355.                     returnSupplies()
  356.                 end
  357.             end
  358.         end
  359.     end
  360.  
  361.     if idown then
  362.         if not (isBlacklist(ddown.name)) then
  363.             if turtle.digDown() then
  364.                 if not collect() then
  365.                     returnSupplies()
  366.                 end
  367.             end
  368.         end
  369.     end
  370.  
  371.     return true
  372. end
  373.  
  374. local function tryDown()
  375.     if not refuel() then
  376.         print( "Not enough Fuel" )
  377.         returnSupplies()
  378.     end
  379.    
  380.     while not trackDown() do
  381.         if turtle.detectDown() then
  382.             if turtle.digDown() then
  383.                 if not collect() then
  384.                     returnSupplies()
  385.                 end
  386.             else
  387.                 return false
  388.             end
  389.         elseif turtle.attackDown() then
  390.             if not collect() then
  391.                 returnSupplies()
  392.             end
  393.         else
  394.             sleep( 0.5 )
  395.         end
  396.     end
  397.  
  398.     --depth = depth + 1
  399.     --if math.fmod( depth, 10 ) == 0 then
  400.     --  print( "Descended "..depth.." metres." )
  401.     --end
  402.  
  403.     return true
  404. end
  405.  
  406. local function careForward()
  407.     if trackForward() then
  408.     elseif lookingAtChest() then
  409.         print("I think I found my chest. I almost tried to break it!")
  410.         turnLeft()
  411.         turnLeft()
  412.         return false
  413.     elseif turtle.dig() or turtle.attack() then
  414.         collect()
  415.     else
  416.         sleep( 0.5 )
  417.     end
  418.     return true
  419. end
  420.  
  421. function goCareTo( x, y, z, xd, zd )
  422.     while posinfo.depth > y do
  423.         if trackUp() then
  424.         elseif chestUp() then
  425.             print("I think I found my chest.")
  426.             return false
  427.         elseif turtle.digUp() or turtle.attackUp() then
  428.             collect()
  429.         else
  430.             sleep( 0.5 )
  431.         end
  432.     end
  433.  
  434.     if posinfo.xPos > x then
  435.         while posinfo.xDir ~= -1 do
  436.             turnLeft()
  437.         end
  438.         while posinfo.xPos > x do
  439.             if careForward() then
  440.                 sleep( 0.5 )
  441.             else
  442.                 return false
  443.             end
  444.         end
  445.     elseif posinfo.xPos < x then
  446.         while posinfo.xDir ~= 1 do
  447.             turnLeft()
  448.         end
  449.         while posinfo.xPos < x do
  450.             if careForward() then
  451.                 sleep( 0.5 )
  452.             else
  453.                 return false
  454.             end
  455.         end
  456.     end
  457.    
  458.     if posinfo.zPos > z then
  459.         while posinfo.zDir ~= -1 do
  460.             turnLeft()
  461.         end
  462.         while posinfo.zPos > z do
  463.             if careForward() then
  464.                 sleep( 0.5 )
  465.             else
  466.                 return false
  467.             end
  468.         end
  469.     elseif posinfo.zPos < z then
  470.         while posinfo.zDir ~= 1 do
  471.             turnLeft()
  472.         end
  473.         while posinfo.zPos < z do
  474.             if careForward() then
  475.                 sleep( 0.5 )
  476.             else
  477.                 return false
  478.             end
  479.         end
  480.     end
  481.    
  482.     while posinfo.depth < y do
  483.         if trackDown() then
  484.             --depth = depth + 1
  485.         elseif turtle.digDown() or turtle.attackDown() then
  486.             collect()
  487.         else
  488.             sleep( 0.5 )
  489.         end
  490.     end
  491.    
  492.     while posinfo.zDir ~= zd or posinfo.xDir ~= xd do
  493.         turnLeft()
  494.     end
  495.     return true
  496. end
  497.  
  498.  
  499. function goTo( x, y, z, xd, zd )
  500.     while posinfo.depth > y do
  501.         if trackUp() then
  502.             --depth = depth - 1
  503.         elseif turtle.digUp() or turtle.attackUp() then
  504.             collect()
  505.         else
  506.             sleep( 0.5 )
  507.         end
  508.     end
  509.  
  510.     if posinfo.xPos > x then
  511.         while posinfo.xDir ~= -1 do
  512.             turnLeft()
  513.         end
  514.         while posinfo.xPos > x do
  515.             if trackForward() then
  516.                 --xPos = xPos - 1
  517.             --elseif turtle.suck() then
  518.             elseif turtle.dig() or turtle.attack() then
  519.                 collect()
  520.             else
  521.                 sleep( 0.5 )
  522.             end
  523.         end
  524.     elseif posinfo.xPos < x then
  525.         while posinfo.xDir ~= 1 do
  526.             turnLeft()
  527.         end
  528.         while posinfo.xPos < x do
  529.             if trackForward() then
  530.                 --xPos = xPos + 1
  531.             elseif turtle.dig() or turtle.attack() then
  532.                 collect()
  533.             else
  534.                 sleep( 0.5 )
  535.             end
  536.         end
  537.     end
  538.    
  539.     if posinfo.zPos > z then
  540.         while posinfo.zDir ~= -1 do
  541.             turnLeft()
  542.         end
  543.         while posinfo.zPos > z do
  544.             if trackForward() then
  545.                 --zPos = zPos - 1
  546.             elseif turtle.dig() or turtle.attack() then
  547.                 collect()
  548.             else
  549.                 sleep( 0.5 )
  550.             end
  551.         end
  552.     elseif posinfo.zPos < z then
  553.         while posinfo.zDir ~= 1 do
  554.             turnLeft()
  555.         end
  556.         while posinfo.zPos < z do
  557.             if trackForward() then
  558.                 --zPos = zPos + 1
  559.             elseif turtle.dig() or turtle.attack() then
  560.                 collect()
  561.             else
  562.                 sleep( 0.5 )
  563.             end
  564.         end
  565.     end
  566.    
  567.     while posinfo.depth < y do
  568.         if trackDown() then
  569.             --depth = depth + 1
  570.         elseif turtle.digDown() or turtle.attackDown() then
  571.             collect()
  572.         else
  573.             sleep( 0.5 )
  574.         end
  575.     end
  576.    
  577.     while posinfo.zDir ~= zd or posinfo.xDir ~= xd do
  578.         turnLeft()
  579.     end
  580. end
  581.  
  582. if not refuel() then
  583.     print( "Out of Fuel" )
  584.     return
  585. end
  586.  
  587. print("Booting up...")
  588. if (fs.exists("posdata")) then
  589.     loadPos()
  590.     if (posinfo.done == true) then
  591.         print("Turtle is done.")
  592.         return
  593.     else
  594.         print("Turtle was interrupted...")
  595.         if wasDestroyed() then
  596.             print("I think I was picked up.")
  597.             return
  598.         end
  599.         if panic() then
  600.             goTo(0,posinfo.maxdepth,0,0,1)
  601.         else
  602.             bsuc,bdat = turtle.inspectUp()
  603.             if (string.find(bdat.name, "cobble")) then
  604.                 if (lookingAtChest()) then
  605.                     turnLeft()
  606.                     turnLeft()
  607.                     print("Aligned!")
  608.                 else
  609.                     turnLeft()
  610.                     turnLeft()
  611.                     if (lookingAtChest()) then
  612.                         turnLeft()
  613.                         turnLeft()
  614.                         print("Aligned!")
  615.                     else
  616.                         turnLeft()
  617.                         turnLeft()
  618.                         print("I'm close as home as possible, couldn't find chest.")
  619.                         return
  620.                     end
  621.                 end
  622.             else
  623.                 print("I'm close as home as possible, alignment failed.")          
  624.                 return
  625.             end
  626.         end
  627.     end
  628. else
  629.     print("Turtle is ready to work...")
  630.     savePos()
  631.     print("Finding Excavation Floor...")
  632.     while not turtle.detectDown() do
  633.         tryDown()
  634.     end
  635. end
  636.  
  637. print( "Excavating..." )
  638.  
  639. local alternate = 0
  640. local done = false
  641. while not done do
  642.     for n=1,size do
  643.         for m=1,size-1 do
  644.             if not tryForwards() then
  645.                 done = true
  646.                 break
  647.             end
  648.         end
  649.         if done then
  650.             break
  651.         end
  652.         if n<size then
  653.             if math.fmod(n + alternate,2) == 0 then
  654.                 turnLeft()
  655.                 if not tryForwards() then
  656.                     done = true
  657.                     break
  658.                 end
  659.                 turnLeft()
  660.             else
  661.                 turnRight()
  662.                 if not tryForwards() then
  663.                     done = true
  664.                     break
  665.                 end
  666.                 turnRight()
  667.             end
  668.         end
  669.     end
  670.     if done then
  671.         break
  672.     end
  673.    
  674.     if size > 1 then
  675.         if math.fmod(size,2) == 0 then
  676.             turnRight()
  677.         else
  678.             if alternate == 0 then
  679.                 turnLeft()
  680.             else
  681.                 turnRight()
  682.             end
  683.             alternate = 1 - alternate
  684.         end
  685.     end
  686.    
  687.     if not tryDown() then
  688.         done = true
  689.         break
  690.     end
  691.     tryDown()
  692.     tryDown()
  693.     if turtle.detectDown() then
  694.         if turtle.digDown() then
  695.             if not collect() then
  696.                 returnSupplies()
  697.             end
  698.         end
  699.     end
  700. end
  701.  
  702. print( "Returning to surface..." )
  703.  
  704. -- Return to where we started
  705. goTo( 0,0,0,0,-1 )
  706. unload( false )
  707. goTo( 0,0,0,0,1 )
  708.  
  709. posinfo.done = true
  710. savePos()
  711.  
  712. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement