damagezone.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/damagezone.cpp $*
  25. * *
  26. * $Author:: Byon_g $*
  27. * *
  28. * $Modtime:: 7/16/01 12:14p $*
  29. * *
  30. * $Revision:: 13 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "damagezone.h"
  36. #include "persistfactory.h"
  37. #include "combatchunkid.h"
  38. #include "simpledefinitionfactory.h"
  39. #include "debug.h"
  40. #include "wwhack.h"
  41. #include "gameobjmanager.h"
  42. #include "smartgameobj.h"
  43. #include "colmath.h"
  44. #include "weapons.h"
  45. #include "damage.h"
  46. #include "wwprofile.h"
  47. #include "combat.h"
  48. #include "crandom.h"
  49. DECLARE_FORCE_LINK( DamageZone )
  50. SimplePersistFactoryClass<DamageZoneGameObjDef, CHUNKID_GAME_OBJECT_DEF_DAMAGE_ZONE> _DamageZoneGameObjDefPersistFactory;
  51. DECLARE_DEFINITION_FACTORY(DamageZoneGameObjDef, CLASSID_GAME_OBJECT_DEF_DAMAGE_ZONE, "Damage Zone") _DamageZoneGameObjDefDefFactory;
  52. DamageZoneGameObjDef::DamageZoneGameObjDef( void ) :
  53. DamageRate( 10 ),
  54. DamageWarhead( 1 ),
  55. Color( 0.7F, 0, 0 )
  56. {
  57. #ifdef PARAM_EDITING_ON
  58. EDITABLE_PARAM( DamageZoneGameObjDef, ParameterClass::TYPE_FLOAT, DamageRate );
  59. EnumParameterClass *param;
  60. param = new EnumParameterClass( &DamageWarhead );
  61. param->Set_Name ( "Damage Warhead" );
  62. for ( int i = 0; i < ArmorWarheadManager::Get_Num_Warhead_Types(); i++ ) {
  63. param->Add_Value ( ArmorWarheadManager::Get_Warhead_Name( i ), i );
  64. }
  65. GENERIC_EDITABLE_PARAM(DamageZoneGameObjDef,param)
  66. EDITABLE_PARAM( DamageZoneGameObjDef, ParameterClass::TYPE_COLOR, Color );
  67. #endif //PARAM_EDITING_ON
  68. }
  69. uint32 DamageZoneGameObjDef::Get_Class_ID (void) const
  70. {
  71. return CLASSID_GAME_OBJECT_DEF_DAMAGE_ZONE;
  72. }
  73. PersistClass * DamageZoneGameObjDef::Create( void ) const
  74. {
  75. DamageZoneGameObj * zone = new DamageZoneGameObj;
  76. zone->Init( *this );
  77. return zone;
  78. }
  79. enum {
  80. CHUNKID_DEF_PARENT = 626000947,
  81. CHUNKID_DEF_VARIABLES,
  82. XXXMICROCHUNKID_DEF_DAMAGE_TYPE = 1,
  83. MICROCHUNKID_DEF_ZONE_COLOR,
  84. MICROCHUNKID_DEF_DAMAGE_RATE,
  85. MICROCHUNKID_DEF_DAMAGE_WARHEAD,
  86. };
  87. bool DamageZoneGameObjDef::Save( ChunkSaveClass & csave )
  88. {
  89. csave.Begin_Chunk( CHUNKID_DEF_PARENT );
  90. BaseGameObjDef::Save( csave );
  91. csave.End_Chunk();
  92. csave.Begin_Chunk( CHUNKID_DEF_VARIABLES );
  93. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_DEF_ZONE_COLOR, Color );
  94. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_DEF_DAMAGE_RATE, DamageRate );
  95. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_DEF_DAMAGE_WARHEAD, DamageWarhead );
  96. csave.End_Chunk();
  97. return true;
  98. }
  99. bool DamageZoneGameObjDef::Load( ChunkLoadClass &cload )
  100. {
  101. while (cload.Open_Chunk()) {
  102. switch(cload.Cur_Chunk_ID()) {
  103. case CHUNKID_DEF_PARENT:
  104. BaseGameObjDef::Load( cload );
  105. break;
  106. case CHUNKID_DEF_VARIABLES:
  107. while (cload.Open_Micro_Chunk()) {
  108. switch(cload.Cur_Micro_Chunk_ID()) {
  109. READ_MICRO_CHUNK( cload, MICROCHUNKID_DEF_ZONE_COLOR, Color );
  110. READ_MICRO_CHUNK( cload, MICROCHUNKID_DEF_DAMAGE_RATE, DamageRate );
  111. READ_MICRO_CHUNK( cload, MICROCHUNKID_DEF_DAMAGE_WARHEAD, DamageWarhead );
  112. default:
  113. Debug_Say(("Unhandled Variable Chunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  114. break;
  115. }
  116. cload.Close_Micro_Chunk();
  117. }
  118. break;
  119. default:
  120. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  121. break;
  122. }
  123. cload.Close_Chunk();
  124. }
  125. return true;
  126. }
  127. const PersistFactoryClass & DamageZoneGameObjDef::Get_Factory (void) const
  128. {
  129. return _DamageZoneGameObjDefPersistFactory;
  130. }
  131. /*
  132. **
  133. */
  134. DamageZoneGameObj::DamageZoneGameObj( void ) :
  135. DamageTimer( 0 )
  136. {
  137. }
  138. DamageZoneGameObj::~DamageZoneGameObj( void )
  139. {
  140. }
  141. void DamageZoneGameObj::Init( void )
  142. {
  143. Init( Get_Definition() );
  144. }
  145. void DamageZoneGameObj::Init( const DamageZoneGameObjDef & definition )
  146. {
  147. BaseGameObj::Init( definition );
  148. }
  149. const DamageZoneGameObjDef & DamageZoneGameObj::Get_Definition( void ) const
  150. {
  151. return (const DamageZoneGameObjDef &)BaseGameObj::Get_Definition();
  152. }
  153. enum {
  154. CHUNKID_PARENT = 626000947,
  155. CHUNKID_VARIABLES,
  156. MICROCHUNKID_BOUNDING_BOX = 1,
  157. MICROCHUNKID_DAMAGE_TIMER,
  158. };
  159. bool DamageZoneGameObj::Save( ChunkSaveClass & csave )
  160. {
  161. csave.Begin_Chunk( CHUNKID_PARENT );
  162. BaseGameObj::Save( csave );
  163. csave.End_Chunk();
  164. csave.Begin_Chunk( CHUNKID_VARIABLES );
  165. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_BOUNDING_BOX, BoundingBox );
  166. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_DAMAGE_TIMER, DamageTimer );
  167. csave.End_Chunk();
  168. return true;
  169. }
  170. bool DamageZoneGameObj::Load( ChunkLoadClass &cload )
  171. {
  172. while (cload.Open_Chunk()) {
  173. switch(cload.Cur_Chunk_ID()) {
  174. case CHUNKID_PARENT:
  175. BaseGameObj::Load( cload );
  176. break;
  177. case CHUNKID_VARIABLES:
  178. while (cload.Open_Micro_Chunk()) {
  179. switch(cload.Cur_Micro_Chunk_ID()) {
  180. READ_MICRO_CHUNK( cload, MICROCHUNKID_BOUNDING_BOX, BoundingBox );
  181. READ_MICRO_CHUNK( cload, MICROCHUNKID_DAMAGE_TIMER, DamageTimer );
  182. default:
  183. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  184. break;
  185. }
  186. cload.Close_Micro_Chunk();
  187. }
  188. break;
  189. default:
  190. Debug_Say(("Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  191. break;
  192. }
  193. cload.Close_Chunk();
  194. }
  195. return true;
  196. }
  197. SimplePersistFactoryClass<DamageZoneGameObj, CHUNKID_GAME_OBJECT_DAMAGE_ZONE> _DamageZonePersistFactory;
  198. const PersistFactoryClass & DamageZoneGameObj::Get_Factory( void ) const
  199. {
  200. return _DamageZonePersistFactory;
  201. }
  202. void DamageZoneGameObj::Think( void )
  203. {
  204. WWPROFILE( "DamageZone Think" );
  205. if ( !CombatManager::I_Am_Server() ) {
  206. return;
  207. }
  208. // Only apply damage every second...
  209. DamageTimer += TimeManager::Get_Frame_Seconds();
  210. // Scramble it a bit so all don't hit at the same time...
  211. float trigger = 1.0f;
  212. trigger = FreeRandom.Get_Float( 1.0f, 1.5f );
  213. if ( DamageTimer >= trigger ) {
  214. // Debug_Say(( "Zone Damage %f\n", DamageTimer ));
  215. OffenseObjectClass offense( Get_Definition().DamageRate, Get_Definition().DamageWarhead );
  216. SLNode<SmartGameObj> * smart_objnode;
  217. for (smart_objnode = GameObjManager::Get_Smart_Game_Obj_List()->Head(); smart_objnode; smart_objnode = smart_objnode->Next()) {
  218. SmartGameObj * obj = smart_objnode->Data();
  219. WWASSERT( obj != NULL );
  220. Vector3 pos;
  221. obj->Get_Position(&pos);
  222. if ( CollisionMath::Overlap_Test(BoundingBox,pos) == CollisionMath::INSIDE ) {
  223. obj->Apply_Damage_Extended( offense, DamageTimer );
  224. }
  225. }
  226. DamageTimer = 0;
  227. }
  228. }