Debian Codegraph @

code/cgame/cg_playerstate.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 // cg_playerstate.c -- this file acts on changes in a new playerState_t
24 // With normal play, this will be done after local prediction, but when
25 // following another player or playing back a demo, it will be checked
26 // when the snapshot transitions like all the other entities
27
28 #include "cg_local.h"
29
30 /*
31 ==============
32 CG_CheckAmmo
33
34 If the ammo has gone low enough to generate the warning, play a sound
35 ==============
36 */
37 void CG_CheckAmmo( void ) {
38 int i;
39 int total;
40 int previous;
41 int weapons;
42
43 // see about how many seconds of ammo we have remaining
44 weapons = cg.snap->ps.stats[ STAT_WEAPONS ];
45 total = 0;
46 for ( i = WP_MACHINEGUN ; i < WP_NUM_WEAPONS ; i++ ) {
47 if ( ! ( weapons & ( 1 << i ) ) ) {
48 continue;
49 }
50 if ( cg.snap->ps.ammo[i] < 0 ) {
51 continue;
52 }
53 switch ( i ) {
54 case WP_ROCKET_LAUNCHER:
55 case WP_GRENADE_LAUNCHER:
56 case WP_RAILGUN:
57 case WP_SHOTGUN:
58 #ifdef MISSIONPACK
59 case WP_PROX_LAUNCHER:
60 #endif
61 total += cg.snap->ps.ammo[i] * 1000;
62 break;
63 default:
64 total += cg.snap->ps.ammo[i] * 200;
65 break;
66 }
67 if ( total >= 5000 ) {
68 cg.lowAmmoWarning = 0;
69 return;
70 }
71 }
72
73 previous = cg.lowAmmoWarning;
74
75 if ( total == 0 ) {
76 cg.lowAmmoWarning = 2;
77 } else {
78 cg.lowAmmoWarning = 1;
79 }
80
81 // play a sound on transitions
82 if ( cg.lowAmmoWarning != previous ) {
83 trap_S_StartLocalSound( cgs.media.noAmmoSound, CHAN_LOCAL_SOUND );
84 }
85 }
86
87 /*
88 ==============
89 CG_DamageFeedback
90 ==============
91 */
92 void CG_DamageFeedback( int yawByte, int pitchByte, int damage ) {
93 float left, front, up;
94 float kick;
95 int health;
96 float scale;
97 vec3_t dir;
98 vec3_t angles;
99 float dist;
100 float yaw, pitch;
101
102 // show the attacking player's head and name in corner
103 cg.attackerTime = cg.time;
104
105 // the lower on health you are, the greater the view kick will be
106 health = cg.snap->ps.stats[STAT_HEALTH];
107 if ( health < 40 ) {
108 scale = 1;
109 } else {
110 scale = 40.0 / health;
111 }
112 kick = damage * scale;
113
114 if (kick < 5)
115 kick = 5;
116 if (kick > 10)
117 kick = 10;
118
119 // if yaw and pitch are both 255, make the damage always centered (falling, etc)
120 if ( yawByte == 255 && pitchByte == 255 ) {
121 cg.damageX = 0;
122 cg.damageY = 0;
123 cg.v_dmg_roll = 0;
124 cg.v_dmg_pitch = -kick;
125 } else {
126 // positional
127 pitch = pitchByte / 255.0 * 360;
128 yaw = yawByte / 255.0 * 360;
129
130 angles[PITCH] = pitch;
131 angles[YAW] = yaw;
132 angles[ROLL] = 0;
133
134 AngleVectors( angles, dir, NULL, NULL );
135 VectorSubtract( vec3_origin, dir, dir );
136
137 front = DotProduct (dir, cg.refdef.viewaxis[0] );
138 left = DotProduct (dir, cg.refdef.viewaxis[1] );
139 up = DotProduct (dir, cg.refdef.viewaxis[2] );
140
141 dir[0] = front;
142 dir[1] = left;
143 dir[2] = 0;
144 dist = VectorLength( dir );
145 if ( dist < 0.1 ) {
146 dist = 0.1f;
147 }
148
149 cg.v_dmg_roll = kick * left;
150
151 cg.v_dmg_pitch = -kick * front;
152
153 if ( front <= 0.1 ) {
154 front = 0.1f;
155 }
156 cg.damageX = -left / front;
157 cg.damageY = up / dist;
158 }
159
160 // clamp the position
161 if ( cg.damageX > 1.0 ) {
162 cg.damageX = 1.0;
163 }
164 if ( cg.damageX < - 1.0 ) {
165 cg.damageX = -1.0;
166 }
167
168 if ( cg.damageY > 1.0 ) {
169 cg.damageY = 1.0;
170 }
171 if ( cg.damageY < - 1.0 ) {
172 cg.damageY = -1.0;
173 }
174
175 // don't let the screen flashes vary as much
176 if ( kick > 10 ) {
177 kick = 10;
178 }
179 cg.damageValue = kick;
180 cg.v_dmg_time = cg.time + DAMAGE_TIME;
181 cg.damageTime = cg.snap->serverTime;
182 }
183
184
185
186
187 /*
188 ================
189 CG_Respawn
190
191 A respawn happened this snapshot
192 ================
193 */
194 void CG_Respawn( void ) {
195 // no error decay on player movement
196 cg.thisFrameTeleport = qtrue;
197
198 // display weapons available
199 cg.weaponSelectTime = cg.time;
200
201 // select the weapon the server says we are using
202 cg.weaponSelect = cg.snap->ps.weapon;
203 }
204
205 extern char *eventnames[];
206
207 /*
208 ==============
209 CG_CheckPlayerstateEvents
210 ==============
211 */
212 void CG_CheckPlayerstateEvents( playerState_t *ps, playerState_t *ops ) {
213 int i;
214 int event;
215 centity_t *cent;
216
217 if ( ps->externalEvent && ps->externalEvent != ops->externalEvent ) {
218 cent = &cg_entities[ ps->clientNum ];
219 cent->currentState.event = ps->externalEvent;
220 cent->currentState.eventParm = ps->externalEventParm;
221 CG_EntityEvent( cent, cent->lerpOrigin );
222 }
223
224 cent = &cg.predictedPlayerEntity; // cg_entities[ ps->clientNum ];
225 // go through the predictable events buffer
226 for ( i = ps->eventSequence - MAX_PS_EVENTS ; i < ps->eventSequence ; i++ ) {
227 // if we have a new predictable event
228 if ( i >= ops->eventSequence
229 // or the server told us to play another event instead of a predicted event we already issued
230 // or something the server told us changed our prediction causing a different event
231 || (i > ops->eventSequence - MAX_PS_EVENTS && ps->events[i & (MAX_PS_EVENTS-1)] != ops->events[i & (MAX_PS_EVENTS-1)]) ) {
232
233 event = ps->events[ i & (MAX_PS_EVENTS-1) ];
234 cent->currentState.event = event;
235 cent->currentState.eventParm = ps->eventParms[ i & (MAX_PS_EVENTS-1) ];
236 CG_EntityEvent( cent, cent->lerpOrigin );
237
238 cg.predictableEvents[ i & (MAX_PREDICTED_EVENTS-1) ] = event;
239
240 cg.eventSequence++;
241 }
242 }
243 }
244
245 /*
246 ==================
247 CG_CheckChangedPredictableEvents
248 ==================
249 */
250 void CG_CheckChangedPredictableEvents( playerState_t *ps ) {
251 int i;
252 int event;
253 centity_t *cent;
254
255 cent = &cg.predictedPlayerEntity;
256 for ( i = ps->eventSequence - MAX_PS_EVENTS ; i < ps->eventSequence ; i++ ) {
257 //
258 if (i >= cg.eventSequence) {
259 continue;
260 }
261 // if this event is not further back in than the maximum predictable events we remember
262 if (i > cg.eventSequence - MAX_PREDICTED_EVENTS) {
263 // if the new playerstate event is different from a previously predicted one
264 if ( ps->events[i & (MAX_PS_EVENTS-1)] != cg.predictableEvents[i & (MAX_PREDICTED_EVENTS-1) ] ) {
265
266 event = ps->events[ i & (MAX_PS_EVENTS-1) ];
267 cent->currentState.event = event;
268 cent->currentState.eventParm = ps->eventParms[ i & (MAX_PS_EVENTS-1) ];
269 CG_EntityEvent( cent, cent->lerpOrigin );
270
271 cg.predictableEvents[ i & (MAX_PREDICTED_EVENTS-1) ] = event;
272
273 if ( cg_showmiss.integer ) {
274 CG_Printf("WARNING: changed predicted event\n");
275 }
276 }
277 }
278 }
279 }
280
281 /*
282 ==================
283 pushReward
284 ==================
285 */
286 static void pushReward(sfxHandle_t sfx, qhandle_t shader, int rewardCount) {
287 if (cg.rewardStack < (MAX_REWARDSTACK-1)) {
288 cg.rewardStack++;
289 cg.rewardSound[cg.rewardStack] = sfx;
290 cg.rewardShader[cg.rewardStack] = shader;
291 cg.rewardCount[cg.rewardStack] = rewardCount;
292 }
293 }
294
295 /*
296 ==================
297 CG_CheckLocalSounds
298 ==================
299 */
300 void CG_CheckLocalSounds( playerState_t *ps, playerState_t *ops ) {
301 int highScore, reward;
302 #ifdef MISSIONPACK
303 int health, armor;
304 #endif
305 sfxHandle_t sfx;
306
307 // don't play the sounds if the player just changed teams
308 if ( ps->persistant[PERS_TEAM] != ops->persistant[PERS_TEAM] ) {
309 return;
310 }
311
312 // hit changes
313 if ( ps->persistant[PERS_HITS] > ops->persistant[PERS_HITS] ) {
314 #ifdef MISSIONPACK
315 armor = ps->persistant[PERS_ATTACKEE_ARMOR] & 0xff;
316 health = ps->persistant[PERS_ATTACKEE_ARMOR] >> 8;
317 if (armor > 50 ) {
318 trap_S_StartLocalSound( cgs.media.hitSoundHighArmor, CHAN_LOCAL_SOUND );
319 } else if (armor || health > 100) {
320 trap_S_StartLocalSound( cgs.media.hitSoundLowArmor, CHAN_LOCAL_SOUND );
321 } else {
322 trap_S_StartLocalSound( cgs.media.hitSound, CHAN_LOCAL_SOUND );
323 }
324 #else
325 trap_S_StartLocalSound( cgs.media.hitSound, CHAN_LOCAL_SOUND );
326 #endif
327 } else if ( ps->persistant[PERS_HITS] < ops->persistant[PERS_HITS] ) {
328 trap_S_StartLocalSound( cgs.media.hitTeamSound, CHAN_LOCAL_SOUND );
329 }
330
331 // health changes of more than -1 should make pain sounds
332 if ( ps->stats[STAT_HEALTH] < ops->stats[STAT_HEALTH] - 1 ) {
333 if ( ps->stats[STAT_HEALTH] > 0 ) {
334 CG_PainEvent( &cg.predictedPlayerEntity, ps->stats[STAT_HEALTH] );
335 }
336 }
337
338
339 // if we are going into the intermission, don't start any voices
340 if ( cg.intermissionStarted ) {
341 return;
342 }
343
344 // reward sounds
345 reward = qfalse;
346 if (ps->persistant[PERS_CAPTURES] != ops->persistant[PERS_CAPTURES]) {
347 pushReward(cgs.media.captureAwardSound, cgs.media.medalCapture, ps->persistant[PERS_CAPTURES]);
348 reward = qtrue;
349 //Com_Printf("capture\n");
350 }
351 if (ps->persistant[PERS_IMPRESSIVE_COUNT] != ops->persistant[PERS_IMPRESSIVE_COUNT]) {
352 #ifdef MISSIONPACK
353 if (ps->persistant[PERS_IMPRESSIVE_COUNT] == 1) {
354 sfx = cgs.media.firstImpressiveSound;
355 } else {
356 sfx = cgs.media.impressiveSound;
357 }
358 #else
359 sfx = cgs.media.impressiveSound;
360 #endif
361 pushReward(sfx, cgs.media.medalImpressive, ps->persistant[PERS_IMPRESSIVE_COUNT]);
362 reward = qtrue;
363 //Com_Printf("impressive\n");
364 }
365 if (ps->persistant[PERS_EXCELLENT_COUNT] != ops->persistant[PERS_EXCELLENT_COUNT]) {
366 #ifdef MISSIONPACK
367 if (ps->persistant[PERS_EXCELLENT_COUNT] == 1) {
368 sfx = cgs.media.firstExcellentSound;
369 } else {
370 sfx = cgs.media.excellentSound;
371 }
372 #else
373 sfx = cgs.media.excellentSound;
374 #endif
375 pushReward(sfx, cgs.media.medalExcellent, ps->persistant[PERS_EXCELLENT_COUNT]);
376 reward = qtrue;
377 //Com_Printf("excellent\n");
378 }
379 if (ps->persistant[PERS_GAUNTLET_FRAG_COUNT] != ops->persistant[PERS_GAUNTLET_FRAG_COUNT]) {
380 #ifdef MISSIONPACK
381 if (ps->persistant[PERS_GAUNTLET_FRAG_COUNT] == 1) {
382 sfx = cgs.media.firstHumiliationSound;
383 } else {
384 sfx = cgs.media.humiliationSound;
385 }
386 #else
387 sfx = cgs.media.humiliationSound;
388 #endif
389 pushReward(sfx, cgs.media.medalGauntlet, ps->persistant[PERS_GAUNTLET_FRAG_COUNT]);
390 reward = qtrue;
391 //Com_Printf("gauntlet frag\n");
392 }
393 if (ps->persistant[PERS_DEFEND_COUNT] != ops->persistant[PERS_DEFEND_COUNT]) {
394 pushReward(cgs.media.defendSound, cgs.media.medalDefend, ps->persistant[PERS_DEFEND_COUNT]);
395 reward = qtrue;
396 //Com_Printf("defend\n");
397 }
398 if (ps->persistant[PERS_ASSIST_COUNT] != ops->persistant[PERS_ASSIST_COUNT]) {
399 pushReward(cgs.media.assistSound, cgs.media.medalAssist, ps->persistant[PERS_ASSIST_COUNT]);
400 reward = qtrue;
401 //Com_Printf("assist\n");
402 }
403 // if any of the player event bits changed
404 if (ps->persistant[PERS_PLAYEREVENTS] != ops->persistant[PERS_PLAYEREVENTS]) {
405 if ((ps->persistant[PERS_PLAYEREVENTS] & PLAYEREVENT_DENIEDREWARD) !=
406 (ops->persistant[PERS_PLAYEREVENTS] & PLAYEREVENT_DENIEDREWARD)) {
407 trap_S_StartLocalSound( cgs.media.deniedSound, CHAN_ANNOUNCER );
408 }
409 else if ((ps->persistant[PERS_PLAYEREVENTS] & PLAYEREVENT_GAUNTLETREWARD) !=
410 (ops->persistant[PERS_PLAYEREVENTS] & PLAYEREVENT_GAUNTLETREWARD)) {
411 trap_S_StartLocalSound( cgs.media.humiliationSound, CHAN_ANNOUNCER );
412 }
413 else if ((ps->persistant[PERS_PLAYEREVENTS] & PLAYEREVENT_HOLYSHIT) !=
414 (ops->persistant[PERS_PLAYEREVENTS] & PLAYEREVENT_HOLYSHIT)) {
415 trap_S_StartLocalSound( cgs.media.holyShitSound, CHAN_ANNOUNCER );
416 }
417 reward = qtrue;
418 }
419
420 // check for flag pickup
421 if ( cgs.gametype > GT_TEAM ) {
422 if ((ps->powerups[PW_REDFLAG] != ops->powerups[PW_REDFLAG] && ps->powerups[PW_REDFLAG]) ||
423 (ps->powerups[PW_BLUEFLAG] != ops->powerups[PW_BLUEFLAG] && ps->powerups[PW_BLUEFLAG]) ||
424 (ps->powerups[PW_NEUTRALFLAG] != ops->powerups[PW_NEUTRALFLAG] && ps->powerups[PW_NEUTRALFLAG]) )
425 {
426 trap_S_StartLocalSound( cgs.media.youHaveFlagSound, CHAN_ANNOUNCER );
427 }
428 }
429
430 // lead changes
431 if (!reward) {
432 //
433 if ( !cg.warmup ) {
434 // never play lead changes during warmup
435 if ( ps->persistant[PERS_RANK] != ops->persistant[PERS_RANK] ) {
436 if ( cgs.gametype < GT_TEAM) {
437 if ( ps->persistant[PERS_RANK] == 0 ) {
438 CG_AddBufferedSound(cgs.media.takenLeadSound);
439 } else if ( ps->persistant[PERS_RANK] == RANK_TIED_FLAG ) {
440 CG_AddBufferedSound(cgs.media.tiedLeadSound);
441 } else if ( ( ops->persistant[PERS_RANK] & ~RANK_TIED_FLAG ) == 0 ) {
442 CG_AddBufferedSound(cgs.media.lostLeadSound);
443 }
444 }
445 }
446 }
447 }
448
449 // timelimit warnings
450 if ( cgs.timelimit > 0 ) {
451 int msec;
452
453 msec = cg.time - cgs.levelStartTime;
454 if ( !( cg.timelimitWarnings & 4 ) && msec > ( cgs.timelimit * 60 + 2 ) * 1000 ) {
455 cg.timelimitWarnings |= 1 | 2 | 4;
456 trap_S_StartLocalSound( cgs.media.suddenDeathSound, CHAN_ANNOUNCER );
457 }
458 else if ( !( cg.timelimitWarnings & 2 ) && msec > (cgs.timelimit - 1) * 60 * 1000 ) {
459 cg.timelimitWarnings |= 1 | 2;
460 trap_S_StartLocalSound( cgs.media.oneMinuteSound, CHAN_ANNOUNCER );
461 }
462 else if ( cgs.timelimit > 5 && !( cg.timelimitWarnings & 1 ) && msec > (cgs.timelimit - 5) * 60 * 1000 ) {
463 cg.timelimitWarnings |= 1;
464 trap_S_StartLocalSound( cgs.media.fiveMinuteSound, CHAN_ANNOUNCER );
465 }
466 }
467
468 // fraglimit warnings
469 if ( cgs.fraglimit > 0 && cgs.gametype < GT_CTF) {
470 highScore = cgs.scores1;
471
472 if (cgs.gametype == GT_TEAM && cgs.scores2 > highScore) {
473 highScore = cgs.scores2;
474 }
475
476 if ( !( cg.fraglimitWarnings & 4 ) && highScore == (cgs.fraglimit - 1) ) {
477 cg.fraglimitWarnings |= 1 | 2 | 4;
478 CG_AddBufferedSound(cgs.media.oneFragSound);
479 }
480 else if ( cgs.fraglimit > 2 && !( cg.fraglimitWarnings & 2 ) && highScore == (cgs.fraglimit - 2) ) {
481 cg.fraglimitWarnings |= 1 | 2;
482 CG_AddBufferedSound(cgs.media.twoFragSound);
483 }
484 else if ( cgs.fraglimit > 3 && !( cg.fraglimitWarnings & 1 ) && highScore == (cgs.fraglimit - 3) ) {
485 cg.fraglimitWarnings |= 1;
486 CG_AddBufferedSound(cgs.media.threeFragSound);
487 }
488 }
489 }
490
491 /*
492 ===============
493 CG_TransitionPlayerState
494
495 ===============
496 */
497 void CG_TransitionPlayerState( playerState_t *ps, playerState_t *ops ) {
498 // check for changing follow mode
499 if ( ps->clientNum != ops->clientNum ) {
500 cg.thisFrameTeleport = qtrue;
501 // make sure we don't get any unwanted transition effects
502 *ops = *ps;
503 }
504
505 // damage events (player is getting wounded)
506 if ( ps->damageEvent != ops->damageEvent && ps->damageCount ) {
507 CG_DamageFeedback( ps->damageYaw, ps->damagePitch, ps->damageCount );
508 }
509
510 // respawning
511 if ( ps->persistant[PERS_SPAWN_COUNT] != ops->persistant[PERS_SPAWN_COUNT] ) {
512 CG_Respawn();
513 }
514
515 if ( cg.mapRestart ) {
516 CG_Respawn();
517 cg.mapRestart = qfalse;
518 }
519
520 if ( cg.snap->ps.pm_type != PM_INTERMISSION
521 && ps->persistant[PERS_TEAM] != TEAM_SPECTATOR ) {
522 CG_CheckLocalSounds( ps, ops );
523 }
524
525 // check for going low on ammo
526 CG_CheckAmmo();
527
528 // run events
529 CG_CheckPlayerstateEvents( ps, ops );
530
531 // smooth the ducking viewheight change
532 if ( ps->viewheight != ops->viewheight ) {
533 cg.duckChange = ps->viewheight - ops->viewheight;
534 cg.duckTime = cg.time;
535 }
536 }
537