metaforce/Runtime/CRandom16.hpp

57 lines
1.3 KiB
C++
Raw Normal View History

2018-10-07 03:42:33 +00:00
#pragma once
2015-07-28 02:24:36 +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:
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() {
IncrementNumNextCalls();
2018-12-08 05:30:43 +00:00
m_seed = (m_seed * 0x41c64e6d) + 0x00003039;
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
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; }
static void IncrementNumNextCalls();
static u32 GetNumNextCalls();
static void ResetNumNextCalls();
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