2018-10-06 20:42:33 -07:00
|
|
|
#pragma once
|
2015-07-27 19:24:36 -07:00
|
|
|
|
2019-09-23 12:00:23 -07:00
|
|
|
#include "Runtime/GCNTypes.hpp"
|
2015-07-27 19:24:36 -07:00
|
|
|
|
2021-04-10 01:42:06 -07:00
|
|
|
namespace metaforce {
|
2018-12-07 21:30:43 -08:00
|
|
|
|
|
|
|
class CRandom16 {
|
2019-06-30 23:14:42 -07:00
|
|
|
s32 m_seed;
|
2018-12-07 21:30:43 -08:00
|
|
|
static CRandom16* g_randomNumber;
|
2015-07-27 19:24:36 -07:00
|
|
|
|
|
|
|
public:
|
2020-04-11 12:35:44 -07:00
|
|
|
explicit CRandom16(s32 seed = 99) : m_seed(seed) {}
|
2015-07-27 19:24:36 -07:00
|
|
|
|
2019-06-30 23:14:42 -07:00
|
|
|
s32 Next() {
|
2020-09-04 23:04:28 -07:00
|
|
|
IncrementNumNextCalls();
|
2018-12-07 21:30:43 -08:00
|
|
|
m_seed = (m_seed * 0x41c64e6d) + 0x00003039;
|
2022-07-02 16:16:33 -07:00
|
|
|
SetLastSeed(m_seed);
|
2019-06-30 23:14:42 -07:00
|
|
|
return (m_seed >> 16) & 0xffff;
|
2018-12-07 21:30:43 -08:00
|
|
|
}
|
2015-07-27 19:24:36 -07:00
|
|
|
|
2019-06-30 23:14:42 -07:00
|
|
|
s32 GetSeed() const { return m_seed; }
|
2015-07-27 19:24:36 -07:00
|
|
|
|
2020-04-11 12:35:44 -07:00
|
|
|
void SetSeed(s32 seed) { m_seed = seed; }
|
2015-07-27 19:24:36 -07:00
|
|
|
|
2018-12-12 23:39:16 -08:00
|
|
|
float Float() { return Next() * 0.000015259022f; }
|
2015-07-27 19:24:36 -07:00
|
|
|
|
2018-12-12 23:39:16 -08:00
|
|
|
float Range(float min, float max) { return min + Float() * (max - min); }
|
2015-07-27 19:24:36 -07:00
|
|
|
|
2018-12-12 23:39:16 -08:00
|
|
|
s32 Range(s32 min, s32 max) { return min + (Next() % ((max - min) + 1)); }
|
2015-07-27 19:24:36 -07:00
|
|
|
|
2018-12-07 21:30:43 -08:00
|
|
|
static CRandom16* GetRandomNumber() { return g_randomNumber; }
|
|
|
|
static void SetRandomNumber(CRandom16* rnd) { g_randomNumber = rnd; }
|
2020-09-04 23:04:28 -07:00
|
|
|
static void IncrementNumNextCalls();
|
|
|
|
static u32 GetNumNextCalls();
|
|
|
|
static void ResetNumNextCalls();
|
2022-07-02 16:16:33 -07:00
|
|
|
static u32 GetLastSeed();
|
|
|
|
static void SetLastSeed(u32 seed);
|
2015-07-27 19:24:36 -07:00
|
|
|
};
|
|
|
|
|
2018-12-07 21:30:43 -08:00
|
|
|
class CGlobalRandom {
|
|
|
|
CRandom16& m_random;
|
|
|
|
CGlobalRandom* m_prev;
|
|
|
|
static CGlobalRandom* g_currentGlobalRandom;
|
|
|
|
|
2015-07-27 19:24:36 -07:00
|
|
|
public:
|
2018-12-07 21:30:43 -08:00
|
|
|
CGlobalRandom(CRandom16& rand) : m_random(rand), m_prev(g_currentGlobalRandom) {
|
|
|
|
g_currentGlobalRandom = this;
|
|
|
|
CRandom16::SetRandomNumber(&m_random);
|
|
|
|
}
|
|
|
|
~CGlobalRandom() {
|
|
|
|
g_currentGlobalRandom = m_prev;
|
|
|
|
if (m_prev)
|
|
|
|
CRandom16::SetRandomNumber(&m_prev->m_random);
|
|
|
|
else
|
|
|
|
CRandom16::SetRandomNumber(nullptr);
|
|
|
|
}
|
2015-07-27 19:24:36 -07:00
|
|
|
};
|
|
|
|
|
2021-04-10 01:42:06 -07:00
|
|
|
} // namespace metaforce
|