Power Network Fixes

Fix power distribution so its spread evenly across all machines
Fix glitch when removing machines from network other machines can stop working
This commit is contained in:
Sirrobzeroone 2021-06-12 13:47:52 +10:00
parent b9ca4fc778
commit b9de6b6ef6
3 changed files with 83 additions and 28 deletions

View File

@ -4,6 +4,7 @@ ele.register_conduit("elepower_dynamics:conduit", {
description = "Power Conduit", description = "Power Conduit",
tiles = {"elepower_conduit.png"}, tiles = {"elepower_conduit.png"},
use_texture_alpha = "clip", use_texture_alpha = "clip",
ele_conductor_density = 1/8,
groups = {oddly_breakable_by_hand = 1, cracky = 1} groups = {oddly_breakable_by_hand = 1, cracky = 1}
}) })

View File

@ -257,8 +257,8 @@ function ele.register_base_device(nodename, nodedef)
-- Override destruct callback -- Override destruct callback
local original_after_destruct = nodedef.after_destruct local original_after_destruct = nodedef.after_destruct
nodedef.after_destruct = function (pos) nodedef.after_destruct = function (pos)
ele.clear_networks(pos)
ele.clear_networks(pos)
if original_after_destruct then if original_after_destruct then
original_after_destruct(pos) original_after_destruct(pos)
end end

View File

@ -139,6 +139,7 @@ local function give_node_power(pos, available)
local capacity = ele.helpers.get_node_property(user_meta, pos, "capacity") local capacity = ele.helpers.get_node_property(user_meta, pos, "capacity")
local inrush = ele.helpers.get_node_property(user_meta, pos, "inrush") local inrush = ele.helpers.get_node_property(user_meta, pos, "inrush")
local storage = user_meta:get_int("storage") local storage = user_meta:get_int("storage")
local want = capacity - storage
local total_add = 0 local total_add = 0
@ -157,9 +158,10 @@ local function give_node_power(pos, available)
storage = capacity storage = capacity
end end
return total_add, storage return total_add, storage, want
end end
minetest.register_abm({ minetest.register_abm({
nodenames = {"group:ele_provider"}, nodenames = {"group:ele_provider"},
label = "elepower Power Transfer Tick", label = "elepower Power Transfer Tick",
@ -218,32 +220,73 @@ minetest.register_abm({
if p_output and pw_storage >= p_output then if p_output and pw_storage >= p_output then
pw_supply = pw_supply + p_output pw_supply = pw_supply + p_output
elseif p_output and pw_storage < p_output then elseif p_output and pw_storage < p_output then
pw_supply = pw_supply + pw_storage pw_supply = pw_supply + pw_storage
end end
end end
-- Give power to users -- Give power to users
for _,ndv in ipairs(users) do local divide_power = {}
if pw_demand > pw_supply then
break for _,ndv in ipairs(users) do --ndv = pos table
-- Check how much power a node wants and can get ie is it close to full charge
local user_gets, user_storage, user_want = give_node_power(ndv, (pw_supply - pw_demand))
-- Add the node_users wanting power to table for later power division
if user_gets > 0 then
table.insert(divide_power,{pos = ndv,user_gets = user_gets, user_storage = user_storage})
end
end
-- The below shares avaliable power from a network between node_users
-- Only whole numbers are accepted so any remainders are added to
-- the first few node_users. If divided power is less than 1 the
-- network is overloaded and delivers no power to any nodes.
-- A node_user can recieve power from two different networks
-- if pw_supply ~= 0 then minetest.debug(node.name.." - Power Supplied: "..pw_supply) end --debug line
local num_users = #divide_power
local div_pwr = pw_supply/num_users
local whole_pwr_num = math.floor(div_pwr)
local remainder_pwr_num = (math.fmod(div_pwr,1))*num_users
if div_pwr < 1 then
num_users = 0 -- network overload
end
local i = 1
while(num_users >= i)do
local final_pwr_num
if remainder_pwr_num > 0.5 then
final_pwr_num = whole_pwr_num + 1
remainder_pwr_num = remainder_pwr_num - 1
else
final_pwr_num = whole_pwr_num
end end
-- Sharing: Determine how much each user gets if final_pwr_num > divide_power[i].user_gets then
local user_gets, user_storage = give_node_power(ndv, (pw_supply - pw_demand)) final_pwr_num = divide_power[i].user_gets
pw_demand = pw_demand + user_gets
if user_gets > 0 then
local user_meta = minetest.get_meta(ndv)
user_meta:set_int("storage", user_storage + user_gets)
-- Set timer on this node
ele.helpers.start_timer(ndv)
end end
--minetest.debug("node_user "..minetest.pos_to_string(divide_power[i].pos).." Power Supplied:"..final_pwr_num) -- debug line
local user_meta = minetest.get_meta(divide_power[i].pos)
user_meta:set_int("storage", divide_power[i].user_storage + final_pwr_num)
pw_demand = pw_demand + final_pwr_num
ele.helpers.start_timer(divide_power[i].pos)
i = i+1
end end
-- Take the power from provider nodes -- Take the power from provider nodes
if pw_demand > 0 then if pw_demand > 0 then
for _, spos in ipairs(providers) do for _, spos in ipairs(providers) do
if pw_demand == 0 then break end if pw_demand == 0 then break end
local smeta = minetest.get_meta(spos) local smeta = minetest.get_meta(spos)
@ -260,9 +303,13 @@ minetest.register_abm({
ele.helpers.start_timer(spos) ele.helpers.start_timer(spos)
end end
end end
--if pw_supply ~= 0 then minetest.debug("end_run") end -- debug line
end, end,
}) })
------------------------
-- Network Add/Remove --
------------------------
local function check_connections(pos) local function check_connections(pos)
local connections = {} local connections = {}
local positions = { local positions = {
@ -290,9 +337,12 @@ function ele.clear_networks(pos)
local placed = name ~= "air" local placed = name ~= "air"
local positions = check_connections(pos) local positions = check_connections(pos)
if #positions < 1 then return end if #positions < 1 then return end
local hash_pos = minetest.hash_node_position(pos) local hash_pos = minetest.hash_node_position(pos)
local dead_end = #positions == 1 local dead_end = #positions == 1
for _,connected_pos in ipairs(positions) do for _,connected_pos in ipairs(positions) do
local networks = ele.graphcache.devices[minetest.hash_node_position(connected_pos)] or local networks = ele.graphcache.devices[minetest.hash_node_position(connected_pos)] or
{minetest.pos_to_string(connected_pos)} {minetest.pos_to_string(connected_pos)}
@ -344,6 +394,7 @@ function ele.clear_networks(pos)
local network_ids = ele.graphcache.devices[minetest.hash_node_position(positions[1])] or local network_ids = ele.graphcache.devices[minetest.hash_node_position(positions[1])] or
{minetest.pos_to_string(positions[1])} {minetest.pos_to_string(positions[1])}
if not #network_ids then if not #network_ids then
-- We're evidently not on a network, nothing to remove ourselves from -- We're evidently not on a network, nothing to remove ourselves from
break break
@ -360,14 +411,17 @@ function ele.clear_networks(pos)
ele.graphcache.devices[pos1] = nil ele.graphcache.devices[pos1] = nil
end end
ele.graphcache[int_net] = nil ele.graphcache[int_net] = nil
else else
-- Search for and remove device -- Search for and remove device
-- This checks and removes from network.users,
-- network.conductors and network.providers
ele.graphcache.devices[hash_pos] = nil ele.graphcache.devices[hash_pos] = nil
for tblname, table in pairs(network) do for tblname, tables in pairs(network) do
if type(table) == "table" then if type(tables) == "table" then
for devicenum, device in pairs(table) do for devicenum, device in pairs(tables) do
if vector.equals(device, pos) then if vector.equals(device, pos) then
table[devicenum] = nil table.remove(tables,devicenum)
end end
end end
end end