Page 1 of 1

Need an unique number for a circuit network

Posted: Mon Jun 12, 2017 1:05 pm
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?

Re: Need an unique number for a circuit network

Posted: Mon Jun 12, 2017 2:34 pm
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.

Re: Need an unique number for a circuit network

Posted: Tue Jun 13, 2017 9:20 am
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.

Re: Need an unique number for a circuit network

Posted: Tue Jun 13, 2017 9:35 am
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.

Re: Need an unique number for a circuit network

Posted: Tue Jun 13, 2017 12:17 pm
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).

Re: Need an unique number for a circuit network

Posted: Tue Jun 13, 2017 2:28 pm
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.

Re: Need an unique number for a circuit network

Posted: Tue Jun 13, 2017 4:32 pm
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 5698 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

Re: Need an unique number for a circuit network

Posted: Fri Jun 16, 2017 9:50 am
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.

Re: Need an unique number for a circuit network

Posted: Fri Jun 16, 2017 4:47 pm
by torne
It seems incredibly unlikely that you will ever have that happen.

Re: Need an unique number for a circuit network

Posted: Fri Jun 16, 2017 10:27 pm
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).

Re: Need an unique number for a circuit network

Posted: Thu Jun 22, 2017 11:59 am
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.

Re: Need an unique number for a circuit network

Posted: Thu Jun 22, 2017 1:02 pm
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.

Re: Need an unique number for a circuit network

Posted: Thu Jun 22, 2017 3:33 pm
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 :)

Re: Need an unique number for a circuit network

Posted: Fri Jun 23, 2017 6:57 am
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.

Re: Need an unique number for a circuit network

Posted: Fri Jun 23, 2017 6:45 pm
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).

Re: Need an unique number for a circuit network

Posted: Mon Jun 26, 2017 9:33 am
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.

Re: Need an unique number for a circuit network

Posted: Mon Jun 26, 2017 10:15 am
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

Re: Need an unique number for a circuit network

Posted: Mon Jun 26, 2017 10:56 am
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?

Re: Need an unique number for a circuit network

Posted: Mon Jun 26, 2017 11:53 am
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