Advertisement
lemmy101

Untitled

Apr 25th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.88 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.  
  130. while not turtle.dig() do
  131. sleep(1)
  132. end
  133.  
  134.  
  135.  
  136.  
  137. return true
  138. else
  139. print("No Block to mine forward")
  140. return false
  141. end
  142. end
  143.  
  144. dig.up = function()
  145. if turtle.detectUp() then
  146. while not turtle.digUp() do
  147. sleep(1)
  148. end
  149. return true
  150. else
  151. print("No Block to mine up")
  152. return false
  153. end
  154. end
  155.  
  156. dig.down = function()
  157. if turtle.detectDown() then
  158. while not turtle.digDown() do
  159. sleep(1)
  160. end
  161. return true
  162. else
  163. print("No Block to mine down")
  164. return false
  165. end
  166. end
  167.  
  168. function DigMoveForward()
  169. if mov.forward(1) == false then
  170. dig.forward()
  171. sleep(0.5)
  172. DigMoveForward()
  173. end
  174. end
  175.  
  176. function DigMoveUp()
  177. if mov.up(1) == false then
  178. dig.up()
  179. sleep(0.5)
  180. DigMoveUp()
  181.  
  182. else
  183. z = z + 1
  184. end
  185.  
  186. end
  187.  
  188. function DigMoveDown()
  189. print("moving down")
  190. if mov.down(1) == false then
  191. print("failed moving down - digging")
  192. dig.down()
  193. sleep(0.5)
  194. print("trying again")
  195. DigMoveDown()
  196. else
  197. print("aucceeeded")
  198. z = z - 1
  199. end
  200. end
  201.  
  202.  
  203. function faceEast()
  204. if(currentDir == north) then
  205. turtle.turnRight()
  206. elseif(currentDir == west) then
  207. turtle.turnRight()
  208. turtle.turnRight()
  209. elseif(currentDir == south) then
  210. turtle.turnLeft()
  211. end
  212. currentDir = east;
  213. end
  214.  
  215. function faceNorth()
  216. if(currentDir == east) then
  217. turtle.turnLeft()
  218. elseif(currentDir == south) then
  219. turtle.turnRight()
  220. turtle.turnRight()
  221. elseif(currentDir == west) then
  222. turtle.turnRight()
  223. end
  224. currentDir = north;
  225. end
  226.  
  227. function faceSouth()
  228. if(currentDir == east) then
  229. turtle.turnRight()
  230. elseif(currentDir == north) then
  231. turtle.turnRight()
  232. turtle.turnRight()
  233. elseif(currentDir == west) then
  234. turtle.turnLeft()
  235. end
  236. currentDir = south;
  237. end
  238.  
  239. function faceWest()
  240. if(currentDir == south) then
  241. turtle.turnRight()
  242. elseif(currentDir == east) then
  243. turtle.turnRight()
  244. turtle.turnRight()
  245. elseif(currentDir == north) then
  246. turtle.turnLeft()
  247. end
  248. currentDir = west;
  249. end
  250.  
  251. function forward()
  252. DigMoveForward()
  253.  
  254. if(currentDir == east) then
  255. x = x + 1
  256. elseif(currentDir == north) then
  257. y = y - 1
  258. elseif(currentDir == south) then
  259. y = y + 1
  260. elseif(currentDir == west) then
  261. x = x - 1
  262. end
  263. end
  264.  
  265. function goWest()
  266. faceWest()
  267. forward()
  268. end
  269.  
  270. function goEast()
  271. faceEast()
  272. forward()
  273. end
  274.  
  275. function goNorth()
  276. faceNorth()
  277. forward()
  278. end
  279.  
  280. function goSouth()
  281. faceSouth()
  282. forward()
  283. end
  284.  
  285. function goUp()
  286. DigMoveUp()
  287.  
  288. end
  289.  
  290. function goDown()
  291. DigMoveDown()
  292.  
  293. end
  294.  
  295.  
  296.  
  297. local function readLines(sPath)
  298. local file = fs.open(sPath, "r") -- open the file
  299. if file then -- check if it's open
  300. local tLines = {} -- table to store the lines
  301. local sLine = file.readLine() -- read a line from the file
  302. while sLine do -- while there's a line in the file
  303. table.insert(tLines, sLine) -- add it to the table
  304. sLine = file.readLine() -- get the next line
  305. end
  306. file.close() -- close the file
  307. return tLines -- return the table with the lines
  308. end
  309. return nil -- there was an error opening the file, return nil to let the user know
  310. end
  311.  
  312. local function writeLines(sPath, tLines)
  313. local file = fs.open(sPath, "w") -- open the file
  314. if file then -- check if the file is open
  315. for _, sLine in ipairs(tLines) do -- for each line in the table
  316. file.writeLine(sLine) -- write the line
  317. end
  318. file.close() -- close the file
  319. end
  320. end
  321.  
  322. function filereadline(filename, line)
  323. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  324. if not tLines then -- if there was an error
  325. return nil -- return nil/error
  326. end
  327. return tLines[line] -- return the line
  328. end
  329.  
  330. function filewriteline(filename, line, text)
  331. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  332. if tLines then -- if there was no error
  333. tLines[line] = text -- set the line
  334. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  335. end
  336. end
  337.  
  338. function goto(tx, ty, tz)
  339.  
  340. local dx = tx - x
  341. local dy = ty - y
  342. local dz = tz - z
  343.  
  344. -- if dz ~= 0 then
  345. while z ~= tz do
  346. print(z .. " - " .. tz)
  347. if z > tz then
  348. goDown()
  349. else
  350. goUp()
  351. end
  352. if(not testFuel()) then
  353. returnToRefuel()
  354. end
  355. --doDump()
  356. --doUpDownCheck()
  357. end
  358. -- end
  359. -- if dx ~= 0 then
  360. while x ~= tx do
  361. print(x .. " - " .. tx)
  362. if x > tx then
  363. goWest()
  364. else
  365. goEast()
  366. end
  367. if(not testFuel()) then
  368. returnToRefuel()
  369. end
  370. --doDump()
  371. --doUpDownCheck()
  372. end
  373. -- end
  374. -- if dy ~= 0 then
  375. while y ~= ty do
  376. print(y .. " - " .. ty)
  377. if y < ty then
  378. goSouth()
  379. else
  380. goNorth()
  381. end
  382. if(not testFuel()) then
  383. returnToRefuel()
  384. end
  385. --doDump()
  386. --doUpDownCheck()
  387. end
  388. -- end
  389.  
  390. end
  391.  
  392. function gotoreverse(tx, ty, tz)
  393.  
  394. local dx = tx - x
  395. local dy = ty - y
  396. local dz = tz - z
  397. -- if dx ~= 0 then
  398. while x ~= tx do
  399. print(x .. " - " .. tx)
  400. if x > tx then
  401. goWest()
  402. else
  403. goEast()
  404. end
  405. if(not testFuel()) then
  406. returnToRefuel()
  407. end
  408. -- doUpDownCheck()
  409. --doDump()
  410. end
  411. -- end
  412. -- if dy ~= 0 then
  413. while y ~= ty do
  414. print(y .. " - " .. ty)
  415. if y < ty then
  416. goSouth()
  417. else
  418. goNorth()
  419. end
  420. if(not testFuel()) then
  421. returnToRefuel()
  422. end
  423. -- doUpDownCheck()
  424. --doDump()
  425. end
  426. -- end
  427.  
  428. -- if dz ~= 0 then
  429. while z ~= tz do
  430. print(z .. " - " .. tz)
  431. if z > tz then
  432. goDown()
  433. else
  434. goUp()
  435. end
  436. if(not testFuel()) then
  437. returnToRefuel()
  438. end
  439. --doUpDownCheck()
  440. --doDump()
  441. end
  442. -- end
  443.  
  444. end
  445.  
  446. function testFuel()
  447. local dist = math.abs(startx - x) + math.abs(starty - y) + math.abs(startz - z);
  448.  
  449. local fuel = estimateFuel()
  450. print("Estimated distance: "..dist)
  451. if(fuel < dist + 10) then
  452. return false
  453. end
  454.  
  455. return true
  456. end
  457.  
  458. function estimateFuel()
  459. return turtle.getFuelLevel();
  460.  
  461. end
  462.  
  463. function returnToRefuel()
  464. local ox = x
  465. local oy = y
  466. local oz = z
  467. goto(startx, starty, startz)
  468. refuelStation();
  469. turtle.refuel()
  470. goto(ox, oy, oz)
  471. end
  472.  
  473. function refuelStation()
  474. faceSouth()
  475. turtle.select(1)
  476. turtle.suck()
  477. turtle.refuel()
  478. turtle.select(yieldStart)
  479. faceNorth()
  480. end
  481.  
  482. print("Input Width")
  483. width = tonumber(read())
  484. print("Input Breadth")
  485. breadth = tonumber(read())
  486. print("Input Height")
  487. height = tonumber(read())
  488. print ("Start at top? y/n")
  489. fromTop = (read() == "y")
  490. print ("Start at right? y/n")
  491. fromRight = (read() == "y")
  492. altz = false
  493. alty = false
  494. altx = false
  495. for cx = 0, width-1 do
  496.  
  497. for cz = 0, height-1 do
  498. for cy = 0, breadth-1 do
  499. forward()
  500. end
  501. turtle.turnLeft()
  502. turtle.turnLeft()
  503. if startTop then
  504. if altz then
  505. goUp()
  506. else
  507. goDown()
  508. end
  509. else
  510. if not altz then
  511. goUp()
  512. else
  513. goDown()
  514. end
  515. end
  516. alty = not alty
  517. end
  518.  
  519. if fromRight then turtle.turnLeft() else turtle.turnRight() end
  520.  
  521. forward()
  522.  
  523. if fromRight then turtle.turnLeft() else turtle.turnRight() end
  524.  
  525. altz = not altz
  526. end
  527.  
  528. alt = false
  529. for cx=0, width-1 do
  530. for cy=0, breadth-1 do
  531. for cz=0, height-1 do
  532.  
  533. local zz = cz;
  534.  
  535. local yy = cy;
  536. local xx = cx;
  537.  
  538. if fromTop then zz = -zz; end
  539. if not fromRight then yy = -cy; end
  540. if alt then
  541. goto(xx, (breadth - yy)-1, zz)
  542. else
  543. goto(xx, yy, zz)
  544. end
  545. end
  546. end
  547. alt = not alt
  548. end
  549.  
  550. goto(0, 0, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement