2019-05-30 00:47:33 +02:00
|
|
|
#pragma once
|
2019-05-29 20:02:58 +02:00
|
|
|
|
2020-03-27 20:53:47 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2019-05-15 16:52:37 +02:00
|
|
|
class CGeneral
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static float GetATanOfXY(float x, float y){
|
2019-05-29 18:06:33 +02:00
|
|
|
if(x == 0.0f && y == 0.0f)
|
|
|
|
return 0.0f;
|
2019-07-10 17:18:26 +02:00
|
|
|
float xabs = Abs(x);
|
|
|
|
float yabs = Abs(y);
|
2019-05-29 18:06:33 +02:00
|
|
|
|
|
|
|
if(xabs < yabs){
|
|
|
|
if(y > 0.0f){
|
|
|
|
if(x > 0.0f)
|
2019-07-10 17:18:26 +02:00
|
|
|
return 0.5f*PI - Atan2(x / y, 1.0f);
|
2019-05-29 18:06:33 +02:00
|
|
|
else
|
2019-07-10 17:18:26 +02:00
|
|
|
return 0.5f*PI + Atan2(-x / y, 1.0f);
|
2019-05-29 18:06:33 +02:00
|
|
|
}else{
|
|
|
|
if(x > 0.0f)
|
2019-07-10 17:18:26 +02:00
|
|
|
return 1.5f*PI + Atan2(x / -y, 1.0f);
|
2019-05-29 18:06:33 +02:00
|
|
|
else
|
2019-07-10 17:18:26 +02:00
|
|
|
return 1.5f*PI - Atan2(-x / -y, 1.0f);
|
2019-05-29 18:06:33 +02:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(y > 0.0f){
|
|
|
|
if(x > 0.0f)
|
2019-07-10 17:18:26 +02:00
|
|
|
return Atan2(y / x, 1.0f);
|
2019-05-29 18:06:33 +02:00
|
|
|
else
|
2019-07-10 17:18:26 +02:00
|
|
|
return PI - Atan2(y / -x, 1.0f);
|
2019-05-29 18:06:33 +02:00
|
|
|
}else{
|
|
|
|
if(x > 0.0f)
|
2019-07-10 17:18:26 +02:00
|
|
|
return 2.0f*PI - Atan2(-y / x, 1.0f);
|
2019-05-29 18:06:33 +02:00
|
|
|
else
|
2019-07-10 17:18:26 +02:00
|
|
|
return PI + Atan2(-y / -x, 1.0f);
|
2019-05-29 18:06:33 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 16:52:37 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 14:11:40 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-20 13:22:44 +02:00
|
|
|
static float LimitRadianAngle(float angle)
|
|
|
|
{
|
2019-09-16 19:32:58 +02:00
|
|
|
float result = clamp(angle, -25.0f, 25.0f);
|
2019-06-20 13:22:44 +02:00
|
|
|
|
|
|
|
while (result >= PI) {
|
|
|
|
result -= 2 * PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (result < -PI) {
|
|
|
|
result += 2 * PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:42:18 +02:00
|
|
|
// Returns an angle such that x2/y2 looks at x1/y1 with its forward vector if rotated by that angle
|
2019-06-20 13:22:44 +02:00
|
|
|
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)
|
2019-07-10 17:18:26 +02:00
|
|
|
return PI - Atan2(x / y, 1.0f);
|
2019-06-20 13:22:44 +02:00
|
|
|
else
|
2019-07-10 17:34:11 +02:00
|
|
|
return -Atan2(x / y, 1.0f);
|
2019-06-20 13:22:44 +02:00
|
|
|
} else {
|
|
|
|
if (y > 0.0f)
|
2019-07-10 17:18:26 +02:00
|
|
|
return -(PI + Atan2(x / y, 1.0f));
|
2019-06-20 13:22:44 +02:00
|
|
|
else
|
2019-07-10 17:34:11 +02:00
|
|
|
return -Atan2(x / y, 1.0f);
|
2019-06-20 13:22:44 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-15 07:03:53 +02:00
|
|
|
|
|
|
|
static float GetAngleBetweenPoints(float x1, float y1, float x2, float y2)
|
|
|
|
{
|
|
|
|
return RADTODEG(GetRadianAngleBetweenPoints(x1, y1, x2, y2));
|
|
|
|
}
|
2019-06-20 13:22:44 +02:00
|
|
|
|
2019-08-10 17:15:22 +02:00
|
|
|
// should return direction in 0-8 range. fits perfectly to peds' path directions.
|
2019-08-16 20:17:15 +02:00
|
|
|
static int GetNodeHeadingFromVector(float x, float y)
|
2019-08-10 17:15:22 +02:00
|
|
|
{
|
|
|
|
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)floorf(angle / DEGTORAD(45.0f));
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:12:58 +01:00
|
|
|
// 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 faststricmp(const char *str1, const char *str2)
|
|
|
|
{
|
|
|
|
for (; *str1; str1++, str2++) {
|
2020-04-19 18:34:08 +02:00
|
|
|
#if MUCH_SLOWER || !defined _WIN32 || defined __MINGW32__
|
2019-10-30 00:12:58 +01:00
|
|
|
if (toupper(*str1) != toupper(*str2))
|
|
|
|
#else
|
|
|
|
if (__ascii_toupper(*str1) != __ascii_toupper(*str2))
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return *str2 != '\0';
|
|
|
|
}
|
|
|
|
|
2020-06-13 22:39:14 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-15 16:52:37 +02:00
|
|
|
// not too sure about all these...
|
|
|
|
static uint16 GetRandomNumber(void)
|
2019-07-04 23:10:04 +02:00
|
|
|
{ return myrand() & MYRAND_MAX; }
|
2019-08-10 09:44:19 +02:00
|
|
|
static bool GetRandomTrueFalse(void)
|
|
|
|
{ return GetRandomNumber() < MYRAND_MAX / 2; }
|
2019-05-15 16:52:37 +02:00
|
|
|
// Probably don't want to ever reach high
|
|
|
|
static float GetRandomNumberInRange(float low, float high)
|
2019-05-30 23:49:06 +02:00
|
|
|
{ return low + (high - low)*(GetRandomNumber()/float(MYRAND_MAX + 1)); }
|
2019-05-29 20:02:58 +02:00
|
|
|
|
2019-06-17 00:16:38 +02:00
|
|
|
static int32 GetRandomNumberInRange(int32 low, int32 high)
|
2019-05-30 23:49:06 +02:00
|
|
|
{ return low + (high - low)*(GetRandomNumber()/float(MYRAND_MAX + 1)); }
|
2020-05-09 19:21:13 +02:00
|
|
|
static void SetRandomSeed(int32 seed)
|
|
|
|
{ mysrand(seed); }
|
2019-05-15 16:52:37 +02:00
|
|
|
};
|