How to read the available power in a given pole's network?

Place to get help with not working mods / modding interface.
User avatar
Maoman
Inserter
Inserter
Posts: 37
Joined: Sat Nov 12, 2016 10:55 am
Contact:

How to read the available power in a given pole's network?

Post by Maoman »

I've already figured out how to scan for the nearest power pole to the player, but I can not for the life of me figure out how to get an actual power value from it. I don't see anything about it in the API or anywhere. Please tell me this isn't inaccessible engine level code?
HunD34TH
Inserter
Inserter
Posts: 28
Joined: Mon Apr 02, 2018 3:16 pm
Contact:

Re: How to read the available power in a given pole's network?

Post by HunD34TH »

viewtopic.php?t=60810 (The link is broken in the thread)

LuaEntity::electric_network_statistics :: Read LuaFlowStatistics
The electric network statistics for this electric pole.
Can only be used if this is ElectricPole

https://lua-api.factorio.com/latest/cla ... stics.html
User avatar
Maoman
Inserter
Inserter
Posts: 37
Joined: Sat Nov 12, 2016 10:55 am
Contact:

Re: How to read the available power in a given pole's network?

Post by Maoman »

I saw that in the API. As far as I can tell, it is no help. This is what I eventually came up with. It's not guaranteed but it should be accurate in like 99% of cases.

Code: Select all

local function is_pole_probably_powered(pole)
    local stats = pole.electric_network_statistics
    if not stats then return false end

    -- If energy is flowing in or out, network is alive
    local flow_in, flow_out = 0, 0
    for _, v in pairs(stats.input_counts) do flow_in = flow_in + v end
    for _, v in pairs(stats.output_counts) do flow_out = flow_out + v end
    if flow_in > 0 or flow_out > 0 then
        return true -- Power is moving
    end

    local radius = 7.5 -- max wire connection range for vanilla medium pole
    local accumulators = pole.surface.find_entities_filtered{
        area = {
            {pole.position.x - radius, pole.position.y - radius},
            {pole.position.x + radius, pole.position.y + radius}
        },
        type = "accumulator"
    }
    for _, acc in pairs(accumulators) do
        if acc.energy and acc.energy > 1 then
            return true -- At least one accumulator has charge
        end
    end

    -- No evidence of power
    return false
end
Basically

Code: Select all

if power_was_flowing_recently or any_accumulator_has_charge then
  return true -- probably powered
else
  return false -- probably not powered
Post Reply

Return to “Modding help”