WineCraftHD

Untitled

Dec 16th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.10 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. dig.forward = function()
  290. if turtle.detect() then
  291. while not turtle.dig() do
  292. sleep(1)
  293. end
  294. return true
  295. else
  296. print("No Block to mine forward")
  297. return false
  298. end
  299. end
  300.  
  301. dig.up = function()
  302. if turtle.detectUp() then
  303. while not turtle.digUp() do
  304. sleep(1)
  305. end
  306. return true
  307. else
  308. print("No Block to mine up")
  309. return false
  310. end
  311. end
  312.  
  313. dig.down = function()
  314. if turtle.detectDown() then
  315. while not turtle.digDown() do
  316. sleep(1)
  317. end
  318. return true
  319. else
  320. print("No Block to mine down")
  321. return false
  322. end
  323. end
  324.  
  325. local function DigMoveForward()
  326. if mov.forward(1) == false then
  327. dig.forward()
  328. sleep(0.5)
  329. DigMoveForward()
  330. end
  331. end
  332.  
  333. local function DigMoveUp()
  334. if mov.up(1) == false then
  335. dig.up()
  336. sleep(0.5)
  337. DigMoveUp()
  338. end
  339. end
  340.  
  341. local function DigMoveDown()
  342. if mov.down(1) == false then
  343. dig.down()
  344. sleep(0.5)
  345. DigMoveDown()
  346. end
  347. end
  348.  
  349. -- Begin BranchMining Code
  350. if UseGPS then
  351. if rednet.open("right") == nil then
  352. print("You had GPS Enabled but there is not a modem on this turtle")
  353. return
  354. end
  355. end
  356.  
  357. local function DepositChest()
  358. print("Depositing the Load...")
  359. for i = IgnoreSlots + 1,15 do
  360. turtle.select(i)
  361. turtle.drop()
  362. end
  363. end
  364.  
  365. local function GetCoalChest()
  366. look(1) -- look left
  367. turtle.select(15)
  368. if turtle.suck() == false then
  369. rot.right()
  370. return false
  371. else
  372. if turtle.compareTo(16) then
  373. turtle.transferTo(16)
  374. turtle.drop()
  375. rot.right()
  376. return true
  377. else
  378. turtle.drop()
  379. rot.right()
  380. return false
  381. end
  382. end
  383. end
  384.  
  385. local function ReturnBack()
  386. local xC, yC, zC = 0
  387. if UseGPS then
  388. xC, yC, zC = gps.locate(5)
  389. else
  390. xC = XX
  391. yC = YY
  392. zC = ZZ
  393. end
  394. look(2) -- look backward
  395. Distance = 0
  396. xC = tonumber(xC)
  397. yC = tonumber(yC)
  398. zC = tonumber(zC)
  399. StartX = tonumber(StartX)
  400. StartY = tonumber(StartY)
  401. StartZ = tonumber(StartZ)
  402. if xC < StartX then
  403. Distance = StartX - xC
  404. elseif xC > StartX then
  405. Distance = xC - StartX
  406. elseif zC < StartZ then
  407. Distance = StartZ - zC
  408. elseif zC > StartZ then
  409. Distance = zC - StartZ
  410. end
  411.  
  412. for i = 1,Distance do
  413. if mov.forward(1) == false then
  414. dig.forward()
  415. mov.forward(1)
  416. end
  417. end
  418. look(0) -- look forward
  419. filewriteline("BranchMineData", 5, RotatePosition)
  420. if UseGPS then
  421. xC, yC, zC = gps.locate(5)
  422. else
  423. xC = XX
  424. yC = YY
  425. zC = ZZ
  426. end
  427. xC = tonumber(xC)
  428. yC = tonumber(yC)
  429. zC = tonumber(zC)
  430. StartX = tonumber(StartX)
  431. StartY = tonumber(StartY)
  432. StartZ = tonumber(StartZ)
  433. if StartY > yC then
  434. mov.up(StartY - yC)
  435. elseif StartY < yC then
  436. mov.down(yC - StartY)
  437. end
  438.  
  439. DepositChest()
  440. if GetCoalChest() == false then
  441. if Refuel() == false then
  442. print("***Out of fuel***")
  443. print("***NO FUEL IN CHEST, PROGRAM HALT***")
  444. return
  445. else
  446. print("Returning to Mine")
  447. MineToMine()
  448. StartMining()
  449. end
  450. else
  451. print("Returning to Mine")
  452. MineToMine()
  453. StartMining()
  454. end
  455. end
  456.  
  457. local function Finish()
  458. local xC, yC, zC = 0
  459. if UseGPS then
  460. xC, yC, zC = gps.locate(5)
  461. else
  462. xC = XX
  463. yC = YY
  464. zC = ZZ
  465. end
  466. look(2) -- look Backward
  467. RotatePosition = "BACKWARD"
  468. filewriteline("BranchMineData", 5, RotatePosition)
  469. Distance = 0
  470. if xC < StartX then
  471. Distance = StartX - xC
  472. elseif xC > StartX then
  473. Distance = xC - StartX
  474. elseif zC < StartZ then
  475. Distance = StartZ - zC
  476. elseif zC > StartZ then
  477. Distance = zC - StartZ
  478. end
  479.  
  480. for i = 1,Distance do
  481. if mov.forward(1) == false then
  482. dig.forward()
  483. mov.forward(1)
  484. end
  485. end
  486. look(0) -- look Forward
  487. RotatePosition = "FORWARD"
  488. filewriteline("BranchMineData", 5, RotatePosition)
  489. if UseGPS then
  490. xC, yC, zC = gps.locate(5)
  491. else
  492. xC = XX
  493. yC = YY
  494. zC = ZZ
  495. end
  496.  
  497. if StartY > yC then
  498. mov.up(StartY - yC)
  499. elseif StartY < yC then
  500. mov.down(yC - StartY)
  501. end
  502.  
  503. DepositChest()
  504. print("Finished!")
  505. end
  506.  
  507. local function InventoryFull()
  508. SlotsFull = 0
  509. for i=1,16 do
  510. if turtle.getItemCount(i) > 0 then
  511. SlotsFull = SlotsFull + 1
  512. end
  513. end
  514.  
  515. if SlotsFull == 16 then
  516. return true;
  517. else
  518. return false;
  519. end
  520. end
  521.  
  522. function StartMining()
  523.  
  524. BlocksTillEnd = TunnelLength - PositionInBranchMine
  525.  
  526. while BlocksTillEnd > 0 do
  527. if Refuel() == false then
  528. print("***Out of fuel***")
  529. ReturnBack()
  530. return
  531. end
  532.  
  533. if InventoryFull() then
  534. print("***Inventory Full***")
  535. ReturnBack()
  536. return
  537. end
  538.  
  539. dig.forward()
  540. DigMoveDown()
  541. DigMoveDown()
  542. DigMoveDown()
  543. DigMoveDown()
  544.  
  545. PositionInBranchMine = PositionInBranchMine + 4
  546. BlocksTillEnd = TunnelLength - PositionInBranchMine
  547. end
  548. Finish()
  549. end
  550.  
  551. local FuelCount = turtle.getItemCount(16)
  552.  
  553. if FuelCount == 0 then
  554. print("please put fuel in the last slot of the Turtle.")
  555. return
  556. else
  557. if FuelCount < 10 then
  558. print("Please put at least 10 of the fuel you are using in the Turtle.")
  559. return
  560. end
  561. end
  562.  
  563. Refuel()
  564. StartMining()
Advertisement
Add Comment
Please, Sign In to add comment