Advertisement
lemmy101

Untitled

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