Prepare for use with techage

master
Joachim Stolberg 2021-06-07 21:59:38 +02:00
parent 094f0b7080
commit 9477cf1a4f
3 changed files with 70 additions and 44 deletions

View File

@ -68,7 +68,7 @@ end
-- Send a request with 'topic' string to all 'tlib2' network
-- nodes of type 'node_type'.
-- Function returns a list with all responses.
function networks.liquid.request(pos, tlib2, outdir, node_type, topic)
function networks.control.request(pos, tlib2, outdir, node_type, topic)
assert(outdir and node_type and topic)
assert(type(topic) == "string")
local t = {}

View File

@ -21,41 +21,55 @@ local N = tubelib2.get_node_lvm
networks.power = {}
networks.registered_networks.power = {}
local DEFAULT_DATA = {
curr_load = 0, -- network storage value
max_capa = 0, -- network storage capacity
consumed = 0, -- consumed power by consumers
provided = 0, -- provided power by generators
available = 0, -- max. available generator power
netw_num = 0, -- network number
}
-- Storage parameters:
-- capa = maximum value in power units
-- load = current value in power units
-- level = ratio value (load/capa) (0..1)
local Power = {} -- {netID = {curr_load, min_load, max_load, max_capa, consumed, provided, available}}
local Power = {} -- {netID = {curr_load, max_capa, consumed, provided, available}}
-- Determine load, capa and other power network data
local function get_power_data(pos, tlib2, outdir)
assert(outdir)
local netw = networks.get_network_table(pos, tlib2, outdir)
local netw = networks.get_network_table(pos, tlib2, outdir) or {}
local max_capa = 0
local max_perf = 0
local curr_load = 0
-- Generators
for _,item in ipairs(netw.gen or {}) do
local ndef = minetest.registered_nodes[N(item.pos).name]
local data = ndef.get_generator_data(item.pos, tlib2)
max_capa = max_capa + data.capa
curr_load = curr_load + (data.level * data.capa)
if data then
max_capa = max_capa + data.capa
max_perf = max_perf + data.perf
curr_load = curr_load + (data.level * data.capa)
end
end
-- Storage systems
for _,item in ipairs(netw.sto or {}) do
local ndef = minetest.registered_nodes[N(item.pos).name]
local data = ndef.get_storage_data(item.pos, tlib2)
max_capa = max_capa + data.capa
curr_load = curr_load + (data.level * data.capa)
if data then
max_capa = max_capa + data.capa
curr_load = curr_load + (data.level * data.capa)
end
end
return {
curr_load = curr_load, -- network storage value
min_load = curr_load, -- minimal storage value
max_load = curr_load, -- maximal storage value
max_capa = max_capa, -- network storage capacity
consumed = 0, -- consumed power over all consumers
provided = 0, -- provided power over all generators
available = 0, -- max. available power over all generators
max_perf = max_perf, -- max. available power
consumed = 0, -- consumed power
provided = 0, -- provided power
available = 0, -- available power
num_nodes = netw.num_nodes,
}
end
@ -129,14 +143,12 @@ function networks.power.consume_power(pos, tlib2, outdir, amount)
local pwr = Power[netID] or get_power_data(pos, tlib2, outdir)
if pwr.curr_load >= amount then
pwr.curr_load = pwr.curr_load - amount
pwr.min_load = math.min(pwr.min_load, pwr.curr_load)
pwr.consumed = pwr.consumed + amount
Power[netID] = pwr
return amount
else
local consumed = pwr.curr_load
pwr.curr_load = 0
pwr.min_load = 0
pwr.consumed = pwr.consumed + consumed
Power[netID] = pwr
return consumed
@ -172,12 +184,11 @@ function networks.power.provide_power(pos, tlib2, outdir, amount, cp1, cp2)
pwr.available = pwr.available + amount
amount = math.min(amount, pwr.max_capa - pwr.curr_load)
cp1 = cp1 or 0.5
cp1 = cp1 or 0.8
cp2 = cp2 or 1.0
if x < cp1 then -- charge with full power
pwr.curr_load = pwr.curr_load + amount
pwr.max_load = math.max(pwr.max_load, pwr.curr_load)
pwr.provided = pwr.provided + amount
Power[netID] = pwr
return amount
@ -185,7 +196,6 @@ function networks.power.provide_power(pos, tlib2, outdir, amount, cp1, cp2)
local factor = 1 - ((x - cp1) / (cp2 - cp1))
local provided = amount * factor
pwr.curr_load = pwr.curr_load + provided
pwr.max_load = math.max(pwr.max_load, pwr.curr_load)
pwr.provided = pwr.provided + provided
Power[netID] = pwr
return provided
@ -196,6 +206,16 @@ function networks.power.provide_power(pos, tlib2, outdir, amount, cp1, cp2)
return 0
end
-- Function for generators with storage capacity
function networks.power.get_storage_load(pos, tlib2, outdir, amount)
local netID = networks.determine_netID(pos, tlib2, outdir)
if netID then
local pwr = Power[netID] or get_power_data(pos, tlib2, outdir)
return pwr.curr_load / pwr.max_capa * amount
end
return 0
end
-------------------------------------------------------------------------------
-- Storage
-------------------------------------------------------------------------------
@ -293,15 +313,24 @@ function networks.power.get_network_data(pos, tlib2, outdir)
local netID = networks.determine_netID(pos, tlib2, outdir)
if netID then
local pwr = Power[netID] or get_power_data(pos, tlib2, outdir)
local consumed, provided, available
if pwr.available > 0 then
local fac = pwr.max_perf / pwr.available
available = pwr.max_perf
provided = pwr.provided * fac
consumed = pwr.consumed * fac
else
available = pwr.max_perf
provided = 0
consumed = pwr.consumed
end
local res = {
curr_load = pwr.curr_load,
min_load = pwr.min_load,
max_load = pwr.max_load,
max_capa = pwr.max_capa,
consumed = pwr.consumed,
provided = pwr.provided,
available = pwr.available,
netw_num = networks.netw_num(netID),
curr_load = pwr.curr_load, -- network storage value
max_capa = pwr.max_capa, -- network storage capacity
consumed = consumed, -- consumed power by consumers
provided = provided, -- provided power by generators
available = available, -- max. available generator power
netw_num = networks.netw_num(netID), -- network number
}
pwr.consumed = 0
pwr.provided = 0
@ -309,16 +338,5 @@ function networks.power.get_network_data(pos, tlib2, outdir)
return res
end
end
end
function networks.power.reset_min_max_load_values(pos, tlib2, outdir)
for _,outdir in ipairs(networks.get_outdirs(pos, tlib2, outdir)) do
local netID = networks.determine_netID(pos, tlib2, outdir)
if netID then
local pwr = Power[netID] or get_power_data(pos, tlib2, outdir)
pwr.min_load = pwr.curr_load
pwr.max_load = pwr.curr_load
return
end
end
return DEFAULT_DATA
end

View File

@ -22,7 +22,13 @@ local CON_MAX = 5
local HIDDEN = true -- enable/disable hidden nodes
local function round(val)
return math.floor(val + 0.5)
if val > 100 then
return math.floor(val + 0.5)
elseif val > 10 then
return math.floor((val * 10) + 0.5) / 10
else
return math.floor((val * 100) + 0.5) / 100
end
end
local power = networks.power
@ -222,6 +228,7 @@ minetest.register_node("networks:generator", {
local outdir = M(pos):get_int("outdir")
local mem = tubelib2.get_mem(pos)
mem.provided = power.provide_power(pos, Cable, outdir, GEN_MAX)
mem.load = power.get_storage_load(pos, Cable, outdir, GEN_MAX)
M(pos):set_string("infotext", "providing "..round(mem.provided))
return true
end,
@ -241,7 +248,11 @@ minetest.register_node("networks:generator", {
power.start_storage_calc(pos, Cable, outdir)
end,
get_generator_data = function(pos, tlib2)
return {level = 0, capa = GEN_MAX * 2} -- generator capa = 2 * performance
local mem = tubelib2.get_mem(pos)
if mem.running then
-- generator storage capa = 2 * performance
return {level = (mem.load or 0) / GEN_MAX, perf = GEN_MAX, capa = GEN_MAX * 2}
end
end,
paramtype2 = "facedir",
on_rotate = screwdriver.disallow,
@ -310,8 +321,6 @@ minetest.register_node("networks:storage", {
local mem = tubelib2.get_mem(pos)
if mem.running then
return {level = (mem.load or 0) / STORAGE_CAPA, capa = STORAGE_CAPA}
else
return {level = 0, capa = 0}
end
end,
paramtype2 = "facedir",
@ -561,11 +570,10 @@ minetest.register_chatcommand("power_data", {
pos = vector.round(pos)
local data = power.get_network_data(pos, Cable)
if data then
local s = string.format("Netw %u: generated = %u/%u, consumed = %u, storage load = %u/%u (min = %u, max = %u)",
local s = string.format("Netw %u: generated = %u/%u, consumed = %u, storage load = %u/%u",
data.netw_num, round(data.provided),
data.available, round(data.consumed),
round(data.curr_load), round(data.max_capa),
round(data.min_load), round(data.max_load))
round(data.curr_load), round(data.max_capa))
return true, s
end
return false, "No valid node position!"