Advertisement
debman

Untitled

Aug 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 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 base_size = tonumber( tArgs[1] )
  9. if base_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.log(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. local d = 1
  128. while not turtle.forward() do
  129. if turtle.detect() then
  130. if turtle.dig() then
  131. print("detected dig")
  132. if not collect() then
  133. returnSupplies()
  134. end
  135. else
  136. print("detected not dig")
  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. if not refuel() then
  147. print( "Not enough Fuel" )
  148. returnSupplies()
  149. end
  150. print(d)
  151. d = d + 1
  152. end
  153.  
  154. xPos = xPos + xDir
  155. zPos = zPos + zDir
  156. return true
  157. end
  158.  
  159. local function tryDown()
  160. if not refuel() then
  161. print( "Not enough Fuel" )
  162. returnSupplies()
  163. end
  164.  
  165. while not turtle.down() do
  166. if turtle.detectDown() then
  167. if turtle.digDown() then
  168. if not collect() then
  169. returnSupplies()
  170. end
  171. else
  172. return false
  173. end
  174. elseif turtle.attackDown() then
  175. if not collect() then
  176. returnSupplies()
  177. end
  178. else
  179. sleep( 0.5 )
  180. end
  181. end
  182.  
  183. depth = depth + 1
  184. if math.fmod( depth, 10 ) == 0 then
  185. print( "Descended "..depth.." metres." )
  186. end
  187.  
  188. return true
  189. end
  190.  
  191. local function turnLeft()
  192. turtle.turnLeft()
  193. xDir, zDir = -zDir, xDir
  194. end
  195.  
  196. local function turnRight()
  197. turtle.turnRight()
  198. xDir, zDir = zDir, -xDir
  199. end
  200.  
  201. function goTo( x, y, z, xd, zd )
  202. while depth < y do
  203. if turtle.down() then
  204. depth = depth + 1
  205. elseif turtle.digDown() or turtle.attackDown() then
  206. collect()
  207. else
  208. sleep( 0.5 )
  209. end
  210. end
  211.  
  212. if xPos > x then
  213. while xDir ~= -1 do
  214. turnLeft()
  215. end
  216. while xPos > x do
  217. if turtle.forward() then
  218. xPos = xPos - 1
  219. elseif turtle.dig() or turtle.attack() then
  220. collect()
  221. else
  222. sleep( 0.5 )
  223. end
  224. end
  225. elseif xPos < x then
  226. while xDir ~= 1 do
  227. turnLeft()
  228. end
  229. while xPos < x do
  230. if turtle.forward() then
  231. xPos = xPos + 1
  232. elseif turtle.dig() or turtle.attack() then
  233. collect()
  234. else
  235. sleep( 0.5 )
  236. end
  237. end
  238. end
  239.  
  240. if zPos > z then
  241. while zDir ~= -1 do
  242. turnLeft()
  243. end
  244. while zPos > z do
  245. if turtle.forward() then
  246. zPos = zPos - 1
  247. elseif turtle.dig() or turtle.attack() then
  248. collect()
  249. else
  250. sleep( 0.5 )
  251. end
  252. end
  253. elseif zPos < z then
  254. while zDir ~= 1 do
  255. turnLeft()
  256. end
  257. while zPos < z do
  258. if turtle.forward() then
  259. zPos = zPos + 1
  260. elseif turtle.dig() or turtle.attack() then
  261. collect()
  262. else
  263. sleep( 0.5 )
  264. end
  265. end
  266. end
  267.  
  268. while depth > y do
  269. if turtle.up() then
  270. depth = depth - 1
  271. elseif turtle.digUp() or turtle.attackUp() then
  272. collect()
  273. else
  274. sleep( 0.5 )
  275. end
  276. end
  277.  
  278. while zDir ~= zd or xDir ~= xd do
  279. turnLeft()
  280. end
  281. end
  282.  
  283. if not refuel() then
  284. print( "Out of Fuel" )
  285. return
  286. end
  287.  
  288.  
  289. local function excavate_area(size, alternate)
  290. for n=1,size do
  291. for m=1,size-1 do
  292. if not tryForwards() then
  293. return true
  294. end
  295. end
  296. if n<size then
  297. if math.fmod(n + alternate,2) == 0 then
  298. turnLeft()
  299. if not tryForwards() then
  300. return true
  301. end
  302. turnLeft()
  303. else
  304. turnRight()
  305. if not tryForwards() then
  306. return true
  307. end
  308. turnRight()
  309. end
  310. end
  311. end
  312. return false
  313. end
  314.  
  315. local function f(h, base_size)
  316. return base_size + p * math.log(h + 1)
  317. end
  318.  
  319. print( "Excavating..." )
  320.  
  321. local reseal = false
  322. turtle.select(1)
  323. if turtle.digDown() then
  324. reseal = true
  325. end
  326.  
  327. local alternate = 0
  328. local done = false
  329. local size = base_size
  330. while not done do
  331. done = excavate_area(size, alternate)
  332. if done then
  333. break
  334. end
  335.  
  336. if not tryDown() then
  337. done = true
  338. break
  339. else
  340. height = height + 1
  341. prev_size = size
  342. size = math.floor(f(height, base_size))
  343. if math.fmod(size, 2) == 1 then
  344. size = size + 1
  345. end
  346. local dx = math.floor((size - prev_size) / 2)
  347. for i=1,dx do
  348. tryForwards()
  349. end
  350. turnLeft()
  351. for i=1,dx do
  352. tryForwards()
  353. end
  354. turnRight()
  355. end
  356.  
  357. if size > 1 then
  358. if math.fmod(size,2) == 0 then
  359. turnRight()
  360. else
  361. if alternate == 0 then
  362. turnLeft()
  363. else
  364. turnRight()
  365. end
  366. alternate = 1 - alternate
  367. end
  368. end
  369. end
  370.  
  371. print( "Returning to surface..." )
  372.  
  373. -- Return to where we started
  374. goTo( 0,0,0,0,-1 )
  375. unload( false )
  376. goTo( 0,0,0,0,1 )
  377.  
  378. -- Seal the hole
  379. if reseal then
  380. turtle.placeDown()
  381. end
  382.  
  383. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement