Advertisement
lemmy101

Untitled

Apr 24th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.20 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. 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(not 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. function doDump()
  343. for i = ignoreStart, 16 do
  344. dump(i)
  345. end
  346. end
  347.  
  348. function dump(index)
  349.  
  350. local count = turtle.getItemCount(index)
  351.  
  352. turtle.select(index)
  353. if count > 1 then
  354. turtle.dropUp(count - 1)
  355. end
  356. end
  357.  
  358. local function readLines(sPath)
  359. local file = fs.open(sPath, "r") -- open the file
  360. if file then -- check if it's open
  361. local tLines = {} -- table to store the lines
  362. local sLine = file.readLine() -- read a line from the file
  363. while sLine do -- while there's a line in the file
  364. table.insert(tLines, sLine) -- add it to the table
  365. sLine = file.readLine() -- get the next line
  366. end
  367. file.close() -- close the file
  368. return tLines -- return the table with the lines
  369. end
  370. return nil -- there was an error opening the file, return nil to let the user know
  371. end
  372.  
  373. local function writeLines(sPath, tLines)
  374. local file = fs.open(sPath, "w") -- open the file
  375. if file then -- check if the file is open
  376. for _, sLine in ipairs(tLines) do -- for each line in the table
  377. file.writeLine(sLine) -- write the line
  378. end
  379. file.close() -- close the file
  380. end
  381. end
  382.  
  383. function filereadline(filename, line)
  384. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  385. if not tLines then -- if there was an error
  386. return nil -- return nil/error
  387. end
  388. return tLines[line] -- return the line
  389. end
  390.  
  391. function filewriteline(filename, line, text)
  392. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  393. if tLines then -- if there was no error
  394. tLines[line] = text -- set the line
  395. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  396. end
  397. end
  398.  
  399. function goto(tx, ty, tz)
  400.  
  401. local dx = tx - x
  402. local dy = ty - y
  403. local dz = tz - z
  404.  
  405. -- if dz ~= 0 then
  406. while z ~= tz do
  407. print(z .. " - " .. tz)
  408. if z > tz then
  409. goDown()
  410. else
  411. goUp()
  412. end
  413. if(not testFuel()) then
  414. returnToRefuel()
  415. end
  416. end
  417. -- end
  418. -- if dx ~= 0 then
  419. while x ~= tx do
  420. print(x .. " - " .. tx)
  421. if x > tx then
  422. goWest()
  423. else
  424. goEast()
  425. end
  426. if(not testFuel()) then
  427. returnToRefuel()
  428. end
  429. end
  430. -- end
  431. -- if dy ~= 0 then
  432. while y ~= ty do
  433. print(y .. " - " .. ty)
  434. if y < ty then
  435. goSouth()
  436. else
  437. goNorth()
  438. end
  439. if(not testFuel()) then
  440. returnToRefuel()
  441. end
  442. end
  443. -- end
  444.  
  445. end
  446.  
  447. function gotoreverse(tx, ty, tz)
  448.  
  449. local dx = tx - x
  450. local dy = ty - y
  451. local dz = tz - z
  452. -- if dx ~= 0 then
  453. while x ~= tx do
  454. print(x .. " - " .. tx)
  455. if x > tx then
  456. goWest()
  457. else
  458. goEast()
  459. end
  460. if(not testFuel()) then
  461. returnToRefuel()
  462. end
  463. doDump()
  464. end
  465. -- end
  466. -- if dy ~= 0 then
  467. while y ~= ty do
  468. print(y .. " - " .. ty)
  469. if y < ty then
  470. goSouth()
  471. else
  472. goNorth()
  473. end
  474. if(not testFuel()) then
  475. returnToRefuel()
  476. end
  477. doDump()
  478. end
  479. -- end
  480.  
  481. -- if dz ~= 0 then
  482. while z ~= tz do
  483. print(z .. " - " .. tz)
  484. if z > tz then
  485. goDown()
  486. else
  487. goUp()
  488. end
  489. if(not testFuel()) then
  490. returnToRefuel()
  491. end
  492. doDump()
  493. end
  494. -- end
  495.  
  496. end
  497.  
  498. function testFuel()
  499. local dist = math.abs(startx - x) + math.abs(starty - y) + math.abs(startz - z);
  500.  
  501. local fuel = estimateFuel()
  502. print("Estimated distance: "..dist)
  503. if(fuel < dist + 10) then
  504. return false
  505. end
  506.  
  507. return true
  508. end
  509.  
  510. function estimateFuel()
  511. local a = turtle.getItemCount(1)
  512. local b = turtle.getItemCount(2)
  513. local c = turtle.getItemCount(3)
  514.  
  515. local steps = (a + b + c) * 96
  516. print("Estimated fuel steps: "..(steps + turtle.getFuelLevel()))
  517. return steps + turtle.getFuelLevel();
  518.  
  519. end
  520.  
  521. function returnToRefuel()
  522. local ox = x
  523. local oy = y
  524. local oz = z
  525. goto(startx, starty, startz)
  526. -- refuelStation();
  527. goto(ox, oy, oz)
  528. end
  529.  
  530. function refuelStation()
  531. faceSouth()
  532. turtle.select(1)
  533. turtle.suck()
  534. turtle.refuel()
  535. turtle.suck()
  536. turtle.refuel()
  537. turtle.suck()
  538. turtle.refuel()
  539. turtle.suck()
  540. turtle.select(yieldStart)
  541. faceNorth()
  542. end
  543.  
  544.  
  545. for i = 0, 100 do
  546. gotoreverse (0, 0, startz)
  547. refuelStation()
  548. local c = 0;
  549. for y = 0, -64, -1 do
  550. goto (startx+(i*5), y, 5)
  551. dig.up()
  552. c = c + 1
  553. if c > 2 then
  554. fx = x
  555. fy = y
  556. fz = z
  557. for b = 1, 4 do
  558. goto (x+1, y, z)
  559. doLeftRight()
  560. goto (x, y, z+1)
  561. doLeftRight()
  562. goto (x, y, z-1)
  563. end
  564. goto(fx, fy, fz)
  565. c = 0
  566. end
  567. end
  568. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement