Advertisement
0xSRK6

mine

Feb 22nd, 2025 (edited)
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. TEST_MODE = true
  2. STRIP_SIZE = (TEST_MODE and 2 or 10) * 16 -- 10 chunks
  3.  
  4.  
  5.  
  6. MOVE_LOOKUP = {
  7.     turtle.up,
  8.     function()
  9.         turtle.turnRight()
  10.         turtle.forward()
  11.         turtle.turnLeft()
  12.     end,
  13.     turtle.forward,
  14.     turtle.back,
  15.     function()
  16.         turtle.turnLeft()
  17.         turtle.forward()
  18.         turtle.turnRight()
  19.     end,
  20.     turtle.down
  21. }
  22.  
  23. TURTLE_STATUS = {
  24.     mining = true,
  25.     current_ore = nil,
  26.     in_vein = false,
  27.     pos = {x = 0, y = 0, z = 0},
  28.    
  29. }
  30.  
  31. local function shouldMine(s)
  32.     return s ~= "minecraft:stone" and
  33.            s ~= "minecraft:dirt" and
  34.            s ~= "minecraft:wall_torch" and
  35.            s ~= "minecraft:torch" and
  36.            s ~= "minecraft:andesite" and
  37.            s ~= "minecraft:diorite" and
  38.            s ~= "minecraft:granite" and
  39.            s ~= "minecraft:gravel" and
  40.            s ~= "minecraft:deepslate" and
  41.            s ~= "promenade:asphalt" and
  42.            s ~= "twigs:rhyolite"
  43. end
  44.  
  45.  
  46. --[[ to grab block name
  47. local _, d = turtle.inspect()
  48. local name = d.name
  49. ]]
  50. local function moveDir(dir)
  51.     MOVE_LOOKUP[dir]()
  52. end
  53.  
  54. local function lookDir(dir, wasBack)
  55.     if dir == 2 then
  56.         turtle.turnRight()
  57.     elseif dir == 4 or wasBack then
  58.         turtle.turnRight()
  59.         turtle.turnRight()
  60.     elseif dir == 5 then
  61.         turtle.turnLeft()
  62.     end
  63. end
  64.  
  65. local function mineDir(dir)
  66.     if dir == 1 then
  67.         turtle.digUp()
  68.         moveDir(1)
  69.     elseif dir == 2 then
  70.         turtle.turnRight()
  71.         turtle.dig()
  72.         turtle.turnLeft()
  73.         moveDir(2)
  74.     elseif dir == 3 then
  75.         turtle.dig()
  76.         moveDir(3)
  77.     elseif dir == 4 then
  78.         turtle.turnRight()
  79.         turtle.turnRight()
  80.         turtle.dig()
  81.         turtle.turnLeft()
  82.         turtle.turnLeft()
  83.         moveDir(4)
  84.     elseif dir == 5 then
  85.         turtle.turnLeft()
  86.         turtle.dig()
  87.         turtle.turnRight()
  88.         moveDir(5)
  89.     else
  90.         turtle.digDown()
  91.         moveDir(6)
  92.     end
  93. end
  94.  
  95. -- {up, right, front, back, left, down}
  96. local function touchingOres()
  97.     local ores = {}
  98.     -- up
  99.     local p, d = turtle.inspectUp()
  100.     table.insert(ores, p and d.name or "")
  101.    
  102.     p, d = turtle.inspect()
  103.     table.insert(ores, 3, p and d.name or "")
  104.    
  105.     turtle.turnRight()
  106.     p, d = turtle.inspect()
  107.     table.insert(ores, p and d.name or "")
  108.    
  109.     turtle.turnRight()
  110.     p, d = turtle.inspect()
  111.     table.insert(ores, 4, p and d.name or "")
  112.    
  113.     turtle.turnRight()
  114.     p, d = turtle.inspect()
  115.     table.insert(ores, 5, p and d.name or "")
  116.  
  117.     turtle.turnRight()
  118.     p, d = turtle.inspectDown()
  119.     table.insert(ores, p and d.name or "")
  120.    
  121.     return ores
  122. end
  123.  
  124.  
  125.  
  126. local function vein()
  127.     local ores = touchingOres()
  128.     for i, ore in ipairs(ores) do
  129.         if shouldMine(ore) and ore ~= "" then
  130.             print("Mining: " .. ore)
  131.             mineDir(i)
  132.             vein()
  133.             moveDir(7 - i)
  134.         end
  135.     end    
  136. end
  137.  
  138.  
  139.  
  140.  
  141. local function returnToInit(dist)
  142.     turtle.turnRight()
  143.     turtle.turnRight()
  144.     print("rotated")
  145.     for _ = 1, dist do
  146.         turtle.forward()
  147.     end
  148.     print("rotated back")
  149.     turtle.turnRight()
  150.     turtle.turnRight()
  151. end
  152.  
  153. local function carveStrip()
  154.     for _ = 1, STRIP_SIZE do
  155.         turtle.dig()
  156.         turtle.forward()
  157.         turtle.digDown()
  158.     end
  159.     returnToInit(STRIP_SIZE)
  160.    
  161. end
  162.  
  163.  
  164.  
  165.  
  166.  
  167. local function isFull()
  168.     for i = 16, 1, -1 do
  169.         turtle.select(i)
  170.         if turtle.getItemCount() == 0 then return false end
  171.     end
  172.     return true
  173. end
  174.  
  175. local function checkToVein()
  176.     local ores = touchingOres()
  177.     for i, ore in ipairs(ores) do
  178.         if shouldMine(ore) and ore ~= "" then
  179.             lookDir(i)
  180.             vein()
  181.             if (i ~= 1) then lookDir(7 - i, i == 4) end
  182.         end
  183.        
  184.     end
  185. end
  186.  
  187. local function mineLoop()
  188.     carveStrip()
  189.     local loc = 0
  190.     while loc <= STRIP_SIZE do
  191.         if (loc % 10 == 0 and isFull()) then
  192.             print("breaking")
  193.             break
  194.         end
  195.         turtle.forward()
  196.         print("Checking for ore at loc = " .. loc)
  197.         checkToVein()
  198.         turtle.down()
  199.         checkToVein()
  200.         turtle.up()
  201.         print("Found all ore at loc = " .. loc .. ", moving...")
  202.  
  203.         loc = loc + 1
  204.     end
  205.     returnToInit(loc)
  206.    
  207. end
  208.  
  209.  
  210. mineLoop()
  211.  
  212. -- local function listen()
  213. --     while true do
  214. --         local _, msg = rednet.receive("minectrl")
  215. --         if msg == "stop" then
  216. --             break
  217. --         end
  218. --     end
  219. -- end
  220.  
  221. local function main()
  222.     -- parallel.waitForAny(main, listen)
  223. end
  224.  
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement