The decompiled C++ tree moves to generated-src/ (classes.h, C++20); src/ is for code written by hand. A curated function defines the generated declaration and starts its comment with its address, and source_tree.py stops emitting that body. src/rng.cpp takes over RNG (SetSeed, the constructor, Next, RandomInt, RandomFloat, Shuffle) with the shift table read from the exe as a constexpr array; src/log.h names KAGE_LogMessage's levels (1 INFO, 2 NET, 4 WARN, 8 ERROR, 16 ASSERT, from its prefix jump table). CMakeLists.txt builds src/*.cpp against generated-src/classes.h (clean with -Wall -Wextra). structs.hpp and headers.py are retired: scripts/merge_fields.py writes the merged field list, classes.h checks the layout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
160 lines
3.8 KiB
C++
160 lines
3.8 KiB
C++
#include "classes.h"
|
|
|
|
// 005cc070
|
|
void RNG::Shuffle(RNG *rng, int *begin, int *end)
|
|
{
|
|
code *pcVar1;
|
|
uint uVar2;
|
|
int i;
|
|
uint remaining;
|
|
int swapValue;
|
|
|
|
remaining = GHIDRA_INT(end) - GHIDRA_INT(begin) >> 2;
|
|
i = remaining - 1;
|
|
if (0 < i) {
|
|
do {
|
|
uVar2 = rng->_seed;
|
|
if (uVar2 == 0) {
|
|
KAGE_LogMessage(0x10,GHIDRA_CAST<byte *>("RNG Seed is zero!\n"));
|
|
uVar2 = rng->_seed;
|
|
if (uVar2 == 0) {
|
|
pcVar1 = (code *)swi(3);
|
|
(*pcVar1)();
|
|
return;
|
|
}
|
|
}
|
|
uVar2 = uVar2 >> ((byte)rng->_shift1 & 0x1f) ^ uVar2;
|
|
uVar2 = uVar2 << ((byte)rng->_shift2 & 0x1f) ^ uVar2;
|
|
uVar2 = uVar2 >> ((byte)rng->_shift3 & 0x1f) ^ uVar2;
|
|
rng->_seed = uVar2;
|
|
if (remaining == 0) {
|
|
uVar2 = 0;
|
|
}
|
|
else {
|
|
uVar2 = uVar2 % remaining;
|
|
}
|
|
swapValue = begin[i];
|
|
remaining = remaining - 1;
|
|
begin[i] = begin[uVar2];
|
|
i = i + -1;
|
|
begin[uVar2] = swapValue;
|
|
} while (0 < i);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// 007e8f90
|
|
void RNG::game_constructor(uint seed, uint shiftidx)
|
|
{
|
|
undefined8 shift1And2;
|
|
int shift3;
|
|
|
|
this->_seed = seed;
|
|
if (0x50 < shiftidx) {
|
|
KAGE_LogMessage(0x10,GHIDRA_CAST<byte *>("RNG: Invalid ShiftIdx\n"));
|
|
}
|
|
shift1And2 = *GHIDRA_CAST<undefined8 *>((&DAT_00b1f4c8 + shiftidx * 0xc));
|
|
shift3 = *GHIDRA_CAST<int *>((&DAT_00b1f4d0 + shiftidx * 0xc));
|
|
this->_shift1 = GHIDRA_INT(shift1And2);
|
|
this->_shift2 = GHIDRA_INT(((ulonglong)shift1And2 >> 0x20));
|
|
this->_shift3 = shift3;
|
|
return;
|
|
}
|
|
|
|
// 007e8fe0
|
|
void RNG::SetSeed(uint Seed, uint ShiftIdx)
|
|
{
|
|
uint clampedShiftIdx;
|
|
uint requestedShiftIdx;
|
|
undefined8 shift12;
|
|
int shift3;
|
|
|
|
this->_seed = Seed;
|
|
requestedShiftIdx = -GHIDRA_UINT((ShiftIdx != 0)) & ShiftIdx;
|
|
clampedShiftIdx = 0x50;
|
|
if (requestedShiftIdx < 0x50) {
|
|
clampedShiftIdx = requestedShiftIdx;
|
|
}
|
|
shift12 = *GHIDRA_CAST<undefined8 *>((&DAT_00b1f4c8 + clampedShiftIdx * 0xc));
|
|
shift3 = *GHIDRA_CAST<int *>((&DAT_00b1f4d0 + clampedShiftIdx * 0xc));
|
|
this->_shift1 = GHIDRA_INT(shift12);
|
|
this->_shift2 = GHIDRA_INT(((ulonglong)shift12 >> 0x20));
|
|
this->_shift3 = shift3;
|
|
return;
|
|
}
|
|
|
|
// 007e9020
|
|
uint RNG::RandomInt(uint max)
|
|
{
|
|
uint seed;
|
|
code *trapFn;
|
|
|
|
seed = this->_seed;
|
|
if (seed == 0) {
|
|
KAGE_LogMessage(0x10,GHIDRA_CAST<byte *>("RNG Seed is zero!\n"));
|
|
seed = this->_seed;
|
|
if (seed == 0) {
|
|
trapFn = (code *)swi(3);
|
|
seed = (*trapFn)();
|
|
return seed;
|
|
}
|
|
}
|
|
seed = seed >> ((byte)this->_shift1 & 0x1f) ^ seed;
|
|
seed = seed << ((byte)this->_shift2 & 0x1f) ^ seed;
|
|
seed = seed >> ((byte)this->_shift3 & 0x1f) ^ seed;
|
|
this->_seed = seed;
|
|
if (max == 0) {
|
|
return 0;
|
|
}
|
|
return seed % max;
|
|
}
|
|
|
|
// 007e9080
|
|
float RNG::RandomFloat()
|
|
{
|
|
code *pcVar1;
|
|
uint seed;
|
|
float10 fVar2;
|
|
RNG *pRVar3;
|
|
|
|
seed = this->_seed;
|
|
if (seed == 0) {
|
|
pRVar3 = this;
|
|
KAGE_LogMessage(0x10,GHIDRA_CAST<byte *>("RNG Seed is zero!\n"));
|
|
seed = this->_seed;
|
|
if (seed == 0) {
|
|
pcVar1 = (code *)swi(3);
|
|
fVar2 = (float10)(*pcVar1)(pRVar3);
|
|
return (float)fVar2;
|
|
}
|
|
}
|
|
seed = seed >> ((byte)this->_shift1 & 0x1f) ^ seed;
|
|
seed = seed << ((byte)this->_shift2 & 0x1f) ^ seed;
|
|
seed = seed >> ((byte)this->_shift3 & 0x1f) ^ seed;
|
|
this->_seed = seed;
|
|
return (float)((double)GHIDRA_INT(seed) + __uint_to_double_fixup[-(GHIDRA_INT(seed) >> 0x1f)]) * 2.3283062e-10;
|
|
}
|
|
|
|
// 007e90f0
|
|
uint RNG::Next()
|
|
{
|
|
code *pcVar1;
|
|
uint seed;
|
|
|
|
seed = this->_seed;
|
|
if (seed == 0) {
|
|
KAGE_LogMessage(0x10,GHIDRA_CAST<byte *>("RNG Seed is zero!\n"));
|
|
seed = this->_seed;
|
|
if (seed == 0) {
|
|
pcVar1 = (code *)swi(3);
|
|
seed = (*pcVar1)();
|
|
return seed;
|
|
}
|
|
}
|
|
seed = seed >> ((byte)this->_shift1 & 0x1f) ^ seed;
|
|
seed = seed << ((byte)this->_shift2 & 0x1f) ^ seed;
|
|
seed = seed >> ((byte)this->_shift3 & 0x1f) ^ seed;
|
|
this->_seed = seed;
|
|
return seed;
|
|
}
|