Detecting Electicity Disconnects - first time modder

Place to get help with not working mods / modding interface.
modlearner234
Manual Inserter
Manual Inserter
Posts: 1
Joined: Wed Aug 17, 2022 10:20 am
Contact:

Detecting Electicity Disconnects - first time modder

Post by modlearner234 »

Hi. First time modding factorio.

I'm trying to detect accidentally power disconnects when a player removes an electric pole.

My approach:
  1. For each power_pole in LuaEntity.neighbours['copper']:
  2. Check power_pole.electric_network_statistics
  3. If consumption >> production, raise a warning/notification
Some challenges:
  1. Entities don't seem to appear on electric_network_statistics unless they have been connected/working at some point. E.g. the 'demand/consumption' of never-powered insterters does not appear
  2. I have to record the neighbors on_pre_player_mined_item, but wait until the pole is actually removed and then maybe 1 more tick for the actual power statistics to update. This feels clumsy.
  3. The electric_network_id seems to change for the entire network anytime a pole is added. This makes it hard to keep track of all the global power networks in my own table, their lifetimes, and check for recent changes. For example, it would be useful to exclude new power networks with age < 5 mins (or some configurable setting) to avoid alerting for things the player is currently building.

I am looking for feedback on the overall approach, or alternative ways of doing this that I might be overlooking. Any thoughts?
BicycleEater
Fast Inserter
Fast Inserter
Posts: 153
Joined: Sun Jul 26, 2020 4:05 pm
Contact:

Re: Detecting Electicity Disconnects - first time modder

Post by BicycleEater »

This does sound interesting, but you're going to have a few interesting problems.

The limiting factor is performance impact.

I would guess the best would be to do what you've been doing so far, and triggering on_pre_player_mined_item (and on_robot_mined_entity), take the entities which are connected and record them, then 30s later (or indeed an amount in mod settings), if the entities are in different networks, raise the alert. This would allow issues during construction to be much less annoying, allow you to check the networks, etc.
Storing the LuaEntity's and then checking the equality of the electric_network_id avoids the statistics updating, and is probably much faster.
Post Reply

Return to “Modding help”