doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Force_Field.cpp
Go to the documentation of this file.
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 #include "../../idlib/precompiled.h"
30 #pragma hdrstop
31 
32 #include "../Game_local.h"
33 
36 
37 /*
38 ================
39 idForce_Field::idForce_Field
40 ================
41 */
44  applyType = FORCEFIELD_APPLY_FORCE;
45  magnitude = 0.0f;
46  dir.Set( 0, 0, 1 );
47  randomTorque = 0.0f;
48  playerOnly = false;
49  monsterOnly = false;
50  clipModel = NULL;
51 }
52 
53 /*
54 ================
55 idForce_Field::~idForce_Field
56 ================
57 */
59  if ( this->clipModel ) {
60  delete this->clipModel;
61  }
62 }
63 
64 /*
65 ================
66 idForce_Field::Save
67 ================
68 */
69 void idForce_Field::Save( idSaveGame *savefile ) const {
70  savefile->WriteInt( type );
71  savefile->WriteInt( applyType);
72  savefile->WriteFloat( magnitude );
73  savefile->WriteVec3( dir );
74  savefile->WriteFloat( randomTorque );
75  savefile->WriteBool( playerOnly );
76  savefile->WriteBool( monsterOnly );
77  savefile->WriteClipModel( clipModel );
78 }
79 
80 /*
81 ================
82 idForce_Field::Restore
83 ================
84 */
86  savefile->ReadInt( (int &)type );
87  savefile->ReadInt( (int &)applyType);
88  savefile->ReadFloat( magnitude );
89  savefile->ReadVec3( dir );
90  savefile->ReadFloat( randomTorque );
91  savefile->ReadBool( playerOnly );
92  savefile->ReadBool( monsterOnly );
93  savefile->ReadClipModel( clipModel );
94 }
95 
96 /*
97 ================
98 idForce_Field::SetClipModel
99 ================
100 */
102  if ( this->clipModel && clipModel != this->clipModel ) {
103  delete this->clipModel;
104  }
105  this->clipModel = clipModel;
106 }
107 
108 /*
109 ================
110 idForce_Field::Uniform
111 ================
112 */
113 void idForce_Field::Uniform( const idVec3 &force ) {
114  dir = force;
115  magnitude = dir.Normalize();
117 }
118 
119 /*
120 ================
121 idForce_Field::Explosion
122 ================
123 */
124 void idForce_Field::Explosion( float force ) {
125  magnitude = force;
127 }
128 
129 /*
130 ================
131 idForce_Field::Implosion
132 ================
133 */
134 void idForce_Field::Implosion( float force ) {
135  magnitude = force;
137 }
138 
139 /*
140 ================
141 idForce_Field::RandomTorque
142 ================
143 */
144 void idForce_Field::RandomTorque( float force ) {
145  randomTorque = force;
146 }
147 
148 /*
149 ================
150 idForce_Field::Evaluate
151 ================
152 */
153 void idForce_Field::Evaluate( int time ) {
154  int numClipModels, i;
155  idBounds bounds;
156  idVec3 force, torque, angularVelocity;
157  idClipModel *cm, *clipModelList[ MAX_GENTITIES ];
158 
159  assert( clipModel );
160 
162  numClipModels = gameLocal.clip.ClipModelsTouchingBounds( bounds, -1, clipModelList, MAX_GENTITIES );
163 
164  for ( i = 0; i < numClipModels; i++ ) {
165  cm = clipModelList[ i ];
166 
167  if ( !cm->IsTraceModel() ) {
168  continue;
169  }
170 
171  idEntity *entity = cm->GetEntity();
172 
173  if ( !entity ) {
174  continue;
175  }
176 
177  idPhysics *physics = entity->GetPhysics();
178 
179  if ( playerOnly ) {
180  if ( !physics->IsType( idPhysics_Player::Type ) ) {
181  continue;
182  }
183  } else if ( monsterOnly ) {
184  if ( !physics->IsType( idPhysics_Monster::Type ) ) {
185  continue;
186  }
187  }
188 
189  if ( !gameLocal.clip.ContentsModel( cm->GetOrigin(), cm, cm->GetAxis(), -1,
191  continue;
192  }
193 
194  switch( type ) {
195  case FORCEFIELD_UNIFORM: {
196  force = dir;
197  break;
198  }
199  case FORCEFIELD_EXPLOSION: {
200  force = cm->GetOrigin() - clipModel->GetOrigin();
201  force.Normalize();
202  break;
203  }
204  case FORCEFIELD_IMPLOSION: {
205  force = clipModel->GetOrigin() - cm->GetOrigin();
206  force.Normalize();
207  break;
208  }
209  default: {
210  gameLocal.Error( "idForce_Field: invalid type" );
211  break;
212  }
213  }
214 
215  if ( randomTorque != 0.0f ) {
216  torque[0] = gameLocal.random.CRandomFloat();
217  torque[1] = gameLocal.random.CRandomFloat();
218  torque[2] = gameLocal.random.CRandomFloat();
219  if ( torque.Normalize() == 0.0f ) {
220  torque[2] = 1.0f;
221  }
222  }
223 
224  switch( applyType ) {
225  case FORCEFIELD_APPLY_FORCE: {
226  if ( randomTorque != 0.0f ) {
227  entity->AddForce( gameLocal.world, cm->GetId(), cm->GetOrigin() + torque.Cross( dir ) * randomTorque, dir * magnitude );
228  }
229  else {
230  entity->AddForce( gameLocal.world, cm->GetId(), cm->GetOrigin(), force * magnitude );
231  }
232  break;
233  }
235  physics->SetLinearVelocity( force * magnitude, cm->GetId() );
236  if ( randomTorque != 0.0f ) {
237  angularVelocity = physics->GetAngularVelocity( cm->GetId() );
238  physics->SetAngularVelocity( 0.5f * (angularVelocity + torque * randomTorque), cm->GetId() );
239  }
240  break;
241  }
243  if ( randomTorque != 0.0f ) {
244  entity->ApplyImpulse( gameLocal.world, cm->GetId(), cm->GetOrigin() + torque.Cross( dir ) * randomTorque, dir * magnitude );
245  }
246  else {
247  entity->ApplyImpulse( gameLocal.world, cm->GetId(), cm->GetOrigin(), force * magnitude );
248  }
249  break;
250  }
251  default: {
252  gameLocal.Error( "idForce_Field: invalid apply type" );
253  break;
254  }
255  }
256  }
257 }
int ClipModelsTouchingBounds(const idBounds &bounds, int contentMask, idClipModel **clipModelList, int maxCount) const
Definition: Clip.cpp:804
idEntity * GetEntity(void) const
Definition: Clip.h:178
void Restore(idRestoreGame *savefile)
Definition: Force_Field.cpp:85
void SetClipModel(idClipModel *clipModel)
float Normalize(void)
Definition: Vector.h:646
assert(prefInfo.fullscreenBtn)
idClip clip
Definition: Game_local.h:296
#define MAX_GENTITIES
Definition: Game_local.h:83
forceFieldApplyType applyType
Definition: Force_Field.h:85
void RandomTorque(float force)
Definition: Force.h:45
void Uniform(const idVec3 &force)
idRandom random
Definition: Game_local.h:291
void void void void void Error(const char *fmt,...) const id_attribute((format(printf
Definition: Game_local.cpp:783
bool IsType(const idTypeInfo &c) const
Definition: Class.h:337
const idMat3 & GetAxis(void) const
Definition: Clip.h:210
Definition: Vector.h:316
void ReadBool(bool &value)
Definition: SaveGame.cpp:976
virtual void SetLinearVelocity(const idVec3 &newLinearVelocity, int id=0)=0
const idVec3 & GetOrigin(void) const
Definition: Clip.h:206
GLuint GLuint GLsizei GLenum type
Definition: glext.h:2845
virtual void ApplyImpulse(idEntity *ent, int id, const idVec3 &point, const idVec3 &impulse)
Definition: Entity.cpp:2887
idVec3 Cross(const idVec3 &a) const
Definition: Vector.h:619
void WriteClipModel(const class idClipModel *clipModel)
Definition: SaveGame.cpp:744
int i
Definition: process.py:33
idClipModel * clipModel
Definition: Force_Field.h:91
void WriteVec3(const idVec3 &vec)
Definition: SaveGame.cpp:253
void Explosion(float force)
void WriteBool(const bool value)
Definition: SaveGame.cpp:222
idPhysics * GetPhysics(void) const
Definition: Entity.cpp:2607
void ReadFloat(float &value)
Definition: SaveGame.cpp:967
idWorldspawn * world
Definition: Game_local.h:280
void WriteFloat(const float value)
Definition: SaveGame.cpp:213
cmHandle_t Handle(void) const
Definition: Clip.cpp:457
#define NULL
Definition: Lib.h:88
const idBounds & GetBounds(void) const
Definition: Clip.h:198
float randomTorque
Definition: Force_Field.h:88
virtual const idVec3 & GetAngularVelocity(int id=0) const =0
virtual void Evaluate(int time)
bool IsTraceModel(void) const
Definition: Clip.h:218
int GetId(void) const
Definition: Clip.h:186
void FromTransformedBounds(const idBounds &bounds, const idVec3 &origin, const idMat3 &axis)
Definition: Bounds.cpp:232
idGameLocal gameLocal
Definition: Game_local.cpp:64
#define END_CLASS
Definition: Class.h:54
void Implosion(float force)
void WriteInt(const int value)
Definition: SaveGame.cpp:168
bool monsterOnly
Definition: Force_Field.h:90
void ReadClipModel(idClipModel *&clipModel)
Definition: SaveGame.cpp:1519
tuple f
Definition: idal.py:89
virtual void AddForce(idEntity *ent, int id, const idVec3 &point, const idVec3 &force)
Definition: Entity.cpp:2896
virtual ~idForce_Field(void)
Definition: Force_Field.cpp:58
float magnitude
Definition: Force_Field.h:86
int ContentsModel(const idVec3 &start, const idClipModel *mdl, const idMat3 &trmAxis, int contentMask, cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis)
Definition: Clip.cpp:1532
#define CLASS_DECLARATION(nameofsuperclass, nameofclass)
Definition: Class.h:110
virtual void SetAngularVelocity(const idVec3 &newAngularVelocity, int id=0)=0
void ReadVec3(idVec3 &vec)
Definition: SaveGame.cpp:1011
void Save(idSaveGame *savefile) const
Definition: Force_Field.cpp:69
void ReadInt(int &value)
Definition: SaveGame.cpp:922
float CRandomFloat(void)
Definition: Random.h:86