How does electric wiring work?

Place to get help with not working mods / modding interface.
Post Reply
Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

How does electric wiring work?

Post by Pi-C »

In Bio Industries, we have powered rails that allow you to transfer power along a railroad track. This is what I do to set them up:
  • When powered rail is placed, create a hidden electric pole at the same position.
  • Store rail and pole in a table in global, indexed by the rail's unit_number.
  • Use pole.disconnect_neighbour() to remove all connections from the pole -- they shouldn't connect to random poles in the vicinity.
  • Check for powered rails going left, straight, or right on the rail tracks back side. If there is another powered rail, get its hidden pole and make a connection.
  • Do the same thing for the front side of the current rail.
This way, two tracks running side by side should be two isolated electric-networks. Rails crossing each other should be isolated from each other (except you build a loop so the rail crosses itself).

Now I've noticed that this does not always work:
crossing.png
crossing.png (3.84 MiB) Viewed 2842 times
The screenshot (with hidden poles revealed for debugging) shows that the two vertical tracks are isolated networks (ID 11 and 12). The lower horizontal track (ID 10) crosses both vertical tracks without making a connection. However, the upper horizontal track (ID 11 as well) somehow connects to the left vertical track (but is isolated from the right vertical track).

I suppose I don't quite understand how electric connections really work. The hidden poles have an empty collision mask, so they can be placed in the same position. Also, I make connections to distinct entities, even though these entities may be in the same spot. Is that correct, or will two wires that end at the same position (even if on different poles) be automatically connected? Or is there anything else that I've just overlooked so far?
Last edited by Pi-C on Fri Feb 05, 2021 4:03 pm, edited 1 time in total.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Xorimuth
Filter Inserter
Filter Inserter
Posts: 625
Joined: Sat Mar 02, 2019 9:39 pm
Contact:

Re: How does electric wiring work?

Post by Xorimuth »

Your screenshot did not work :)
My mods
Content: Freight Forwarding | Spidertron Patrols | Spidertron Enhancements | Power Overload
QoL: Factory Search | Remote Configuration | Module Inserter Simplified | Wire Shortcuts X | Ghost Warnings

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How does electric wiring work?

Post by Pi-C »

Xorimuth wrote:
Fri Feb 05, 2021 3:46 pm
Your screenshot did not work :)
What do you mean, can't you see it? It's visible to me even though I'm on another computer now …
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Xorimuth
Filter Inserter
Filter Inserter
Posts: 625
Joined: Sat Mar 02, 2019 9:39 pm
Contact:

Re: How does electric wiring work?

Post by Xorimuth »

Pi-C wrote:
Fri Feb 05, 2021 3:59 pm
Xorimuth wrote:
Fri Feb 05, 2021 3:46 pm
Your screenshot did not work :)
What do you mean, can't you see it? It's visible to me even though I'm on another computer now …
No worries, its working now... the source html has changed since, so no idea what happened there!
My mods
Content: Freight Forwarding | Spidertron Patrols | Spidertron Enhancements | Power Overload
QoL: Factory Search | Remote Configuration | Module Inserter Simplified | Wire Shortcuts X | Ghost Warnings

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How does electric wiring work?

Post by Pi-C »

@Xorimuth: I replaced the link to the image in the original post with the actual file. I hope that's what you meant. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
ptx0
Smart Inserter
Smart Inserter
Posts: 1507
Joined: Wed Jan 01, 2020 7:16 pm
Contact:

Re: How does electric wiring work?

Post by ptx0 »

fwiw the way you've implemented this, destroys UPS by raising the entity update time on thousands of newly active chunks.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How does electric wiring work?

Post by Pi-C »

ptx0 wrote:
Fri Feb 05, 2021 8:49 pm
fwiw the way you've implemented this, destroys UPS by raising the entity update time on thousands of newly active chunks.
Could you suggest something better? The problem is, the poles need to have a wire reach of 9 tiles because the poles are farther apart when curved rails are placed. But 9 tiles is a lot! Not only would the hidden poles connect to other poles, they would also connect to more hidden poles than just those on the neighboring rails. Before I implemented this, things were quite messy:
Image
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
ptx0
Smart Inserter
Smart Inserter
Posts: 1507
Joined: Wed Jan 01, 2020 7:16 pm
Contact:

Re: How does electric wiring work?

Post by ptx0 »

Pi-C wrote:
Fri Feb 05, 2021 9:11 pm
Could you suggest something better? The problem is, the poles need to have a wire reach of 9 tiles because the poles are farther apart when curved rails are placed.
unfortunately i'm not really sure why it's so bad, just that the removal of those fixed the problem for some reporters.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How does electric wiring work?

Post by Pi-C »

ptx0 wrote:
Fri Feb 05, 2021 9:23 pm
unfortunately i'm not really sure why it's so bad
Could be because there are more electric-power networks the game has to deal with …
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
ptx0
Smart Inserter
Smart Inserter
Posts: 1507
Joined: Wed Jan 01, 2020 7:16 pm
Contact:

Re: How does electric wiring work?

Post by ptx0 »

Pi-C wrote:
Fri Feb 05, 2021 9:34 pm
ptx0 wrote:
Fri Feb 05, 2021 9:23 pm
unfortunately i'm not really sure why it's so bad
Could be because there are more electric-power networks the game has to deal with …
https://mulark.github.io/tests/test-000 ... 00013.html

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: How does electric wiring work?

Post by PFQNiet »

Well personally I wouldn't let a difference between 4.6 and 4.7ms to be an obstacle to this really cool idea :D

It would be nice to get some dev confirmation but it does seem like connections are somehow position-based. What if you offset the pole by half a tile in the direction of the rail? You'll need "placeable-off-grid", of course, but this would avoid poles from crossed lines from being in the same position.

User avatar
ptx0
Smart Inserter
Smart Inserter
Posts: 1507
Joined: Wed Jan 01, 2020 7:16 pm
Contact:

Re: How does electric wiring work?

Post by ptx0 »

PFQNiet wrote:
Sat Feb 06, 2021 12:26 am
Well personally I wouldn't let a difference between 4.6 and 4.7ms to be an obstacle to this really cool idea :D
that's with just 32 networks.

at something like 2,200 or whatever one player experienced, their UPS dropped to the 30s.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How does electric wiring work?

Post by Pi-C »

PFQNiet wrote:
Sat Feb 06, 2021 12:26 am
Well personally I wouldn't let a difference between 4.6 and 4.7ms to be an obstacle to this really cool idea :D
:-)
It would be nice to get some dev confirmation but it does seem like connections are somehow position-based. What if you offset the pole by half a tile in the direction of the rail? You'll need "placeable-off-grid", of course, but this would avoid poles from crossed lines from being in the same position.
Alll my hidden entities have these flags:

Code: Select all

BI.hidden_entities.flags = {  "hidden",  "hide-alt-info",  "no-copy-paste",  "not-blueprintable",  "not-deconstructable",  "not-flammable",  "not-in-kill-statistics",
  "not-on-map",  "not-repairable",  "not-selectable-in-game",  "not-upgradable",  "placeable-off-grid" }
Using an offset isn't practical, though. The screenshot in the original post shows just the simple case of using straight rails. But we also have curved and diagonal rails:
curved_diagonal.png
curved_diagonal.png (3.28 MiB) Viewed 2712 times
So I'd probably have to take direction into account when placing the poles, which makes things more complicated. (I may have to do that anyways -- still trying to figure out a better way to make connections to the rail-to-power connectors, the medium poles shown in the image. Would be nice if the connectors could be shown as a kind of connector box attached to the rails, or connectors in the middle of the rails.)

I've had the idea to skip the poles on crossings:
  • Before a powered rail is placed, look for existing powered rails at that position.
  • Place rail and hidden pole, store both in the global table.
  • Disconnect the pole.
  • If there was another rail, this is a crossing. Add a "disconnect" flag to the pole's table entry.
  • If the pole doesn't have the flag, try to connect it to the poles on neighboring rails.
  • If a neighbor has the "disconnect" flag, check if it has other neighbors at the other end that could be connected.
  • Do that until a pole without the flag has been found, or until the distance between the poles is larger than the poles' wire reach.
Thus, I could leave a gap when two tracks are crossing, so poles wouldn't end up at the same position. But this would make placing the rails more expensive -- and there could be problems if there are more than two tracks adjacent to each other, with no space between the rails. (Granted, that's not practical because there would be no space to place signals. But there may be odd settings where no signals are needed, e.g. as a defensive perimeter where heavy trains are continuously waste fuel in order to squash biters.) If there were enough tracks, the distance that has to be bridged could exceed the maximum wire reach, and then the connection would be broken.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How does electric wiring work?

Post by Pi-C »

ptx0 wrote:
Sat Feb 06, 2021 5:28 am
PFQNiet wrote:
Sat Feb 06, 2021 12:26 am
Well personally I wouldn't let a difference between 4.6 and 4.7ms to be an obstacle to this really cool idea :D
that's with just 32 networks.

at something like 2,200 or whatever one player experienced, their UPS dropped to the 30s.
There is something players can do against that UPS drop: Connect different networks by placing power-to-rail connectors at a position where they can connect to both tracks! This way, they could still have isolated networks where it's necessary while being safe from accidentally hooking up the hidden poles to random other poles. I guess we should point that out on our info page …
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
ptx0
Smart Inserter
Smart Inserter
Posts: 1507
Joined: Wed Jan 01, 2020 7:16 pm
Contact:

Re: How does electric wiring work?

Post by ptx0 »

Pi-C wrote:
Sat Feb 06, 2021 10:58 am
ptx0 wrote:
Sat Feb 06, 2021 5:28 am
PFQNiet wrote:
Sat Feb 06, 2021 12:26 am
Well personally I wouldn't let a difference between 4.6 and 4.7ms to be an obstacle to this really cool idea :D
that's with just 32 networks.

at something like 2,200 or whatever one player experienced, their UPS dropped to the 30s.
There is something players can do against that UPS drop: Connect different networks by placing power-to-rail connectors at a position where they can connect to both tracks! This way, they could still have isolated networks where it's necessary while being safe from accidentally hooking up the hidden poles to random other poles. I guess we should point that out on our info page …
interesting! then i guess you can monitor the count of your managed networks and let the user know via game.print() (optionally? via map settings?)

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How does electric wiring work?

Post by Pi-C »

ptx0 wrote:
Mon Feb 08, 2021 2:27 pm
interesting! then i guess you can monitor the count of your managed networks and let the user know via game.print() (optionally? via map settings?)
I don't think that's really practicable. The network ID changes whenever a new connection is made or an old connection dropped. I probably could keep tabs on the special power-to-rail connector poles and see how many different networks they are connected to. But there is no such event as on_electric_connection_changed (for a good enough reason, because that would be triggered all the time). Checking when a connector is placed would only work IF a connector is placed -- so it would be of no use to players who don't know about it already. Checking when a powered rail is placed is also out of the question because usually you place a lot of rails at a time, so that probably would slow down things too much.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Modding help”