Debian Codegraph @

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