WineCraftHD

Untitled

Dec 16th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.63 KB | None | 0 0
  1. local IgnoreSlots = 2
  2. local fuelIndex = 2
  3.  
  4. local TunnelLength = 120
  5.  
  6. -- Set this to false if you don't have a working GPS system
  7. local UseGPS = false
  8. --ALL THIS MUST BE FILLED IN IF YOU DON"T USE GPS
  9. local StartX = 697
  10. local StartY = 125
  11. local StartZ = 1396
  12. -- EX: "SOUTH", "NORTH", "EAST", "WEST"
  13. local ForwardFacingDirection = "WEST"
  14. --END NO GPS FILL INFORMATION
  15.  
  16.  
  17.  
  18. --DO NOT TOUCH ANYTHING BELOW THIS LINE
  19. local mov = {}
  20. local rot = {}
  21. local dig = {}
  22. local PositionInBranchMine = 0
  23. local RotatePosition = "FORWARD"
  24.  
  25. local InsertedIntoLeftWall = false
  26. local InsertedIntoRightWall = false
  27. local InsertionAmount = 0
  28.  
  29. local XX = StartX
  30. local YY = StartY
  31. local ZZ = StartZ
  32. local PrimaryAxis = ""
  33. local SecondaryAxis = ""
  34.  
  35. local function Refuel()
  36. currentFuelRequired = (10 + (TunnelLength-PositionInBranchMine)*2)/40
  37. if turtle.getFuelLevel() < currentFuelRequired then
  38. turtle.select(fuelIndex)
  39. return turtle.refuel(currentFuelRequired)
  40. else
  41. return true
  42. end
  43. end
  44.  
  45.  
  46. local function readLines(sPath)
  47. local file = fs.open(sPath, "r") -- open the file
  48. if file then -- check if it's open
  49. local tLines = {} -- table to store the lines
  50. local sLine = file.readLine() -- read a line from the file
  51. while sLine do -- while there's a line in the file
  52. table.insert(tLines, sLine) -- add it to the table
  53. sLine = file.readLine() -- get the next line
  54. end
  55. file.close() -- close the file
  56. return tLines -- return the table with the lines
  57. end
  58. return nil -- there was an error opening the file, return nil to let the user know
  59. end
  60.  
  61. local function writeLines(sPath, tLines)
  62. local file = fs.open(sPath, "w") -- open the file
  63. if file then -- check if the file is open
  64. for _, sLine in ipairs(tLines) do -- for each line in the table
  65. file.writeLine(sLine) -- write the line
  66. end
  67. file.close() -- close the file
  68. end
  69. end
  70.  
  71. function filereadline(filename, line)
  72. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  73. if not tLines then -- if there was an error
  74. return nil -- return nil/error
  75. end
  76. return tLines[line] -- return the line
  77. end
  78.  
  79. function filewriteline(filename, line, text)
  80. local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  81. if tLines then -- if there was no error
  82. tLines[line] = text -- set the line
  83. writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  84. end
  85. end
  86.  
  87. local function incrementCoordinates(incX,incY,incZ)
  88. XX = XX + incX
  89. YY = YY + incY
  90. ZZ = ZZ + incZ
  91. filewriteline("BranchMineData", 7, XX)
  92. filewriteline("BranchMineData", 8, YY)
  93. filewriteline("BranchMineData", 9, ZZ)
  94. end
  95.  
  96. --directionInteger 0 = forward, 1 = left, 2 = backward, 3 = right, 4 = up, 5 = down
  97. local function updateCoordinates(directionInteger)
  98. --Displace the direction by the current Rotation
  99. if directionInteger <= 3 then
  100. if directionInteger == 0 then
  101. if string.match(RotatePosition,"LEFT") then
  102. directionInteger = 1
  103. elseif string.match(RotatePosition,"BACKWARD") then
  104. directionInteger = 2
  105. elseif string.match(RotatePosition,"RIGHT") then
  106. directionInteger = 3
  107. end
  108. elseif directionInteger == 2 then
  109. if string.match(RotatePosition,"LEFT") then
  110. directionInteger = 3
  111. elseif string.match(RotatePosition,"BACKWARD") then
  112. directionInteger = 0
  113. elseif string.match(RotatePosition,"RIGHT") then
  114. directionInteger = 1
  115. end
  116. end
  117. end
  118.  
  119. local incrementPrimary = true
  120. local incrementBy = 0
  121. local incrementX = 0
  122. local incrementY = 0
  123. local incrementZ = 0
  124. --if we are moving forward
  125. if directionInteger == 0 then
  126. incrementPrimary = true
  127. incrementBy = 1
  128. --if we are moving left
  129. elseif directionInteger == 1 then
  130. incrementPrimary = false
  131. incrementBy = 1
  132. --if we are moving backward
  133. elseif directionInteger == 2 then
  134. incrementPrimary = true
  135. incrementBy = -1
  136. --if we are moving right
  137. elseif directionInteger == 3 then
  138. incrementPrimary =false
  139. incrementBy = -1
  140. elseif directionInteger == 4 then
  141. incrementY = 1
  142. elseif directionInteger == 5 then
  143. incrementY = -1
  144. end
  145.  
  146. if incrementPrimary then
  147. if PrimaryAxis == "X" then
  148. incrementX = incrementBy
  149. elseif PrimaryAxis == "-X" then
  150. incrementX = -1 * incrementBy
  151. elseif PrimaryAxis == "Z" then
  152. incrementZ = incrementBy
  153. elseif PrimaryAxis == "-Z" then
  154. incrementZ = -1 * incrementBy
  155. end
  156. else
  157. if SecondaryAxis == "X" then
  158. incrementX = incrementBy
  159. elseif SecondaryAxis == "-X" then
  160. incrementX = -1 * incrementBy
  161. elseif SecondaryAxis == "Z" then
  162. incrementZ = incrementBy
  163. elseif SecondaryAxis == "-Z" then
  164. incrementZ = -1 * incrementBy
  165. end
  166. end
  167. incrementCoordinates(incrementX,incrementY,incrementZ)
  168. end
  169.  
  170. mov.forward = function(times)
  171. local boolean v = false
  172. for i=1,times do
  173. if turtle.detect() == false then
  174. while not turtle.forward() do
  175. Refuel()
  176. sleep(1)
  177. end
  178. updateCoordinates(0)
  179. v = true
  180. else
  181. v = false
  182. return v
  183. end
  184. end
  185. return v
  186. end
  187.  
  188. mov.back = function(times)
  189. local boolean v = false
  190. for i=1,times do
  191. while not turtle.back() do
  192. Refuel()
  193. sleep(1)
  194. end
  195. updateCoordinates(2)
  196. v = true
  197. end
  198. return v
  199. end
  200.  
  201. mov.up = function(times)
  202. local boolean v = false
  203. for i=1,times do
  204. if turtle.detectUp() == false then
  205. while not turtle.up() do
  206. Refuel()
  207. sleep(1)
  208. end
  209. YY = YY + 1
  210. v = true
  211. else
  212. v = false
  213. return v
  214. end
  215. end
  216. return v
  217. end
  218.  
  219. mov.down = function(times)
  220. local boolean v = false
  221. for i=1,times do
  222. if turtle.detectDown() == false then
  223. while not turtle.down() do
  224. Refuel()
  225. sleep(1)
  226. end
  227. YY = YY - 1
  228. v = true
  229. else
  230. v = false
  231. return v
  232. end
  233. end
  234. return v
  235. end
  236.  
  237. mov.place = function()
  238. while not turtle.place() do
  239. sleep(1)
  240. end
  241. return true
  242. end
  243.  
  244. rot.right = function()
  245. while not turtle.turnRight() do
  246. sleep(1)
  247. end
  248.  
  249. if RotatePosition == "FORWARD" then
  250. RotatePosition = "RIGHT"
  251. elseif RotatePosition == "RIGHT" then
  252. RotatePosition = "BACKWARD"
  253. elseif RotatePosition == "BACKWARD" then
  254. RotatePosition = "LEFT"
  255. elseif RotatePosition == "LEFT" then
  256. RotatePosition = "FORWARD"
  257. end
  258. filewriteline("BranchMineData", 5, RotatePosition)
  259. return true
  260. end
  261.  
  262. rot.left = function()
  263. while not turtle.turnLeft() do
  264. sleep(1)
  265. end
  266.  
  267. if RotatePosition == "FORWARD" then
  268. RotatePosition = "LEFT"
  269. elseif RotatePosition == "LEFT" then
  270. RotatePosition = "BACKWARD"
  271. elseif RotatePosition == "BACKWARD" then
  272. RotatePosition = "RIGHT"
  273. elseif RotatePosition == "RIGHT" then
  274. RotatePosition = "FORWARD"
  275. end
  276. filewriteline("BranchMineData", 5, RotatePosition)
  277. return true
  278. end
  279.  
  280.  
  281. -- RotateTo is a number where 0 = Forward, 1 = Left, 2 = Backward, and 3 = Right
  282. local function look(RotateTo)
  283. --This rids one odd occurance
  284. if RotatePosition == "RIGHT" and RotateTo == 0 then
  285. rot.left()
  286. return
  287. end
  288. --
  289.  
  290. rotateNumber = 0
  291. if RotatePosition == "LEFT" then
  292. rotateNumber = 1
  293. elseif RotatePosition == "BACKWARD" then
  294. rotateNumber = 2
  295. elseif RotatePosition == "RIGHT" then
  296. rotateNumber = 3
  297. end
  298.  
  299. rotateRight = rotateNumber - RotateTo
  300. if rotateRight >= 0 then
  301. for i = 1,rotateRight do
  302. rot.right()
  303. end
  304. else
  305. for i = 1,math.abs(rotateRight) do
  306. rot.left()
  307. end
  308. end
  309.  
  310. end
  311.  
  312. dig.forward = function()
  313. if turtle.detect() then
  314. while not turtle.dig() do
  315. sleep(1)
  316. end
  317. return true
  318. else
  319. print("No Block to mine forward")
  320. return false
  321. end
  322. end
  323.  
  324. dig.up = function()
  325. if turtle.detectUp() then
  326. while not turtle.digUp() do
  327. sleep(1)
  328. end
  329. return true
  330. else
  331. print("No Block to mine up")
  332. return false
  333. end
  334. end
  335.  
  336. dig.down = function()
  337. if turtle.detectDown() then
  338. while not turtle.digDown() do
  339. sleep(1)
  340. end
  341. return true
  342. else
  343. print("No Block to mine down")
  344. return false
  345. end
  346. end
  347.  
  348. local function DigMoveForward()
  349. if mov.forward(1) == false then
  350. dig.forward()
  351. sleep(0.5)
  352. DigMoveForward()
  353. end
  354. end
  355.  
  356. local function DigMoveUp()
  357. if mov.up(1) == false then
  358. dig.up()
  359. sleep(0.5)
  360. DigMoveUp()
  361. end
  362. end
  363.  
  364. local function DigMoveDown()
  365. if mov.down(1) == false then
  366. dig.down()
  367. sleep(0.5)
  368. DigMoveDown()
  369. end
  370. end
  371.  
  372. local function DepositChest()
  373. turtle.select(1)
  374. if not turtle.compareUp() then
  375. print("ERROR: Should be below chest.")
  376. return
  377. end
  378.  
  379. print("Depositing the Load...")
  380. for i = IgnoreSlots + 1,16 do
  381. turtle.select(i)
  382. turtle.dropUp()
  383. end
  384. end
  385.  
  386. local function ReturnBack()
  387. print("ReturnBack")
  388. turtle.select(1)
  389. while not turtle.compareUp() do
  390. DigMoveUp()
  391. end
  392. end
  393.  
  394. local function Finish()
  395. print("Finishing!")
  396. ReturnBack()
  397. DepositChest()
  398. DigMoveForward()
  399. DigMoveForward()
  400. DigMoveForward()
  401. DigMoveForward()
  402. print("Finished!")
  403. end
  404.  
  405. local function InventoryFull()
  406. SlotsFull = 0
  407. for i=1,16 do
  408. if turtle.getItemCount(i) > 0 then
  409. SlotsFull = SlotsFull + 1
  410. end
  411. end
  412.  
  413. if SlotsFull == 16 then
  414. return true;
  415. else
  416. return false;
  417. end
  418. end
  419.  
  420. function StartMining()
  421.  
  422. BlocksTillEnd = TunnelLength - PositionInBranchMine
  423.  
  424. while BlocksTillEnd > 0 do
  425. if Refuel() == false then
  426. print("***Out of fuel***")
  427. ReturnBack()
  428. while Refuel() do
  429.  
  430. end
  431. for i=1,PositionInBranchMine do
  432. DigMoveDown()
  433. end
  434. end
  435.  
  436. if InventoryFull() then
  437. print("***Inventory Full***")
  438. ReturnBack()
  439. DepositChest()
  440. for i=1,PositionInBranchMine do
  441. DigMoveDown()
  442. end
  443. end
  444.  
  445. dig.forward()
  446.  
  447. for i=1,4 do
  448. DigMoveDown()
  449. PositionInBranchMine = PositionInBranchMine + 1
  450. BlocksTillEnd = TunnelLength - PositionInBranchMine
  451. end
  452.  
  453. print("Depth: ")
  454. print(PositionInBranchMine)
  455. end
  456. Finish()
  457. end
  458.  
  459. local FuelCount = turtle.getItemCount(fuelIndex)
  460.  
  461. if FuelCount == 0 then
  462. print("please put fuel in the last slot of the Turtle.")
  463. return
  464. else
  465. if FuelCount < 10 then
  466. print("Please put at least 10 of the fuel you are using in the Turtle.")
  467. return
  468. end
  469. end
  470.  
  471. Refuel()
  472. turtle.select(1)
  473. turtle.placeUp()
  474. StartMining()
Advertisement
Add Comment
Please, Sign In to add comment