 # YATP

Jun 23rd, 2021
649
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. -- Created by: pastebin.com/u/clintwk
2.
3. local function progHelp()
4.     print( "Usage: " )
5.     print( shell.getRunningProgram().." <length> (branches) (L or R)" )
6.     print( "Place path blocks in slot 1!" )
7. end
8. local tArgs = { ... }
9. if #tArgs <= 3 and #tArgs > 0 then
10.     if #tArgs == 2 then
11.         print( "Must put argument for branch direction (L) or (R)" )
12.         progHelp()
13.         return
14.     end
15. else
16.     progHelp()
17.     return
18. end
19.
20. -- Mine in a quarry pattern until we hit something we can't dig
21. if not tonumber( tArgs ) then
22.     print( "Tunnel length must be a positive number" )
23.     progHelp()
24.     return
25. end
26. local length = tonumber( tArgs )
27. local branches = 1
28. local direction = "L"
29. if length < 1 then
30.     print( "Tunnel length must be a positive number" )
31.     progHelp()
32.     return
33. end
34.
35. if #tArgs == 3 then
36.     if not tonumber( tArgs ) then
37.             print( "Branches must be a positive number" )
38.         progHelp()
39.         return
40.     end
41.     branches = tonumber( tArgs )
42.     if branches < 1 then
43.         print( "Number of branches must be a positive number" )
44.         progHelp()
45.         return
46.     end
47.     direction = tArgs
48.     if direction ~= "L" and direction ~= "l" and direction ~= "R" and direction ~= "r" then
49.         print( "Please enter the direction you would like your branches to go (either L or R)" )
50.         progHelp()
51.         return
52.     end
53.     if direction == "L" or direction == "l" then
54.         direction = "L"
55.     else
56.         direction = "R"
57.     end
58. end
59.
60. local depth = 0
61. local collected = 0
62.
63. local function collect()
64.     collected = collected + 1
65.     if math.fmod(collected, 25) == 0 then
66.         print( "Mined "..collected.." items." )
67.     end
68. end
69.
70. local function tryDig()
71.     while turtle.detect() do
72.         if turtle.dig() then
73.             collect()
74.             sleep(0.2)
75.         else
76.             return false
77.         end
78.     end
79.     return true
80. end
81.
82. local function tryDigUp()
83.     while turtle.detectUp() do
84.         if turtle.digUp() then
85.             collect()
86.             sleep(0.2)
87.         else
88.             return false
89.         end
90.     end
91.     return true
92. end
93. local function refuel()
94.     local fuelLevel = turtle.getFuelLevel()
95.     if fuelLevel == "unlimited" or fuelLevel > 0 then
96.         return
97.     end
98.
99.     local function tryRefuel()
100.         for n=1,16 do
101.             if turtle.getItemCount(n) > 0 then
102.                 turtle.select(n)
103.                 if turtle.refuel(1) then
104.                     turtle.select(1)
105.                     return true
106.                 end
107.             end
108.         end
109.         turtle.select(1)
110.         return false
111.     end
112.
113.     if not tryRefuel() then
114.         print( "Add more fuel to continue." )
115.         while not tryRefuel() do
116.             sleep(1)
117.         end
118.         print( "Resuming Tunnel." )
119.     end
120. end
121.
122. local function tryUp()
123.     refuel()
124.     while not turtle.up() do
125.         if turtle.detectUp() then
126.             if not tryDigUp() then
127.                 return false
128.             end
129.         elseif turtle.attackUp() then
130.             collect()
131.         else
132.             sleep( 0.2 )
133.         end
134.     end
135.     return true
136. end
137.
138. local function tryForward()
139.     refuel()
140.     while not turtle.forward() do
141.         if turtle.detect() then
142.             if not tryDig() then
143.                 return false
144.             end
145.         elseif turtle.attack() then
146.             collect()
147.         else
148.             sleep( 0.2 )
149.         end
150.     end
151.     return true
152. end
153.
154. print( "Tunnelling..." )
155.
156. local function tunnel(tunStart, tunEnd)
157.     for n=tunStart,tunEnd do
158.         turtle.placeDown()
159.         tryDigUp()
160.         turtle.suck()
161.
162.         if n<length then
163.             tryDig()
164.             if not tryForward() then
165.                 print( "Aborting Tunnel." )
166.                 break
167.             end
168.         else
169.             print( "Tunnel complete." )
170.         end
171.
172.     end
173. end
174. local function newBranch()
175.     tunnel(1, 3)
176.     if direction == "L" then
177.         turtle.turnLeft()
178.         direction = "R"
179.     else
180.         turtle.turnRight()
181.         direction = "L"
182.     end
183. end
184.
185. local function connectBranch()
186.     if direction == "L" then
187.         turtle.turnRight()
188.     else
189.         turtle.turnLeft()
190.     end
191.     tunnel(1, 2)
192.     turtle.turnRight()
193.     turtle.turnRight()
194.     tunnel(1, 2)
195. end
196.
197. for b=1,branches do
198.     tunnel(1, length)
199.     if b ~= 1 then
200.         connectBranch()
201.     end
202.     if b ~= branches then
203.         if b == 1 then
204.             if direction == "L" then
205.                 turtle.turnLeft()
206.             else
207.                 turtle.turnRight()
208.             end
209.         end
210.         newBranch()
211.     end
212. end
213. --[[
214. print( "Returning to start..." )
215.
217. turtle.turnLeft()
218. turtle.turnLeft()
219. while depth > 0 do
220.     if turtle.forward() then
221.         depth = depth - 1
222.     else
223.         turtle.dig()
224.     end
225. end
226. turtle.turnRight()
227. turtle.turnRight()
228. ]]
229.
230. print( "Tunnel complete." )
231. print( "Mined "..collected.." items total." )
RAW Paste Data