Advertisement
lemmy101

Untitled

Apr 24th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.14 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-2 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. turtle.select(index)
  445. local count = turtle.getItemCount(index)
  446.  
  447. if count > 1 then
  448. dig.down()
  449. print("dropping "..(count - 1))
  450. turtle.dropDown(count - 1)
  451. end
  452. end
  453. function dumpAll(index)
  454.  
  455. local count = turtle.getItemCount(index)
  456.  
  457. turtle.select(index)
  458. if count > 0 then
  459. dig.down()
  460. turtle.dropDown(count)
  461. end
  462. end
  463.  
  464. local function readLines(sPath)
  465. local file = fs.open(sPath, "r") -- open the file
  466. if file then -- check if it's open
  467. local tLines = {} -- table to store the lines
  468. local sLine = file.readLine() -- read a line from the file
  469. while sLine do -- while there's a line in the file
  470. table.insert(tLines, sLine) -- add it to the table
  471. sLine = file.readLine() -- get the next line
  472. end
  473. file.close() -- close the file
  474. return tLines -- return the table with the lines
  475. end
  476. return nil -- there was an error opening the file, return nil to let the user know
  477. end
  478.  
  479. local function writeLines(sPath, tLines)
  480. local file = fs.open(sPath, "w") -- open the file
  481. if file then -- check if the file is open
  482. for _, sLine in ipairs(tLines) do -- for each line in the table
  483. file.writeLine(sLine) -- write the line
  484. end
  485. file.close() -- close the file
  486. end
  487. end
  488.  
  489. function filereadline(filename, line)
  490. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  491. if not tLines then -- if there was an error
  492. return nil -- return nil/error
  493. end
  494. return tLines[line] -- return the line
  495. end
  496.  
  497. function filewriteline(filename, line, text)
  498. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  499. if tLines then -- if there was no error
  500. tLines[line] = text -- set the line
  501. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  502. end
  503. end
  504.  
  505. function goto(tx, ty, tz)
  506.  
  507. local dx = tx - x
  508. local dy = ty - y
  509. local dz = tz - z
  510.  
  511. -- if dz ~= 0 then
  512. while z ~= tz do
  513. print(z .. " - " .. tz)
  514. if z > tz then
  515. goDown()
  516. else
  517. goUp()
  518. end
  519. if(not testFuel()) then
  520. returnToRefuel()
  521. end
  522. doDump()
  523. --doUpDownCheck()
  524. end
  525. -- end
  526. -- if dx ~= 0 then
  527. while x ~= tx do
  528. print(x .. " - " .. tx)
  529. if x > tx then
  530. goWest()
  531. else
  532. goEast()
  533. end
  534. if(not testFuel()) then
  535. returnToRefuel()
  536. end
  537. doDump()
  538. --doUpDownCheck()
  539. end
  540. -- end
  541. -- if dy ~= 0 then
  542. while y ~= ty do
  543. print(y .. " - " .. ty)
  544. if y < ty then
  545. goSouth()
  546. else
  547. goNorth()
  548. end
  549. if(not testFuel()) then
  550. returnToRefuel()
  551. end
  552. doDump()
  553. --doUpDownCheck()
  554. end
  555. -- end
  556.  
  557. end
  558.  
  559. function gotoreverse(tx, ty, tz)
  560.  
  561. local dx = tx - x
  562. local dy = ty - y
  563. local dz = tz - z
  564. -- if dx ~= 0 then
  565. while x ~= tx do
  566. print(x .. " - " .. tx)
  567. if x > tx then
  568. goWest()
  569. else
  570. goEast()
  571. end
  572. if(not testFuel()) then
  573. returnToRefuel()
  574. end
  575. -- doUpDownCheck()
  576. doDump()
  577. end
  578. -- end
  579. -- if dy ~= 0 then
  580. while y ~= ty do
  581. print(y .. " - " .. ty)
  582. if y < ty then
  583. goSouth()
  584. else
  585. goNorth()
  586. end
  587. if(not testFuel()) then
  588. returnToRefuel()
  589. end
  590. -- doUpDownCheck()
  591. doDump()
  592. end
  593. -- end
  594.  
  595. -- if dz ~= 0 then
  596. while z ~= tz do
  597. print(z .. " - " .. tz)
  598. if z > tz then
  599. goDown()
  600. else
  601. goUp()
  602. end
  603. if(not testFuel()) then
  604. returnToRefuel()
  605. end
  606. --doUpDownCheck()
  607. doDump()
  608. end
  609. -- end
  610.  
  611. end
  612.  
  613. function testFuel()
  614. local dist = math.abs(startx - x) + math.abs(starty - y) + math.abs(startz - z);
  615.  
  616. local fuel = estimateFuel()
  617. print("Estimated distance: "..dist)
  618. if(fuel < dist + 10) then
  619. return false
  620. end
  621.  
  622. return true
  623. end
  624.  
  625. function estimateFuel()
  626. return turtle.getFuelLevel();
  627.  
  628. end
  629.  
  630. function returnToRefuel()
  631. local ox = x
  632. local oy = y
  633. local oz = z
  634. goto(startx, starty, startz)
  635. refuelStation();
  636. turtle.refuel()
  637. goto(ox, oy, oz)
  638. end
  639.  
  640. function dropLoot()
  641. faceWest()
  642. for i = 1, ignoreStart-1 do
  643. turtle.select(i)
  644. turtle.drop()
  645. end
  646. print("dropping loot")
  647. faceSouth()
  648. end
  649.  
  650. function refuelStation()
  651. faceSouth()
  652. turtle.select(1)
  653. turtle.suck()
  654. turtle.refuel()
  655. turtle.select(yieldStart)
  656. faceNorth()
  657. end
  658.  
  659.  
  660. for i = 0, 100 do
  661. gotoreverse (0, 0, startz)
  662. dropLoot()
  663. refuelStation()
  664. local c = 0;
  665. local nn = 3 * 8
  666. for y = 0, -nn, -1 do
  667. goto (startx+(i*5), y, 5)
  668. if(i == 0) then
  669. doLeftUpDownCheck()
  670. goto (x, y, z+1)
  671. doLeftUpDownCheck()
  672. goto (x, y, z-1)
  673.  
  674. end
  675. dig.up()
  676. c = c + 1
  677. if c > 2 then
  678. fx = x
  679. fy = y
  680. fz = z
  681. for b = 1, 4 do
  682. goto (x+1, y, z)
  683. doLeftRight()
  684. goto (x, y, z+1)
  685. doLeftRight()
  686. goto (x, y, z-1)
  687. end
  688. goto(fx, fy, fz)
  689. c = 0
  690. end
  691. end
  692. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement