Debian Codegraph @

code/q3_ui/ui_players.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23 // ui_players.c
24
25 #include "ui_local.h"
26
27
28 #define UI_TIMER_GESTURE 2300
29 #define UI_TIMER_JUMP 1000
30 #define UI_TIMER_LAND 130
31 #define UI_TIMER_WEAPON_SWITCH 300
32 #define UI_TIMER_ATTACK 500
33 #define UI_TIMER_MUZZLE_FLASH 20
34 #define UI_TIMER_WEAPON_DELAY 250
35
36 #define JUMP_HEIGHT 56
37
38 #define SWINGSPEED 0.3f
39
40 #define SPIN_SPEED 0.9f
41 #define COAST_TIME 1000
42
43
44 static int dp_realtime;
45 static float jumpHeight;
46
47
48 /*
49 ===============
50 UI_PlayerInfo_SetWeapon
51 ===============
52 */
53 static void UI_PlayerInfo_SetWeapon( playerInfo_t *pi, weapon_t weaponNum ) {
54 gitem_t * item;
55 char path[MAX_QPATH];
56
57 pi->currentWeapon = weaponNum;
58 tryagain:
59 pi->realWeapon = weaponNum;
60 pi->weaponModel = 0;
61 pi->barrelModel = 0;
62 pi->flashModel = 0;
63
64 if ( weaponNum == WP_NONE ) {
65 return;
66 }
67
68 for ( item = bg_itemlist + 1; item->classname ; item++ ) {
69 if ( item->giType != IT_WEAPON ) {
70 continue;
71 }
72 if ( item->giTag == weaponNum ) {
73 break;
74 }
75 }
76
77 if ( item->classname ) {
78 pi->weaponModel = trap_R_RegisterModel( item->world_model[0] );
79 }
80
81 if( pi->weaponModel == 0 ) {
82 if( weaponNum == WP_MACHINEGUN ) {
83 weaponNum = WP_NONE;
84 goto tryagain;
85 }
86 weaponNum = WP_MACHINEGUN;
87 goto tryagain;
88 }
89
90 if ( weaponNum == WP_MACHINEGUN || weaponNum == WP_GAUNTLET || weaponNum == WP_BFG ) {
91 COM_StripExtension( item->world_model[0], path, sizeof(path) );
92 Q_strcat( path, sizeof(path), "_barrel.md3" );
93 pi->barrelModel = trap_R_RegisterModel( path );
94 }
95
96 COM_StripExtension( item->world_model[0], path, sizeof(path) );
97 Q_strcat( path, sizeof(path), "_flash.md3" );
98 pi->flashModel = trap_R_RegisterModel( path );
99
100 switch( weaponNum ) {
101 case WP_GAUNTLET:
102 MAKERGB( pi->flashDlightColor, 0.6f, 0.6f, 1 );
103 break;
104
105 case WP_MACHINEGUN:
106 MAKERGB( pi->flashDlightColor, 1, 1, 0 );
107 break;
108
109 case WP_SHOTGUN:
110 MAKERGB( pi->flashDlightColor, 1, 1, 0 );
111 break;
112
113 case WP_GRENADE_LAUNCHER:
114 MAKERGB( pi->flashDlightColor, 1, 0.7f, 0.5f );
115 break;
116
117 case WP_ROCKET_LAUNCHER:
118 MAKERGB( pi->flashDlightColor, 1, 0.75f, 0 );
119 break;
120
121 case WP_LIGHTNING:
122 MAKERGB( pi->flashDlightColor, 0.6f, 0.6f, 1 );
123 break;
124
125 case WP_RAILGUN:
126 MAKERGB( pi->flashDlightColor, 1, 0.5f, 0 );
127 break;
128
129 case WP_PLASMAGUN:
130 MAKERGB( pi->flashDlightColor, 0.6f, 0.6f, 1 );
131 break;
132
133 case WP_BFG:
134 MAKERGB( pi->flashDlightColor, 1, 0.7f, 1 );
135 break;
136
137 case WP_GRAPPLING_HOOK:
138 MAKERGB( pi->flashDlightColor, 0.6f, 0.6f, 1 );
139 break;
140
141 default:
142 MAKERGB( pi->flashDlightColor, 1, 1, 1 );
143 break;
144 }
145 }
146
147
148 /*
149 ===============
150 UI_ForceLegsAnim
151 ===============
152 */
153 static void UI_ForceLegsAnim( playerInfo_t *pi, int anim ) {
154 pi->legsAnim = ( ( pi->legsAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | anim;
155
156 if ( anim == LEGS_JUMP ) {
157 pi->legsAnimationTimer = UI_TIMER_JUMP;
158 }
159 }
160
161
162 /*
163 ===============
164 UI_SetLegsAnim
165 ===============
166 */
167 static void UI_SetLegsAnim( playerInfo_t *pi, int anim ) {
168 if ( pi->pendingLegsAnim ) {
169 anim = pi->pendingLegsAnim;
170 pi->pendingLegsAnim = 0;
171 }
172 UI_ForceLegsAnim( pi, anim );
173 }
174
175
176 /*
177 ===============
178 UI_ForceTorsoAnim
179 ===============
180 */
181 static void UI_ForceTorsoAnim( playerInfo_t *pi, int anim ) {
182 pi->torsoAnim = ( ( pi->torsoAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | anim;
183
184 if ( anim == TORSO_GESTURE ) {
185 pi->torsoAnimationTimer = UI_TIMER_GESTURE;
186 }
187
188 if ( anim == TORSO_ATTACK || anim == TORSO_ATTACK2 ) {
189 pi->torsoAnimationTimer = UI_TIMER_ATTACK;
190 }
191 }
192
193
194 /*
195 ===============
196 UI_SetTorsoAnim
197 ===============
198 */
199 static void UI_SetTorsoAnim( playerInfo_t *pi, int anim ) {
200 if ( pi->pendingTorsoAnim ) {
201 anim = pi->pendingTorsoAnim;
202 pi->pendingTorsoAnim = 0;
203 }
204
205 UI_ForceTorsoAnim( pi, anim );
206 }
207
208
209 /*
210 ===============
211 UI_TorsoSequencing
212 ===============
213 */
214 static void UI_TorsoSequencing( playerInfo_t *pi ) {
215 int currentAnim;
216
217 currentAnim = pi->torsoAnim & ~ANIM_TOGGLEBIT;
218
219 if ( pi->weapon != pi->currentWeapon ) {
220 if ( currentAnim != TORSO_DROP ) {
221 pi->torsoAnimationTimer = UI_TIMER_WEAPON_SWITCH;
222 UI_ForceTorsoAnim( pi, TORSO_DROP );
223 }
224 }
225
226 if ( pi->torsoAnimationTimer > 0 ) {
227 return;
228 }
229
230 if( currentAnim == TORSO_GESTURE ) {
231 UI_SetTorsoAnim( pi, TORSO_STAND );
232 return;
233 }
234
235 if( currentAnim == TORSO_ATTACK || currentAnim == TORSO_ATTACK2 ) {
236 UI_SetTorsoAnim( pi, TORSO_STAND );
237 return;
238 }
239
240 if ( currentAnim == TORSO_DROP ) {
241 UI_PlayerInfo_SetWeapon( pi, pi->weapon );
242 pi->torsoAnimationTimer = UI_TIMER_WEAPON_SWITCH;
243 UI_ForceTorsoAnim( pi, TORSO_RAISE );
244 return;
245 }
246
247 if ( currentAnim == TORSO_RAISE ) {
248 UI_SetTorsoAnim( pi, TORSO_STAND );
249 return;
250 }
251 }
252
253
254 /*
255 ===============
256 UI_LegsSequencing
257 ===============
258 */
259 static void UI_LegsSequencing( playerInfo_t *pi ) {
260 int currentAnim;
261
262 currentAnim = pi->legsAnim & ~ANIM_TOGGLEBIT;
263
264 if ( pi->legsAnimationTimer > 0 ) {
265 if ( currentAnim == LEGS_JUMP ) {
266 jumpHeight = JUMP_HEIGHT * sin( M_PI * ( UI_TIMER_JUMP - pi->legsAnimationTimer ) / UI_TIMER_JUMP );
267 }
268 return;
269 }
270
271 if ( currentAnim == LEGS_JUMP ) {
272 UI_ForceLegsAnim( pi, LEGS_LAND );
273 pi->legsAnimationTimer = UI_TIMER_LAND;
274 jumpHeight = 0;
275 return;
276 }
277
278 if ( currentAnim == LEGS_LAND ) {
279 UI_SetLegsAnim( pi, LEGS_IDLE );
280 return;
281 }
282 }
283
284
285 /*
286 ======================
287 UI_PositionEntityOnTag
288 ======================
289 */
290 static void UI_PositionEntityOnTag( refEntity_t *entity, const refEntity_t *parent,
291 clipHandle_t parentModel, char *tagName ) {
292 int i;
293 orientation_t lerped;
294
295 // lerp the tag
296 trap_CM_LerpTag( &lerped, parentModel, parent->oldframe, parent->frame,
297 1.0 - parent->backlerp, tagName );
298
299 // FIXME: allow origin offsets along tag?
300 VectorCopy( parent->origin, entity->origin );
301 for ( i = 0 ; i < 3 ; i++ ) {
302 VectorMA( entity->origin, lerped.origin[i], parent->axis[i], entity->origin );
303 }
304
305 // cast away const because of compiler problems
306 MatrixMultiply( lerped.axis, ((refEntity_t*)parent)->axis, entity->axis );
307 entity->backlerp = parent->backlerp;
308 }
309
310
311 /*
312 ======================
313 UI_PositionRotatedEntityOnTag
314 ======================
315 */
316 static void UI_PositionRotatedEntityOnTag( refEntity_t *entity, const refEntity_t *parent,
317 clipHandle_t parentModel, char *tagName ) {
318 int i;
319 orientation_t lerped;
320 vec3_t tempAxis[3];
321
322 // lerp the tag
323 trap_CM_LerpTag( &lerped, parentModel, parent->oldframe, parent->frame,
324 1.0 - parent->backlerp, tagName );
325
326 // FIXME: allow origin offsets along tag?
327 VectorCopy( parent->origin, entity->origin );
328 for ( i = 0 ; i < 3 ; i++ ) {
329 VectorMA( entity->origin, lerped.origin[i], parent->axis[i], entity->origin );
330 }
331
332 // cast away const because of compiler problems
333 MatrixMultiply( entity->axis, lerped.axis, tempAxis );
334 MatrixMultiply( tempAxis, ((refEntity_t *)parent)->axis, entity->axis );
335 }
336
337
338 /*
339 ===============
340 UI_SetLerpFrameAnimation
341 ===============
342 */
343 static void UI_SetLerpFrameAnimation( playerInfo_t *ci, lerpFrame_t *lf, int newAnimation ) {
344 animation_t *anim;
345
346 lf->animationNumber = newAnimation;
347 newAnimation &= ~ANIM_TOGGLEBIT;
348
349 if ( newAnimation < 0 || newAnimation >= MAX_ANIMATIONS ) {
350 trap_Error( va("Bad animation number: %i", newAnimation) );
351 }
352
353 anim = &ci->animations[ newAnimation ];
354
355 lf->animation = anim;
356 lf->animationTime = lf->frameTime + anim->initialLerp;
357 }
358
359
360 /*
361 ===============
362 UI_RunLerpFrame
363 ===============
364 */
365 static void UI_RunLerpFrame( playerInfo_t *ci, lerpFrame_t *lf, int newAnimation ) {
366 int f, numFrames;
367 animation_t *anim;
368
369 // see if the animation sequence is switching
370 if ( newAnimation != lf->animationNumber || !lf->animation ) {
371 UI_SetLerpFrameAnimation( ci, lf, newAnimation );
372 }
373
374 // if we have passed the current frame, move it to
375 // oldFrame and calculate a new frame
376 if ( dp_realtime >= lf->frameTime ) {
377 lf->oldFrame = lf->frame;
378 lf->oldFrameTime = lf->frameTime;
379
380 // get the next frame based on the animation
381 anim = lf->animation;
382 if ( !anim->frameLerp ) {
383 return; // shouldn't happen
384 }
385 if ( dp_realtime < lf->animationTime ) {
386 lf->frameTime = lf->animationTime; // initial lerp
387 } else {
388 lf->frameTime = lf->oldFrameTime + anim->frameLerp;
389 }
390 f = ( lf->frameTime - lf->animationTime ) / anim->frameLerp;
391
392 numFrames = anim->numFrames;
393 if (anim->flipflop) {
394 numFrames *= 2;
395 }
396 if ( f >= numFrames ) {
397 f -= numFrames;
398 if ( anim->loopFrames ) {
399 f %= anim->loopFrames;
400 f += anim->numFrames - anim->loopFrames;
401 } else {
402 f = numFrames - 1;
403 // the animation is stuck at the end, so it
404 // can immediately transition to another sequence
405 lf->frameTime = dp_realtime;
406 }
407 }
408 if ( anim->reversed ) {
409 lf->frame = anim->firstFrame + anim->numFrames - 1 - f;
410 }
411 else if (anim->flipflop && f>=anim->numFrames) {
412 lf->frame = anim->firstFrame + anim->numFrames - 1 - (f%anim->numFrames);
413 }
414 else {
415 lf->frame = anim->firstFrame + f;
416 }
417 if ( dp_realtime > lf->frameTime ) {
418 lf->frameTime = dp_realtime;
419 }
420 }
421
422 if ( lf->frameTime > dp_realtime + 200 ) {
423 lf->frameTime = dp_realtime;
424 }
425
426 if ( lf->oldFrameTime > dp_realtime ) {
427 lf->oldFrameTime = dp_realtime;
428 }
429 // calculate current lerp value
430 if ( lf->frameTime == lf->oldFrameTime ) {
431 lf->backlerp = 0;
432 } else {
433 lf->backlerp = 1.0 - (float)( dp_realtime - lf->oldFrameTime ) / ( lf->frameTime - lf->oldFrameTime );
434 }
435 }
436
437
438 /*
439 ===============
440 UI_PlayerAnimation
441 ===============
442 */
443 static void UI_PlayerAnimation( playerInfo_t *pi, int *legsOld, int *legs, float *legsBackLerp,
444 int *torsoOld, int *torso, float *torsoBackLerp ) {
445
446 // legs animation
447 pi->legsAnimationTimer -= uis.frametime;
448 if ( pi->legsAnimationTimer < 0 ) {
449 pi->legsAnimationTimer = 0;
450 }
451
452 UI_LegsSequencing( pi );
453
454 if ( pi->legs.yawing && ( pi->legsAnim & ~ANIM_TOGGLEBIT ) == LEGS_IDLE ) {
455 UI_RunLerpFrame( pi, &pi->legs, LEGS_TURN );
456 } else {
457 UI_RunLerpFrame( pi, &pi->legs, pi->legsAnim );
458 }
459 *legsOld = pi->legs.oldFrame;
460 *legs = pi->legs.frame;
461 *legsBackLerp = pi->legs.backlerp;
462
463 // torso animation
464 pi->torsoAnimationTimer -= uis.frametime;
465 if ( pi->torsoAnimationTimer < 0 ) {
466 pi->torsoAnimationTimer = 0;
467 }
468
469 UI_TorsoSequencing( pi );
470
471 UI_RunLerpFrame( pi, &pi->torso, pi->torsoAnim );
472 *torsoOld = pi->torso.oldFrame;
473 *torso = pi->torso.frame;
474 *torsoBackLerp = pi->torso.backlerp;
475 }
476
477
478 /*
479 ==================
480 UI_SwingAngles
481 ==================
482 */
483 static void UI_SwingAngles( float destination, float swingTolerance, float clampTolerance,
484 float speed, float *angle, qboolean *swinging ) {
485 float swing;
486 float move;
487 float scale;
488
489 if ( !*swinging ) {
490 // see if a swing should be started
491 swing = AngleSubtract( *angle, destination );
492 if ( swing > swingTolerance || swing < -swingTolerance ) {
493 *swinging = qtrue;
494 }
495 }
496
497 if ( !*swinging ) {
498 return;
499 }
500
501 // modify the speed depending on the delta
502 // so it doesn't seem so linear
503 swing = AngleSubtract( destination, *angle );
504 scale = fabs( swing );
505 if ( scale < swingTolerance * 0.5 ) {
506 scale = 0.5;
507 } else if ( scale < swingTolerance ) {
508 scale = 1.0;
509 } else {
510 scale = 2.0;
511 }
512
513 // swing towards the destination angle
514 if ( swing >= 0 ) {
515 move = uis.frametime * scale * speed;
516 if ( move >= swing ) {
517 move = swing;
518 *swinging = qfalse;
519 }
520 *angle = AngleMod( *angle + move );
521 } else if ( swing < 0 ) {
522 move = uis.frametime * scale * -speed;
523 if ( move <= swing ) {
524 move = swing;
525 *swinging = qfalse;
526 }
527 *angle = AngleMod( *angle + move );
528 }
529
530 // clamp to no more than tolerance
531 swing = AngleSubtract( destination, *angle );
532 if ( swing > clampTolerance ) {
533 *angle = AngleMod( destination - (clampTolerance - 1) );
534 } else if ( swing < -clampTolerance ) {
535 *angle = AngleMod( destination + (clampTolerance - 1) );
536 }
537 }
538
539
540 /*
541 ======================
542 UI_MovedirAdjustment
543 ======================
544 */
545 static float UI_MovedirAdjustment( playerInfo_t *pi ) {
546 vec3_t relativeAngles;
547 vec3_t moveVector;
548
549 VectorSubtract( pi->viewAngles, pi->moveAngles, relativeAngles );
550 AngleVectors( relativeAngles, moveVector, NULL, NULL );
551 if ( Q_fabs( moveVector[0] ) < 0.01 ) {
552 moveVector[0] = 0.0;
553 }
554 if ( Q_fabs( moveVector[1] ) < 0.01 ) {
555 moveVector[1] = 0.0;
556 }
557
558 if ( moveVector[1] == 0 && moveVector[0] > 0 ) {
559 return 0;
560 }
561 if ( moveVector[1] < 0 && moveVector[0] > 0 ) {
562 return 22;
563 }
564 if ( moveVector[1] < 0 && moveVector[0] == 0 ) {
565 return 45;
566 }
567 if ( moveVector[1] < 0 && moveVector[0] < 0 ) {
568 return -22;
569 }
570 if ( moveVector[1] == 0 && moveVector[0] < 0 ) {
571 return 0;
572 }
573 if ( moveVector[1] > 0 && moveVector[0] < 0 ) {
574 return 22;
575 }
576 if ( moveVector[1] > 0 && moveVector[0] == 0 ) {
577 return -45;
578 }
579
580 return -22;
581 }
582
583
584 /*
585 ===============
586 UI_PlayerAngles
587 ===============
588 */
589 static void UI_PlayerAngles( playerInfo_t *pi, vec3_t legs[3], vec3_t torso[3], vec3_t head[3] ) {
590 vec3_t legsAngles, torsoAngles, headAngles;
591 float dest;
592 float adjust;
593
594 VectorCopy( pi->viewAngles, headAngles );
595 headAngles[YAW] = AngleMod( headAngles[YAW] );
596 VectorClear( legsAngles );
597 VectorClear( torsoAngles );
598
599 // --------- yaw -------------
600
601 // allow yaw to drift a bit
602 if ( ( pi->legsAnim & ~ANIM_TOGGLEBIT ) != LEGS_IDLE
603 || ( pi->torsoAnim & ~ANIM_TOGGLEBIT ) != TORSO_STAND ) {
604 // if not standing still, always point all in the same direction
605 pi->torso.yawing = qtrue; // always center
606 pi->torso.pitching = qtrue; // always center
607 pi->legs.yawing = qtrue; // always center
608 }
609
610 // adjust legs for movement dir
611 adjust = UI_MovedirAdjustment( pi );
612 legsAngles[YAW] = headAngles[YAW] + adjust;
613 torsoAngles[YAW] = headAngles[YAW] + 0.25 * adjust;
614
615
616 // torso
617 UI_SwingAngles( torsoAngles[YAW], 25, 90, SWINGSPEED, &pi->torso.yawAngle, &pi->torso.yawing );
618 UI_SwingAngles( legsAngles[YAW], 40, 90, SWINGSPEED, &pi->legs.yawAngle, &pi->legs.yawing );
619
620 torsoAngles[YAW] = pi->torso.yawAngle;
621 legsAngles[YAW] = pi->legs.yawAngle;
622
623 // --------- pitch -------------
624
625 // only show a fraction of the pitch angle in the torso
626 if ( headAngles[PITCH] > 180 ) {
627 dest = (-360 + headAngles[PITCH]) * 0.75;
628 } else {
629 dest = headAngles[PITCH] * 0.75;
630 }
631 UI_SwingAngles( dest, 15, 30, 0.1f, &pi->torso.pitchAngle, &pi->torso.pitching );
632 torsoAngles[PITCH] = pi->torso.pitchAngle;
633
634 if ( pi->fixedtorso ) {
635 torsoAngles[PITCH] = 0.0f;
636 }
637
638 if ( pi->fixedlegs ) {
639 legsAngles[YAW] = torsoAngles[YAW];
640 legsAngles[PITCH] = 0.0f;
641 legsAngles[ROLL] = 0.0f;
642 }
643
644 // pull the angles back out of the hierarchial chain
645 AnglesSubtract( headAngles, torsoAngles, headAngles );
646 AnglesSubtract( torsoAngles, legsAngles, torsoAngles );
647 AnglesToAxis( legsAngles, legs );
648 AnglesToAxis( torsoAngles, torso );
649 AnglesToAxis( headAngles, head );
650 }
651
652
653 /*
654 ===============
655 UI_PlayerFloatSprite
656 ===============
657 */
658 static void UI_PlayerFloatSprite( playerInfo_t *pi, vec3_t origin, qhandle_t shader ) {
659 refEntity_t ent;
660
661 memset( &ent, 0, sizeof( ent ) );
662 VectorCopy( origin, ent.origin );
663 ent.origin[2] += 48;
664 ent.reType = RT_SPRITE;
665 ent.customShader = shader;
666 ent.radius = 10;
667 ent.renderfx = 0;
668 trap_R_AddRefEntityToScene( &ent );
669 }
670
671
672 /*
673 ======================
674 UI_MachinegunSpinAngle
675 ======================
676 */
677 float UI_MachinegunSpinAngle( playerInfo_t *pi ) {
678 int delta;
679 float angle;
680 float speed;
681 int torsoAnim;
682
683 delta = dp_realtime - pi->barrelTime;
684 if ( pi->barrelSpinning ) {
685 angle = pi->barrelAngle + delta * SPIN_SPEED;
686 } else {
687 if ( delta > COAST_TIME ) {
688 delta = COAST_TIME;
689 }
690
691 speed = 0.5 * ( SPIN_SPEED + (float)( COAST_TIME - delta ) / COAST_TIME );
692 angle = pi->barrelAngle + delta * speed;
693 }
694
695 torsoAnim = pi->torsoAnim & ~ANIM_TOGGLEBIT;
696 if( torsoAnim == TORSO_ATTACK2 ) {
697 torsoAnim = TORSO_ATTACK;
698 }
699 if ( pi->barrelSpinning == !(torsoAnim == TORSO_ATTACK) ) {
700 pi->barrelTime = dp_realtime;
701 pi->barrelAngle = AngleMod( angle );
702 pi->barrelSpinning = !!(torsoAnim == TORSO_ATTACK);
703 }
704
705 return angle;
706 }
707
708
709 /*
710 ===============
711 UI_DrawPlayer
712 ===============
713 */
714 void UI_DrawPlayer( float x, float y, float w, float h, playerInfo_t *pi, int time ) {
715 refdef_t refdef;
716 refEntity_t legs = {0};
717 refEntity_t torso = {0};
718 refEntity_t head = {0};
719 refEntity_t gun = {0};
720 refEntity_t barrel = {0};
721 refEntity_t flash = {0};
722 vec3_t origin;
723 int renderfx;
724 vec3_t mins = {-16, -16, -24};
725 vec3_t maxs = {16, 16, 32};
726 float len;
727 float xx;
728
729 if ( !pi->legsModel || !pi->torsoModel || !pi->headModel || !pi->animations[0].numFrames ) {
730 return;
731 }
732
733 dp_realtime = time;
734
735 if ( pi->pendingWeapon != WP_NUM_WEAPONS && dp_realtime > pi->weaponTimer ) {
736 pi->weapon = pi->pendingWeapon;
737 pi->lastWeapon = pi->pendingWeapon;
738 pi->pendingWeapon = WP_NUM_WEAPONS;
739 pi->weaponTimer = 0;
740 if( pi->currentWeapon != pi->weapon ) {
741 trap_S_StartLocalSound( weaponChangeSound, CHAN_LOCAL );
742 }
743 }
744
745 UI_AdjustFrom640( &x, &y, &w, &h );
746
747 y -= jumpHeight;
748
749 memset( &refdef, 0, sizeof( refdef ) );
750 memset( &legs, 0, sizeof(legs) );
751 memset( &torso, 0, sizeof(torso) );
752 memset( &head, 0, sizeof(head) );
753
754 refdef.rdflags = RDF_NOWORLDMODEL;
755
756 AxisClear( refdef.viewaxis );
757
758 refdef.x = x;
759 refdef.y = y;
760 refdef.width = w;
761 refdef.height = h;
762
763 refdef.fov_x = (int)((float)refdef.width / uis.xscale / 640.0f * 90.0f);
764 xx = refdef.width / uis.xscale / tan( refdef.fov_x / 360 * M_PI );
765 refdef.fov_y = atan2( refdef.height / uis.yscale, xx );
766 refdef.fov_y *= ( 360 / M_PI );
767
768 // calculate distance so the player nearly fills the box
769 len = 0.7 * ( maxs[2] - mins[2] );
770 origin[0] = len / tan( DEG2RAD(refdef.fov_x) * 0.5 );
771 origin[1] = 0.5 * ( mins[1] + maxs[1] );
772 origin[2] = -0.5 * ( mins[2] + maxs[2] );
773
774 refdef.time = dp_realtime;
775
776 trap_R_ClearScene();
777
778 // get the rotation information
779 UI_PlayerAngles( pi, legs.axis, torso.axis, head.axis );
780
781 // get the animation state (after rotation, to allow feet shuffle)
782 UI_PlayerAnimation( pi, &legs.oldframe, &legs.frame, &legs.backlerp,
783 &torso.oldframe, &torso.frame, &torso.backlerp );
784
785 renderfx = RF_LIGHTING_ORIGIN | RF_NOSHADOW;
786
787 //
788 // add the legs
789 //
790 legs.hModel = pi->legsModel;
791 legs.customSkin = pi->legsSkin;
792
793 VectorCopy( origin, legs.origin );
794
795 VectorCopy( origin, legs.lightingOrigin );
796 legs.renderfx = renderfx;
797 VectorCopy (legs.origin, legs.oldorigin);
798
799 trap_R_AddRefEntityToScene( &legs );
800
801 if (!legs.hModel) {
802 return;
803 }
804
805 //
806 // add the torso
807 //
808 torso.hModel = pi->torsoModel;
809 if (!torso.hModel) {
810 return;
811 }
812
813 torso.customSkin = pi->torsoSkin;
814
815 VectorCopy( origin, torso.lightingOrigin );
816
817 UI_PositionRotatedEntityOnTag( &torso, &legs, pi->legsModel, "tag_torso");
818
819 torso.renderfx = renderfx;
820
821 trap_R_AddRefEntityToScene( &torso );
822
823 //
824 // add the head
825 //
826 head.hModel = pi->headModel;
827 if (!head.hModel) {
828 return;
829 }
830 head.customSkin = pi->headSkin;
831
832 VectorCopy( origin, head.lightingOrigin );
833
834 UI_PositionRotatedEntityOnTag( &head, &torso, pi->torsoModel, "tag_head");
835
836 head.renderfx = renderfx;
837
838 trap_R_AddRefEntityToScene( &head );
839
840 //
841 // add the gun
842 //
843 if ( pi->currentWeapon != WP_NONE ) {
844 memset( &gun, 0, sizeof(gun) );
845 gun.hModel = pi->weaponModel;
846 if( pi->currentWeapon == WP_RAILGUN ) {
847 Byte4Copy( pi->c1RGBA, gun.shaderRGBA );
848 }
849 else {
850 Byte4Copy( colorWhite, gun.shaderRGBA );
851 }
852 VectorCopy( origin, gun.lightingOrigin );
853 UI_PositionEntityOnTag( &gun, &torso, pi->torsoModel, "tag_weapon");
854 gun.renderfx = renderfx;
855 trap_R_AddRefEntityToScene( &gun );
856 }
857
858 //
859 // add the spinning barrel
860 //
861 if ( pi->realWeapon == WP_MACHINEGUN || pi->realWeapon == WP_GAUNTLET || pi->realWeapon == WP_BFG ) {
862 vec3_t angles;
863
864 memset( &barrel, 0, sizeof(barrel) );
865 VectorCopy( origin, barrel.lightingOrigin );
866 barrel.renderfx = renderfx;
867
868 barrel.hModel = pi->barrelModel;
869 angles[YAW] = 0;
870 angles[PITCH] = 0;
871 angles[ROLL] = UI_MachinegunSpinAngle( pi );
872 AnglesToAxis( angles, barrel.axis );
873
874 UI_PositionRotatedEntityOnTag( &barrel, &gun, pi->weaponModel, "tag_barrel");
875
876 trap_R_AddRefEntityToScene( &barrel );
877 }
878
879 //
880 // add muzzle flash
881 //
882 if ( dp_realtime <= pi->muzzleFlashTime ) {
883 if ( pi->flashModel ) {
884 memset( &flash, 0, sizeof(flash) );
885 flash.hModel = pi->flashModel;
886 if( pi->currentWeapon == WP_RAILGUN ) {
887 Byte4Copy( pi->c1RGBA, flash.shaderRGBA );
888 }
889 else {
890 Byte4Copy( colorWhite, flash.shaderRGBA );
891 }
892 VectorCopy( origin, flash.lightingOrigin );
893 UI_PositionEntityOnTag( &flash, &gun, pi->weaponModel, "tag_flash");
894 flash.renderfx = renderfx;
895 trap_R_AddRefEntityToScene( &flash );
896 }
897
898 // make a dlight for the flash
899 if ( pi->flashDlightColor[0] || pi->flashDlightColor[1] || pi->flashDlightColor[2] ) {
900 trap_R_AddLightToScene( flash.origin, 200 + (rand()&31), pi->flashDlightColor[0],
901 pi->flashDlightColor[1], pi->flashDlightColor[2] );
902 }
903 }
904
905 //
906 // add the chat icon
907 //
908 if ( pi->chat ) {
909 UI_PlayerFloatSprite( pi, origin, trap_R_RegisterShaderNoMip( "sprites/balloon3" ) );
910 }
911
912 //
913 // add an accent light
914 //
915 origin[0] -= 100; // + = behind, - = in front
916 origin[1] += 100; // + = left, - = right
917 origin[2] += 100; // + = above, - = below
918 trap_R_AddLightToScene( origin, 500, 1.0, 1.0, 1.0 );
919
920 origin[0] -= 100;
921 origin[1] -= 100;
922 origin[2] -= 100;
923 trap_R_AddLightToScene( origin, 500, 1.0, 0.0, 0.0 );
924
925 trap_R_RenderScene( &refdef );
926 }
927
928
929 /*
930 ==========================
931 UI_RegisterClientSkin
932 ==========================
933 */
934 static qboolean UI_RegisterClientSkin( playerInfo_t *pi, const char *modelName, const char *skinName ) {
935 char filename[MAX_QPATH];
936
937 Com_sprintf( filename, sizeof( filename ), "models/players/%s/lower_%s.skin", modelName, skinName );
938 pi->legsSkin = trap_R_RegisterSkin( filename );
939
940 Com_sprintf( filename, sizeof( filename ), "models/players/%s/upper_%s.skin", modelName, skinName );
941 pi->torsoSkin = trap_R_RegisterSkin( filename );
942
943 Com_sprintf( filename, sizeof( filename ), "models/players/%s/head_%s.skin", modelName, skinName );
944 pi->headSkin = trap_R_RegisterSkin( filename );
945
946 if ( !pi->legsSkin || !pi->torsoSkin || !pi->headSkin ) {
947 return qfalse;
948 }
949
950 return qtrue;
951 }
952
953
954 /*
955 ======================
956 UI_ParseAnimationFile
957 ======================
958 */
959 static qboolean UI_ParseAnimationFile( const char *filename, playerInfo_t *pi ) {
960 char *text_p, *prev;
961 int len;
962 int i;
963 char *token;
964 float fps;
965 int skip;
966 char text[20000];
967 fileHandle_t f;
968 animation_t *animations;
969
970 animations = pi->animations;
971
972 memset( animations, 0, sizeof( animation_t ) * MAX_ANIMATIONS );
973
974 pi->fixedlegs = qfalse;
975 pi->fixedtorso = qfalse;
976
977 // load the file
978 len = trap_FS_FOpenFile( filename, &f, FS_READ );
979 if ( len <= 0 ) {
980 return qfalse;
981 }
982 if ( len >= ( sizeof( text ) - 1 ) ) {
983 Com_Printf( "File %s too long\n", filename );
984 trap_FS_FCloseFile( f );
985 return qfalse;
986 }
987 trap_FS_Read( text, len, f );
988 text[len] = 0;
989 trap_FS_FCloseFile( f );
990
991 // parse the text
992 text_p = text;
993 skip = 0; // quite the compiler warning
994
995 // read optional parameters
996 while ( 1 ) {
997 prev = text_p; // so we can unget
998 token = COM_Parse( &text_p );
999 if ( !token[0] ) {
1000 break;
1001 }
1002 if ( !Q_stricmp( token, "footsteps" ) ) {
1003 token = COM_Parse( &text_p );
1004 if ( !token[0] ) {
1005 break;
1006 }
1007 continue;
1008 } else if ( !Q_stricmp( token, "headoffset" ) ) {
1009 for ( i = 0 ; i < 3 ; i++ ) {
1010 token = COM_Parse( &text_p );
1011 if ( !token[0] ) {
1012 break;
1013 }
1014 }
1015 continue;
1016 } else if ( !Q_stricmp( token, "sex" ) ) {
1017 token = COM_Parse( &text_p );
1018 if ( !token[0] ) {
1019 break;
1020 }
1021 continue;
1022 } else if ( !Q_stricmp( token, "fixedlegs" ) ) {
1023 pi->fixedlegs = qtrue;
1024 continue;
1025 } else if ( !Q_stricmp( token, "fixedtorso" ) ) {
1026 pi->fixedtorso = qtrue;
1027 continue;
1028 }
1029
1030 // if it is a number, start parsing animations
1031 if ( token[0] >= '0' && token[0] <= '9' ) {
1032 text_p = prev; // unget the token
1033 break;
1034 }
1035
1036 Com_Printf( "unknown token '%s' in %s\n", token, filename );
1037 }
1038
1039 // read information for each frame
1040 for ( i = 0 ; i < MAX_ANIMATIONS ; i++ ) {
1041
1042 token = COM_Parse( &text_p );
1043 if ( !token[0] ) {
1044 if( i >= TORSO_GETFLAG && i <= TORSO_NEGATIVE ) {
1045 animations[i].firstFrame = animations[TORSO_GESTURE].firstFrame;
1046 animations[i].frameLerp = animations[TORSO_GESTURE].frameLerp;
1047 animations[i].initialLerp = animations[TORSO_GESTURE].initialLerp;
1048 animations[i].loopFrames = animations[TORSO_GESTURE].loopFrames;
1049 animations[i].numFrames = animations[TORSO_GESTURE].numFrames;
1050 animations[i].reversed = qfalse;
1051 animations[i].flipflop = qfalse;
1052 continue;
1053 }
1054 break;
1055 }
1056 animations[i].firstFrame = atoi( token );
1057 // leg only frames are adjusted to not count the upper body only frames
1058 if ( i == LEGS_WALKCR ) {
1059 skip = animations[LEGS_WALKCR].firstFrame - animations[TORSO_GESTURE].firstFrame;
1060 }
1061 if ( i >= LEGS_WALKCR && i<TORSO_GETFLAG) {
1062 animations[i].firstFrame -= skip;
1063 }
1064
1065 token = COM_Parse( &text_p );
1066 if ( !token[0] ) {
1067 break;
1068 }
1069 animations[i].numFrames = atoi( token );
1070
1071 animations[i].reversed = qfalse;
1072 animations[i].flipflop = qfalse;
1073 // if numFrames is negative the animation is reversed
1074 if (animations[i].numFrames < 0) {
1075 animations[i].numFrames = -animations[i].numFrames;
1076 animations[i].reversed = qtrue;
1077 }
1078
1079 token = COM_Parse( &text_p );
1080 if ( !token[0] ) {
1081 break;
1082 }
1083 animations[i].loopFrames = atoi( token );
1084
1085 token = COM_Parse( &text_p );
1086 if ( !token[0] ) {
1087 break;
1088 }
1089 fps = atof( token );
1090 if ( fps == 0 ) {
1091 fps = 1;
1092 }
1093 animations[i].frameLerp = 1000 / fps;
1094 animations[i].initialLerp = 1000 / fps;
1095 }
1096
1097 if ( i != MAX_ANIMATIONS ) {
1098 Com_Printf( "Error parsing animation file: %s\n", filename );
1099 return qfalse;
1100 }
1101
1102 return qtrue;
1103 }
1104
1105
1106 /*
1107 ==========================
1108 UI_RegisterClientModelname
1109 ==========================
1110 */
1111 qboolean UI_RegisterClientModelname( playerInfo_t *pi, const char *modelSkinName ) {
1112 char modelName[MAX_QPATH];
1113 char skinName[MAX_QPATH];
1114 char filename[MAX_QPATH];
1115 char *slash;
1116
1117 pi->torsoModel = 0;
1118 pi->headModel = 0;
1119
1120 if ( !modelSkinName[0] ) {
1121 return qfalse;
1122 }
1123
1124 Q_strncpyz( modelName, modelSkinName, sizeof( modelName ) );
1125
1126 slash = strchr( modelName, '/' );
1127 if ( !slash ) {
1128 // modelName did not include a skin name
1129 Q_strncpyz( skinName, "default", sizeof( skinName ) );
1130 } else {
1131 Q_strncpyz( skinName, slash + 1, sizeof( skinName ) );
1132 // truncate modelName
1133 *slash = 0;
1134 }
1135
1136 // load cmodels before models so filecache works
1137
1138 Com_sprintf( filename, sizeof( filename ), "models/players/%s/lower.md3", modelName );
1139 pi->legsModel = trap_R_RegisterModel( filename );
1140 if ( !pi->legsModel ) {
1141 Com_Printf( "Failed to load model file %s\n", filename );
1142 return qfalse;
1143 }
1144
1145 Com_sprintf( filename, sizeof( filename ), "models/players/%s/upper.md3", modelName );
1146 pi->torsoModel = trap_R_RegisterModel( filename );
1147 if ( !pi->torsoModel ) {
1148 Com_Printf( "Failed to load model file %s\n", filename );
1149 return qfalse;
1150 }
1151
1152 Com_sprintf( filename, sizeof( filename ), "models/players/%s/head.md3", modelName );
1153 pi->headModel = trap_R_RegisterModel( filename );
1154 if ( !pi->headModel ) {
1155 Com_Printf( "Failed to load model file %s\n", filename );
1156 return qfalse;
1157 }
1158
1159 // if any skins failed to load, fall back to default
1160 if ( !UI_RegisterClientSkin( pi, modelName, skinName ) ) {
1161 if ( !UI_RegisterClientSkin( pi, modelName, "default" ) ) {
1162 Com_Printf( "Failed to load skin file: %s : %s\n", modelName, skinName );
1163 return qfalse;
1164 }
1165 }
1166
1167 // load the animations
1168 Com_sprintf( filename, sizeof( filename ), "models/players/%s/animation.cfg", modelName );
1169 if ( !UI_ParseAnimationFile( filename, pi ) ) {
1170 Com_Printf( "Failed to load animation file %s\n", filename );
1171 return qfalse;
1172 }
1173
1174 return qtrue;
1175 }
1176
1177
1178 /*
1179 ===============
1180 UI_PlayerInfo_SetModel
1181 ===============
1182 */
1183 void UI_PlayerInfo_SetModel( playerInfo_t *pi, const char *model ) {
1184 memset( pi, 0, sizeof(*pi) );
1185 UI_RegisterClientModelname( pi, model );
1186 pi->weapon = WP_MACHINEGUN;
1187 pi->currentWeapon = pi->weapon;
1188 pi->lastWeapon = pi->weapon;
1189 pi->pendingWeapon = WP_NUM_WEAPONS;
1190 pi->weaponTimer = 0;
1191 pi->chat = qfalse;
1192 pi->newModel = qtrue;
1193 UI_PlayerInfo_SetWeapon( pi, pi->weapon );
1194 }
1195
1196
1197 /*
1198 ===============
1199 UI_PlayerInfo_SetInfo
1200 ===============
1201 */
1202 void UI_PlayerInfo_SetInfo( playerInfo_t *pi, int legsAnim, int torsoAnim, vec3_t viewAngles, vec3_t moveAngles, weapon_t weaponNumber, qboolean chat ) {
1203 int currentAnim;
1204 weapon_t weaponNum;
1205 int c;
1206
1207 pi->chat = chat;
1208
1209 c = (int)trap_Cvar_VariableValue( "color1" );
1210
1211 VectorClear( pi->color1 );
1212
1213 if( c < 1 || c > 7 ) {
1214 VectorSet( pi->color1, 1, 1, 1 );
1215 }
1216 else {
1217 if( c & 1 ) {
1218 pi->color1[2] = 1.0f;
1219 }
1220
1221 if( c & 2 ) {
1222 pi->color1[1] = 1.0f;
1223 }
1224
1225 if( c & 4 ) {
1226 pi->color1[0] = 1.0f;
1227 }
1228 }
1229
1230 pi->c1RGBA[0] = 255 * pi->color1[0];
1231 pi->c1RGBA[1] = 255 * pi->color1[1];
1232 pi->c1RGBA[2] = 255 * pi->color1[2];
1233 pi->c1RGBA[3] = 255;
1234
1235 // view angles
1236 VectorCopy( viewAngles, pi->viewAngles );
1237
1238 // move angles
1239 VectorCopy( moveAngles, pi->moveAngles );
1240
1241 if ( pi->newModel ) {
1242 pi->newModel = qfalse;
1243
1244 jumpHeight = 0;
1245 pi->pendingLegsAnim = 0;
1246 UI_ForceLegsAnim( pi, legsAnim );
1247 pi->legs.yawAngle = viewAngles[YAW];
1248 pi->legs.yawing = qfalse;
1249
1250 pi->pendingTorsoAnim = 0;
1251 UI_ForceTorsoAnim( pi, torsoAnim );
1252 pi->torso.yawAngle = viewAngles[YAW];
1253 pi->torso.yawing = qfalse;
1254
1255 if ( weaponNumber != WP_NUM_WEAPONS ) {
1256 pi->weapon = weaponNumber;
1257 pi->currentWeapon = weaponNumber;
1258 pi->lastWeapon = weaponNumber;
1259 pi->pendingWeapon = WP_NUM_WEAPONS;
1260 pi->weaponTimer = 0;
1261 UI_PlayerInfo_SetWeapon( pi, pi->weapon );
1262 }
1263
1264 return;
1265 }
1266
1267 // weapon
1268 if ( weaponNumber == WP_NUM_WEAPONS ) {
1269 pi->pendingWeapon = WP_NUM_WEAPONS;
1270 pi->weaponTimer = 0;
1271 }
1272 else if ( weaponNumber != WP_NONE ) {
1273 pi->pendingWeapon = weaponNumber;
1274 pi->weaponTimer = dp_realtime + UI_TIMER_WEAPON_DELAY;
1275 }
1276 weaponNum = pi->lastWeapon;
1277 pi->weapon = weaponNum;
1278
1279 if ( torsoAnim == BOTH_DEATH1 || legsAnim == BOTH_DEATH1 ) {
1280 torsoAnim = legsAnim = BOTH_DEATH1;
1281 pi->weapon = pi->currentWeapon = WP_NONE;
1282 UI_PlayerInfo_SetWeapon( pi, pi->weapon );
1283
1284 jumpHeight = 0;
1285 pi->pendingLegsAnim = 0;
1286 UI_ForceLegsAnim( pi, legsAnim );
1287
1288 pi->pendingTorsoAnim = 0;
1289 UI_ForceTorsoAnim( pi, torsoAnim );
1290
1291 return;
1292 }
1293
1294 // leg animation
1295 currentAnim = pi->legsAnim & ~ANIM_TOGGLEBIT;
1296 if ( legsAnim != LEGS_JUMP && ( currentAnim == LEGS_JUMP || currentAnim == LEGS_LAND ) ) {
1297 pi->pendingLegsAnim = legsAnim;
1298 }
1299 else if ( legsAnim != currentAnim ) {
1300 jumpHeight = 0;
1301 pi->pendingLegsAnim = 0;
1302 UI_ForceLegsAnim( pi, legsAnim );
1303 }
1304
1305 // torso animation
1306 if ( torsoAnim == TORSO_STAND || torsoAnim == TORSO_STAND2 ) {
1307 if ( weaponNum == WP_NONE || weaponNum == WP_GAUNTLET ) {
1308 torsoAnim = TORSO_STAND2;
1309 }
1310 else {
1311 torsoAnim = TORSO_STAND;
1312 }
1313 }
1314
1315 if ( torsoAnim == TORSO_ATTACK || torsoAnim == TORSO_ATTACK2 ) {
1316 if ( weaponNum == WP_NONE || weaponNum == WP_GAUNTLET ) {
1317 torsoAnim = TORSO_ATTACK2;
1318 }
1319 else {
1320 torsoAnim = TORSO_ATTACK;
1321 }
1322 pi->muzzleFlashTime = dp_realtime + UI_TIMER_MUZZLE_FLASH;
1323 //FIXME play firing sound here
1324 }
1325
1326 currentAnim = pi->torsoAnim & ~ANIM_TOGGLEBIT;
1327
1328 if ( weaponNum != pi->currentWeapon || currentAnim == TORSO_RAISE || currentAnim == TORSO_DROP ) {
1329 pi->pendingTorsoAnim = torsoAnim;
1330 }
1331 else if ( ( currentAnim == TORSO_GESTURE || currentAnim == TORSO_ATTACK ) && ( torsoAnim != currentAnim ) ) {
1332 pi->pendingTorsoAnim = torsoAnim;
1333 }
1334 else if ( torsoAnim != currentAnim ) {
1335 pi->pendingTorsoAnim = 0;
1336 UI_ForceTorsoAnim( pi, torsoAnim );
1337 }
1338 }