Advertisement
kovakovi2000

CC: Updated Turtle With Lava Refuel

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