Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
99
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 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. local function mineVein()
  116.  
  117. local backtrackCache = {}
  118. for k,v in pairs(backtrack) do
  119. table.insert(backtrackCache, v)
  120. end
  121.  
  122. backtrack = {}
  123.  
  124. -- Store the number of things in the backtrack list so
  125. -- we can come back to the turtle's position later
  126. local backtrackListLength = #backtrack
  127.  
  128. for i = 1, 4 do
  129. turtle.turnRight()
  130.  
  131. local success, infront = turtle.inspect()
  132. if success and isOre(infront.name) then
  133. digVein()
  134. doBacktrack()
  135. end
  136. end
  137.  
  138. local success, up = turtle.inspectUp()
  139. if success and isOre(up.name) then
  140. digVeinUp()
  141. doBacktrack()
  142. end
  143.  
  144. local success, down = turtle.inspectDown()
  145. if success and isOre(down.name) then
  146. digVeinDown()
  147. doBacktrack()
  148. end
  149.  
  150. backtrack = backtrackCache
  151. end
  152.  
  153. digVeinUp = function()
  154.  
  155. local isRoot = not logSteps
  156. local success, inspect = turtle.inspectUp()
  157.  
  158. if not success then
  159. return
  160. end
  161.  
  162. -- If it's not an ore then just mine it anyway and continue
  163. if not isOre(inspect.name) then
  164. digUp()
  165. return
  166. end
  167.  
  168. logSteps = true
  169. up()
  170. mineVein()
  171.  
  172. if isRoot then
  173. logSteps = false
  174. end
  175. end
  176.  
  177. digVein = function()
  178.  
  179. local isRoot = not logSteps
  180. local success, inspect = turtle.inspect()
  181.  
  182. if not success then
  183. return
  184. end
  185.  
  186. if not isOre(inspect.name) then
  187. dig()
  188. return
  189. end
  190.  
  191. logSteps = true
  192. forward()
  193. mineVein()
  194.  
  195. if isRoot then
  196. logSteps = false
  197. end
  198. end
  199.  
  200. digVeinDown = function()
  201.  
  202. local isRoot = not logSteps
  203. local success, inspect = turtle.inspectDown()
  204.  
  205. if not success then
  206. return
  207. end
  208.  
  209. if not isOre(inspect.name) then
  210. dig()
  211. return
  212. end
  213.  
  214. logSteps = true
  215. down()
  216. mineVein()
  217.  
  218. if isRoot then
  219. logSteps = false
  220. end
  221. end
  222.  
  223. local function aboutFace()
  224. turtle.turnLeft()
  225. turtle.turnLeft()
  226.  
  227. log("l")
  228. log("l")
  229. end
  230.  
  231. local function left()
  232. turtle.turnLeft()
  233. log("l")
  234. end
  235.  
  236. local function right()
  237. turtle.turnRight()
  238. log("r")
  239. end
  240.  
  241. local function executeInstruction(instruction)
  242.  
  243. if instruction == "f" then forward()
  244. elseif instruction == "b" then back()
  245. elseif instruction == "u" then up()
  246. elseif instruction == "d" then down()
  247. elseif instruction == "l" then turnLeft()
  248. elseif instruction == "r" then turnRight()
  249. end
  250. end
  251.  
  252. local function doBacktrack()
  253.  
  254. local currentInstruction = table.remove(backtrack)
  255.  
  256. while currentInstruction do
  257. executeInstruction(oppositeInstructions[currentInstruction])
  258. currentInstruction = table.remove(backtrack)
  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