Skip to content

Instantly share code, notes, and snippets.

@kawashirov
Created June 23, 2024 22:25
Show Gist options
  • Save kawashirov/f9d6e849f173f0f8e127d7142f83d627 to your computer and use it in GitHub Desktop.
Save kawashirov/f9d6e849f173f0f8e127d7142f83d627 to your computer and use it in GitHub Desktop.
cc-simple-miner-computer
return {
redstone_side = "right",
ae2_peripheral = "ae2:growth_accelerator",
demand_items = {
-- items thats need to be mined by this setup, will mine if any below limit
["minecraft:diamond_ore"] = 8000,
},
demand_fluids = {
-- fluids thats need to be mined by this setup, will mine if any below limit
["modern_industrialization:crude_oil"] = 600 * 1000
},
critical_items = {
-- items will be used for mining (as drill/enegry), don't mine if any below limit
["minecraft:coal_ore"] = 4000,
["modern_industrialization:lignite_coal_ore"] = 4000,
},
critical_fluids = {
-- fluids will be used for mining (as drill/enegry), don't mine if any below limit
["modern_industrialization:steam"] = 20 * 1000,
}
}
-- ae2 = peripheral.find("ae2:growth_accelerator")
CONFIG = "demand_config.lua"
SIMPLIFY = {
["minecraft"] = "MC",
["modern_industrialization"] = "MI",
["techreborn"] = "TR",
}
local function simplify_ns(s)
-- modern_industrialization:very_fucking_long_name -> MI:very_fucking_long_name
local simplified = s
for simplify_key, simplify_val in pairs(SIMPLIFY) do
simplified = string.gsub(simplified, simplify_key, simplify_val)
end
return simplified
end
local function simplify_num(d)
-- 123000 -> 123k
return (d % 1000 == 0) and (("%dk"):format(d / 1000)) or d
end
local function count_pairs(t)
-- count pairs in key-table
local count = 0
for key, value in pairs(t) do
count = count + 1
end
return count
end
local function transform_table(raw_t, valid_d, valid_c, key_k, value_k)
-- transform index-table to key-table
local new_t = {}
for _, item in ipairs(raw_t) do
key = item[key_k]
if valid_d[key] or valid_c[key] then
new_t[key] = item[value_k]
end
end
return new_t
end
function count_demand(demand_t, current_t, print_all)
-- count how many elements in current_t have less values than in demand_t, and print info
local total_types = 0
for name, demand_c in pairs(demand_t) do
local current_c = current_t[name] or 0
local need_more = current_c < demand_c
total_types = total_types + (need_more and 1 or 0)
if print_all or need_more then
local p = need_more and printError or print
local percent = math.floor(current_c / demand_c * 100 + 0.5)
local mark = need_more and "!" or " "
p(("%s %s %d%% %s / %s"):format(mark, simplify_ns(name), percent, simplify_num(current_c), simplify_num(demand_c)))
end
end
return total_types
end
local function main_loop()
term.clear()
term.setCursorPos(1, 1)
local config = dofile(CONFIG)
local ae2 = peripheral.find(config.ae2_peripheral)
local demand_item_types = count_pairs(config.demand_items)
local demand_fluid_types = count_pairs(config.demand_fluids)
local demand_types = demand_item_types + demand_fluid_types
local crit_item_types = count_pairs(config.critical_items)
local crit_fluid_types = count_pairs(config.critical_fluids)
local crit_types = crit_item_types + crit_fluid_types
local current_items_raw = ae2.items()
local current_items = transform_table(current_items_raw, config.demand_items, config.critical_items, "name", "count")
local current_fluids_raw = ae2.tanks()
local current_fluids = transform_table(current_fluids_raw, config.demand_fluids, config.critical_fluids, "name", "amount")
print(("Types: Stored: %dI/%dF, Demand: %dI/%dF"):format(#current_items_raw, #current_fluids_raw, demand_item_types, demand_fluid_types))
local allow_drill = count_demand(config.demand_items, current_items, true) + count_demand(config.demand_fluids, current_fluids, true)
print(("Types: Critical: %dI/%dF"):format(crit_item_types, crit_fluid_types))
local block_drill = count_demand(config.critical_items, current_items, false) + count_demand(config.critical_fluids, current_fluids, false)
local should_drill = allow_drill > 0 and block_drill < 1
print(("Drill: %s (D: %d/%d, C: %d/%d)"):format(should_drill, allow_drill, demand_types, block_drill, crit_types))
if should_drill then
redstone.setOutput(config.redstone_side, true)
sleep(1)
end
redstone.setOutput(config.redstone_side, false)
end
while true do
main_loop()
sleep(5)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment