Browse code

Replace std::rand with RNG based on actual DS RNG.

(This also means the playback of songs with randomness in them will be deterministic now.)

Naram Qashat authored on 2021/03/20 18:46:24
Showing 2 changed files
... ...
@@ -402,6 +402,14 @@ static inline uint8_t SseqCommandByteCount(int cmd)
402 402
 		}
403 403
 }
404 404
 
405
+static std::uint32_t RandomU{ 0x12345678 };
406
+
407
+static std::uint16_t CalcRandom()
408
+{
409
+	RandomU = RandomU * 1664525 + 1013904223;
410
+	return static_cast<std::uint16_t>(RandomU >> 16);
411
+}
412
+
405 413
 static auto varFuncSet = [](int16_t, int16_t value) { return value; };
406 414
 static auto varFuncAdd = [](int16_t var, int16_t value) -> int16_t { return var + value; };
407 415
 static auto varFuncSub = [](int16_t var, int16_t value) -> int16_t { return var - value; };
... ...
@@ -417,9 +425,9 @@ static auto varFuncShift = [](int16_t var, int16_t value) -> int16_t
417 425
 static auto varFuncRand = [](int16_t, int16_t value) -> int16_t
418 426
 {
419 427
 	if (value < 0)
420
-		return -(std::rand() % (-value + 1));
428
+		return -(CalcRandom() % (-value + 1));
421 429
 	else
422
-		return std::rand() % (value + 1);
430
+		return CalcRandom() % (value + 1);
423 431
 };
424 432
 
425 433
 static inline std::function<int16_t (int16_t, int16_t)> VarFunc(int cmd)
... ...
@@ -732,7 +740,7 @@ void Track::Run()
732 740
 						this->overriding.extraValue = read8(pData);
733 741
 					int16_t minVal = read16(pData);
734 742
 					int16_t maxVal = read16(pData);
735
-					this->overriding.value = (std::rand() % (maxVal - minVal + 1)) + minVal;
743
+					this->overriding.value = (CalcRandom() % (maxVal - minVal + 1)) + minVal;
736 744
 					break;
737 745
 				}
738 746
 
... ...
@@ -166,8 +166,6 @@ bool XSFPlayer_NCSF::Load()
166 166
 	soundViewThreadHandle = CreateThread(nullptr, 0, soundViewThread, this, 0, nullptr);
167 167
 #endif
168 168
 
169
-	std::srand(static_cast<unsigned>(std::time(nullptr)));
170
-
171 169
 	PseudoFile file;
172 170
 	file.data = &this->sdatData;
173 171
 	this->sdat.reset(new SDAT(file, this->sseq));