The best way I can think of doing it is with Godot’s built-in AStar class. I’m not certain, but I’d guess my way is also faster than yours by quite a bit.
Start with AStar’s empty point array.
Add the point of the power source.
For every wire location x, add a new aStar point.
Look at points on your grid adjacent to x, if there are points or “wires” there, it’s AStar’s connect points function. This connects wires that are together on the grid.
When placing an object O that requires power, take the following steps:
Use Astar’s function get_closest_point(O.location).
If the closest AStar point is adjacent, ask AStar to make a path from that point to any of your power suppliers.
If it can’t make a path to any of them, it gets no power, else it does. This way you can also find out where it’s drawing power and subtract power appropriately like how in prison architect the power supply has limits.
Keep object O powered every frame.
When a point is added or removed from the power line grid, ask all power-needing objects to re-evaluate their connection to a power supply.
Using AStar has the added bonus that you can disable points from being used to form paths, meaning every point already has the functionality of being a switch. So, for example, if you wanted the wire to quit working b/c it was damaged or something, you could just disable that point.
As far as I know the only downside is that AStar doesn’t seem to be multi-thread safe. Also. I LOVE those games you listed sooo much. Probably 2000+ hours combined in those three.