Debian Codegraph @

code/game/bg_pmove.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 // bg_pmove.c -- both games player movement code
24 // takes a playerstate and a usercmd as input and returns a modifed playerstate
25
26 #include "../qcommon/q_shared.h"
27 #include "bg_public.h"
28 #include "bg_local.h"
29
30 pmove_t *pm;
31 pml_t pml;
32
33 // movement parameters
34 float pm_stopspeed = 100.0f;
35 float pm_duckScale = 0.25f;
36 float pm_swimScale = 0.50f;
37
38 float pm_accelerate = 10.0f;
39 float pm_airaccelerate = 1.0f;
40 float pm_wateraccelerate = 4.0f;
41 float pm_flyaccelerate = 8.0f;
42
43 float pm_friction = 6.0f;
44 float pm_waterfriction = 1.0f;
45 float pm_flightfriction = 3.0f;
46 float pm_spectatorfriction = 5.0f;
47
48 int c_pmove = 0;
49
50
51 /*
52 ===============
53 PM_AddEvent
54
55 ===============
56 */
57 void PM_AddEvent( int newEvent ) {
58 BG_AddPredictableEventToPlayerstate( newEvent, 0, pm->ps );
59 }
60
61 /*
62 ===============
63 PM_AddTouchEnt
64 ===============
65 */
66 void PM_AddTouchEnt( int entityNum ) {
67 int i;
68
69 if ( entityNum == ENTITYNUM_WORLD ) {
70 return;
71 }
72 if ( pm->numtouch == MAXTOUCH ) {
73 return;
74 }
75
76 // see if it is already added
77 for ( i = 0 ; i < pm->numtouch ; i++ ) {
78 if ( pm->touchents[ i ] == entityNum ) {
79 return;
80 }
81 }
82
83 // add it
84 pm->touchents[pm->numtouch] = entityNum;
85 pm->numtouch++;
86 }
87
88 /*
89 ===================
90 PM_StartTorsoAnim
91 ===================
92 */
93 static void PM_StartTorsoAnim( int anim ) {
94 if ( pm->ps->pm_type >= PM_DEAD ) {
95 return;
96 }
97 pm->ps->torsoAnim = ( ( pm->ps->torsoAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT )
98 | anim;
99 }
100 static void PM_StartLegsAnim( int anim ) {
101 if ( pm->ps->pm_type >= PM_DEAD ) {
102 return;
103 }
104 if ( pm->ps->legsTimer > 0 ) {
105 return; // a high priority animation is running
106 }
107 pm->ps->legsAnim = ( ( pm->ps->legsAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT )
108 | anim;
109 }
110
111 static void PM_ContinueLegsAnim( int anim ) {
112 if ( ( pm->ps->legsAnim & ~ANIM_TOGGLEBIT ) == anim ) {
113 return;
114 }
115 if ( pm->ps->legsTimer > 0 ) {
116 return; // a high priority animation is running
117 }
118 PM_StartLegsAnim( anim );
119 }
120
121 static void PM_ContinueTorsoAnim( int anim ) {
122 if ( ( pm->ps->torsoAnim & ~ANIM_TOGGLEBIT ) == anim ) {
123 return;
124 }
125 if ( pm->ps->torsoTimer > 0 ) {
126 return; // a high priority animation is running
127 }
128 PM_StartTorsoAnim( anim );
129 }
130
131 static void PM_ForceLegsAnim( int anim ) {
132 pm->ps->legsTimer = 0;
133 PM_StartLegsAnim( anim );
134 }
135
136
137 /*
138 ==================
139 PM_ClipVelocity
140
141 Slide off of the impacting surface
142 ==================
143 */
144 void PM_ClipVelocity( vec3_t in, vec3_t normal, vec3_t out, float overbounce ) {
145 float backoff;
146 float change;
147 int i;
148
149 backoff = DotProduct (in, normal);
150
151 if ( backoff < 0 ) {
152 backoff *= overbounce;
153 } else {
154 backoff /= overbounce;
155 }
156
157 for ( i=0 ; i<3 ; i++ ) {
158 change = normal[i]*backoff;
159 out[i] = in[i] - change;
160 }
161 }
162
163
164 /*
165 ==================
166 PM_Friction
167
168 Handles both ground friction and water friction
169 ==================
170 */
171 static void PM_Friction( void ) {
172 vec3_t vec;
173 float *vel;
174 float speed, newspeed, control;
175 float drop;
176
177 vel = pm->ps->velocity;
178
179 VectorCopy( vel, vec );
180 if ( pml.walking ) {
181 vec[2] = 0; // ignore slope movement
182 }
183
184 speed = VectorLength(vec);
185 if (speed < 1) {
186 vel[0] = 0;
187 vel[1] = 0; // allow sinking underwater
188 // FIXME: still have z friction underwater?
189 return;
190 }
191
192 drop = 0;
193
194 // apply ground friction
195 if ( pm->waterlevel <= 1 ) {
196 if ( pml.walking && !(pml.groundTrace.surfaceFlags & SURF_SLICK) ) {
197 // if getting knocked back, no friction
198 if ( ! (pm->ps->pm_flags & PMF_TIME_KNOCKBACK) ) {
199 control = speed < pm_stopspeed ? pm_stopspeed : speed;
200 drop += control*pm_friction*pml.frametime;
201 }
202 }
203 }
204
205 // apply water friction even if just wading
206 if ( pm->waterlevel ) {
207 drop += speed*pm_waterfriction*pm->waterlevel*pml.frametime;
208 }
209
210 // apply flying friction
211 if ( pm->ps->powerups[PW_FLIGHT]) {
212 drop += speed*pm_flightfriction*pml.frametime;
213 }
214
215 if ( pm->ps->pm_type == PM_SPECTATOR) {
216 drop += speed*pm_spectatorfriction*pml.frametime;
217 }
218
219 // scale the velocity
220 newspeed = speed - drop;
221 if (newspeed < 0) {
222 newspeed = 0;
223 }
224 newspeed /= speed;
225
226 vel[0] = vel[0] * newspeed;
227 vel[1] = vel[1] * newspeed;
228 vel[2] = vel[2] * newspeed;
229 }
230
231
232 /*
233 ==============
234 PM_Accelerate
235
236 Handles user intended acceleration
237 ==============
238 */
239 static void PM_Accelerate( vec3_t wishdir, float wishspeed, float accel ) {
240 #if 1
241 // q2 style
242 int i;
243 float addspeed, accelspeed, currentspeed;
244
245 currentspeed = DotProduct (pm->ps->velocity, wishdir);
246 addspeed = wishspeed - currentspeed;
247 if (addspeed <= 0) {
248 return;
249 }
250 accelspeed = accel*pml.frametime*wishspeed;
251 if (accelspeed > addspeed) {
252 accelspeed = addspeed;
253 }
254
255 for (i=0 ; i<3 ; i++) {
256 pm->ps->velocity[i] += accelspeed*wishdir[i];
257 }
258 #else
259 // proper way (avoids strafe jump maxspeed bug), but feels bad
260 vec3_t wishVelocity;
261 vec3_t pushDir;
262 float pushLen;
263 float canPush;
264
265 VectorScale( wishdir, wishspeed, wishVelocity );
266 VectorSubtract( wishVelocity, pm->ps->velocity, pushDir );
267 pushLen = VectorNormalize( pushDir );
268
269 canPush = accel*pml.frametime*wishspeed;
270 if (canPush > pushLen) {
271 canPush = pushLen;
272 }
273
274 VectorMA( pm->ps->velocity, canPush, pushDir, pm->ps->velocity );
275 #endif
276 }
277
278
279
280 /*
281 ============
282 PM_CmdScale
283
284 Returns the scale factor to apply to cmd movements
285 This allows the clients to use axial -127 to 127 values for all directions
286 without getting a sqrt(2) distortion in speed.
287 ============
288 */
289 static float PM_CmdScale( usercmd_t *cmd ) {
290 int max;
291 float total;
292 float scale;
293
294 max = abs( cmd->forwardmove );
295 if ( abs( cmd->rightmove ) > max ) {
296 max = abs( cmd->rightmove );
297 }
298 if ( abs( cmd->upmove ) > max ) {
299 max = abs( cmd->upmove );
300 }
301 if ( !max ) {
302 return 0;
303 }
304
305 total = sqrt( cmd->forwardmove * cmd->forwardmove
306 + cmd->rightmove * cmd->rightmove + cmd->upmove * cmd->upmove );
307 scale = (float)pm->ps->speed * max / ( 127.0 * total );
308
309 return scale;
310 }
311
312
313 /*
314 ================
315 PM_SetMovementDir
316
317 Determine the rotation of the legs relative
318 to the facing dir
319 ================
320 */
321 static void PM_SetMovementDir( void ) {
322 if ( pm->cmd.forwardmove || pm->cmd.rightmove ) {
323 if ( pm->cmd.rightmove == 0 && pm->cmd.forwardmove > 0 ) {
324 pm->ps->movementDir = 0;
325 } else if ( pm->cmd.rightmove < 0 && pm->cmd.forwardmove > 0 ) {
326 pm->ps->movementDir = 1;
327 } else if ( pm->cmd.rightmove < 0 && pm->cmd.forwardmove == 0 ) {
328 pm->ps->movementDir = 2;
329 } else if ( pm->cmd.rightmove < 0 && pm->cmd.forwardmove < 0 ) {
330 pm->ps->movementDir = 3;
331 } else if ( pm->cmd.rightmove == 0 && pm->cmd.forwardmove < 0 ) {
332 pm->ps->movementDir = 4;
333 } else if ( pm->cmd.rightmove > 0 && pm->cmd.forwardmove < 0 ) {
334 pm->ps->movementDir = 5;
335 } else if ( pm->cmd.rightmove > 0 && pm->cmd.forwardmove == 0 ) {
336 pm->ps->movementDir = 6;
337 } else if ( pm->cmd.rightmove > 0 && pm->cmd.forwardmove > 0 ) {
338 pm->ps->movementDir = 7;
339 }
340 } else {
341 // if they aren't actively going directly sideways,
342 // change the animation to the diagonal so they
343 // don't stop too crooked
344 if ( pm->ps->movementDir == 2 ) {
345 pm->ps->movementDir = 1;
346 } else if ( pm->ps->movementDir == 6 ) {
347 pm->ps->movementDir = 7;
348 }
349 }
350 }
351
352
353 /*
354 =============
355 PM_CheckJump
356 =============
357 */
358 static qboolean PM_CheckJump( void ) {
359 if ( pm->ps->pm_flags & PMF_RESPAWNED ) {
360 return qfalse; // don't allow jump until all buttons are up
361 }
362
363 if ( pm->cmd.upmove < 10 ) {
364 // not holding jump
365 return qfalse;
366 }
367
368 // must wait for jump to be released
369 if ( pm->ps->pm_flags & PMF_JUMP_HELD ) {
370 // clear upmove so cmdscale doesn't lower running speed
371 pm->cmd.upmove = 0;
372 return qfalse;
373 }
374
375 pml.groundPlane = qfalse; // jumping away
376 pml.walking = qfalse;
377 pm->ps->pm_flags |= PMF_JUMP_HELD;
378
379 pm->ps->groundEntityNum = ENTITYNUM_NONE;
380 pm->ps->velocity[2] = JUMP_VELOCITY;
381 PM_AddEvent( EV_JUMP );
382
383 if ( pm->cmd.forwardmove >= 0 ) {
384 PM_ForceLegsAnim( LEGS_JUMP );
385 pm->ps->pm_flags &= ~PMF_BACKWARDS_JUMP;
386 } else {
387 PM_ForceLegsAnim( LEGS_JUMPB );
388 pm->ps->pm_flags |= PMF_BACKWARDS_JUMP;
389 }
390
391 return qtrue;
392 }
393
394 /*
395 =============
396 PM_CheckWaterJump
397 =============
398 */
399 static qboolean PM_CheckWaterJump( void ) {
400 vec3_t spot;
401 int cont;
402 vec3_t flatforward;
403
404 if (pm->ps->pm_time) {
405 return qfalse;
406 }
407
408 // check for water jump
409 if ( pm->waterlevel != 2 ) {
410 return qfalse;
411 }
412
413 flatforward[0] = pml.forward[0];
414 flatforward[1] = pml.forward[1];
415 flatforward[2] = 0;
416 VectorNormalize (flatforward);
417
418 VectorMA (pm->ps->origin, 30, flatforward, spot);
419 spot[2] += 4;
420 cont = pm->pointcontents (spot, pm->ps->clientNum );
421 if ( !(cont & CONTENTS_SOLID) ) {
422 return qfalse;
423 }
424
425 spot[2] += 16;
426 cont = pm->pointcontents (spot, pm->ps->clientNum );
427 if ( cont & (CONTENTS_SOLID|CONTENTS_PLAYERCLIP|CONTENTS_BODY) ) {
428 return qfalse;
429 }
430
431 // jump out of water
432 VectorScale (pml.forward, 200, pm->ps->velocity);
433 pm->ps->velocity[2] = 350;
434
435 pm->ps->pm_flags |= PMF_TIME_WATERJUMP;
436 pm->ps->pm_time = 2000;
437
438 return qtrue;
439 }
440
441 //============================================================================
442
443
444 /*
445 ===================
446 PM_WaterJumpMove
447
448 Flying out of the water
449 ===================
450 */
451 static void PM_WaterJumpMove( void ) {
452 // waterjump has no control, but falls
453
454 PM_StepSlideMove( qtrue );
455
456 pm->ps->velocity[2] -= pm->ps->gravity * pml.frametime;
457 if (pm->ps->velocity[2] < 0) {
458 // cancel as soon as we are falling down again
459 pm->ps->pm_flags &= ~PMF_ALL_TIMES;
460 pm->ps->pm_time = 0;
461 }
462 }
463
464 /*
465 ===================
466 PM_WaterMove
467
468 ===================
469 */
470 static void PM_WaterMove( void ) {
471 int i;
472 vec3_t wishvel;
473 float wishspeed;
474 vec3_t wishdir;
475 float scale;
476 float vel;
477
478 if ( PM_CheckWaterJump() ) {
479 PM_WaterJumpMove();
480 return;
481 }
482 #if 0
483 // jump = head for surface
484 if ( pm->cmd.upmove >= 10 ) {
485 if (pm->ps->velocity[2] > -300) {
486 if ( pm->watertype & CONTENTS_WATER ) {
487 pm->ps->velocity[2] = 100;
488 } else if ( pm->watertype & CONTENTS_SLIME ) {
489 pm->ps->velocity[2] = 80;
490 } else {
491 pm->ps->velocity[2] = 50;
492 }
493 }
494 }
495 #endif
496 PM_Friction ();
497
498 scale = PM_CmdScale( &pm->cmd );
499 //
500 // user intentions
501 //
502 if ( !scale ) {
503 wishvel[0] = 0;
504 wishvel[1] = 0;
505 wishvel[2] = -60; // sink towards bottom
506 } else {
507 for (i=0 ; i<3 ; i++)
508 wishvel[i] = scale * pml.forward[i]*pm->cmd.forwardmove + scale * pml.right[i]*pm->cmd.rightmove;
509
510 wishvel[2] += scale * pm->cmd.upmove;
511 }
512
513 VectorCopy (wishvel, wishdir);
514 wishspeed = VectorNormalize(wishdir);
515
516 if ( wishspeed > pm->ps->speed * pm_swimScale ) {
517 wishspeed = pm->ps->speed * pm_swimScale;
518 }
519
520 PM_Accelerate (wishdir, wishspeed, pm_wateraccelerate);
521
522 // make sure we can go up slopes easily under water
523 if ( pml.groundPlane && DotProduct( pm->ps->velocity, pml.groundTrace.plane.normal ) < 0 ) {
524 vel = VectorLength(pm->ps->velocity);
525 // slide along the ground plane
526 PM_ClipVelocity (pm->ps->velocity, pml.groundTrace.plane.normal,
527 pm->ps->velocity, OVERCLIP );
528
529 VectorNormalize(pm->ps->velocity);
530 VectorScale(pm->ps->velocity, vel, pm->ps->velocity);
531 }
532
533 PM_SlideMove( qfalse );
534 }
535
536 #ifdef MISSIONPACK
537 /*
538 ===================
539 PM_InvulnerabilityMove
540
541 Only with the invulnerability powerup
542 ===================
543 */
544 static void PM_InvulnerabilityMove( void ) {
545 pm->cmd.forwardmove = 0;
546 pm->cmd.rightmove = 0;
547 pm->cmd.upmove = 0;
548 VectorClear(pm->ps->velocity);
549 }
550 #endif
551
552 /*
553 ===================
554 PM_FlyMove
555
556 Only with the flight powerup
557 ===================
558 */
559 static void PM_FlyMove( void ) {
560 int i;
561 vec3_t wishvel;
562 float wishspeed;
563 vec3_t wishdir;
564 float scale;
565
566 // normal slowdown
567 PM_Friction ();
568
569 scale = PM_CmdScale( &pm->cmd );
570 //
571 // user intentions
572 //
573 if ( !scale ) {
574 wishvel[0] = 0;
575 wishvel[1] = 0;
576 wishvel[2] = 0;
577 } else {
578 for (i=0 ; i<3 ; i++) {
579 wishvel[i] = scale * pml.forward[i]*pm->cmd.forwardmove + scale * pml.right[i]*pm->cmd.rightmove;
580 }
581
582 wishvel[2] += scale * pm->cmd.upmove;
583 }
584
585 VectorCopy (wishvel, wishdir);
586 wishspeed = VectorNormalize(wishdir);
587
588 PM_Accelerate (wishdir, wishspeed, pm_flyaccelerate);
589
590 PM_StepSlideMove( qfalse );
591 }
592
593
594 /*
595 ===================
596 PM_AirMove
597
598 ===================
599 */
600 static void PM_AirMove( void ) {
601 int i;
602 vec3_t wishvel;
603 float fmove, smove;
604 vec3_t wishdir;
605 float wishspeed;
606 float scale;
607 usercmd_t cmd;
608
609 PM_Friction();
610
611 fmove = pm->cmd.forwardmove;
612 smove = pm->cmd.rightmove;
613
614 cmd = pm->cmd;
615 scale = PM_CmdScale( &cmd );
616
617 // set the movementDir so clients can rotate the legs for strafing
618 PM_SetMovementDir();
619
620 // project moves down to flat plane
621 pml.forward[2] = 0;
622 pml.right[2] = 0;
623 VectorNormalize (pml.forward);
624 VectorNormalize (pml.right);
625
626 for ( i = 0 ; i < 2 ; i++ ) {
627 wishvel[i] = pml.forward[i]*fmove + pml.right[i]*smove;
628 }
629 wishvel[2] = 0;
630
631 VectorCopy (wishvel, wishdir);
632 wishspeed = VectorNormalize(wishdir);
633 wishspeed *= scale;
634
635 // not on ground, so little effect on velocity
636 PM_Accelerate (wishdir, wishspeed, pm_airaccelerate);
637
638 // we may have a ground plane that is very steep, even
639 // though we don't have a groundentity
640 // slide along the steep plane
641 if ( pml.groundPlane ) {
642 PM_ClipVelocity (pm->ps->velocity, pml.groundTrace.plane.normal,
643 pm->ps->velocity, OVERCLIP );
644 }
645
646 #if 0
647 //ZOID: If we are on the grapple, try stair-stepping
648 //this allows a player to use the grapple to pull himself
649 //over a ledge
650 if (pm->ps->pm_flags & PMF_GRAPPLE_PULL)
651 PM_StepSlideMove ( qtrue );
652 else
653 PM_SlideMove ( qtrue );
654 #endif
655
656 PM_StepSlideMove ( qtrue );
657 }
658
659 /*
660 ===================
661 PM_GrappleMove
662
663 ===================
664 */
665 static void PM_GrappleMove( void ) {
666 vec3_t vel, v;
667 float vlen;
668
669 VectorScale(pml.forward, -16, v);
670 VectorAdd(pm->ps->grapplePoint, v, v);
671 VectorSubtract(v, pm->ps->origin, vel);
672 vlen = VectorLength(vel);
673 VectorNormalize( vel );
674
675 if (vlen <= 100)
676 VectorScale(vel, 10 * vlen, vel);
677 else
678 VectorScale(vel, 800, vel);
679
680 VectorCopy(vel, pm->ps->velocity);
681
682 pml.groundPlane = qfalse;
683 }
684
685 /*
686 ===================
687 PM_WalkMove
688
689 ===================
690 */
691 static void PM_WalkMove( void ) {
692 int i;
693 vec3_t wishvel;
694 float fmove, smove;
695 vec3_t wishdir;
696 float wishspeed;
697 float scale;
698 usercmd_t cmd;
699 float accelerate;
700 float vel;
701
702 if ( pm->waterlevel > 2 && DotProduct( pml.forward, pml.groundTrace.plane.normal ) > 0 ) {
703 // begin swimming
704 PM_WaterMove();
705 return;
706 }
707
708
709 if ( PM_CheckJump () ) {
710 // jumped away
711 if ( pm->waterlevel > 1 ) {
712 PM_WaterMove();
713 } else {
714 PM_AirMove();
715 }
716 return;
717 }
718
719 PM_Friction ();
720
721 fmove = pm->cmd.forwardmove;
722 smove = pm->cmd.rightmove;
723
724 cmd = pm->cmd;
725 scale = PM_CmdScale( &cmd );
726
727 // set the movementDir so clients can rotate the legs for strafing
728 PM_SetMovementDir();
729
730 // project moves down to flat plane
731 pml.forward[2] = 0;
732 pml.right[2] = 0;
733
734 // project the forward and right directions onto the ground plane
735 PM_ClipVelocity (pml.forward, pml.groundTrace.plane.normal, pml.forward, OVERCLIP );
736 PM_ClipVelocity (pml.right, pml.groundTrace.plane.normal, pml.right, OVERCLIP );
737 //
738 VectorNormalize (pml.forward);
739 VectorNormalize (pml.right);
740
741 for ( i = 0 ; i < 3 ; i++ ) {
742 wishvel[i] = pml.forward[i]*fmove + pml.right[i]*smove;
743 }
744 // when going up or down slopes the wish velocity should Not be zero
745 // wishvel[2] = 0;
746
747 VectorCopy (wishvel, wishdir);
748 wishspeed = VectorNormalize(wishdir);
749 wishspeed *= scale;
750
751 // clamp the speed lower if ducking
752 if ( pm->ps->pm_flags & PMF_DUCKED ) {
753 if ( wishspeed > pm->ps->speed * pm_duckScale ) {
754 wishspeed = pm->ps->speed * pm_duckScale;
755 }
756 }
757
758 // clamp the speed lower if wading or walking on the bottom
759 if ( pm->waterlevel ) {
760 float waterScale;
761
762 waterScale = pm->waterlevel / 3.0;
763 waterScale = 1.0 - ( 1.0 - pm_swimScale ) * waterScale;
764 if ( wishspeed > pm->ps->speed * waterScale ) {
765 wishspeed = pm->ps->speed * waterScale;
766 }
767 }
768
769 // when a player gets hit, they temporarily lose
770 // full control, which allows them to be moved a bit
771 if ( ( pml.groundTrace.surfaceFlags & SURF_SLICK ) || pm->ps->pm_flags & PMF_TIME_KNOCKBACK ) {
772 accelerate = pm_airaccelerate;
773 } else {
774 accelerate = pm_accelerate;
775 }
776
777 PM_Accelerate (wishdir, wishspeed, accelerate);
778
779 //Com_Printf("velocity = %1.1f %1.1f %1.1f\n", pm->ps->velocity[0], pm->ps->velocity[1], pm->ps->velocity[2]);
780 //Com_Printf("velocity1 = %1.1f\n", VectorLength(pm->ps->velocity));
781
782 if ( ( pml.groundTrace.surfaceFlags & SURF_SLICK ) || pm->ps->pm_flags & PMF_TIME_KNOCKBACK ) {
783 pm->ps->velocity[2] -= pm->ps->gravity * pml.frametime;
784 } else {
785 // don't reset the z velocity for slopes
786 // pm->ps->velocity[2] = 0;
787 }
788
789 vel = VectorLength(pm->ps->velocity);
790
791 // slide along the ground plane
792 PM_ClipVelocity (pm->ps->velocity, pml.groundTrace.plane.normal,
793 pm->ps->velocity, OVERCLIP );
794
795 // don't decrease velocity when going up or down a slope
796 VectorNormalize(pm->ps->velocity);
797 VectorScale(pm->ps->velocity, vel, pm->ps->velocity);
798
799 // don't do anything if standing still
800 if (!pm->ps->velocity[0] && !pm->ps->velocity[1]) {
801 return;
802 }
803
804 PM_StepSlideMove( qfalse );
805
806 //Com_Printf("velocity2 = %1.1f\n", VectorLength(pm->ps->velocity));
807
808 }
809
810
811 /*
812 ==============
813 PM_DeadMove
814 ==============
815 */
816 static void PM_DeadMove( void ) {
817 float forward;
818
819 if ( !pml.walking ) {
820 return;
821 }
822
823 // extra friction
824
825 forward = VectorLength (pm->ps->velocity);
826 forward -= 20;
827 if ( forward <= 0 ) {
828 VectorClear (pm->ps->velocity);
829 } else {
830 VectorNormalize (pm->ps->velocity);
831 VectorScale (pm->ps->velocity, forward, pm->ps->velocity);
832 }
833 }
834
835
836 /*
837 ===============
838 PM_NoclipMove
839 ===============
840 */
841 static void PM_NoclipMove( void ) {
842 float speed, drop, friction, control, newspeed;
843 int i;
844 vec3_t wishvel;
845 float fmove, smove;
846 vec3_t wishdir;
847 float wishspeed;
848 float scale;
849
850 pm->ps->viewheight = DEFAULT_VIEWHEIGHT;
851
852 // friction
853
854 speed = VectorLength (pm->ps->velocity);
855 if (speed < 1)
856 {
857 VectorCopy (vec3_origin, pm->ps->velocity);
858 }
859 else
860 {
861 drop = 0;
862
863 friction = pm_friction*1.5; // extra friction
864 control = speed < pm_stopspeed ? pm_stopspeed : speed;
865 drop += control*friction*pml.frametime;
866
867 // scale the velocity
868 newspeed = speed - drop;
869 if (newspeed < 0)
870 newspeed = 0;
871 newspeed /= speed;
872
873 VectorScale (pm->ps->velocity, newspeed, pm->ps->velocity);
874 }
875
876 // accelerate
877 scale = PM_CmdScale( &pm->cmd );
878
879 fmove = pm->cmd.forwardmove;
880 smove = pm->cmd.rightmove;
881
882 for (i=0 ; i<3 ; i++)
883 wishvel[i] = pml.forward[i]*fmove + pml.right[i]*smove;
884 wishvel[2] += pm->cmd.upmove;
885
886 VectorCopy (wishvel, wishdir);
887 wishspeed = VectorNormalize(wishdir);
888 wishspeed *= scale;
889
890 PM_Accelerate( wishdir, wishspeed, pm_accelerate );
891
892 // move
893 VectorMA (pm->ps->origin, pml.frametime, pm->ps->velocity, pm->ps->origin);
894 }
895
896 //============================================================================
897
898 /*
899 ================
900 PM_FootstepForSurface
901
902 Returns an event number appropriate for the groundsurface
903 ================
904 */
905 static int PM_FootstepForSurface( void ) {
906 if ( pml.groundTrace.surfaceFlags & SURF_NOSTEPS ) {
907 return 0;
908 }
909 if ( pml.groundTrace.surfaceFlags & SURF_METALSTEPS ) {
910 return EV_FOOTSTEP_METAL;
911 }
912 return EV_FOOTSTEP;
913 }
914
915
916 /*
917 =================
918 PM_CrashLand
919
920 Check for hard landings that generate sound events
921 =================
922 */
923 static void PM_CrashLand( void ) {
924 float delta;
925 float dist;
926 float vel, acc;
927 float t;
928 float a, b, c, den;
929
930 // decide which landing animation to use
931 if ( pm->ps->pm_flags & PMF_BACKWARDS_JUMP ) {
932 PM_ForceLegsAnim( LEGS_LANDB );
933 } else {
934 PM_ForceLegsAnim( LEGS_LAND );
935 }
936
937 pm->ps->legsTimer = TIMER_LAND;
938
939 // calculate the exact velocity on landing
940 dist = pm->ps->origin[2] - pml.previous_origin[2];
941 vel = pml.previous_velocity[2];
942 acc = -pm->ps->gravity;
943
944 a = acc / 2;
945 b = vel;
946 c = -dist;
947
948 den = b * b - 4 * a * c;
949 if ( den < 0 ) {
950 return;
951 }
952 t = (-b - sqrt( den ) ) / ( 2 * a );
953
954 delta = vel + t * acc;
955 delta = delta*delta * 0.0001;
956
957 // ducking while falling doubles damage
958 if ( pm->ps->pm_flags & PMF_DUCKED ) {
959 delta *= 2;
960 }
961
962 // never take falling damage if completely underwater
963 if ( pm->waterlevel == 3 ) {
964 return;
965 }
966
967 // reduce falling damage if there is standing water
968 if ( pm->waterlevel == 2 ) {
969 delta *= 0.25;
970 }
971 if ( pm->waterlevel == 1 ) {
972 delta *= 0.5;
973 }
974
975 if ( delta < 1 ) {
976 return;
977 }
978
979 // create a local entity event to play the sound
980
981 // SURF_NODAMAGE is used for bounce pads where you don't ever
982 // want to take damage or play a crunch sound
983 if ( !(pml.groundTrace.surfaceFlags & SURF_NODAMAGE) ) {
984 if ( delta > 60 ) {
985 PM_AddEvent( EV_FALL_FAR );
986 } else if ( delta > 40 ) {
987 // this is a pain grunt, so don't play it if dead
988 if ( pm->ps->stats[STAT_HEALTH] > 0 ) {
989 PM_AddEvent( EV_FALL_MEDIUM );
990 }
991 } else if ( delta > 7 ) {
992 PM_AddEvent( EV_FALL_SHORT );
993 } else {
994 PM_AddEvent( PM_FootstepForSurface() );
995 }
996 }
997
998 // start footstep cycle over
999 pm->ps->bobCycle = 0;
1000 }
1001
1002 /*
1003 =============
1004 PM_CheckStuck
1005 =============
1006 */
1007 /*
1008 void PM_CheckStuck(void) {
1009 trace_t trace;
1010
1011 pm->trace (&trace, pm->ps->origin, pm->mins, pm->maxs, pm->ps->origin, pm->ps->clientNum, pm->tracemask);
1012 if (trace.allsolid) {
1013 //int shit = qtrue;
1014 }
1015 }
1016 */
1017
1018 /*
1019 =============
1020 PM_CorrectAllSolid
1021 =============
1022 */
1023 static int PM_CorrectAllSolid( trace_t *trace ) {
1024 int i, j, k;
1025 vec3_t point;
1026
1027 if ( pm->debugLevel ) {
1028 Com_Printf("%i:allsolid\n", c_pmove);
1029 }
1030
1031 // jitter around
1032 for (i = -1; i <= 1; i++) {
1033 for (j = -1; j <= 1; j++) {
1034 for (k = -1; k <= 1; k++) {
1035 VectorCopy(pm->ps->origin, point);
1036 point[0] += (float) i;
1037 point[1] += (float) j;
1038 point[2] += (float) k;
1039 pm->trace (trace, point, pm->mins, pm->maxs, point, pm->ps->clientNum, pm->tracemask);
1040 if ( !trace->allsolid ) {
1041 point[0] = pm->ps->origin[0];
1042 point[1] = pm->ps->origin[1];
1043 point[2] = pm->ps->origin[2] - 0.25;
1044
1045 pm->trace (trace, pm->ps->origin, pm->mins, pm->maxs, point, pm->ps->clientNum, pm->tracemask);
1046 pml.groundTrace = *trace;
1047 return qtrue;
1048 }
1049 }
1050 }
1051 }
1052
1053 pm->ps->groundEntityNum = ENTITYNUM_NONE;
1054 pml.groundPlane = qfalse;
1055 pml.walking = qfalse;
1056
1057 return qfalse;
1058 }
1059
1060
1061 /*
1062 =============
1063 PM_GroundTraceMissed
1064
1065 The ground trace didn't hit a surface, so we are in freefall
1066 =============
1067 */
1068 static void PM_GroundTraceMissed( void ) {
1069 trace_t trace;
1070 vec3_t point;
1071
1072 if ( pm->ps->groundEntityNum != ENTITYNUM_NONE ) {
1073 // we just transitioned into freefall
1074 if ( pm->debugLevel ) {
1075 Com_Printf("%i:lift\n", c_pmove);
1076 }
1077
1078 // if they aren't in a jumping animation and the ground is a ways away, force into it
1079 // if we didn't do the trace, the player would be backflipping down staircases
1080 VectorCopy( pm->ps->origin, point );
1081 point[2] -= 64;
1082
1083 pm->trace (&trace, pm->ps->origin, pm->mins, pm->maxs, point, pm->ps->clientNum, pm->tracemask);
1084 if ( trace.fraction == 1.0 ) {
1085 if ( pm->cmd.forwardmove >= 0 ) {
1086 PM_ForceLegsAnim( LEGS_JUMP );
1087 pm->ps->pm_flags &= ~PMF_BACKWARDS_JUMP;
1088 } else {
1089 PM_ForceLegsAnim( LEGS_JUMPB );
1090 pm->ps->pm_flags |= PMF_BACKWARDS_JUMP;
1091 }
1092 }
1093 }
1094
1095 pm->ps->groundEntityNum = ENTITYNUM_NONE;
1096 pml.groundPlane = qfalse;
1097 pml.walking = qfalse;
1098 }
1099
1100
1101 /*
1102 =============
1103 PM_GroundTrace
1104 =============
1105 */
1106 static void PM_GroundTrace( void ) {
1107 vec3_t point;
1108 trace_t trace;
1109
1110 point[0] = pm->ps->origin[0];
1111 point[1] = pm->ps->origin[1];
1112 point[2] = pm->ps->origin[2] - 0.25;
1113
1114 pm->trace (&trace, pm->ps->origin, pm->mins, pm->maxs, point, pm->ps->clientNum, pm->tracemask);
1115 pml.groundTrace = trace;
1116
1117 // do something corrective if the trace starts in a solid...
1118 if ( trace.allsolid ) {
1119 if ( !PM_CorrectAllSolid(&trace) )
1120 return;
1121 }
1122
1123 // if the trace didn't hit anything, we are in free fall
1124 if ( trace.fraction == 1.0 ) {
1125 PM_GroundTraceMissed();
1126 pml.groundPlane = qfalse;
1127 pml.walking = qfalse;
1128 return;
1129 }
1130
1131 // check if getting thrown off the ground
1132 if ( pm->ps->velocity[2] > 0 && DotProduct( pm->ps->velocity, trace.plane.normal ) > 10 ) {
1133 if ( pm->debugLevel ) {
1134 Com_Printf("%i:kickoff\n", c_pmove);
1135 }
1136 // go into jump animation
1137 if ( pm->cmd.forwardmove >= 0 ) {
1138 PM_ForceLegsAnim( LEGS_JUMP );
1139 pm->ps->pm_flags &= ~PMF_BACKWARDS_JUMP;
1140 } else {
1141 PM_ForceLegsAnim( LEGS_JUMPB );
1142 pm->ps->pm_flags |= PMF_BACKWARDS_JUMP;
1143 }
1144
1145 pm->ps->groundEntityNum = ENTITYNUM_NONE;
1146 pml.groundPlane = qfalse;
1147 pml.walking = qfalse;
1148 return;
1149 }
1150
1151 // slopes that are too steep will not be considered onground
1152 if ( trace.plane.normal[2] < MIN_WALK_NORMAL ) {
1153 if ( pm->debugLevel ) {
1154 Com_Printf("%i:steep\n", c_pmove);
1155 }
1156 // FIXME: if they can't slide down the slope, let them
1157 // walk (sharp crevices)
1158 pm->ps->groundEntityNum = ENTITYNUM_NONE;
1159 pml.groundPlane = qtrue;
1160 pml.walking = qfalse;
1161 return;
1162 }
1163
1164 pml.groundPlane = qtrue;
1165 pml.walking = qtrue;
1166
1167 // hitting solid ground will end a waterjump
1168 if (pm->ps->pm_flags & PMF_TIME_WATERJUMP)
1169 {
1170 pm->ps->pm_flags &= ~(PMF_TIME_WATERJUMP | PMF_TIME_LAND);
1171 pm->ps->pm_time = 0;
1172 }
1173
1174 if ( pm->ps->groundEntityNum == ENTITYNUM_NONE ) {
1175 // just hit the ground
1176 if ( pm->debugLevel ) {
1177 Com_Printf("%i:Land\n", c_pmove);
1178 }
1179
1180 PM_CrashLand();
1181
1182 // don't do landing time if we were just going down a slope
1183 if ( pml.previous_velocity[2] < -200 ) {
1184 // don't allow another jump for a little while
1185 pm->ps->pm_flags |= PMF_TIME_LAND;
1186 pm->ps->pm_time = 250;
1187 }
1188 }
1189
1190 pm->ps->groundEntityNum = trace.entityNum;
1191
1192 // don't reset the z velocity for slopes
1193 // pm->ps->velocity[2] = 0;
1194
1195 PM_AddTouchEnt( trace.entityNum );
1196 }
1197
1198
1199 /*
1200 =============
1201 PM_SetWaterLevel FIXME: avoid this twice? certainly if not moving
1202 =============
1203 */
1204 static void PM_SetWaterLevel( void ) {
1205 vec3_t point;
1206 int cont;
1207 int sample1;
1208 int sample2;
1209
1210 //
1211 // get waterlevel, accounting for ducking
1212 //
1213 pm->waterlevel = 0;
1214 pm->watertype = 0;
1215
1216 point[0] = pm->ps->origin[0];
1217 point[1] = pm->ps->origin[1];
1218 point[2] = pm->ps->origin[2] + MINS_Z + 1;
1219 cont = pm->pointcontents( point, pm->ps->clientNum );
1220
1221 if ( cont & MASK_WATER ) {
1222 sample2 = pm->ps->viewheight - MINS_Z;
1223 sample1 = sample2 / 2;
1224
1225 pm->watertype = cont;
1226 pm->waterlevel = 1;
1227 point[2] = pm->ps->origin[2] + MINS_Z + sample1;
1228 cont = pm->pointcontents (point, pm->ps->clientNum );
1229 if ( cont & MASK_WATER ) {
1230 pm->waterlevel = 2;
1231 point[2] = pm->ps->origin[2] + MINS_Z + sample2;
1232 cont = pm->pointcontents (point, pm->ps->clientNum );
1233 if ( cont & MASK_WATER ){
1234 pm->waterlevel = 3;
1235 }
1236 }
1237 }
1238
1239 }
1240
1241 /*
1242 ==============
1243 PM_CheckDuck
1244
1245 Sets mins, maxs, and pm->ps->viewheight
1246 ==============
1247 */
1248 static void PM_CheckDuck (void)
1249 {
1250 trace_t trace;
1251
1252 if ( pm->ps->powerups[PW_INVULNERABILITY] ) {
1253 if ( pm->ps->pm_flags & PMF_INVULEXPAND ) {
1254 // invulnerability sphere has a 42 units radius
1255 VectorSet( pm->mins, -INVUL_RADIUS, -INVUL_RADIUS, -INVUL_RADIUS );
1256 VectorSet( pm->maxs, INVUL_RADIUS, INVUL_RADIUS, INVUL_RADIUS );
1257 }
1258 else {
1259 VectorSet( pm->mins, -PLAYER_WIDTH, -PLAYER_WIDTH, MINS_Z );
1260 VectorSet( pm->maxs, PLAYER_WIDTH, PLAYER_WIDTH, 16 );
1261 }
1262 pm->ps->pm_flags |= PMF_DUCKED;
1263 pm->ps->viewheight = CROUCH_VIEWHEIGHT;
1264 return;
1265 }
1266 pm->ps->pm_flags &= ~PMF_INVULEXPAND;
1267
1268 pm->mins[0] = -PLAYER_WIDTH;
1269 pm->mins[1] = -PLAYER_WIDTH;
1270
1271 pm->maxs[0] = PLAYER_WIDTH;
1272 pm->maxs[1] = PLAYER_WIDTH;
1273
1274 pm->mins[2] = MINS_Z;
1275
1276 if (pm->ps->pm_type == PM_DEAD)
1277 {
1278 pm->maxs[2] = DEAD_HEIGHT;
1279 pm->ps->viewheight = DEAD_VIEWHEIGHT;
1280 return;
1281 }
1282
1283 if (pm->cmd.upmove < 0)
1284 { // duck
1285 pm->ps->pm_flags |= PMF_DUCKED;
1286 }
1287 else
1288 { // stand up if possible
1289 if (pm->ps->pm_flags & PMF_DUCKED)
1290 {
1291 // try to stand up
1292 pm->maxs[2] = DEFAULT_HEIGHT;
1293 pm->trace (&trace, pm->ps->origin, pm->mins, pm->maxs, pm->ps->origin, pm->ps->clientNum, pm->tracemask );
1294 if (!trace.allsolid)
1295 pm->ps->pm_flags &= ~PMF_DUCKED;
1296 }
1297 }
1298
1299 if (pm->ps->pm_flags & PMF_DUCKED)
1300 {
1301 pm->maxs[2] = CROUCH_HEIGHT;
1302 pm->ps->viewheight = CROUCH_VIEWHEIGHT;
1303 }
1304 else
1305 {
1306 pm->maxs[2] = DEFAULT_HEIGHT;
1307 pm->ps->viewheight = DEFAULT_VIEWHEIGHT;
1308 }
1309 }
1310
1311
1312
1313 //===================================================================
1314
1315
1316 /*
1317 ===============
1318 PM_Footsteps
1319 ===============
1320 */
1321 static void PM_Footsteps( void ) {
1322 float bobmove;
1323 int old;
1324 qboolean footstep;
1325
1326 //
1327 // calculate speed and cycle to be used for
1328 // all cyclic walking effects
1329 //
1330 pm->xyspeed = sqrt( pm->ps->velocity[0] * pm->ps->velocity[0]
1331 + pm->ps->velocity[1] * pm->ps->velocity[1] );
1332
1333 if ( pm->ps->groundEntityNum == ENTITYNUM_NONE ) {
1334
1335 if ( pm->ps->powerups[PW_INVULNERABILITY] ) {
1336 PM_ContinueLegsAnim( LEGS_IDLECR );
1337 }
1338 // airborne leaves position in cycle intact, but doesn't advance
1339 if ( pm->waterlevel > 1 ) {
1340 PM_ContinueLegsAnim( LEGS_SWIM );
1341 }
1342 return;
1343 }
1344
1345 // if not trying to move
1346 if ( !pm->cmd.forwardmove && !pm->cmd.rightmove ) {
1347 if ( pm->xyspeed < 5 ) {
1348 pm->ps->bobCycle = 0; // start at beginning of cycle again
1349 if ( pm->ps->pm_flags & PMF_DUCKED ) {
1350 PM_ContinueLegsAnim( LEGS_IDLECR );
1351 } else {
1352 PM_ContinueLegsAnim( LEGS_IDLE );
1353 }
1354 }
1355 return;
1356 }
1357
1358
1359 footstep = qfalse;
1360
1361 if ( pm->ps->pm_flags & PMF_DUCKED ) {
1362 bobmove = 0.5; // ducked characters bob much faster
1363 if ( pm->ps->pm_flags & PMF_BACKWARDS_RUN ) {
1364 PM_ContinueLegsAnim( LEGS_BACKCR );
1365 }
1366 else {
1367 PM_ContinueLegsAnim( LEGS_WALKCR );
1368 }
1369 // ducked characters never play footsteps
1370 /*
1371 } else if ( pm->ps->pm_flags & PMF_BACKWARDS_RUN ) {
1372 if ( !( pm->cmd.buttons & BUTTON_WALKING ) ) {
1373 bobmove = 0.4; // faster speeds bob faster
1374 footstep = qtrue;
1375 } else {
1376 bobmove = 0.3;
1377 }
1378 PM_ContinueLegsAnim( LEGS_BACK );
1379 */
1380 } else {
1381 if ( !( pm->cmd.buttons & BUTTON_WALKING ) ) {
1382 bobmove = 0.4f; // faster speeds bob faster
1383 if ( pm->ps->pm_flags & PMF_BACKWARDS_RUN ) {
1384 PM_ContinueLegsAnim( LEGS_BACK );
1385 }
1386 else {
1387 PM_ContinueLegsAnim( LEGS_RUN );
1388 }
1389 footstep = qtrue;
1390 } else {
1391 bobmove = 0.3f; // walking bobs slow
1392 if ( pm->ps->pm_flags & PMF_BACKWARDS_RUN ) {
1393 PM_ContinueLegsAnim( LEGS_BACKWALK );
1394 }
1395 else {
1396 PM_ContinueLegsAnim( LEGS_WALK );
1397 }
1398 }
1399 }
1400
1401 // check for footstep / splash sounds
1402 old = pm->ps->bobCycle;
1403 pm->ps->bobCycle = (int)( old + bobmove * pml.msec ) & 255;
1404
1405 // if we just crossed a cycle boundary, play an appropriate footstep event
1406 if ( ( ( old + 64 ) ^ ( pm->ps->bobCycle + 64 ) ) & 128 ) {
1407 if ( pm->waterlevel == 0 ) {
1408 // on ground will only play sounds if running
1409 if ( footstep && !pm->noFootsteps ) {
1410 PM_AddEvent( PM_FootstepForSurface() );
1411 }
1412 } else if ( pm->waterlevel == 1 ) {
1413 // splashing
1414 PM_AddEvent( EV_FOOTSPLASH );
1415 } else if ( pm->waterlevel == 2 ) {
1416 // wading / swimming at surface
1417 PM_AddEvent( EV_SWIM );
1418 } else if ( pm->waterlevel == 3 ) {
1419 // no sound when completely underwater
1420
1421 }
1422 }
1423 }
1424
1425 /*
1426 ==============
1427 PM_WaterEvents
1428
1429 Generate sound events for entering and leaving water
1430 ==============
1431 */
1432 static void PM_WaterEvents( void ) { // FIXME?
1433 //
1434 // if just entered a water volume, play a sound
1435 //
1436 if (!pml.previous_waterlevel && pm->waterlevel) {
1437 PM_AddEvent( EV_WATER_TOUCH );
1438 }
1439
1440 //
1441 // if just completely exited a water volume, play a sound
1442 //
1443 if (pml.previous_waterlevel && !pm->waterlevel) {
1444 PM_AddEvent( EV_WATER_LEAVE );
1445 }
1446
1447 //
1448 // check for head just going under water
1449 //
1450 if (pml.previous_waterlevel != 3 && pm->waterlevel == 3) {
1451 PM_AddEvent( EV_WATER_UNDER );
1452 }
1453
1454 //
1455 // check for head just coming out of water
1456 //
1457 if (pml.previous_waterlevel == 3 && pm->waterlevel != 3) {
1458 PM_AddEvent( EV_WATER_CLEAR );
1459 }
1460 }
1461
1462
1463 /*
1464 ===============
1465 PM_BeginWeaponChange
1466 ===============
1467 */
1468 static void PM_BeginWeaponChange( int weapon ) {
1469 if ( weapon <= WP_NONE || weapon >= WP_NUM_WEAPONS ) {
1470 return;
1471 }
1472
1473 if ( !( pm->ps->stats[STAT_WEAPONS] & ( 1 << weapon ) ) ) {
1474 return;
1475 }
1476
1477 if ( pm->ps->weaponstate == WEAPON_DROPPING ) {
1478 return;
1479 }
1480
1481 PM_AddEvent( EV_CHANGE_WEAPON );
1482 pm->ps->weaponstate = WEAPON_DROPPING;
1483 pm->ps->weaponTime += 200;
1484 PM_StartTorsoAnim( TORSO_DROP );
1485 }
1486
1487
1488 /*
1489 ===============
1490 PM_FinishWeaponChange
1491 ===============
1492 */
1493 static void PM_FinishWeaponChange( void ) {
1494 int weapon;
1495
1496 weapon = pm->cmd.weapon;
1497 if ( weapon < WP_NONE || weapon >= WP_NUM_WEAPONS ) {
1498 weapon = WP_NONE;
1499 }
1500
1501 if ( !( pm->ps->stats[STAT_WEAPONS] & ( 1 << weapon ) ) ) {
1502 weapon = WP_NONE;
1503 }
1504
1505 pm->ps->weapon = weapon;
1506 pm->ps->weaponstate = WEAPON_RAISING;
1507 pm->ps->weaponTime += 250;
1508 PM_StartTorsoAnim( TORSO_RAISE );
1509 }
1510
1511
1512 /*
1513 ==============
1514 PM_TorsoAnimation
1515
1516 ==============
1517 */
1518 static void PM_TorsoAnimation( void ) {
1519 if ( pm->ps->weaponstate == WEAPON_READY ) {
1520 if ( pm->ps->weapon == WP_GAUNTLET ) {
1521 PM_ContinueTorsoAnim( TORSO_STAND2 );
1522 } else {
1523 PM_ContinueTorsoAnim( TORSO_STAND );
1524 }
1525 return;
1526 }
1527 }
1528
1529
1530 /*
1531 ==============
1532 PM_Weapon
1533
1534 Generates weapon events and modifes the weapon counter
1535 ==============
1536 */
1537 static void PM_Weapon( void ) {
1538 int addTime;
1539
1540 // don't allow attack until all buttons are up
1541 if ( pm->ps->pm_flags & PMF_RESPAWNED ) {
1542 return;
1543 }
1544
1545 // ignore if spectator
1546 if ( pm->ps->persistant[PERS_TEAM] == TEAM_SPECTATOR ) {
1547 return;
1548 }
1549
1550 // check for dead player
1551 if ( pm->ps->stats[STAT_HEALTH] <= 0 ) {
1552 pm->ps->weapon = WP_NONE;
1553 return;
1554 }
1555
1556 // check for item using
1557 if ( pm->cmd.buttons & BUTTON_USE_HOLDABLE ) {
1558 if ( ! ( pm->ps->pm_flags & PMF_USE_ITEM_HELD ) ) {
1559 if ( bg_itemlist[pm->ps->stats[STAT_HOLDABLE_ITEM]].giTag == HI_MEDKIT
1560 && pm->ps->stats[STAT_HEALTH] >= (pm->ps->stats[STAT_MAX_HEALTH] + 25) ) {
1561 // don't use medkit if at max health
1562 } else {
1563 pm->ps->pm_flags |= PMF_USE_ITEM_HELD;
1564 PM_AddEvent( EV_USE_ITEM0 + bg_itemlist[pm->ps->stats[STAT_HOLDABLE_ITEM]].giTag );
1565 pm->ps->stats[STAT_HOLDABLE_ITEM] = 0;
1566 }
1567 return;
1568 }
1569 } else {
1570 pm->ps->pm_flags &= ~PMF_USE_ITEM_HELD;
1571 }
1572
1573
1574 // make weapon function
1575 if ( pm->ps->weaponTime > 0 ) {
1576 pm->ps->weaponTime -= pml.msec;
1577 }
1578
1579 // check for weapon change
1580 // can't change if weapon is firing, but can change
1581 // again if lowering or raising
1582 if ( pm->ps->weaponTime <= 0 || pm->ps->weaponstate != WEAPON_FIRING ) {
1583 if ( pm->ps->weapon != pm->cmd.weapon ) {
1584 PM_BeginWeaponChange( pm->cmd.weapon );
1585 }
1586 }
1587
1588 if ( pm->ps->weaponTime > 0 ) {
1589 return;
1590 }
1591
1592 // change weapon if time
1593 if ( pm->ps->weaponstate == WEAPON_DROPPING ) {
1594 PM_FinishWeaponChange();
1595 return;
1596 }
1597
1598 if ( pm->ps->weaponstate == WEAPON_RAISING ) {
1599 pm->ps->weaponstate = WEAPON_READY;
1600 if ( pm->ps->weapon == WP_GAUNTLET ) {
1601 PM_StartTorsoAnim( TORSO_STAND2 );
1602 } else {
1603 PM_StartTorsoAnim( TORSO_STAND );
1604 }
1605 return;
1606 }
1607
1608 // check for fire
1609 if ( ! (pm->cmd.buttons & BUTTON_ATTACK) ) {
1610 pm->ps->weaponTime = 0;
1611 pm->ps->weaponstate = WEAPON_READY;
1612 return;
1613 }
1614
1615 // start the animation even if out of ammo
1616 if ( pm->ps->weapon == WP_GAUNTLET ) {
1617 // the guantlet only "fires" when it actually hits something
1618 if ( !pm->gauntletHit ) {
1619 pm->ps->weaponTime = 0;
1620 pm->ps->weaponstate = WEAPON_READY;
1621 return;
1622 }
1623 PM_StartTorsoAnim( TORSO_ATTACK2 );
1624 } else {
1625 PM_StartTorsoAnim( TORSO_ATTACK );
1626 }
1627
1628 pm->ps->weaponstate = WEAPON_FIRING;
1629
1630 // check for out of ammo
1631 if ( ! pm->ps->ammo[ pm->ps->weapon ] ) {
1632 PM_AddEvent( EV_NOAMMO );
1633 pm->ps->weaponTime += 500;
1634 return;
1635 }
1636
1637 // take an ammo away if not infinite
1638 if ( pm->ps->ammo[ pm->ps->weapon ] != -1 ) {
1639 pm->ps->ammo[ pm->ps->weapon ]--;
1640 }
1641
1642 // fire weapon
1643 PM_AddEvent( EV_FIRE_WEAPON );
1644
1645 switch( pm->ps->weapon ) {
1646 default:
1647 case WP_GAUNTLET:
1648 addTime = 400;
1649 break;
1650 case WP_LIGHTNING:
1651 addTime = 50;
1652 break;
1653 case WP_SHOTGUN:
1654 addTime = 1000;
1655 break;
1656 case WP_MACHINEGUN:
1657 addTime = 100;
1658 break;
1659 case WP_GRENADE_LAUNCHER:
1660 addTime = 800;
1661 break;
1662 case WP_ROCKET_LAUNCHER:
1663 addTime = 800;
1664 break;
1665 case WP_PLASMAGUN:
1666 addTime = 100;
1667 break;
1668 case WP_RAILGUN:
1669 addTime = 1500;
1670 break;
1671 case WP_BFG:
1672 addTime = 200;
1673 break;
1674 case WP_GRAPPLING_HOOK:
1675 addTime = 400;
1676 break;
1677 #ifdef MISSIONPACK
1678 case WP_NAILGUN:
1679 addTime = 1000;
1680 break;
1681 case WP_PROX_LAUNCHER:
1682 addTime = 800;
1683 break;
1684 case WP_CHAINGUN:
1685 addTime = 30;
1686 break;
1687 #endif
1688 }
1689
1690 #ifdef MISSIONPACK
1691 if( bg_itemlist[pm->ps->stats[STAT_PERSISTANT_POWERUP]].giTag == PW_SCOUT ) {
1692 addTime /= 1.5;
1693 }
1694 else
1695 if( bg_itemlist[pm->ps->stats[STAT_PERSISTANT_POWERUP]].giTag == PW_AMMOREGEN ) {
1696 addTime /= 1.3;
1697 }
1698 else
1699 #endif
1700 if ( pm->ps->powerups[PW_HASTE] ) {
1701 addTime /= 1.3;
1702 }
1703
1704 pm->ps->weaponTime += addTime;
1705 }
1706
1707 /*
1708 ================
1709 PM_Animate
1710 ================
1711 */
1712
1713 static void PM_Animate( void ) {
1714 if ( pm->cmd.buttons & BUTTON_GESTURE ) {
1715 if ( pm->ps->torsoTimer == 0 ) {
1716 PM_StartTorsoAnim( TORSO_GESTURE );
1717 pm->ps->torsoTimer = TIMER_GESTURE;
1718 PM_AddEvent( EV_TAUNT );
1719 }
1720 #ifdef MISSIONPACK
1721 } else if ( pm->cmd.buttons & BUTTON_GETFLAG ) {
1722 if ( pm->ps->torsoTimer == 0 ) {
1723 PM_StartTorsoAnim( TORSO_GETFLAG );
1724 pm->ps->torsoTimer = 600; //TIMER_GESTURE;
1725 }
1726 } else if ( pm->cmd.buttons & BUTTON_GUARDBASE ) {
1727 if ( pm->ps->torsoTimer == 0 ) {
1728 PM_StartTorsoAnim( TORSO_GUARDBASE );
1729 pm->ps->torsoTimer = 600; //TIMER_GESTURE;
1730 }
1731 } else if ( pm->cmd.buttons & BUTTON_PATROL ) {
1732 if ( pm->ps->torsoTimer == 0 ) {
1733 PM_StartTorsoAnim( TORSO_PATROL );
1734 pm->ps->torsoTimer = 600; //TIMER_GESTURE;
1735 }
1736 } else if ( pm->cmd.buttons & BUTTON_FOLLOWME ) {
1737 if ( pm->ps->torsoTimer == 0 ) {
1738 PM_StartTorsoAnim( TORSO_FOLLOWME );
1739 pm->ps->torsoTimer = 600; //TIMER_GESTURE;
1740 }
1741 } else if ( pm->cmd.buttons & BUTTON_AFFIRMATIVE ) {
1742 if ( pm->ps->torsoTimer == 0 ) {
1743 PM_StartTorsoAnim( TORSO_AFFIRMATIVE);
1744 pm->ps->torsoTimer = 600; //TIMER_GESTURE;
1745 }
1746 } else if ( pm->cmd.buttons & BUTTON_NEGATIVE ) {
1747 if ( pm->ps->torsoTimer == 0 ) {
1748 PM_StartTorsoAnim( TORSO_NEGATIVE );
1749 pm->ps->torsoTimer = 600; //TIMER_GESTURE;
1750 }
1751 #endif
1752 }
1753 }
1754
1755
1756 /*
1757 ================
1758 PM_DropTimers
1759 ================
1760 */
1761 static void PM_DropTimers( void ) {
1762 // drop misc timing counter
1763 if ( pm->ps->pm_time ) {
1764 if ( pml.msec >= pm->ps->pm_time ) {
1765 pm->ps->pm_flags &= ~PMF_ALL_TIMES;
1766 pm->ps->pm_time = 0;
1767 } else {
1768 pm->ps->pm_time -= pml.msec;
1769 }
1770 }
1771
1772 // drop animation counter
1773 if ( pm->ps->legsTimer > 0 ) {
1774 pm->ps->legsTimer -= pml.msec;
1775 if ( pm->ps->legsTimer < 0 ) {
1776 pm->ps->legsTimer = 0;
1777 }
1778 }
1779
1780 if ( pm->ps->torsoTimer > 0 ) {
1781 pm->ps->torsoTimer -= pml.msec;
1782 if ( pm->ps->torsoTimer < 0 ) {
1783 pm->ps->torsoTimer = 0;
1784 }
1785 }
1786 }
1787
1788 /*
1789 ================
1790 PM_UpdateViewAngles
1791
1792 This can be used as another entry point when only the viewangles
1793 are being updated instead of a full move
1794 ================
1795 */
1796 void PM_UpdateViewAngles( playerState_t *ps, const usercmd_t *cmd ) {
1797 short temp;
1798 int i;
1799
1800 if ( ps->pm_type == PM_INTERMISSION || ps->pm_type == PM_SPINTERMISSION) {
1801 return; // no view changes at all
1802 }
1803
1804 if ( ps->pm_type != PM_SPECTATOR && ps->stats[STAT_HEALTH] <= 0 ) {
1805 return; // no view changes at all
1806 }
1807
1808 // circularly clamp the angles with deltas
1809 for (i=0 ; i<3 ; i++) {
1810 temp = cmd->angles[i] + ps->delta_angles[i];
1811 if ( i == PITCH ) {
1812 // don't let the player look up or down more than 90 degrees
1813 if ( temp > 16000 ) {
1814 ps->delta_angles[i] = 16000 - cmd->angles[i];
1815 temp = 16000;
1816 } else if ( temp < -16000 ) {
1817 ps->delta_angles[i] = -16000 - cmd->angles[i];
1818 temp = -16000;
1819 }
1820 }
1821 ps->viewangles[i] = SHORT2ANGLE(temp);
1822 }
1823
1824 }
1825
1826
1827 /*
1828 ================
1829 PmoveSingle
1830
1831 ================
1832 */
1833 void trap_SnapVector( float *v );
1834
1835 void PmoveSingle (pmove_t *pmove) {
1836 pm = pmove;
1837
1838 // this counter lets us debug movement problems with a journal
1839 // by setting a conditional breakpoint fot the previous frame
1840 c_pmove++;
1841
1842 // clear results
1843 pm->numtouch = 0;
1844 pm->watertype = 0;
1845 pm->waterlevel = 0;
1846
1847 if ( pm->ps->stats[STAT_HEALTH] <= 0 ) {
1848 pm->tracemask &= ~CONTENTS_BODY; // corpses can fly through bodies
1849 }
1850
1851 // make sure walking button is clear if they are running, to avoid
1852 // proxy no-footsteps cheats
1853 if ( abs( pm->cmd.forwardmove ) > 64 || abs( pm->cmd.rightmove ) > 64 ) {
1854 pm->cmd.buttons &= ~BUTTON_WALKING;
1855 }
1856
1857 // set the talk balloon flag
1858 if ( pm->cmd.buttons & BUTTON_TALK ) {
1859 pm->ps->eFlags |= EF_TALK;
1860 } else {
1861 pm->ps->eFlags &= ~EF_TALK;
1862 }
1863
1864 // set the firing flag for continuous beam weapons
1865 if ( !(pm->ps->pm_flags & PMF_RESPAWNED) && pm->ps->pm_type != PM_INTERMISSION && pm->ps->pm_type != PM_NOCLIP
1866 && ( pm->cmd.buttons & BUTTON_ATTACK ) && pm->ps->ammo[ pm->ps->weapon ] ) {
1867 pm->ps->eFlags |= EF_FIRING;
1868 } else {
1869 pm->ps->eFlags &= ~EF_FIRING;
1870 }
1871
1872 // clear the respawned flag if attack and use are cleared
1873 if ( pm->ps->stats[STAT_HEALTH] > 0 &&
1874 !( pm->cmd.buttons & (BUTTON_ATTACK | BUTTON_USE_HOLDABLE) ) ) {
1875 pm->ps->pm_flags &= ~PMF_RESPAWNED;
1876 }
1877
1878 // if talk button is down, dissallow all other input
1879 // this is to prevent any possible intercept proxy from
1880 // adding fake talk balloons
1881 if ( pmove->cmd.buttons & BUTTON_TALK ) {
1882 // keep the talk button set tho for when the cmd.serverTime > 66 msec
1883 // and the same cmd is used multiple times in Pmove
1884 pmove->cmd.buttons = BUTTON_TALK;
1885 pmove->cmd.forwardmove = 0;
1886 pmove->cmd.rightmove = 0;
1887 pmove->cmd.upmove = 0;
1888 }
1889
1890 // clear all pmove local vars
1891 memset (&pml, 0, sizeof(pml));
1892
1893 // determine the time
1894 pml.msec = pmove->cmd.serverTime - pm->ps->commandTime;
1895 if ( pml.msec < 1 ) {
1896 pml.msec = 1;
1897 } else if ( pml.msec > 200 ) {
1898 pml.msec = 200;
1899 }
1900 pm->ps->commandTime = pmove->cmd.serverTime;
1901
1902 // save old org in case we get stuck
1903 VectorCopy (pm->ps->origin, pml.previous_origin);
1904
1905 // save old velocity for crashlanding
1906 VectorCopy (pm->ps->velocity, pml.previous_velocity);
1907
1908 pml.frametime = pml.msec * 0.001;
1909
1910 // update the viewangles
1911 PM_UpdateViewAngles( pm->ps, &pm->cmd );
1912
1913 AngleVectors (pm->ps->viewangles, pml.forward, pml.right, pml.up);
1914
1915 if ( pm->cmd.upmove < 10 ) {
1916 // not holding jump
1917 pm->ps->pm_flags &= ~PMF_JUMP_HELD;
1918 }
1919
1920 // decide if backpedaling animations should be used
1921 if ( pm->cmd.forwardmove < 0 ) {
1922 pm->ps->pm_flags |= PMF_BACKWARDS_RUN;
1923 } else if ( pm->cmd.forwardmove > 0 || ( pm->cmd.forwardmove == 0 && pm->cmd.rightmove ) ) {
1924 pm->ps->pm_flags &= ~PMF_BACKWARDS_RUN;
1925 }
1926
1927 if ( pm->ps->pm_type >= PM_DEAD ) {
1928 pm->cmd.forwardmove = 0;
1929 pm->cmd.rightmove = 0;
1930 pm->cmd.upmove = 0;
1931 }
1932
1933 if ( pm->ps->pm_type == PM_SPECTATOR ) {
1934 PM_CheckDuck ();
1935 PM_FlyMove ();
1936 PM_DropTimers ();
1937 return;
1938 }
1939
1940 if ( pm->ps->pm_type == PM_NOCLIP ) {
1941 PM_NoclipMove ();
1942 PM_DropTimers ();
1943 return;
1944 }
1945
1946 if (pm->ps->pm_type == PM_FREEZE) {
1947 return; // no movement at all
1948 }
1949
1950 if ( pm->ps->pm_type == PM_INTERMISSION || pm->ps->pm_type == PM_SPINTERMISSION) {
1951 return; // no movement at all
1952 }
1953
1954 // set watertype, and waterlevel
1955 PM_SetWaterLevel();
1956 pml.previous_waterlevel = pmove->waterlevel;
1957
1958 // set mins, maxs, and viewheight
1959 PM_CheckDuck ();
1960
1961 // set groundentity
1962 PM_GroundTrace();
1963
1964 if ( pm->ps->pm_type == PM_DEAD ) {
1965 PM_DeadMove ();
1966 }
1967
1968 PM_DropTimers();
1969
1970 #ifdef MISSIONPACK
1971 if ( pm->ps->powerups[PW_INVULNERABILITY] ) {
1972 PM_InvulnerabilityMove();
1973 } else
1974 #endif
1975 if ( pm->ps->powerups[PW_FLIGHT] ) {
1976 // flight powerup doesn't allow jump and has different friction
1977 PM_FlyMove();
1978 } else if (pm->ps->pm_flags & PMF_GRAPPLE_PULL) {
1979 PM_GrappleMove();
1980 // We can wiggle a bit
1981 PM_AirMove();
1982 } else if (pm->ps->pm_flags & PMF_TIME_WATERJUMP) {
1983 PM_WaterJumpMove();
1984 } else if ( pm->waterlevel > 1 ) {
1985 // swimming
1986 PM_WaterMove();
1987 } else if ( pml.walking ) {
1988 // walking on ground
1989 PM_WalkMove();
1990 } else {
1991 // airborne
1992 PM_AirMove();
1993 }
1994
1995 PM_Animate();
1996
1997 // set groundentity, watertype, and waterlevel
1998 PM_GroundTrace();
1999 PM_SetWaterLevel();
2000
2001 // weapons
2002 PM_Weapon();
2003
2004 // torso animation
2005 PM_TorsoAnimation();
2006
2007 // footstep events / legs animations
2008 PM_Footsteps();
2009
2010 // entering / leaving water splashes
2011 PM_WaterEvents();
2012
2013 // snap some parts of playerstate to save network bandwidth
2014 trap_SnapVector( pm->ps->velocity );
2015 }
2016
2017
2018 /*
2019 ================
2020 Pmove
2021
2022 Can be called by either the server or the client
2023 ================
2024 */
2025 void Pmove (pmove_t *pmove) {
2026 int finalTime;
2027
2028 finalTime = pmove->cmd.serverTime;
2029
2030 if ( finalTime < pmove->ps->commandTime ) {
2031 return; // should not happen
2032 }
2033
2034 if ( finalTime > pmove->ps->commandTime + 1000 ) {
2035 pmove->ps->commandTime = finalTime - 1000;
2036 }
2037
2038 pmove->ps->pmove_framecount = (pmove->ps->pmove_framecount+1) & ((1<<PS_PMOVEFRAMECOUNTBITS)-1);
2039
2040 // chop the move up if it is too long, to prevent framerate
2041 // dependent behavior
2042 while ( pmove->ps->commandTime != finalTime ) {
2043 int msec;
2044
2045 msec = finalTime - pmove->ps->commandTime;
2046
2047 if ( pmove->pmove_fixed ) {
2048 if ( msec > pmove->pmove_msec ) {
2049 msec = pmove->pmove_msec;
2050 }
2051 }
2052 else {
2053 if ( msec > 66 ) {
2054 msec = 66;
2055 }
2056 }
2057 pmove->cmd.serverTime = pmove->ps->commandTime + msec;
2058 PmoveSingle( pmove );
2059
2060 if ( pmove->ps->pm_flags & PMF_JUMP_HELD ) {
2061 pmove->cmd.upmove = 20;
2062 }
2063 }
2064
2065 //PM_CheckStuck();
2066
2067 }
2068