Advertisement
Blazuno

veinmine test

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