Advertisement
lemmy101

Untitled

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