Calculate energy share relative to node maximum

- Network nodes don’t have a energy limit of the weakest node
This commit is contained in:
Valentin Anger 2017-08-21 15:02:37 +02:00
parent 098204aca0
commit a526fc56ea

View File

@ -1,3 +1,11 @@
local function round(x)
if x >= 0 then
return math.floor(x+0.5)
else
math.ceil(x-0.5)
end
end
-- internal var net_master stores all slave nodes -- internal var net_master stores all slave nodes
-- sparktech_net_passive = are used to follow nets, do not count towards energy -- sparktech_net_passive = are used to follow nets, do not count towards energy
-- sparktech_net_trigger = trigger updates -- sparktech_net_trigger = trigger updates
@ -13,7 +21,7 @@ local function net_get_slaves(master)
local cor = string.split(val, ":") local cor = string.split(val, ":")
slaves[#slaves + 1] = { x=tonumber(cor[1]), y=tonumber(cor[2]), z=tonumber(cor[3])} slaves[#slaves + 1] = { x=tonumber(cor[1]), y=tonumber(cor[2]), z=tonumber(cor[3])}
end end
minetest.debug("slaves " .. dump2(slaves)) --minetest.debug("slaves " .. dump2(slaves))
return slaves return slaves
end end
@ -22,16 +30,21 @@ local function net_distribute(pos)
nodes[#nodes +1] = pos nodes[#nodes +1] = pos
-- all nodes are in local now -- all nodes are in local now
local energy = 0 local energy = 0
local nodecount = #nodes for x = 1, #nodes do
for x = 1, nodecount do
energy = energy + minetest.get_meta(nodes[x]):get_int("energy") energy = energy + minetest.get_meta(nodes[x]):get_int("energy")
end end
local max_energy = 0
for x = 1, #nodes do
max_energy = max_energy + minetest.get_item_group(minetest.get_node(nodes[x]).name, "sparktech_energy_max")
end
-- now we know all energy in the system, now to distribute it -- now we know all energy in the system, now to distribute it
local share = energy / nodecount; local share = energy / max_energy;
for x = 1, nodecount do for x = 1, #nodes do
minetest.get_meta(nodes[x]):set_int("energy",share) local meta = minetest.get_meta(nodes[x])
local node = minetest.get_node(nodes[x])
meta:set_int("energy", round(share * minetest.get_item_group(node.name, "sparktech_energy_max")))
end end
end end
@ -55,4 +68,4 @@ minetest.register_abm({
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
filter(pos) filter(pos)
end end
}) })