Advertisement
lemmy101

Untitled

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