Advertisement
lemmy101

Untitled

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