Advertisement
Guest User

Miner

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