File: cm_trace_lbd.cpp

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (623 lines) | stat: -rw-r--r-- 14,431 bytes parent folder | download
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
===========================================================================
Copyright (C) 2015 the OpenMoHAA team

This file is part of OpenMoHAA source code.

OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

#include "cm_local.h"
#include "../server/server.h"
#include "../client/client.h"
#include "tiki.h"

#define LOCATION_FAIL			-2
#define MAX_HITLOCATIONS		19

const char *szLocArray[] =
{
	"Bip01 Head",
	"Bip01 Head",
	"Bip01 Neck",
	"Bip01 Spine2",
	"Bip01 Spine1",
	"Bip01 Spine", 
	"Bip01 Pelvis",
	"Bip01 R UpperArm",
	"Bip01 L UpperArm",
	"Bip01 R Thigh",
	"Bip01 L Thigh",
	"Bip01 R Forearm",
	"Bip01 L Forearm",
	"Bip01 R Calf",
	"Bip01 L Calf",
	"Bip01 R Hand",
	"Bip01 L Hand",
	"Bip01 R Foot",
	"Bip01 L Foot"
};

float fLocRadius[] =
{
	5.0,
	5.5,
	4.0,
	9.0,
	8.0,
	9.0,
	9.0,
	7.0,
	7.0,
	8.0,
	8.0,
	5.5,
	5.5,
	7.5,
	7.5,
	6.0,
	6.0,
	6.0,
	6.0,
};

vec3_t vLocOffset[] =
{
	{ 2.5,		3.5,		0.0 },
	{ 8.5,		1.0,		0.0 },
	{ 3.5,		-1.0,		0.0 },
	{ 5.0,		1.0,		3.0 },
	{ 2.0,		1.0,		3.0 },
	{ 1.0,		1.0,		3.0 },
	{ -3.0,		1.0,		4.0 },
	{ 4.0,		0.0,		0.0 },
	{ 4.0,		0.0,		0.0 },
	{ 12.0,		0.0,		0.0 },
	{ 12.0,		0.0,		0.0 },
	{ 5.0,		1.0,		0.0 },
	{ 5.0,		1.0,		0.0 },
	{ 8.0,		0.0,		0.0 },
	{ 8.0,		0.0,		0.0 },
	{ 5.0,		1.0,		0.0 },
	{ 5.0,		1.0,		0.0 },
	{ 3.0,		2.0,		0.0 },
	{ 3.0,		2.0,		0.0 },
};

float fLocRadius_Secondary[] =
{
	0.0,
	5.5,
	4.0,
	9.0,
	8.0,
	9.0,
	9.0,
	6.0,
	6.0,
	8.0,
	8.0,
	5.0,
	5.0,
	6.5,
	6.5,
	0.0,
	0.0,
	5.0,
	5.0,
};

vec3_t vLocOffset_Secondary[] =
{
	{ 0.0,		0.0,		0.0 },
	{ 7.0,		-2.5,		0.0 },
	{ 2.0,		2.0,		0.0 },
	{ 5.0,		1.0,		-3.0 },
	{ 2.0,		1.0,		-3.0 },
	{ 1.0,		1.0,		-3.0 },
	{ -3.0,		1.0,		-4.0 },
	{ 11.5,		0.0,		0.0 },
	{ 11.5,		0.0,		0.0 },
	{ 22.0,		0.0,		0.0 },
	{ 22.0,		0.0,		0.0 },
	{ 11.5,		0.0,		0.0 },
	{ 11.5,		0.0,		0.0 },
	{ 19.0,		0.0,		0.0 },
	{ 19.0,		0.0,		0.0 },
	{ 0.0,		0.0,		0.0 },
	{ 0.0,		0.0,		0.0 },
	{ 4.0,		9.0,		0.0 },
	{ 4.0,		9.0,		0.0 },
};

/*
==================
LineSegmentToSphereIntersect
==================
*/
static qboolean LineSegmentToSphereIntersect( trace_t *pTrace, float fRadius, const vec3_t vCntr, const vec3_t vStart, const vec3_t vEnd )
{
	vec3_t	vDiff, vDir;
	float	fDist;
	float	fLSqrd;
	float	fRSqrd;
	float	fDSqrd;
	float	fLength;
	float	fOfs;
	float	fAngSin;
	vec3_t	vProject, vOfs;

	fRSqrd = fRadius * fRadius;

	VectorSubtract( vEnd, vStart, vDir );
	fLength = VectorNormalize( vDir );

	VectorSubtract( vCntr, vStart, vDiff );
	fDSqrd = DotProduct( vDiff, vDir );
	fDist = VectorLengthSquared( vDiff );

	if( fDSqrd < 0 && fDist > fRSqrd ) {
		return qfalse;
	}

	fLSqrd = fDist - fDSqrd * fDSqrd;
	if( fLSqrd > fRSqrd || fDSqrd > fLength && fDSqrd - fLength > fRadius ) {
		return qfalse;
	}

	vOfs[ 0 ] = vStart[ 0 ] + vDir[ 0 ] * fDSqrd;
	vOfs[ 1 ] = vStart[ 1 ] + vDir[ 1 ] * fDSqrd;
	vOfs[ 2 ] = vStart[ 2 ] + vDir[ 2 ] * fDSqrd;
	VectorSubtract( vCntr, vOfs, vProject );

	fOfs = VectorLength( vProject ) / fRadius;

	if( fOfs >= -0.5 )
	{
		if( fOfs <= 0.5 )
		{
			fAngSin = fOfs * ( -0.531387674508458 * fOfs - 2.11e-14 ) + 1.00086138065435;
		}
		else
		{
			fAngSin = fOfs
				* ( ( ( ( ( -1507.55394345521 * fOfs + 6580.58002318442 ) * fOfs - 11860.0735285953 ) * fOfs
				+ 11290.7510782536 )
				* fOfs
				- 5986.89654545347 )
				* fOfs
				+ 1675.66417006387 )
				- 192.426950291139;
		}
	}
	else
	{
		fAngSin = fOfs
			* ( ( ( ( ( -337.31875783205 * fOfs - 1237.54375255107 ) * fOfs - 1802.11467325687 ) * fOfs - 1303.19904613494 )
			* fOfs
			- 471.347871690988 )
			* fOfs
			- 70.0883838161826 );
	}

	pTrace->fraction = ( fDSqrd - fRadius * fAngSin ) / fLength;
	return qtrue;
}

/*
==================
CM_GetHitLocationInfo
==================
*/
const char *CM_GetHitLocationInfo( int i_iLocation, float *o_fRadius, vec3_t o_vOffset )
{
	if( i_iLocation < MAX_HITLOCATIONS )
	{
		*o_fRadius = fLocRadius[ i_iLocation ];
		VectorCopy( vLocOffset[ i_iLocation ], o_vOffset );
		return szLocArray[ i_iLocation ];
	}
	else
	{
		*o_fRadius = 8.0f;
		VectorClear( o_vOffset );
		return NULL;
	}
}

/*
==================
CM_GetHitLocationInfoSecondary
==================
*/
const char *CM_GetHitLocationInfoSecondary( int i_iLocation, float *o_fRadius, vec3_t o_vOffset )
{
	if( i_iLocation < MAX_HITLOCATIONS )
	{
		*o_fRadius = fLocRadius_Secondary[ i_iLocation ];
		VectorCopy( vLocOffset_Secondary[ i_iLocation ], o_vOffset );
		return szLocArray[ i_iLocation ];
	}
	else
	{
		*o_fRadius = 8.0f;
		VectorClear( o_vOffset );
		return NULL;
	}
}

/*
==================
CM_TraceDeepSimple
==================
*/
static qboolean CM_TraceDeepSimple( trace_t *results, const vec3_t vStart, const vec3_t vEnd, int iBrushMask, int iEntContents, const vec3_t vEntMins, const vec3_t vEntMaxs, const vec3_t vEntAngles, vec3_t vTransStart, vec3_t vTransEnd )
{
	vec3_t	vCntr;
	vec3_t	vTemp;
	vec3_t	vNewMins, vNewMaxs;
	vec3_t	vForward, vLeft, vUp;
	clipHandle_t	clipHandle;

	vNewMins[ 0 ] = vEntMins[ 0 ] - 40.0;
	vNewMins[ 1 ] = vEntMins[ 1 ] - 40.0;
	vNewMins[ 2 ] = vEntMins[ 2 ] - 40.0;
	vNewMaxs[ 0 ] = vEntMaxs[ 0 ] + 40.0;
	vNewMaxs[ 1 ] = vEntMaxs[ 1 ] + 40.0;
	vNewMaxs[ 2 ] = vEntMaxs[ 2 ] + 40.0;

	clipHandle = CM_TempBoxModel( vNewMins, vNewMaxs, iEntContents );
	AngleVectorsLeft( vEntAngles, vForward, vLeft, vUp );

	VectorCopy( vTransStart, vTemp );
	vTransStart[ 0 ] = DotProduct( vTemp, vForward );
	vTransStart[ 1 ] = DotProduct( vTemp, vLeft );
	vTransStart[ 2 ] = DotProduct( vTemp, vUp );

	VectorCopy( vTransEnd, vTemp );
	vTransEnd[ 0 ] = DotProduct( vTemp, vForward );
	vTransEnd[ 1 ] = DotProduct( vTemp, vLeft );
	vTransEnd[ 2 ] = DotProduct( vTemp, vUp );

	// don't use mins and maxs
	VectorClear( vCntr );
	CM_BoxTrace( results, vTransStart, vTransEnd, vCntr, vCntr, clipHandle, iBrushMask, qfalse );

	if( results->fraction == 1.0 && !results->startsolid && !results->allsolid )
	{
		VectorCopy( vEnd, results->endpos );
		return qfalse;
	}

	return qtrue;
}

/*
==================
CM_TraceDeepSimple2
==================
*/
static qboolean CM_TraceDeepSimple2( trace_t *results, const orientation_t *orPosition, vec3_t vOffset, float fRad, const char *pszTagName, int iLocation, const vec3_t vTransStart, const vec3_t vTransEnd )
{
	vec3_t	vCntr;

	VectorCopy( orPosition->origin, vCntr );

	if( vOffset[ 0 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 0 ], orPosition->axis[ 0 ], vCntr );
	}
	if( vOffset[ 1 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 1 ], orPosition->axis[ 1 ], vCntr );
	}
	if( vOffset[ 2 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 2 ], orPosition->axis[ 2 ], vCntr );
	}

	if( LineSegmentToSphereIntersect( results, fRad, vCntr, vTransStart, vTransEnd ) ) {
		return qtrue;
	}

	// get the secondary location if any
	if( !CM_GetHitLocationInfoSecondary( iLocation, &fRad, vOffset ) ) {
		return qfalse;
	}

	VectorCopy( orPosition->origin, vCntr );

	if( vOffset[ 0 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 0 ], orPosition->axis[ 0 ], vCntr );
	}
	if( vOffset[ 1 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 1 ], orPosition->axis[ 1 ], vCntr );
	}
	if( vOffset[ 2 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 2 ], orPosition->axis[ 2 ], vCntr );
	}

	if( LineSegmentToSphereIntersect( results, fRad, vCntr, vTransStart, vTransEnd ) ) {
		return qtrue;
	}

	return qfalse;
}

/*
==================
CM_TraceDeepFail
==================
*/
static void CM_TraceDeepFail( trace_t *results, const vec3_t vEnd )
{
	results->fraction	= 1.0;
	results->allsolid	= qfalse;
	results->startsolid	= qfalse;
	VectorCopy( vEnd, results->endpos );
	results->entityNum	= ENTITYNUM_NONE;
	results->location	= LOCATION_FAIL;
	results->ent		= NULL;
}

/*
==================
CM_TraceDeepSuccess
==================
*/
static void CM_TraceDeepSuccess( trace_t *results, const vec3_t vStart, const vec3_t vEnd, int iEntNum, int iEntContents, int iLocation )
{
	results->endpos[ 0 ] = vStart[ 0 ] + results->fraction * ( vEnd[ 0 ] - vStart[ 0 ] );
	results->endpos[ 1 ] = vStart[ 1 ] + results->fraction * ( vEnd[ 1 ] - vStart[ 1 ] );
	results->endpos[ 2 ] = vStart[ 2 ] + results->fraction * ( vEnd[ 2 ] - vStart[ 2 ] );
	results->allsolid = qfalse;
	results->startsolid = qfalse;
	results->contents = iEntContents;
	results->entityNum = iEntNum;
	results->location = iLocation;
	results->ent = NULL;
}

/*
==================
SV_TraceDeep_DebugDraw
==================
*/
void SV_TraceDeep_DebugDraw( gentity_t *touch, orientation_t& orTag, int iLocation, qboolean bHit )
{
	int		i;
	float	fRad;
	vec3_t	vOffset;
	vec3_t	vCntr;
	orientation_t	orEnt;
	orientation_t	orPosition;
	const char		*pszLocationName;

	if( !sv_deeptracedebug->integer ) {
		return;
	}

	VectorCopy( touch->s.origin, orEnt.origin );
	AngleVectors( touch->s.angles, orEnt.axis[ 0 ], orEnt.axis[ 1 ], orEnt.axis[ 2 ] );
	VectorInverse( orEnt.axis[ 1 ] );

	pszLocationName = CM_GetHitLocationInfo( iLocation, &fRad, vOffset );

	if( bHit ) {
		Com_Printf( "Hit %i '%s'\n", iLocation, pszLocationName );
	}

	// get the orientation
	VectorCopy( orEnt.origin, orPosition.origin );

	for( i = 0; i < 3; i++ )
	{
		VectorMA( orPosition.origin, orEnt.origin[ i ], orEnt.axis[ i ], orPosition.origin );
	}
	MatrixMultiply( orTag.axis, orEnt.axis, orPosition.axis );

	VectorCopy( orPosition.origin, vCntr );

	if( vOffset[ 0 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 0 ], orPosition.axis[ 0 ], vCntr );
	}
	if( vOffset[ 1 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 1 ], orPosition.axis[ 1 ], vCntr );
	}
	if( vOffset[ 2 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 2 ], orPosition.axis[ 2 ], vCntr );
	}

	if( bHit ) {
		ge->DebugCircle( vCntr, fRad, 1.0, 0.5, 0.0, 1.0, qfalse );
	} else {
		ge->DebugCircle( vCntr, fRad, 0.0, 0.5, 1.0, 1.0, qfalse );
	}

	// get the secondary location if any
	if( !CM_GetHitLocationInfoSecondary( iLocation, &fRad, vOffset ) ) {
		return;
	}

	VectorCopy( orPosition.origin, vCntr );

	if( vOffset[ 0 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 0 ], orPosition.axis[ 0 ], vCntr );
	}
	if( vOffset[ 1 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 1 ], orPosition.axis[ 1 ], vCntr );
	}
	if( vOffset[ 2 ] != 0.0 ) {
		VectorMA( vCntr, vOffset[ 2 ], orPosition.axis[ 2 ], vCntr );
	}

	if( bHit ) {
		ge->DebugCircle( vCntr, fRad, 1.0, 0.0, 0.5, 1.0, qfalse );
	} else {
		ge->DebugCircle( vCntr, fRad, 0.5, 0.0, 1.0, 1.0, qfalse );
	}
}

/*
==================
SV_TraceDeep
==================
*/
void SV_TraceDeep( trace_t *results, const vec3_t vStart, const vec3_t vEnd, int iBrushMask, gentity_t *touch )
{
	int				iLocation, iBoneNum;
	float			fRad;
	vec3_t			vOffset;
	orientation_t	orPosition;
	const char		*pszTagName;
	dtiki_t			*tiki;
	vec3_t			vTransStart, vTransEnd;

	// subtract the origin
	VectorSubtract( vStart, touch->s.origin, vTransStart );
	VectorSubtract( vEnd, touch->s.origin, vTransEnd );

	// check if something was not hit first
	if( !CM_TraceDeepSimple( results,
		vStart, vEnd,
		iBrushMask, touch->r.contents,
		touch->r.mins, touch->r.maxs, touch->s.angles,
		vTransStart, vTransEnd
		) ) {
		return;
	}

	tiki = touch->tiki;

	// trace through each locations
	for( iLocation = 0; iLocation < MAX_HITLOCATIONS; iLocation++ )
	{
		pszTagName = CM_GetHitLocationInfo( iLocation, &fRad, vOffset );
		iBoneNum = tiki->GetBoneNumFromName( pszTagName );

		if( iBoneNum == -1 ) {
			continue;
		}

		// retrieve the orientation from the game dll
		orPosition = ge->TIKI_Orientation( touch, iBoneNum );

		// check if the tag was hit
		if( CM_TraceDeepSimple2( results,
			&orPosition, vOffset, fRad,
			pszTagName, iLocation,
			vTransStart, vTransEnd ) )
		{
			SV_TraceDeep_DebugDraw( touch, orPosition, iLocation, qtrue );

			// set the location
			CM_TraceDeepSuccess( results, vStart, vEnd, touch->s.number, touch->r.contents, iLocation );
			return;
		}

		SV_TraceDeep_DebugDraw( touch, orPosition, iLocation, qfalse );
	}

	// fail
	CM_TraceDeepFail( results, vEnd );
}

/*
==================
CL_CheckWeights
==================
*/
qboolean CL_CheckWeights( refEntity_t *ent )
{
	int i;

	if( !ent->tiki )
	{
		return qtrue;
	}

	for( i = 0; i < MAX_FRAMEINFOS; i++ )
	{
		if( ent->frameInfo[ i ].weight > 0.0f )
		{
			return qtrue;
		}
	}

	return qfalse;
}

#ifdef CLIENT

/*
==================
CL_TraceDeep
==================
*/
qboolean CL_TraceDeep( trace_t *results, const vec3_t vStart, const vec3_t vEnd, int iBrushMask, int iEntNum, int iEntContents, const vec3_t vEntMins, const vec3_t vEntMaxs, const vec3_t vEntOrigin, const vec3_t vEntAngles, refEntity_t *model )
{
	int				iLocation, iBoneNum;
	float			fRad;
	vec3_t			vOffset;
	orientation_t	orPosition;
	const char		*pszTagName;
	dtiki_t			*tiki;
	vec3_t			vTransStart, vTransEnd;

	// subtract the origin
	VectorSubtract( vStart, vEntOrigin, vTransStart );
	VectorSubtract( vEnd, vEntOrigin, vTransEnd );

	// check if something was not hit first
	if( !CM_TraceDeepSimple( results,
		vStart, vEnd,
		iBrushMask, iEntContents,
		vEntMins, vEntMaxs, vEntAngles,
		vTransStart, vTransEnd
		) ) {
		return qfalse;
	}

	tiki = model->tiki;

	// trace through each locations
	for( iLocation = 0; iLocation < MAX_HITLOCATIONS; iLocation++ )
	{
		pszTagName = CM_GetHitLocationInfo( iLocation, &fRad, vOffset );
		iBoneNum = tiki->GetBoneNumFromName( pszTagName );

		if( iBoneNum == -1 ) {
			continue;
		}

		// retrieve the orientation from the game dll
		orPosition = re.TIKI_Orientation( model, iBoneNum );

		// check if the tag was hit
		if( CM_TraceDeepSimple2( results,
			&orPosition, vOffset, fRad,
			pszTagName, iLocation,
			vTransStart, vTransEnd ) )
		{
			// set the location
			CM_TraceDeepSuccess( results, vStart, vEnd, iEntNum, iEntContents, iLocation );
			return qtrue;
		}
	}

	// fail
	CM_TraceDeepFail( results, vEnd );
	return qfalse;
}

#endif