Advertisement
Hydro1231

Untitled

Nov 14th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1.  
  2. local args = {...}
  3.  
  4. -- Config to determine what the turtle should mine
  5.  
  6. -- Function calls declared early. We need this as they are inter dependant
  7. local digVeinUp = nil
  8. local digVein = nil
  9. local digVeinDown = nil
  10. local mineVein = nil
  11.  
  12. local throwaways = {
  13. "stone",
  14. "dirt",
  15. "gravel"
  16. }
  17.  
  18. local config = {
  19. ["diamond"] = true,
  20. ["iron ore"] = true,
  21. ["misc"] = false,
  22. }
  23.  
  24. local oppositeInstructions = {
  25. ["f"] = "b",
  26. ["b"] = "f",
  27. ["l"] = "r",
  28. ["r"] = "l",
  29. ["u"] = "d",
  30. ["d"] = "u"
  31. }
  32.  
  33. local minFuelLevel = 200
  34. local torchSlot = 1
  35. local fuelSlot = 2
  36. local chestSlot = 3
  37.  
  38. local logSteps = false
  39. local backtrack = {}
  40.  
  41. local function initialize()
  42. print("Starting turtle...")
  43. print("Torches set to slot "..torchSlot)
  44. print("Fuel set to slot "..fuelSlot)
  45. end
  46.  
  47. local function shouldDrop(name)
  48. for k,v in pairs(throwaways) do
  49. if string.find(v, name) then
  50. return true
  51. end
  52. end
  53.  
  54. return false
  55. end
  56.  
  57. local function dropShit()
  58. for i = 1, 16 do
  59. turtle.select(i)
  60. local slotData = turtle.getItemDetail(i)
  61.  
  62. if slotData and shouldDrop(slotData.name) then
  63. turtle.drop()
  64. end
  65. end
  66. end
  67.  
  68. local function log(instruction)
  69. if not logSteps then
  70. return
  71. end
  72.  
  73. local instructionCount = #backtrack
  74. local lastInstruction = backtrack[instructionCount]
  75.  
  76. if (instruction == "f" and lastInstruction == "b") or
  77. (instruction == "b" and lastInstruction == "f") or
  78. (instruction == "l" and lastInstruction == "r") or
  79. (instruction == "r" and lastInstruction == "l") or
  80. (instruction == "u" and lastInstruction == "d") or
  81. (instruction == "d" and lastInstruction == "u") then
  82.  
  83. table.remove(lastInstruction, instructionCount)
  84. else
  85. table.insert(backtrack, instruction)
  86. end
  87. end
  88.  
  89. local function isOre(name)
  90. return string.find(name, "_ore")
  91. end
  92.  
  93. local function digUp()
  94.  
  95. while turtle.detectUp() do
  96. turtle.digUp()
  97. end
  98. end
  99.  
  100. local function digDown()
  101.  
  102. while turtle.detectDown() do
  103. turtle.digDown()
  104. end
  105. end
  106.  
  107. local function dig()
  108.  
  109. while turtle.detect() do
  110. turtle.dig()
  111. end
  112. end
  113.  
  114. local function forward()
  115. while not turtle.forward() do
  116. dig()
  117. end
  118.  
  119. log("f")
  120. end
  121.  
  122. local function up()
  123. while not turtle.up() do
  124. digUp()
  125. end
  126.  
  127. log("u")
  128. end
  129.  
  130. local function down()
  131. while not turtle.down() do
  132. digDown()
  133. end
  134.  
  135. log("d")
  136. end
  137.  
  138. local function back()
  139. turtle.back()
  140. log("b")
  141. end
  142.  
  143. local function executeInstruction(instruction)
  144.  
  145. if instruction == "f" then forward()
  146. elseif instruction == "b" then back()
  147. elseif instruction == "u" then up()
  148. elseif instruction == "d" then down()
  149. elseif instruction == "l" then turnLeft()
  150. elseif instruction == "r" then turnRight()
  151. end
  152. end
  153.  
  154. local function doBacktrack()
  155. local isLogging = logSteps
  156. logSteps = false
  157.  
  158. local currentInstruction = table.remove(backtrack)
  159.  
  160. while currentInstruction do
  161. executeInstruction(oppositeInstructions[currentInstruction])
  162. currentInstruction = table.remove(backtrack)
  163. end
  164.  
  165. logSteps = isLogging
  166. end
  167.  
  168. local function mineVein()
  169.  
  170. local backtrackCache = {}
  171. for k,v in pairs(backtrack) do
  172. table.insert(backtrackCache, v)
  173. end
  174.  
  175. backtrack = {}
  176.  
  177. -- Store the number of things in the backtrack list so
  178. -- we can come back to the turtle's position later
  179. local backtrackListLength = #backtrack
  180.  
  181. for i = 1, 4 do
  182. turtle.turnRight()
  183.  
  184. local success, infront = turtle.inspect()
  185. if success and isOre(infront.name) then
  186. digVein()
  187. doBacktrack()
  188. end
  189. end
  190.  
  191. local success, up = turtle.inspectUp()
  192. if success and isOre(up.name) then
  193. digVeinUp()
  194. doBacktrack()
  195. end
  196.  
  197. local success, down = turtle.inspectDown()
  198. if success and isOre(down.name) then
  199. digVeinDown()
  200. doBacktrack()
  201. end
  202.  
  203. backtrack = backtrackCache
  204. end
  205.  
  206. digVeinUp = function(oreOnly)
  207.  
  208. oreOnly = oreOnly or false
  209.  
  210. local isRoot = not logSteps
  211. local success, inspect = turtle.inspectUp()
  212.  
  213. if not success then
  214. return
  215. end
  216.  
  217. -- If it's not an ore then just mine it anyway and continue
  218. if not isOre(inspect.name) then
  219.  
  220. if not oreOnly then
  221. digUp()
  222. end
  223.  
  224. return
  225. end
  226.  
  227. logSteps = true
  228. up()
  229. mineVein()
  230.  
  231. if isRoot then
  232. logSteps = false
  233. down()
  234. backtrack = {}
  235. end
  236. end
  237.  
  238. digVein = function(oreOnly)
  239.  
  240. oreOnly = oreOnly or false
  241.  
  242. local isRoot = not logSteps
  243. local success, inspect = turtle.inspect()
  244.  
  245. if not success then
  246. return
  247. end
  248.  
  249. if not isOre(inspect.name) then
  250.  
  251. if not oreOnly then
  252. dig()
  253. end
  254.  
  255. return
  256. end
  257.  
  258. logSteps = true
  259. forward()
  260. mineVein()
  261.  
  262. if isRoot then
  263. logSteps = false
  264. back()
  265. backtrack = {}
  266. end
  267. end
  268.  
  269. digVeinDown = function(oreOnly)
  270.  
  271. oreOnly = oreOnly or false
  272.  
  273. local isRoot = not logSteps
  274. local success, inspect = turtle.inspectDown()
  275.  
  276. if not success then
  277. return
  278. end
  279.  
  280. if not isOre(inspect.name) then
  281.  
  282. if not oreOnly then
  283. digDown()
  284. end
  285.  
  286. return
  287. end
  288.  
  289. logSteps = true
  290. down()
  291. mineVein()
  292.  
  293. if isRoot then
  294. logSteps = false
  295. up()
  296. backtrack = {}
  297. end
  298. end
  299.  
  300. local function aboutFace()
  301. turtle.turnLeft()
  302. turtle.turnLeft()
  303.  
  304. log("l")
  305. log("l")
  306. end
  307.  
  308. local function left()
  309. turtle.turnLeft()
  310. log("l")
  311. end
  312.  
  313. local function right()
  314. turtle.turnRight()
  315. log("r")
  316. end
  317.  
  318. local function checkFuelLevel()
  319.  
  320. local fuelLevel = turtle.getFuelLevel()
  321.  
  322. if fuelLevel < minFuelLevel then
  323. turtle.select(fuelSlot)
  324. turtle.refuel(1)
  325. print("Refuelling... fuel level is now "..turtle.getFuelLevel())
  326. end
  327. end
  328.  
  329. local function placeChest()
  330. -- Place a chest behind the turtle
  331. turtle.select(chestSlot)
  332.  
  333. up()
  334. digVein()
  335.  
  336. down()
  337. digVein()
  338.  
  339. while not turtle.place() do turtle.dig() end
  340.  
  341. for i = 4, 16 do
  342. turtle.select(i)
  343. turtle.drop()
  344. end
  345. end
  346.  
  347. local function mineAboutMine()
  348. digVein()
  349. left()
  350. left()
  351. digVein()
  352. end
  353.  
  354. local function doStripMine()
  355.  
  356. local stripLength = 80
  357.  
  358. checkFuelLevel()
  359.  
  360. ------------------------------------
  361. -- STARTING FROM MIDDLE, GO UP THEN MINE DOWN
  362. ------------------------------------
  363. up()
  364.  
  365. local mf = 0
  366.  
  367. for i=1, stripLength / 2 do
  368.  
  369. if i % 2 == 0 then
  370. dropShit()
  371. end
  372.  
  373. checkFuelLevel()
  374.  
  375. -- Mine top layer, finish looking right
  376.  
  377. turtle.turnLeft()
  378. digVein()
  379. aboutFace()
  380. digVein()
  381.  
  382. -- Scan above for ore
  383. digVeinUp(true)
  384.  
  385. if (i % 6) == 0 then
  386. turtle.select(torchSlot)
  387. turtle.place()
  388. end
  389.  
  390. -- Mine middle layer, finish looking left
  391. down()
  392. digVein()
  393. aboutFace()
  394. dig()
  395.  
  396. -- Mine bottom layer, finish looking right
  397. down()
  398. digVein()
  399. aboutFace()
  400. digVein()
  401.  
  402. -- Scan below for ore
  403. digVeinDown(true)
  404.  
  405. -- Go forwards to start mining the next layer
  406. turtle.turnLeft()
  407. forward()
  408. mf = mf + 1
  409.  
  410. ------------------------------------
  411. -- STARTING FROM BOTTOM, MINE UP TO THE TOP
  412. ------------------------------------
  413.  
  414. -- Mine bottom layer, finish looking right
  415. turtle.turnLeft()
  416. digVein()
  417. aboutFace()
  418. digVein()
  419.  
  420. -- Scan below for ore
  421. digVeinDown(true)
  422.  
  423. -- Mine middle layer, finish looking left
  424. up()
  425. digVein()
  426. aboutFace()
  427. digVein()
  428.  
  429. -- Mine top layer, finish looking forward
  430. up()
  431. digVein()
  432. aboutFace()
  433. digVein()
  434. turtle.turnLeft()
  435.  
  436. -- Scan above for ore
  437. digVeinUp(true)
  438.  
  439. forward()
  440.  
  441.  
  442. mf = mf + 1
  443. end
  444.  
  445. down()
  446. turtle.turnLeft()
  447. turtle.turnLeft()
  448.  
  449. for i=1, mf do
  450. forward()
  451. end
  452.  
  453. placeChest()
  454. end
  455.  
  456.  
  457. for i=1, 10 do
  458. doStripMine()
  459.  
  460. turtle.turnLeft()
  461.  
  462. for j=1, 5 do
  463. forward()
  464. digUp()
  465. digDown()
  466. end
  467.  
  468. turtle.turnLeft()
  469. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement