1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8 
9 Doom 3 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 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 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 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 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_MONSTER_H__
30 #define __PHYSICS_MONSTER_H__
31 
32 #include "idlib/math/Vector.h"
33 
34 #include "physics/Physics_Actor.h"
35 
36 /*
37 ===================================================================================
38 
39 	Monster physics
40 
41 	Simulates the motion of a monster through the environment. The monster motion
42 	is typically driven by animations.
43 
44 ===================================================================================
45 */
46 
47 typedef enum {
48 	MM_OK,
49 	MM_SLIDING,
50 	MM_BLOCKED,
51 	MM_STEPPED,
52 	MM_FALLING
53 } monsterMoveResult_t;
54 
55 typedef struct monsterPState_s {
56 	int						atRest;
57 	bool					onGround;
58 	idVec3					origin;
59 	idVec3					velocity;
60 	idVec3					localOrigin;
61 	idVec3					pushVelocity;
62 } monsterPState_t;
63 
64 class idPhysics_Monster : public idPhysics_Actor {
65 
66 public:
67 	CLASS_PROTOTYPE( idPhysics_Monster );
68 
69 							idPhysics_Monster( void );
70 
71 	void					Save( idSaveGame *savefile ) const;
72 	void					Restore( idRestoreGame *savefile );
73 
74 							// maximum step up the monster can take, default 18 units
75 	void					SetMaxStepHeight( const float newMaxStepHeight );
76 	float					GetMaxStepHeight( void ) const;
77 							// minimum cosine of floor angle to be able to stand on the floor
78 	void					SetMinFloorCosine( const float newMinFloorCosine );
79 							// set delta for next move
80 	void					SetDelta( const idVec3 &d );
81 							// returns true if monster is standing on the ground
82 	bool					OnGround( void ) const;
83 							// returns the movement result
84 	monsterMoveResult_t		GetMoveResult( void ) const;
85 							// overrides any velocity for pure delta movement
86 	void					ForceDeltaMove( bool force );
87 							// whether velocity should be affected by gravity
88 	void					UseFlyMove( bool force );
89 							// don't use delta movement
90 	void					UseVelocityMove( bool force );
91 							// get entity blocking the move
92 	idEntity *				GetSlideMoveEntity( void ) const;
93 							// enable/disable activation by impact
94 	void					EnableImpact( void );
95 	void					DisableImpact( void );
96 
97 public:	// common physics interface
98 	bool					Evaluate( int timeStepMSec, int endTimeMSec );
99 	void					UpdateTime( int endTimeMSec );
100 	int						GetTime( void ) const;
101 
102 	void					GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const;
103 	void					ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse );
104 	void					Activate( void );
105 	void					PutToRest( void );
106 	bool					IsAtRest( void ) const;
107 	int						GetRestStartTime( void ) const;
108 
109 	void					SaveState( void );
110 	void					RestoreState( void );
111 
112 	void					SetOrigin( const idVec3 &newOrigin, int id = -1 );
113 	void					SetAxis( const idMat3 &newAxis, int id = -1 );
114 
115 	void					Translate( const idVec3 &translation, int id = -1 );
116 	void					Rotate( const idRotation &rotation, int id = -1 );
117 
118 	void					SetLinearVelocity( const idVec3 &newLinearVelocity, int id = 0 );
119 
120 	const idVec3 &			GetLinearVelocity( int id = 0 ) const;
121 
122 	void					SetPushed( int deltaTime );
123 	const idVec3 &			GetPushedLinearVelocity( const int id = 0 ) const;
124 
125 	void					SetMaster( idEntity *master, const bool orientated = true );
126 
127 	void					WriteToSnapshot( idBitMsgDelta &msg ) const;
128 	void					ReadFromSnapshot( const idBitMsgDelta &msg );
129 
130 private:
131 	// monster physics state
132 	monsterPState_t			current;
133 	monsterPState_t			saved;
134 
135 	// properties
136 	float					maxStepHeight;		// maximum step height
137 	float					minFloorCosine;		// minimum cosine of floor angle
138 	idVec3					delta;				// delta for next move
139 
140 	bool					forceDeltaMove;
141 	bool					fly;
142 	bool					useVelocityMove;
143 	bool					noImpact;			// if true do not activate when another object collides
144 
145 	// results of last evaluate
146 	monsterMoveResult_t		moveResult;
147 	idEntity *				blockingEntity;
148 
149 private:
150 	void					CheckGround( monsterPState_t &state );
151 	monsterMoveResult_t		SlideMove( idVec3 &start, idVec3 &velocity, const idVec3 &delta );
152 	monsterMoveResult_t		StepMove( idVec3 &start, idVec3 &velocity, const idVec3 &delta );
153 	void					Rest( void );
154 };
155 
156 #endif /* !__PHYSICS_MONSTER_H__ */
157