Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 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 log(instruction)
  46. if not logSteps then
  47. return
  48. end
  49.  
  50. local instructionCount = #backtrack
  51. local lastInstruction = backtrack[instructionCount]
  52.  
  53. if (instruction == "f" and lastInstruction == "b") or
  54. (instruction == "b" and lastInstruction == "f") or
  55. (instruction == "l" and lastInstruction == "r") or
  56. (instruction == "r" and lastInstruction == "l") or
  57. (instruction == "u" and lastInstruction == "d") or
  58. (instruction == "d" and lastInstruction == "u") then
  59.  
  60. table.remove(lastInstruction, instructionCount)
  61. else
  62. table.insert(backtrack, instruction)
  63. end
  64. end
  65.  
  66. local function isOre(name)
  67. return string.find(name, "_ore")
  68. end
  69.  
  70. local function digUp()
  71.  
  72. while turtle.detectUp() do
  73. turtle.digUp()
  74. end
  75. end
  76.  
  77. local function digDown()
  78.  
  79. while turtle.detectDown() do
  80. turtle.digDown()
  81. end
  82. end
  83.  
  84. local function dig()
  85.  
  86. while turtle.detect() do
  87. turtle.dig()
  88. end
  89. end
  90.  
  91. local function forward()
  92. while not turtle.forward() do
  93. dig()
  94. end
  95.  
  96. log("f")
  97. end
  98.  
  99. local function up()
  100. while not turtle.up() do
  101. digUp()
  102. end
  103.  
  104. log("u")
  105. end
  106.  
  107. local function down()
  108. while not turtle.down() do
  109. digDown()
  110. end
  111.  
  112. log("d")
  113. end
  114.  
  115. digVeinUp = function()
  116.  
  117. local isRoot = not logSteps
  118. local inspect = turtle.inspectUp()
  119.  
  120. if not inspect then
  121. return
  122. end
  123.  
  124. -- If it's not an ore then just mine it anyway and continue
  125. if not isOre(inspect.name) then
  126. digUp()
  127. return
  128. end
  129.  
  130. logSteps = true
  131. up()
  132. mineVein()
  133.  
  134. if isRoot then
  135. logSteps = false
  136. end
  137. end
  138.  
  139. digVein = function()
  140.  
  141. local isRoot = not logSteps
  142. local inspect = turtle.inspect()
  143.  
  144. if not inspect then
  145. return
  146. end
  147.  
  148. if not isOre(inspect.name) then
  149. dig()
  150. return
  151. end
  152.  
  153. logSteps = true
  154. forward()
  155. mineVein()
  156.  
  157. if isRoot then
  158. logSteps = false
  159. end
  160. end
  161.  
  162. digVeinDown = function()
  163.  
  164. local isRoot = not logSteps
  165. local inspect = turtle.inspectDown()
  166.  
  167. if not inspect then
  168. return
  169. end
  170.  
  171. if not isOre(inspect.name) then
  172. dig()
  173. return
  174. end
  175.  
  176. logSteps = true
  177. down()
  178. mineVein()
  179.  
  180. if isRoot then
  181. logSteps = false
  182. end
  183. end
  184.  
  185. local function aboutFace()
  186. turtle.turnLeft()
  187. turtle.turnLeft()
  188.  
  189. log("l")
  190. log("l")
  191. end
  192.  
  193. local function left()
  194. turtle.turnLeft()
  195. log("l")
  196. end
  197.  
  198. local function right()
  199. turtle.turnRight()
  200. log("r")
  201. end
  202.  
  203. local function executeInstruction(instruction)
  204.  
  205. if instruction == "f" then forward()
  206. elseif instruction == "b" then back()
  207. elseif instruction == "u" then up()
  208. elseif instruction == "d" then down()
  209. elseif instruction == "l" then turnLeft()
  210. elseif instruction == "r" then turnRight()
  211. end
  212. end
  213.  
  214. local function doBacktrack()
  215.  
  216. local currentInstruction = table.remove(backtrack)
  217.  
  218. while currentInstruction do
  219. executeInstruction(oppositeInstructions[currentInstruction])
  220. currentInstruction = table.remove(backtrack)
  221. end
  222. end
  223.  
  224. local function mineVein()
  225.  
  226. local backtrackCache = {}
  227. for k,v in pairs(backtrack) do
  228. table.insert(backtrackCache, v)
  229. end
  230.  
  231. backtrack = {}
  232.  
  233. -- Store the number of things in the backtrack list so
  234. -- we can come back to the turtle's position later
  235. local backtrackListLength = #backtrack
  236.  
  237. for i = 1, 4 do
  238. turtle.turnRight()
  239.  
  240. local infront = turtle.inspect()
  241. if infront and isOre(infront.name) then
  242. digVein()
  243. doBacktrack()
  244. end
  245. end
  246.  
  247. local up = turtle.inspectUp()
  248. if up and isOre(up.name) then
  249. digVeinUp()
  250. doBacktrack()
  251. end
  252.  
  253. local down = turtle.inspectDown()
  254. if down and isOre(down.name) then
  255. digVeinDown()
  256. doBacktrack()
  257. end
  258.  
  259. backtrack = backtrackCache
  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