WineCraftHD

Untitled

Dec 16th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.64 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 = 122
  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. updateCoordinates(4)
  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. updateCoordinates(5)
  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 MineToMine()
  390. local x, y, z = 0
  391. if UseGPS then
  392. x, y, z = gps.locate(5)
  393. else
  394. x = XX
  395. y = YY
  396. z = ZZ
  397. end
  398. if MineDown then
  399. x = tonumber(x)
  400. y = tonumber(y)
  401. z = tonumber(z)
  402. if y > BranchMineLevel then
  403. MineCount = y - BranchMineLevel
  404. for i = 1,MineCount do
  405. if mov.down(1) then
  406. -- Print("Coords: ",x," ",y - i," ",z)
  407. else
  408. dig.down()
  409. mov.down(1)
  410. -- Print("Coords: ",x," ",y - i," ",z)
  411. end
  412. end
  413. elseif y < BranchMineLevel then
  414. MineCount = BranchMineLevel - y
  415. for i = 1,MineCount do
  416. if mov.up(1) then
  417. -- Print("Coords: ",x," ",y - i," ",z)
  418. else
  419. dig.up()
  420. mov.up(1)
  421. -- Print("Coords: ",x," ",y - i," ",z)
  422. end
  423. end
  424. end
  425. end
  426. for i = 1,tonumber(PositionInBranchMine) do
  427. DigMoveForward()
  428. end
  429. end
  430.  
  431. local function DepositChest()
  432. rot.right()
  433. rot.right()
  434. if not turtle.detect() then
  435. turtle.select(1)
  436. turtle.place()
  437. end
  438. print("Depositing the Load...")
  439. for i = IgnoreSlots + 1,15 do
  440. turtle.select(i)
  441. turtle.drop()
  442. end
  443. rot.right()
  444. rot.right()
  445. end
  446.  
  447. local function GetCoalChest()
  448. look(1) -- look left
  449. turtle.select(15)
  450. if turtle.suck() == false then
  451. rot.right()
  452. return false
  453. else
  454. if turtle.compareTo(16) then
  455. turtle.transferTo(16)
  456. turtle.drop()
  457. rot.right()
  458. return true
  459. else
  460. turtle.drop()
  461. rot.right()
  462. return false
  463. end
  464. end
  465. end
  466.  
  467. local function ReturnBack()
  468. local xC, yC, zC = 0
  469. if UseGPS then
  470. xC, yC, zC = gps.locate(5)
  471. else
  472. xC = XX
  473. yC = YY
  474. zC = ZZ
  475. end
  476. look(2) -- look backward
  477. Distance = 0
  478. xC = tonumber(xC)
  479. yC = tonumber(yC)
  480. zC = tonumber(zC)
  481. StartX = tonumber(StartX)
  482. StartY = tonumber(StartY)
  483. StartZ = tonumber(StartZ)
  484. if xC < StartX then
  485. Distance = StartX - xC
  486. elseif xC > StartX then
  487. Distance = xC - StartX
  488. elseif zC < StartZ then
  489. Distance = StartZ - zC
  490. elseif zC > StartZ then
  491. Distance = zC - StartZ
  492. end
  493.  
  494. for i = 1,Distance do
  495. if mov.forward(1) == false then
  496. dig.forward()
  497. mov.forward(1)
  498. end
  499. end
  500. look(0) -- look forward
  501. filewriteline("BranchMineData", 5, RotatePosition)
  502. if UseGPS then
  503. xC, yC, zC = gps.locate(5)
  504. else
  505. xC = XX
  506. yC = YY
  507. zC = ZZ
  508. end
  509. xC = tonumber(xC)
  510. yC = tonumber(yC)
  511. zC = tonumber(zC)
  512. StartX = tonumber(StartX)
  513. StartY = tonumber(StartY)
  514. StartZ = tonumber(StartZ)
  515. if StartY > yC then
  516. mov.up(StartY - yC)
  517. elseif StartY < yC then
  518. mov.down(yC - StartY)
  519. end
  520.  
  521. DepositChest()
  522. if GetCoalChest() == false then
  523. if Refuel() == false then
  524. print("***Out of fuel***")
  525. print("***NO FUEL IN CHEST, PROGRAM HALT***")
  526. return
  527. else
  528. print("Returning to Mine")
  529. MineToMine()
  530. StartMining()
  531. end
  532. else
  533. print("Returning to Mine")
  534. MineToMine()
  535. StartMining()
  536. end
  537. end
  538.  
  539. local function Finish()
  540. local xC, yC, zC = 0
  541. if UseGPS then
  542. xC, yC, zC = gps.locate(5)
  543. else
  544. xC = XX
  545. yC = YY
  546. zC = ZZ
  547. end
  548. look(2) -- look Backward
  549. RotatePosition = "BACKWARD"
  550. filewriteline("BranchMineData", 5, RotatePosition)
  551. Distance = 0
  552. if xC < StartX then
  553. Distance = StartX - xC
  554. elseif xC > StartX then
  555. Distance = xC - StartX
  556. elseif zC < StartZ then
  557. Distance = StartZ - zC
  558. elseif zC > StartZ then
  559. Distance = zC - StartZ
  560. end
  561.  
  562. for i = 1,Distance do
  563. if mov.forward(1) == false then
  564. dig.forward()
  565. mov.forward(1)
  566. end
  567. end
  568. look(0) -- look Forward
  569. RotatePosition = "FORWARD"
  570. filewriteline("BranchMineData", 5, RotatePosition)
  571. if UseGPS then
  572. xC, yC, zC = gps.locate(5)
  573. else
  574. xC = XX
  575. yC = YY
  576. zC = ZZ
  577. end
  578.  
  579. if StartY > yC then
  580. mov.up(StartY - yC)
  581. elseif StartY < yC then
  582. mov.down(yC - StartY)
  583. end
  584.  
  585. DepositChest()
  586. DigMoveForward()
  587. DigMoveForward()
  588. DigMoveForward()
  589. DigMoveForward()
  590. print("Finished!")
  591. end
  592.  
  593. local function InventoryFull()
  594. SlotsFull = 0
  595. for i=1,16 do
  596. if turtle.getItemCount(i) > 0 then
  597. SlotsFull = SlotsFull + 1
  598. end
  599. end
  600.  
  601. if SlotsFull == 16 then
  602. return true;
  603. else
  604. return false;
  605. end
  606. end
  607.  
  608. function StartMining()
  609.  
  610. BlocksTillEnd = TunnelLength - PositionInBranchMine
  611.  
  612. while BlocksTillEnd > 0 do
  613. if Refuel() == false then
  614. print("***Out of fuel***")
  615. ReturnBack()
  616. return
  617. end
  618.  
  619. if InventoryFull() then
  620. print("***Inventory Full***")
  621. ReturnBack()
  622. return
  623. end
  624.  
  625. dig.forward()
  626. DigMoveDown()
  627. DigMoveDown()
  628. DigMoveDown()
  629. DigMoveDown()
  630.  
  631. PositionInBranchMine = PositionInBranchMine + 4
  632. BlocksTillEnd = TunnelLength - PositionInBranchMine
  633. end
  634. Finish()
  635. end
  636.  
  637. local FuelCount = turtle.getItemCount(16)
  638.  
  639. if FuelCount == 0 then
  640. print("please put fuel in the last slot of the Turtle.")
  641. return
  642. else
  643. if FuelCount < 10 then
  644. print("Please put at least 10 of the fuel you are using in the Turtle.")
  645. return
  646. end
  647. end
  648.  
  649. Refuel()
  650. StartMining()
Advertisement
Add Comment
Please, Sign In to add comment