WineCraftHD

Untitled

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