Advertisement
Guest User

Untitled

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