WineCraftHD

Untitled

Dec 16th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.48 KB | None | 0 0
  1. -- Remember to put a chest directly behind the turtle at it's current location.
  2. -- It will deliver it's resources to this chest when full
  3.  
  4. -- Y Level to Branch-mine at
  5. local BranchMineLevel = 5
  6.  
  7. -- How many blocks on the ignore list
  8. -- that you put in the first number of slots
  9. local IgnoreSlots = 1
  10.  
  11. -- Set this to true if it is ok for the turtle to mine strait down to the y level for the Branch mine
  12. local MineDown = true
  13. local TunnelLength = 120
  14.  
  15. -- Set this to false if you don't have a working GPS system
  16. local UseGPS = false
  17. --ALL THIS MUST BE FILLED IN IF YOU DON"T USE GPS
  18. local StartX = 697
  19. local StartY = 125
  20. local StartZ = 1396
  21. -- EX: "SOUTH", "NORTH", "EAST", "WEST"
  22. local ForwardFacingDirection = "WEST"
  23. --END NO GPS FILL INFORMATION
  24.  
  25.  
  26.  
  27. --DO NOT TOUCH ANYTHING BELOW THIS LINE
  28. local mov = {}
  29. local rot = {}
  30. local dig = {}
  31. local PositionInBranchMine = 0
  32. local RotatePosition = "FORWARD"
  33.  
  34. local InsertedIntoLeftWall = false
  35. local InsertedIntoRightWall = false
  36. local InsertionAmount = 0
  37.  
  38. local XX = StartX
  39. local YY = StartY
  40. local ZZ = StartZ
  41. local PrimaryAxis = ""
  42. local SecondaryAxis = ""
  43.  
  44. local function Refuel()
  45. currentFuelRequired = (10 + (TunnelLength-PositionInBranchMine)*2)/40
  46. if turtle.getFuelLevel() < currentFuelRequired then
  47. turtle.select(16)
  48. return turtle.refuel(currentFuelRequired)
  49. else
  50. return true
  51. end
  52. end
  53.  
  54.  
  55. local function readLines(sPath)
  56. local file = fs.open(sPath, "r") -- open the file
  57. if file then -- check if it's open
  58. local tLines = {} -- table to store the lines
  59. local sLine = file.readLine() -- read a line from the file
  60. while sLine do -- while there's a line in the file
  61. table.insert(tLines, sLine) -- add it to the table
  62. sLine = file.readLine() -- get the next line
  63. end
  64. file.close() -- close the file
  65. return tLines -- return the table with the lines
  66. end
  67. return nil -- there was an error opening the file, return nil to let the user know
  68. end
  69.  
  70. local function writeLines(sPath, tLines)
  71. local file = fs.open(sPath, "w") -- open the file
  72. if file then -- check if the file is open
  73. for _, sLine in ipairs(tLines) do -- for each line in the table
  74. file.writeLine(sLine) -- write the line
  75. end
  76. file.close() -- close the file
  77. end
  78. end
  79.  
  80. function filereadline(filename, line)
  81. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  82. if not tLines then -- if there was an error
  83. return nil -- return nil/error
  84. end
  85. return tLines[line] -- return the line
  86. end
  87.  
  88. function filewriteline(filename, line, text)
  89. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  90. if tLines then -- if there was no error
  91. tLines[line] = text -- set the line
  92. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  93. end
  94. end
  95.  
  96. local function incrementCoordinates(incX,incY,incZ)
  97. XX = XX + incX
  98. YY = YY + incY
  99. ZZ = ZZ + incZ
  100. filewriteline("BranchMineData", 7, XX)
  101. filewriteline("BranchMineData", 8, YY)
  102. filewriteline("BranchMineData", 9, ZZ)
  103. end
  104.  
  105. --directionInteger 0 = forward, 1 = left, 2 = backward, 3 = right, 4 = up, 5 = down
  106. local function updateCoordinates(directionInteger)
  107. --Displace the direction by the current Rotation
  108. if directionInteger <= 3 then
  109. if directionInteger == 0 then
  110. if string.match(RotatePosition,"LEFT") then
  111. directionInteger = 1
  112. elseif string.match(RotatePosition,"BACKWARD") then
  113. directionInteger = 2
  114. elseif string.match(RotatePosition,"RIGHT") then
  115. directionInteger = 3
  116. end
  117. elseif directionInteger == 2 then
  118. if string.match(RotatePosition,"LEFT") then
  119. directionInteger = 3
  120. elseif string.match(RotatePosition,"BACKWARD") then
  121. directionInteger = 0
  122. elseif string.match(RotatePosition,"RIGHT") then
  123. directionInteger = 1
  124. end
  125. end
  126. end
  127.  
  128. local incrementPrimary = true
  129. local incrementBy = 0
  130. local incrementX = 0
  131. local incrementY = 0
  132. local incrementZ = 0
  133. --if we are moving forward
  134. if directionInteger == 0 then
  135. incrementPrimary = true
  136. incrementBy = 1
  137. --if we are moving left
  138. elseif directionInteger == 1 then
  139. incrementPrimary = false
  140. incrementBy = 1
  141. --if we are moving backward
  142. elseif directionInteger == 2 then
  143. incrementPrimary = true
  144. incrementBy = -1
  145. --if we are moving right
  146. elseif directionInteger == 3 then
  147. incrementPrimary =false
  148. incrementBy = -1
  149. elseif directionInteger == 4 then
  150. incrementY = 1
  151. elseif directionInteger == 5 then
  152. incrementY = -1
  153. end
  154.  
  155. if incrementPrimary then
  156. if PrimaryAxis == "X" then
  157. incrementX = incrementBy
  158. elseif PrimaryAxis == "-X" then
  159. incrementX = -1 * incrementBy
  160. elseif PrimaryAxis == "Z" then
  161. incrementZ = incrementBy
  162. elseif PrimaryAxis == "-Z" then
  163. incrementZ = -1 * incrementBy
  164. end
  165. else
  166. if SecondaryAxis == "X" then
  167. incrementX = incrementBy
  168. elseif SecondaryAxis == "-X" then
  169. incrementX = -1 * incrementBy
  170. elseif SecondaryAxis == "Z" then
  171. incrementZ = incrementBy
  172. elseif SecondaryAxis == "-Z" then
  173. incrementZ = -1 * incrementBy
  174. end
  175. end
  176. incrementCoordinates(incrementX,incrementY,incrementZ)
  177. end
  178.  
  179. mov.forward = function(times)
  180. local boolean v = false
  181. for i=1,times do
  182. if turtle.detect() == false then
  183. while not turtle.forward() do
  184. Refuel()
  185. sleep(1)
  186. end
  187. updateCoordinates(0)
  188. v = true
  189. else
  190. v = false
  191. return v
  192. end
  193. end
  194. return v
  195. end
  196.  
  197. mov.back = function(times)
  198. local boolean v = false
  199. for i=1,times do
  200. while not turtle.back() do
  201. Refuel()
  202. sleep(1)
  203. end
  204. updateCoordinates(2)
  205. v = true
  206. end
  207. return v
  208. end
  209.  
  210. mov.up = function(times)
  211. local boolean v = false
  212. for i=1,times do
  213. if turtle.detectUp() == false then
  214. while not turtle.up() do
  215. Refuel()
  216. sleep(1)
  217. end
  218. YY = YY + 1
  219. v = true
  220. else
  221. v = false
  222. return v
  223. end
  224. end
  225. return v
  226. end
  227.  
  228. mov.down = function(times)
  229. local boolean v = false
  230. for i=1,times do
  231. if turtle.detectDown() == false then
  232. while not turtle.down() do
  233. Refuel()
  234. sleep(1)
  235. end
  236. YY = YY - 1
  237. v = true
  238. else
  239. v = false
  240. return v
  241. end
  242. end
  243. return v
  244. end
  245.  
  246. mov.place = function()
  247. while not turtle.place() do
  248. sleep(1)
  249. end
  250. return true
  251. end
  252.  
  253. rot.right = function()
  254. while not turtle.turnRight() do
  255. sleep(1)
  256. end
  257.  
  258. if RotatePosition == "FORWARD" then
  259. RotatePosition = "RIGHT"
  260. elseif RotatePosition == "RIGHT" then
  261. RotatePosition = "BACKWARD"
  262. elseif RotatePosition == "BACKWARD" then
  263. RotatePosition = "LEFT"
  264. elseif RotatePosition == "LEFT" then
  265. RotatePosition = "FORWARD"
  266. end
  267. filewriteline("BranchMineData", 5, RotatePosition)
  268. return true
  269. end
  270.  
  271. rot.left = function()
  272. while not turtle.turnLeft() do
  273. sleep(1)
  274. end
  275.  
  276. if RotatePosition == "FORWARD" then
  277. RotatePosition = "LEFT"
  278. elseif RotatePosition == "LEFT" then
  279. RotatePosition = "BACKWARD"
  280. elseif RotatePosition == "BACKWARD" then
  281. RotatePosition = "RIGHT"
  282. elseif RotatePosition == "RIGHT" then
  283. RotatePosition = "FORWARD"
  284. end
  285. filewriteline("BranchMineData", 5, RotatePosition)
  286. return true
  287. end
  288.  
  289.  
  290. -- RotateTo is a number where 0 = Forward, 1 = Left, 2 = Backward, and 3 = Right
  291. local function look(RotateTo)
  292. --This rids one odd occurance
  293. if RotatePosition == "RIGHT" and RotateTo == 0 then
  294. rot.left()
  295. return
  296. end
  297. --
  298.  
  299. rotateNumber = 0
  300. if RotatePosition == "LEFT" then
  301. rotateNumber = 1
  302. elseif RotatePosition == "BACKWARD" then
  303. rotateNumber = 2
  304. elseif RotatePosition == "RIGHT" then
  305. rotateNumber = 3
  306. end
  307.  
  308. rotateRight = rotateNumber - RotateTo
  309. if rotateRight >= 0 then
  310. for i = 1,rotateRight do
  311. rot.right()
  312. end
  313. else
  314. for i = 1,math.abs(rotateRight) do
  315. rot.left()
  316. end
  317. end
  318.  
  319. end
  320.  
  321. dig.forward = function()
  322. if turtle.detect() then
  323. while not turtle.dig() do
  324. sleep(1)
  325. end
  326. return true
  327. else
  328. print("No Block to mine forward")
  329. return false
  330. end
  331. end
  332.  
  333. dig.up = function()
  334. if turtle.detectUp() then
  335. while not turtle.digUp() do
  336. sleep(1)
  337. end
  338. return true
  339. else
  340. print("No Block to mine up")
  341. return false
  342. end
  343. end
  344.  
  345. dig.down = function()
  346. if turtle.detectDown() then
  347. while not turtle.digDown() do
  348. sleep(1)
  349. end
  350. return true
  351. else
  352. print("No Block to mine down")
  353. return false
  354. end
  355. end
  356.  
  357. local function DigMoveForward()
  358. if mov.forward(1) == false then
  359. dig.forward()
  360. sleep(0.5)
  361. DigMoveForward()
  362. end
  363. end
  364.  
  365. local function DigMoveUp()
  366. if mov.up(1) == false then
  367. dig.up()
  368. sleep(0.5)
  369. DigMoveUp()
  370. end
  371. end
  372.  
  373. local function DigMoveDown()
  374. if mov.down(1) == false then
  375. dig.down()
  376. sleep(0.5)
  377. DigMoveDown()
  378. end
  379. end
  380.  
  381. -- Begin BranchMining Code
  382. if UseGPS then
  383. if rednet.open("right") == nil then
  384. print("You had GPS Enabled but there is not a modem on this turtle")
  385. return
  386. end
  387. end
  388.  
  389. local function DepositChest()
  390. rot.right()
  391. rot.right()
  392. if not turtle.detect() then
  393. turtle.select(1)
  394. turtle.place()
  395. end
  396. print("Depositing the Load...")
  397. for i = IgnoreSlots + 1,15 do
  398. turtle.select(i)
  399. turtle.drop()
  400. end
  401. rot.right()
  402. rot.right()
  403. end
  404.  
  405. local function GetCoalChest()
  406. look(1) -- look left
  407. turtle.select(15)
  408. if turtle.suck() == false then
  409. rot.right()
  410. return false
  411. else
  412. if turtle.compareTo(16) then
  413. turtle.transferTo(16)
  414. turtle.drop()
  415. rot.right()
  416. return true
  417. else
  418. turtle.drop()
  419. rot.right()
  420. return false
  421. end
  422. end
  423. end
  424.  
  425. local function ReturnBack()
  426. local xC, yC, zC = 0
  427. if UseGPS then
  428. xC, yC, zC = gps.locate(5)
  429. else
  430. xC = XX
  431. yC = YY
  432. zC = ZZ
  433. end
  434. look(2) -- look backward
  435. Distance = 0
  436. xC = tonumber(xC)
  437. yC = tonumber(yC)
  438. zC = tonumber(zC)
  439. StartX = tonumber(StartX)
  440. StartY = tonumber(StartY)
  441. StartZ = tonumber(StartZ)
  442. if xC < StartX then
  443. Distance = StartX - xC
  444. elseif xC > StartX then
  445. Distance = xC - StartX
  446. elseif zC < StartZ then
  447. Distance = StartZ - zC
  448. elseif zC > StartZ then
  449. Distance = zC - StartZ
  450. end
  451.  
  452. for i = 1,Distance do
  453. if mov.forward(1) == false then
  454. dig.forward()
  455. mov.forward(1)
  456. end
  457. end
  458. look(0) -- look forward
  459. filewriteline("BranchMineData", 5, RotatePosition)
  460. if UseGPS then
  461. xC, yC, zC = gps.locate(5)
  462. else
  463. xC = XX
  464. yC = YY
  465. zC = ZZ
  466. end
  467. xC = tonumber(xC)
  468. yC = tonumber(yC)
  469. zC = tonumber(zC)
  470. StartX = tonumber(StartX)
  471. StartY = tonumber(StartY)
  472. StartZ = tonumber(StartZ)
  473. if StartY > yC then
  474. mov.up(StartY - yC)
  475. elseif StartY < yC then
  476. mov.down(yC - StartY)
  477. end
  478.  
  479. DepositChest()
  480. if GetCoalChest() == false then
  481. if Refuel() == false then
  482. print("***Out of fuel***")
  483. print("***NO FUEL IN CHEST, PROGRAM HALT***")
  484. return
  485. else
  486. print("Returning to Mine")
  487. for i=1,PositionInBranchMine do
  488. DigMoveDown()
  489. end
  490. StartMining()
  491. end
  492. else
  493. print("Returning to Mine")
  494. for i=1,PositionInBranchMine do
  495. DigMoveDown()
  496. end
  497. StartMining()
  498. end
  499. end
  500.  
  501. local function Finish()
  502. local xC, yC, zC = 0
  503. if UseGPS then
  504. xC, yC, zC = gps.locate(5)
  505. else
  506. xC = XX
  507. yC = YY
  508. zC = ZZ
  509. end
  510. look(2) -- look Backward
  511. RotatePosition = "BACKWARD"
  512. filewriteline("BranchMineData", 5, RotatePosition)
  513. Distance = 0
  514. if xC < StartX then
  515. Distance = StartX - xC
  516. elseif xC > StartX then
  517. Distance = xC - StartX
  518. elseif zC < StartZ then
  519. Distance = StartZ - zC
  520. elseif zC > StartZ then
  521. Distance = zC - StartZ
  522. end
  523.  
  524. for i = 1,Distance do
  525. if mov.forward(1) == false then
  526. dig.forward()
  527. mov.forward(1)
  528. end
  529. end
  530. look(0) -- look Forward
  531. RotatePosition = "FORWARD"
  532. filewriteline("BranchMineData", 5, RotatePosition)
  533. if UseGPS then
  534. xC, yC, zC = gps.locate(5)
  535. else
  536. xC = XX
  537. yC = YY
  538. zC = ZZ
  539. end
  540.  
  541. if StartY > yC then
  542. mov.up(StartY - yC)
  543. elseif StartY < yC then
  544. mov.down(yC - StartY)
  545. end
  546.  
  547. DepositChest()
  548. DigMoveForward()
  549. DigMoveForward()
  550. DigMoveForward()
  551. DigMoveForward()
  552. print("Finished!")
  553. end
  554.  
  555. local function InventoryFull()
  556. SlotsFull = 0
  557. for i=1,16 do
  558. if turtle.getItemCount(i) > 0 then
  559. SlotsFull = SlotsFull + 1
  560. end
  561. end
  562.  
  563. if SlotsFull == 16 then
  564. return true;
  565. else
  566. return false;
  567. end
  568. end
  569.  
  570. function StartMining()
  571.  
  572. BlocksTillEnd = TunnelLength - PositionInBranchMine
  573.  
  574. while BlocksTillEnd > 0 do
  575. if Refuel() == false then
  576. print("***Out of fuel***")
  577. ReturnBack()
  578. return
  579. end
  580.  
  581. if InventoryFull() then
  582. print("***Inventory Full***")
  583. ReturnBack()
  584. return
  585. end
  586.  
  587. dig.forward()
  588. DigMoveDown()
  589. DigMoveDown()
  590. DigMoveDown()
  591. DigMoveDown()
  592.  
  593. PositionInBranchMine = PositionInBranchMine + 4
  594. BlocksTillEnd = TunnelLength - PositionInBranchMine
  595. end
  596. Finish()
  597. end
  598.  
  599. local FuelCount = turtle.getItemCount(16)
  600.  
  601. if FuelCount == 0 then
  602. print("please put fuel in the last slot of the Turtle.")
  603. return
  604. else
  605. if FuelCount < 10 then
  606. print("Please put at least 10 of the fuel you are using in the Turtle.")
  607. return
  608. end
  609. end
  610.  
  611. Refuel()
  612. StartMining()
Advertisement
Add Comment
Please, Sign In to add comment