Advertisement
lemmy101

Untitled

Apr 24th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.08 KB | None | 0 0
  1. north = 1
  2. south = 2
  3. west = 3
  4. east = 4
  5.  
  6. ignoreStart = 14
  7.  
  8. northDir = {0, 1}
  9. southDir = {0, -1}
  10. eastDir = {1, 0}
  11. westDir = {-1, 0}
  12. startz = 68
  13. startx = 0
  14. starty = 0
  15. x = 0
  16. y = 0
  17. z = startz
  18.  
  19. fuelStack = 1
  20. yieldStart = 1
  21.  
  22. -- start at facing north
  23. currentDir = north
  24.  
  25. local mov = {}
  26. local rot = {}
  27. local dig = {}
  28.  
  29. mov.forward = function(times)
  30. local boolean v = false
  31. for i=1,times do
  32. if turtle.detect() == false then
  33. while not turtle.forward() do
  34. sleep(1)
  35. end
  36. v = true
  37. else
  38. v = false
  39. return v
  40. end
  41. end
  42. return v
  43. end
  44.  
  45. mov.back = function(times)
  46. local boolean v = false
  47. for i=1,times do
  48. while not turtle.back() do
  49. sleep(1)
  50. end
  51. v = true
  52. end
  53. return v
  54. end
  55.  
  56. mov.up = function(times)
  57. local boolean v = false
  58. for i=1,times do
  59. if turtle.detectUp() == false then
  60. while not turtle.up() do
  61. sleep(1)
  62. end
  63. v = true
  64. else
  65. v = false
  66. return v
  67. end
  68. end
  69. return v
  70. end
  71.  
  72. mov.down = function(times)
  73. local boolean v = false
  74. print("mov.down");
  75. for i=1,times do
  76. if turtle.detectDown() == false then
  77. print("!detectDown");
  78. while not turtle.down() do
  79. sleep(1)
  80. end
  81. v = true
  82. else
  83. print("detectDown");
  84. v = false
  85. return v
  86. end
  87. end
  88. return v
  89. end
  90.  
  91. mov.place = function()
  92. while not turtle.place() do
  93. sleep(1)
  94. end
  95. return true
  96. end
  97.  
  98. rot.right = function()
  99. while not turtle.turnRight() do
  100. sleep(1)
  101. end
  102.  
  103. if RotatePosition == "FORWARD" then
  104. RotatePosition = "RIGHT"
  105. elseif RotatePosition == "LEFT" then
  106. RotatePosition = "FORWARD"
  107. end
  108. filewriteline("BranchMineData", 5, RotatePosition)
  109. return true
  110. end
  111.  
  112. rot.left = function()
  113. while not turtle.turnLeft() do
  114. sleep(1)
  115. end
  116.  
  117. if RotatePosition == "FORWARD" then
  118. RotatePosition = "LEFT"
  119. elseif RotatePosition == "RIGHT" then
  120. RotatePosition = "FORWARD"
  121. end
  122. filewriteline("BranchMineData", 5, RotatePosition)
  123. return true
  124. end
  125.  
  126. dig.forward = function()
  127. if turtle.detect() then
  128. local doit = true
  129. turtle.select(ignoreStart)
  130. for i = ignoreStart, 16 do
  131. turtle.select(i)
  132. if(turtle.compare()) then
  133. doit = false
  134. end
  135. end
  136. if not doit then
  137. for i = 1, ignoreStart-1 do
  138. turtle.select(i)
  139. if(turtle.compare()) then
  140. dumpAll(i)
  141. end
  142. end
  143.  
  144. end
  145.  
  146. while not turtle.dig() do
  147. sleep(1)
  148. end
  149.  
  150.  
  151.  
  152.  
  153. return true
  154. else
  155. print("No Block to mine forward")
  156. return false
  157. end
  158. end
  159.  
  160. dig.up = function()
  161. if turtle.detectUp() then
  162. turtle.select(ignoreStart)
  163. while not turtle.digUp() do
  164. sleep(1)
  165. end
  166. return true
  167. else
  168. print("No Block to mine up")
  169. return false
  170. end
  171. end
  172.  
  173. dig.down = function()
  174. if turtle.detectDown() then
  175. turtle.select(ignoreStart)
  176. while not turtle.digDown() do
  177. sleep(1)
  178. end
  179. return true
  180. else
  181. print("No Block to mine down")
  182. return false
  183. end
  184. end
  185.  
  186. function DigMoveForward()
  187. if mov.forward(1) == false then
  188. dig.forward()
  189. sleep(0.5)
  190. DigMoveForward()
  191. end
  192. end
  193.  
  194. function DigMoveUp()
  195. if mov.up(1) == false then
  196. dig.up()
  197. sleep(0.5)
  198. DigMoveUp()
  199.  
  200. else
  201. z = z + 1
  202. end
  203.  
  204. end
  205.  
  206. function DigMoveDown()
  207. print("moving down")
  208. if mov.down(1) == false then
  209. print("failed moving down - digging")
  210. dig.down()
  211. sleep(0.5)
  212. print("trying again")
  213. DigMoveDown()
  214. else
  215. print("aucceeeded")
  216. z = z - 1
  217. end
  218. end
  219.  
  220.  
  221. function faceEast()
  222. if(currentDir == north) then
  223. turtle.turnRight()
  224. elseif(currentDir == west) then
  225. turtle.turnRight()
  226. turtle.turnRight()
  227. elseif(currentDir == south) then
  228. turtle.turnLeft()
  229. end
  230. currentDir = east;
  231. end
  232.  
  233. function faceNorth()
  234. if(currentDir == east) then
  235. turtle.turnLeft()
  236. elseif(currentDir == south) then
  237. turtle.turnRight()
  238. turtle.turnRight()
  239. elseif(currentDir == west) then
  240. turtle.turnRight()
  241. end
  242. currentDir = north;
  243. end
  244.  
  245. function faceSouth()
  246. if(currentDir == east) then
  247. turtle.turnRight()
  248. elseif(currentDir == north) then
  249. turtle.turnRight()
  250. turtle.turnRight()
  251. elseif(currentDir == west) then
  252. turtle.turnLeft()
  253. end
  254. currentDir = south;
  255. end
  256.  
  257. function faceWest()
  258. if(currentDir == south) then
  259. turtle.turnRight()
  260. elseif(currentDir == east) then
  261. turtle.turnRight()
  262. turtle.turnRight()
  263. elseif(currentDir == north) then
  264. turtle.turnLeft()
  265. end
  266. currentDir = west;
  267. end
  268.  
  269. function forward()
  270. DigMoveForward()
  271.  
  272. if(currentDir == east) then
  273. x = x + 1
  274. elseif(currentDir == north) then
  275. y = y - 1
  276. elseif(currentDir == south) then
  277. y = y + 1
  278. elseif(currentDir == west) then
  279. x = x - 1
  280. end
  281. end
  282.  
  283. function goWest()
  284. faceWest()
  285. forward()
  286. end
  287.  
  288. function goEast()
  289. faceEast()
  290. forward()
  291. end
  292.  
  293. function goNorth()
  294. faceNorth()
  295. forward()
  296. end
  297.  
  298. function goSouth()
  299. faceSouth()
  300. forward()
  301. end
  302.  
  303. function goUp()
  304. DigMoveUp()
  305.  
  306. end
  307.  
  308. function goDown()
  309. DigMoveDown()
  310.  
  311. end
  312.  
  313.  
  314.  
  315. function doLeftRight()
  316. turtle.turnLeft()
  317. local doit = true
  318. for i = ignoreStart, 16 do
  319. turtle.select(i)
  320. if(turtle.detect() and turtle.compare()) then
  321. doit = false
  322. end
  323. end
  324. if doit then
  325. dig.forward()
  326. end
  327. turtle.turnRight()
  328. turtle.turnRight()
  329. doit = true
  330. for i = ignoreStart, 16 do
  331. turtle.select(i)
  332. if(turtle.detect() and turtle.compare()) then
  333. doit = false
  334. end
  335.  
  336. end
  337. if doit then
  338. dig.forward()
  339. end
  340. turtle.turnLeft()
  341. doit = true
  342. for i = ignoreStart, 16 do
  343. turtle.select(i)
  344. if(turtle.detectUp() and turtle.compareUp()) then
  345. doit = false
  346. end
  347.  
  348. end
  349. if doit then
  350. dig.up()
  351. end
  352.  
  353. doit = true
  354. for i = ignoreStart, 16 do
  355. turtle.select(i)
  356. if(turtle.detectDown() and turtle.compareDown()) then
  357. doit = false
  358. end
  359. end
  360. if doit then
  361. dig.down()
  362. end
  363.  
  364. end
  365.  
  366.  
  367. function doLeftUpDownCheck()
  368. turtle.turnLeft()
  369. local doit = true
  370. for i = ignoreStart, 16 do
  371. turtle.select(i)
  372. if(turtle.detect() and turtle.compare()) then
  373. doit = false
  374. end
  375. end
  376. if doit then
  377. dig.forward()
  378. end
  379. doit = true
  380. for i = ignoreStart, 16 do
  381. turtle.select(i)
  382. if(turtle.detectUp() and turtle.compareUp()) then
  383. doit = false
  384. end
  385.  
  386. end
  387. if doit then
  388. dig.up()
  389. end
  390.  
  391. doit = true
  392. for i = ignoreStart, 16 do
  393. turtle.select(i)
  394. if(turtle.detectDown() and turtle.compareDown()) then
  395. doit = false
  396. end
  397. end
  398. if doit then
  399. dig.down()
  400. end
  401. turtle.turnRight()
  402.  
  403. end
  404.  
  405.  
  406. function doUpDownCheck()
  407.  
  408. local doit = true
  409.  
  410. doit = true
  411. for i = ignoreStart, 16 do
  412. turtle.select(i)
  413. if(turtle.detectUp() and turtle.compareUp()) then
  414. doit = false
  415. end
  416.  
  417. end
  418. if doit then
  419. dig.up()
  420. end
  421.  
  422. doit = true
  423. for i = ignoreStart, 16 do
  424. turtle.select(i)
  425. if(turtle.detectDown() and turtle.compareDown()) then
  426. doit = false
  427. end
  428. end
  429. if doit then
  430. dig.down()
  431. end
  432.  
  433.  
  434. end
  435.  
  436. function doDump()
  437. for i = ignoreStart, 16 do
  438. dump(i)
  439. end
  440. end
  441.  
  442. function dump(index)
  443.  
  444. local count = turtle.getItemCount(index)
  445.  
  446. turtle.select(index)
  447. if count > 1 then
  448. dig.down()
  449. turtle.dropDown(count - 1)
  450. end
  451. end
  452. function dumpAll(index)
  453.  
  454. local count = turtle.getItemCount(index)
  455.  
  456. turtle.select(index)
  457. if count > 0 then
  458. dig.down()
  459. turtle.dropDown(count)
  460. end
  461. end
  462.  
  463. local function readLines(sPath)
  464. local file = fs.open(sPath, "r") -- open the file
  465. if file then -- check if it's open
  466. local tLines = {} -- table to store the lines
  467. local sLine = file.readLine() -- read a line from the file
  468. while sLine do -- while there's a line in the file
  469. table.insert(tLines, sLine) -- add it to the table
  470. sLine = file.readLine() -- get the next line
  471. end
  472. file.close() -- close the file
  473. return tLines -- return the table with the lines
  474. end
  475. return nil -- there was an error opening the file, return nil to let the user know
  476. end
  477.  
  478. local function writeLines(sPath, tLines)
  479. local file = fs.open(sPath, "w") -- open the file
  480. if file then -- check if the file is open
  481. for _, sLine in ipairs(tLines) do -- for each line in the table
  482. file.writeLine(sLine) -- write the line
  483. end
  484. file.close() -- close the file
  485. end
  486. end
  487.  
  488. function filereadline(filename, line)
  489. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  490. if not tLines then -- if there was an error
  491. return nil -- return nil/error
  492. end
  493. return tLines[line] -- return the line
  494. end
  495.  
  496. function filewriteline(filename, line, text)
  497. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  498. if tLines then -- if there was no error
  499. tLines[line] = text -- set the line
  500. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  501. end
  502. end
  503.  
  504. function goto(tx, ty, tz)
  505.  
  506. local dx = tx - x
  507. local dy = ty - y
  508. local dz = tz - z
  509.  
  510. -- if dz ~= 0 then
  511. while z ~= tz do
  512. print(z .. " - " .. tz)
  513. if z > tz then
  514. goDown()
  515. else
  516. goUp()
  517. end
  518. if(not testFuel()) then
  519. returnToRefuel()
  520. end
  521. doDump()
  522. --doUpDownCheck()
  523. end
  524. -- end
  525. -- if dx ~= 0 then
  526. while x ~= tx do
  527. print(x .. " - " .. tx)
  528. if x > tx then
  529. goWest()
  530. else
  531. goEast()
  532. end
  533. if(not testFuel()) then
  534. returnToRefuel()
  535. end
  536. doDump()
  537. --doUpDownCheck()
  538. end
  539. -- end
  540. -- if dy ~= 0 then
  541. while y ~= ty do
  542. print(y .. " - " .. ty)
  543. if y < ty then
  544. goSouth()
  545. else
  546. goNorth()
  547. end
  548. if(not testFuel()) then
  549. returnToRefuel()
  550. end
  551. doDump()
  552. --doUpDownCheck()
  553. end
  554. -- end
  555.  
  556. end
  557.  
  558. function gotoreverse(tx, ty, tz)
  559.  
  560. local dx = tx - x
  561. local dy = ty - y
  562. local dz = tz - z
  563. -- if dx ~= 0 then
  564. while x ~= tx do
  565. print(x .. " - " .. tx)
  566. if x > tx then
  567. goWest()
  568. else
  569. goEast()
  570. end
  571. if(not testFuel()) then
  572. returnToRefuel()
  573. end
  574. -- doUpDownCheck()
  575. doDump()
  576. end
  577. -- end
  578. -- if dy ~= 0 then
  579. while y ~= ty do
  580. print(y .. " - " .. ty)
  581. if y < ty then
  582. goSouth()
  583. else
  584. goNorth()
  585. end
  586. if(not testFuel()) then
  587. returnToRefuel()
  588. end
  589. -- doUpDownCheck()
  590. doDump()
  591. end
  592. -- end
  593.  
  594. -- if dz ~= 0 then
  595. while z ~= tz do
  596. print(z .. " - " .. tz)
  597. if z > tz then
  598. goDown()
  599. else
  600. goUp()
  601. end
  602. if(not testFuel()) then
  603. returnToRefuel()
  604. end
  605. --doUpDownCheck()
  606. doDump()
  607. end
  608. -- end
  609.  
  610. end
  611.  
  612. function testFuel()
  613. local dist = math.abs(startx - x) + math.abs(starty - y) + math.abs(startz - z);
  614.  
  615. local fuel = estimateFuel()
  616. print("Estimated distance: "..dist)
  617. if(fuel < dist + 10) then
  618. return false
  619. end
  620.  
  621. return true
  622. end
  623.  
  624. function estimateFuel()
  625. return turtle.getFuelLevel();
  626.  
  627. end
  628.  
  629. function returnToRefuel()
  630. local ox = x
  631. local oy = y
  632. local oz = z
  633. goto(startx, starty, startz)
  634. refuelStation();
  635. turtle.refuel()
  636. goto(ox, oy, oz)
  637. end
  638.  
  639. function dropLoot()
  640. faceWest()
  641. for i = 1, ignoreStart-1 do
  642. turtle.select(i)
  643. turtle.drop()
  644. end
  645. faceSouth()
  646. end
  647.  
  648. function refuelStation()
  649. faceSouth()
  650. turtle.select(1)
  651. turtle.suck()
  652. turtle.refuel()
  653. turtle.select(yieldStart)
  654. faceNorth()
  655. end
  656.  
  657.  
  658. for i = 0, 100 do
  659. gotoreverse (0, 0, startz)
  660. dropLoot()
  661. refuelStation()
  662. local c = 0;
  663. local nn = 3 * 2
  664. for y = 0, -nn, -1 do
  665. goto (startx+(i*5), y, 5)
  666. if(i == 0) then
  667. doLeftUpDownCheck()
  668. goto (x, y, z+1)
  669. doLeftUpDownCheck()
  670. goto (x, y, z-1)
  671.  
  672. end
  673. dig.up()
  674. c = c + 1
  675. if c > 2 then
  676. fx = x
  677. fy = y
  678. fz = z
  679. for b = 1, 4 do
  680. goto (x+1, y, z)
  681. doLeftRight()
  682. goto (x, y, z+1)
  683. doLeftRight()
  684. goto (x, y, z-1)
  685. end
  686. goto(fx, fy, fz)
  687. c = 0
  688. end
  689. end
  690. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement