Advertisement
CBredlow

3x3 attempt

Apr 3rd, 2024 (edited)
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.19 KB | Source Code | 0 0
  1. -- SPDX-FileCopyrightText: 2017 Daniel Ratcliffe
  2. --
  3. -- SPDX-License-Identifier: LicenseRef-CCPL
  4.  
  5. if not turtle then
  6.     printError("Requires a Turtle")
  7.     return
  8. end
  9.  
  10. local tArgs = { ... }
  11. if #tArgs ~= 1 then
  12.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  13.     print("Usage: " .. programName .. " <length>")
  14.     return
  15. end
  16.  
  17. -- Mine in a quarry pattern until we hit something we can't dig
  18. local length = tonumber(tArgs[1])
  19. if length < 1 then
  20.     print("Tunnel length must be positive")
  21.     return
  22. end
  23. local collected = 0
  24.  
  25. local function collect()
  26.     collected = collected + 1
  27.     if math.fmod(collected, 25) == 0 then
  28.         print("Mined " .. collected .. " items.")
  29.     end
  30. end
  31.  
  32. local function tryDig()
  33.     while turtle.detect() do
  34.         if turtle.dig() then
  35.             collect()
  36.             sleep(0.5)
  37.         else
  38.             return false
  39.         end
  40.     end
  41.     return true
  42. end
  43.  
  44. local function tryDigUp()
  45.     while turtle.detectUp() do
  46.         if turtle.digUp() then
  47.             collect()
  48.             sleep(0.5)
  49.         else
  50.             return false
  51.         end
  52.     end
  53.     return true
  54. end
  55.  
  56. local function tryDigDown()
  57.     while turtle.detectDown() do
  58.         if turtle.digDown() then
  59.             collect()
  60.             sleep(0.5)
  61.         else
  62.             return false
  63.         end
  64.     end
  65.     return true
  66. end
  67.  
  68. local function refuel()
  69.     local fuelLevel = turtle.getFuelLevel()
  70.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  71.         return
  72.     end
  73.  
  74.     local function tryRefuel()
  75.         for n = 1, 16 do
  76.             if turtle.getItemCount(n) > 0 then
  77.                 turtle.select(n)
  78.                 if turtle.refuel(1) then
  79.                     turtle.select(1)
  80.                     return true
  81.                 end
  82.             end
  83.         end
  84.         turtle.select(1)
  85.         return false
  86.     end
  87.  
  88.     if not tryRefuel() then
  89.         print("Add more fuel to continue.")
  90.         while not tryRefuel() do
  91.             os.pullEvent("turtle_inventory")
  92.         end
  93.         print("Resuming Tunnel.")
  94.     end
  95. end
  96.  
  97. local function tryUp()
  98.     refuel()
  99.     while not turtle.up() do
  100.         if turtle.detectUp() then
  101.             if not tryDigUp() then
  102.                 return false
  103.             end
  104.         elseif turtle.attackUp() then
  105.             collect()
  106.         else
  107.             sleep(0.5)
  108.         end
  109.     end
  110.     return true
  111. end
  112.  
  113. local function tryDown()
  114.     refuel()
  115.     while not turtle.down() do
  116.         if turtle.detectDown() then
  117.             if not tryDigDown() then
  118.                 return false
  119.             end
  120.         elseif turtle.attackDown() then
  121.             collect()
  122.         else
  123.             sleep(0.5)
  124.         end
  125.     end
  126.     return true
  127. end
  128.  
  129. local function tryForward()
  130.     refuel()
  131.     while not turtle.forward() do
  132.         if turtle.detect() then
  133.             if not tryDig() then
  134.                 return false
  135.             end
  136.         elseif turtle.attack() then
  137.             collect()
  138.         else
  139.             sleep(0.5)
  140.         end
  141.     end
  142.     return true
  143. end
  144.  
  145. print("Tunnelling...")
  146.  
  147. for n = 1, length do
  148. --    turtle.placeDown()
  149. -- 0 1 2     N
  150. -- 3 4 5   W   E
  151. -- 6 7 8     S
  152. -- do center column
  153. -- Starting N
  154.     tryDigUp()--1
  155.     tryDigDown() -- 7
  156.     turtle.turnLeft() --W
  157.     tryDig() -- 3
  158.     tryUp()
  159.     tryDig() --0
  160.     turtle.turnRight() -- N
  161.     turtle.turnRight() -- E
  162.     tryDig() --2
  163.     tryDown()
  164.     tryDig() -- 5
  165.     tryDown()
  166.     tryDig() --8
  167.     turtle.turnLeft() -- N
  168.     turtle.turnLeft() -- W
  169.     tryDig() -- 6
  170.     turtle.turnRight() --N
  171.     tryUp() -- return to center
  172.  
  173.  
  174.  
  175.     if n < length then
  176.         tryDig() --4
  177.         if not tryForward() then
  178.             print("Aborting Tunnel.")
  179.             break
  180.         end
  181.     else
  182.         print("Tunnel complete.")
  183.     end
  184.  
  185. end
  186.  
  187. --[[
  188. print( "Returning to start..." )
  189.  
  190. -- Return to where we started
  191. turtle.turnLeft()
  192. turtle.turnLeft()
  193. while depth > 0 do
  194.     if turtle.forward() then
  195.         depth = depth - 1
  196.     else
  197.         turtle.dig()
  198.     end
  199. end
  200. turtle.turnRight()
  201. turtle.turnRight()
  202. ]]
  203.  
  204. print("Tunnel complete.")
  205. print("Mined " .. collected .. " items total.")
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement