Advertisement
lemmy101

Untitled

Apr 25th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.67 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. end
  254.  
  255. function goWest()
  256. faceWest()
  257. forward()
  258. end
  259.  
  260. function goEast()
  261. faceEast()
  262. forward()
  263. end
  264.  
  265. function goNorth()
  266. faceNorth()
  267. forward()
  268. end
  269.  
  270. function goSouth()
  271. faceSouth()
  272. forward()
  273. end
  274.  
  275. function goUp()
  276. DigMoveUp()
  277.  
  278. end
  279.  
  280. function goDown()
  281. DigMoveDown()
  282.  
  283. end
  284.  
  285.  
  286.  
  287. local function readLines(sPath)
  288. local file = fs.open(sPath, "r") -- open the file
  289. if file then -- check if it's open
  290. local tLines = {} -- table to store the lines
  291. local sLine = file.readLine() -- read a line from the file
  292. while sLine do -- while there's a line in the file
  293. table.insert(tLines, sLine) -- add it to the table
  294. sLine = file.readLine() -- get the next line
  295. end
  296. file.close() -- close the file
  297. return tLines -- return the table with the lines
  298. end
  299. return nil -- there was an error opening the file, return nil to let the user know
  300. end
  301.  
  302. local function writeLines(sPath, tLines)
  303. local file = fs.open(sPath, "w") -- open the file
  304. if file then -- check if the file is open
  305. for _, sLine in ipairs(tLines) do -- for each line in the table
  306. file.writeLine(sLine) -- write the line
  307. end
  308. file.close() -- close the file
  309. end
  310. end
  311.  
  312. function filereadline(filename, line)
  313. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  314. if not tLines then -- if there was an error
  315. return nil -- return nil/error
  316. end
  317. return tLines[line] -- return the line
  318. end
  319.  
  320. function filewriteline(filename, line, text)
  321. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  322. if tLines then -- if there was no error
  323. tLines[line] = text -- set the line
  324. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  325. end
  326. end
  327.  
  328. function goto(tx, ty, tz)
  329.  
  330. local dx = tx - x
  331. local dy = ty - y
  332. local dz = tz - z
  333.  
  334. -- if dz ~= 0 then
  335. while z ~= tz do
  336. print(z .. " - " .. tz)
  337. if z > tz then
  338. goDown()
  339. else
  340. goUp()
  341. end
  342. if(not testFuel()) then
  343. returnToRefuel()
  344. end
  345. --doDump()
  346. --doUpDownCheck()
  347. end
  348. -- end
  349. -- if dx ~= 0 then
  350. while x ~= tx do
  351. print(x .. " - " .. tx)
  352. if x > tx then
  353. goWest()
  354. else
  355. goEast()
  356. end
  357. if(not testFuel()) then
  358. returnToRefuel()
  359. end
  360. --doDump()
  361. --doUpDownCheck()
  362. end
  363. -- end
  364. -- if dy ~= 0 then
  365. while y ~= ty do
  366. print(y .. " - " .. ty)
  367. if y < ty then
  368. goSouth()
  369. else
  370. goNorth()
  371. end
  372. if(not testFuel()) then
  373. returnToRefuel()
  374. end
  375. --doDump()
  376. --doUpDownCheck()
  377. end
  378. -- end
  379.  
  380. end
  381.  
  382. function gotoreverse(tx, ty, tz)
  383.  
  384. local dx = tx - x
  385. local dy = ty - y
  386. local dz = tz - z
  387. -- if dx ~= 0 then
  388. while x ~= tx do
  389. print(x .. " - " .. tx)
  390. if x > tx then
  391. goWest()
  392. else
  393. goEast()
  394. end
  395. if(not testFuel()) then
  396. returnToRefuel()
  397. end
  398. -- doUpDownCheck()
  399. --doDump()
  400. end
  401. -- end
  402. -- if dy ~= 0 then
  403. while y ~= ty do
  404. print(y .. " - " .. ty)
  405. if y < ty then
  406. goSouth()
  407. else
  408. goNorth()
  409. end
  410. if(not testFuel()) then
  411. returnToRefuel()
  412. end
  413. -- doUpDownCheck()
  414. --doDump()
  415. end
  416. -- end
  417.  
  418. -- if dz ~= 0 then
  419. while z ~= tz do
  420. print(z .. " - " .. tz)
  421. if z > tz then
  422. goDown()
  423. else
  424. goUp()
  425. end
  426. if(not testFuel()) then
  427. returnToRefuel()
  428. end
  429. --doUpDownCheck()
  430. --doDump()
  431. end
  432. -- end
  433.  
  434. end
  435.  
  436. function testFuel()
  437. local dist = math.abs(startx - x) + math.abs(starty - y) + math.abs(startz - z);
  438.  
  439. local fuel = estimateFuel()
  440. print("Estimated distance: "..dist)
  441. if(fuel < dist + 10) then
  442. return false
  443. end
  444.  
  445. return true
  446. end
  447.  
  448. function estimateFuel()
  449. return turtle.getFuelLevel();
  450.  
  451. end
  452.  
  453. function returnToRefuel()
  454. local ox = x
  455. local oy = y
  456. local oz = z
  457. goto(startx, starty, startz)
  458. refuelStation();
  459. turtle.refuel()
  460. goto(ox, oy, oz)
  461. end
  462.  
  463. function refuelStation()
  464. faceSouth()
  465. turtle.select(1)
  466. turtle.suck()
  467. turtle.refuel()
  468. turtle.select(yieldStart)
  469. faceNorth()
  470. end
  471.  
  472. print("Input Width")
  473. width = tonumber(read())
  474. print("Input Breadth")
  475. breadth = tonumber(read())
  476. print("Input Height")
  477. height = tonumber(read())
  478. print ("Start at top? y/n")
  479. fromTop = (read() == "y")
  480. print ("Start at right? y/n")
  481. fromRight = (read() == "y")
  482. altz = false
  483. alty = false
  484. altx = false
  485. turtle.refuel()
  486. isZOdd = math.floor(height/2) ~= height
  487. for cx = 0, width-1 do
  488.  
  489. for cz = 0, height-1 do
  490. for cy = 0, breadth-1 do
  491. forward()
  492. end
  493. turtle.turnLeft()
  494. turtle.turnLeft()
  495. if startTop then
  496. if altz then
  497. goUp()
  498. else
  499. goDown()
  500. end
  501. else
  502. if not altz then
  503. goUp()
  504. else
  505. goDown()
  506. end
  507. end
  508. alty = not alty
  509. end
  510.  
  511. print ("fromRight = "..tostring(fromRight))
  512. local test = fromRight
  513.  
  514. if isZOdd then test = not test end
  515. print ("isZOdd = "..tostring(isZOdd))
  516. if altz then test = not test end
  517. print ("altz = "..tostring(altz))
  518.  
  519. if test then
  520. print ("left")
  521. turtle.turnLeft()
  522. else
  523. print ("right")
  524. turtle.turnRight()
  525. end
  526.  
  527. forward()
  528.  
  529. if test then
  530. print ("left")
  531. turtle.turnLeft()
  532. else
  533. print ("right")
  534. turtle.turnRight()
  535. end
  536.  
  537. altz = not altz
  538.  
  539. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement