Jharakn

Turtle Room Miner 0.6

Dec 1st, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.44 KB | None | 0 0
  1. --Variables--
  2. current_x = 1
  3. current_y = 1
  4. current_z = 1
  5. current_direction = 1
  6.  
  7. memory_x = 1
  8. memory_y = 1
  9. memory_z = 1
  10. memory_direction = 1
  11. memory_y_offset = 0
  12.  
  13. max_x = 1
  14. max_y = 1
  15. max_z = 1
  16.  
  17. flag_offset = false
  18. y_offset = 0
  19.  
  20. function waitForReset()
  21. local counter = 1
  22. while turtle.forward() == false do
  23. term.clear()
  24. term.setCursorPos (1,1)
  25. print("Turtle is stuck, count = "..tostring(counter))
  26. counter = counter + 1
  27. if counter == 200 then
  28. print("turtle is fed up of being ignored and has shut down")
  29. exit()
  30. end
  31. sleep(1)
  32. end
  33. updateCoords(current_direction)
  34. end
  35.  
  36. function mineForward()
  37. local counter = 0
  38. local flag_success = true
  39. while turtle.forward() == false do
  40. turtle.dig()
  41. turtle.attack()
  42. sleep(1)
  43. counter = counter + 1
  44. if counter == 4 then
  45. flag_success = false
  46. break
  47. end
  48. end
  49. if flag_success == true then
  50. return true
  51. else
  52. return false
  53. end
  54. end
  55.  
  56. function mineUp()
  57. local counter = 0
  58. local flag_success = true
  59. while turtle.up() == false do
  60. turtle.digUp()
  61. turtle.attackUp()
  62. sleep(1)
  63. counter = counter + 1
  64. if counter == 4 then
  65. flag_success = false
  66. break
  67. end
  68. end
  69. if flag_success == true then
  70. return true
  71. else
  72. return false
  73. end
  74. end
  75.  
  76. function mineDown()
  77. local counter = 0
  78. local flag_success = true
  79. while turtle.down() == false do
  80. turtle.digDown()
  81. turtle.attackDown()
  82. sleep(1)
  83. counter = counter + 1
  84. if counter == 4 then
  85. flag_success = false
  86. break
  87. end
  88. end
  89. if flag_success == true then
  90. return true
  91. else
  92. return false
  93. end
  94. end
  95.  
  96. function updateCoords(direction_moved)
  97. if direction_moved == 1 then
  98. current_z = current_z + 1
  99. end
  100. if direction_moved == 2 then
  101. current_x = current_x + 1
  102. end
  103. if direction_moved == 3 then
  104. current_z = current_z - 1
  105. end
  106. if direction_moved == 4 then
  107. current_x = current_x - 1
  108. end
  109. end
  110.  
  111. function turn(direction) --Note facing is clockwise so 1 is Forwards, 2 is right, 3 is backwards, 4 is left
  112. while current_direction ~= direction do
  113. if current_direction < direction then
  114. if current_direction == 1 and direction == 4 then
  115. turtle.turnLeft()
  116. current_direction = 4
  117. else
  118. turtle.turnRight()
  119. current_direction = current_direction + 1
  120. end
  121. else
  122. if current_direction == 4 and direction == 1 then
  123. turtle.turnRight()
  124. current_direction = 1
  125. else
  126. turtle.turnLeft()
  127. current_direction = current_direction - 1
  128. end
  129. end
  130. end
  131. end
  132.  
  133. function hasInventorySpace()
  134. local flag_space = false
  135. local i = 1
  136. repeat
  137. if turtle.getItemCount(i) == 0 then
  138. flag_space = true
  139. end
  140. i = i + 1
  141. until i == 17
  142. if flag_space == false then
  143. print("Inventory check failed")
  144. end
  145. return flag_space
  146. end
  147.  
  148. function hasFuel()
  149. local cost_to_return = current_x + current_y + current_z + 20
  150. if turtle.getFuelLevel() < cost_to_return then
  151. print("Fuel check failed")
  152. return false
  153. else
  154. return true
  155. end
  156. end
  157.  
  158. function unloadAndRefuel()
  159.  
  160. --return section
  161.  
  162. local y_offset_correct_fail = false --might cause an error if a local doesn't work in the main programme and a function
  163.  
  164. memory_x = current_x
  165. memory_y = current_y
  166. memory_z = current_z
  167. memory_direction = current_direction
  168. memory_y_offset = y_offset
  169.  
  170. current_y = current_y + y_offset
  171.  
  172. if current_y ~= 1 then
  173. if mineDown() == true then
  174. current_y = current_y - 1
  175. end
  176. end
  177. if current_y ~= 1 then
  178. if mineDown() == true then
  179. current_y = current_y - 1
  180. end
  181. end
  182. turn(3)
  183. while current_z ~= 1 do
  184. y_offset_correct_fail = false
  185. while y_offset ~= 0 and y_offset_correct_fail == false do
  186. if mineDown() == true then
  187. y_offset = y_offset - 1
  188. else
  189. y_offset_correct_fail = true
  190. end
  191. end
  192.  
  193. while mineForward() == false do
  194. if mineUp() == true then
  195. y_offset = y_offset + 1
  196. else
  197. waitForReset()
  198. end
  199. if y_offset == 10 then
  200. print("turtle is climbing without end, something is very very odd. Shutting down")
  201. exit()
  202. end
  203. end
  204. current_z = current_z - 1
  205. end
  206.  
  207. turn(4)
  208. while current_x ~= 1 do
  209. y_offset_correct_fail = false
  210. while y_offset ~= 0 and y_offset_correct_fail == false do
  211. if mineDown() == true then
  212. y_offset = y_offset - 1
  213. else
  214. y_offset_correct_fail = true
  215. end
  216. end
  217.  
  218. while mineForward() == false do
  219. if mineUp() == true then
  220. y_offset = y_offset + 1
  221. else
  222. waitForReset()
  223. end
  224. if y_offset == 10 then
  225. print("turtle is climbing without end, something is very very odd. Shutting down")
  226. exit()
  227. end
  228. end
  229. current_x = current_x - 1
  230. end
  231.  
  232. while current_y ~= 1 do
  233. if mineDown() == false then
  234. waitForReset()
  235. end
  236. current_y = current_y - 1
  237. end
  238.  
  239. --Unload Section
  240.  
  241. turn(3)
  242.  
  243. local i = 1
  244. repeat
  245. turtle.select(i)
  246. if turtle.drop() == false then
  247. turtle.dropDown()
  248. end
  249. i = i + 1
  250. until i == 17
  251.  
  252. if turtle.getFuelLevel() < 1000 then
  253. turn(2)
  254. if mineForward() == false then
  255. waitForReset()
  256. end
  257. if mineForward() == false then
  258. waitForReset()
  259. end
  260. if mineForward() == false then
  261. waitForReset()
  262. end
  263. turn(3)
  264. turtle.select(1)
  265. if turtle.suck() == false then
  266. waitForReset()
  267. end
  268. turtle.refuel()
  269. turn(4)
  270. if mineForward() == false then
  271. waitForReset()
  272. end
  273. if mineForward() == false then
  274. waitForReset()
  275. end
  276. if mineForward() == false then
  277. waitForReset()
  278. end
  279. turn(3)
  280. if turtle.drop() == false then
  281. waitForReset()
  282. end
  283. end
  284.  
  285. --return to memory section
  286.  
  287. local y_return_level
  288. if memory_y == 1 then
  289. y_return_level = memory_y
  290. end
  291. if memory_y == 2 then
  292. y_return_level = (memory_y - 1)
  293. end
  294. if memory_y > 2 then
  295. y_return_level = (memory_y - 2)
  296. end
  297.  
  298. while current_y ~= y_return_level do
  299. if mineUp() == false then
  300. waitForReset()
  301. end
  302. current_y = current_y + 1
  303. end
  304.  
  305. turn(2)
  306. while current_x ~= memory_x do
  307. y_offset_correct_fail = false
  308. while y_offset ~= 0 and y_offset_correct_fail == false do
  309. if mineDown() == true then
  310. y_offset = y_offset - 1
  311. else
  312. y_offset_correct_fail = true
  313. end
  314. end
  315.  
  316. while mineForward() == false do
  317. if mineUp() == true then
  318. y_offset = y_offset + 1
  319. else
  320. waitForReset()
  321. end
  322. if y_offset == 10 then
  323. print("turtle is climbing without end, something is very very odd. Shutting down")
  324. exit()
  325. end
  326. end
  327. current_x = current_x + 1
  328. end
  329.  
  330. turn(1)
  331. while current_z ~= memory_z do
  332. y_offset_correct_fail = false
  333. while y_offset ~= 0 and y_offset_correct_fail == false do
  334. if mineDown() == true then
  335. y_offset = y_offset - 1
  336. else
  337. y_offset_correct_fail = true
  338. end
  339. end
  340.  
  341. while mineForward() == false do
  342. if mineUp() == true then
  343. y_offset = y_offset + 1
  344. else
  345. waitForReset()
  346. end
  347. if y_offset == 10 then
  348. print("turtle is climbing without end, something is very very odd. Shutting down")
  349. exit()
  350. end
  351. end
  352. current_z = current_z + 1
  353. end
  354.  
  355. while current_y ~= memory_y do
  356. if mineUp() == false then
  357. waitForReset()
  358. end
  359. current_y = current_y + 1
  360. end
  361.  
  362. turn(memory_direction)
  363. end
  364.  
  365. --Main Program--
  366.  
  367. term.clear()
  368. term.setCursorPos(1, 1)
  369. print ("Please enter how WIDE you want me to dig.")
  370. max_x = tonumber(read())
  371.  
  372. term.clear()
  373. term.setCursorPos(1, 1)
  374. print ("Please enter how HIGH you want me to dig.")
  375. max_y = tonumber(read())
  376.  
  377. term.clear()
  378. term.setCursorPos(1, 1)
  379. print ("Please enter how FAR BACK you want me to dig.")
  380. max_z = tonumber(read())
  381.  
  382. local y_offset_correct_fail = false
  383. local x_direction_reverse = false
  384. local z_direction_reverse = false
  385. local startup = true
  386.  
  387. repeat
  388. if startup == false then
  389. if y_offset == 0 then
  390. if mineUp() == true then
  391. current_y = current_y + 1
  392. else
  393. waitForReset()
  394. end
  395. else
  396. y_offset = y_offset - 1
  397. current_y = current_y + 1
  398. end
  399. end
  400. startup = true
  401. if z_direction_reverse == false then
  402. repeat
  403. if startup == false then
  404. turn(1)
  405. local y_offset_correct_fail = false
  406. while y_offset ~= 0 and y_offset_correct_fail == false do
  407. if mineDown() == true then
  408. y_offset = y_offset - 1
  409. else
  410. y_offset_correct_fail = true
  411. end
  412. end
  413. while mineForward() == false do
  414. if mineUp() == true then
  415. y_offset = y_offset + 1
  416. else
  417. waitForReset()
  418. end
  419. if y_offset == 10 then
  420. print("turtle is climbing without end, something is very very odd. Shutting down")
  421. exit()
  422. end
  423. end
  424. current_z = current_z + 1
  425. end
  426. if x_direction_reverse == false then
  427. turn(2)
  428. repeat
  429. y_offset_correct_fail = false
  430. while y_offset ~= 0 and y_offset_correct_fail == false do
  431. if mineDown() == true then
  432. y_offset = y_offset - 1
  433. else
  434. y_offset_correct_fail = true
  435. end
  436. end
  437.  
  438. while mineForward() == false do
  439. if mineUp() == true then
  440. y_offset = y_offset + 1
  441. else
  442. waitForReset()
  443. end
  444. if y_offset == 10 then
  445. print("turtle is climbing without end, something is very very odd. Shutting down")
  446. exit()
  447. end
  448. end
  449. current_x = current_x + 1
  450. if hasInventorySpace() == false or hasFuel() == false then
  451. unloadAndRefuel()
  452. end
  453. until current_x == max_x
  454. x_direction_reverse = true
  455. startup = false
  456. else
  457. turn(4)
  458. repeat
  459. y_offset_correct_fail = false
  460. while y_offset ~= 0 and y_offset_correct_fail == false do
  461. if mineDown() == true then
  462. y_offset = y_offset - 1
  463. else
  464. y_offset_correct_fail = true
  465. end
  466. end
  467.  
  468. while mineForward() == false do
  469. if mineUp() == true then
  470. y_offset = y_offset + 1
  471. else
  472. waitForReset()
  473. end
  474. if y_offset == 10 then
  475. print("turtle is climbing without end, something is very very odd. Shutting down")
  476. exit()
  477. end
  478. end
  479. current_x = current_x - 1
  480. if hasInventorySpace() == false or hasFuel() == false then
  481. unloadAndRefuel()
  482. end
  483. until current_x == 1
  484. x_direction_reverse = false
  485. startup = false
  486. end
  487. until current_z == max_z
  488. z_direction_reverse = true
  489. else
  490. repeat
  491. if startup == false then
  492. turn(3)
  493. local y_offset_correct_fail = false
  494. while y_offset ~= 0 and y_offset_correct_fail == false do
  495. if mineDown() == true then
  496. y_offset = y_offset - 1
  497. else
  498. y_offset_correct_fail = true
  499. end
  500. end
  501. while mineForward() == false do
  502. if mineUp() == true then
  503. y_offset = y_offset + 1
  504. else
  505. waitForReset()
  506. end
  507. if y_offset == 10 then
  508. print("turtle is climbing without end, something is very very odd. Shutting down")
  509. exit()
  510. end
  511. end
  512. current_z = current_z - 1
  513. end
  514. if x_direction_reverse == true then
  515. turn(4)
  516. repeat
  517. y_offset_correct_fail = false
  518. while y_offset ~= 0 and y_offset_correct_fail == false do
  519. if mineDown() == true then
  520. y_offset = y_offset - 1
  521. else
  522. y_offset_correct_fail = true
  523. end
  524. end
  525.  
  526. while mineForward() == false do
  527. if mineUp() == true then
  528. y_offset = y_offset + 1
  529. else
  530. waitForReset()
  531. end
  532. if y_offset == 10 then
  533. print("turtle is climbing without end, something is very very odd. Shutting down")
  534. exit()
  535. end
  536. end
  537. current_x = current_x - 1
  538. if hasInventorySpace() == false or hasFuel() == false then
  539. unloadAndRefuel()
  540. end
  541. until current_x == 1
  542. x_direction_reverse = false
  543. startup = false
  544. else
  545. turn(2)
  546. repeat
  547. y_offset_correct_fail = false
  548. while y_offset ~= 0 and y_offset_correct_fail == false do
  549. if mineDown() == true then
  550. y_offset = y_offset - 1
  551. else
  552. y_offset_correct_fail = true
  553. end
  554. end
  555.  
  556. while mineForward() == false do
  557. if mineUp() == true then
  558. y_offset = y_offset + 1
  559. else
  560. waitForReset()
  561. end
  562. if y_offset == 10 then
  563. print("turtle is climbing without end, something is very very odd. Shutting down")
  564. exit()
  565. end
  566. end
  567. current_x = current_x + 1
  568. if hasInventorySpace() == false or hasFuel() == false then
  569. unloadAndRefuel()
  570. end
  571. until current_x == max_x
  572. x_direction_reverse = true
  573. startup = false
  574. end
  575. until current_z == 1
  576. z_direction_reverse = false
  577. end
  578. until current_y == max_y
Advertisement
Add Comment
Please, Sign In to add comment