Skip to content

Commit

Permalink
Propagate electricity transfers across servers
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielv123 committed Sep 7, 2024
1 parent 16431ec commit bca5b66
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions module/edge/power_link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,44 @@ local function poll_links(id, edge, ticks_left)
end
::continue::
end

-- Balance links in the same power network
local networks = {}
for _, edge in pairs(global.universal_edges.edges) do

Check failure on line 65 in module/edge/power_link.lua

View workflow job for this annotation

GitHub Actions / Luacheck

(W422) shadowing definition of argument edge on line 7
if not edge.linked_power then
goto continue
end
for _offset, link in pairs(edge.linked_power) do
if not link then
log("FATAL: Received power for non-existant link at offset " .. link.offset)
goto continue2
end
if not link.eei then
log("FATAL: received power for a link that does not have an eei " .. link.offset)
goto continue2
end
if link.eei.valid then
local network = link.eei.electric_network_id
if not networks[network] then
networks[network] = {}
end
networks[network][#networks[network] + 1] = link
end
::continue2::
end
::continue::
end
for _id, network in pairs(networks) do
local total_energy = 0
for _, link in pairs(network) do
total_energy = total_energy + link.eei.energy + (link.lua_buffered_energy or 0)
end
local average_energy = total_energy / #network
for _, link in pairs(network) do
link.eei.electric_buffer_size = math.max(link.eei.electric_buffer_size, link.eei.energy + (link.lua_buffered_energy or 0), average_energy)
link.eei.energy = average_energy
end
end
end

local function receive_transfers(edge, power_transfers)
Expand Down

0 comments on commit bca5b66

Please sign in to comment.