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
|
#pragma once
#include <ctype.h>
class CGeneral
{
public:
static float GetATanOfXY(float x, float y){
float arc = atan2f(y, x);
if(arc < 0.0f)
arc += TWOPI;
return arc;
}
static float LimitAngle(float angle)
{
float result = angle;
while (result >= 180.0f) {
result -= 2 * 180.0f;
}
while (result < -180.0f) {
result += 2 * 180.0f;
}
return result;
}
static float LimitRadianAngle(float angle)
{
float result = Clamp(angle, -25.0f, 25.0f);
while (result >= PI) {
result -= 2 * PI;
}
while (result < -PI) {
result += 2 * PI;
}
return result;
}
// Returns an angle such that x2/y2 looks at x1/y1 with its forward vector if rotated by that angle
static float GetRadianAngleBetweenPoints(float x1, float y1, float x2, float y2)
{
float x = x2 - x1;
float y = y2 - y1;
if (y == 0.0f)
y = 0.0001f;
if (x > 0.0f) {
if (y > 0.0f)
return PI - Atan2(x / y, 1.0f);
else
return -Atan2(x / y, 1.0f);
} else {
if (y > 0.0f)
return -(PI + Atan2(x / y, 1.0f));
else
return -Atan2(x / y, 1.0f);
}
}
static float GetAngleBetweenPoints(float x1, float y1, float x2, float y2)
{
return RADTODEG(GetRadianAngleBetweenPoints(x1, y1, x2, y2));
}
// should return direction in 0-8 range. fits perfectly to peds' path directions.
static int GetNodeHeadingFromVector(float x, float y)
{
float angle = CGeneral::GetRadianAngleBetweenPoints(x, y, 0.0f, 0.0f);
if (angle < 0.0f)
angle += TWOPI;
angle = DEGTORAD(22.5f) + TWOPI - angle;
if (angle >= TWOPI)
angle -= TWOPI;
return (int)Floor(angle / DEGTORAD(45.0f));
}
// Unlike usual string comparison functions, these don't care about greater or lesser
static bool faststrcmp(const char *str1, const char *str2)
{
for (; *str1; str1++, str2++) {
if (*str1 != *str2)
return true;
}
return *str2 != '\0';
}
static bool faststrncmp(const char *str1, const char *str2, uint32 count)
{
for(uint32 i = 0; *str1 && i < count; str1++, str2++, i++) {
if (*str1 != *str2)
return true;
}
return false;
}
static bool faststricmp(const char *str1, const char *str2)
{
for (; *str1; str1++, str2++) {
#ifndef ASCII_STRCMP
if (toupper(*str1) != toupper(*str2))
#else
if (__ascii_toupper(*str1) != __ascii_toupper(*str2))
#endif
return true;
}
return *str2 != '\0';
}
static bool SolveQuadratic(float a, float b, float c, float &root1, float &root2)
{
float discriminant = b * b - 4.f * a * c;
if (discriminant < 0.f)
return false;
float discriminantSqrt = Sqrt(discriminant);
root2 = (-b + discriminantSqrt) / (2.f * a);
root1 = (-b - discriminantSqrt) / (2.f * a);
return true;
}
// not too sure about all these...
static uint16 GetRandomNumber(void)
{ return myrand() & MYRAND_MAX; }
static bool GetRandomTrueFalse(void)
{ return GetRandomNumber() < MYRAND_MAX / 2; }
// Probably don't want to ever reach high
static float GetRandomNumberInRange(float low, float high)
{ return low + (high - low)*(GetRandomNumber()/float(MYRAND_MAX + 1)); }
static int32 GetRandomNumberInRange(int32 low, int32 high)
{ return low + (high - low)*(GetRandomNumber()/float(MYRAND_MAX + 1)); }
static void SetRandomSeed(int32 seed)
{ mysrand(seed); }
};
|