1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
#if defined _gamechaos_stocks_maths_included
#endinput
#endif
#define _gamechaos_stocks_maths_included
#define GC_MATHS_VERSION 0x02_00_00
#define GC_MATHS_VERSION_STRING "2.0.0"
#include <gamechaos/vectors>
#define GC_PI 3.14159265359
#define GC_DEGREES(%1) ((%1) * 180.0 / GC_PI) // convert radians to degrees
#define GC_RADIANS(%1) ((%1) * GC_PI / 180.0) // convert degrees to radians
#define GC_FLOAT_NAN view_as<float>(0xffffffff)
#define GC_FLOAT_INFINITY view_as<float>(0x7f800000)
#define GC_FLOAT_NEGATIVE_INFINITY view_as<float>(0xff800000)
#define GC_FLOAT_LARGEST_POSITIVE view_as<float>(0x7f7fffff)
#define GC_FLOAT_SMALLEST_NEGATIVE view_as<float>(0xff7fffff)
#define GC_FLOAT_SMALLEST_POSITIVE view_as<float>(0x00000001)
#define GC_FLOAT_LARGEST_NEGATIVE view_as<float>(0x80000001)
#define GC_INT_MAX 0x7fffffff
#define GC_INT_MIN 0xffffffff
/**
* Credit: https://stackoverflow.com/questions/5666222/3d-line-plane-intersection
* Determines the point of intersection between a plane defined by a point and a normal vector and a line defined by a point and a direction vector.
*
* @param planePoint A point on the plane.
* @param planeNormal Normal vector of the plane.
* @param linePoint A point on the line.
* @param lineDirection Direction vector of the line.
* @param result Resultant vector.
*/
stock void GCLineIntersection(const float planePoint[3], const float planeNormal[3], const float linePoint[3], const float lineDirection[3], float result[3])
{
if (GetVectorDotProduct(planeNormal, lineDirection) == 0)
{
return;
}
float t = (GetVectorDotProduct(planeNormal, planePoint)
- GetVectorDotProduct(planeNormal, linePoint))
/ GetVectorDotProduct(planeNormal, lineDirection);
float lineDir[3];
lineDir = lineDirection;
NormalizeVector(lineDir, lineDir);
ScaleVector(lineDir, t);
AddVectors(linePoint, lineDir, result);
}
/**
* Calculates a point according to angles supplied that is a certain distance away.
*
* @param client Client index.
* @param result Resultant vector.
* @param distance Maximum distance to trace.
* @return True on success, false otherwise.
*/
stock void GCCalcPointAngleDistance(const float start[3], const float angle[3], float distance, float result[3])
{
float zsine = Sine(DegToRad(-angle[0]));
float zcos = Cosine(DegToRad(-angle[0]));
result[0] = Cosine(DegToRad(angle[1])) * zcos;
result[1] = Sine(DegToRad(angle[1])) * zcos;
result[2] = zsine;
ScaleVector(result, distance);
AddVectors(start, result, result);
}
/**
* Compares how close 2 floats are.
*
* @param z1 Float 1
* @param z2 Float 2
* @param tolerance How close the floats have to be to return true.
* @return True on success, false otherwise.
*/
stock bool GCIsRoughlyEqual(float z1, float z2, float tolerance)
{
return FloatAbs(z1 - z2) < tolerance;
}
/**
* Checks if a float is within a range
*
* @param number Float to check.
* @param min Minimum range.
* @param max Maximum range.
* @return True on success, false otherwise.
*/
stock bool GCIsFloatInRange(float number, float min, float max)
{
return number >= min && number <= max;
}
/**
* Keeps the yaw angle within the range of -180 to 180.
*
* @param angle Angle.
* @return Normalised angle.
*/
stock float GCNormaliseYaw(float angle)
{
if (angle <= -180.0)
{
angle += 360.0;
}
if (angle > 180.0)
{
angle -= 360.0;
}
return angle;
}
/**
* Keeps the yaw angle within the range of -180 to 180.
*
* @param angle Angle.
* @return Normalised angle.
*/
stock float GCNormaliseYawRad(float angle)
{
if (angle <= -FLOAT_PI)
{
angle += FLOAT_PI * 2;
}
if (angle > FLOAT_PI)
{
angle -= FLOAT_PI * 2;
}
return angle;
}
/**
* Linearly interpolates between 2 values.
*
* @param f1 Float 1.
* @param f2 Float 2.
* @param fraction Amount to interpolate.
* @return Interpolated value.
*/
stock float GCInterpLinear(float f1, float f2, float fraction)
{
float diff = f2 - f1;
return diff * fraction + f1;
}
/**
* Calculates the linear fraction from a value that was interpolated and 2 values it was interpolated from.
*
* @param f1 Float 1.
* @param f2 Float 2.
* @param fraction Interpolated value.
* @return Fraction.
*/
stock float GCCalcLerpFraction(float f1, float f2, float lerped)
{
float diff = f2 - f1;
float fraction = lerped - f1 / diff;
return fraction;
}
/**
* Calculate absolute value of an integer.
*
* @param x Integer.
* @return Absolute value of integer.
*/
stock int GCIntAbs(int x)
{
return x >= 0 ? x : -x;
}
/**
* Get the maximum of 2 integers.
*
* @param n1 Integer.
* @param n2 Integer.
* @return The biggest of n1 and n2.
*/
stock int GCIntMax(int n1, int n2)
{
return n1 > n2 ? n1 : n2;
}
/**
* Get the minimum of 2 integers.
*
* @param n1 Integer.
* @param n2 Integer.
* @return The smallest of n1 and n2.
*/
stock int GCIntMin(int n1, int n2)
{
return n1 < n2 ? n1 : n2;
}
/**
* Checks if an integer is within a range
*
* @param number Integer to check.
* @param min Minimum range.
* @param max Maximum range.
* @return True on success, false otherwise.
*/
stock bool GCIsIntInRange(int number, int min, int max)
{
return number >= min && number <= max;
}
/**
* Calculates a float percentage from a common fraction.
*
* @param numerator Numerator.
* @param denominator Denominator.
* @return Float percentage. -1.0 on failure.
*/
stock float GCCalcIntPercentage(int numerator, int denominator)
{
return float(numerator) / float(denominator) * 100.0;
}
/**
* Integer power.
* Returns the base raised to the power of the exponent.
* Returns 0 if exponent is negative.
*
* @param base Base to be raised.
* @param exponent Value to raise the base.
* @return Value to the power of exponent.
*/
stock int GCIntPow(int base, int exponent)
{
if (exponent < 0)
{
return 0;
}
int result = 1;
for (;;)
{
if (exponent & 1)
{
result *= base
}
exponent >>= 1;
if (!exponent)
{
break;
}
base *= base;
}
return result;
}
/**
* Swaps the values of 2 variables.
*
* @param cell1 Cell 1.
* @param cell2 Cell 2.
*/
stock void GCSwapCells(any &cell1, any &cell2)
{
any temp = cell1;
cell1 = cell2;
cell2 = temp;
}
/**
* Clamps an int between min and max.
*
* @param value Float to clamp.
* @param min Minimum range.
* @param max Maximum range.
* @return Clamped value.
*/
stock int GCIntClamp(int value, int min, int max)
{
if (value < min)
{
return min;
}
if (value > max)
{
return max;
}
return value;
}
/**
* Returns the biggest of 2 values.
*
* @param num1 Number 1.
* @param num2 Number 2.
* @return Biggest number.
*/
stock float GCFloatMax(float num1, float num2)
{
if (num1 > num2)
{
return num1;
}
return num2;
}
/**
* Returns the smallest of 2 values.
*
* @param num1 Number 1.
* @param num2 Number 2.
* @return Smallest number.
*/
stock float GCFloatMin(float num1, float num2)
{
if (num1 < num2)
{
return num1;
}
return num2;
}
/**
* Clamps a float between min and max.
*
* @param value Float to clamp.
* @param min Minimum range.
* @param max Maximum range.
* @return Clamped value.
*/
stock float GCFloatClamp(float value, float min, float max)
{
if (value < min)
{
return min;
}
if (value > max)
{
return max;
}
return value;
}
|