Natural Tree Expansion

by aster26

Aliens reproduce, why trees don't?

8 years ago
0.13 - 0.14
36

g math.random()

8 years ago

I haven't tested your MOD in MP but my MOD also makes use of the math.random() function and seams to work fine on a 2player MP game.

8 years ago

I wonder how it works. Lua's standard math.random() must be using ANSI C's rand() or POSIX random(), which completely depend on the local machine. At least synchronizing randomseed() for each joining player should be necessary. In my impression, Lua's random() doesn't seem to be designed for network-synchronizing states. There should be 'state vector' type for random number sequence which can be explicitly transferred to remote machines, and if Factorio manages it under the cover, I'm pretty curious how they've implemented it.

8 years ago

I have a few desync reports when running only this mod in MP. If a friend joins very early in the game then everything's fine but my guess is that the math.random() call isn't the same during the catch-up phase as it was for the server. Does the modding API offer a deterministic random function that you could use instead of math.random()?

8 years ago

At least I couldn't find one in API documentation. Implementing 'predictable' random number sequence in Lua won't be too difficult, though. There are some lightweight yet effective algorithms I know of, e.g. Xorshift algorithm (https://en.wikipedia.org/wiki/Xorshift). Fully-equipped Mersenne Twister is popular, but has too heavy state vector, which takes time and bandwidth to synchronize among systems. Problem would be that running the algorithm in Lua could be much slower than C++ implementation.

Simply running 'math.randomseed(game.tick)' might work, as long as all the clients and the server are running on the same OS. If Windows and Linux clients connect, there will be no guarantee the same seed yields the same random sequence.

8 years ago

Bit late to the party perhaps, but math.random() is fully MP compatible because factorio intercepts it. This also means you can't set the seed, except when creating a new map.

source:
https://forums.factorio.com/viewtopic.php?f=25&t=19353#p123629

8 years ago

Thanks for the information. Now I clearly understand what's going on under the hood.

New response