Blazuno

veinmine v2

Dec 21st, 2024 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.98 KB | None | 0 0
  1. -- things not to veinmine
  2. local blacklist = {
  3.     "minecraft:stone",
  4.     "minecraft:dirt",
  5.     "minecraft:diorite",
  6.     "minecraft:andesite",
  7.     "minecraft:granite",
  8.     "minecraft:gravel",
  9.     "quark:slate",
  10.     "minecraft:grass",
  11.     "chisel:limestone",
  12.     "chisel:limestone2",
  13.     "minecraft:lava",
  14.     "minecraft:water",
  15.     "minecraft:flowing_water",
  16.     "minecraft:flowing_lava",
  17.     "minecraft:clay",
  18.     "chisel:marble",
  19.     "chisel:marble2",
  20.     "chisel:basalt",
  21.     "chisel:basalt2",
  22.     "quark:biome_cobblestone"
  23.  
  24. }
  25.  
  26. -- vars to identify the current pos relative to start point
  27. local x = 0
  28. local y = 0
  29. local z = 0
  30. local rotation = 0
  31.  
  32. -- params
  33. local x_size, y_size, depth, increment = ...
  34. x_size, y_size, depth, increment = tonumber(x_size), tonumber(y_size) - 1, tonumber(depth), tonumber(increment)
  35.  
  36. --checking ore
  37. local function check_ore(direction)
  38.     if direction == "up" then
  39.         _, j = turtle.inspectUp()
  40.         if not _ then return false end
  41.         for i,v in pairs(blacklist) do
  42.             if j.name == v then
  43.                 return false
  44.             end
  45.         end
  46.         print(j.name)
  47.         return true
  48.     elseif direction == "down" then
  49.         _, j = turtle.inspectDown()
  50.         if not _ then return false end
  51.         for i,v in pairs(blacklist) do
  52.             if _ and j.name == v then
  53.                 return false
  54.             end
  55.         end
  56.         print(j.name)
  57.         return true
  58.     elseif direction == "forward" then
  59.         _, j = turtle.inspect()
  60.         if not _ then return false end
  61.         for i,v in pairs(blacklist) do
  62.             if _ and j.name == v then
  63.                 return false
  64.             end
  65.         end
  66.         print(j.name)
  67.         return true
  68.     end
  69. end
  70.  
  71. --this is mostly for gravel/confirming if ore
  72. local function better_forward()
  73.     while not turtle.forward() do
  74.         turtle.dig()
  75.         turtle.attack()
  76.     end
  77.     if check_ore("forward") then return "forward" end
  78.     if check_ore("up") then return "up" end
  79.     if check_ore("down") then return "down" end
  80. end
  81.  
  82.  
  83.  
  84. --veinmine
  85. local function veinmine(direction)
  86.     local rot, positions, tx,ty,tz
  87.     -- initializing vars and kickstarting based on where ore was found
  88.     if direction == "forward" then
  89.         while not turtle.forward() do
  90.             turtle.dig()
  91.             turtle.attack()
  92.         end
  93.         rot = 0
  94.         tx, ty, tz = 0,0,1
  95.         positions = {0}
  96.     elseif direction == "up" then
  97.         while not turtle.up() do
  98.             turtle.digUp()
  99.             turtle.attackUp()
  100.         end
  101.         rot = 0
  102.         tx, ty, tz = 0,1,0
  103.         positions = {"down"}
  104.     elseif direction == "down" then
  105.         while not turtle.down() do
  106.             turtle.digDown()
  107.             turtle.attackDown()
  108.         end
  109.         rot = 0
  110.         tx, ty, tz = 0,-1,0
  111.         positions = {"up"}
  112.     end    
  113.     local shortcuts = 0
  114.     local skips = 0
  115.     local mined = {}
  116.  
  117.     --checks if blocks around turtle are already cleared
  118.     local function check_around()
  119.         if mined[tostring(tx - 1) .. tostring(ty) .. tostring(tz)] and mined[tostring(tx + 1) .. tostring(ty) .. tostring(tz)] and mined[tostring(tx) .. tostring(ty) .. tostring(tz - 1)] and mined[tostring(tx) .. tostring(ty) .. tostring(tz + 1)] then
  120.             return true
  121.         end
  122.         return false
  123.     end
  124.  
  125.     local function check_left()
  126.         --checking if the block to left is checked
  127.         if rot % 4 == 0 and mined[tostring(tx-1) .. tostring(ty).. tostring(tz)] then
  128.             return false
  129.         elseif rot % 4 == 2 and mined[tostring(tx+1) .. tostring(ty).. tostring(tz)] then
  130.             return false
  131.         elseif rot % 4 == 1 and mined[tostring(tx) .. tostring(ty).. tostring(tz+1)] then
  132.             return false
  133.         elseif rot % 4 == 3 and mined[tostring(tx) .. tostring(ty).. tostring(tz-1)] then
  134.             return false
  135.         end
  136.  
  137.         --checking if the other blocks around are mined/checked
  138.         if rot % 4 == 0 and (not mined[tostring(tx) .. tostring(ty).. tostring(tz+1)] or not mined[tostring(tx+1) .. tostring(ty).. tostring(tz)] or mined[tostring(tx) .. tostring(ty).. tostring(tz-1)]) then
  139.             return false
  140.         elseif rot % 4 == 2 and (not mined[tostring(tx) .. tostring(ty).. tostring(tz - 1)] or not mined[tostring(tx - 1) .. tostring(ty).. tostring(tz)] or not mined[tostring(tx) .. tostring(ty).. tostring(tz + 1)]) then
  141.             return false
  142.         elseif rot % 4 == 1 and (not mined[tostring(tx+1) .. tostring(ty).. tostring(tz)] or not mined[tostring(tx) .. tostring(ty).. tostring(tz - 1)] or not mined[tostring(tx - 1) .. tostring(ty).. tostring(tz)]) then
  143.             return false
  144.         elseif rot % 4 == 3 and (not mined[tostring(tx - 1) .. tostring(ty).. tostring(tz)] or not mined[tostring(tx) .. tostring(ty).. tostring(tz + 1)] or not mined[tostring(tx + 1) .. tostring(ty).. tostring(tz)]) then
  145.             return false
  146.         end
  147.         return true
  148.     end
  149.  
  150.     --the veinmine part
  151.     while #positions ~= 0 do
  152.         print("Local position:", tx, ty, tz)
  153.         print("Local rotation:", rot)
  154.         local found_ore = false
  155.         if not check_ore("up") then
  156.             mined[tostring(tx) .. tostring(ty+1) .. tostring(tz)] = true
  157.         end
  158.         if not check_ore("down") then
  159.             mined[tostring(tx) .. tostring(ty - 1) .. tostring(tz)] = true
  160.         end
  161.         if check_ore("up") then
  162.             table.insert(positions, "down")
  163.             while not turtle.up() do
  164.                 turtle.digUp()
  165.                 turtle.attackUp()
  166.             end
  167.             found_ore = true
  168.             ty = ty + 1
  169.             mined[tostring(tx) .. tostring(ty) .. tostring(tz)] = true
  170.         elseif check_ore("down") then
  171.             table.insert(positions, "up")
  172.             while not turtle.down() do
  173.                 turtle.digDown()
  174.                 turtle.attackDown()
  175.             end
  176.             found_ore = true
  177.             ty = ty - 1
  178.             mined[tostring(tx) .. tostring(ty) .. tostring(tz)] = true
  179.         elseif not check_around() then
  180.             if check_left() then
  181.                 shortcuts = shortcuts + 1
  182.                 turtle.turnLeft()
  183.                 if check_ore("forward") then
  184.                     table.insert(positions, 3)
  185.                     rot = rot + 3
  186.                     while not turtle.forward() do
  187.                         turtle.dig()
  188.                         turtle.attack()
  189.                     end
  190.                     if rot % 4 == 1 then
  191.                         tx = tx + 1
  192.                     elseif rot % 4 == 2 then
  193.                         tz = tz - 1
  194.                     elseif rot % 4 == 3 then
  195.                         tx = tx - 1
  196.                     elseif rot % 4 == 0 then
  197.                         tz = tz + 1
  198.                     end
  199.                     found_ore = true
  200.                     mined[tostring(tx) .. tostring(ty) .. tostring(tz)] = true
  201.                 else
  202.                     local qx, qy, qz = tx, ty, tz
  203.                     if rot % 4 == 1 then
  204.                         qx = qx + 1
  205.                     elseif rot % 4 == 2 then
  206.                         qz = qz - 1
  207.                     elseif rot % 4 == 3 then
  208.                         qx = qx - 1
  209.                     elseif rot % 4 == 0 then
  210.                         qz = qz + 1
  211.                     end
  212.                     mined[tostring(qx) .. tostring(qy) .. tostring(qz)] = true
  213.                     turtle.turnRight()
  214.                 end
  215.             else
  216.                 for i = 1,4 do
  217.                     if check_ore("forward") then
  218.                         table.insert(positions, i - 1)
  219.                         while not turtle.forward() do
  220.                             turtle.dig()
  221.                             turtle.attack()
  222.                         end
  223.                         if rot % 4 == 1 then
  224.                             tx = tx + 1
  225.                         elseif rot % 4 == 2 then
  226.                             tz = tz - 1
  227.                         elseif rot % 4 == 3 then
  228.                             tx = tx - 1
  229.                         elseif rot % 4 == 0 then
  230.                             tz = tz + 1
  231.                         end
  232.                         found_ore = true
  233.                         mined[tostring(tx) .. tostring(ty) .. tostring(tz)] = true
  234.                         break
  235.                     else
  236.                         local qx, qy, qz = tx, ty, tz
  237.                         if rot % 4 == 1 then
  238.                             qx = qx + 1
  239.                         elseif rot % 4 == 2 then
  240.                             qz = qz - 1
  241.                         elseif rot % 4 == 3 then
  242.                             qx = qx - 1
  243.                         elseif rot % 4 == 0 then
  244.                             qz = qz + 1
  245.                         end
  246.                         mined[tostring(qx) .. tostring(qy) .. tostring(qz)] = true
  247.                     end
  248.                     turtle.turnRight()
  249.                     rot = rot + 1
  250.                 end
  251.             end
  252.         else
  253.             skips = skips + 1
  254.         end
  255.         if not found_ore then
  256.             if positions[#positions] == "up" then
  257.                 while not turtle.up() do
  258.                     turtle.digUp()
  259.                     turtle.attackUp()
  260.                 end
  261.                 ty = ty + 1
  262.             elseif positions[#positions] == "down" then
  263.                 while not turtle.down() do
  264.                     turtle.digDown()
  265.                     turtle.attackDown()
  266.                 end
  267.                 ty = ty - 1
  268.             else
  269.                 if not turtle.back() then
  270.                     turtle.turnRight()
  271.                     turtle.turnRight()
  272.                     while not turtle.forward() do
  273.                         turtle.dig()
  274.                         turtle.attack()
  275.                     end
  276.                     turtle.turnRight()
  277.                     turtle.turnRight()
  278.                 end
  279.                 if rot % 4 == 1 then
  280.                     tx = tx - 1
  281.                 elseif rot % 4 == 2 then
  282.                     tz = tz + 1
  283.                 elseif rot % 4 == 3 then
  284.                     tx = tx + 1
  285.                 elseif rot % 4 == 0 then
  286.                     tz = tz - 1
  287.                 end
  288.                 if positions[#positions] == 3 then
  289.                     turtle.turnRight()
  290.                     rot = rot - 3
  291.                 elseif positions[#positions] ~= 0 then
  292.                     for i = 1, positions[#positions] do
  293.                         turtle.turnLeft()
  294.                     end
  295.                     rot = rot - positions[#positions]
  296.                 end
  297.                
  298.                
  299.             end
  300.             table.remove(positions)
  301.         end  
  302.     end  
  303.     print("Shortcuts used:", shortcuts)
  304.     print("Skips used:", skips)
  305. end
  306.  
  307. -- function that returns to start point
  308. local function return_to_origin(up)
  309.     --switch for when something is blocking its way
  310.     local offset = false
  311.     -- rotating it to face the starting wall
  312.     if rotation ~= 2 then
  313.         if rotation > 2 then
  314.             repeat turtle.turnLeft() rotation = rotation - 1 until rotation == 2
  315.         elseif rotation < 2 then
  316.             repeat turtle.turnRight() rotation = rotation + 1 until rotation == 2
  317.         end
  318.     end
  319.     -- moving it out of the way if theres a block in front of it
  320.     if turtle.detect() and x ~= 0 then
  321.         offset = true
  322.         turtle.turnRight()
  323.         better_forward()
  324.         turtle.turnLeft()
  325.     end
  326.     -- returning to start
  327.     for i = 1,y do
  328.         better_forward()
  329.     end
  330.     turtle.turnRight()
  331.     if offset then
  332.         for i = 1,x-1 do
  333.             better_forward()
  334.         end
  335.     else
  336.         for i = 1,x do
  337.             better_forward()
  338.         end
  339.     end
  340.     --rotating back to 0
  341.     turtle.turnRight()
  342.     -- going back to the top if desired
  343.     if up then
  344.         for i = 1, z do
  345.             turtle.up()
  346.         end
  347.     end
  348. end
  349.  
  350. --emptying storage
  351. local function empty_storage(finished)
  352.     --checking if all slots have at least 1 item in them
  353.     if not finished then
  354.         for i = 1,16 do
  355.             if turtle.getItemCount(i) == 0 then
  356.                 return
  357.             end
  358.         end
  359.     end
  360.     local old_rotation = rotation
  361.     --returning to top
  362.     return_to_origin(true)
  363.     turtle.turnRight()
  364.     turtle.turnRight()
  365.     --emptying inv
  366.     for i = 1,16 do
  367.         turtle.select(i)
  368.         turtle.drop(64)
  369.     end
  370.     turtle.turnRight()
  371.     turtle.turnRight()
  372.     --going back down to where it was if need be
  373.     if not finished then
  374.         for i = 1, z do
  375.             turtle.down()
  376.         end
  377.         for i = 1, y do
  378.             better_forward()
  379.         end
  380.         turtle.turnRight()
  381.         for i = 1, x do
  382.             better_forward()
  383.         end
  384.         if old_rotation == 0 then
  385.             turtle.turnLeft()
  386.         elseif old_rotation == 2 then
  387.             turtle.turnRight()
  388.         end
  389.     end
  390. end
  391.  
  392.  
  393. local function mine_sector()
  394.     -- switch to alternate digging left/right
  395.     local left = false
  396.     for i = 1, x_size do
  397.         --digging forward
  398.         for i = 1, y_size do
  399.             local result = better_forward()
  400.             if result then
  401.                 if result == "forward" then
  402.                     veinmine("forward")
  403.                 elseif result == "up" then
  404.                     veinmine("up")
  405.                 elseif result == "down" then
  406.                     veinmine("down")
  407.                 end
  408.             end
  409.             if not left then y = y + 1
  410.             else y = y - 1 end
  411.             if y ~= y_size then
  412.                 empty_storage()
  413.             end
  414.         end
  415.         --turning using switch
  416.         if x ~= x_size - 1 then
  417.             if not left then
  418.                 turtle.turnRight()
  419.                 local result = better_forward()
  420.                 if result then
  421.                     if result == "forward" then
  422.                         veinmine("forward")
  423.                     elseif result == "up" then
  424.                         veinmine("up")
  425.                     elseif result == "down" then
  426.                         veinmine("down")
  427.                     end
  428.                 end
  429.                 turtle.turnRight()
  430.                 left = true
  431.                 rotation = 2
  432.                 x = x + 1
  433.             else
  434.                 turtle.turnLeft()
  435.                 local result = better_forward()
  436.                 if result then
  437.                     if result == "forward" then
  438.                         veinmine("forward")
  439.                     elseif result == "up" then
  440.                         veinmine("up")
  441.                     elseif result == "down" then
  442.                         veinmine("down")
  443.                     end
  444.                 end
  445.                 turtle.turnLeft()
  446.                 left = false
  447.                 rotation = 0
  448.                 x = x + 1
  449.             end
  450.         end
  451.     end
  452. end
  453.  
  454.  
  455.  
  456. local function dig_deeper()
  457.     return_to_origin(false)
  458.     for i = 1, increment do
  459.         if z == depth  then return end
  460.         z = z + 1
  461.         turtle.digDown()
  462.         turtle.down()
  463.     end
  464. end
  465.  
  466. local function smart_fuel()
  467.     local fuel_needed = (x_size * y_size) * math.floor(depth / increment) * 2
  468.     term.clear()
  469.     if turtle.getFuelLevel() <= fuel_needed then
  470.         while turtle.getFuelLevel() <= fuel_needed do
  471.             term.clear()
  472.             term.setCursorPos(1,1)
  473.             print("Fuel needed:", fuel_needed)
  474.             term.setCursorPos(1,2)
  475.             print("Current fuel:", turtle.getFuelLevel())
  476.             turtle.refuel()
  477.         end
  478.     end
  479.     term.clear()
  480.     term.setCursorPos(1,1)
  481.     print("Success, running quarry.")
  482. end
  483.  
  484.  
  485. local function quarry()
  486.     smart_fuel()
  487.     while true do
  488.         dig_deeper()
  489.         x,y,rotation = 0,0,0
  490.         if z >= depth then break end
  491.         mine_sector()
  492.     end
  493.     if depth % increment == 0 then
  494.         mine_sector()
  495.     end
  496.     empty_storage(true)
  497. end
  498.  
  499. quarry()
Add Comment
Please, Sign In to add comment