Advertisement
ConnorFreebairn

Miner

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