PDA

View Full Version : koraX's utils


koraX
25-03-2005, 00:29
so again something new :p

download (http://neuron.tuke.sk/~wagner/download_ku.php)

xml/cfg/ini parser
runtime profiler based on CPU cycles with low overhead (30 cycles)
3d vector/position/angles, math stuff, compatible with HL SDK Vector class
loger to xml, redirecting cout
thread synchronization, thread creation
float variables comparsion, random number generator
...more (detect CPU speed, string conversions, min-max, simple RGB class, ...)
... and more (process and cpu id, ISO time and date format, ...)
all optimized for usage in hl plugins, but can be used anywhere
cross platform, tested undex MSVC.NET, DevC++, MinGW, gcc
open source, free, GPL with Half-Life exception
simple documentation and many examples

Just include source code into your program, include util.h in your .cpp and you can use everything. I'm using these utils in my bot and I consider them veery fast :)

@$3.1415rin
25-03-2005, 12:46
just looking a bit thru it, looks nice

some suggestions : accordinging to some measurements I did quite some time ago, dividing all components by a factor is faster when you calculate 1/factor first and then multiply each of them, although this might be slightly less accurate due to floating point imprecisions, but this shouldnt be a problem with bot stuff at all. you did that already in the normalize function I see.

btw, and why isnt the RGB class derived from the vector class ? since then you had all arithmetrics stuff like + - whatever there too, which might be handy together with a clamp function.

koraX
25-03-2005, 17:46
btw, and why isnt the RGB class derived from the vector class ? since then you had all arithmetrics stuff like + - whatever there too, which might be handy together with a clamp function.
good point, I've done RGB class months ago and vector stuff is pretty new. But I like unsigned char for color, rather than floats :)

Pierre-Marie Baty
25-03-2005, 19:05
Nice! :) You know what I'm thinking ? Bots United should create and maintain a bot utilities DLL :)

sfx1999
25-03-2005, 20:28
Who says it would need to be a DLL? It could just be headers.

Cpl. Shrike
25-03-2005, 22:51
Darn if i just knew what yr talkin about ???:(

@$3.1415rin
26-03-2005, 09:54
good point, I've done RGB class months ago and vector stuff is pretty new. But I like unsigned char for color, rather than floats :)

then maybe write a "conversion" ctor or how those thingies are called :)

Whistler
26-03-2005, 14:44
this is from my bot, it isn't very "complicated" but may be enough for using in bots:

/*
* Copyright (c) 2004, Wei Mingzhi <whistler_wmz@users.sf.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License (version 2 or any later
* version) as published by the Free Software Foundation.
*
* [In other words, you are welcome to use, share and improve this
* program. You are forbidden to forbid anyone else to use, share
* and improve what you give them.]
*
* As a special exception, you have permission to link this program with
* the Half-Life 1, as long as you follow the requirements of the GNU GPL
* in regard to all of the software aside from HL1.
*/

//
// mathlib.h
//

#ifndef MATHLIBRARY_H
#define MATHLIBRARY_H

// Use this definition globally
#define ON_EPSILON 0.01
#define EQUAL_EPSILON 0.001

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#include <math.h>

#define FZero(v) (fabs(v) < ON_EPSILON)
#define FEqual(v1, v2) (fabs((v1) - (v2)) < EQUAL_EPSILON)
#define SQUARE(f) (f * f)

#pragma warning (disable: 4239)
#pragma warning (disable: 4244)

/**
* This function adds or substracts 360 enough times needed to the given angle in
* order to set it into the range [0, 360) and returns the resulting angle. Letting
* the engine have a hand on angles that are outside these bounds may cause the
* game to freeze by screwing up the engine math code.
*/
inline float AngleMod(float a)
{
return (360.0 / 65536) * ((int)(a * (65536.0 / 360.0)) & 65535);
}

/**
* This function adds or substracts 360 enough times needed to the given angle in
* order to set it into the range [-180, 180) and returns the resulting angle. Letting
* the engine have a hand on angles that are outside these bounds may cause the game
* to freeze by screwing up the engine math code.
*/
inline float AngleNormalize(float angle)
{
return (360.0 / 65536) * ((int)((angle + 180) * (65536.0 / 360.0)) & 65535) - 180;
}

// fast trig routine using inline assembly
void inline SinCos( float rad, float *flSin, float *flCos )
{
#ifdef __GNUC__
// GNU style assembly, works with both GNU/Linux and MinGW
register double __cosr, __sinr;
__asm __volatile__ ("fsincos" : "=t" (__cosr), "=u" (__sinr) : "0" (rad));
*flSin = __sinr;
*flCos = __cosr;
#else
#ifndef _MSC_VER
// don't use ASM as we're using some other compilers
*flSin = sinf(rad);
*flCos = cosf(rad);
#else
// Microsoft style assembly
__asm {
fld DWORD PTR[rad]
fsincos
mov edx, DWORD PTR[flCos]
mov eax, DWORD PTR[flSin]
fstp DWORD PTR[edx]
fstp DWORD PTR[eax]
}
#endif
#endif
}

inline float AngleDiff( float destAngle, float srcAngle )
{
return AngleNormalize(destAngle - srcAngle);
}

// 2D Vector
class Vector2D
{
public:
Vector2D() : x(0), y(0) {}
Vector2D(float a, float b) : x(a), y(b) {}
Vector2D(float *v) : x(v[0]), y(v[1]) {}

inline Vector2D operator-(void) const
{
return Vector2D(-x, -y);
}

inline Vector2D operator+(const Vector2D &v) const
{
return Vector2D(x + v.x, y + v.y);
}

inline Vector2D operator-(const Vector2D &v) const
{
return Vector2D(x - v.x, y - v.y);
}

inline Vector2D operator*(float f) const
{
return Vector2D(x * f, y * f);
}

inline Vector2D operator/(float f) const
{
return Vector2D(x / f, y / f);
}

inline bool operator==(const Vector2D &v) const
{
return FEqual(x, v.x) && FEqual(y, v.y);
}

inline bool operator!=(const Vector2D &v) const
{
return !FEqual(x, v.x) && !FEqual(y, v.y);
}

inline double Length() const
{
return sqrt((double)x * (double)x + (double)y * (double)y);
}

inline double LengthSquared() const
{
return (double)x * (double)x + (double)y * (double)y;
}

inline operator float *()
{
return &x;
}

inline operator const float *() const
{
return &x;
}

inline Vector2D Normalize(void) const
{
if (FZero(x) && FZero(y))
return Vector2D(0, 0); // reliability check

double l = 1 / Length();
return Vector2D(x * l, y * l);
}

float x, y;
};

inline double DotProduct(const Vector2D &a, const Vector2D &b)
{
return (double)a.x * (double)b.x + (double)a.y + (double)b.y;
}

// 3D Vector
class Vector
{
public:
Vector() : x(0), y(0), z(0) {}
Vector(float a, float b, float c) : x(a), y(b), z(c) {}
Vector(float *v) : x(v[0]), y(v[1]), z(v[2]) {}

inline Vector operator-(void) const
{
return Vector(-x, -y, -z);
}

inline Vector operator+(const Vector &v) const
{
return Vector(x + v.x, y + v.y, z + v.z);
}

inline Vector operator-(const Vector &v) const
{
return Vector(x - v.x, y - v.y, z - v.z);
}

inline Vector operator*(float f) const
{
return Vector(x * f, y * f, z * f);
}

inline Vector operator/(float f) const
{
return Vector(x / f, y / f, z / f);
}

inline bool operator==(const Vector &v) const
{
return FEqual(x, v.x) && FEqual(y, v.y) && FEqual(z, v.z);
}

inline bool operator!=(const Vector &v) const
{
return !FEqual(x, v.x) && !FEqual(y, v.y) && !FEqual(z, v.z);
}

inline double Length() const
{
return sqrt((double)x * (double)x + (double)y * (double)y +
(double)z * (double)z);
}

inline double LengthSquared() const
{
return (double)x * (double)x + (double)y * (double)y +
(double)z * (double)z;
}

inline double Length2D() const
{
return sqrt((double)x * (double)x + (double)y * (double)y);
}

inline double LengthSquared2D() const
{
return (double)x * (double)x + (double)y * (double)y;
}

inline operator float *()
{
return &x;
}

inline operator const float *() const
{
return &x;
}

inline Vector Normalize(void) const
{
if (FZero(x) && FZero(y) && FZero(z))
return Vector(0, 0, 1); // reliability check

double l = 1 / Length();
return Vector(x * l, y * l, z * l);
}

inline Vector2D Make2D(void) const
{
return Vector2D(x, y);
}

inline void ClampAngles(void)
{
x = AngleNormalize(x);
y = AngleNormalize(y);
z = 0;
}

// The purpose of this function is to convert a spatial location determined by the vector
// passed in into an absolute Y angle (yaw) from the origin of the world.
inline float ToYaw(void) const
{
if (FZero(x) && FZero(y))
return 0;
else
return atan2(y, x) * (180 / M_PI);
}

// The purpose of this function is to convert a spatial location determined by the vector
// passed in into an absolute X angle (pitch) from the origin of the world.
inline float ToPitch(void) const
{
if (FZero(x) && FZero(y))
return 0;
else
return atan2(z, Length2D()) * (180 / M_PI);
}

// The purpose of this function is to convert a spatial location determined by the vector
// passed in into absolute angles from the origin of the world.
inline Vector ToAngles(void) const
{
float yaw, pitch;

if (FZero(x) && FZero(y)) {
yaw = 0;
pitch = (z > 0) ? 90 : 270;
} else {
yaw = atan2(y, x) * (180 / M_PI);
pitch = atan2(z, Length2D()) * (180 / M_PI);
}

return Vector(pitch, yaw, 0);
}

// This function builds a 3D referential from a view angle, that is to say, the relative
// "forward", "right" and "upwards" direction that a player would have if he were facing this
// view angle. World angles are stored in Vector structs too, the "x" component corresponding
// to the X angle (horizontal angle), and the "y" component corresponding to the Y angle
// (vertical angle).
inline void AngleVectors( Vector *v_forward,
Vector *v_right = 0,
Vector *v_up = 0 ) const
{
float sp = 0, cp = 0, sy = 0, cy = 0, sr = 0, cr = 0;
float angle = x * (M_PI / 180);
SinCos(angle, &sp, &cp);
angle = y * (M_PI / 180);
SinCos(angle, &sy, &cy);
angle = z * (M_PI / 180);
SinCos(angle, &sr, &cr);

if (v_forward) {
v_forward->x = cp * cy;
v_forward->y = cp * sy;
v_forward->z = -sp;
}

if (v_right) {
v_right->x = -sr * sp * cy + cr * sy;
v_right->y = -sr * sp * sy - cr * cy;
v_right->z = -sr * cp;
}

if (v_up) {
v_up->x = cr * sp * cy + sr * sy;
v_up->y = cr * sp * sy - sr * cy;
v_up->z = cr * cp;
}
}

// This function returns the angle in degrees between the two vectors, regardless of
// the axial planes (ie, considering the plane formed by the vectors themselves)
inline float AngleOf(const Vector &v)
{
Vector v1 = Normalize(), v2 = v.Normalize();
return acos((double)v1.x * (double)v2.x + (double)v1.y * (double)v2.y +
(double)v1.z * (double)v2.z);
}

float x, y, z;
};

static const Vector NULLVEC(0, 0, 0);

inline double DotProduct(const Vector &a, const Vector &b)
{
return (double)a.x * (double)b.x + (double)a.y * (double)b.y +
(double)a.z * (double)b.z;
}

inline Vector CrossProduct(const Vector &a, const Vector &b)
{
return Vector((double)a.y * (double)b.z - (double)a.z * (double)b.y,
(double)a.z * (double)b.x - (double)a.x * (double)b.z,
(double)a.x * (double)b.y - (double)a.y * (double)b.x);
}

// Returns if the specified point is inside the bounding box specified
// by origin, mins, maxs.
inline bool PointInsideBoundingBox(const Vector &point,
const Vector &origin,
const Vector &mins,
const Vector &maxs)
{
float deltaX = origin.x - point.x;
float deltaY = origin.y - point.y;
float deltaZ = origin.z - point.z;

return (deltaX >= mins.x && deltaX <= maxs.x &&
deltaY >= mins.y && deltaY <= maxs.y &&
deltaZ >= mins.z && deltaZ <= maxs.z);
}

// Returns if the specified two bounding boxes are touching each other.
inline bool BoundingBoxesTouching(const Vector &origin1,
const Vector &mins1,
const Vector &maxs1,
const Vector &origin2,
const Vector &mins2,
const Vector &maxs2)
{
return (origin1.x + maxs1.x >= origin2.x + mins2.x &&
origin1.x + mins1.x <= origin2.x + maxs2.x &&
origin1.y + maxs1.y >= origin2.y + mins2.y &&
origin1.y + mins1.y <= origin2.y + maxs2.y &&
origin1.z + maxs1.z >= origin2.z + mins2.z &&
origin1.z + mins1.z <= origin2.z + maxs2.z);
}

#endif



warning: this hasn't been fully tested anyway so be aware of (possible) bugs :)

@$3.1415rin
26-03-2005, 14:48
yep, that sincos asm stuff would be definately something to have on a 'bots united util lib'

although i'm not sure about that 2d vector, would it be missed if it wasnt there ?

Pierre-Marie Baty
26-03-2005, 19:44
nice comments in Whistler's code :P ;)

why not maintain this ASM stuff in the wiki ?

sfx1999
27-03-2005, 03:13
If only I knew how to get assembler working in MinGW.

mirv
27-03-2005, 08:31
Though I don't do much assembly for the stock standard PC (though I've done some work with microcontrollers) it may be a good idea make sure things run on 32bit and 64bit machines if any library is made. Endian correctness too, if applicable. Most of that can be done with #defines. I haven't come across many endian problems (except where forgetting about network data conversions), though I have had some with getting really useful libraries working under a 64bit environment.
Note that I only had a brief look at the code posted earlier, so dunno if any of that applies; I'm posting more as a general comment.

Pierre-Marie Baty
27-03-2005, 18:45
Do you really think endian correctness is necessary for... games ?

I mean today pretty much everything runs on x86. I doubt we'll see a HL2 server on an IBM machine.

mirv
30-03-2005, 09:06
If the games run on an mac powerpc system, that's big endian. Some of the larger software titles have been ported to run on mac systems.

Pierre-Marie Baty
01-04-2005, 01:49
My bad, I thought the Macs had left the 68000 platform :-/