File: sv_wallhack.c

package info (click to toggle)
iortcw 1.51.c%2Bdfsg1-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 25,304 kB
  • sloc: ansic: 457,326; cpp: 6,507; makefile: 4,737; sh: 1,292; asm: 1,176; xml: 31
file content (589 lines) | stat: -rw-r--r-- 14,946 bytes parent folder | download | duplicates (5)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
   sv_wallhack.c -- functions to prevent wallhack cheats

   Copyright (C) 2013 Laszlo Menczel

   This is free software distributed under the terms of the GNU
   General Public License version 2. NO WARRANTY, see 'LICENSE.TXT'.
 */

#ifdef ANTIWALLHACK		// added whole file

#include <time.h>               // for random seed generation
#include "server.h"

//======================================================================

static vec3_t pred_ppos, pred_opos;

static trajectory_t traject;

static int rand_seed;

static float delta_sign[8][3] =
{
	{  1,  1,  1  },
	{  1,  1,  1  },
	{  1,  1, -1  },
	{  1,  1, -1  },
	{ -1,  1,  1  },
	{  1, -1,  1  },
	{ -1,  1, -1  },
	{  1, -1, -1  }
};

static vec3_t delta[8];

//======================================================================
// local functions
//======================================================================

#define POS_LIM     1.0
#define NEG_LIM    -1.0

static int zero_vector(vec3_t v)
{
	if (v[0] > POS_LIM || v[0] < NEG_LIM)
		return 0;

	if (v[1] > POS_LIM || v[1] < NEG_LIM)
		return 0;

	if (v[2] > POS_LIM || v[2] < NEG_LIM)
		return 0;

	return 1;
}

//======================================================================
/*
   The following functions for predicting player positions
   have been adopted from 'g_unlagged.c' which is part of the
   'unlagged' system created by Neil "haste" Toronto.
   WEB site: http://www.ra.is/unlagged
 */

#define OVERCLIP                1.001f

static void predict_clip_velocity(vec3_t in, vec3_t normal, vec3_t out)
{
	float backoff;

	// find the magnitude of the vector "in" along "normal"
	backoff = DotProduct(in, normal);

	// tilt the plane a bit to avoid floating-point error issues
	if (backoff < 0)
		backoff *= OVERCLIP;
	else
		backoff /= OVERCLIP;

	// slide along
	VectorMA(in, -backoff, normal, out);
}

//======================================================================

#define MAX_CLIP_PLANES   5
#define Z_ADJUST          1

static int predict_slide_move(sharedEntity_t * ent, float frametime, trajectory_t * tr, vec3_t result)
{
	int count, numbumps, numplanes, i, j, k;

	float d, time_left, into;

	vec3_t planes[MAX_CLIP_PLANES],
	       velocity, origin, clipVelocity, endVelocity, endClipVelocity, dir, end;

	trace_t trace;

	numbumps = 4;

	VectorCopy(tr->trBase, origin);
	origin[2] += Z_ADJUST;  // move it off the floor

	VectorCopy(tr->trDelta, velocity);
	VectorCopy(tr->trDelta, endVelocity);

	time_left = frametime;

	numplanes = 0;

	for (count = 0; count < numbumps; count++)
	{
		// calculate position we are trying to move to
		VectorMA(origin, time_left, velocity, end);

		// see if we can make it there
		SV_Trace(&trace, origin, ent->r.mins, ent->r.maxs, end, ent->s.number,
		         CONTENTS_SOLID, qfalse);

		if (trace.allsolid)
		{
			// entity is completely trapped in another solid
			VectorCopy(origin, result);
			return 0;
		}

		if (trace.fraction > 0.99) // moved the entire distance
		{
			VectorCopy(trace.endpos, result);
			return 1;
		}

		if (trace.fraction > 0) // covered some distance
			VectorCopy(trace.endpos, origin);

		time_left -= time_left * trace.fraction;

		if (numplanes >= MAX_CLIP_PLANES)
		{
			// this shouldn't really happen
			VectorCopy(origin, result);
			return 0;
		}

		// if this is the same plane we hit before, nudge velocity
		// out along it, which fixes some epsilon issues with
		// non-axial planes
		for (i = 0; i < numplanes; i++)
			if (DotProduct(trace.plane.normal, planes[i]) > 0.99)
			{
				VectorAdd(trace.plane.normal, velocity, velocity);
				break;
			}

		if (i < numplanes)
			continue;

		VectorCopy(trace.plane.normal, planes[numplanes]);
		numplanes++;

		// modify velocity so it parallels all of the clip planes
		// find a plane that it enters
		for (i = 0; i < numplanes; i++)
		{
			into = DotProduct(velocity, planes[i]);
			if (into >= 0.1) // move doesn't interact with the plane
				continue;

			// slide along the plane
			predict_clip_velocity(velocity, planes[i], clipVelocity);

			// slide along the plane
			predict_clip_velocity(endVelocity, planes[i], endClipVelocity);

			// see if there is a second plane that the new move enters
			for (j = 0; j < numplanes; j++)
			{
				if (j == i)
					continue;

				if (DotProduct(clipVelocity, planes[j]) >= 0.1) // move doesn't interact with the plane
					continue;

				// try clipping the move to the plane
				predict_clip_velocity(clipVelocity, planes[j], clipVelocity);
				predict_clip_velocity(endClipVelocity, planes[j], endClipVelocity);

				// see if it goes back into the first clip plane
				if (DotProduct(clipVelocity, planes[i]) >= 0)
					continue;

				// slide the original velocity along the crease
				CrossProduct(planes[i], planes[j], dir);
				VectorNormalize(dir);
				d = DotProduct(dir, velocity);
				VectorScale(dir, d, clipVelocity);

				CrossProduct(planes[i], planes[j], dir);
				VectorNormalize(dir);
				d = DotProduct(dir, endVelocity);
				VectorScale(dir, d, endClipVelocity);

				// see if there is a third plane the new move enters
				for (k = 0; k < numplanes; k++)
				{
					if (k == i || k == j)
						continue;

					if (DotProduct(clipVelocity, planes[k]) >= 0.1) // move doesn't interact with the plane
						continue;

					// stop dead at a tripple plane interaction
					VectorCopy(origin, result);
					return 1;
				}
			}

			// if we have fixed all interactions, try another move
			VectorCopy(clipVelocity, velocity);
			VectorCopy(endClipVelocity, endVelocity);
			break;
		}
	}

	VectorCopy(origin, result);

	if (count == 0)
		return 1;

	return 0;
}

//======================================================================

#define STEPSIZE 18

// 'frametime' is interpreted as seconds

static void predict_move(sharedEntity_t * ent, float frametime, trajectory_t * tr, vec3_t result)
{
	float stepSize;

	vec3_t start_o, start_v, down, up;

	trace_t trace;

	VectorCopy(tr->trBase, result); // assume the move fails

	if (zero_vector(tr->trDelta)) // not moving
		return;

	if (predict_slide_move(ent, frametime, tr, result)) // move completed
		return;

	VectorCopy(tr->trBase, start_o);
	VectorCopy(tr->trDelta, start_v);

	VectorCopy(start_o, up);
	up[2] += STEPSIZE;

	// test the player position if they were a stepheight higher
	SV_Trace(&trace, start_o, ent->r.mins, ent->r.maxs, up, ent->s.number,
	         CONTENTS_SOLID, qfalse);

	if (trace.allsolid)     // can't step up
		return;

	stepSize = trace.endpos[2] - start_o[2];

	// try slidemove from this position
	VectorCopy(trace.endpos, tr->trBase);
	VectorCopy(start_v, tr->trDelta);

	predict_slide_move(ent, frametime, tr, result);

	// push down the final amount
	VectorCopy(tr->trBase, down);
	down[2] -= stepSize;
	SV_Trace(&trace, tr->trBase, ent->r.mins, ent->r.maxs, down, ent->s.number,
	         CONTENTS_SOLID, qfalse);

	if (!trace.allsolid)
		VectorCopy(trace.endpos, result);
}

//======================================================================
/*
   Calculates the view point of a player model at position 'org' using
   information in the player state 'ps' of its client, and stores the
   viewpoint coordinates in 'vp'.
 */

static void calc_viewpoint(playerState_t * ps, vec3_t org, vec3_t vp)
{
	VectorCopy(org, vp);

	if ( ps->leanf != 0 )
	{
		vec3_t right, v3ViewAngles;
		VectorCopy( ps->viewangles, v3ViewAngles );
		v3ViewAngles[2] += ps->leanf / 2.0f;
		AngleVectors( v3ViewAngles, NULL, right, NULL );
		VectorMA( org, ps->leanf, right, org );
	}

	if (ps->pm_flags & PMF_DUCKED)
		vp[2] += CROUCH_VIEWHEIGHT;
	else
		vp[2] += DEFAULT_VIEWHEIGHT;
}

//======================================================================

#define MAX_PITCH    20

static int player_in_fov(vec3_t viewangle, vec3_t ppos, vec3_t opos)
{
	float yaw, pitch, cos_angle;
	vec3_t dir, los;

	/*
	   FIXME:
	   For some reason my FOV calculation does not work correctly for large
	   pitch values. It does not matter, the test's purpose is to eliminate
	   info that would reveal the position of opponents behind the player
	   on the same floor.
	 */
	if (viewangle[PITCH] > MAX_PITCH || viewangle[PITCH] < -1 * MAX_PITCH)
		return 1;

	// calculate unit vector of the direction the player looks at
	yaw = viewangle[YAW] * (M_PI * 2 / 360);
	pitch = viewangle[PITCH] * (M_PI * 2 / 360);
	dir[0] = cos(yaw) * cos(pitch);
	dir[1] = sin(yaw);
	dir[2] = cos(yaw) * sin(pitch);

	// calculate unit vector corresponding to line of sight to opponent
	VectorSubtract(opos, ppos, los);
	VectorNormalize(los);

	// calculate and test the angle between the two vectors
	cos_angle = DotProduct(dir, los);
	if (cos_angle > 0)      // +/- 90 degrees (fov = 180)
		return 1;

	return 0;
}

//======================================================================

static void copy_trajectory(trajectory_t * src, trajectory_t * dst)
{
	dst->trType = src->trType;
	dst->trTime = src->trTime;
	dst->trDuration = src->trDuration;
	VectorCopy(src->trBase, dst->trBase);
	VectorCopy(src->trDelta, dst->trDelta);
}

//======================================================================

int is_visible(vec3_t start, vec3_t end)
{
	trace_t trace;

	CM_BoxTrace(&trace, start, end, NULL, NULL, 0, CONTENTS_SOLID, 0);

	if (trace.contents & CONTENTS_SOLID)
		return 0;

	return 1;
}

//======================================================================

#define MIN_DIST           200.0
#define MAX_DIST           1000.0
#define MIN_OFS_FACT       0.1
#define MAX_OFS_FACT       0.4
#define OFS_FACT_DIFF      (MAX_OFS_FACT - MIN_OFS_FACT)

static void randomize_position(sharedEntity_t *anchor, sharedEntity_t *object)
{
	vec3_t los;
	float dist, ofs, rand_fact, dist_fact;

	VectorSubtract(anchor->s.pos.trBase, object->s.pos.trBase, los);
	dist = VectorLength(los);

	if (dist > MAX_DIST)
		dist_fact = MIN_OFS_FACT;
	else if (dist < MIN_DIST)
		dist_fact = MAX_OFS_FACT;
	else
		dist_fact = MAX_OFS_FACT - OFS_FACT_DIFF * (dist - MIN_DIST) / (MAX_DIST - MIN_DIST);

	rand_fact = (float) (rand() % 100) / 100.0;
	ofs = dist * dist_fact;
	ofs += ofs * rand_fact;

	if (rand() & 1)
		object->s.pos.trBase[0] += ofs;
	else
		object->s.pos.trBase[0] -= ofs;

	rand_fact = (float) (rand() % 100) / 100.0;
	ofs = dist * dist_fact;
	ofs += ofs * rand_fact;

	if (rand() & 1)
		object->s.pos.trBase[1] += ofs;
	else
		object->s.pos.trBase[1] -= ofs;

	rand_fact = (float) (rand() % 100) / 100.0;
	ofs = dist * dist_fact;
	ofs += ofs * rand_fact;

	if (rand() & 1)
		object->s.pos.trBase[2] += ofs;
	else
		object->s.pos.trBase[2] -= ofs;
}

//======================================================================
/*
   'can_see' checks if 'player' can see 'other' or not. First
   a check is made if 'other' is in the maximum allowed fov of
   'player'. If not, then zero is returned w/o any further checks.
   Next traces are carried out from the present viewpoint of 'player'
   to the corners of the bounding box of 'other'. If any of these
   traces are successful (i.e. nothing solid is between the start
   and end positions) then non-zero is returned.

   Otherwise the expected positions of the two players are calculated,
   by extrapolating their movements for PREDICT_TIME seconds and the above
   tests are carried out again. The result is reported by returning non-zero
   (expected to become visible) or zero (not expected to become visible
   in the next frame).
 */

#define PREDICT_TIME      0.1
#define VOFS              6

static int can_see(sharedEntity_t *pent, sharedEntity_t*oent, playerState_t *ps)
{
	vec3_t viewpoint, tmp;
	int i;

	/* check if 'other' is in the maximum fov allowed */
	if (!player_in_fov(pent->s.apos.trBase, pent->s.pos.trBase, oent->s.pos.trBase))
		return 0;

	/* check if visible in this frame */
	calc_viewpoint(ps, pent->s.pos.trBase, viewpoint);

	for (i = 0; i < 8; i++)
	{
		VectorCopy(oent->s.pos.trBase, tmp);
		tmp[0] += delta[i][0];
		tmp[1] += delta[i][1];
		tmp[2] += delta[i][2] + VOFS;

		if (is_visible(viewpoint, tmp))
			return 1;
	}

	/* predict player positions */
	copy_trajectory(&pent->s.pos, &traject);
	predict_move(pent, PREDICT_TIME, &traject, pred_ppos);

	copy_trajectory(&oent->s.pos, &traject);
	predict_move(oent, PREDICT_TIME, &traject, pred_opos);

	/*
	   Check again if 'other' is in the maximum fov allowed.
	   FIXME: We use the original viewangle that may have
	   changed during the move. This could introduce some
	   errors.
	 */
	if (!player_in_fov(pent->s.apos.trBase, pred_ppos, pred_opos))
		return 0;

	/* check if expected to be visible in the next frame */
	calc_viewpoint(ps, pred_ppos, viewpoint);

	for (i = 0; i < 8; i++)
	{
		VectorCopy(pred_opos, tmp);
		tmp[0] += delta[i][0];
		tmp[1] += delta[i][1];
		tmp[2] += delta[i][2] + VOFS;

		if (is_visible(viewpoint, tmp))
			return 1;
	}

	return 0;
}

//======================================================================
// public functions
//======================================================================

void AWH_Init(void)
{
	int i;

	for (i = 0; i < 8; i++)
	{
		delta[i][0] = ((float) awh_bbox_horz->integer * delta_sign[i][0]) / 2.0;
		delta[i][1] = ((float) awh_bbox_horz->integer * delta_sign[i][1]) / 2.0;
		delta[i][2] = ((float) awh_bbox_vert->integer * delta_sign[i][2]) / 2.0;
	}

	rand_seed = (int) time(NULL);
}

//======================================================================

int AWH_CanSee(int player, int other)
{
	sharedEntity_t *pent, *oent;
	playerState_t *ps;

	ps = SV_GameClientNum(player);
	pent = SV_GentityNum(player);
	oent = SV_GentityNum(other);

	return can_see(pent, oent, ps);
}

//======================================================================

// The value below is equal to the default value of the Cvar
// 's_alMaxDistance' (= 1024).
#define SOUND_HEARING_LIMIT    1024

int AWH_CanHear(int player, int other)
{
	sharedEntity_t *pent, *oent;
	vec3_t dist;

	pent = SV_GentityNum(player);
	oent = SV_GentityNum(other);

	VectorSubtract(pent->s.pos.trBase, oent->s.pos.trBase, dist);
	if (VectorLength(dist) > SOUND_HEARING_LIMIT)
		return 0;

	return 1;
}

//======================================================================
/*
   Randomizes the position of 'other'. Amount of displacement depends
   on the distance of 'other' from 'player'. Checks if the new position
   is still invisible from the position of 'player', returns if yes,
   otherwise tries again. After three attempts the function gives up and
   restores the original position of 'other'.
 */

void AWH_RandomizePos(int player, int other)
{
	int i;
	sharedEntity_t *pent, *oent;
	playerState_t *ps;
	vec3_t pos;

	ps = SV_GameClientNum(player);
	pent = SV_GentityNum(player);
	oent = SV_GentityNum(other);
	VectorCopy(oent->s.pos.trBase, pos);

	for (i = 0; i < 3; i++)
	{
		randomize_position(pent, oent);

		if (!can_see(pent, oent, ps))
			return;
		else
			VectorCopy(pos, oent->s.pos.trBase);
	}
}

#endif