damageablegameobj.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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/damageablegameobj.cpp $*
  25. * *
  26. * $Author:: Tom_s $*
  27. * *
  28. * $Modtime:: 12/18/01 3:02p $*
  29. * *
  30. * $Revision:: 19 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "damageablegameobj.h"
  36. #include "debug.h"
  37. #include "armedgameobj.h"
  38. #include "playertype.h"
  39. #include "colors.h"
  40. /*
  41. ** DamageableGameObjDef - Defintion class for a DamageableGameObj
  42. */
  43. DamageableGameObjDef::DamageableGameObjDef( void ) :
  44. TranslatedNameID( 0 ),
  45. EncyclopediaType( EncyclopediaMgrClass::TYPE_UNKNOWN ),
  46. EncyclopediaID( 0 ),
  47. NotTargetable( false ),
  48. DefaultPlayerType( PLAYERTYPE_NEUTRAL )
  49. {
  50. DEFENSEOBJECTDEF_EDITABLE_PARAMS( DamageableGameObjDef, DefenseObjectDef );
  51. EDITABLE_PARAM( DamageableGameObjDef, ParameterClass::TYPE_STRINGSDB_ID, TranslatedNameID );
  52. FILENAME_PARAM( DamageableGameObjDef, InfoIconTextureFilename, "InfoIconTextureFilename", ".TGA" );
  53. #ifdef PARAM_EDITING_ON
  54. EnumParameterClass *param = new EnumParameterClass( (int *)&EncyclopediaType );
  55. param->Set_Name( "Encyclopedia Type" );
  56. param->Add_Value( "<NA>", 0 );
  57. param->Add_Value( "Character", EncyclopediaMgrClass::TYPE_CHARACTER );
  58. param->Add_Value( "Weapon", EncyclopediaMgrClass::TYPE_WEAPON );
  59. param->Add_Value( "Vehicle", EncyclopediaMgrClass::TYPE_VEHICLE );
  60. param->Add_Value( "Building", EncyclopediaMgrClass::TYPE_BUILDING );
  61. GENERIC_EDITABLE_PARAM( DamageableGameObjDef, param )
  62. param = new EnumParameterClass( &DefaultPlayerType );
  63. param->Set_Name ("PlayerType");
  64. param->Add_Value ( "Mutant", PLAYERTYPE_MUTANT );
  65. param->Add_Value ( "Unteamed", PLAYERTYPE_NEUTRAL );
  66. param->Add_Value ( "Renegade", PLAYERTYPE_RENEGADE );
  67. param->Add_Value ( "Nod", PLAYERTYPE_NOD );
  68. param->Add_Value ( "GDI", PLAYERTYPE_GDI );
  69. GENERIC_EDITABLE_PARAM(DamageableGameObjDef,param)
  70. #endif
  71. EDITABLE_PARAM( DamageableGameObjDef, ParameterClass::TYPE_INT, EncyclopediaID );
  72. EDITABLE_PARAM( DamageableGameObjDef, ParameterClass::TYPE_BOOL, NotTargetable );
  73. return ;
  74. }
  75. enum {
  76. CHUNKID_DEF_PARENT = 207011205,
  77. CHUNKID_DEF_VARIABLES,
  78. CHUNKID_DEF_DEFENSEOBJECTDEF,
  79. MICROCHUNKID_DEF_TRANSLATED_NAME_ID = 1,
  80. MICROCHUNKID_DEF_INFO_ICON_TEXTURE_FILENAME,
  81. MICROCHUNKID_DEF_ENCY_TYPE,
  82. MICROCHUNKID_DEF_ENCY_ID,
  83. MICROCHUNKID_DEF_NOT_TARGETABLE,
  84. MICROCHUNKID_DEF_DEFAULT_PLAYER_TYPE,
  85. };
  86. bool DamageableGameObjDef::Save( ChunkSaveClass & csave )
  87. {
  88. csave.Begin_Chunk( CHUNKID_DEF_PARENT );
  89. ScriptableGameObjDef::Save( csave );
  90. csave.End_Chunk();
  91. csave.Begin_Chunk( CHUNKID_DEF_VARIABLES );
  92. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_DEF_TRANSLATED_NAME_ID, TranslatedNameID );
  93. WRITE_MICRO_CHUNK_WWSTRING( csave, MICROCHUNKID_DEF_INFO_ICON_TEXTURE_FILENAME, InfoIconTextureFilename );
  94. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_DEF_ENCY_TYPE, EncyclopediaType );
  95. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_DEF_ENCY_ID, EncyclopediaID );
  96. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_DEF_NOT_TARGETABLE, NotTargetable );
  97. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_DEF_DEFAULT_PLAYER_TYPE, DefaultPlayerType );
  98. csave.End_Chunk();
  99. csave.Begin_Chunk( CHUNKID_DEF_DEFENSEOBJECTDEF );
  100. DefenseObjectDef.Save(csave);
  101. csave.End_Chunk();
  102. return true;
  103. }
  104. bool DamageableGameObjDef::Load( ChunkLoadClass &cload )
  105. {
  106. while (cload.Open_Chunk()) {
  107. switch(cload.Cur_Chunk_ID()) {
  108. case CHUNKID_DEF_PARENT:
  109. ScriptableGameObjDef::Load( cload );
  110. break;
  111. case CHUNKID_DEF_VARIABLES:
  112. while (cload.Open_Micro_Chunk()) {
  113. switch(cload.Cur_Micro_Chunk_ID()) {
  114. READ_MICRO_CHUNK( cload, MICROCHUNKID_DEF_TRANSLATED_NAME_ID, TranslatedNameID );
  115. READ_MICRO_CHUNK_WWSTRING( cload, MICROCHUNKID_DEF_INFO_ICON_TEXTURE_FILENAME, InfoIconTextureFilename );
  116. READ_MICRO_CHUNK( cload, MICROCHUNKID_DEF_ENCY_TYPE, EncyclopediaType );
  117. READ_MICRO_CHUNK( cload, MICROCHUNKID_DEF_ENCY_ID, EncyclopediaID );
  118. READ_MICRO_CHUNK( cload, MICROCHUNKID_DEF_NOT_TARGETABLE, NotTargetable );
  119. READ_MICRO_CHUNK( cload, MICROCHUNKID_DEF_DEFAULT_PLAYER_TYPE, DefaultPlayerType );
  120. default:
  121. Debug_Say(( "Unhandled MicroChunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  122. break;
  123. }
  124. cload.Close_Micro_Chunk();
  125. }
  126. break;
  127. case CHUNKID_DEF_DEFENSEOBJECTDEF:
  128. DefenseObjectDef.Load(cload);
  129. break;
  130. default:
  131. Debug_Say(( "Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  132. break;
  133. }
  134. cload.Close_Chunk();
  135. }
  136. return true;
  137. }
  138. /*
  139. ** DamageableGameObj
  140. */
  141. DamageableGameObj::DamageableGameObj( void ) :
  142. IsHealthBarDisplayed( true )
  143. {
  144. Set_Player_Type(PLAYERTYPE_NEUTRAL);
  145. }
  146. DamageableGameObj::~DamageableGameObj( void )
  147. {
  148. Remove_All_Observers();
  149. }
  150. /*
  151. **
  152. */
  153. void DamageableGameObj::Init( const DamageableGameObjDef & definition )
  154. {
  155. ScriptableGameObj::Init( definition );
  156. Copy_Settings( definition );
  157. return ;
  158. }
  159. /*
  160. **
  161. */
  162. void DamageableGameObj::Copy_Settings( const DamageableGameObjDef & definition )
  163. {
  164. Set_Player_Type(definition.DefaultPlayerType);
  165. DefenseObject.Init(definition.DefenseObjectDef, this );
  166. return ;
  167. }
  168. /*
  169. **
  170. */
  171. void DamageableGameObj::Re_Init( const DamageableGameObjDef & definition )
  172. {
  173. int old_player_type = PlayerType;
  174. //
  175. // Record the health and shield percent so we can restore the
  176. // appropriate amount of health and shield after we've re-initialized
  177. //
  178. //float health_percent = DefenseObject.Get_Health() / DefenseObject.Get_Health_Max();
  179. //float shield_percent = DefenseObject.Get_Shield_Strength() / DefenseObject.Get_Shield_Strength_Max();
  180. //
  181. // Re-initialize the base class
  182. //
  183. ScriptableGameObj::Re_Init( definition );
  184. //
  185. // Copy any internal settings from the definition
  186. //
  187. Copy_Settings( definition );
  188. //
  189. // Reset the health and shield to appropriate values
  190. //
  191. //DefenseObject.Set_Health (DefenseObject.Get_Health_Max () * health_percent);
  192. //DefenseObject.Set_Shield_Strength (DefenseObject.Get_Shield_Strength_Max () * shield_percent);
  193. Set_Player_Type( old_player_type );
  194. return ;
  195. }
  196. const DamageableGameObjDef & DamageableGameObj::Get_Definition( void ) const
  197. {
  198. return (const DamageableGameObjDef &)BaseGameObj::Get_Definition();
  199. }
  200. /*
  201. ** DamageableGameObj Save and Load
  202. */
  203. enum {
  204. CHUNKID_PARENT = 207011212,
  205. CHUNKID_DEFENSEOBJECT,
  206. CHUNKID_VARIABLES,
  207. MICROCHUNKID_PLAYER_TYPE = 1,
  208. MICROCHUNKID_IS_HEALTH_BAR_DISPLAYED,
  209. };
  210. bool DamageableGameObj::Save( ChunkSaveClass & csave )
  211. {
  212. csave.Begin_Chunk( CHUNKID_PARENT );
  213. ScriptableGameObj::Save( csave );
  214. csave.End_Chunk();
  215. csave.Begin_Chunk( CHUNKID_VARIABLES );
  216. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_PLAYER_TYPE, PlayerType );
  217. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_IS_HEALTH_BAR_DISPLAYED, IsHealthBarDisplayed );
  218. csave.End_Chunk();
  219. csave.Begin_Chunk( CHUNKID_DEFENSEOBJECT );
  220. DefenseObject.Save(csave);
  221. csave.End_Chunk();
  222. return true;
  223. }
  224. bool DamageableGameObj::Load( ChunkLoadClass &cload )
  225. {
  226. while (cload.Open_Chunk()) {
  227. switch(cload.Cur_Chunk_ID()) {
  228. case CHUNKID_PARENT:
  229. ScriptableGameObj::Load( cload );
  230. break;
  231. case CHUNKID_VARIABLES:
  232. while (cload.Open_Micro_Chunk()) {
  233. switch(cload.Cur_Micro_Chunk_ID()) {
  234. READ_MICRO_CHUNK( cload, MICROCHUNKID_PLAYER_TYPE, PlayerType );
  235. READ_MICRO_CHUNK( cload, MICROCHUNKID_IS_HEALTH_BAR_DISPLAYED, IsHealthBarDisplayed );
  236. default:
  237. Debug_Say(( "Unhandled MicroChunk:%d File:%s Line:%d\r\n",cload.Cur_Micro_Chunk_ID(),__FILE__,__LINE__));
  238. break;
  239. }
  240. cload.Close_Micro_Chunk();
  241. }
  242. break;
  243. case CHUNKID_DEFENSEOBJECT:
  244. DefenseObject.Load( cload );
  245. break;
  246. default:
  247. Debug_Say(( "Unhandled Chunk:%d File:%s Line:%d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  248. break;
  249. }
  250. cload.Close_Chunk();
  251. }
  252. return true;
  253. }
  254. void DamageableGameObj::Apply_Damage( const OffenseObjectClass & damager, float scale, int alternate_skin )
  255. {
  256. if ( DefenseObject.Get_Health() <= 0 ) {
  257. return;
  258. }
  259. if (Is_Delete_Pending()) {
  260. return;
  261. }
  262. float old_health = DefenseObject.Get_Health();
  263. float old_shield = DefenseObject.Get_Shield_Strength();
  264. DefenseObject.Apply_Damage( damager, scale, alternate_skin );
  265. float new_health = DefenseObject.Get_Health();
  266. float new_shield = DefenseObject.Get_Shield_Strength();
  267. float diff = old_health + old_shield - new_health - new_shield;
  268. // Notify the observers
  269. const GameObjObserverList & observer_list = Get_Observers();
  270. for( int index = 0; index < observer_list.Count(); index++ ) {
  271. observer_list[ index ]->Damaged( this, damager.Get_Owner(), diff );
  272. }
  273. if ( DefenseObject.Get_Health() <= 0 ) {
  274. // notify the observers
  275. for( int index = 0; index < observer_list.Count(); index++ ) {
  276. observer_list[ index ]->Killed( this, damager.Get_Owner() );
  277. }
  278. Completely_Damaged( damager );
  279. }
  280. }
  281. //------------------------------------------------------------------------------------
  282. /*void DamageableGameObj::Get_Information( StringClass & string )
  283. {
  284. // If we just came from the editor, call created on all out observers
  285. const GameObjObserverList & observer_list = Get_Observers();
  286. for( int index = 0; index < observer_list.Count(); index++ ) {
  287. StringClass temp;
  288. temp.Format( "%s\n", observer_list[ index ]->Get_Name() );
  289. string += temp;
  290. }
  291. } */
  292. /*
  293. **
  294. */
  295. void DamageableGameObj::Export_Occasional( BitStreamClass &packet )
  296. {
  297. ScriptableGameObj::Export_Occasional( packet );
  298. //
  299. // Export the defense object's state
  300. //
  301. DefenseObject.Export (packet);
  302. }
  303. /*
  304. **
  305. */
  306. void DamageableGameObj::Import_Occasional( BitStreamClass &packet )
  307. {
  308. ScriptableGameObj::Import_Occasional( packet );
  309. //
  310. // Update the defense object's state
  311. //
  312. float old_health = DefenseObject.Get_Health();
  313. DefenseObject.Import (packet);
  314. float new_health = DefenseObject.Get_Health();
  315. /*
  316. //
  317. // Hack !
  318. //
  319. if ( Is_Delete_Pending() ) {
  320. if ( new_health != 0 ) {
  321. DefenseObject.Set_Health( 0 );
  322. new_health = DefenseObject.Get_Health();
  323. }
  324. } else {
  325. if ( new_health == 0 ) {
  326. DefenseObject.Set_Health( 0.01f );
  327. new_health = DefenseObject.Get_Health();
  328. }
  329. }
  330. */
  331. if (old_health > 0 && old_health > new_health) {
  332. // Notify the observers that we are damaged
  333. const GameObjObserverList& observer_list = Get_Observers();
  334. int count = observer_list.Count();
  335. for (int index = 0; index < count; ++index) {
  336. observer_list[index]->Damaged(this, NULL, old_health - new_health);
  337. }
  338. }
  339. //
  340. // Check to see if the object is completely damaged
  341. //
  342. if ( old_health > 0 && new_health <= 0 ) {
  343. // Notify the observers that the building has been destroyed
  344. const GameObjObserverList& observer_list = Get_Observers();
  345. int count = observer_list.Count();
  346. for (int index = 0; index < count; ++index) {
  347. observer_list[index]->Killed(this, NULL);
  348. }
  349. OffenseObjectClass dummy_offense_obj;
  350. Completely_Damaged( dummy_offense_obj );
  351. }
  352. }
  353. //-----------------------------------------------------------------------------
  354. bool DamageableGameObj::Is_Team_Player(void)
  355. {
  356. return PlayerType == PLAYERTYPE_NOD || PlayerType == PLAYERTYPE_GDI;
  357. }
  358. //-----------------------------------------------------------------------------
  359. Vector3 DamageableGameObj::Get_Team_Color(void)
  360. {
  361. return Get_Color_For_Team(PlayerType);
  362. }
  363. //-----------------------------------------------------------------------------
  364. void DamageableGameObj::Set_Player_Type(int id)
  365. {
  366. PlayerType = id;
  367. Set_Object_Dirty_Bit( NetworkObjectClass::BIT_RARE, true );
  368. }
  369. //-----------------------------------------------------------------------------
  370. bool DamageableGameObj::Is_Teammate(DamageableGameObj * p_obj)
  371. {
  372. WWASSERT(p_obj != NULL);
  373. return ((p_obj == this) ||
  374. (Is_Team_Player() && Get_Player_Type() == p_obj->Get_Player_Type()));
  375. }
  376. bool DamageableGameObj::Is_Enemy(DamageableGameObj * p_obj)
  377. {
  378. WWASSERT(p_obj != NULL);
  379. return ( (p_obj != this) && Player_Types_Are_Enemies( Get_Player_Type(), p_obj->Get_Player_Type() ) );
  380. }