Debian Codegraph @

code/q3_ui/ui_players.c

raw ◀ ▶
1 Debian patch touch this file
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 strcat( path, "_barrel.md3" );
93 pi->barrelModel = trap_R_RegisterModel( path );
94 }
95
•96 COM_StripExtension( item->world_model[0], path, sizeof(path) );
97 strcat( 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, ((refEntity_t *)parent)->axis, tempAxis );
334 MatrixMultiply( lerped.axis, tempAxis, 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;
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 ( dp_realtime < lf->animationTime ) {
383 lf->frameTime = lf->animationTime; // initial lerp
384 } else {
385 lf->frameTime = lf->oldFrameTime + anim->frameLerp;
386 }
387 f = ( lf->frameTime - lf->animationTime ) / anim->frameLerp;
388 if ( f >= anim->numFrames ) {
389 f -= anim->numFrames;
390 if ( anim->loopFrames ) {
391 f %= anim->loopFrames;
392 f += anim->numFrames - anim->loopFrames;
393 } else {
394 f = anim->numFrames - 1;
395 // the animation is stuck at the end, so it
396 // can immediately transition to another sequence
397 lf->frameTime = dp_realtime;
398 }
399 }
400 lf->frame = anim->firstFrame + f;
401 if ( dp_realtime > lf->frameTime ) {
402 lf->frameTime = dp_realtime;
403 }
404 }
405
406 if ( lf->frameTime > dp_realtime + 200 ) {
407 lf->frameTime = dp_realtime;
408 }
409
410 if ( lf->oldFrameTime > dp_realtime ) {
411 lf->oldFrameTime = dp_realtime;
412 }
413 // calculate current lerp value
414 if ( lf->frameTime == lf->oldFrameTime ) {
415 lf->backlerp = 0;
416 } else {
417 lf->backlerp = 1.0 - (float)( dp_realtime - lf->oldFrameTime ) / ( lf->frameTime - lf->oldFrameTime );
418 }
419 }
420
421
422 /*
423 ===============
424 UI_PlayerAnimation
425 ===============
426 */
427 static void UI_PlayerAnimation( playerInfo_t *pi, int *legsOld, int *legs, float *legsBackLerp,
428 int *torsoOld, int *torso, float *torsoBackLerp ) {
429
430 // legs animation
431 pi->legsAnimationTimer -= uis.frametime;
432 if ( pi->legsAnimationTimer < 0 ) {
433 pi->legsAnimationTimer = 0;
434 }
435
436 UI_LegsSequencing( pi );
437
438 if ( pi->legs.yawing && ( pi->legsAnim & ~ANIM_TOGGLEBIT ) == LEGS_IDLE ) {
439 UI_RunLerpFrame( pi, &pi->legs, LEGS_TURN );
440 } else {
441 UI_RunLerpFrame( pi, &pi->legs, pi->legsAnim );
442 }
443 *legsOld = pi->legs.oldFrame;
444 *legs = pi->legs.frame;
445 *legsBackLerp = pi->legs.backlerp;
446
447 // torso animation
448 pi->torsoAnimationTimer -= uis.frametime;
449 if ( pi->torsoAnimationTimer < 0 ) {
450 pi->torsoAnimationTimer = 0;
451 }
452
453 UI_TorsoSequencing( pi );
454
455 UI_RunLerpFrame( pi, &pi->torso, pi->torsoAnim );
456 *torsoOld = pi->torso.oldFrame;
457 *torso = pi->torso.frame;
458 *torsoBackLerp = pi->torso.backlerp;
459 }
460
461
462 /*
463 ==================
464 UI_SwingAngles
465 ==================
466 */
467 static void UI_SwingAngles( float destination, float swingTolerance, float clampTolerance,
468 float speed, float *angle, qboolean *swinging ) {
469 float swing;
470 float move;
471 float scale;
472
473 if ( !*swinging ) {
474 // see if a swing should be started
475 swing = AngleSubtract( *angle, destination );
476 if ( swing > swingTolerance || swing < -swingTolerance ) {
477 *swinging = qtrue;
478 }
479 }
480
481 if ( !*swinging ) {
482 return;
483 }
484
485 // modify the speed depending on the delta
486 // so it doesn't seem so linear
487 swing = AngleSubtract( destination, *angle );
488 scale = fabs( swing );
489 if ( scale < swingTolerance * 0.5 ) {
490 scale = 0.5;
491 } else if ( scale < swingTolerance ) {
492 scale = 1.0;
493 } else {
494 scale = 2.0;
495 }
496
497 // swing towards the destination angle
498 if ( swing >= 0 ) {
499 move = uis.frametime * scale * speed;
500 if ( move >= swing ) {
501 move = swing;
502 *swinging = qfalse;
503 }
504 *angle = AngleMod( *angle + move );
505 } else if ( swing < 0 ) {
506 move = uis.frametime * scale * -speed;
507 if ( move <= swing ) {
508 move = swing;
509 *swinging = qfalse;
510 }
511 *angle = AngleMod( *angle + move );
512 }
513
514 // clamp to no more than tolerance
515 swing = AngleSubtract( destination, *angle );
516 if ( swing > clampTolerance ) {
517 *angle = AngleMod( destination - (clampTolerance - 1) );
518 } else if ( swing < -clampTolerance ) {
519 *angle = AngleMod( destination + (clampTolerance - 1) );
520 }
521 }
522
523
524 /*
525 ======================
526 UI_MovedirAdjustment
527 ======================
528 */
529 static float UI_MovedirAdjustment( playerInfo_t *pi ) {
530 vec3_t relativeAngles;
531 vec3_t moveVector;
532
533 VectorSubtract( pi->viewAngles, pi->moveAngles, relativeAngles );
534 AngleVectors( relativeAngles, moveVector, NULL, NULL );
535 if ( Q_fabs( moveVector[0] ) < 0.01 ) {
536 moveVector[0] = 0.0;
537 }
538 if ( Q_fabs( moveVector[1] ) < 0.01 ) {
539 moveVector[1] = 0.0;
540 }
541
542 if ( moveVector[1] == 0 && moveVector[0] > 0 ) {
543 return 0;
544 }
545 if ( moveVector[1] < 0 && moveVector[0] > 0 ) {
546 return 22;
547 }
548 if ( moveVector[1] < 0 && moveVector[0] == 0 ) {
549 return 45;
550 }
551 if ( moveVector[1] < 0 && moveVector[0] < 0 ) {
552 return -22;
553 }
554 if ( moveVector[1] == 0 && moveVector[0] < 0 ) {
555 return 0;
556 }
557 if ( moveVector[1] > 0 && moveVector[0] < 0 ) {
558 return 22;
559 }
560 if ( moveVector[1] > 0 && moveVector[0] == 0 ) {
561 return -45;
562 }
563
564 return -22;
565 }
566
567
568 /*
569 ===============
570 UI_PlayerAngles
571 ===============
572 */
573 static void UI_PlayerAngles( playerInfo_t *pi, vec3_t legs[3], vec3_t torso[3], vec3_t head[3] ) {
574 vec3_t legsAngles, torsoAngles, headAngles;
575 float dest;
576 float adjust;
577
578 VectorCopy( pi->viewAngles, headAngles );
579 headAngles[YAW] = AngleMod( headAngles[YAW] );
580 VectorClear( legsAngles );
581 VectorClear( torsoAngles );
582
583 // --------- yaw -------------
584
585 // allow yaw to drift a bit
586 if ( ( pi->legsAnim & ~ANIM_TOGGLEBIT ) != LEGS_IDLE
587 || ( pi->torsoAnim & ~ANIM_TOGGLEBIT ) != TORSO_STAND ) {
588 // if not standing still, always point all in the same direction
589 pi->torso.yawing = qtrue; // always center
590 pi->torso.pitching = qtrue; // always center
591 pi->legs.yawing = qtrue; // always center
592 }
593
594 // adjust legs for movement dir
595 adjust = UI_MovedirAdjustment( pi );
596 legsAngles[YAW] = headAngles[YAW] + adjust;
597 torsoAngles[YAW] = headAngles[YAW] + 0.25 * adjust;
598
599
600 // torso
601 UI_SwingAngles( torsoAngles[YAW], 25, 90, SWINGSPEED, &pi->torso.yawAngle, &pi->torso.yawing );
602 UI_SwingAngles( legsAngles[YAW], 40, 90, SWINGSPEED, &pi->legs.yawAngle, &pi->legs.yawing );
603
604 torsoAngles[YAW] = pi->torso.yawAngle;
605 legsAngles[YAW] = pi->legs.yawAngle;
606
607 // --------- pitch -------------
608
609 // only show a fraction of the pitch angle in the torso
610 if ( headAngles[PITCH] > 180 ) {
611 dest = (-360 + headAngles[PITCH]) * 0.75;
612 } else {
613 dest = headAngles[PITCH] * 0.75;
614 }
615 UI_SwingAngles( dest, 15, 30, 0.1f, &pi->torso.pitchAngle, &pi->torso.pitching );
616 torsoAngles[PITCH] = pi->torso.pitchAngle;
617
618 // pull the angles back out of the hierarchial chain
619 AnglesSubtract( headAngles, torsoAngles, headAngles );
620 AnglesSubtract( torsoAngles, legsAngles, torsoAngles );
621 AnglesToAxis( legsAngles, legs );
622 AnglesToAxis( torsoAngles, torso );
623 AnglesToAxis( headAngles, head );
624 }
625
626
627 /*
628 ===============
629 UI_PlayerFloatSprite
630 ===============
631 */
632 static void UI_PlayerFloatSprite( playerInfo_t *pi, vec3_t origin, qhandle_t shader ) {
633 refEntity_t ent;
634
635 memset( &ent, 0, sizeof( ent ) );
636 VectorCopy( origin, ent.origin );
637 ent.origin[2] += 48;
638 ent.reType = RT_SPRITE;
639 ent.customShader = shader;
640 ent.radius = 10;
641 ent.renderfx = 0;
642 trap_R_AddRefEntityToScene( &ent );
643 }
644
645
646 /*
647 ======================
648 UI_MachinegunSpinAngle
649 ======================
650 */
651 float UI_MachinegunSpinAngle( playerInfo_t *pi ) {
652 int delta;
653 float angle;
654 float speed;
655 int torsoAnim;
656
657 delta = dp_realtime - pi->barrelTime;
658 if ( pi->barrelSpinning ) {
659 angle = pi->barrelAngle + delta * SPIN_SPEED;
660 } else {
661 if ( delta > COAST_TIME ) {
662 delta = COAST_TIME;
663 }
664
665 speed = 0.5 * ( SPIN_SPEED + (float)( COAST_TIME - delta ) / COAST_TIME );
666 angle = pi->barrelAngle + delta * speed;
667 }
668
669 torsoAnim = pi->torsoAnim & ~ANIM_TOGGLEBIT;
670 if( torsoAnim == TORSO_ATTACK2 ) {
671 torsoAnim = TORSO_ATTACK;
672 }
673 if ( pi->barrelSpinning == !(torsoAnim == TORSO_ATTACK) ) {
674 pi->barrelTime = dp_realtime;
675 pi->barrelAngle = AngleMod( angle );
676 pi->barrelSpinning = !!(torsoAnim == TORSO_ATTACK);
677 }
678
679 return angle;
680 }
681
682
683 /*
684 ===============
685 UI_DrawPlayer
686 ===============
687 */
688 void UI_DrawPlayer( float x, float y, float w, float h, playerInfo_t *pi, int time ) {
689 refdef_t refdef;
690 refEntity_t legs;
691 refEntity_t torso;
692 refEntity_t head;
693 refEntity_t gun;
694 refEntity_t barrel;
695 refEntity_t flash;
696 vec3_t origin;
697 int renderfx;
698 vec3_t mins = {-16, -16, -24};
699 vec3_t maxs = {16, 16, 32};
700 float len;
701 float xx;
702
703 if ( !pi->legsModel || !pi->torsoModel || !pi->headModel || !pi->animations[0].numFrames ) {
704 return;
705 }
706
707 dp_realtime = time;
708
709 if ( pi->pendingWeapon != -1 && dp_realtime > pi->weaponTimer ) {
710 pi->weapon = pi->pendingWeapon;
711 pi->lastWeapon = pi->pendingWeapon;
712 pi->pendingWeapon = -1;
713 pi->weaponTimer = 0;
714 if( pi->currentWeapon != pi->weapon ) {
715 trap_S_StartLocalSound( weaponChangeSound, CHAN_LOCAL );
716 }
717 }
718
719 UI_AdjustFrom640( &x, &y, &w, &h );
720
721 y -= jumpHeight;
722
723 memset( &refdef, 0, sizeof( refdef ) );
724 memset( &legs, 0, sizeof(legs) );
725 memset( &torso, 0, sizeof(torso) );
726 memset( &head, 0, sizeof(head) );
727
728 refdef.rdflags = RDF_NOWORLDMODEL;
729
730 AxisClear( refdef.viewaxis );
731
732 refdef.x = x;
733 refdef.y = y;
734 refdef.width = w;
735 refdef.height = h;
736
737 refdef.fov_x = (int)((float)refdef.width / 640.0f * 90.0f);
738 xx = refdef.width / tan( refdef.fov_x / 360 * M_PI );
739 refdef.fov_y = atan2( refdef.height, xx );
740 refdef.fov_y *= ( 360 / M_PI );
741
742 // calculate distance so the player nearly fills the box
743 len = 0.7 * ( maxs[2] - mins[2] );
744 origin[0] = len / tan( DEG2RAD(refdef.fov_x) * 0.5 );
745 origin[1] = 0.5 * ( mins[1] + maxs[1] );
746 origin[2] = -0.5 * ( mins[2] + maxs[2] );
747
748 refdef.time = dp_realtime;
749
750 trap_R_ClearScene();
751
752 // get the rotation information
753 UI_PlayerAngles( pi, legs.axis, torso.axis, head.axis );
754
755 // get the animation state (after rotation, to allow feet shuffle)
756 UI_PlayerAnimation( pi, &legs.oldframe, &legs.frame, &legs.backlerp,
757 &torso.oldframe, &torso.frame, &torso.backlerp );
758
759 renderfx = RF_LIGHTING_ORIGIN | RF_NOSHADOW;
760
761 //
762 // add the legs
763 //
764 legs.hModel = pi->legsModel;
765 legs.customSkin = pi->legsSkin;
766
767 VectorCopy( origin, legs.origin );
768
769 VectorCopy( origin, legs.lightingOrigin );
770 legs.renderfx = renderfx;
771 VectorCopy (legs.origin, legs.oldorigin);
772
773 trap_R_AddRefEntityToScene( &legs );
774
775 if (!legs.hModel) {
776 return;
777 }
778
779 //
780 // add the torso
781 //
782 torso.hModel = pi->torsoModel;
783 if (!torso.hModel) {
784 return;
785 }
786
787 torso.customSkin = pi->torsoSkin;
788
789 VectorCopy( origin, torso.lightingOrigin );
790
791 UI_PositionRotatedEntityOnTag( &torso, &legs, pi->legsModel, "tag_torso");
792
793 torso.renderfx = renderfx;
794
795 trap_R_AddRefEntityToScene( &torso );
796
797 //
798 // add the head
799 //
800 head.hModel = pi->headModel;
801 if (!head.hModel) {
802 return;
803 }
804 head.customSkin = pi->headSkin;
805
806 VectorCopy( origin, head.lightingOrigin );
807
808 UI_PositionRotatedEntityOnTag( &head, &torso, pi->torsoModel, "tag_head");
809
810 head.renderfx = renderfx;
811
812 trap_R_AddRefEntityToScene( &head );
813
814 //
815 // add the gun
816 //
817 if ( pi->currentWeapon != WP_NONE ) {
818 memset( &gun, 0, sizeof(gun) );
819 gun.hModel = pi->weaponModel;
820 VectorCopy( origin, gun.lightingOrigin );
821 UI_PositionEntityOnTag( &gun, &torso, pi->torsoModel, "tag_weapon");
822 gun.renderfx = renderfx;
823 trap_R_AddRefEntityToScene( &gun );
824 }
825
826 //
827 // add the spinning barrel
828 //
829 if ( pi->realWeapon == WP_MACHINEGUN || pi->realWeapon == WP_GAUNTLET || pi->realWeapon == WP_BFG ) {
830 vec3_t angles;
831
832 memset( &barrel, 0, sizeof(barrel) );
833 VectorCopy( origin, barrel.lightingOrigin );
834 barrel.renderfx = renderfx;
835
836 barrel.hModel = pi->barrelModel;
837 angles[YAW] = 0;
838 angles[PITCH] = 0;
839 angles[ROLL] = UI_MachinegunSpinAngle( pi );
840 if( pi->realWeapon == WP_GAUNTLET || pi->realWeapon == WP_BFG ) {
841 angles[PITCH] = angles[ROLL];
842 angles[ROLL] = 0;
843 }
844 AnglesToAxis( angles, barrel.axis );
845
846 UI_PositionRotatedEntityOnTag( &barrel, &gun, pi->weaponModel, "tag_barrel");
847
848 trap_R_AddRefEntityToScene( &barrel );
849 }
850
851 //
852 // add muzzle flash
853 //
854 if ( dp_realtime <= pi->muzzleFlashTime ) {
855 if ( pi->flashModel ) {
856 memset( &flash, 0, sizeof(flash) );
857 flash.hModel = pi->flashModel;
858 VectorCopy( origin, flash.lightingOrigin );
859 UI_PositionEntityOnTag( &flash, &gun, pi->weaponModel, "tag_flash");
860 flash.renderfx = renderfx;
861 trap_R_AddRefEntityToScene( &flash );
862 }
863
864 // make a dlight for the flash
865 if ( pi->flashDlightColor[0] || pi->flashDlightColor[1] || pi->flashDlightColor[2] ) {
866 trap_R_AddLightToScene( flash.origin, 200 + (rand()&31), pi->flashDlightColor[0],
867 pi->flashDlightColor[1], pi->flashDlightColor[2] );
868 }
869 }
870
871 //
872 // add the chat icon
873 //
874 if ( pi->chat ) {
875 UI_PlayerFloatSprite( pi, origin, trap_R_RegisterShaderNoMip( "sprites/balloon3" ) );
876 }
877
878 //
879 // add an accent light
880 //
881 origin[0] -= 100; // + = behind, - = in front
882 origin[1] += 100; // + = left, - = right
883 origin[2] += 100; // + = above, - = below
884 trap_R_AddLightToScene( origin, 500, 1.0, 1.0, 1.0 );
885
886 origin[0] -= 100;
887 origin[1] -= 100;
888 origin[2] -= 100;
889 trap_R_AddLightToScene( origin, 500, 1.0, 0.0, 0.0 );
890
891 trap_R_RenderScene( &refdef );
892 }
893
894
895 /*
896 ==========================
897 UI_RegisterClientSkin
898 ==========================
899 */
900 static qboolean UI_RegisterClientSkin( playerInfo_t *pi, const char *modelName, const char *skinName ) {
901 char filename[MAX_QPATH];
902
903 Com_sprintf( filename, sizeof( filename ), "models/players/%s/lower_%s.skin", modelName, skinName );
904 pi->legsSkin = trap_R_RegisterSkin( filename );
905
906 Com_sprintf( filename, sizeof( filename ), "models/players/%s/upper_%s.skin", modelName, skinName );
907 pi->torsoSkin = trap_R_RegisterSkin( filename );
908
909 Com_sprintf( filename, sizeof( filename ), "models/players/%s/head_%s.skin", modelName, skinName );
910 pi->headSkin = trap_R_RegisterSkin( filename );
911
912 if ( !pi->legsSkin || !pi->torsoSkin || !pi->headSkin ) {
913 return qfalse;
914 }
915
916 return qtrue;
917 }
918
919
920 /*
921 ======================
922 UI_ParseAnimationFile
923 ======================
924 */
925 static qboolean UI_ParseAnimationFile( const char *filename, animation_t *animations ) {
926 char *text_p, *prev;
927 int len;
928 int i;
929 char *token;
930 float fps;
931 int skip;
932 char text[20000];
933 fileHandle_t f;
934
935 memset( animations, 0, sizeof( animation_t ) * MAX_ANIMATIONS );
936
937 // load the file
938 len = trap_FS_FOpenFile( filename, &f, FS_READ );
939 if ( len <= 0 ) {
940 return qfalse;
941 }
942 if ( len >= ( sizeof( text ) - 1 ) ) {
943 Com_Printf( "File %s too long\n", filename );
944 trap_FS_FCloseFile( f );
945 return qfalse;
946 }
947 trap_FS_Read( text, len, f );
948 text[len] = 0;
949 trap_FS_FCloseFile( f );
950
951 // parse the text
952 text_p = text;
953 skip = 0; // quite the compiler warning
954
955 // read optional parameters
956 while ( 1 ) {
957 prev = text_p; // so we can unget
958 token = COM_Parse( &text_p );
959 if ( !token ) {
960 break;
961 }
962 if ( !Q_stricmp( token, "footsteps" ) ) {
963 token = COM_Parse( &text_p );
964 if ( !token ) {
965 break;
966 }
967 continue;
968 } else if ( !Q_stricmp( token, "headoffset" ) ) {
969 for ( i = 0 ; i < 3 ; i++ ) {
970 token = COM_Parse( &text_p );
971 if ( !token ) {
972 break;
973 }
974 }
975 continue;
976 } else if ( !Q_stricmp( token, "sex" ) ) {
977 token = COM_Parse( &text_p );
978 if ( !token ) {
979 break;
980 }
981 continue;
982 }
983
984 // if it is a number, start parsing animations
985 if ( token[0] >= '0' && token[0] <= '9' ) {
986 text_p = prev; // unget the token
987 break;
988 }
989
990 Com_Printf( "unknown token '%s' is %s\n", token, filename );
991 }
992
993 // read information for each frame
994 for ( i = 0 ; i < MAX_ANIMATIONS ; i++ ) {
995
996 token = COM_Parse( &text_p );
997 if ( !token ) {
998 break;
999 }
1000 animations[i].firstFrame = atoi( token );
1001 // leg only frames are adjusted to not count the upper body only frames
1002 if ( i == LEGS_WALKCR ) {
1003 skip = animations[LEGS_WALKCR].firstFrame - animations[TORSO_GESTURE].firstFrame;
1004 }
1005 if ( i >= LEGS_WALKCR ) {
1006 animations[i].firstFrame -= skip;
1007 }
1008
1009 token = COM_Parse( &text_p );
1010 if ( !token ) {
1011 break;
1012 }
1013 animations[i].numFrames = atoi( token );
1014
1015 token = COM_Parse( &text_p );
1016 if ( !token ) {
1017 break;
1018 }
1019 animations[i].loopFrames = atoi( token );
1020
1021 token = COM_Parse( &text_p );
1022 if ( !token ) {
1023 break;
1024 }
1025 fps = atof( token );
1026 if ( fps == 0 ) {
1027 fps = 1;
1028 }
1029 animations[i].frameLerp = 1000 / fps;
1030 animations[i].initialLerp = 1000 / fps;
1031 }
1032
1033 if ( i != MAX_ANIMATIONS ) {
1034 Com_Printf( "Error parsing animation file: %s\n", filename );
1035 return qfalse;
1036 }
1037
1038 return qtrue;
1039 }
1040
1041
1042 /*
1043 ==========================
1044 UI_RegisterClientModelname
1045 ==========================
1046 */
1047 qboolean UI_RegisterClientModelname( playerInfo_t *pi, const char *modelSkinName ) {
1048 char modelName[MAX_QPATH];
1049 char skinName[MAX_QPATH];
1050 char filename[MAX_QPATH];
1051 char *slash;
1052
1053 pi->torsoModel = 0;
1054 pi->headModel = 0;
1055
1056 if ( !modelSkinName[0] ) {
1057 return qfalse;
1058 }
1059
1060 Q_strncpyz( modelName, modelSkinName, sizeof( modelName ) );
1061
1062 slash = strchr( modelName, '/' );
1063 if ( !slash ) {
1064 // modelName did not include a skin name
1065 Q_strncpyz( skinName, "default", sizeof( skinName ) );
1066 } else {
1067 Q_strncpyz( skinName, slash + 1, sizeof( skinName ) );
1068 // truncate modelName
1069 *slash = 0;
1070 }
1071
1072 // load cmodels before models so filecache works
1073
1074 Com_sprintf( filename, sizeof( filename ), "models/players/%s/lower.md3", modelName );
1075 pi->legsModel = trap_R_RegisterModel( filename );
1076 if ( !pi->legsModel ) {
1077 Com_Printf( "Failed to load model file %s\n", filename );
1078 return qfalse;
1079 }
1080
1081 Com_sprintf( filename, sizeof( filename ), "models/players/%s/upper.md3", modelName );
1082 pi->torsoModel = trap_R_RegisterModel( filename );
1083 if ( !pi->torsoModel ) {
1084 Com_Printf( "Failed to load model file %s\n", filename );
1085 return qfalse;
1086 }
1087
1088 Com_sprintf( filename, sizeof( filename ), "models/players/%s/head.md3", modelName );
1089 pi->headModel = trap_R_RegisterModel( filename );
1090 if ( !pi->headModel ) {
1091 Com_Printf( "Failed to load model file %s\n", filename );
1092 return qfalse;
1093 }
1094
1095 // if any skins failed to load, fall back to default
1096 if ( !UI_RegisterClientSkin( pi, modelName, skinName ) ) {
1097 if ( !UI_RegisterClientSkin( pi, modelName, "default" ) ) {
1098 Com_Printf( "Failed to load skin file: %s : %s\n", modelName, skinName );
1099 return qfalse;
1100 }
1101 }
1102
1103 // load the animations
1104 Com_sprintf( filename, sizeof( filename ), "models/players/%s/animation.cfg", modelName );
1105 if ( !UI_ParseAnimationFile( filename, pi->animations ) ) {
1106 Com_Printf( "Failed to load animation file %s\n", filename );
1107 return qfalse;
1108 }
1109
1110 return qtrue;
1111 }
1112
1113
1114 /*
1115 ===============
1116 UI_PlayerInfo_SetModel
1117 ===============
1118 */
1119 void UI_PlayerInfo_SetModel( playerInfo_t *pi, const char *model ) {
1120 memset( pi, 0, sizeof(*pi) );
1121 UI_RegisterClientModelname( pi, model );
1122 pi->weapon = WP_MACHINEGUN;
1123 pi->currentWeapon = pi->weapon;
1124 pi->lastWeapon = pi->weapon;
1125 pi->pendingWeapon = -1;
1126 pi->weaponTimer = 0;
1127 pi->chat = qfalse;
1128 pi->newModel = qtrue;
1129 UI_PlayerInfo_SetWeapon( pi, pi->weapon );
1130 }
1131
1132
1133 /*
1134 ===============
1135 UI_PlayerInfo_SetInfo
1136 ===============
1137 */
1138 void UI_PlayerInfo_SetInfo( playerInfo_t *pi, int legsAnim, int torsoAnim, vec3_t viewAngles, vec3_t moveAngles, weapon_t weaponNumber, qboolean chat ) {
1139 int currentAnim;
1140 weapon_t weaponNum;
1141
1142 pi->chat = chat;
1143
1144 // view angles
1145 VectorCopy( viewAngles, pi->viewAngles );
1146
1147 // move angles
1148 VectorCopy( moveAngles, pi->moveAngles );
1149
1150 if ( pi->newModel ) {
1151 pi->newModel = qfalse;
1152
1153 jumpHeight = 0;
1154 pi->pendingLegsAnim = 0;
1155 UI_ForceLegsAnim( pi, legsAnim );
1156 pi->legs.yawAngle = viewAngles[YAW];
1157 pi->legs.yawing = qfalse;
1158
1159 pi->pendingTorsoAnim = 0;
1160 UI_ForceTorsoAnim( pi, torsoAnim );
1161 pi->torso.yawAngle = viewAngles[YAW];
1162 pi->torso.yawing = qfalse;
1163
1164 if ( weaponNumber != -1 ) {
1165 pi->weapon = weaponNumber;
1166 pi->currentWeapon = weaponNumber;
1167 pi->lastWeapon = weaponNumber;
1168 pi->pendingWeapon = -1;
1169 pi->weaponTimer = 0;
1170 UI_PlayerInfo_SetWeapon( pi, pi->weapon );
1171 }
1172
1173 return;
1174 }
1175
1176 // weapon
1177 if ( weaponNumber == -1 ) {
1178 pi->pendingWeapon = -1;
1179 pi->weaponTimer = 0;
1180 }
1181 else if ( weaponNumber != WP_NONE ) {
1182 pi->pendingWeapon = weaponNumber;
1183 pi->weaponTimer = dp_realtime + UI_TIMER_WEAPON_DELAY;
1184 }
1185 weaponNum = pi->lastWeapon;
1186 pi->weapon = weaponNum;
1187
1188 if ( torsoAnim == BOTH_DEATH1 || legsAnim == BOTH_DEATH1 ) {
1189 torsoAnim = legsAnim = BOTH_DEATH1;
1190 pi->weapon = pi->currentWeapon = WP_NONE;
1191 UI_PlayerInfo_SetWeapon( pi, pi->weapon );
1192
1193 jumpHeight = 0;
1194 pi->pendingLegsAnim = 0;
1195 UI_ForceLegsAnim( pi, legsAnim );
1196
1197 pi->pendingTorsoAnim = 0;
1198 UI_ForceTorsoAnim( pi, torsoAnim );
1199
1200 return;
1201 }
1202
1203 // leg animation
1204 currentAnim = pi->legsAnim & ~ANIM_TOGGLEBIT;
1205 if ( legsAnim != LEGS_JUMP && ( currentAnim == LEGS_JUMP || currentAnim == LEGS_LAND ) ) {
1206 pi->pendingLegsAnim = legsAnim;
1207 }
1208 else if ( legsAnim != currentAnim ) {
1209 jumpHeight = 0;
1210 pi->pendingLegsAnim = 0;
1211 UI_ForceLegsAnim( pi, legsAnim );
1212 }
1213
1214 // torso animation
1215 if ( torsoAnim == TORSO_STAND || torsoAnim == TORSO_STAND2 ) {
1216 if ( weaponNum == WP_NONE || weaponNum == WP_GAUNTLET ) {
1217 torsoAnim = TORSO_STAND2;
1218 }
1219 else {
1220 torsoAnim = TORSO_STAND;
1221 }
1222 }
1223
1224 if ( torsoAnim == TORSO_ATTACK || torsoAnim == TORSO_ATTACK2 ) {
1225 if ( weaponNum == WP_NONE || weaponNum == WP_GAUNTLET ) {
1226 torsoAnim = TORSO_ATTACK2;
1227 }
1228 else {
1229 torsoAnim = TORSO_ATTACK;
1230 }
1231 pi->muzzleFlashTime = dp_realtime + UI_TIMER_MUZZLE_FLASH;
1232 //FIXME play firing sound here
1233 }
1234
1235 currentAnim = pi->torsoAnim & ~ANIM_TOGGLEBIT;
1236
1237 if ( weaponNum != pi->currentWeapon || currentAnim == TORSO_RAISE || currentAnim == TORSO_DROP ) {
1238 pi->pendingTorsoAnim = torsoAnim;
1239 }
1240 else if ( ( currentAnim == TORSO_GESTURE || currentAnim == TORSO_ATTACK ) && ( torsoAnim != currentAnim ) ) {
1241 pi->pendingTorsoAnim = torsoAnim;
1242 }
1243 else if ( torsoAnim != currentAnim ) {
1244 pi->pendingTorsoAnim = 0;
1245 UI_ForceTorsoAnim( pi, torsoAnim );
1246 }
1247 }