Improve power terminal

This commit is contained in:
Joachim Stolberg 2023-02-19 16:47:06 +01:00
parent 66937f5743
commit 6d9c9bb51f
4 changed files with 128 additions and 30 deletions

View File

@ -3,7 +3,7 @@
TechAge TechAge
======= =======
Copyright (C) 2019-2021 Joachim Stolberg Copyright (C) 2019-2023 Joachim Stolberg
AGPL v3 AGPL v3
See LICENSE.txt for more information See LICENSE.txt for more information
@ -27,10 +27,11 @@ local control = networks.control
local HELP = S([[Commands local HELP = S([[Commands
help . . . print this text help . . . print this text
cls . . . . . clear screen cls . . . . clear screen
gen . . . . print all generators gen . . print generators
sto . . . . . print all storage systems sto . . print storage systems
con . . . . . print main consumers con1 . . print consumers with power consumption between 1 and 10 ku
con2 . . print consumers with power consumption with 10 ku or above
]]) ]])
local function row(num, label, data) local function row(num, label, data)
@ -41,7 +42,6 @@ local function row(num, label, data)
"label[8.5,"..y..";" .. data .. "]" "label[8.5,"..y..";" .. data .. "]"
end end
local function formspec1(pos, data) local function formspec1(pos, data)
local mem = techage.get_mem(pos) local mem = techage.get_mem(pos)
local outdir = M(pos):get_int("outdir") local outdir = M(pos):get_int("outdir")
@ -94,12 +94,32 @@ local function formspec2(pos)
"button[7.9,7.4;2,1;enter;"..S("Enter").."]" "button[7.9,7.4;2,1;enter;"..S("Enter").."]"
end end
--------------------------------------------------------------------------------------
--Overwrite networks.power.consume_power function
--------------------------------------------------------------------------------------
local origin_consume_power = networks.power.consume_power
function networks.power.consume_power(pos, tlib2, outdir, amount)
local nvm = techage.get_nvm(pos)
nvm.power_taken = origin_consume_power(pos, tlib2, outdir, amount)
return nvm.power_taken
end
local function get_consumer_power_consumption(pos)
if pos then
local nvm = techage.get_nvm(pos)
if nvm.running or techage.needs_power(nvm) then
return nvm.power_taken or 0
end
end
return 0
end
local function generators(pos) local function generators(pos)
local tbl = {} local tbl = {}
local outdir = M(pos):get_int("outdir") local outdir = M(pos):get_int("outdir")
local resp = control.request(pos, Cable, outdir, "gen", "info") local resp = control.request(pos, Cable, outdir, "gen", "info")
for _, item in ipairs(resp) do for _, item in ipairs(resp) do
local name = item.type .. " (" .. item.number .. ")"
if item.running then if item.running then
local s = string.format("%s (%s): %s/%u ku (%s)", local s = string.format("%s (%s): %s/%u ku (%s)",
item.type, item.number, techage.round(item.provided), item.available, item.termpoint) item.type, item.number, techage.round(item.provided), item.available, item.termpoint)
@ -119,26 +139,28 @@ local function storages(pos)
local outdir = M(pos):get_int("outdir") local outdir = M(pos):get_int("outdir")
local resp = control.request(pos, Cable, outdir, "sto", "info") local resp = control.request(pos, Cable, outdir, "sto", "info")
for _, item in ipairs(resp) do for _, item in ipairs(resp) do
local name = item.type .. " (" .. item.number .. ")" -- TA4/TA5 heatexchangers are no storage systems
if item.running then if item.capa and item.capa > 1 then
local s = string.format("%s (%s): %s/%s kud", if item.running then
item.type, item.number, local s = string.format("%s (%s): %s/%s kud",
techage.round(item.load / techage.CYCLES_PER_DAY), item.type, item.number,
techage.round(item.capa / techage.CYCLES_PER_DAY)) techage.round(item.load / techage.CYCLES_PER_DAY),
tbl[#tbl + 1] = s techage.round(item.capa / techage.CYCLES_PER_DAY))
else tbl[#tbl + 1] = s
local s = string.format("%s (%s): %s/%s kud (off)", else
item.type, item.number, local s = string.format("%s (%s): %s/%s kud (off)",
techage.round(item.load / techage.CYCLES_PER_DAY), item.type, item.number,
techage.round(item.capa / techage.CYCLES_PER_DAY)) techage.round(item.load / techage.CYCLES_PER_DAY),
tbl[#tbl + 1] = s techage.round(item.capa / techage.CYCLES_PER_DAY))
tbl[#tbl + 1] = s
end
end end
end end
table.sort(tbl) table.sort(tbl)
return table.concat(tbl, "\n") return table.concat(tbl, "\n")
end end
local function consumers(pos) local function consumers(pos, min, max)
local tbl = {} local tbl = {}
local outdir = M(pos):get_int("outdir") local outdir = M(pos):get_int("outdir")
local netw = networks.get_network_table(pos, Cable, outdir) or {} local netw = networks.get_network_table(pos, Cable, outdir) or {}
@ -147,7 +169,17 @@ local function consumers(pos)
if number then if number then
local name = techage.get_node_lvm(item.pos).name local name = techage.get_node_lvm(item.pos).name
name = (minetest.registered_nodes[name] or {}).description or "unknown" name = (minetest.registered_nodes[name] or {}).description or "unknown"
tbl[#tbl + 1] = name .. " (" .. number .. ")" local taken = techage.round(get_consumer_power_consumption(item.pos))
if taken > min and taken < max then
tbl[#tbl + 1] = string.format("%s (%s): %s ku", name, number, taken)
end
else
local name = techage.get_node_lvm(item.pos).name
name = (minetest.registered_nodes[name] or {}).description or "unknown"
local taken = techage.round(get_consumer_power_consumption(item.pos))
if taken > min and taken < max then
tbl[#tbl + 1] = string.format("%s: %s ku", name, taken)
end
end end
end end
table.sort(tbl) table.sort(tbl)
@ -157,7 +189,7 @@ end
local function output(pos, command, text) local function output(pos, command, text)
local meta = M(pos) local meta = M(pos)
text = meta:get_string("output") .. "\n$ " .. command .. "\n" .. (text or "") text = meta:get_string("output") .. "\n$ " .. command .. "\n" .. (text or "")
text = text:sub(-2000,-1) text = text:sub(-5000,-1)
meta:set_string("output", text) meta:set_string("output", text)
end end
@ -177,8 +209,10 @@ local function command(pos, nvm, command)
output(pos, command, generators(pos)) output(pos, command, generators(pos))
elseif cmd == "sto" then elseif cmd == "sto" then
output(pos, command, storages(pos)) output(pos, command, storages(pos))
elseif cmd == "con" then elseif cmd == "con1" then
output(pos, command, consumers(pos)) output(pos, command, consumers(pos, 1, 10))
elseif cmd == "con2" then
output(pos, command, consumers(pos, 9, 1000))
elseif command ~= "" then elseif command ~= "" then
output(pos, command, "") output(pos, command, "")
end end

View File

@ -23,6 +23,7 @@ local PWR_CAPA = 2400 -- ticks (2s) with 1 ku ==> 80 min = 4 game days
local Cable = techage.ElectricCable local Cable = techage.ElectricCable
local power = networks.power local power = networks.power
local control = networks.control
local function node_timer(pos, elapsed) local function node_timer(pos, elapsed)
local nvm = techage.get_nvm(pos) local nvm = techage.get_nvm(pos)
@ -46,7 +47,7 @@ local function node_timer(pos, elapsed)
power.start_storage_calc(pos, Cable, 5) power.start_storage_calc(pos, Cable, 5)
nvm.providing = true nvm.providing = true
else else
nvm.provided = power.provide_power(pos, Cable, 5, PWR_PERF) nvm.provided = power.provide_power(pos, Cable, 5, PWR_PERF, 0.8, 1.0)
nvm.capa = nvm.capa - nvm.provided nvm.capa = nvm.capa - nvm.provided
end end
else else
@ -105,6 +106,27 @@ minetest.register_node("techage:ta4_solar_minicell", {
power.register_nodes({"techage:ta4_solar_minicell"}, Cable, "gen", {"D"}) power.register_nodes({"techage:ta4_solar_minicell"}, Cable, "gen", {"D"})
control.register_nodes({"techage:ta4_solar_minicell"}, {
on_receive = function(pos, tlib2, topic, payload)
end,
on_request = function(pos, tlib2, topic)
if topic == "info" then
local nvm = techage.get_nvm(pos)
local meta = M(pos)
return {
type = S("TA4 Streetlamp Solar Cell"),
number = meta:get_string("node_number") or "",
running = nvm.providing or false,
available = PWR_PERF,
provided = nvm.provided or 0,
termpoint = "80% - 100%",
}
end
return false
end,
}
)
techage.register_node({"techage:ta4_solar_minicell"}, { techage.register_node({"techage:ta4_solar_minicell"}, {
on_recv_message = function(pos, src, topic, payload) on_recv_message = function(pos, src, topic, payload)
local nvm = techage.get_nvm(pos) local nvm = techage.get_nvm(pos)

View File

@ -3,7 +3,7 @@
TechAge TechAge
======= =======
Copyright (C) 2019-2021 Joachim Stolberg Copyright (C) 2019-2023 Joachim Stolberg
AGPL v3 AGPL v3
See LICENSE.txt for more information See LICENSE.txt for more information
@ -21,6 +21,7 @@ local S2P = minetest.string_to_pos
local Cable = techage.ElectricCable local Cable = techage.ElectricCable
local Axle = techage.Axle local Axle = techage.Axle
local power = networks.power local power = networks.power
local control = networks.control
local CYCLE_TIME = 2 local CYCLE_TIME = 2
local PWR_PERF = 24 local PWR_PERF = 24
@ -45,9 +46,7 @@ local function node_timer_on(pos, elapsed)
nvm.buffer = nvm.buffer + taken - 1 -- some loss nvm.buffer = nvm.buffer + taken - 1 -- some loss
if nvm.buffer >= PWR_PERF then if nvm.buffer >= PWR_PERF then
local tp1 = tonumber(meta:get_string("termpoint1")) nvm.provided = power.provide_power(pos, Cable, outdir, PWR_PERF, 0.8, 1.0)
local tp2 = tonumber(meta:get_string("termpoint2"))
nvm.provided = power.provide_power(pos, Cable, outdir, PWR_PERF, tp1, tp2)
nvm.load = power.get_storage_load(pos, Cable, outdir, PWR_PERF) nvm.load = power.get_storage_load(pos, Cable, outdir, PWR_PERF)
nvm.buffer = nvm.buffer - nvm.provided nvm.buffer = nvm.buffer - nvm.provided
end end
@ -172,6 +171,27 @@ techage.register_node({"techage:ta2_generator_off", "techage:ta2_generator_on"},
power.register_nodes({"techage:ta2_generator_off", "techage:ta2_generator_on"}, Axle, "con", {"L"}) power.register_nodes({"techage:ta2_generator_off", "techage:ta2_generator_on"}, Axle, "con", {"L"})
power.register_nodes({"techage:ta2_generator_off", "techage:ta2_generator_on"}, Cable, "gen", {"R"}) power.register_nodes({"techage:ta2_generator_off", "techage:ta2_generator_on"}, Cable, "gen", {"R"})
control.register_nodes({"techage:ta2_generator_off", "techage:ta2_generator_on"}, {
on_receive = function(pos, tlib2, topic, payload)
end,
on_request = function(pos, tlib2, topic)
if topic == "info" then
local nvm = techage.get_nvm(pos)
local meta = M(pos)
return {
type = S("TA2 Power Generator"),
number = "---",
running = true,
available = PWR_PERF,
provided = nvm.provided or 0,
termpoint = "80% - 100%",
}
end
return false
end,
}
)
minetest.register_craft({ minetest.register_craft({
output = "techage:ta2_generator_off", output = "techage:ta2_generator_off",
recipe = { recipe = {

View File

@ -22,6 +22,7 @@ local fuel = techage.fuel
local Pipe = techage.LiquidPipe local Pipe = techage.LiquidPipe
local power = networks.power local power = networks.power
local liquid = networks.liquid local liquid = networks.liquid
local control = networks.control
local CYCLE_TIME = 2 local CYCLE_TIME = 2
local STANDBY_TICKS = 1 local STANDBY_TICKS = 1
@ -327,6 +328,27 @@ local liquid_def = {
power.register_nodes({"techage:tiny_generator", "techage:tiny_generator_on"}, Cable, "gen", {"R"}) power.register_nodes({"techage:tiny_generator", "techage:tiny_generator_on"}, Cable, "gen", {"R"})
liquid.register_nodes({"techage:tiny_generator", "techage:tiny_generator_on"}, Pipe, "tank", nil, liquid_def) liquid.register_nodes({"techage:tiny_generator", "techage:tiny_generator_on"}, Pipe, "tank", nil, liquid_def)
control.register_nodes({"techage:tiny_generator", "techage:tiny_generator_on"}, {
on_receive = function(pos, tlib2, topic, payload)
end,
on_request = function(pos, tlib2, topic)
if topic == "info" then
local nvm = techage.get_nvm(pos)
local meta = M(pos)
return {
type = S("TA3 Tiny Power Generator"),
number = meta:get_string("node_number") or "",
running = techage.is_running(nvm) or false,
available = PWR_PERF,
provided = nvm.provided or 0,
termpoint = meta:get_string("termpoint"),
}
end
return false
end,
}
)
techage.register_node({"techage:tiny_generator", "techage:tiny_generator_on"}, { techage.register_node({"techage:tiny_generator", "techage:tiny_generator_on"}, {
on_recv_message = function(pos, src, topic, payload) on_recv_message = function(pos, src, topic, payload)
local nvm = techage.get_nvm(pos) local nvm = techage.get_nvm(pos)