Advertisement
Blazuno

TurtleSwarmPORTABLE

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