Advertisement
lemmy101

Untitled

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