Force_Field.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../../idlib/precompiled.h"
  22. #include "../Game_local.h"
  23. CLASS_DECLARATION( idForce, idForce_Field )
  24. END_CLASS
  25. /*
  26. ================
  27. idForce_Field::idForce_Field
  28. ================
  29. */
  30. idForce_Field::idForce_Field() {
  31. type = FORCEFIELD_UNIFORM;
  32. applyType = FORCEFIELD_APPLY_FORCE;
  33. magnitude = 0.0f;
  34. dir.Set( 0, 0, 1 );
  35. randomTorque = 0.0f;
  36. playerOnly = false;
  37. monsterOnly = false;
  38. clipModel = NULL;
  39. }
  40. /*
  41. ================
  42. idForce_Field::~idForce_Field
  43. ================
  44. */
  45. idForce_Field::~idForce_Field() {
  46. if ( this->clipModel ) {
  47. delete this->clipModel;
  48. }
  49. }
  50. /*
  51. ================
  52. idForce_Field::Save
  53. ================
  54. */
  55. void idForce_Field::Save( idSaveGame *savefile ) const {
  56. savefile->WriteInt( type );
  57. savefile->WriteInt( applyType);
  58. savefile->WriteFloat( magnitude );
  59. savefile->WriteVec3( dir );
  60. savefile->WriteFloat( randomTorque );
  61. savefile->WriteBool( playerOnly );
  62. savefile->WriteBool( monsterOnly );
  63. savefile->WriteClipModel( clipModel );
  64. }
  65. /*
  66. ================
  67. idForce_Field::Restore
  68. ================
  69. */
  70. void idForce_Field::Restore( idRestoreGame *savefile ) {
  71. savefile->ReadInt( (int &)type );
  72. savefile->ReadInt( (int &)applyType);
  73. savefile->ReadFloat( magnitude );
  74. savefile->ReadVec3( dir );
  75. savefile->ReadFloat( randomTorque );
  76. savefile->ReadBool( playerOnly );
  77. savefile->ReadBool( monsterOnly );
  78. savefile->ReadClipModel( clipModel );
  79. }
  80. /*
  81. ================
  82. idForce_Field::SetClipModel
  83. ================
  84. */
  85. void idForce_Field::SetClipModel( idClipModel *clipModel ) {
  86. if ( this->clipModel && clipModel != this->clipModel ) {
  87. delete this->clipModel;
  88. }
  89. this->clipModel = clipModel;
  90. }
  91. /*
  92. ================
  93. idForce_Field::Uniform
  94. ================
  95. */
  96. void idForce_Field::Uniform( const idVec3 &force ) {
  97. dir = force;
  98. magnitude = dir.Normalize();
  99. type = FORCEFIELD_UNIFORM;
  100. }
  101. /*
  102. ================
  103. idForce_Field::Explosion
  104. ================
  105. */
  106. void idForce_Field::Explosion( float force ) {
  107. magnitude = force;
  108. type = FORCEFIELD_EXPLOSION;
  109. }
  110. /*
  111. ================
  112. idForce_Field::Implosion
  113. ================
  114. */
  115. void idForce_Field::Implosion( float force ) {
  116. magnitude = force;
  117. type = FORCEFIELD_IMPLOSION;
  118. }
  119. /*
  120. ================
  121. idForce_Field::RandomTorque
  122. ================
  123. */
  124. void idForce_Field::RandomTorque( float force ) {
  125. randomTorque = force;
  126. }
  127. /*
  128. ================
  129. idForce_Field::Evaluate
  130. ================
  131. */
  132. void idForce_Field::Evaluate( int time ) {
  133. int numClipModels, i;
  134. idBounds bounds;
  135. idVec3 force, torque, angularVelocity;
  136. idClipModel *cm, *clipModelList[ MAX_GENTITIES ];
  137. assert( clipModel );
  138. bounds.FromTransformedBounds( clipModel->GetBounds(), clipModel->GetOrigin(), clipModel->GetAxis() );
  139. numClipModels = gameLocal.clip.ClipModelsTouchingBounds( bounds, -1, clipModelList, MAX_GENTITIES );
  140. for ( i = 0; i < numClipModels; i++ ) {
  141. cm = clipModelList[ i ];
  142. if ( !cm->IsTraceModel() ) {
  143. continue;
  144. }
  145. idEntity *entity = cm->GetEntity();
  146. if ( !entity ) {
  147. continue;
  148. }
  149. idPhysics *physics = entity->GetPhysics();
  150. if ( playerOnly ) {
  151. if ( !physics->IsType( idPhysics_Player::Type ) ) {
  152. continue;
  153. }
  154. } else if ( monsterOnly ) {
  155. if ( !physics->IsType( idPhysics_Monster::Type ) ) {
  156. continue;
  157. }
  158. }
  159. if ( !gameLocal.clip.ContentsModel( cm->GetOrigin(), cm, cm->GetAxis(), -1,
  160. clipModel->Handle(), clipModel->GetOrigin(), clipModel->GetAxis() ) ) {
  161. continue;
  162. }
  163. switch( type ) {
  164. case FORCEFIELD_UNIFORM: {
  165. force = dir;
  166. break;
  167. }
  168. case FORCEFIELD_EXPLOSION: {
  169. force = cm->GetOrigin() - clipModel->GetOrigin();
  170. force.Normalize();
  171. break;
  172. }
  173. case FORCEFIELD_IMPLOSION: {
  174. force = clipModel->GetOrigin() - cm->GetOrigin();
  175. force.Normalize();
  176. break;
  177. }
  178. default: {
  179. gameLocal.Error( "idForce_Field: invalid type" );
  180. break;
  181. }
  182. }
  183. if ( randomTorque != 0.0f ) {
  184. torque[0] = gameLocal.random.CRandomFloat();
  185. torque[1] = gameLocal.random.CRandomFloat();
  186. torque[2] = gameLocal.random.CRandomFloat();
  187. if ( torque.Normalize() == 0.0f ) {
  188. torque[2] = 1.0f;
  189. }
  190. }
  191. switch( applyType ) {
  192. case FORCEFIELD_APPLY_FORCE: {
  193. if ( randomTorque != 0.0f ) {
  194. entity->AddForce( gameLocal.world, cm->GetId(), cm->GetOrigin() + torque.Cross( dir ) * randomTorque, dir * magnitude );
  195. }
  196. else {
  197. entity->AddForce( gameLocal.world, cm->GetId(), cm->GetOrigin(), force * magnitude );
  198. }
  199. break;
  200. }
  201. case FORCEFIELD_APPLY_VELOCITY: {
  202. physics->SetLinearVelocity( force * magnitude, cm->GetId() );
  203. if ( randomTorque != 0.0f ) {
  204. angularVelocity = physics->GetAngularVelocity( cm->GetId() );
  205. physics->SetAngularVelocity( 0.5f * (angularVelocity + torque * randomTorque), cm->GetId() );
  206. }
  207. break;
  208. }
  209. case FORCEFIELD_APPLY_IMPULSE: {
  210. if ( randomTorque != 0.0f ) {
  211. entity->ApplyImpulse( gameLocal.world, cm->GetId(), cm->GetOrigin() + torque.Cross( dir ) * randomTorque, dir * magnitude );
  212. }
  213. else {
  214. entity->ApplyImpulse( gameLocal.world, cm->GetId(), cm->GetOrigin(), force * magnitude );
  215. }
  216. break;
  217. }
  218. default: {
  219. gameLocal.Error( "idForce_Field: invalid apply type" );
  220. break;
  221. }
  222. }
  223. }
  224. }