Advertisement
lemmy101

Untitled

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