yohanbohan

TurtleTunnel

Mar 20th, 2024 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. local tArgs = {...}
  2. if #tArgs ~= 2 then
  3. print("Usage: turtletunnel <length> <width> Place a torch in slot 2 for autoplacing torches")
  4. return
  5. end
  6.  
  7. -- mine in a tunnel pattern until something undiggable is found (ie. bedrock)
  8. Length = tonumber( tArgs[1] )
  9. if Length < 1 then
  10. print("tunnel length must be positive")
  11. return
  12. end
  13.  
  14. Width = tonumber(tArgs[2])
  15. if Width < 1 then
  16. print("Tunnel width must be positive integer")
  17. end
  18.  
  19. local unloaded = 0
  20. local collected = 0
  21.  
  22. local xPos,zPos = 0,0
  23. local xDir,zDir = 0,1
  24.  
  25. local function unload( _bKeepOneFuelStack )
  26. print( "Unloading items..." )
  27. for n=1,16 do
  28. local nCount = turtle.getItemCount(n)
  29. if nCount > 0 then
  30. turtle.select(n)
  31. local bDrop = true
  32. if turtle.getItemDetail()["name"]=="minecraft:torch" and n==2 then
  33. bDrop = false
  34. end
  35. if _bKeepOneFuelStack and turtle.refuel(0) then
  36. bDrop = false
  37. _bKeepOneFuelStack = false
  38. end
  39. if bDrop then
  40. turtle.drop()
  41. unloaded = unloaded + nCount
  42. end
  43. end
  44. end
  45. collected = 0
  46. turtle.select(1)
  47. end
  48.  
  49. local function returnSupplies()
  50. local x,z,xd,zd = xPos,zPos,xDir,zDir
  51. print( "Returning to surface..." )
  52. goTo( 0,0,0,-1 )
  53.  
  54. local fuelNeeded = 2*(x+z) + 1
  55. if not refuel( fuelNeeded ) then
  56. unload( true )
  57. print( "Waiting for fuel" )
  58. while not refuel( fuelNeeded ) do
  59. os.pullEvent( "turtle_inventory" )
  60. end
  61. else
  62. unload( true )
  63. end
  64.  
  65. print( "Resuming mining..." )
  66. goTo( x,z,xd,zd )
  67. end
  68.  
  69. local function collect()
  70. local bFull = true
  71. local nTotalItems = 0
  72. for n=1,16 do
  73. local nCount = turtle.getItemCount(n)
  74. if nCount == 0 then
  75. bFull = false
  76. end
  77. nTotalItems = nTotalItems + nCount
  78. end
  79.  
  80. if nTotalItems > collected then
  81. collected = nTotalItems
  82. if math.fmod(collected + unloaded, 50) == 0 then
  83. print( "Mined "..(collected + unloaded).." items." )
  84. end
  85. end
  86.  
  87. if bFull then
  88. print( "No empty slots left." )
  89. return false
  90. end
  91. return true
  92. end
  93.  
  94. function refuel( amount )
  95. local fuelLevel = turtle.getFuelLevel()
  96. if fuelLevel == "unlimited" then
  97. return true
  98. end
  99.  
  100. local needed = amount or (xPos + zPos + 2)
  101. if turtle.getFuelLevel() < needed then
  102. local fueled = false
  103. for n=1,16 do
  104. if turtle.getItemCount(n) > 0 then
  105. turtle.select(n)
  106. if turtle.refuel(1) then
  107. while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  108. turtle.refuel(1)
  109. end
  110. if turtle.getFuelLevel() >= needed then
  111. turtle.select(1)
  112. return true
  113. end
  114. end
  115. end
  116. end
  117. turtle.select(1)
  118. return false
  119. end
  120.  
  121. return true
  122. end
  123.  
  124. local function tryForwards()
  125. if not refuel() then
  126. print( "Not enough Fuel" )
  127. returnSupplies()
  128. end
  129.  
  130. while not turtle.forward() do
  131. if turtle.detect() then
  132. if turtle.dig() then
  133. if not collect() then
  134. returnSupplies()
  135. end
  136. else
  137. return false
  138. end
  139. elseif turtle.attack() then
  140. if not collect() then
  141. returnSupplies()
  142. end
  143. else
  144. sleep( 0.5 )
  145. end
  146. end
  147.  
  148. xPos = xPos + xDir
  149. zPos = zPos + zDir
  150. return true
  151. end
  152.  
  153.  
  154. local function turnLeft()
  155. turtle.turnLeft()
  156. xDir, zDir = -zDir, xDir
  157. end
  158.  
  159. local function turnRight()
  160. turtle.turnRight()
  161. xDir, zDir = zDir, -xDir
  162. end
  163.  
  164. function goTo( x, z, xd, zd )
  165.  
  166. if xPos > x then
  167. while xDir ~= -1 do
  168. turnLeft()
  169. end
  170. while xPos > x do
  171. if turtle.forward() then
  172. xPos = xPos - 1
  173. elseif turtle.dig() or turtle.attack() then
  174. collect()
  175. else
  176. sleep( 0.5 )
  177. end
  178. end
  179. elseif xPos < x then
  180. while xDir ~= 1 do
  181. turnLeft()
  182. end
  183. while xPos < x do
  184. if turtle.forward() then
  185. xPos = xPos + 1
  186. elseif turtle.dig() or turtle.attack() then
  187. collect()
  188. else
  189. sleep( 0.5 )
  190. end
  191. end
  192. end
  193.  
  194. if zPos > z then
  195. while zDir ~= -1 do
  196. turnLeft()
  197. end
  198. while zPos > z do
  199. if turtle.forward() then
  200. zPos = zPos - 1
  201. elseif turtle.dig() or turtle.attack() then
  202. collect()
  203. else
  204. sleep( 0.5 )
  205. end
  206. end
  207. elseif zPos < z then
  208. while zDir ~= 1 do
  209. turnLeft()
  210. end
  211. while zPos < z do
  212. if turtle.forward() then
  213. zPos = zPos + 1
  214. elseif turtle.dig() or turtle.attack() then
  215. collect()
  216. else
  217. sleep( 0.5 )
  218. end
  219. end
  220. end
  221.  
  222. while zDir ~= zd or xDir ~= xd do
  223. turnLeft()
  224. end
  225. end
  226.  
  227. if not refuel() then
  228. print( "Out of Fuel" )
  229. return
  230. end
  231.  
  232. print( "Tunneling..." )
  233.  
  234. --[[ local reseal = false
  235. turtle.select(1)
  236. if turtle.digDown() then
  237. reseal = true
  238. end --]]
  239. for m=1, Length do --for loop that mines out the length of command
  240. tryForwards()
  241. turtle.digUp()
  242. turtle.digDown()
  243. if turtle.getItemDetail(2)["name"]=="minecraft:torch" and m%10==0 then
  244. turtle.select(2)
  245. turtle.placeDown()
  246. turtle.select(1)
  247. end
  248. if m%2==1 then
  249. turnRight()
  250. elseif m%2==0 then
  251. turnLeft()
  252. end
  253. for n=2, Width do --for loop that mines out the width of command
  254. tryForwards()
  255. turtle.digUp()
  256. turtle.digDown()
  257. if turtle.getItemDetail(2)["name"]=="minecraft:torch" and n%8==0 and m%8==0 then
  258. turtle.select(2)
  259. turtle.placeDown()
  260. turtle.select(1)
  261. end
  262. end
  263. if m%2==1 then
  264. turnLeft()
  265. end
  266. if m%2==0 then
  267. turnRight()
  268. end
  269.  
  270. -- turnLeft()
  271.  
  272. --[[for l=2, Width do -- for loop that moves back to origin
  273. tryForwards()
  274. end
  275. turnRight() -- corrects direction to face forward to continue a length mining for loop
  276. -- goTo(m+1, 1-Width, -zDir, xDir) -- not sure if works need to test --]]
  277.  
  278.  
  279. end
  280.  
  281. --[[
  282. local alternate = 0
  283. local done = false
  284. while not done do
  285. for n=1,size do
  286. for m=1,size-1 do
  287. if not tryForwards() then
  288. done = true
  289. break
  290. end
  291. end
  292. if done then
  293. break
  294. end
  295. if n<size then
  296. if math.fmod(n + alternate,2) == 0 then
  297. turnLeft()
  298. if not tryForwards() then
  299. done = true
  300. break
  301. end
  302. turnLeft()
  303. else
  304. turnRight()
  305. if not tryForwards() then
  306. done = true
  307. break
  308. end
  309. turnRight()
  310. end
  311. end
  312. end
  313. if done then
  314. break
  315. end
  316.  
  317. if size > 1 then
  318. if math.fmod(size,2) == 0 then
  319. turnRight()
  320. else
  321. if alternate == 0 then
  322. turnLeft()
  323. else
  324. turnRight()
  325. end
  326. alternate = 1 - alternate
  327. end
  328. end
  329.  
  330. end ]]
  331.  
  332. print( "Returning to surface..." )
  333.  
  334. -- Return to where we started
  335. goTo( 0,0,0,-1 )
  336. unload( false )
  337. goTo( 0,0,0,1 )
  338.  
  339. -- Seal the hole
  340.  
  341. print( "Mined "..(collected + unloaded).." items total." )
Add Comment
Please, Sign In to add comment