Advertisement
lemmy101

Untitled

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