Skip to content

Commit ccfbd9a

Browse files
committed
Add option to use modern pseudo random number generator
Signed-off-by: Thomas Rohloff <v10lator@myway.de>
1 parent 01fca44 commit ccfbd9a

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

src/port/Engine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
// #include <Fast3D/gfx_rendering_api.h>
3333
#include <SDL2/SDL.h>
3434

35+
#include <stdlib.h>
36+
#include <time.h>
37+
3538
#include <utility>
3639

3740
#ifdef __SWITCH__
@@ -121,6 +124,8 @@ GameEngine::GameEngine() {
121124
}
122125
}
123126

127+
srand(time(NULL));
128+
124129
this->context = Ship::Context::CreateUninitializedInstance("Spaghetti Kart", "spaghettify", "spaghettify.cfg.json");
125130

126131
this->context->InitConfiguration(); // without this line InitConsoleVariables fails at Config::Reload()

src/port/ui/PortMenu.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ void PortMenu::AddEnhancements() {
383383
.CVar("gShowSpaghettiVersion")
384384
.Options(CheckboxOptions().Tooltip("Show the Spaghetti Kart version on the Mario Kart menu").DefaultValue(true));
385385

386+
AddWidget(path, "Use modern PRNG", WIDGET_CVAR_CHECKBOX).CVar("gModernPRNG");
387+
386388
path = { "Enhancements", "Cheats", SECTION_COLUMN_1 };
387389
AddSidebarEntry("Enhancements", "Cheats", 3);
388390
AddWidget(path, "Moon Jump", WIDGET_CVAR_CHECKBOX).CVar("gEnableMoonJump");

src/racing/math_util.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "port/Game.h"
1313
#include <port/interpolation/FrameInterpolation.h>
1414
#include <port/interpolation/matrix.h>
15+
#include <stdlib.h>
1516
#pragma intrinsic(sqrtf, fabs)
1617

1718
s32 D_802B91C0[2] = { 13, 13 };
@@ -990,35 +991,45 @@ UNUSED s16 func_802B7D28(f32 arg0) {
990991
}
991992

992993
u16 random_u16(void) {
993-
u16 temp1, temp2;
994+
if (CVarGetInteger("gModernPRNG", false) == false)
995+
{
996+
u16 temp1, temp2;
994997

995-
if (gRandomSeed16 == 22026) {
996-
gRandomSeed16 = 0;
997-
}
998+
if (gRandomSeed16 == 22026) {
999+
gRandomSeed16 = 0;
1000+
}
9981001

999-
temp1 = (gRandomSeed16 & 0x00FF) << 8;
1000-
temp1 = temp1 ^ gRandomSeed16;
1002+
temp1 = (gRandomSeed16 & 0x00FF) << 8;
1003+
temp1 = temp1 ^ gRandomSeed16;
10011004

1002-
gRandomSeed16 = ((temp1 & 0x00FF) << 8) + ((temp1 & 0xFF00) >> 8);
1005+
gRandomSeed16 = ((temp1 & 0x00FF) << 8) + ((temp1 & 0xFF00) >> 8);
10031006

1004-
temp1 = ((temp1 & 0x00FF) << 1) ^ gRandomSeed16;
1005-
temp2 = (temp1 >> 1) ^ 0xFF80;
1007+
temp1 = ((temp1 & 0x00FF) << 1) ^ gRandomSeed16;
1008+
temp2 = (temp1 >> 1) ^ 0xFF80;
10061009

1007-
if ((temp1 & 1) == 0) {
1008-
if (temp2 == 43605) {
1009-
gRandomSeed16 = 0;
1010+
if ((temp1 & 1) == 0) {
1011+
if (temp2 == 43605) {
1012+
gRandomSeed16 = 0;
1013+
} else {
1014+
gRandomSeed16 = temp2 ^ 0x1FF4;
1015+
}
10101016
} else {
1011-
gRandomSeed16 = temp2 ^ 0x1FF4;
1017+
gRandomSeed16 = temp2 ^ 0x8180;
10121018
}
1013-
} else {
1014-
gRandomSeed16 = temp2 ^ 0x8180;
1019+
1020+
return gRandomSeed16;
10151021
}
10161022

1017-
return gRandomSeed16;
1023+
return (rand() % (UINT16_MAX + 1));
10181024
}
10191025

10201026
u16 random_int(u16 arg0) {
1021-
return arg0 * (((f32) random_u16()) / 65535.0);
1027+
if (CVarGetInteger("gModernPRNG", false) == false)
1028+
{
1029+
return arg0 * (((f32) random_u16()) / 65535.0);
1030+
}
1031+
1032+
return (rand() % (arg0 + 1));
10221033
}
10231034

10241035
s16 func_802B7F34(f32 arg0, f32 arg1, f32 arg2, f32 arg3) {

0 commit comments

Comments
 (0)