DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Physics_Player.h (6994B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #ifndef __PHYSICS_PLAYER_H__
     30 #define __PHYSICS_PLAYER_H__
     31 
     32 /*
     33 ===================================================================================
     34 
     35 	Player physics
     36 
     37 	Simulates the motion of a player through the environment. Input from the
     38 	player is used to allow a certain degree of control over the motion.
     39 
     40 ===================================================================================
     41 */
     42 
     43 // movementType
     44 typedef enum {
     45 	PM_NORMAL,				// normal physics
     46 	PM_DEAD,				// no acceleration or turning, but free falling
     47 	PM_SPECTATOR,			// flying without gravity but with collision detection
     48 	PM_FREEZE,				// stuck in place without control
     49 	PM_NOCLIP				// flying without collision detection nor gravity
     50 } pmtype_t;
     51 
     52 typedef enum {
     53 	WATERLEVEL_NONE,
     54 	WATERLEVEL_FEET,
     55 	WATERLEVEL_WAIST,
     56 	WATERLEVEL_HEAD
     57 } waterLevel_t;
     58 
     59 #define	MAXTOUCH					32
     60 
     61 typedef struct playerPState_s {
     62 	idVec3					origin;
     63 	idVec3					velocity;
     64 	idVec3					localOrigin;
     65 	idVec3					pushVelocity;
     66 	float					stepUp;
     67 	int						movementType;
     68 	int						movementFlags;
     69 	int						movementTime;
     70 
     71 	playerPState_s() :
     72 		origin( vec3_zero ),
     73 		velocity( vec3_zero ),
     74 		localOrigin( vec3_zero ),
     75 		pushVelocity( vec3_zero ),
     76 		stepUp( 0.0f ),
     77 		movementType( 0 ),
     78 		movementFlags( 0 ),
     79 		movementTime( 0 ) {
     80 	}
     81 } playerPState_t;
     82 
     83 class idPhysics_Player : public idPhysics_Actor {
     84 
     85 public:
     86 	CLASS_PROTOTYPE( idPhysics_Player );
     87 
     88 							idPhysics_Player();
     89 
     90 	void					Save( idSaveGame *savefile ) const;
     91 	void					Restore( idRestoreGame *savefile );
     92 
     93 							// initialisation
     94 	void					SetSpeed( const float newWalkSpeed, const float newCrouchSpeed );
     95 	void					SetMaxStepHeight( const float newMaxStepHeight );
     96 	float					GetMaxStepHeight() const;
     97 	void					SetMaxJumpHeight( const float newMaxJumpHeight );
     98 	void					SetMovementType( const pmtype_t type );
     99 	void					SetPlayerInput( const usercmd_t &cmd, const idVec3 &forwardVector );
    100 	void					SetKnockBack( const int knockBackTime );
    101 	void					SetDebugLevel( bool set );
    102 							// feed back from last physics frame
    103 	waterLevel_t			GetWaterLevel() const;
    104 	int						GetWaterType() const;
    105 	bool					HasJumped() const;
    106 	bool					HasSteppedUp() const;
    107 	float					GetStepUp() const;
    108 	bool					IsCrouching() const;
    109 	bool					OnLadder() const;
    110 	const idVec3 &			PlayerGetOrigin() const;	// != GetOrigin
    111 
    112 public:	// common physics interface
    113 	bool					Evaluate( int timeStepMSec, int endTimeMSec );
    114 	bool					Interpolate( const float fraction );
    115 	void					UpdateTime( int endTimeMSec );
    116 	int						GetTime() const;
    117 
    118 	void					GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const;
    119 	void					ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse );
    120 	bool					IsAtRest() const;
    121 	int						GetRestStartTime() const;
    122 
    123 	void					SaveState();
    124 	void					RestoreState();
    125 
    126 	void					SetOrigin( const idVec3 &newOrigin, int id = -1 );
    127 	void					SetAxis( const idMat3 &newAxis, int id = -1 );
    128 
    129 	void					Translate( const idVec3 &translation, int id = -1 );
    130 	void					Rotate( const idRotation &rotation, int id = -1 );
    131 
    132 	void					SetLinearVelocity( const idVec3 &newLinearVelocity, int id = 0 );
    133 
    134 	const idVec3 &			GetLinearVelocity( int id = 0 ) const;
    135 
    136 	bool					ClientPusherLocked( bool & justBecameUnlocked );
    137 	void					SetPushed( int deltaTime );
    138 	void					SetPushedWithAbnormalVelocityHack( int deltaTime );
    139 	const idVec3 &			GetPushedLinearVelocity( const int id = 0 ) const;
    140 	void					ClearPushedVelocity();
    141 
    142 	void					SetMaster( idEntity *master, const bool orientated = true );
    143 
    144 	void					WriteToSnapshot( idBitMsg &msg ) const;
    145 	void					ReadFromSnapshot( const idBitMsg &msg );
    146 
    147 	void					SnapToNextState() { current = next; previous = current; }
    148 
    149 private:
    150 	// player physics state
    151 	playerPState_t			current;
    152 	playerPState_t			saved;
    153 
    154 	// physics state for client interpolation
    155 	playerPState_t			previous;
    156 	playerPState_t			next;
    157 
    158 	// properties
    159 	float					walkSpeed;
    160 	float					crouchSpeed;
    161 	float					maxStepHeight;
    162 	float					maxJumpHeight;
    163 	int						debugLevel;				// if set, diagnostic output will be printed
    164 
    165 	// player input
    166 	usercmd_t				command;
    167 	idVec3					commandForward;		// can't use cmd.angles cause of the delta_angles and head tracking
    168 
    169 	// run-time variables
    170 	int						framemsec;
    171 	float					frametime;
    172 	float					playerSpeed;
    173 	idVec3					viewForward;
    174 	idVec3					viewRight;
    175 
    176 	// walk movement
    177 	bool					walking;
    178 	bool					groundPlane;
    179 	trace_t					groundTrace;
    180 	const idMaterial *		groundMaterial;
    181 
    182 	// ladder movement
    183 	bool					ladder;
    184 	idVec3					ladderNormal;
    185 
    186 	// results of last evaluate
    187 	waterLevel_t			waterLevel;
    188 	int						waterType;
    189 
    190 	bool					clientPusherLocked;
    191 
    192 private:
    193 	float					CmdScale( const usercmd_t &cmd ) const;
    194 	void					Accelerate( const idVec3 &wishdir, const float wishspeed, const float accel );
    195 	bool					SlideMove( bool gravity, bool stepUp, bool stepDown, bool push );
    196 	void					Friction();
    197 	void					WaterJumpMove();
    198 	void					WaterMove();
    199 	void					FlyMove();
    200 	void					AirMove();
    201 	void					WalkMove();
    202 	void					DeadMove();
    203 	void					NoclipMove();
    204 	void					SpectatorMove();
    205 	void					LadderMove();
    206 	void					CorrectAllSolid( trace_t &trace, int contents );
    207 	void					CheckGround();
    208 	void					CheckDuck();
    209 	void					CheckLadder();
    210 	bool					CheckJump();
    211 	bool					CheckWaterJump();
    212 	void					SetWaterLevel();
    213 	void					DropTimers();
    214 	void					MovePlayer( int msec );
    215 };
    216 
    217 #endif /* !__PHYSICS_PLAYER_H__ */