Reconstruction layout: generated-src/ (generated) and src/ (curated), CMake, the first takeover: RNG
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>
This commit is contained in:
1 parent
2ff2d5d87f
commit
3ce74c6e2d
331 files changed
+485
-7835
No files matched your search
+79
@@ -0,0 +1,79 @@
|
||||
#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(uint seed, uint 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(uint seed, uint shiftIndex)
|
||||
{
|
||||
if (shiftIndex >= SHIFT_COUNT) KAGE_LogMessage(LOG_ASSERT, (byte*)"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.
|
||||
uint RNG::Next()
|
||||
{
|
||||
if (_seed == 0) KAGE_LogMessage(LOG_ASSERT, (byte*)"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.
|
||||
uint RNG::RandomInt(uint max)
|
||||
{
|
||||
uint 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--)
|
||||
{
|
||||
uint other = rng->Next() % (uint)(i + 1);
|
||||
int value = begin[i];
|
||||
|
||||
begin[i] = begin[other];
|
||||
begin[other] = value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user