WineCraftHD

Untitled

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