the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "Mth.h"
3#include "Random.h"
4#include "StringHelpers.h"
5
6const int Mth::BIG_ENOUGH_INT = 1024;
7const float Mth::BIG_ENOUGH_FLOAT = BIG_ENOUGH_INT;
8const float Mth::DEGRAD = PI / 180.0f;
9const float Mth::RADDEG = 180.0f / PI;
10const float Mth::RAD_TO_GRAD = PI / 180.0f;
11
12float *Mth::_sin = NULL;
13
14const float Mth::sinScale = 65536.0f / (float) (PI * 2);
15
16// 4J - added - was in static constructor
17void Mth::init()
18{
19 _sin = new float[65536];
20 for (int i = 0; i < 65536; i++)
21 {
22 _sin[i] = (float) ::sin(i * PI * 2 / 65536.0f);
23 }
24}
25
26float Mth::sin(float i)
27{
28 if(_sin == NULL) init(); // 4J - added
29 return _sin[(int) (i * sinScale) & 65535];
30}
31
32float Mth::cos(float i)
33{
34 if(_sin == NULL) init(); // 4J - added
35 return _sin[(int) (i * sinScale + 65536 / 4) & 65535];
36}
37
38float Mth::sqrt(float x)
39{
40 return (float) ::sqrt(x);
41}
42
43float Mth::sqrt(double x)
44{
45 return (float) ::sqrt(x);
46}
47
48int Mth::floor(float v)
49{
50 int i = (int) v;
51 return v < i ? i - 1 : i;
52}
53
54__int64 Mth::lfloor(double v)
55{
56 __int64 i = (__int64) v;
57 return v < i ? i - 1 : i;
58}
59
60int Mth::fastFloor(double x)
61{
62 return (int) (x + BIG_ENOUGH_FLOAT) - BIG_ENOUGH_INT;
63}
64
65int Mth::floor(double v)
66{
67 int i = (int) v;
68 return v < i ? i - 1 : i;
69}
70
71int Mth::absFloor(double v)
72{
73 return (int) (v >= 0 ? v : -v + 1);
74}
75
76float Mth::abs(float v)
77{
78 return v >= 0 ? v : -v;
79}
80
81int Mth::abs(int v)
82{
83 return v >= 0 ? v : -v;
84}
85
86int Mth::ceil(float v)
87{
88 int i = (int) v;
89 return v > i ? i + 1 : i;
90}
91
92int Mth::clamp(int value, int min, int max)
93{
94 if (value < min)
95 {
96 return min;
97 }
98 if (value > max)
99 {
100 return max;
101 }
102 return value;
103}
104
105float Mth::clamp(float value, float min, float max)
106{
107 if (value < min)
108 {
109 return min;
110 }
111 if (value > max)
112 {
113 return max;
114 }
115 return value;
116}
117
118double Mth::asbMax(double a, double b)
119{
120 if (a < 0) a = -a;
121 if (b < 0) b = -b;
122 return a > b ? a : b;
123}
124
125int Mth::intFloorDiv(int a, int b)
126{
127 if (a < 0) return -((-a - 1) / b) - 1;
128 return a / b;
129}
130
131
132int Mth::nextInt(Random *random, int minInclusive, int maxInclusive)
133{
134 if (minInclusive >= maxInclusive)
135 {
136 return minInclusive;
137 }
138 return random->nextInt(maxInclusive - minInclusive + 1) + minInclusive;
139}
140
141float Mth::nextFloat(Random *random, float min, float max)
142{
143 if (min >= max) return min;
144 return (random->nextFloat() * (max - min)) + min;
145}
146
147double Mth::nextDouble(Random *random, double min, double max)
148{
149 if (min >= max) return min;
150 return (random->nextDouble() * (max - min)) + min;
151}
152
153float Mth::wrapDegrees(float input)
154{
155 //input %= 360;
156 while (input >= 180)
157 {
158 input -= 360;
159 }
160 while (input < -180)
161 {
162 input += 360;
163 }
164 return input;
165}
166
167double Mth::wrapDegrees(double input)
168{
169 //input %= 360;
170 while (input >= 180)
171 {
172 input -= 360;
173 }
174 while (input < -180)
175 {
176 input += 360;
177 }
178 return input;
179}
180
181int Mth::getInt(const wstring &input, int def)
182{
183 int result = def;
184
185 result = _fromString<int>(input);
186
187 return result;
188}
189
190int Mth::getInt(const wstring &input, int def, int min)
191{
192 int result = def;
193
194 result = _fromString<int>(input);
195
196 if (result < min) result = min;
197 return result;
198}
199
200double Mth::getDouble(const wstring &input, double def)
201{
202 double result = def;
203
204 result = _fromString<double>(input);
205
206 return result;
207}
208
209double Mth::getDouble(const wstring &input, double def, double min)
210{
211 double result = def;
212
213 result = _fromString<double>(input);
214
215 if (result < min) result = min;
216 return result;
217}
218
219// 4J Changed this to remove the use of the actuall UUID type
220wstring Mth::createInsecureUUID(Random *random)
221{
222 wchar_t output[33];
223 output[32] = 0;
224 __int64 high = (random->nextLong() & ~UUID_VERSION) | UUID_VERSION_TYPE_4;
225 __int64 low = (random->nextLong() & ~UUID_VARIANT) | UUID_VARIANT_2;
226 for(int i = 0; i < 16; i++ )
227 {
228 wchar_t nybbleHigh = high & 0xf;
229 wchar_t nybbleLow = low & 0xf;
230 nybbleHigh = (nybbleHigh > 9 ) ? ( nybbleHigh + (L'a'-10) ) : ( nybbleHigh + L'0' );
231 nybbleLow = (nybbleLow > 9 ) ? ( nybbleLow + (L'a'-10) ) : ( nybbleLow + L'0' );
232 high >>= 4;
233 low >>= 4;
234 output[31 - i] = nybbleLow;
235 output[15 - i] = nybbleHigh;
236 }
237 return wstring(output);
238}
239
240
241// 4J Added
242bool Mth::almostEquals( double double1, double double2, double precision)
243{
244 return (std::abs(double1 - double2) <= precision);
245}