2
// C++ Implementation: bot_util
4
// Description: Basic util functions, mostly bot related
7
// Author: <rickhelmus@gmail.com>
16
// Function code by PMB - Begin
19
// Random functions begin
21
// http://forums.bots-united.com/showthread.php?t=244&page=1&pp=10&highlight=random
22
// So credits to PMB and the person(s) who actually made it :]
24
// maximum value returned by our number generator (2^31 - 1 = 0x7FFFFFFF)
25
#define LRAND_MAX 2147483647L
26
long lseed; // our random number generator's seed
30
// this function is the equivalent of the rand() standard C library function,
31
// except that whereas rand() works only with short integers
32
// (i.e. not above 32767), this function is able to generate 32-bit random
33
// numbers. Isn't that nice ?
34
// credits go to Ray Gardner for his fast implementation of minimal random
36
// http://c.snippets.org/snip_lister.php?fname=rg_rand.c
38
// compose the two 16-bit parts of the long integer and assemble them
39
static unsigned long lrand_lo, lrand_hi;
40
lrand_lo = 16807 * (long) (lseed & 0xFFFF); // low part
41
lrand_hi = 16807 * (long) ((unsigned long) lseed >> 16);
43
lrand_lo += (lrand_hi & 0x7FFF) << 16;
44
// assemble both in lrand_lo
45
// is the resulting number greater than LRAND_MAX (half the capacity) ?
46
if (lrand_lo > LRAND_MAX)
48
lrand_lo &= LRAND_MAX; // then get rid of the disturbing bit
49
lrand_lo++; // and increase it a bit (to avoid overflow problems, I suppose)
52
lrand_lo += lrand_hi >> 15;
53
// now do twisted maths to generate the next seed
54
// is the resulting number greater than LRAND_MAX (half the capacity) ?
55
if (lrand_lo > LRAND_MAX)
57
lrand_lo &= LRAND_MAX; // then get rid of the disturbing bit
58
lrand_lo++; // and increase it a bit (to avoid overflow problems, I suppose)
60
// now we've got our (pseudo-)random number.
61
lseed = (long) lrand_lo; // put it in the seed for next time
62
return (lseed); // and return it. Yeah, simple as that.
65
void lsrand (unsigned long initial_seed)
67
// this function initializes the random seed based on the initial seed value
68
// passed in the initial_seed parameter. Since random seeds are
69
// usually initialized with the time of day, and time is a value that
70
// changes slowly, we bump the seed twice to have it in a more
71
// random state for future calls of the random number generator.
73
// fill in the initial seed of the random number generator
74
lseed = (long) initial_seed;
75
lseed = lrand (); // bump it once
76
lseed = lrand (); // bump it twice
77
return; // that's all folks
80
long RandomLong (long from, long to)
82
// this function returns a random integer number between (and including)
83
// the starting and ending values passed by parameters from and to.
87
return (from + lrand () / (LRAND_MAX / (to - from + 1)));
90
float RandomFloat (float from, float to)
92
// this function returns a random floating-point number between (and including)
93
// the starting and ending values passed by parameters from and to.
98
return (from + (float) lrand () / (LRAND_MAX / (to - from)));
101
// End random functions
103
void AnglesToVectors(vec angles, vec &forward, vec &right, vec &up)
105
static float degrees_to_radians = 2 * PI / 360;
114
// For some reason this has to be done :)
116
angles.x = -angles.x;
119
// compute the sin and cosine of the pitch component
120
angle = angles.x * degrees_to_radians;
121
sin_pitch = sinf (angle);
122
cos_pitch = cosf (angle);
124
// compute the sin and cosine of the yaw component
125
angle = angles.y * degrees_to_radians;
126
sin_yaw = sinf (angle);
127
cos_yaw = cosf (angle);
129
// compute the sin and cosine of the roll component
130
angle = angles.z * degrees_to_radians;
131
sin_roll = sinf (angle);
132
cos_roll = cosf (angle);
134
// build the FORWARD vector
135
forward.x = cos_pitch * cos_yaw;
136
forward.y = cos_pitch * sin_yaw;
137
forward.z = -sin_pitch;
139
// build the RIGHT vector
140
right.x = -(-(sin_roll * sin_pitch * cos_yaw) - (cos_roll * -sin_yaw));
141
right.y = -(-(sin_roll * sin_pitch * sin_yaw) - (cos_roll * cos_yaw));
142
right.z = -(sin_roll * cos_pitch);
144
// build the UPWARDS vector
145
up.x = ((cos_roll * sin_pitch * cos_yaw) - (sin_roll * -sin_yaw));
146
up.y = ((cos_roll * sin_pitch * sin_yaw) - (sin_roll * cos_yaw));
147
up.z = cos_roll * cos_pitch;
152
// Function code by PMB - End
154
float WrapXAngle(float angle)
156
if (angle > 90) angle = 90;
157
else if (angle < -90) angle = -90;
162
float WrapYZAngle(float angle)
164
while (angle >= 360.0f) angle -= 360.0f;
165
while (angle < 0.0f) angle += 360.0f;
170
// Sees how far it can come from 'from' to 'to' and puts the end vector in 'end'
171
// (1337 description :-)
172
// UNDONE: Optimize this as much as possible
173
void TraceLine(vec from, vec to, dynent *pTracer, bool CheckPlayers, traceresult_s *tr,
177
tr->collided = false;
179
static float flNearestDist, flDist;
183
flNearestDist = 9999.0f;
185
if(OUTBORD((int)from.x, (int)from.y)) return;
187
// Check if the 'line' collides with entities like mapmodels
191
if(e.type!=MAPMODEL) continue; // Only check map models for now
193
mapmodelinfo &mmi = getmminfo(e.attr2);
194
if(!&mmi || !mmi.h) continue;
196
float lo = (float)(S(e.x, e.y)->floor+mmi.zoff+e.attr3);
198
float z = (fabs(from.z-lo) < fabs(from.z-hi)) ? lo : hi;
199
makevec(&v, e.x, e.y, z);
200
flDist = GetDistance(from, v);
202
if ((flDist < flNearestDist) && (intersect(&e, from, to, &tr->end)))
204
flNearestDist = GetDistance(from, tr->end);
211
// Check if the 'line' collides with players
214
playerent *d = players[i];
215
if(!d || (d==pTracer) || (d->state != CS_ALIVE)) continue; // Only check valid players
217
flDist = GetDistance(from, d->o);
219
if ((flDist < flNearestDist) && (intersect(d, from, to, &tr->end)))
221
flNearestDist = flDist;
226
// Check if the 'line' collides with the local player(player1)
227
playerent *d = player1; // Shortcut
228
if (d && (d!=pTracer) && !BotManager.m_pBotToView && (d->state == CS_ALIVE))
230
flDist = GetDistance(from, d->o);
232
if ((flDist < flNearestDist) && (intersect(d, from, to, &tr->end)))
234
flNearestDist = flDist;
241
float dx = to.x-from.x;
242
float dy = to.y-from.y;
243
int steps = (int)(sqrt(dx*dx+dy*dy)/0.9);
244
if(!steps) // If from and to are on the same cube...
246
if (GetDistance(from, to) < flNearestDist)
249
sqr *s = S(int(from.x), int(from.y));
250
float flr = GetCubeFloor(int(from.x), int(from.y));
251
solid = ((!SkipTags || !s->tag) && SOLID(s));
252
if (solid || (to.z < flr) || (to.z > s->ceil))
255
tr->end.z = (fabs(to.z-s->ceil) < fabs(to.z-flr)) ? s->ceil : flr;
266
// Now check if the 'line' is hit by a cube
269
if(OUTBORD((int)x, (int)y)) break;
270
if (GetDistance(from, endcube) >= flNearestDist) break;
271
sqr *s = S(int(x), int(y));
272
solid = ((!SkipTags || !s->tag) && SOLID(s));
274
float floor = s->floor;
275
if(s->type==FHF) floor -= s->vdelta/4.0f;
276
float ceil = s->ceil;
277
if(s->type==CHF) ceil += s->vdelta/4.0f;
278
float rz = from.z-((from.z-to.z)*(i/(float)steps));
279
if(rz<floor || rz>ceil) break;
284
x += dx/(float)steps;
285
y += dy/(float)steps;
289
if ((i>=steps) && !tr->collided)
296
if (GetDistance(from, endcube) < flNearestDist)
303
float GetDistance(vec v1, vec v2)
305
if ((v1.x == 0) && (v2.x == 0) && (v1.y == 0) && (v2.y == 0) && (v1.z == 0) &&
312
// As GetDistance(), but doesn take height in account.
313
float Get2DDistance(vec v1, vec v2)
315
if ((v1.x == 0) && (v2.x == 0) && (v1.y == 0) && (v2.y == 0))
322
bool IsVisible(vec v1, vec v2, dynent *tracer, bool SkipTags)
325
TraceLine(v1, v2, tracer, (tracer!=NULL), &tr, SkipTags);
330
// - pos: Current position
331
// - vel: Current velocity
332
// - Time: In seconds, predict how far it is in Time seconds
333
vec PredictPos(vec pos, vec vel, float Time)
335
float flVelLength = vel.magnitude();
337
if (flVelLength <= 1.0)
338
return pos; // don't bother with low velocities...
346
// returns true if the given file is valid
347
bool IsValidFile(const char *szFileName)
349
FILE *fp = fopen(szFileName, "r");
360
bool FileIsOlder(const char *szFileName1, const char *szFileName2)
363
struct stat stat1, stat2;
365
file1 = open(szFileName1, O_RDONLY);
366
file2 = open(szFileName2, O_RDONLY);
368
fstat(file1, &stat1);
369
fstat(file2, &stat2);
374
return(stat1.st_mtime < stat2.st_mtime);*/ return false; // FIXME
379
float flLen = sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
380
static vec v_norm = g_vecZero;
383
v_norm.x=0;v_norm.y=0;v_norm.z=1;
388
v_norm.x = v.x * flLen; v_norm.y = v.y * flLen; v_norm.z = v.z * flLen;
393
float GetYawDiff(float curyaw, vec v1, vec v2)
395
float yaw = -(float)atan2(v2.x - v1.x, v2.y - v1.y)/PI*180+180;
399
vec CrossProduct(const vec &a, const vec &b)
401
vec cross(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
405
int GetDirection(const vec &angles, const vec &v1, const vec &v2)
407
vec tmp, forward, right, up, cross;
410
AnglesToVectors(angles, forward, right, up);
414
tmp.z = 0.0f; // Make 2D
415
tmp = Normalize(tmp);
416
forward.z = 0; // Make 2D
418
flDot = tmp.dot(forward);
419
cross = CrossProduct(tmp, forward);
423
if (cross.z > 0.1f) Dir |= LEFT;
424
else if (cross.z < -0.1f) Dir |= RIGHT;
425
if (flDot < -0.1f) Dir |= BACKWARD;
426
else if (flDot > 0.1f) Dir |= FORWARD;
428
if (v1.z > v2.z) Dir |= DOWN;
429
if (v1.z < v2.z) Dir |= UP;
434
float GetCubeFloor(int x, int y)
437
float floor = s->floor;
439
floor -= (s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f;
443
float GetCubeHeight(int x, int y)
446
float ceil = s->ceil;
448
ceil += (s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f;
452
const char *SkillNrToSkillName(short skillnr)
454
if (skillnr == 0) return "best";
455
else if (skillnr == 1) return "good";
456
else if (skillnr == 2) return "medium";
457
else if (skillnr == 3) return "worse";
458
else if (skillnr == 4) return "bad";
463
bool IsInGame(dynent *d)
465
if (!d) return false;
467
if (d->type==ENT_BOT)
484
if (!players[i] || (players[i]->state == CS_DEDHOST)) continue;
485
#elif defined(AC_CUBE)
486
if (!players[i]) continue;