Advertisement
fatboychummy

simplisticDig

Aug 1st, 2019
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. local skip = ...
  2. local sDistance = 2
  3. if type(tonumber(skip)) == "number" then
  4.   sDistance = tonumber(skip)
  5. end
  6. local dist = 0
  7. local maxDist = 0
  8. local dug = 0
  9.  
  10. local function checkInv()
  11.   local o = 0
  12.   for i = 1, 16 do
  13.     if turtle.getItemCount(i) > 0 then
  14.       o = o + 1
  15.     end
  16.   end
  17.   if o >= 15 then return true end
  18.   return false
  19. end
  20.  
  21. local function dropEverything()
  22.   for i = 1, 16 do
  23.     turtle.select(i)
  24.     turtle.drop()
  25.   end
  26. end
  27.  
  28. local function revert()
  29.   turtle.up = turtle.native.up
  30.   turtle.down = turtle.native.down
  31.   turtle.dig = turtle.native.dig
  32.   turtle.digDown = turtle.native.digDown
  33. end
  34.  
  35. local function changeTurtleStuff()
  36.   function turtle.down()
  37.     local ok, err = turtle.native.down()
  38.     if ok then
  39.       dist = dist + 1
  40.       if dist > maxDist then
  41.         maxDist = dist
  42.       end
  43.     end
  44.     return ok, err
  45.   end
  46.  
  47.   function turtle.up()
  48.     local ok, err = turtle.native.up()
  49.     if ok then
  50.       dist = dist - 1
  51.     end
  52.     return ok, err
  53.   end
  54.  
  55.   function turtle.dig()
  56.     local ok, err = turtle.native.dig()
  57.     if ok then
  58.       dug = dug + 1
  59.     end
  60.     return ok, err
  61.   end
  62.  
  63.   function turtle.digDown()
  64.     local ok, err = turtle.native.digDown()
  65.     if ok then
  66.       dug = dug + 1
  67.     end
  68.     return ok, err
  69.   end
  70. end
  71.  
  72.  
  73. local function checkFuel()
  74.   local fuel = turtle.getFuelLevel()
  75.  
  76.   print("We normally need to dig about 50-100 blocks down.")
  77.   print("That means we normally need about 100-200 fuel.")
  78.   print("We have " .. tostring(fuel) .. " / 200 fuel.")
  79.  
  80.   if fuel >= 100 and fuel <= 200 then
  81.     print("We may be able to make it, but we may not.")
  82.     print("Wait 10 seconds to start, or terminate now to stop.")
  83.     os.sleep(10)
  84.   end
  85.   if fuel < 100 then
  86.     error("We do not have enough fuel.")
  87.   end
  88. end
  89.  
  90. local function returnToSurface(t)
  91.   print("Made it to " .. tostring(maxDist) .. ", returning.")
  92.   while dist > 0 do
  93.     turtle.up()
  94.   end
  95.   if t then
  96.     local cFlag = false
  97.     for i = 1, 4 do
  98.       local a = peripheral.getType("front")
  99.       if a then
  100.         if a:find("chest") then
  101.           cFlag = true
  102.           break
  103.         end
  104.       end
  105.       turtle.turnRight()
  106.     end
  107.     if cFlag then
  108.       dropEverything()
  109.     else
  110.       error("No chest, inventory too full.")
  111.     end
  112.  
  113.     checkFuel()
  114.   end
  115. end
  116.  
  117. local function go(distt)
  118.   -- move down two blocks to be just under the top block
  119.   checkFuel()
  120.   local function hopDown(dy)
  121.     for i = 1, dy do
  122.       turtle.digDown()
  123.       turtle.down()
  124.     end
  125.   end
  126.  
  127.   hopDown(distt)
  128.  
  129.   -- dig circle then go down
  130.   local fails = 0
  131.   repeat
  132.     if fails == 0 then
  133.       for i = 1, 3 do
  134.         turtle.dig()
  135.         turtle.turnRight()
  136.       end
  137.       turtle.dig()
  138.       turtle.digDown()
  139.       if checkInv() then
  140.         returnToSurface(true)
  141.         checkFuel()
  142.         hopDown(maxDist - 1)
  143.       end
  144.     end
  145.     local f = turtle.down()
  146.     if not f then
  147.       turtle.attackDown()
  148.       turtle.digDown()
  149.       fails = fails + 1
  150.     else
  151.       fails = 0
  152.     end
  153.   until fails > 10
  154.  
  155.   returnToSurface()
  156.  
  157.   print("Job complete")
  158.   print("Went " .. tostring(maxDist) .. " blocks down.")
  159.   print("That means " .. tostring(maxDist * 2) .. " fuel was used.")
  160.   print("Dug " .. tostring(dug) .. " blocks total.")
  161. end
  162.  
  163. changeTurtleStuff()
  164. if skip == "unknown" then
  165.   sDistance = 0
  166.   while turtle.down() do end
  167. end
  168. go(sDistance)
  169. revert()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement