Need an unique number for a circuit network

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
mrvn
Smart Inserter
Smart Inserter
Posts: 5699
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Need an unique number for a circuit network

Post by mrvn »

I'm trying to send messages through the circuit network between outposts and I need something to identify a station. Currently I have 2 ideas for this:

1) constant combinator set to some unique number manually
2) place some rail, a train stop, a train and read the train ID from the train stop

Both of those can't be blueprinted.

Any other ideas how to get a unique number for each station in the network?

Muche
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Jun 02, 2017 6:20 pm
Contact:

Re: Need an unique number for a circuit network

Post by Muche »

I have this idea:
There is an outpost number coordinator circuitry that remembers the maximum number of used outpost id and broadcasts it (let's say as signal M, minimum of 1).
The outpost circuitry, when first connected to the network (i.e. M>0), reads M, remembers it forever as its ID and broadcasts other signal N=M+1.
Coordinator updates M to be N. Outpost detects this and stops transmitting N, allowing the next outpost to register itself.

I'm not sure how hard it is to implement. Also it has the race condition if two outposts were added at the same time, so some care needs to be taken when connecting them in multiplayer.

mrvn
Smart Inserter
Smart Inserter
Posts: 5699
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Need an unique number for a circuit network

Post by mrvn »

Simpler idea: Every outpost outputs a signal: M=1. M is then automatically the number of outposts. So the new one can take M+1.

Problem is when you blueprint outposts then two outposts may take their ID at the same time. They can detect this because when they take their ID and add their M=1 then M increases by more than 1. But what to do then? How to decide which one goes first?

Note: I need the unqiue ID in the outpost so that when two outposts want to post a message at the same time they can detect the collision, wait a few ticks and try again. The amount of ticks to wait will depend on the unqiue ID, wait ID % <attempt number> ticks. Since the ID is unique any set of stations will eventually wait a different amount of time and the collision can be cleared.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Need an unique number for a circuit network

Post by DaveMcW »

To prevent collisions, have each new outpost request a unique item that you only have 1 of in your network. Only assign an ID when the station has the item.

mrvn
Smart Inserter
Smart Inserter
Posts: 5699
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Need an unique number for a circuit network

Post by mrvn »

DaveMcW wrote:To prevent collisions, have each new outpost request a unique item that you only have 1 of in your network. Only assign an ID when the station has the item.
An unique item. Like a train. :) Since I'm doing this for train stations a new outpost could request a control train. Whatever station the control train arrives at picks a number and sends the train back. If two outposts are blueprinted then they each get the control train at different times.

Problem is that that needs either manually renaming stations, in which case I might just assign a number myself, or having an extra station that is only enabled once (assuming they fix that stations in blueprints can have names pre assigned).

torne
Filter Inserter
Filter Inserter
Posts: 341
Joined: Sun Jan 01, 2017 11:54 am
Contact:

Re: Need an unique number for a circuit network

Post by torne »

If you don't mind the numbers not being sequential and potentially being very large there's a simple way to do this: just build a central clock somewhere that counts ticks and outputs it on some virtual signal, and then in your outpost blueprint, build a combinator setup which will just grab whatever the current value of the clock is and remember that as the outpost ID. Unless you connect up two of the outpost combinator setups on the exact same tick these values will be unique.

If you want to keep the numbers "smaller" you could have the tick counter loop around at some maximum value - this creates the probability of collisions, though. Smaller numbers are mainly useful if they can be shown in the factorio UI though, and unfortunately if you limit the number to 0-999 it will only take about 40 outposts before you get a collision through random chance.

Muche
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Jun 02, 2017 6:20 pm
Contact:

Re: Need an unique number for a circuit network

Post by Muche »

mrvn wrote:Simpler idea: Every outpost outputs a signal: M=1. M is then automatically the number of outposts. So the new one can take M+1.
I think this causes problems when you dismantle an (older) outpost. The next new outpost detects M-1, takes M as ID, conflicting with previous outpost.

Anyways, here is my prototype:
UID prototype example with debug circuitry
UID prototype example with debug circuitry
UID prototype.jpg (197.55 KiB) Viewed 5517 times
The master is on the top left, others are slaves, along some debug lamps showing M,N and individual IDs (in binary) and constant combinators allowing manual reset of an entity. It does have the race condition being discussed above (new ID requests have to be apart at least 7 ticks. It might be possible to lower it to 6 ticks by removing N=max(N,1) combinators in the master.)
blueprint book

mrvn
Smart Inserter
Smart Inserter
Posts: 5699
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Need an unique number for a circuit network

Post by mrvn »

torne wrote:If you don't mind the numbers not being sequential and potentially being very large there's a simple way to do this: just build a central clock somewhere that counts ticks and outputs it on some virtual signal, and then in your outpost blueprint, build a combinator setup which will just grab whatever the current value of the clock is and remember that as the outpost ID. Unless you connect up two of the outpost combinator setups on the exact same tick these values will be unique.

If you want to keep the numbers "smaller" you could have the tick counter loop around at some maximum value - this creates the probability of collisions, though. Smaller numbers are mainly useful if they can be shown in the factorio UI though, and unfortunately if you limit the number to 0-999 it will only take about 40 outposts before you get a collision through random chance.
And having two outposts be build at the same tick is exactly the problem we are talking about.

torne
Filter Inserter
Filter Inserter
Posts: 341
Joined: Sun Jan 01, 2017 11:54 am
Contact:

Re: Need an unique number for a circuit network

Post by torne »

It seems incredibly unlikely that you will ever have that happen.

Muche
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Jun 02, 2017 6:20 pm
Contact:

Re: Need an unique number for a circuit network

Post by Muche »

torne wrote:It seems incredibly unlikely that you will ever have that happen.
You can make it happen by having two outposts built but not connected to the power/circuit network yet and connecting them at the same time by building one power pole. Solutions proposed so far effectively use world position to distinguish them (the one closer to the base presumably gets the item/train first).

mrvn
Smart Inserter
Smart Inserter
Posts: 5699
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Need an unique number for a circuit network

Post by mrvn »

Muche wrote:
torne wrote:It seems incredibly unlikely that you will ever have that happen.
You can make it happen by having two outposts built but not connected to the power/circuit network yet and connecting them at the same time by building one power pole. Solutions proposed so far effectively use world position to distinguish them (the one closer to the base presumably gets the item/train first).
Or a power pole is destroyed by aliens or accidentally removed and outposts forget their ID. Then when you repair the power they all try to get a new ID at the same time. Or *shiver* you have a brownout and all outposts forget their ID.

Muche
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Jun 02, 2017 6:20 pm
Contact:

Re: Need an unique number for a circuit network

Post by Muche »

mrvn wrote:
Muche wrote:
torne wrote:It seems incredibly unlikely that you will ever have that happen.
You can make it happen by having two outposts built but not connected to the power/circuit network yet and connecting them at the same time by building one power pole. Solutions proposed so far effectively use world position to distinguish them (the one closer to the base presumably gets the item/train first).
Or a power pole is destroyed by aliens or accidentally removed and outposts forget their ID. Then when you repair the power they all try to get a new ID at the same time. Or *shiver* you have a brownout and all outposts forget their ID.
During brownout combinators seem to continue to work, albeit at reduced rate (in my simulated brownout it was one update per couple of seconds).
Without power they don't update, but keep sending the last known signals.
But yeah, their direct destruction (and subsequent bot-assisted replacement) can result in unwanted behavior as well.

torne
Filter Inserter
Filter Inserter
Posts: 341
Joined: Sun Jan 01, 2017 11:54 am
Contact:

Re: Need an unique number for a circuit network

Post by torne »

Yeah, you won't lose the ID from losing power or brownouts, and if the combinator itself is destroyed then you're just back in the original situation of them being built again, where it will only be a problem if they get rebuilt on the exact same tick.

You're right that the one thing that actually makes this likely is if you build several outputs without connecting them to power, and then connect them both to power by placing a single pole, since that will certainly make the combinators activate on the same tick. That seems like an unusual way to build to me, but if that's a concern for you then sure :)

User avatar
Lav
Filter Inserter
Filter Inserter
Posts: 384
Joined: Mon Mar 27, 2017 10:12 am
Contact:

Re: Need an unique number for a circuit network

Post by Lav »

mrvn wrote:Any other ideas how to get a unique number for each station in the network?
A new item: "UID combinator". All UID combinators on the map are guaranteed to generate different values.

Or maybe rename it to "circuit signal generator" and stuff all kinds of other functionality in it, like random number generation, map coordinates generation etc.

XKnight
Filter Inserter
Filter Inserter
Posts: 329
Joined: Thu May 28, 2015 10:40 pm
Contact:

Re: Need an unique number for a circuit network

Post by XKnight »

mrvn wrote: And having two outposts be build at the same tick is exactly the problem we are talking about.
This is a very-very interesting task.
As far as I know, there is only one readable distributed system which depends on the build order (the true unique ID) - train network.
Unfortunately, they have be built manually so we can not use them to solve the initial task.

But you can use some other criteria to resolve cases when different outposts are connected to the main network at the same tick:
For example, self-linked combinator A = A + 1 with an independent power source can be used to resolve conflicts (outpost with lower A signal will capture true ID), because different outposts were built at different ticks their A-values will be unique.

Another way to achieve desired behavior is to divide the entire combinator network: instead of having one huge network you can create a tree-like network. In this tree leafs will be our outposts (possible without ID). Nodes of this tree will propagate "initializing" signal to the leftmost sub-tree with at least one uninitialized outpost. As a result, only one outpost will receive new ID and after initialization phase all outposts will have unique ID. This solution has one drawback: it will slowdown communication between outpost and base depending on the tree depth.

Also, you can resolve conflicts by having different RND generator at each outpost (with different seeds), but this is possible only if you have some natural seed signal (for example: remaining ore/repair packs count/liquid amount in tank etc).

mrvn
Smart Inserter
Smart Inserter
Posts: 5699
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Need an unique number for a circuit network

Post by mrvn »

XKnight wrote:
mrvn wrote: And having two outposts be build at the same tick is exactly the problem we are talking about.
This is a very-very interesting task.
As far as I know, there is only one readable distributed system which depends on the build order (the true unique ID) - train network.
Unfortunately, they have be built manually so we can not use them to solve the initial task.

But you can use some other criteria to resolve cases when different outposts are connected to the main network at the same tick:
For example, self-linked combinator A = A + 1 with an independent power source can be used to resolve conflicts (outpost with lower A signal will capture true ID), because different outposts were built at different ticks their A-values will be unique.

Another way to achieve desired behavior is to divide the entire combinator network: instead of having one huge network you can create a tree-like network. In this tree leafs will be our outposts (possible without ID). Nodes of this tree will propagate "initializing" signal to the leftmost sub-tree with at least one uninitialized outpost. As a result, only one outpost will receive new ID and after initialization phase all outposts will have unique ID. This solution has one drawback: it will slowdown communication between outpost and base depending on the tree depth.

Also, you can resolve conflicts by having different RND generator at each outpost (with different seeds), but this is possible only if you have some natural seed signal (for example: remaining ore/repair packs count/liquid amount in tank etc).
You are ignoring the problem of 2 stations coming online in the same tick. As mentioned before this can easily happen, e.g. when you build an outpost with 3 stations (for example 2 item inputs, one output) and then go and connect the power all 3 stations come online the same tick.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Need an unique number for a circuit network

Post by darkfrei »

Lav wrote:
mrvn wrote:Any other ideas how to get a unique number for each station in the network?
A new item: "UID combinator". All UID combinators on the map are guaranteed to generate different values.

Or maybe rename it to "circuit signal generator" and stuff all kinds of other functionality in it, like random number generation, map coordinates generation etc.
It can be something like this:
UniqueSignal

mrvn
Smart Inserter
Smart Inserter
Posts: 5699
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Need an unique number for a circuit network

Post by mrvn »

darkfrei wrote:
Lav wrote:
mrvn wrote:Any other ideas how to get a unique number for each station in the network?
A new item: "UID combinator". All UID combinators on the map are guaranteed to generate different values.

Or maybe rename it to "circuit signal generator" and stuff all kinds of other functionality in it, like random number generation, map coordinates generation etc.
It can be something like this:
UniqueSignal
What number do you output? Map coordinates or some entity ID likes trains do?

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Need an unique number for a circuit network

Post by darkfrei »

mrvn wrote:
darkfrei wrote: It can be something like this:
UniqueSignal
What number do you output? Map coordinates or some entity ID likes trains do?
Just n = n+1

Post Reply

Return to “Gameplay Help”