Advertisement
Guest User

Excavate line

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