sead
Loading...
Searching...
No Matches
eft_Random.h
Go to the documentation of this file.
1#ifndef EFT_RANDOM_H_
2#define EFT_RANDOM_H_
3
4#include <nw/eft/eft_typeDef.h>
5
6namespace nw { namespace eft {
7
8class Random
9{
10public:
11 Random()
12 {
13 Init();
14 }
15
16 explicit Random(u32 seed)
17 {
18 Init(seed);
19 }
20
21 Random(u32 seed_0, u32 seed_1, u32 seed_2, u32 seed_3)
22 {
23 Init(seed_0, seed_1, seed_2, seed_3);
24 }
25
26 void Init()
27 {
28 Init(0);
29 }
30
31 void Init(u32 seed)
32 {
33 static const u32 a = 1812433253;
34 mX = (seed ^ (seed >> 30)) * a + 1;
35 mY = (mX ^ (mX >> 30)) * a + 2;
36 mZ = (mY ^ (mY >> 30)) * a + 3;
37 mW = (mZ ^ (mZ >> 30)) * a + 4;
38 }
39
40 void Init(u32 seed_0, u32 seed_1, u32 seed_2, u32 seed_3)
41 {
42 if (seed_0 == 0 && seed_1 == 0 && seed_2 == 0 && seed_3 == 0) // seeds must not be all zero.
43 Init(0);
44
45 else
46 {
47 mX = seed_0;
48 mY = seed_1;
49 mZ = seed_2;
50 mW = seed_3;
51 }
52 }
53
54 u32 GetU32()
55 {
56 u32 t = (mX^(mX<<11));
57 mX = mY;
58 mY = mZ;
59 mZ = mW;
60 mW = (mW^(mW>>19))^(t^(t>>8));
61 return mW;
62 }
63
64 u32 GetU32(u32 ceil)
65 {
66 return static_cast<u32>(((u64)GetU32() * ceil) >> 32);
67 }
68
69 s32 GetS32Range(s32 a, s32 b)
70 {
71 return GetU32(b - a) + a;
72 }
73
74 f32 GetF32()
75 {
76 return GetU32() * (1.0f / 4294967296.0f);
77 }
78
79 f32 GetF32(f32 ceil)
80 {
81 return GetF32() * ceil;
82 }
83
84 f32 GetF32Range(f32 a, f32 b)
85 {
86 return GetF32(b - a) + a;
87 }
88
89 f64 GetF64()
90 {
91 return GetU32() * (1.0 / 4294967296.0);
92 }
93
94 f64 GetF64(f64 ceil)
95 {
96 return GetF64() * ceil;
97 }
98
99 f64 GetF64Range(f64 a, f64 b)
100 {
101 return GetF64(b - a) + a;
102 }
103
104 int GetSign()
105 {
106 return (GetU32() & 2) - 1;
107 }
108
109 bool GetBool()
110 {
111 return static_cast<bool>(GetU32() & 1);
112 }
113
114 void GetContext(u32* num_0, u32* num_1, u32* num_2, u32* num_3) const
115 {
116 *num_0 = mX;
117 *num_1 = mY;
118 *num_2 = mZ;
119 *num_3 = mW;
120 }
121
122private:
123 u32 mX;
124 u32 mY;
125 u32 mZ;
126 u32 mW;
127};
128static_assert(sizeof(Random) == 0x10, "Random size mismatch");
129
130class Heap;
131
132class PtclRandom
133{
134public:
135 PtclRandom();
136
137 static void CreateRandomTbl(Heap* heap);
138 static void DestroyRandomTbl(Heap* heap);
139
140 void SetSeed(u32 val)
141 {
142 mVec3RndIx = val >> 0;
143 mNormalizedVec3RndIx = val >> 16;
144 mRnd = val;
145 }
146
147 const nw::math::VEC3& GetVec3 () { return mVec3Tbl [ ( mVec3RndIx++ ) & cNumVec3TblMask ]; }
148 const nw::math::VEC3& GetNormalizedVec3() { return mNormalizedVec3Tbl[ ( mNormalizedVec3RndIx++ ) & cNumVec3TblMask ]; }
149
151 {
152 u32 ret = mRnd;
153 mRnd = mRnd * 1103515245 + 12345;
154 return ret;
155 }
156
157 f32 GetF32()
158 {
159 return GetU32Direct() * (1.0f / 4294967296.0f);
160 }
161
162 /* f32 GetF32(f32 ceil)
163 {
164 return GetF32() * ceil;
165 }
166
167 f32 GetF32Range(f32 a, f32 b)
168 {
169 return GetF32(b - a) + a;
170 } */
171
172 s32 GetS32()
173 {
174 return GetU32Direct() >> 24;
175 }
176
177 s32 GetS32(int val)
178 {
179 return static_cast<s32>(((u64)GetU32Direct() * val) >> 32);
180 }
181
182 static Random* GetGlobalRandom();
183
184private:
185 enum
186 {
187 cNumVec3Tbl = 512 ,
189 };
190
191 static nw::math::VEC3* mVec3Tbl;
192 static nw::math::VEC3* mNormalizedVec3Tbl;
193
196 u32 mRnd;
197
198 static Random gRandom;
199};
200static_assert(sizeof(PtclRandom) == 8, "nw::eft::PtclRandom size mismatch");
201
202} } // namespace nw::eft
203
204#endif // EFT_RANDOM_H_
u32 mRnd
Definition eft_Random.h:196
static Random gRandom
Definition eft_Random.h:198
static nw::math::VEC3 * mVec3Tbl
Definition eft_Random.h:191
const nw::math::VEC3 & GetVec3()
static nw::math::VEC3 * mNormalizedVec3Tbl
Definition eft_Random.h:192
s32 GetS32(int val)
void SetSeed(u32 val)
u16 mNormalizedVec3RndIx
Definition eft_Random.h:195
static Random * GetGlobalRandom()
Definition eft_Random.cpp:11
const nw::math::VEC3 & GetNormalizedVec3()
u16 mVec3RndIx
Definition eft_Random.h:194
static void CreateRandomTbl(Heap *heap)
Definition eft_Random.cpp:16
static void DestroyRandomTbl(Heap *heap)
Definition eft_Random.cpp:36
s32 GetS32Range(s32 a, s32 b)
f32 GetF32Range(f32 a, f32 b)
u32 GetU32(u32 ceil)
Random(u32 seed)
u32 mY
Definition eft_Random.h:124
void Init(u32 seed_0, u32 seed_1, u32 seed_2, u32 seed_3)
void GetContext(u32 *num_0, u32 *num_1, u32 *num_2, u32 *num_3) const
void Init(u32 seed)
Random(u32 seed_0, u32 seed_1, u32 seed_2, u32 seed_3)
f64 GetF64Range(f64 a, f64 b)
u32 mW
Definition eft_Random.h:126
f32 GetF32(f32 ceil)
f64 GetF64(f64 ceil)
u32 mZ
Definition eft_Random.h:125
u32 mX
Definition eft_Random.h:123