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