Advertisement
Guest User

Untitled

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