Advertisement
Greyman27

nether miner

Dec 8th, 2024 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.53 KB | None | 0 0
  1. -- netherite and pyrite strip miner - gps required
  2. --changes needed
  3. --refuel funtion
  4.     --testing required
  5. --dig_home() while loops needs error detection
  6.  
  7. local strip_start_level = 15
  8. local lava_gather_level = 29
  9. local bedrock_level_start = 4
  10. local length_blocks = 50 -- blocks long
  11. local width_blocks = 50 -- blocks wide
  12. local height_blocks = strip_start_level -- blocks high
  13. local important_ores = {["minecraft:ancient_debris"]=true,["techreborn:pyrite_ore"]=true,["minecraft:nether_quartz_ore"]=true,["techreborn:cinnabar_ore"]=true,["techreborn:sphalerite_ore"]=true,["betternether:nether_redstone_ore"]=true,
  14.                         ["betternether:nether_ruby_ore"]=true,["betternether:cincinnasite_ore"]=true,["archon:fire_node"]=true,["minecraft:nether_gold_ore"]=true}
  15. local saved_blocks = {["minecraft:ancient_debris"]=true,["techreborn:yellow_garnet_gem"]=true,["techreborn:red_garnet_gem"]=true}
  16.  
  17.  
  18. os.loadAPI("grey_api/move.lua")
  19. os.loadAPI("grey_api/util.lua")
  20. local log_filename = "mine_nether.log"
  21. local log_file = io.open(log_filename, "w")
  22. if not log_file then print("log file: "..log_filename.." could not be opened") else io.output(log_file) end
  23. local length = length_blocks --strip length in blocks - forward
  24. local floor_spacing = 3
  25. local width = math.floor(((width_blocks+1)/3)+1) --number of strips- right
  26. local height = math.floor(((height_blocks+1)/floor_spacing)+1) -- number of levels of strips - down
  27. local current_width = width
  28. local current_height = height
  29. local minimum_fuel_level = 1000
  30. local maximum_fuel_level = 5000
  31. local face_forward = nil
  32. local face_backward = nil
  33. local face_right = nil
  34. local x_home = nil
  35. local y_home = nil
  36. local z_home = nil
  37. local facing_home = nil
  38. local max_depth = 25
  39. local function_blocks = {["minecraft:lava_bucket"]=true,["minecraft:bucket"]=true}
  40. local quit = false
  41.  
  42. function quit_program()
  43.     while not quit do
  44.         os.sleep(0.5)
  45.     end
  46. end
  47.  
  48. --drop all items that aren't in saved_blocks or function_blocks
  49. function purge_inventory(dir,start,stop)
  50.     dir = dir or "down"
  51.     start = start or 1
  52.     stop = stop or 16
  53.     local item = nil
  54.    
  55.     --place lava
  56.     if dir=="down" then
  57.         turtle.digDown()
  58.     elseif dir=="up" then
  59.         turtle.digUp()
  60.     else
  61.         turtle.dig()
  62.     end
  63.     while not util.select_item("minecraft:lava_bucket") do io.write("purge_inventory() can't find lava bucket. Going to get some lava\n") refuel(true) end
  64.     turtle.placeDown()
  65.    
  66.     --drop items
  67.     for i=start,stop do
  68.         item = turtle.getItemDetail(i)
  69.         if item~=nil and not (saved_blocks[item["name"]] or function_blocks[item["name"]]) then
  70.             turtle.select(i)
  71.             if dir=="down" then
  72.                 turtle.dropDown()
  73.             elseif dir=="up" then
  74.                 turtle.dropUp()
  75.             else
  76.                 turtle.drop()
  77.             end
  78.         end
  79.     end
  80.    
  81.     --pcik up lava
  82.     util.select_item("minecraft:bucket")
  83.     if dir=="down" then
  84.         turtle.placeDown()
  85.     elseif dir=="up" then
  86.         turtle.placeUp()
  87.     else
  88.         turtle.place()
  89.     end
  90.    
  91.     -- if still full return false
  92.     if util.is_full() then
  93.         return false
  94.     end
  95.     return true
  96. end
  97.  
  98. --tests block in direction dir.
  99. --if block["name"] is in important_ores
  100. function test(dir, dig)
  101.     dir = dir or "forward" --can be forward, up, down, left or right
  102.     if dig==nil then dig=true end
  103.     local b,block = nil
  104.     local rtn = true
  105.    
  106.     if dir=="up" then
  107.         b,block = turtle.inspectUp()
  108.     elseif dir=="down" then
  109.         b,block = turtle.inspectDown()
  110.     elseif dir=="left" or dir=="right" then
  111.         move.turn(dir)
  112.         b,block = turtle.inspect()
  113.     else
  114.         b,block = turtle.inspect()
  115.     end
  116.    
  117.     if b and important_ores[block["name"]] then
  118.         --print("Found Vein of "..block["name"])
  119.         rtn = dig_vein(block["name"])
  120.     elseif dig then
  121.         if dir=="up" then
  122.             rtn = turtle.digUp()
  123.         elseif dir=="down" then
  124.             rtn = turtle.digDown()
  125.         else
  126.             rtn = turtle.dig()
  127.         end
  128.     end
  129.    
  130.     if dir=="left" then
  131.         move.turn("right")
  132.     elseif dir=="right" then
  133.         move.turn("left")
  134.     end
  135.    
  136.     if util.is_full() then
  137.         if not purge_inventory("down") then
  138.             print("inventory is full of goodies!")
  139.             dig_home()
  140.             quit = true
  141.             os.sleep(1)
  142.         end
  143.     end
  144.     return rtn
  145. end
  146.  
  147. --goes to 0,0,0,0 while testing
  148. function dig_home(go_x,go_y,go_z,go_face)
  149.     if go_x==nil then
  150.         go_x = true
  151.     end
  152.     if go_y==nil then
  153.         go_y = true
  154.     end
  155.     if go_z==nil then
  156.         go_z = true
  157.     end
  158.     if go_face==nil then
  159.         go_face = true
  160.     end
  161.    
  162.     local _x = move.get_x()
  163.     local _y = move.get_y()
  164.     local _z = move.get_z()
  165.    
  166.     --go x
  167.     if go_x then
  168.         if _x > x_home then
  169.             move.face(2)
  170.         else
  171.             move.face(0)
  172.         end
  173.         while move.get_x() ~= x_home do
  174.             test()
  175.             move.go("forward",1,true)
  176.             test("up",false)
  177.             test("down",false)
  178.         end
  179.     end
  180.     --go z
  181.     if go_z then
  182.         if _z > z_home then
  183.             move.face(3)
  184.         else
  185.             move.face(1)
  186.         end
  187.         while move.get_z() ~= z_home do
  188.             test()
  189.             move.go("forward",1,true)
  190.             test("up",false)
  191.             test("down",false)
  192.         end
  193.     end
  194.     --go y
  195.     if go_y then
  196.         while move.get_y() ~= y_home do
  197.             if _y>y_home then
  198.                 test("down")
  199.                 move.go("down",1,true)
  200.             else
  201.                 test("up")
  202.                 move.go("up",1,true)
  203.             end
  204.         end
  205.     end
  206.     --go face
  207.     if go_face then
  208.         move.face(facing_home)
  209.     end
  210. end
  211.  
  212. function dig_vein(block_name, depth)
  213.     depth = depth or 0
  214.     --print("digging vein at depth: "..depth)
  215.     if depth > max_depth then io.write("Warning: dig_vein() hit max depth at "..max_depth.." on "..block_name..".\n") return false end
  216.     local b,block = nil
  217.  
  218.     --check up
  219.     --print("check up")
  220.     b,block = turtle.inspectUp()
  221.     if b and block_name == block["name"] then
  222.         move.go("up",1,true)
  223.         dig_vein(block_name,depth+1)
  224.         move.go("down",1,true)
  225.     end
  226.     --check forward
  227.     --print("check forward")
  228.     b,block = turtle.inspect()
  229.     if b and block_name == block["name"] then
  230.         move.go("forward",1,true)
  231.         dig_vein(block_name,depth+1)
  232.         move.turn("right",2)
  233.         move.go("forward",1,true)
  234.         move.turn("right",2)
  235.     end
  236.     --check left
  237.     --print("check left")
  238.     move.turn("left")
  239.     b,block = turtle.inspect()
  240.     if b and block_name == block["name"] then
  241.         move.go("forward",1,true)
  242.         dig_vein(block_name,depth+1)
  243.         move.turn("right",2)
  244.         move.go("forward",1,true)
  245.         move.turn("right",2)
  246.     end
  247.     --check back
  248.     --print("check back")
  249.     move.turn("left")
  250.     b,block = turtle.inspect()
  251.     if b and block_name == block["name"] then
  252.         move.go("forward",1,true)
  253.         dig_vein(block_name,depth+1)
  254.         move.turn("right",2)
  255.         move.go("forward",1,true)
  256.         move.turn("right",2)
  257.     end
  258.     --check right
  259.     --print("check right")
  260.     move.turn("left")
  261.     b,block = turtle.inspect()
  262.     if b and block_name == block["name"] then
  263.         move.go("forward",1,true)
  264.         dig_vein(block_name,depth+1)
  265.         move.turn("right",2)
  266.         move.go("forward",1,true)
  267.         move.turn("right",2)
  268.     end
  269.     --check down
  270.     --print("check down")
  271.     move.turn("left")
  272.     b,block = turtle.inspectDown()
  273.     if b and block_name == block["name"] then
  274.         move.go("down",1,true)
  275.         dig_vein(block_name,depth+1)
  276.         move.go("up",1,true)
  277.     end
  278.  
  279.     return true
  280. end
  281.  
  282. function refuel(override)
  283.     if override == nil then override = false end
  284.     if turtle.getFuelLevel() < minimum_fuel_level or override then
  285.         local b,block = nil
  286.         io.write("Refueling\n")
  287.         move.sync_coords_gps() --error prevention
  288.         --make sure we have an empty bucket and a lava bucket
  289.         if not (util.count_items("minecraft:bucket") > 0 or util.count_items("minecraft:lava_bucket") > 0) then
  290.             io.write("refueling function couldn't find buckets\n")
  291.             return false
  292.         end
  293.         --save location
  294.         local saved_x,saved_y,saved_z = gps.locate()
  295.         local saved_facing = move.get_facing()
  296.         --go up to lava level
  297.         move.goto(move.get_x(), lava_gather_level, move.get_z(), move.get_facing(), true)
  298.         --get lava from front back left right and up - use all of it
  299.         while turtle.getFuelLevel() < maximum_fuel_level or not util.select_item("minecraft:lava_bucket") do
  300.             --get lava from forward
  301.             b,block = turtle.inspect()
  302.             if b and block["name"] == "minecraft:lava" and block["state"]["level"]==0 then
  303.                 if util.select_item("minecraft:bucket") then turtle.place() end
  304.                 --use it
  305.                 if turtle.getFuelLevel() < maximum_fuel_level or util.count_items("minecraft:lava_bucket") > 1 then
  306.                     if util.select_item("minecraft:lava_bucket") then turtle.refuel() end
  307.                 end
  308.             end
  309.             --get lava from up
  310.             b,block = turtle.inspectUp()
  311.             if b and block["name"] == "minecraft:lava" and block["state"]["level"]==0 then
  312.                 if util.select_item("minecraft:bucket") then turtle.place() end
  313.                 --use it
  314.                 if turtle.getFuelLevel() < maximum_fuel_level or util.count_items("minecraft:lava_bucket") > 1 then
  315.                     if util.select_item("minecraft:lava_bucket") then turtle.refuel() end
  316.                 end
  317.             end
  318.             --get lava from down
  319.             b,block = turtle.inspectDown()
  320.             if b and block["name"] == "minecraft:lava" and block["state"]["level"]==0 then
  321.                 if util.select_item("minecraft:bucket") then turtle.place() end
  322.                 --use it
  323.                 if turtle.getFuelLevel() < maximum_fuel_level or util.count_items("minecraft:lava_bucket") > 1 then
  324.                     if util.select_item("minecraft:lava_bucket") then turtle.refuel() end
  325.                 end
  326.             end
  327.             --get lava from left
  328.             move.turn("left")
  329.             b,block = turtle.inspect()
  330.             if b and block["name"] == "minecraft:lava" and block["state"]["level"]==0 then
  331.                 if util.select_item("minecraft:bucket") then turtle.place() end
  332.                 --use it
  333.                 if turtle.getFuelLevel() < maximum_fuel_level or util.count_items("minecraft:lava_bucket") > 1 then
  334.                     if util.select_item("minecraft:lava_bucket") then turtle.refuel() end
  335.                 end
  336.             end
  337.             --get lava from back
  338.             move.turn("left")
  339.             b,block = turtle.inspect()
  340.             if b and block["name"] == "minecraft:lava" and block["state"]["level"]==0 then
  341.                 if util.select_item("minecraft:bucket") then turtle.place() end
  342.                 --use it
  343.                 if turtle.getFuelLevel() < maximum_fuel_level or util.count_items("minecraft:lava_bucket") > 1 then
  344.                     if util.select_item("minecraft:lava_bucket") then turtle.refuel() end
  345.                 end
  346.             end
  347.             --get lava from right
  348.             move.turn("left")
  349.             b,block = turtle.inspect()
  350.             if b and block["name"] == "minecraft:lava" and block["state"]["level"]==0 then
  351.                 if util.select_item("minecraft:bucket") then turtle.place() end
  352.                 --use it
  353.                 if turtle.getFuelLevel() < maximum_fuel_level or util.count_items("minecraft:lava_bucket") > 1 then
  354.                     if util.select_item("minecraft:lava_bucket") then turtle.refuel() end
  355.                 end
  356.             end
  357.             move.turn("left")--back to starting direction
  358.            
  359.             if turtle.getFuelLevel() < maximum_fuel_level  or not util.select_item("minecraft:lava_bucket") then
  360.                 move.go("forward",1,true)
  361.             end
  362.         end
  363.            
  364.         --go back to saved location
  365.         move.goto(saved_x,saved_y,saved_z,saved_facing,true)
  366.         --continue mining
  367.         return true
  368.     end
  369.     return true
  370. end
  371.  
  372. function mine_line()
  373.     if not refuel() then return false end
  374.     test()
  375.     move.go("forward",1,true)
  376.     test("down",false)
  377.     test("up",false)
  378.     test("left",false)
  379.     test("right",false)
  380.     return not util.is_full()
  381. end
  382.  
  383. function mine_turn()
  384.     if not refuel() then return false end
  385.     local old_facing = move.get_facing()
  386.     move.face(face_right)
  387.     for i=1,3 do
  388.         test()
  389.         move.go("forward",1,true)
  390.         test("up",false)
  391.         test("down",false)
  392.     end
  393.     if old_facing==face_forward then
  394.         move.face(face_backward)
  395.     elseif old_facing==face_backward then
  396.         move.face(face_forward)
  397.     end
  398.     return true
  399. end
  400.  
  401. function mine_down()
  402.     move.sync_coords_gps()
  403.     if move.get_y() - floor_spacing <= bedrock_level_start then
  404.         io.write("mine_down() ran into bedrock. Returning home\n")
  405.         dig_home()
  406.         quit = true
  407.         os.sleep(1)
  408.         return false
  409.     end
  410.     if not refuel() then return false end
  411.    
  412.     current_width = width
  413.    
  414.     dig_home(true,false,true,true)
  415.     for i=1,floor_spacing do
  416.         test("down")
  417.         move.go("down",1,true)
  418.     end
  419.     test("down",false)
  420.    
  421.     return true
  422. end
  423.  
  424. function mine()
  425.     --io.input(stdin)
  426.     refuel()
  427.     --mine to start height
  428.     io.write("Started at "..textutils.formatTime(os.time("local")).."\n")
  429.     io.write("Mine to starting level\n")
  430.     --io.read()
  431.     while move.get_y() > strip_start_level do
  432.         test("down")
  433.         move.go("down")
  434.     end
  435.     while move.get_y() < strip_start_level do
  436.         test("up")
  437.         move.go("up")
  438.     end
  439.     test("down", false)
  440.     test("up", false)
  441.  
  442.     io.write("Start strip mining\n")
  443.     io.write(current_height.." levels left\n")
  444.     --io.read()
  445.     while true do
  446.         for i=1,length-1 do
  447.             if not mine_line() then break end
  448.         end
  449.        
  450.         current_width = current_width-1
  451.         if current_width<=0 then
  452.             current_height = current_height-1
  453.             if current_height <= 0 then
  454.                 io.write("Done. Going home: ("..x_home..","..y_home..","..z_home..","..facing_home..")\n")
  455.                 dig_home()
  456.                 break
  457.             else
  458.                 mine_down()
  459.                 --print(current_height.." levels left")
  460.             end
  461.         else
  462.             mine_turn()
  463.         end
  464.     end
  465.     return true
  466. end
  467.  
  468. if length<=0 or width<=0 or height<=0 then io.write("invalid strip mine dimensions\n") return false end
  469. if turtle.getFuelLevel() < minimum_fuel_level and not util.fuel(1,{"minecraft:lava_bucket"}) then io.write("Out of fuel before starting\n") return false end
  470. if not (util.count_items("minecraft:bucket") > 0 or util.count_items("minecraft:lava_bucket") > 0) then
  471.     io.write("Can't find bucket\n")
  472.     return false
  473. end
  474. if not move.load_gps() then io.write("mine_nether couldn't load gps\n") return false end
  475. face_forward = move.get_facing()
  476. face_backward = (move.get_facing() + 2) % 4
  477. face_right = (move.get_facing() + 1) % 4
  478.  
  479. x_home = move.get_x()
  480. y_home = move.get_y()
  481. z_home = move.get_z()
  482. facing_home = move.get_facing()
  483. parallel.waitForAny(quit_program, mine)
  484. purge_inventory()
  485. io.write("Finished at "..textutils.formatTime(os.time("local")).."\n")
  486. io.close(log_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement