Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_Math.c
Go to the documentation of this file.
2{
3 //~ Random generator used in scripted Math functions
4 protected const static ref RandomGenerator RANDOM_GENERATOR = new RandomGenerator();
5
6 static const float MILS_NATO2DEG = 0.05625;
7 static const float MILS_WP2DEG = 0.06;
8 static const float MILS_STRECK2DEG = 0.05714;
9
10 static const int MAX_RANDOM = 0x7FFF; // 32767
11
12 //------------------------------------------------------------------------------------------------
18 static float RandomFloat(float min, float max)
19 {
20 if (min == max)
21 return min;
22 else if (min < max)
23 return Math.RandomFloat(min, max);
24 else
25 return Math.RandomFloat(max, min);
26 }
27
28 //------------------------------------------------------------------------------------------------
34 static float RandomFloatInclusive(float min, float max)
35 {
36 if (min == max)
37 return min;
38 else if (min < max)
39 return Math.RandomFloatInclusive(min, max);
40 else
41 return Math.RandomFloatInclusive(max, min);
42 }
43
44 //------------------------------------------------------------------------------------------------
50 static float RandomGaussFloat(float min, float mid, float max)
51 {
52 if (min == max)
53 return min;
54
55 if (min > max)
56 {
57 float tmp = min;
58 min = max;
59 max = tmp;
60 }
61
62 float result = Math.RandomGaussFloat((max - min) / 6.0, mid); // ~99.73% cases covered
63
64 if (result < min)
65 return min;
66
67 if (result > max)
68 return max;
69
70 return result;
71 }
72
73 //------------------------------------------------------------------------------------------------
79 static int RandomInt(int min, int max)
80 {
81 if (min == max)
82 return min;
83 else if (min < max)
84 return Math.RandomInt(min, max);
85 else
86 return Math.RandomInt(max, min);
87 }
88
89 //------------------------------------------------------------------------------------------------
95 static int RandomIntInclusive(int min, int max)
96 {
97 if (min == max)
98 return min;
99 else if (min < max)
100 return Math.RandomIntInclusive(min, max);
101 else
102 return Math.RandomIntInclusive(max, min);
103 }
104
105 //------------------------------------------------------------------------------------------------
110 // TODO: [Obsolete("Use Math.Mod instead")] ?
111 // beware, negative numbers return different results:
112 // Math.Mod(-500, 360) = -140
113 // SCR_Math.fmod(-500, 360) = 220
114 static float fmod(float dividend, float divisor)
115 {
116 if (divisor == 0)
117 return 0;
118
119 return dividend - Math.Floor(dividend / divisor) * divisor;
120 }
121
122 //------------------------------------------------------------------------------------------------
128 static float LerpAngle(float a, float b, float t)
129 {
130 float dt = fmod(b - a, 360);
131 if (dt > 180)
132 dt -= 360;
133
134 return fmod(Math.Lerp(a, a + dt, t), 360);
135 }
136
137 //------------------------------------------------------------------------------------------------
142 static float DeltaAngle(float a, float b)
143 {
144 return 180 - Math.AbsFloat(fmod(Math.AbsFloat(b - a), 360) - 180);
145 }
146
147 //------------------------------------------------------------------------------------------------
151 static int IntegerMask(int x)
152 {
153 x |= (x >> 1);
154 x |= (x >> 2);
155 x |= (x >> 4);
156 x |= (x >> 8);
157 x |= (x >> 16);
158 return x;
159 }
160
161 //------------------------------------------------------------------------------------------------
166 static float GetDistanceToStop(float speed, float deceleration)
167 {
168 float distance = 0;
169 if (deceleration > 0)
170 distance = 0.5 * speed * speed / deceleration;
171 else if (speed > 0)
172 distance = float.INFINITY;
173
174 return distance;
175 }
176
177 //------------------------------------------------------------------------------------------------
182 static float GetSpeedToReachDistance(float distance, float deceleration)
183 {
184 if (distance <= 0 || deceleration <= 0)
185 return 0;
186
187 return Math.Sqrt(2 * distance * deceleration);
188 }
189
190 //------------------------------------------------------------------------------------------------
196 static float GetSpeedToReachDistanceInTime(float distance, float deceleration, float time)
197 {
198 if (time <= 0)
199 return float.INFINITY;
200
201 // Get the decelerated part
202 float speed = GetSpeedToReachDistance(distance, deceleration);
203 float averageSpeed = distance / time;
204 float additionalSpeed = averageSpeed - speed * 0.5;
205 return speed + additionalSpeed;
206 }
207
208 //------------------------------------------------------------------------------------------------
213 static float GetGaussianDistributionRandom(float min, float mid, float max)
214 {
215 if (min == max)
216 return min;
217
218 if (min > max)
219 {
220 float tmp = min;
221 min = max;
222 max = tmp;
223 }
224
225 float result = Math.RandomGaussFloat((max - min) / 6.0, mid); // ~99.73% cases covered
226
227 if (result < min)
228 return min;
229
230 if (result > max)
231 return max;
232
233 return result;
234 }
235
236 //------------------------------------------------------------------------------------------------
242 static float GetBatesDistributionRandom(float min, float mid, float max)
243 {
244 if (min == max)
245 return min;
246
247 if (min > max)
248 {
249 float tmp = min;
250 min = max;
251 max = tmp;
252 }
253
254 const float bates = (
255 RANDOM_GENERATOR.RandFloat01()
256 + RANDOM_GENERATOR.RandFloat01()
257 + RANDOM_GENERATOR.RandFloat01()
258 + RANDOM_GENERATOR.RandFloat01()
259 ) * 0.25;
260
261 if (bates < 0.5)
262 return Math.Lerp(min, mid, bates * 2);
263 else
264 return Math.Lerp(mid, max, bates * 2 - 1);
265 }
266
267 //------------------------------------------------------------------------------------------------
274
275 //------------------------------------------------------------------------------------------------
280 static float FixAngle(float angle, float units = Math.PI)
281 {
282 return Math.Repeat(units + angle, units * 2) - units;
283 }
284
285 //------------------------------------------------------------------------------------------------
290 static float ConvertFromRadians(float radianAngleFrom, SCR_EOpticsAngleUnits toUnitType)
291 {
292 switch (toUnitType)
293 {
294 case SCR_EOpticsAngleUnits.DEGREES:
295 radianAngleFrom = radianAngleFrom * Math.RAD2DEG;
296 break;
297
298 case SCR_EOpticsAngleUnits.MILS_WP:
299 radianAngleFrom = radianAngleFrom * Math.RAD2DEG / MILS_WP2DEG;
300 break;
301
302 case SCR_EOpticsAngleUnits.MILS_NATO:
303 radianAngleFrom = radianAngleFrom * Math.RAD2DEG / MILS_NATO2DEG;
304 break;
305
306 case SCR_EOpticsAngleUnits.MILS_STRECK:
307 radianAngleFrom = radianAngleFrom * Math.RAD2DEG / MILS_STRECK2DEG;
308 break;
309
310 default:
311 radianAngleFrom = radianAngleFrom * 1000; //from radians to milliradians
312 break;
313 }
314
315 return radianAngleFrom;
316 }
317
318 //------------------------------------------------------------------------------------------------
323 static float ConvertToRadians(float angleFrom, SCR_EOpticsAngleUnits fromUnitType)
324 {
325 switch (fromUnitType)
326 {
327 case SCR_EOpticsAngleUnits.DEGREES:
328 {
329 angleFrom = angleFrom * Math.DEG2RAD;
330 break;
331 }
332
333 case SCR_EOpticsAngleUnits.MILS_WP:
334 {
335 angleFrom = angleFrom * MILS_WP2DEG * Math.DEG2RAD;
336 break;
337 }
338
339 case SCR_EOpticsAngleUnits.MILS_NATO:
340 {
341 angleFrom = angleFrom * MILS_NATO2DEG * Math.DEG2RAD;
342 break;
343 }
344
345 case SCR_EOpticsAngleUnits.MILS_STRECK:
346 {
347 angleFrom = angleFrom * MILS_STRECK2DEG * Math.DEG2RAD;
348 break;
349 }
350
351 default:
352 {
353 angleFrom = angleFrom * 0.001; //from milliradians to radians
354 break;
355 }
356 }
357
358 return angleFrom;
359 }
360}
float distance
SCR_EOpticsAngleUnits
Definition Math.c:13
static float FixAngle(float angle, float units=Math.PI)
Definition SCR_Math.c:280
static float GetSpeedToReachDistanceInTime(float distance, float deceleration, float time)
Definition SCR_Math.c:196
static float GetBatesDistributionRandom(float min, float mid, float max)
Definition SCR_Math.c:242
static int RandomInt(int min, int max)
Definition SCR_Math.c:79
static float GetSpeedToReachDistance(float distance, float deceleration)
Definition SCR_Math.c:182
static float RandomFloat(float min, float max)
Definition SCR_Math.c:18
static RandomGenerator GetMathRandomGenerator()
Definition SCR_Math.c:270
static int IntegerMask(int x)
Definition SCR_Math.c:151
static const ref RandomGenerator RANDOM_GENERATOR
Definition SCR_Math.c:4
static float GetDistanceToStop(float speed, float deceleration)
Definition SCR_Math.c:166
static float ConvertFromRadians(float radianAngleFrom, SCR_EOpticsAngleUnits toUnitType)
Definition SCR_Math.c:290
static float DeltaAngle(float a, float b)
Definition SCR_Math.c:142
static float RandomFloatInclusive(float min, float max)
Definition SCR_Math.c:34
static float fmod(float dividend, float divisor)
Definition SCR_Math.c:114
static float LerpAngle(float a, float b, float t)
Definition SCR_Math.c:128
static int RandomIntInclusive(int min, int max)
Definition SCR_Math.c:95
static const float MILS_NATO2DEG
Definition SCR_Math.c:6
static const float MILS_WP2DEG
Definition SCR_Math.c:7
static float RandomGaussFloat(float min, float mid, float max)
Definition SCR_Math.c:50
static const int MAX_RANDOM
Definition SCR_Math.c:10
static float ConvertToRadians(float angleFrom, SCR_EOpticsAngleUnits fromUnitType)
Definition SCR_Math.c:323
static const float MILS_STRECK2DEG
Definition SCR_Math.c:8
static float GetGaussianDistributionRandom(float min, float mid, float max)
Definition SCR_Math.c:213