Files
isaac-port/src/rng.cpp
T
shweetandClaude Opus 5.5 1326bb1571 source_tree: generated code coexists with the standard library and simdjson
Ghidra's primitive names become the standard types they are (undefined4/uint/ulong -> uint32_t, byte -> uint8_t ...;
Ghidra's ulong is 4 bytes, glibc's 8); the prelude includes the C library headers and every Ghidra type, global, import
or free function the headers already declare is left to them; the MSVC STL stand-ins move to namespace msvc. A curated
file can now include <string>, <vector>, simdjson.h and classes.h together. Body rate unchanged: 877/3256.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 21:30:14 -05:00

80 lines
2.9 KiB
C++

#include "classes.h"
#include "log.h"
// The xorshift triples a seed is paired with, by shift index.
constexpr unsigned int SHIFT_COUNT = 81;
constexpr unsigned int SHIFTS[SHIFT_COUNT][3] = {
{1, 3, 10}, {1, 5, 16}, {1, 5, 19}, {1, 9, 29}, {1, 11, 6}, {1, 11, 16},
{1, 19, 3}, {1, 21, 20}, {1, 27, 27}, {2, 5, 15}, {2, 5, 21}, {2, 7, 7},
{2, 7, 9}, {2, 7, 25}, {2, 9, 15}, {2, 15, 17}, {2, 15, 25}, {2, 21, 9},
{3, 1, 14}, {3, 3, 26}, {3, 3, 28}, {3, 3, 29}, {3, 5, 20}, {3, 5, 22},
{3, 5, 25}, {3, 7, 29}, {3, 13, 7}, {3, 23, 25}, {3, 25, 24}, {3, 27, 11},
{4, 3, 17}, {4, 3, 27}, {4, 5, 15}, {5, 3, 21}, {5, 7, 22}, {5, 9, 7},
{5, 9, 28}, {5, 9, 31}, {5, 13, 6}, {5, 15, 17}, {5, 17, 13}, {5, 21, 12},
{5, 27, 8}, {5, 27, 21}, {5, 27, 25}, {5, 27, 28}, {6, 1, 11}, {6, 3, 17},
{6, 17, 9}, {6, 21, 7}, {6, 21, 13}, {7, 1, 9}, {7, 1, 18}, {7, 1, 25},
{7, 13, 25}, {7, 17, 21}, {7, 25, 12}, {7, 25, 20}, {8, 7, 23}, {8, 9, 23},
{9, 5, 14}, {9, 5, 25}, {9, 11, 19}, {9, 21, 16}, {10, 9, 21}, {10, 9, 25},
{11, 7, 12}, {11, 7, 16}, {11, 17, 13}, {11, 21, 13}, {12, 9, 23}, {13, 3, 17},
{13, 3, 27}, {13, 5, 19}, {13, 17, 15}, {14, 1, 15}, {14, 13, 15}, {15, 1, 29},
{17, 15, 20}, {17, 15, 23}, {17, 15, 26},
};
// Just under 1 / 2^32, so a float drawn from any seed stays below 1.
constexpr float FLOAT_SCALE = 2.3283062e-10f;
// 007e8fe0 Seeds the generator, clamping the shift index to the last triple.
void RNG::SetSeed(uint32_t seed, uint32_t shiftIndex)
{
unsigned int index = shiftIndex < SHIFT_COUNT ? shiftIndex : SHIFT_COUNT - 1;
_seed = seed;
_shift1 = SHIFTS[index][0];
_shift2 = SHIFTS[index][1];
_shift3 = SHIFTS[index][2];
}
// 007e8f90 Seeds the generator as its constructor does, asserting on a shift index past the table.
void RNG::game_constructor(uint32_t seed, uint32_t shiftIndex)
{
if (shiftIndex >= SHIFT_COUNT) KAGE_LogMessage(LOG_ASSERT, (uint8_t*)"RNG: Invalid ShiftIdx\n");
SetSeed(seed, shiftIndex);
}
// 007e90f0 Steps the seed through its xorshift and returns it.
// The game stops in the debugger on a zero seed, which never moves again.
uint32_t RNG::Next()
{
if (_seed == 0) KAGE_LogMessage(LOG_ASSERT, (uint8_t*)"RNG Seed is zero!\n");
_seed ^= _seed >> _shift1;
_seed ^= _seed << _shift2;
_seed ^= _seed >> _shift3;
return _seed;
}
// 007e9020 A number below max, or 0 for a max of 0.
uint32_t RNG::RandomInt(uint32_t max)
{
uint32_t value = Next();
return max == 0 ? 0 : value % max;
}
// 007e9080 A number in [0, 1).
float RNG::RandomFloat()
{
return (float)Next() * FLOAT_SCALE;
}
// 005cc070 Shuffles [begin, end) in place, from the back, each element swapped with one at or before it.
void RNG::Shuffle(RNG* rng, int* begin, int* end)
{
for (long i = end - begin - 1; i > 0; i--)
{
uint32_t other = rng->Next() % (uint32_t)(i + 1);
int value = begin[i];
begin[i] = begin[other];
begin[other] = value;
}
}