BruceWplays

Strip Miner chest fix

Jun 30th, 2022 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.10 KB | None | 0 0
  1. --[[
  2. Miner
  3. By Blueberrys
  4.  
  5. Requires BlueAPI
  6. http://pastebin.com/yy7gqfBQ
  7. ]]
  8.  
  9. --
  10. -- GitHub
  11. --
  12.  
  13. local user = "blueberrys"
  14. local repo = "cc-miner"
  15. local root = "blue-miner"
  16. local exclFiles = {
  17.     "README.md",
  18.     ".gitattributes",
  19.     ".gitignore",
  20.  
  21.     "pastebin",
  22. }
  23.  
  24. local blueApi = "blue-api/init"
  25.  
  26.  
  27. --
  28. -- Constants
  29. --
  30.  
  31. local alias = "Miner"
  32. local selfRun = fs.combine(root, "Miner")
  33.  
  34. local settings_file = fs.combine(root, "MinerSettings")
  35.  
  36. local t = turtle
  37.  
  38. local items = {
  39.     torch = "minecraft:torch",
  40.  
  41.     cobble = "minecraft:cobblestone",
  42.     sandstone = "minecraft:sandstone",
  43.  
  44.     chest = "quark:custom_chest",
  45.  
  46.     coal = "minecraft:coal",
  47.     blz_rod = "minecraft:blaze_rod",
  48.     lava = "minecraft:lava_bucket",
  49.  
  50.     bucket = "minecraft:bucket",
  51.     lava_block = "minecraft:flowing_lava",
  52. }
  53.  
  54.  
  55. --
  56. -- Settings
  57. --
  58.  
  59. local auto_update = true
  60. local aggressive = true
  61. local neat = true
  62. local safe = true
  63. local debug = false
  64.  
  65. local dim = {
  66.     main_shaft = {
  67.         w=2,
  68.     },
  69.     base_room = {
  70.         w=3,
  71.         l=3,
  72.     },
  73.     strips = {
  74.         l=20,
  75.         w=1,
  76.     },
  77. }
  78.  
  79. local fuel_items_whitelist = {
  80.     items.coal,
  81.     items.lava,
  82.     items.blz_rod,
  83. }
  84.  
  85.  
  86. --
  87. -- Variable values
  88. --
  89.  
  90. local dist_from_chest
  91. local dist_across_shaft
  92.  
  93. local strip_spacing
  94. local strip_total_w
  95.  
  96. local torch_spacing
  97. local torch_per_strip
  98.  
  99. local ave_speed
  100. local max_place_tries
  101. local wait_period
  102.  
  103. local function valid_sett(sett)
  104.     local function use_valid(opt, default)
  105.         if (opt~=nil) then return opt
  106.         else return default
  107.         end
  108.     end
  109.  
  110.     sett.auto_update = use_valid(sett.auto_update, auto_update)
  111.     sett.aggressive = use_valid(sett.aggressive, aggressive)
  112.     sett.neat = use_valid(sett.neat, neat)
  113.     sett.safe = use_valid(sett.safe, safe)
  114.     sett.debug = use_valid(sett.debug, debug)
  115.  
  116.     sett.strip_len = use_valid(sett.strip_len, dim.strips.l)
  117.     sett.strip_spacing = use_valid(sett.strip_spacing, strip_spacing)
  118.     sett.ave_speed = use_valid(sett.ave_speed, ave_speed)
  119.  
  120.     sett.fuel_items_whitelist = use_valid(sett.fuel_items_whitelist, fuel_items_whitelist)
  121. end
  122.  
  123. local function init(sett)
  124.     if (sett) then
  125.         valid_sett(sett)
  126.  
  127.         auto_update = sett.auto_update
  128.         aggressive = sett.aggressive
  129.         neat = sett.neat
  130.         safe = sett.safe
  131.         debug = sett.debug
  132.  
  133.         dim.strips.l = sett.strip_len
  134.         strip_spacing = sett.strip_spacing
  135.         ave_speed = sett.ave_speed
  136.  
  137.         fuel_items_whitelist = sett.fuel_items_whitelist
  138.     end
  139.  
  140.     dist_from_chest = dim.base_room.l-1
  141.     dist_across_shaft = dim.main_shaft.w-1
  142.  
  143.     strip_spacing = strip_spacing or 3
  144.     strip_total_w = strip_spacing + dim.strips.w
  145.  
  146.     torch_spacing = torch_spacing or 6
  147.     torch_spacing=torch_spacing+1
  148.     torch_per_strip = dim.strips.l/torch_spacing
  149.  
  150.     ave_speed = ave_speed or 5
  151.     max_place_tries = 10-ave_speed
  152.     wait_period = 1-(ave_speed/10)
  153. end
  154.  
  155. init()
  156.  
  157.  
  158. --
  159. -- Tables
  160. --
  161.  
  162. local save_items = {}
  163. local function add_save_item(item_fn, quantity, ui_name)
  164.     if (quantity < 1) then return end
  165.     local item = item_fn()
  166.     if item then
  167.         table.insert(save_items, {name=item, quantity=quantity})
  168.     elseif ui_name then
  169.         print("Don't have any ", ui_name)
  170.     end
  171. end
  172.  
  173. local get = {}
  174.  
  175. local tested_items = {}
  176. local slots_mem = {}
  177. local function reset_item_slots(set_saves, torches)
  178.     print("Scanning slots..")
  179.  
  180.     tested_items = {}
  181.     slots_mem = {}
  182.  
  183.     if set_saves then
  184.         if not torches then torches = 64 end
  185.         add_save_item(get.torch, torches, "torches")
  186.         add_save_item(get.cobble, 64*(neat and 2 or 1), "cobblestone")
  187.         add_save_item(get.fuel, 64, "fueling items")
  188.         add_save_item(get.chest, 2, "chests")
  189.         add_save_item(get.bucket, 3, "bucket")
  190.     end
  191. end
  192.  
  193.  
  194. --
  195. -- I/O
  196. --
  197.  
  198. local function log(...)
  199.     if debug then
  200.         print(...)
  201.     end
  202. end
  203.  
  204. local function print_ln(...)
  205.     for i=1, #arg, 1 do
  206.         print(arg[i])
  207.     end
  208. end
  209.  
  210. local function clear()
  211.     for i=1, 12, 1 do
  212.         print()
  213.     end
  214.  
  215.     print_ln("Miner - By Blueberrys", "")
  216. end
  217.  
  218. local function input_bool()
  219.     print("(y/n)")
  220.     local answer = io.read():sub(1,1)
  221.     return (answer=="y" or answer=="Y")
  222. end
  223.  
  224. local function input_num()
  225.     print("Number:")
  226.     return tonumber(io.read()) or 0
  227. end
  228.  
  229. local function wait_enter(msg)
  230.     if not msg then msg = "continue" end
  231.     print_ln("", "Press enter to " .. msg)
  232.     io.read()
  233. end
  234.  
  235. local function bool_str(bool)
  236.     return (bool and "Yes" or "No")
  237. end
  238.  
  239.  
  240. --
  241. -- File management
  242. --
  243.  
  244. local function save_settings(settings)
  245.     if fs.exists(settings_file) then
  246.         fs.delete(settings_file)
  247.     end
  248.  
  249.     local h = fs.open(settings_file, "w")
  250.     h.write(textutils.serialize(settings))
  251.     h.close()
  252. end
  253.  
  254. local function load_settings()
  255.     if fs.exists(settings_file) then
  256.         local h = fs.open(settings_file, "r")
  257.         local str = h.readAll()
  258.         h.close()
  259.  
  260.         local sett = textutils.unserialize(str)
  261.         clear()
  262.         print_ln("Load previous settings?")
  263.         if (input_bool()) then
  264.             init(sett)
  265.         else
  266.             print_ln("", "Delete previous settings?")
  267.             if (input_bool()) then
  268.                 fs.delete(settings_file)
  269.             end
  270.         end -- ask load
  271.     end -- file exists
  272. end -- load fn
  273.  
  274. --
  275. -- Item Selection
  276. --
  277.  
  278. local function is_selected(item_name)
  279.     local info = t.getItemDetail()
  280.     return info and (info.name == item_name)
  281. end
  282.  
  283. local function select_item(item_name)
  284.     -- -- Getting item detail takes more time than just selecting
  285.     --  if is_selected(item_name) then
  286.     --      return true
  287.     --  end
  288.  
  289.     if slots_mem[item_name] then
  290.         t.select(slots_mem[item_name])
  291.         if is_selected(item_name) then
  292.             return item_name
  293.         else
  294.             slots_mem[item_name] = nil
  295.         end
  296.     end
  297.  
  298.     -- local old_slot = t.getSelectedSlot()
  299.  
  300.     for i=1,16,1 do
  301.         t.select(i)
  302.         if is_selected(item_name) then
  303.             slots_mem[item_name] = i
  304.             return item_name
  305.         end
  306.     end
  307.  
  308.     -- t.select(old_slot)
  309.     t.select(1)
  310.     return false
  311. end
  312.  
  313. local function use_tested_items(id, test_items)
  314.     if not tested_items[id] then
  315.         for _, item in pairs(test_items) do
  316.             if (select_item(item)) then
  317.                 tested_items[id] = item
  318.                 break
  319.             end
  320.         end
  321.  
  322.         if not tested_items[id] then
  323.             tested_items[id] = 0
  324.         end
  325.     end
  326.  
  327.     if (tested_items[id] ~= 0) then
  328.         local sel_item = select_item(tested_items[id])
  329.         if not sel_item then
  330.             tested_items[id] = nil
  331.         end
  332.         return sel_item
  333.     else
  334.         return false
  335.     end
  336. end
  337.  
  338. get.torch = function()
  339.     return use_tested_items("light", {
  340.         items.torch,
  341.     })
  342. end
  343.  
  344. get.cobble = function()
  345.     return use_tested_items("solid", {
  346.         items.cobble,
  347.         items.sandstone,
  348.     })
  349. end
  350.  
  351. get.chest = function()
  352.     return use_tested_items("store", {
  353.         items.chest,
  354.     })
  355. end
  356.  
  357. get.fuel = function()
  358.     return use_tested_items("fuel", fuel_items_whitelist)
  359. end
  360.  
  361. get.bucket = function()
  362.     return use_tested_items("bucket", {
  363.         items.bucket,
  364.     })
  365. end
  366.  
  367.  
  368. --
  369. -- Fuel Management
  370. --
  371.  
  372. local function items_for_fuel(fuel, item_name)
  373.     if not item_name then
  374.         local info = t.getItemDetail()
  375.         if info then
  376.             item_name = info.name
  377.         else
  378.             return "Error"
  379.         end
  380.     end
  381.  
  382.     local items_needed = 0
  383.  
  384.     local divi = 0
  385.     if item_name==items.coal then
  386.         divi = 80
  387.     elseif item_name == items.lava then
  388.         divi = 1000
  389.     elseif item_name == items.blz_rod then
  390.         divi = 120
  391.     end
  392.  
  393.     items_needed = fuel/divi
  394.  
  395.     return items_needed
  396. end
  397.  
  398. -- Tested
  399. local function calc_fuel_dig_shaft_total(l, w)
  400.     if (not l) or (not w) then
  401.         return 0
  402.     end
  403.  
  404.     local fuel_needed = 0
  405.         + (1) -- up + 1
  406.         + (l*w) -- + l*w
  407.         + (1) -- down + 1
  408.         + ( (w%2~=0) and (l-1) or 0 ) -- if odd w, + l
  409.         + (w-1) -- + w
  410.         + (1) -- forward + 1
  411.  
  412.     return fuel_needed
  413. end
  414.  
  415. -- Tested
  416. local function calc_fuel_next_shaft(shaft_num)
  417.     if not shaft_num then
  418.         return 0
  419.     end
  420.  
  421.     local fuel_needed = 0
  422.         + (dist_from_chest*2) -- to/from chest
  423.         + ((shaft_num*strip_total_w) * 2) -- to/from shaft
  424.  
  425.         + (calc_fuel_dig_shaft_total(dim.strips.l, dim.strips.w) * 2) -- both shafts
  426.         + (dist_across_shaft * 2) -- across main shaft twice
  427.     return fuel_needed
  428. end
  429.  
  430. -- Tested
  431. local function calc_fuel_setup(depth)
  432.     local fuel_needed = 0
  433.         + (depth*2) -- stairs
  434.         + (calc_fuel_dig_shaft_total(dim.base_room.l, dim.base_room.w)) -- Base room
  435.         + (2) -- Chest
  436.         + (7) -- make roof space
  437.         + (dist_from_chest) -- move to wall
  438.  
  439.     return fuel_needed
  440. end
  441.  
  442. -- Tested
  443. local function calc_fuel_mine_strips(total_strips, start_strip)
  444.     if not start_strip then start_strip = 1 end
  445.  
  446.     local fuel_needed = 0
  447.  
  448.     for i=start_strip, total_strips, 1 do
  449.         fuel_needed = fuel_needed + calc_fuel_next_shaft(i) -- Strips
  450.     end
  451.  
  452.     return fuel_needed
  453. end
  454.  
  455. -- Tested
  456. local function calc_fuel_strip_mine_total(total_strips)
  457.     local fuel_needed = 0
  458.         + (calc_fuel_dig_shaft_total(total_strips*strip_total_w, dim.main_shaft.w)) -- Main shaft
  459.         + (dist_from_chest)--*(2-1) -- Fill chest, go back +2 (-1 for last trip)
  460.         + (calc_fuel_mine_strips(total_strips, 1))
  461.  
  462.     return fuel_needed
  463. end
  464.  
  465. local function ensure_fuel(min, calc)
  466.     local msg
  467.     local msg_calc = {
  468.         coal = "- Coal/Charcoal",
  469.         blz = "- Blaze Rod",
  470.         lava = "- Lava Bucket",
  471.     }
  472.     local function set_msg_calc(need)
  473.         if calc then
  474.             msg_calc = {
  475.                 coal = "[" .. math.ceil(items_for_fuel(need, items.coal)) .. "] - Coal/Charcoal",
  476.                 blz = "[" .. math.ceil(items_for_fuel(need, items.blz_rod)) .. "] - Blaze Rod",
  477.                 lava = "[" .. math.ceil(items_for_fuel(need, items.lava)) .. "] - Lava Bucket",
  478.             }
  479.         end
  480.     end
  481.  
  482.     local fuel_max = t.getFuelLimit()
  483.     local fuel_lvl = t.getFuelLevel()
  484.  
  485.     if not min then
  486.         min = 1
  487.         msg = "Out of fuel"
  488.     else
  489.         msg = "Low on fuel"
  490.         --set_msg_calc(min-fuel_lvl)
  491.         fuel_max = min
  492.     end
  493.  
  494.     if (type(fuel_lvl)=="number") then
  495.         while (fuel_lvl<min) do
  496.             print("Checking for fuel..")
  497.             while (not get.fuel()) do
  498.                 clear()
  499.                 --set_msg_calc(min-fuel_lvl)
  500.                 print_ln(msg
  501.                     , "(Required: " .. min .. ") (Current: " .. fuel_lvl .. ")" , ""
  502.                     , "Please ensure there is at least one of the following in the bots inventory:"
  503.                     , msg_calc.coal, msg_calc.blz, msg_calc.lava)
  504.  
  505.                 wait_enter()
  506.                 tested_items.fuel = nil
  507.             end
  508.  
  509.             local fuel_amt = math.min(items_for_fuel(fuel_max-fuel_lvl), 64)
  510.             if fuel_amt < 1 then fuel_amt = 1 end
  511.             t.refuel(fuel_amt)
  512.             fuel_lvl = t.getFuelLevel()
  513.         end
  514.     end
  515.  
  516.     return true
  517. end
  518.  
  519.  
  520. --
  521. -- Torch Management
  522. --
  523.  
  524. local function calc_torches_setup(depth)
  525.     local torches_needed = 0
  526.         + (depth/3) -- stairs
  527.         + (2) -- base room
  528.  
  529.     return torches_needed
  530. end
  531.  
  532. local function calc_torches_strip_mine(total_strips)
  533.     if not total_strips then
  534.         return 64
  535.     end
  536.  
  537.     local torches_needed = 0
  538.         + ((total_strips*strip_total_w)/torch_spacing) -- shaft
  539.         + (torch_per_strip * total_strips) -- strips
  540.         + 1 -- in case
  541.  
  542.     return torches_needed
  543. end
  544.  
  545. local function ensure_torches(min)
  546.     if not min then min = 64 end
  547.  
  548.     local have = 0
  549.  
  550.     for i=1, 16, 1 do
  551.         t.select(i)
  552.         local info = t.getItemDetail()
  553.         if (info and info.name==items.torch) then
  554.             have = have + info.count
  555.         end
  556.     end
  557.  
  558.     if (have < min) then
  559.         clear()
  560.         print_ln("Need more torches"
  561.             , "(Required: " .. min .. ") (Current: " .. have .. ")")
  562.  
  563.         wait_enter()
  564.     end
  565. end
  566.  
  567. --
  568. -- Inventory Management
  569. --
  570.  
  571. local function move_to_start()
  572.     if t.getItemCount() > 0 then
  573.         for i=1, 16, 1 do
  574.             if t.transferTo(i) then
  575.                 break
  576.             end
  577.         end
  578.     end
  579. end
  580.  
  581. local function move_to_end()
  582.     if t.getItemCount() > 0 then
  583.         for i=16, 1, -1 do
  584.             if t.transferTo(i) then
  585.                 break
  586.             end
  587.         end
  588.     end
  589. end
  590.  
  591. local function sort_inventory()
  592.     if (#save_items < 1) then
  593.         return
  594.     end
  595.  
  596.     print("Sorting inventory..")
  597.  
  598.     local save_slots = 0
  599.  
  600.     for i=1, #save_items, 1 do
  601.         local stacks = math.ceil(save_items[i].quantity/64)
  602.         save_slots = save_slots + stacks
  603.  
  604.         if (save_slots >= 16) then
  605.             save_slots = 16
  606.             break
  607.         end
  608.     end
  609.  
  610.     if (save_slots > 0) then
  611.         -- Clear slots for save items
  612.         for i=1, save_slots, 1 do
  613.             t.select(i)
  614.             move_to_end()
  615.         end
  616.     end
  617.  
  618.     -- Move save items to beginning
  619.     for i=1, 16, 1 do
  620.         t.select(i)
  621.         local info = t.getItemDetail()
  622.  
  623.         if info then
  624.             for _, item in ipairs(save_items) do
  625.                 if (info.name == item.name) then
  626.                     move_to_start()
  627.                 end
  628.             end -- all save_items
  629.         end -- if info
  630.     end -- other slots
  631.  
  632.     t.select(1)
  633. end
  634.  
  635. local function dump_all()
  636.     for i=1, 16, 1 do
  637.         t.select(i)
  638.         t.drop()
  639.     end
  640.  
  641.     t.select(1)
  642. end
  643.  
  644. local function dump_junk(can_reset)
  645.     if (#save_items < 1) then
  646.         if can_reset then
  647.             reset_item_slots(true)
  648.         else
  649.             dump_all()
  650.             return
  651.         end
  652.     end
  653.  
  654.     local saved = {}
  655.  
  656.     for i=1, 16, 1 do
  657.         t.select(i)
  658.         local info = t.getItemDetail()
  659.  
  660.         local throw = true
  661.  
  662.         if info then
  663.             for _, item in ipairs(save_items) do
  664.                 if (info.name == item.name) then
  665.                     if not saved[item.name] then
  666.                         saved[item.name] = 0
  667.                     end
  668.                     -- log("name: ", item.name, " amt: ", item.quantity)
  669.                     if (saved[item.name] < item.quantity) then
  670.                         saved[item.name] = (saved[item.name] + info.count)
  671.                         throw = false
  672.                         break -- Break save_items loop, continue slot loop
  673.                     end -- item 64
  674.                 end -- item name
  675.             end -- all save_items
  676.  
  677.             if throw then
  678.                 t.drop()
  679.             end
  680.         end -- if info
  681.     end -- for slots
  682.  
  683.     sort_inventory()
  684. end
  685.  
  686. local function add_fuel_whitelist()
  687.     for i=1, 16, 1 do
  688.         t.select(i)
  689.         local item = t.getItemDetail()
  690.         if item and t.refuel(0) then
  691.             local exists = false
  692.             for _, w_item in pairs(fuel_items_whitelist) do
  693.                 if (w_item == item.name) then
  694.                     exists = true
  695.                     print_ln(item.name .. " is already in whitelist")
  696.                     break
  697.                 end
  698.             end
  699.             if not exists then
  700.                 table.insert(fuel_items_whitelist, item.name)
  701.                 print_ln(item.name .. " added to whitelist")
  702.             end
  703.         end
  704.     end
  705. end
  706.  
  707.  
  708. --
  709. -- Turning
  710. --
  711.  
  712. local function turn_full()
  713.     t.turnLeft()
  714.     t.turnLeft()
  715. end
  716.  
  717. local function swap_dir(dir)
  718.     if dir==t.turnLeft then
  719.         return t.turnRight
  720.     else -- if dir==t.turnRight then
  721.         return t.turnLeft
  722.     end
  723. end
  724.  
  725.  
  726. --
  727. -- Forced Digging
  728. --
  729.  
  730. local function force_dig()
  731.     while t.detect() do
  732.         if not t.dig() then
  733.             log("Can't dig forward")
  734.             return false
  735.         end
  736.     end
  737.  
  738.     return true
  739. end
  740.  
  741. local function force_digUp()
  742.     while t.detectUp() do
  743.         if not t.digUp() then
  744.             log("Can't dig up")
  745.             return false
  746.         end
  747.     end
  748.  
  749.     return true
  750. end
  751.  
  752. local function force_digDown()
  753.     while t.detectDown() do
  754.         if not t.digDown() then
  755.             log("Can't dig down")
  756.             return false
  757.         end
  758.     end
  759.  
  760.     return true
  761. end
  762.  
  763.  
  764. --
  765. -- Forced Movement
  766. --
  767.  
  768. local function attack()
  769.     return aggressive and t.attack()
  770. end
  771.  
  772. local function attackUp()
  773.     return aggressive and t.attackUp()
  774. end
  775.  
  776. local function attackDown()
  777.     return aggressive and t.attackDown()
  778. end
  779.  
  780. local function force_forward()
  781.     if not ensure_fuel() then
  782.         return false
  783.     end
  784.     while (not t.forward()) do
  785.         if not force_dig() then
  786.             return false
  787.         end
  788.         attack()
  789.     end
  790. end
  791.  
  792. local function force_up()
  793.     if not ensure_fuel() then
  794.         return false
  795.     end
  796.     while (not t.up()) do
  797.         if not force_digUp() then
  798.             return false
  799.         end
  800.         attackUp()
  801.     end
  802. end
  803.  
  804. local function force_down()
  805.     if not ensure_fuel() then
  806.         return false
  807.     end
  808.     while (not t.down()) do
  809.         if not force_digDown() then
  810.             return false
  811.         end
  812.         attackDown()
  813.     end
  814. end
  815.  
  816. local function force_back()
  817.     turn_full()
  818.     force_forward()
  819.     turn_full()
  820. end
  821.  
  822.  
  823. --
  824. --  Forced Placement
  825. --
  826.  
  827. local function force_place(give_up)
  828.     local tries = 0
  829.  
  830.     while (not t.detect()) and (not t.place()) do
  831.         log("Can't place forward. Attempt: " .. tries+1)
  832.         attack()
  833.  
  834.         tries = tries + 1
  835.         if give_up and (tries >= give_up) then
  836.             break
  837.         end
  838.  
  839.         sleep(wait_period)
  840.     end
  841. end
  842.  
  843. local function force_placeUp(give_up)
  844.     local tries = 0
  845.  
  846.     while (not t.detectUp()) and (not t.placeUp()) do
  847.         log("Can't place up. Attempt: " .. tries+1)
  848.         attackUp()
  849.  
  850.         tries = tries + 1
  851.         if give_up and (tries >= give_up) then
  852.             break
  853.         end
  854.  
  855.         sleep(wait_period)
  856.     end
  857. end
  858.  
  859. local function force_placeDown(give_up)
  860.     local tries = 0
  861.  
  862.     while (not t.detectDown()) and (not t.placeDown()) do
  863.         log("Can't place down. Attempt: " .. tries+1)
  864.         attackDown()
  865.  
  866.         tries = tries + 1
  867.         if give_up and (tries >= give_up) then
  868.             break
  869.         end
  870.  
  871.         sleep(wait_period)
  872.     end
  873. end
  874.  
  875.  
  876. --
  877. -- Filling
  878. --
  879.  
  880. local function fill_floor()
  881.     if (not t.detectDown()) and get.cobble() then
  882.         force_placeDown(max_place_tries)
  883.     end
  884. end
  885.  
  886. local function fill_roof()
  887.     if (not t.detectUp()) and get.cobble() then
  888.         force_placeUp(max_place_tries)
  889.     end
  890. end
  891.  
  892. local function fill_wall(turn)
  893.     if turn then
  894.         turn()
  895.  
  896.         if (not t.detect()) and get.cobble() then
  897.             force_place(max_place_tries)
  898.         end
  899.  
  900.         swap_dir(turn)()
  901.     end
  902. end
  903.  
  904. local function fill_wall_torch(turn)
  905.     if turn then
  906.  
  907.         turn()
  908.         if (not t.detect()) and get.cobble() then
  909.             force_place(max_place_tries)
  910.         end
  911.  
  912.         if not get.torch() then
  913.             swap_dir(turn)()
  914.         else
  915.             turn()
  916.             force_place(max_place_tries)
  917.             turn_full()
  918.         end
  919.  
  920.     end
  921. end
  922.  
  923.  
  924. --
  925. -- Digging Actions
  926. --
  927.  
  928. local function mine_side(turn)
  929.     turn()
  930.     force_dig()
  931.     swap_dir(turn)()
  932. end
  933.  
  934. local function mine_forward_up()
  935.     force_forward()
  936.     force_digUp()
  937. end
  938.  
  939. local function mine_forward_up_fill(turn, torch)
  940.     mine_forward_up()
  941.  
  942.     fill_floor()
  943.  
  944.     if torch then
  945.         fill_wall_torch(turn)
  946.     else
  947.         fill_wall(turn)
  948.     end
  949. end
  950.  
  951. local function mine_forward_down()
  952.     force_forward()
  953.     force_digDown()
  954. end
  955.  
  956. local function mine_forward_down_fill(turn, torch)
  957.     mine_forward_down()
  958.  
  959.     fill_roof()
  960.  
  961.     if torch then
  962.         fill_wall_torch(turn)
  963.     else
  964.         fill_wall(turn)
  965.     end
  966. end
  967.  
  968. local function mine_uturn(turn, fill)
  969.     turn()
  970.     mine_forward_down_fill()
  971.     if (fill) then
  972.         force_place(max_place_tries)
  973.     end
  974.     turn()
  975. end
  976.  
  977. local function test_pull_lava(use)
  978.     -- return false if no bucket
  979.  
  980.     local function test(test_fn, pull_fn)
  981.         if (not get.bucket()) then
  982.             return false
  983.         end
  984.  
  985.         local success, data = test_fn()
  986.         if (success and (data.name == items.lava_block) and (data.metadata==0)) then
  987.             pull_fn()
  988.             if use then
  989.                 t.refuel()
  990.             end
  991.         end
  992.  
  993.         return true
  994.     end
  995.  
  996.     if not test(t.inspect, t.place) then
  997.         return false
  998.     end
  999.     if not test(t.inspectUp, t.placeUp) then
  1000.         return false
  1001.     end
  1002.     if not test(t.inspectDown, t.placeDown) then
  1003.         return false
  1004.     end
  1005.  
  1006.     return true
  1007. end
  1008.  
  1009.  
  1010. --
  1011. -- Strip Mining Actions
  1012. --
  1013.  
  1014. local function travel_chest_dist()
  1015.     for i=1, dist_from_chest, 1 do
  1016.         force_forward()
  1017.     end
  1018. end
  1019.  
  1020. local function move_fill_chest(strips_left)
  1021.     travel_chest_dist() -- to chest
  1022.  
  1023.     reset_item_slots((strips_left~=nil), calc_torches_strip_mine(strips_left))
  1024.     dump_junk()
  1025.  
  1026.     if strips_left and strips_left > 0 then
  1027.         turn_full()
  1028.         travel_chest_dist() -- back again
  1029.     end
  1030. end
  1031.  
  1032. local function travel_betwen_chest_shaft(num)
  1033.     for i=1, num*strip_total_w, 1 do
  1034.         force_forward()
  1035.     end
  1036. end
  1037.  
  1038. local function goto_strip_from_chest(num)
  1039.     travel_betwen_chest_shaft(num)
  1040.     t.turnRight()
  1041. end
  1042.  
  1043. local function travel_shaft_w()
  1044.     for i=1, (dist_across_shaft), 1 do
  1045.         force_forward()
  1046.     end
  1047. end
  1048.  
  1049. local function goto_chest_from_nextStrip(num)
  1050.     travel_shaft_w()
  1051.     t.turnRight()
  1052.     travel_betwen_chest_shaft(num)
  1053. end
  1054.  
  1055.  
  1056. --
  1057. -- Shaft Digging Process
  1058. --
  1059.  
  1060. local function dig_even(l, w, side, fill_sides, clear_lava)
  1061.     l = l-1 -- 0 index
  1062.  
  1063.     local turn = side
  1064.     local fill_turn = fill_sides and swap_dir(side)
  1065.     local fill_all = (w==1 and fill_sides)
  1066.  
  1067.     local even_w = (w%2 == 0)
  1068.  
  1069.     local torch_offset = (l%torch_spacing)
  1070.     local torch_end_wall = (w>torch_spacing)
  1071.  
  1072.     force_up()
  1073.     mine_forward_down_fill(fill_turn)
  1074.     if (w~=1) then mine_side(turn) end
  1075.  
  1076.     local put_torch_next = true
  1077.  
  1078.     for x=1, w, 1 do
  1079.         if (x~=1 and fill_turn) then
  1080.             fill_turn = nil
  1081.         end
  1082.         if (x==w and not fill_turn) then
  1083.             if even_w then
  1084.                 fill_turn = fill_sides and swap_dir(side)
  1085.             else
  1086.                 fill_turn = fill_sides and side
  1087.             end
  1088.         end
  1089.  
  1090.         for z=1, l, 1 do
  1091.             mine_forward_down_fill(fill_turn, put_torch_next)
  1092.             if fill_all then fill_wall(turn) end -- single file, fill both sides
  1093.  
  1094.             if fill_sides and get.torch() and z~=l then
  1095.                 -- log("z:", z, " x:", x, " w:", w, " off:", torch_offset, " space:", torch_spacing)
  1096.                 if (x==1) then -- first wall
  1097.                     put_torch_next = ((z)%torch_spacing == 0)
  1098.                 elseif not torch_end_wall then -- don't put on last wall
  1099.                     put_torch_next = false
  1100.                 elseif (x==w) then -- last wall
  1101.                     put_torch_next = ((z-torch_offset)%torch_spacing == 0)
  1102.                 end
  1103.  
  1104.                 if (w~=1) and (x==1) and put_torch_next then
  1105.                     -- first wall, not single file
  1106.                     mine_side(turn)
  1107.                 end
  1108.             end
  1109.  
  1110.             if clear_lava then
  1111.                 test_pull_lava(true)
  1112.             end
  1113.  
  1114.         end -- for z
  1115.  
  1116.         if (x<w) then -- Not last
  1117.             mine_uturn(turn, (x==w-1))
  1118.             turn = swap_dir(turn)
  1119.         end
  1120.     end
  1121.  
  1122.     force_down()
  1123. end
  1124.  
  1125. local function dig_odd(l, w, side, fill_sides, clear_lava)
  1126.     dig_even(l, w, side, fill_sides, clear_lava)
  1127.     turn_full()
  1128.  
  1129.     local turn = side
  1130.     local fill_turn = fill_sides and swap_dir(side)
  1131.     local fill_all = (w==1 and fill_sides)
  1132.  
  1133.     for z=1, l-1, 1 do
  1134.         force_forward() -- back to z = 1
  1135.         fill_floor()
  1136.         fill_wall(fill_turn) -- won't work if not fill_sides
  1137.         if fill_all then fill_wall(turn) end -- single file, fill both sides
  1138.     end
  1139. end
  1140.  
  1141. local function dig_shaft(l, w, side, fill_sides, clear_lava)
  1142.     if not side then
  1143.         side = t.turnLeft
  1144.     end
  1145.  
  1146.     local even = (w % 2 == 0)
  1147.  
  1148.     if even then
  1149.         dig_even(l, w, side, fill_sides, clear_lava)
  1150.     else
  1151.         dig_odd(l, w, side, fill_sides, clear_lava)
  1152.     end
  1153.  
  1154.     if (w~=1) then
  1155.         side()
  1156.         for x=1, w-1, 1 do
  1157.             force_forward() -- back to x = 1
  1158.             fill_floor()
  1159.         end
  1160.         swap_dir(side)()
  1161.     end
  1162.     force_forward()
  1163. end
  1164.  
  1165.  
  1166. --
  1167. -- Stairs Digging Process
  1168. --
  1169.  
  1170. local function single_stair_down(torch)
  1171.     force_forward()
  1172.     force_digUp()
  1173.  
  1174.     if torch and get.torch() then
  1175.         force_placeUp(max_place_tries)
  1176.     end
  1177.  
  1178.     force_down()
  1179.  
  1180.     fill_floor()
  1181. end
  1182.  
  1183. local function stairs_down(steps)
  1184.     for i=1, steps, 1 do
  1185.         local turn = i%3==0
  1186.         single_stair_down(turn)
  1187.         if (turn) then
  1188.             t.turnLeft()
  1189.         end
  1190.     end
  1191. end
  1192.  
  1193. local function make_roof_space()
  1194.     -- Uses 7 fuel
  1195.     turn_full()
  1196.     force_up()
  1197.     force_up()
  1198.     force_forward()
  1199.     force_forward()
  1200.     force_down()
  1201.     force_down()
  1202.     turn_full()
  1203.     force_forward()
  1204. end
  1205.  
  1206. local function place_chest(turn)
  1207.     -- Uses 2 fuel
  1208.     local turn_other = swap_dir(turn)
  1209.  
  1210.     -- place 1
  1211.     if get.chest() then
  1212.         force_place(max_place_tries)
  1213.     end
  1214.  
  1215.     -- place 2
  1216.     if get.chest() then
  1217.         turn()
  1218.         force_forward()
  1219.         turn_other()
  1220.         force_dig()
  1221.         force_place(max_place_tries)
  1222.         turn_other()
  1223.         force_forward()
  1224.         turn()
  1225.     end
  1226. end
  1227.  
  1228. local function make_base_room()
  1229.     dig_shaft(dim.base_room.l, dim.base_room.w, t.turnLeft, neat, safe)
  1230.  
  1231.     make_roof_space()
  1232.  
  1233.     place_chest(t.turnRight)
  1234.  
  1235.     reset_item_slots(true, 16*64) -- keep all torches
  1236.     dump_junk()
  1237.  
  1238.     turn_full()
  1239.     travel_chest_dist()
  1240.  
  1241.     if get.torch() then
  1242.         t.turnLeft()
  1243.         force_place(max_place_tries)
  1244.         t.turnRight()
  1245.     end
  1246. end
  1247.  
  1248.  
  1249. --
  1250. -- Strip Mining Process
  1251. --
  1252.  
  1253. local function mine_single_strip(strip_num, fill_sides, clear_lava)
  1254.     print("Starting shaft #", strip_num)
  1255.     goto_strip_from_chest(strip_num)
  1256.     dig_shaft(dim.strips.l, dim.strips.w, t.turnLeft, fill_sides, clear_lava)
  1257.     travel_shaft_w()
  1258.     dig_shaft(dim.strips.l, dim.strips.w, t.turnRight, fill_sides, clear_lava)
  1259.     goto_chest_from_nextStrip(strip_num)
  1260. end
  1261.  
  1262. local function strip_mine(strips)
  1263.     local len = strips*strip_total_w
  1264.  
  1265.     local old_torch_spacing = torch_spacing
  1266.     torch_spacing = strip_total_w
  1267.     dig_shaft(len, dim.main_shaft.w, t.turnLeft, neat, safe)
  1268.     torch_spacing = old_torch_spacing
  1269.  
  1270.     move_fill_chest(strips)
  1271.  
  1272.     for strip_num=1, strips, 1 do
  1273.         mine_single_strip(strip_num, neat, safe)
  1274.         move_fill_chest(strips-strip_num)
  1275.     end
  1276.  
  1277.     dump_all()
  1278. end
  1279.  
  1280.  
  1281. --
  1282. -- Startup
  1283. -- User Inputs
  1284. --
  1285.  
  1286. local function setup_start()
  1287.     clear()
  1288.     print("Enter current y coordinate")
  1289.     local current_y = input_num()
  1290.     print("Enter destination y coordinate")
  1291.     local dest_y = input_num()
  1292.     local depth = current_y-dest_y
  1293.  
  1294.     ensure_fuel(calc_fuel_setup(depth), true)
  1295.  
  1296.     local torches = calc_torches_setup(depth)
  1297.     ensure_torches(torches)
  1298.     sort_inventory()
  1299.     reset_item_slots(true, torches)
  1300.  
  1301.     if (not get.chest()) then
  1302.         clear()
  1303.         print("Need at least 1 chest")
  1304.         wait_enter()
  1305.         tested_items.store = nil
  1306.     end
  1307.  
  1308.     clear()
  1309.     print("Initializing")
  1310.  
  1311.     print("Making stairs..")
  1312.     stairs_down(depth)
  1313.  
  1314.     print("Making base room..")
  1315.     make_base_room()
  1316.  
  1317.     clear()
  1318.     print_ln("Miner ready!")
  1319. end
  1320.  
  1321. local function strip_mine_start()
  1322.     clear()
  1323.     print("Enter number of shafts")
  1324.     local strips = input_num()
  1325.  
  1326.     ensure_fuel(calc_fuel_strip_mine_total(strips), true)
  1327.  
  1328.     local torches
  1329.     if neat then
  1330.         torches = calc_torches_strip_mine(strips)
  1331.         ensure_torches(torches)
  1332.     end
  1333.     sort_inventory()
  1334.     reset_item_slots(true, torches)
  1335.  
  1336.     clear()
  1337.     print("Mining in progress!")
  1338.  
  1339.     strip_mine(strips)
  1340.  
  1341.     clear()
  1342.     print_ln("Mining complete"
  1343.         , "Thanks for using Miner!")
  1344. end
  1345.  
  1346. local function settings_start()
  1347.     local sett = {}
  1348.  
  1349.     clear()
  1350.     print_ln("Setting options:","")
  1351.  
  1352.     print_ln("Auto update on startup?"
  1353.         , "Current: " .. bool_str(auto_update) .. ")")
  1354.     sett.auto_update = input_bool()
  1355.  
  1356.     print_ln("Aggressive bot? (Attack when blocked)"
  1357.         , "(Current: " .. bool_str(aggressive) .. ")")
  1358.     sett.aggressive = input_bool()
  1359.  
  1360.     print_ln("", "Neat bot? (Places walls and torches)"
  1361.         , "(Current: " .. bool_str(neat) .. ")")
  1362.     sett.neat = input_bool()
  1363.  
  1364.     print_ln("", "Safe bot? (Removes lava)"
  1365.         , "(Current: " .. bool_str(safe) .. ")")
  1366.     sett.safe = input_bool()
  1367.  
  1368.     print_ln("", "Debugging? (For development)"
  1369.         , "(Current: " .. bool_str(debug) .. ")")
  1370.     sett.debug = input_bool()
  1371.  
  1372.     print_ln("", "Set spacing and speed?")
  1373.     if (input_bool()) then
  1374.         print_ln("", "Side shafts length"
  1375.             , "(Current: " .. dim.strips.l .. ")")
  1376.         sett.strip_len = math.max(input_num(),1)
  1377.  
  1378.         print_ln("", "Spacing between shafts"
  1379.             , "(Current: " .. strip_spacing .. ")")
  1380.         sett.strip_spacing = math.max(input_num(), 1)
  1381.  
  1382.         print_ln("", "Ave. speed (1-10)"
  1383.             , "(Current: " .. ave_speed .. " )"
  1384.             , "Higher - Faster, less accurate"
  1385.             , "Lower - Slower, more accurate")
  1386.         sett.ave_speed = math.min(math.max(input_num(), 1), 10)
  1387.     end
  1388.  
  1389.     print_ln("", "Add items to fuel whitelist?")
  1390.     if (input_bool()) then
  1391.         print_ln("Place fueling items in bots inventory")
  1392.         wait_enter()
  1393.         add_fuel_whitelist()
  1394.         sett.fuel_items_whitelist = fuel_items_whitelist
  1395.     end
  1396.  
  1397.     print_ln("", "Confirm and save setttings?")
  1398.     if (input_bool()) then
  1399.         init(sett)
  1400.         save_settings(sett)
  1401.     end
  1402. end
  1403.  
  1404. local function debug_start()
  1405.     local function calc_fuels()
  1406.         reset_item_slots(true, 16*64)
  1407.  
  1408.         local estim = calc_fuel_setup(5)
  1409.         ensure_fuel(estim, true)
  1410.  
  1411.         local prev_lvl = t.getFuelLevel()
  1412.         stairs_down(5)
  1413.         make_base_room()
  1414.  
  1415.         local used = prev_lvl - t.getFuelLevel()
  1416.         print_ln("Estimate: " .. estim, "Used: " .. used)
  1417.     end
  1418.  
  1419.     -- print(test_pull_lava(true))
  1420.  
  1421.     -- calc_fuels()
  1422.  
  1423.     --  local prev_lvl = t.getFuelLevel()
  1424.     --  t.refuel(1)
  1425.     --  local more = t.getFuelLevel() - prev_lvl
  1426.     --  print(more)
  1427.  
  1428.     --  print("Enter num")
  1429.     --  print(items_for_fuel(io.read(), items.coal))
  1430.  
  1431.     -- dump_junk(true)
  1432.     -- sort_inventory()
  1433. end
  1434.  
  1435. --
  1436.  
  1437. local function startup()
  1438.     if not t then
  1439.         print_ln("Miner can only run on turtles")
  1440.         return
  1441.     end
  1442.  
  1443.     load_settings()
  1444.  
  1445.     -- Uses BlueAPI
  1446.     if not b_api then
  1447.         shell.run(blueApi)
  1448.         if not b_api then
  1449.             print_ln("Miner needs BlueAPI to run")
  1450.             return
  1451.         end
  1452.     end
  1453.  
  1454.     local luaFile = selfRun .. ".lua"
  1455.     if fs.exists(luaFile) then
  1456.         b_startup.removeAlias(alias, luaFile)
  1457.         b_files.trimLuaExtDir(root, true)
  1458.         b_startup.addAlias(alias, selfRun)
  1459.         shell.setAlias(alias, selfRun)
  1460.     end
  1461.  
  1462.     if auto_update then
  1463.         b_api.load("b_update")
  1464.         b_update.gitUpdate(user, repo, "master", root, exclFiles)
  1465.     end
  1466.  
  1467.     clear()
  1468.     print_ln("Welcome to Miner!", "")
  1469.  
  1470.     print_ln("Please ensure these items are loaded for a better experience:"
  1471.         , "- Coal, Lava, or Blaze Rods"
  1472.         , "- Torches"
  1473.         , "- Some Cobblestone"
  1474.         , "- 1-2 Chests"
  1475.         , "- 1-3 Empty Bucket")
  1476.     wait_enter()
  1477.  
  1478.     local function pick_option()
  1479.         local opt
  1480.  
  1481.         clear()
  1482.         print_ln("Pick an option:"
  1483.             ,"1 - Setup (Use this first)"
  1484.             ,"2 - Start strip mine"
  1485.             ,"3 - Settings")
  1486.         if debug then print("4 - Debug") end
  1487.         opt = input_num()
  1488.  
  1489.         if opt==1 then
  1490.             setup_start()
  1491.             wait_enter("start mining")
  1492.             strip_mine_start()
  1493.         elseif opt== 2 then
  1494.             strip_mine_start()
  1495.         elseif opt==3 then
  1496.             settings_start()
  1497.         elseif debug and opt==4 then
  1498.             debug_start()
  1499.         else
  1500.             opt = nil
  1501.         end
  1502.  
  1503.         return opt
  1504.     end
  1505.  
  1506.     local opt = pick_option()
  1507.     while (not opt or opt==3) do
  1508.         if not opt then print_ln("Invalid option", "") end
  1509.         opt = pick_option()
  1510.     end
  1511. end
  1512.  
  1513. startup()
Add Comment
Please, Sign In to add comment