If you are not familiar with this idea, please read that article before this one
viewtopic.php?f=80&t=15546 Cable Resistance / Non-looseless Power Transmission
I thought a bit and have found this solution:
1. We need to split the power (Watts) between Voltage and Ampere. A first simple step would be to have a standard Voltage of 1000 Volts. So the produced Amperes you need to generate is just the current power divided with 1000.
2. The calculation of the total power usage now depends on the needed power of each entity divided by Voltage level at the entities place. ... Or something like that. The principle is, that the total power depends on total voltage levels: The higher it is, the lower the needed power. You see: You don't need to go into the details it's enough to calculate an average power level for each device type. This is important for performance!
3. In the ideal case, that there is no power used, the network will have everywhere 1000 Volts. This is also the level the network tries to "fall back" to.
4. We need to calculate Voltage-Levels in the network. This is done by sending "random packets" through the network, like a token.
The algorithm to generate this regeneration will look like so:
a) Randomly choose one pair of a generator and a consumer by chance, depending on it's level of production/consumption. In other words: An inserter or solar panel has a much lesser chance to be picked than a turbine or laser tower (while fireing).
b) calculate the "shortest" path from producer to consumer. This path calculation will take into amount the existing voltage levels of former calculations: If a path has a lower voltage a path around this is eventually "shorter".
c) calculate the amount of amperes (the siye of the package) needed to supply the destination depending on voltage levels between source and destination. This takes into account the amount of already calculated Voltage decrease from previous calculations.
d) Each cable (and/or pole) on the way from consumer to producer will reduce the Voltage on the network a bit, depending on the package size.
For example: If a package size is 10 Amperes, the voltage level of the wires this package needs to be going through is decreased by 10 Volts.
5. Now go through the whole electric network and increase the voltage logarithmic in very small steps back to 1000 Volts.
6. The power consumption is not dependent on this calculations. It will take some while to "weight out" a network, until it doesn't change anymore. This inertia can be explained with "cable temperature".
That's it.
Example:
Step 0
In step 0 the network will look like so:
Code: Select all
[Generator - 1000 MW] ---- 1000 V ---- [Consumer A - 10 MW] --- 1000 V --- [Consumer B - 500 MW]
\ /
\-------------------- 1000 V --------------------/
Step 1
Calculating the chances for a pair of the radom package:
Generator -> Consumer A: 1000 + 10 = 1010
Generator -> Consumer B: 1000 + 500 = 1500
Average:
1010 + 1500 = 2510; Average is 2510 / 2 = 1255
Chance of pairs:
A) Generator -> A: 1010/1255 = 0.8
B) Generator -> B: 1500/1255 = 1.2
Randomly picking path A.
Calculating path:
Generator -> A: 1000
Generator -> B -> A: 2000 / 2 (two cables) = 1000
(just adding voltages as path-length and divide through number of cables it runs through)
In this case both path are equal so it takes the first (G->A).
Calculating of package size: 10 MW / 1000 V = 10000 Ampere
Let"s say 1000 Ampere reduce voltage by 1, then the network will look in this next step like so:
Code: Select all
[Generator - 1000 MW] ---- 990 V ---- [Consumer A - 10 MW] --- 1000 V --- [Consumer B - 500 MW]
\ /
\-------------------- 1000 V --------------------/
Now it randomly chooses path B and picks the lower cable. At the same time the upper path will be increased back to 1000 by 1 Volt (this is just a number to calculate easier):
Needed power is 5000000 Ampere -> Reduction of 500 Volts.
Code: Select all
[Generator - 1000 MW] ---- 991 V ---- [Consumer A - 10 MW] --- 1000 V --- [Consumer B - 500 MW]
\ /
\-------------------- 500 V --------------------/
Step 3:
Now things get complicated to follow (the algorithm does not change):
The randomly choosen pair is G->B again.
Calculating path:
Generator -> B: 500
Generator -> A -> B: (991 + 1000) / 2 = 995.5
Will take now path G->A->B!
Needed power is now higher: 500MW / 995.5 V = 502260.2 Ampere -> Reduction of 502 Volts
Code: Select all
[Generator - 1000 MW] ---- 491 V ---- [Consumer A - 10 MW] --- 498 V --- [Consumer B - 500 MW]
\ /
\-------------------- 501 V --------------------/
Chosen pair is G->A
Calculating path:
Generator -> A: 491
Generator -> B -> A: (491+498) / 2 = 494.5
Will take now path G->B->A!
Needed power: 10MW / 494.5 V = 20222.5 Ampere -> Reduction of 20 Volts
Code: Select all
[Generator - 1000 MW] ---- 492 V ---- [Consumer A - 10 MW] --- 478 V --- [Consumer B - 500 MW]
\ /
\-------------------- 481 V --------------------/
Chosen pair is G->B
Calculating path:
Generator -> B: 492
Generator -> B -> A: (481+478) / 2 = 479.5
Will take now path G->B!
Needed power: 500MW / 492 V => Reduction of 1016 Volts.... ok, that is the equivalent of an electric shortage!
Here this algorithm needs obviously some kind of balancing. I will end here, cause I think you can see, how that can work.
What you see is this:
- I have choose a too big reduction and to slow rebound! --> This is part of balancing.
- Calculation is not needed for every tick! With a large network I think it is enough to calculate one pair per second, especially, cause a larger network will not change so fast as with a small network.
- Indeed I think the amount needed for calculation to guarantee a nice gameplay is only dependend on the amount of changes that is done to the networks: I think the time depends on the sum of all devices in all Networks and rises of course with the number of paths on which a device is connected.
- To increase speed more I think, that it is eventually not needed to calculate always the shortest path (which needs to calculate every possible path), but just one more or less random path; half random, half calculated for example.
- The amount of power needed for things more far away depends on the number of cables inbetween. The number of connections will add in sum a bigger resistance than a short connection, which drains more power then. Which is what we want.
- The resistance reduces, if less power is transported (cause not enough power available). Which is good: If you are near a power failure the network "recovers" in that case. Interesting effects can happen.
- This is also the case, if you turn parts of the factory off: Cables "recover" and can have suddenly much lesser power reduction. That gives a player more time.
- A device should turn of, if the voltage goes below some amount to enable recovering. This is also quite beginner friendly, cause you will see very early random power failures. Maybe you can configure this level per network?