Supply converter: demany only the needed power (#577)

Supply converter demands only the needed power to satisfy the demand on the output network. It can still be too much if the output network has other power sources, because supply is not taken in account.

This surplus of energy can be used to charge batteries.
This commit is contained in:
ZettaScript 2022-10-20 17:56:10 +02:00 committed by GitHub
parent fa39b24ed5
commit 718a5beda1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 10 deletions

View File

@ -120,7 +120,7 @@ local run = function(pos, node, run_stage)
return
end
local remain = 0.9
local efficiency = 0.9
-- Machine information
local machine_name = S("Supply Converter")
local meta = minetest.get_meta(pos)
@ -133,7 +133,6 @@ local run = function(pos, node, run_stage)
enabled = enabled == "1"
end
enabled = enabled and (meta:get_int("mesecon_mode") == 0 or meta:get_int("mesecon_effect") ~= 0)
local demand = enabled and meta:get_int("power") or 0
local pos_up = {x=pos.x, y=pos.y+1, z=pos.z}
local pos_down = {x=pos.x, y=pos.y-1, z=pos.z}
@ -144,14 +143,36 @@ local run = function(pos, node, run_stage)
local to = technic.get_cable_tier(name_down)
if from and to then
local input = meta:get_int(from.."_EU_input")
meta:set_int(from.."_EU_demand", demand)
meta:set_int(from.."_EU_supply", 0)
meta:set_int(to.."_EU_demand", 0)
meta:set_int(to.."_EU_supply", input * remain)
meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name,
technic.EU_string(input), from,
technic.EU_string(input * remain), to))
-- Get the "to" network switching station for EU demand calculation
local network_hash = technic.cables[minetest.hash_node_position(pos_down)]
local network = network_hash and minetest.get_position_from_hash(network_hash)
local sw_pos = network and {x=network.x,y=network.y+1,z=network.z}
local timeout = 0
for tier in pairs(technic.machines) do
-- Supply converter must be connected to a network
timeout = math.max(meta:get_int(tier.."_EU_timeout"), timeout)
end
if timeout > 0 and sw_pos and minetest.get_node(sw_pos).name == "technic:switching_station" then
local sw_meta = minetest.get_meta(sw_pos)
local demand = 0
if enabled then
-- Reverse evaluate the required machine and round to a nice number
demand = 100 * math.ceil((sw_meta:get_int("demand") / efficiency) / 100)
-- Do not draw more than the limit
demand = math.min(demand, meta:get_int("power"))
end
local input = meta:get_int(from.."_EU_input") -- actual input
meta:set_int(from.."_EU_demand", demand) -- desired input
meta:set_int(from.."_EU_supply", 0)
meta:set_int(to.."_EU_demand", 0)
meta:set_int(to.."_EU_supply", input * efficiency)
meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name,
technic.EU_string(input), from,
technic.EU_string(input * efficiency), to))
else
meta:set_string("infotext",S("%s Has No Network"):format(machine_name))
end
else
meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name))
if to then