Jharakn

Turtle Room Miner 0.4

Nov 27th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. -----------------------------
  2. --Mining Program by Jharakn--
  3. -----------------------------
  4.  
  5. --Variables--
  6. current_x = 1
  7. current_y = 1
  8. current_z = 1
  9. current_direction = 1
  10.  
  11. memory_x = 1
  12. memory_y = 1
  13. memory_z = 1
  14. memory_direction = 1
  15.  
  16. max_x = 1
  17. max_y = 1
  18. max_z = 1
  19.  
  20. skip_z_move = false
  21.  
  22. function Crash()
  23. print ("The Turtle Stopped Unexpectedly i.e. ErrorErrorBzzzzd")
  24. exit()
  25. end
  26.  
  27. function ForwardMine()
  28. while turtle.detect() == true do
  29. turtle.dig()
  30. sleep(1)
  31. end
  32.  
  33. local movecheck
  34. movecheck = turtle.forward()
  35.  
  36. if movecheck == false then
  37. sleep(5)
  38. turtle.dig()
  39. movecheck = turtle.forward()
  40. end
  41. if movecheck == false then
  42. sleep(5)
  43. turtle.dig()
  44. movecheck = turtle.forward()
  45. end
  46. if movecheck == false then
  47. sleep(5)
  48. turtle.dig()
  49. movecheck = turtle.forward()
  50. end
  51. return movecheck
  52. end
  53.  
  54. function UpMine()
  55. while turtle.detectUp() == true do
  56. turtle.digUp()
  57. sleep(1)
  58. end
  59.  
  60. local movecheck
  61. movecheck = turtle.up()
  62.  
  63. if movecheck == false then
  64. sleep(5)
  65. turtle.digUp()
  66. movecheck = turtle.up()
  67. end
  68. if movecheck == false then
  69. sleep(5)
  70. turtle.digUp()
  71. movecheck = turtle.up()
  72. end
  73. if movecheck == false then
  74. sleep(5)
  75. turtle.digUp()
  76. movecheck = turtle.up()
  77. end
  78. return movecheck
  79. end
  80.  
  81. function DownMine()
  82. while turtle.detectDown() == true do
  83. turtle.digDown()
  84. sleep(1)
  85. end
  86.  
  87. local movecheck
  88. movecheck = turtle.down()
  89.  
  90. if movecheck == false then
  91. sleep(5)
  92. turtle.digDown()
  93. movecheck = turtle.down()
  94. end
  95. if movecheck == false then
  96. sleep(5)
  97. turtle.digDown()
  98. movecheck = turtle.down()
  99. end
  100. if movecheck == false then
  101. sleep(5)
  102. turtle.digDown()
  103. movecheck = turtle.down()
  104. end
  105. return movecheck
  106. end
  107.  
  108. function Turn(direction) --Note facing is clockwise so 1 is Forwards, 2 is right, 3 is backwards, 4 is left
  109. while current_direction ~= direction do
  110. if current_direction < direction then
  111. if current_direction == 1 and direction == 4 then
  112. turtle.turnLeft()
  113. current_direction = 4
  114. else
  115. turtle.turnRight()
  116. current_direction = current_direction + 1
  117. end
  118. else
  119. if current_direction == 4 and direction == 1 then
  120. turtle.turnRight()
  121. current_direction = 1
  122. else
  123. turtle.turnLeft()
  124. current_direction = current_direction - 1
  125. end
  126. end
  127. end
  128. end
  129.  
  130. function HasInventorySpace()
  131. local empty_space = false
  132. local i = 1
  133. repeat
  134. if turtle.getItemCount(i) == 0 then
  135. empty_space = true
  136. end
  137. i = i + 1
  138. until i == 17
  139. if empty_space == false then
  140. print("Inventory check failed")
  141. end
  142. return empty_space
  143. end
  144.  
  145. function HasFuel()
  146. local cost_to_return = current_x + current_y + current_z + 20
  147. if turtle.getFuelLevel() < cost_to_return then
  148. print("Fuel check failed")
  149. return false
  150. else
  151. return true
  152. end
  153. end
  154.  
  155. function ReturnToStart(return_y)
  156. Turn(3)
  157.  
  158. while current_z ~= 1 do
  159. ForwardMine()
  160. current_z = current_z - 1
  161. end
  162.  
  163. Turn(4)
  164.  
  165. while current_x ~= 1 do
  166. ForwardMine()
  167. current_x = current_x - 1
  168. end
  169.  
  170. if return_y == true then
  171. while current_y ~= 1 do
  172. DownMine()
  173. current_y = current_y - 1
  174. end
  175. end
  176. print("remaining Fuel"..turtle.getFuelLevel())
  177. end
  178.  
  179. function UnloadCycle()
  180. Turn(3)
  181. local i = 1
  182. repeat
  183. turtle.select(i)
  184. if turtle.drop() == false then
  185. turtle.dropDown()
  186. end
  187. i = i + 1
  188. until i == 17
  189.  
  190. if turtle.getFuelLevel() < 1000 then
  191. Turn(2)
  192. ForwardMine()
  193. ForwardMine()
  194. ForwardMine()
  195. Turn(3)
  196. turtle.select(1)
  197. if turtle.suck() == false then
  198. Crash()
  199. end
  200. turtle.refuel()
  201. Turn(4)
  202. ForwardMine()
  203. ForwardMine()
  204. ForwardMine()
  205. Turn(3)
  206. if turtle.drop() == false then
  207. Crash()
  208. end
  209. end
  210. end
  211.  
  212. function ReturnToMemory()
  213. while current_y ~= memory_y do
  214. UpMine()
  215. current_y = current_y + 1
  216. end
  217. while current_x ~= memory_x do
  218. Turn(2)
  219. ForwardMine()
  220. current_x = current_x + 1
  221. end
  222. while current_z ~= memory_z do
  223. Turn(1)
  224. ForwardMine()
  225. current_z = current_z + 1
  226. end
  227. Turn(memory_direction)
  228. end
  229.  
  230. function DodgeBlockage(invert)
  231. Turn(1)
  232.  
  233. if ForwardMine() == false then
  234. print("Turtle Blocked")
  235. while turtle.forward() == false do
  236. sleep(5)
  237. end
  238. end
  239.  
  240. current_y = current_y + 1
  241.  
  242. if invert == false then
  243. Turn(2)
  244. if ForwardMine() == false then
  245. print("Turtle Blocked")
  246. while turtle.forward() == false do
  247. sleep(5)
  248. end
  249. end
  250.  
  251. current_x = current_x + 1
  252.  
  253. Turn(3)
  254. if ForwardMine() == false then
  255. if current_x ~= max_x then
  256. Turn(2)
  257. if ForwardMine() == false then
  258. print("Turtle Blocked")
  259. while turtle.forward() == false do
  260. sleep(5)
  261. end
  262. end
  263. current_x = current_x + 1
  264. Turn(3)
  265. if ForwardMine() == false then
  266. print("Turtle Blocked")
  267. while turtle.forward() == false do
  268. sleep(5)
  269. end
  270. end
  271. else
  272. skip_z_move = true
  273. end
  274. end
  275. current_y = current_y - 1
  276. else
  277. Turn(4)
  278. if ForwardMine() == false then
  279. print("Turtle Blocked")
  280. while turtle.forward() == false do
  281. sleep(5)
  282. end
  283. end
  284. current_x = current_x - 1
  285.  
  286. Turn(3)
  287. if ForwardMine() == false then
  288. if current_x ~= 1 then
  289. Turn(4)
  290. if ForwardMine() == false then
  291. print("Turtle Blocked")
  292. while turtle.forward() == false do
  293. sleep(5)
  294. end
  295. end
  296. current_x = current_x - 1
  297. Turn(3)
  298. if ForwardMine() == false then
  299. print("Turtle Blocked")
  300. while turtle.forward() == false do
  301. sleep(5)
  302. end
  303. end
  304. else
  305. skip_z_move = true
  306. end
  307. end
  308. current_y = current_y - 1
  309. end
  310. end
  311.  
  312.  
  313. ----------------------------
  314. --Main Program version 0.2--
  315. ----------------------------
  316.  
  317. term.clear()
  318. term.setCursorPos(1, 1)
  319. print ("Please enter how WIDE you want me to dig.")
  320. max_x = tonumber(read())
  321.  
  322. term.clear()
  323. term.setCursorPos(1, 1)
  324. print ("Please enter how HIGH you want me to dig.")
  325. max_y = tonumber(read())
  326.  
  327. term.clear()
  328. term.setCursorPos(1, 1)
  329. print ("Please enter how FAR BACK you want me to dig.")
  330. max_z = tonumber(read())
  331.  
  332. local startup = true
  333. local invert_mining = false
  334.  
  335. repeat
  336.  
  337. if startup == false then
  338. ReturnToStart(false)
  339. Turn(1)
  340. UpMine()
  341. current_y = current_y + 1
  342. startup = true
  343. invert_mining = false
  344. end
  345.  
  346. repeat
  347.  
  348. Turn(1)
  349.  
  350. if startup == false or skip_z_move == false then
  351. ForwardMine()
  352. current_z = current_z + 1
  353. end
  354.  
  355. skip_y_move = false
  356.  
  357. startup = false
  358.  
  359. if invert_mining == true then
  360. repeat
  361. Turn(4)
  362. if ForwardMine() == true then
  363. current_x = current_x - 1
  364. else
  365. DodgeBlockage(invert_mining)
  366. end
  367.  
  368. if HasInventorySpace() == false or HasFuel() == false then
  369. print("Returning to base for fuel or to unload inventory")
  370. memory_x = current_x
  371. memory_y = current_y
  372. memory_z = current_z
  373. memory_direction = current_direction
  374. ReturnToStart(true)
  375. UnloadCycle()
  376. ReturnToMemory()
  377. end
  378. until current_x == 1
  379. else
  380. repeat
  381. Turn(2)
  382. if ForwardMine() == true then
  383. current_x = current_x + 1
  384. else
  385. DodgeBlockage(invert_mining)
  386. end
  387.  
  388. if HasInventorySpace() == false or HasFuel() == false then
  389. print("Returning to base for fuel or to unload inventory")
  390. memory_x = current_x
  391. memory_y = current_y
  392. memory_z = current_z
  393. memory_direction = current_direction
  394. ReturnToStart(true)
  395. UnloadCycle()
  396. ReturnToMemory()
  397. end
  398. until current_x == max_x
  399. end
  400.  
  401. if invert_mining == true then
  402. invert_mining = false
  403. else
  404. invert_mining = true
  405. end
  406. until current_z == max_z
  407. until current_y == max_y
  408.  
  409.  
  410.  
  411. ReturnToStart(true)
  412. print("Finished")
Advertisement
Add Comment
Please, Sign In to add comment