explosion.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/explosion.cpp $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 1/15/02 2:07p $*
  29. * *
  30. * $Revision:: 69 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "explosion.h"
  36. #include "debug.h"
  37. #include "damage.h"
  38. #include "combat.h"
  39. #include "sound3d.h"
  40. #include "combatchunkid.h"
  41. #include "simpledefinitionfactory.h"
  42. #include "persistfactory.h"
  43. #include "timeddecophys.h"
  44. #include "wwaudio.h"
  45. #include "gameobjmanager.h"
  46. #include "physicalgameobj.h"
  47. #include "crandom.h"
  48. #include "assets.h"
  49. #include "htree.h"
  50. #include "hanim.h"
  51. #include "smartgameobj.h"
  52. #include "building.h"
  53. #include "scexplosionevent.h"
  54. /*
  55. ** ExplosionDefinitionClass
  56. */
  57. SimplePersistFactoryClass<ExplosionDefinitionClass, CHUNKID_EXPLOSION_DEF> _ExplosionDefPersistFactory;
  58. DECLARE_DEFINITION_FACTORY(ExplosionDefinitionClass, CLASSID_DEF_EXPLOSION, "Explosion") _ExplosionDefDefFactory;
  59. uint32 ExplosionDefinitionClass::Get_Class_ID (void) const { return CLASSID_DEF_EXPLOSION; }
  60. const PersistFactoryClass & ExplosionDefinitionClass::Get_Factory (void) const { return _ExplosionDefPersistFactory; }
  61. ExplosionDefinitionClass::ExplosionDefinitionClass( void ) :
  62. PhysDefID( 0 ),
  63. SoundDefID( 0 ),
  64. DamageRadius( 0 ),
  65. DamageStrength( 0 ),
  66. DamageWarhead( 0 ),
  67. DamageIsScaled( true ),
  68. DecalSize( 10 ),
  69. AnimatedExplosion( true ),
  70. CameraShakeIntensity( 0.0f ),
  71. CameraShakeRadius( 25.0f ),
  72. CameraShakeDuration( 1.5f )
  73. {
  74. #ifdef PARAM_EDITING_ON
  75. MODEL_DEF_PARAM( ExplosionDefinitionClass, PhysDefID, "TimedDecorationPhysDef" );
  76. EDITABLE_PARAM( ExplosionDefinitionClass, ParameterClass::TYPE_SOUNDDEFINITIONID, SoundDefID );
  77. EDITABLE_PARAM( ExplosionDefinitionClass, ParameterClass::TYPE_FLOAT, DamageRadius );
  78. EDITABLE_PARAM( ExplosionDefinitionClass, ParameterClass::TYPE_FLOAT, DamageStrength );
  79. // EDITABLE_PARAM( ExplosionDefinitionClass, ParameterClass::TYPE_INT, DamageWarhead );
  80. EnumParameterClass *param;
  81. param = new EnumParameterClass( &DamageWarhead );
  82. param->Set_Name ( "Warhead" );
  83. for ( int i = 0; i < ArmorWarheadManager::Get_Num_Warhead_Types(); i++ ) {
  84. param->Add_Value ( ArmorWarheadManager::Get_Warhead_Name( i ), i );
  85. }
  86. GENERIC_EDITABLE_PARAM(ExplosionDefinitionClass,param)
  87. EDITABLE_PARAM( ExplosionDefinitionClass, ParameterClass::TYPE_BOOL, DamageIsScaled );
  88. EDITABLE_PARAM( ExplosionDefinitionClass, ParameterClass::TYPE_FILENAME, DecalFilename );
  89. EDITABLE_PARAM( ExplosionDefinitionClass, ParameterClass::TYPE_FLOAT, DecalSize );
  90. EDITABLE_PARAM( ExplosionDefinitionClass, ParameterClass::TYPE_BOOL, AnimatedExplosion );
  91. FLOAT_EDITABLE_PARAM( ExplosionDefinitionClass, CameraShakeIntensity, 0.0f, 1.0f);
  92. FLOAT_UNITS_PARAM( ExplosionDefinitionClass, CameraShakeRadius, 0.01f, 1000.0f, "meters");
  93. FLOAT_UNITS_PARAM( ExplosionDefinitionClass, CameraShakeDuration, 0.01f, 60.0f, "seconds");
  94. #endif //PARAM_EDITING_ON
  95. }
  96. /*
  97. **
  98. */
  99. enum {
  100. CHUNKID_EXPLOSION_DEF_VARIABLES = 0317001525,
  101. CHUNKID_EXPLOSION_DEF_PARENT,
  102. MICROCHUNKID_EXPLOSION_DEF_PHYS_DEF_ID = 1,
  103. MICROCHUNKID_EXPLOSION_DEF_SOUND_DEF_ID,
  104. MICROCHUNKID_EXPLOSION_DEF_DAMAGE_RADIUS,
  105. MICROCHUNKID_EXPLOSION_DEF_DAMAGE_STRENGTH,
  106. MICROCHUNKID_EXPLOSION_DEF_DAMAGE_WARHEAD,
  107. MICROCHUNKID_EXPLOSION_DEF_DAMAGE_IS_SCALED,
  108. MICROCHUNKID_EXPLOSION_DEF_DECAL_FILENAME,
  109. MICROCHUNKID_EXPLOSION_DEF_DECAL_SIZE,
  110. XXXMICROCHUNKID_EXPLOSION_DEF_ANIMATION,
  111. MICROCHUNKID_EXPLOSION_DEF_ANIMATED_EXPLOLSION,
  112. MICROCHUNKID_EXPLOSION_DEF_CAMERASHAKEINTENSITY,
  113. MICROCHUNKID_EXPLOSION_DEF_CAMERASHAKERADIUS,
  114. MICROCHUNKID_EXPLOSION_DEF_CAMERASHAKEDURATION,
  115. };
  116. bool ExplosionDefinitionClass::Save( ChunkSaveClass & csave )
  117. {
  118. csave.Begin_Chunk( CHUNKID_EXPLOSION_DEF_PARENT );
  119. DefinitionClass::Save( csave );
  120. csave.End_Chunk();
  121. csave.Begin_Chunk( CHUNKID_EXPLOSION_DEF_VARIABLES );
  122. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_PHYS_DEF_ID, PhysDefID );
  123. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_SOUND_DEF_ID, SoundDefID );
  124. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_DAMAGE_RADIUS, DamageRadius )
  125. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_DAMAGE_STRENGTH, DamageStrength );
  126. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_DAMAGE_WARHEAD, DamageWarhead );
  127. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_DAMAGE_IS_SCALED, DamageIsScaled );
  128. WRITE_MICRO_CHUNK_WWSTRING( csave, MICROCHUNKID_EXPLOSION_DEF_DECAL_FILENAME, DecalFilename );
  129. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_DECAL_SIZE, DecalSize );
  130. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_ANIMATED_EXPLOLSION, AnimatedExplosion );
  131. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_CAMERASHAKEINTENSITY, CameraShakeIntensity);
  132. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_CAMERASHAKERADIUS, CameraShakeRadius);
  133. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_EXPLOSION_DEF_CAMERASHAKEDURATION, CameraShakeDuration);
  134. csave.End_Chunk();
  135. return true;
  136. }
  137. bool ExplosionDefinitionClass::Load( ChunkLoadClass &cload )
  138. {
  139. while (cload.Open_Chunk()) {
  140. switch(cload.Cur_Chunk_ID()) {
  141. case CHUNKID_EXPLOSION_DEF_PARENT:
  142. DefinitionClass::Load( cload );
  143. break;
  144. case CHUNKID_EXPLOSION_DEF_VARIABLES:
  145. while (cload.Open_Micro_Chunk()) {
  146. switch(cload.Cur_Micro_Chunk_ID()) {
  147. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_PHYS_DEF_ID, PhysDefID );
  148. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_SOUND_DEF_ID, SoundDefID );
  149. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_DAMAGE_RADIUS, DamageRadius )
  150. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_DAMAGE_STRENGTH, DamageStrength );
  151. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_DAMAGE_WARHEAD, DamageWarhead );
  152. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_DAMAGE_IS_SCALED, DamageIsScaled );
  153. READ_MICRO_CHUNK_WWSTRING( cload, MICROCHUNKID_EXPLOSION_DEF_DECAL_FILENAME, DecalFilename );
  154. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_DECAL_SIZE, DecalSize );
  155. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_ANIMATED_EXPLOLSION, AnimatedExplosion );
  156. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_CAMERASHAKEINTENSITY, CameraShakeIntensity);
  157. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_CAMERASHAKERADIUS, CameraShakeRadius);
  158. READ_MICRO_CHUNK( cload, MICROCHUNKID_EXPLOSION_DEF_CAMERASHAKEDURATION, CameraShakeDuration);
  159. default:
  160. Debug_Say(( "Unrecognized ExplosionDef Variable chunkID\n" ));
  161. break;
  162. }
  163. cload.Close_Micro_Chunk();
  164. }
  165. break;
  166. default:
  167. Debug_Say(( "Unrecognized ExplosionDef chunkID\n" ));
  168. break;
  169. }
  170. cload.Close_Chunk();
  171. }
  172. return true;
  173. }
  174. /*
  175. **
  176. */
  177. void ExplosionManager::Create_Explosion_At( int explosion_def_id, const Vector3 & pos, ArmedGameObj * damager, const Vector3 & blast_direction, DamageableGameObj * force_victim )
  178. {
  179. Matrix3D up_tm(1);
  180. up_tm.Set_Translation( pos );
  181. Create_Explosion_At( explosion_def_id, up_tm, damager, blast_direction, force_victim );
  182. }
  183. void ExplosionManager::Create_Explosion_At( int explosion_def_id, const Matrix3D & up_tm, ArmedGameObj * damager, const Vector3 & blast_direction, DamageableGameObj * force_victim )
  184. {
  185. // Make a camera convention transform pointing in the blast direction
  186. Vector3 pos = up_tm.Get_Translation();
  187. #ifdef WWDEBUG
  188. if (blast_direction.Length2() < WWMATH_EPSILON) {
  189. WWDEBUG_SAY(("ExplosionManager::Create_Explosion_At - Invalid Blast Direction! %f %f %f\r\n",blast_direction.X,blast_direction.Y,blast_direction.Z));
  190. }
  191. #endif
  192. Matrix3D blast_tm;
  193. blast_tm.Look_At( pos, pos + blast_direction, FreeRandom.Get_Float( 0, DEG_TO_RADF( 360 ) ) );
  194. // Find Explosion Def
  195. ExplosionDefinitionClass * explosion_def = (ExplosionDefinitionClass *)DefinitionMgrClass::Find_Definition( explosion_def_id );
  196. if ( explosion_def == NULL ) {
  197. Debug_Say(( "Explosion Def %d not found\n", explosion_def_id ));
  198. return;
  199. }
  200. WWASSERT( explosion_def );
  201. // Make the Phys Object
  202. if ( explosion_def->PhysDefID != 0 ) {
  203. //if ( explosion_def->PhysDefID != 0 ) {
  204. PhysDefClass * phys_def = (PhysDefClass *)DefinitionMgrClass::Find_Definition( explosion_def->PhysDefID );
  205. if ( phys_def != NULL ) {
  206. WWASSERT( phys_def );
  207. WWASSERT( phys_def->Is_Type( "TimedDecorationPhysDef" ) );
  208. TimedDecorationPhysClass * explosion = (TimedDecorationPhysClass *)phys_def->Create();
  209. if ( explosion ) {
  210. RenderObjClass * model = explosion->Peek_Model();
  211. WWASSERT(model != NULL);
  212. if (model != NULL) {
  213. explosion->Set_Transform( up_tm );
  214. if (model->Get_HTree() != NULL && explosion_def->AnimatedExplosion) {
  215. // Auto play an explosion anim if we find it
  216. StringClass exp_anim_name;
  217. exp_anim_name.Format( "%s.%s",
  218. model->Get_HTree()->Get_Name(),
  219. model->Get_HTree()->Get_Name() );
  220. WWASSERT(WW3DAssetManager::Get_Instance() != NULL);
  221. HAnimClass * anim = WW3DAssetManager::Get_Instance()->Get_HAnim( exp_anim_name );
  222. if ( anim != NULL ) {
  223. model->Set_Animation( anim, 0, RenderObjClass::ANIM_MODE_ONCE );
  224. anim->Release_Ref();
  225. }
  226. }
  227. WWASSERT(COMBAT_SCENE != NULL);
  228. COMBAT_SCENE->Add_Dynamic_Object( explosion );
  229. }
  230. explosion->Release_Ref();
  231. }
  232. }
  233. }
  234. // Make the decal
  235. if ( !explosion_def->DecalFilename.Is_Empty() ) {
  236. StringClass new_name(true);
  237. ::Strip_Path_From_Filename( new_name, explosion_def->DecalFilename );
  238. PhysicsSceneClass::Get_Instance()->Create_Decal( blast_tm, new_name,
  239. explosion_def->DecalSize, false, NULL );
  240. }
  241. // Apply the damage
  242. // if ( CombatManager::I_Am_Server() ) Clients can create damage now...
  243. {
  244. float radius = explosion_def->DamageRadius;
  245. WWASSERT(WWMath::Is_Valid_Float(radius));
  246. if (radius > 0.0f) {
  247. WWASSERT(pos.Is_Valid());
  248. // Create an offense object to carry the damage information
  249. OffenseObjectClass offense( explosion_def->DamageStrength, explosion_def->DamageWarhead, damager );
  250. // Loop over all game objects, appling damage to those close enough
  251. SLNode<BaseGameObj> *objnode;
  252. for ( objnode = GameObjManager::Get_Game_Obj_List()->Head(); objnode; objnode = objnode->Next()) {
  253. PhysicalGameObj *obj = objnode->Data()->As_PhysicalGameObj();
  254. // If this object is the force victim, give him it all!
  255. if ( objnode->Data() == force_victim ) {
  256. // physical objs take extended damage
  257. if ( obj != NULL ) {
  258. obj->Apply_Damage_Extended( offense );
  259. } else {
  260. force_victim->Apply_Damage( offense );
  261. }
  262. } else {
  263. if ( obj && obj->Peek_Physical_Object() ) { // zones have no phy obj CHANGE THIS
  264. if ( !obj->Takes_Explosion_Damage() ) { // Cinematics shouldn't take explosion damage
  265. continue; // Because that don't exist where they are drawn
  266. }
  267. Vector3 obj_pos,v;
  268. obj_pos = obj->Get_Bullseye_Position();
  269. WWASSERT(obj_pos.Is_Valid());
  270. v = obj_pos - pos;
  271. float dist = v.Length();
  272. #ifdef WWDEBUG
  273. if (!WWMath::Is_Valid_Float(dist)) {
  274. WWDEBUG_SAY(("Explosion Distance Bug!\r\n"));
  275. Vector3 obj_pos;
  276. obj->Get_Position(&obj_pos);
  277. WWDEBUG_SAY((" explosion pos: %f, %f, %f object pos: %f, %f, %f\r\n",pos.X,pos.Y,pos.Z,obj_pos.X,obj_pos.Y,obj_pos.Z));
  278. WWDEBUG_SAY((" object definition name: %s\r\n", obj->Get_Definition().Get_Name()));
  279. WWDEBUG_SAY((" explosion definition name; %s\r\n", explosion_def->Get_Name()));
  280. }
  281. #endif
  282. WWASSERT(WWMath::Is_Valid_Float(dist));
  283. if ( dist <= radius ) {
  284. float scale = 1.0f;
  285. if ( explosion_def->DamageIsScaled ) {
  286. WWASSERT(radius > WWMATH_EPSILON);
  287. scale = 1.0 - (dist / radius);
  288. WWASSERT(WWMath::Is_Valid_Float(scale));
  289. }
  290. // Check for collisions in the path of the object
  291. CastResultStruct res;
  292. LineSegClass ray(obj_pos,pos);
  293. PhysRayCollisionTestClass raytest(ray,&res,obj->Peek_Physical_Object()->Get_Collision_Group(),COLLISION_TYPE_PROJECTILE);
  294. raytest.CheckDynamicObjs = false;
  295. PhysicsSceneClass::Get_Instance()->Cast_Ray(raytest);
  296. if (res.Fraction < 1.0f - WWMATH_EPSILON) {
  297. scale *= 0.25f;
  298. }
  299. // Apply the damage
  300. obj->Apply_Damage_Extended( offense, scale, v );
  301. }
  302. }
  303. }
  304. }
  305. }
  306. }
  307. // Make the Sound
  308. if ( explosion_def->SoundDefID != 0 ) {
  309. RefCountedGameObjReference *owner_ref = new RefCountedGameObjReference;
  310. owner_ref->Set_Ptr( damager );
  311. WWAudioClass::Get_Instance()->Create_Instant_Sound( explosion_def->SoundDefID, up_tm, owner_ref );
  312. REF_PTR_RELEASE( owner_ref );
  313. }
  314. // Make the camera shake!
  315. if ( explosion_def->CameraShakeIntensity > 0.0f ) {
  316. COMBAT_SCENE->Add_Camera_Shake( pos,
  317. explosion_def->CameraShakeRadius,
  318. explosion_def->CameraShakeDuration,
  319. explosion_def->CameraShakeIntensity );
  320. }
  321. }
  322. void ExplosionManager::Explosion_Damage_Building( int explosion_def_id, BuildingGameObj * building, bool mct_damage, ArmedGameObj * damager )
  323. {
  324. // if ( CombatManager::I_Am_Server() ) Clients can make damage now
  325. {
  326. // Find Explosion Def
  327. ExplosionDefinitionClass * explosion_def = (ExplosionDefinitionClass *)DefinitionMgrClass::Find_Definition( explosion_def_id );
  328. WWASSERT( explosion_def );
  329. OffenseObjectClass offense( explosion_def->DamageStrength, explosion_def->DamageWarhead, damager );
  330. building->Apply_Damage_Building( offense, mct_damage );
  331. }
  332. }
  333. /*
  334. ** Broadcastes server explosion event
  335. */
  336. void ExplosionManager::Server_Explode( int explosion_def_id, const Vector3 & pos, int owner_id, DamageableGameObj * force_victim )
  337. {
  338. WWASSERT( CombatManager::I_Am_Server() );
  339. cScExplosionEvent * event = new cScExplosionEvent();
  340. if ( event ) {
  341. int victim_id = 0;
  342. if ( force_victim ) {
  343. victim_id = force_victim->Get_ID();
  344. }
  345. event->Init( explosion_def_id, pos, owner_id, victim_id );
  346. }
  347. }
  348. void ExplosionManager::Explode( int explosion_def_id, const Vector3 & pos, int owner_id, int victim_id )
  349. {
  350. if ( explosion_def_id != 0 ) {
  351. ArmedGameObj * owner = NULL;
  352. if ( owner_id != 0 ) {
  353. owner = GameObjManager::Find_SmartGameObj( owner_id );
  354. }
  355. DamageableGameObj * force_victim = NULL;
  356. if ( victim_id != 0 ) {
  357. force_victim = GameObjManager::Find_PhysicalGameObj( victim_id );
  358. }
  359. Create_Explosion_At( explosion_def_id, pos, owner, Vector3( 0,0,-1), force_victim );
  360. }
  361. }
  362. /* 04/23/01 - reenabling - in the hopes that sr was to blame
  363. #ifdef WWDEBUG
  364. char computer_name[200];
  365. DWORD size = sizeof(computer_name);
  366. GetComputerName(computer_name, &size);
  367. if (cMiscUtil::Is_String_Same(computer_name, "TOMSS2")) {
  368. //return;
  369. is_enabled = false;
  370. }
  371. #endif // WWDEBUG
  372. /**/