Advertisement
lemmy101

Untitled

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