From 685dbb8326837745d4a56c440f36efc97a1d58a8 Mon Sep 17 00:00:00 2001 From: ac-stef Date: Thu, 25 Dec 2014 22:00:46 +0100 Subject: [PATCH] store old state of random generator to restore after a seed() The light calculation requires the random generator to be seeded with a constant - otherwise repeated light recalculations would make the lights change. This patch allows the random generator to go back to the state before the static seed(). --- source/src/crypto.cpp | 11 ++++++++++- source/src/tools.h | 1 + source/src/worldlight.cpp | 5 ++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/source/src/crypto.cpp b/source/src/crypto.cpp index 44041bed..7380acdd 100644 --- a/source/src/crypto.cpp +++ b/source/src/crypto.cpp @@ -849,11 +849,12 @@ bool checkchallenge(const char *answerstr, void *correct) #define M (397) #define K (0x9908B0DFU) -static uint state[N]; +static uint state[2 * N]; static int next = N; void seedMT(uint seed) { + loopi(N) state[i + N] = state[i]; state[0] = seed; for(uint i = 1; i < N; i++) state[i] = seed = 1812433253U * (seed ^ (seed >> 30)) + i; @@ -877,3 +878,11 @@ uint randomMT() return y; } +void popMT() // undo last seedMT() +{ + loopi(N) state[i] = state[i + N]; +} +#undef N +#undef M +#undef K + diff --git a/source/src/tools.h b/source/src/tools.h index 59465597..d78544a9 100644 --- a/source/src/tools.h +++ b/source/src/tools.h @@ -967,6 +967,7 @@ extern bool isbigendian(); extern void strtoupper(char *t, const char *s = NULL); extern void seedMT(uint seed); extern uint randomMT(void); +extern void popMT(void); struct iprange { enet_uint32 lr, ur; }; extern const char *atoip(const char *s, enet_uint32 *ip); diff --git a/source/src/worldlight.cpp b/source/src/worldlight.cpp index 63143248..5b5826bf 100644 --- a/source/src/worldlight.cpp +++ b/source/src/worldlight.cpp @@ -195,8 +195,7 @@ void calclight() s++; } - uint keep = randomMT(); - seedMT(ents.length() + hdr.maprevision); + seedMT(ents.length() + hdr.maprevision); // static seed -> nothing random here loopv(ents) { @@ -204,7 +203,7 @@ void calclight() if(e.type==LIGHT) calclightsource(e); } - seedMT(keep); + popMT(); // undo the static seedMT() from above block bb = { mapdims.x1 - 1, mapdims.y1 - 1, mapdims.xspan + 2, mapdims.yspan + 2 }; postlightarea(bb);