transitiongameobj.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/transitiongameobj.cpp $*
  25. * *
  26. * $Author:: Patrick $*
  27. * *
  28. * $Modtime:: 11/17/01 10:28a $*
  29. * *
  30. * $Revision:: 10 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. /*
  36. ** Includes
  37. */
  38. #include "transitiongameobj.h"
  39. #include "debug.h"
  40. #include "wwhack.h"
  41. #include "persistfactory.h"
  42. #include "combatchunkid.h"
  43. #include "simpledefinitionfactory.h"
  44. #include "combat.h"
  45. /*
  46. ** TransitionGameObjDef
  47. */
  48. DECLARE_FORCE_LINK( Transition )
  49. SimplePersistFactoryClass<TransitionGameObjDef, CHUNKID_GAME_OBJECT_DEF_TRANSITION> _TransitionGameObjDefPersistFactory;
  50. DECLARE_DEFINITION_FACTORY(TransitionGameObjDef, CLASSID_GAME_OBJECT_DEF_TRANSITION, "Transition") _TransitionGameObjDefDefFactory;
  51. TransitionGameObjDef::~TransitionGameObjDef( void )
  52. {
  53. Free_Transition_List();
  54. }
  55. uint32 TransitionGameObjDef::Get_Class_ID (void) const
  56. {
  57. return CLASSID_GAME_OBJECT_DEF_TRANSITION;
  58. }
  59. PersistClass * TransitionGameObjDef::Create( void ) const
  60. {
  61. TransitionGameObj * obj = new TransitionGameObj;
  62. obj->Init( *this );
  63. return obj;
  64. }
  65. enum {
  66. CHUNKID_DEF_PARENT = 1111991201,
  67. CHUNKID_DEF_TRANSITION,
  68. };
  69. bool TransitionGameObjDef::Save( ChunkSaveClass & csave )
  70. {
  71. csave.Begin_Chunk( CHUNKID_DEF_PARENT );
  72. BaseGameObjDef::Save( csave );
  73. csave.End_Chunk();
  74. /*
  75. ** Save each of the transition 'definitions' in our list.
  76. */
  77. for( int index = 0; index < Transitions.Count (); index ++ ) {
  78. TransitionDataClass *transition = Transitions[index];
  79. if( transition != NULL ) {
  80. /*
  81. ** Save this transition 'defintion' to its own chunk.
  82. */
  83. csave.Begin_Chunk( CHUNKID_DEF_TRANSITION );
  84. transition->Save( csave );
  85. csave.End_Chunk();
  86. }
  87. }
  88. return true;
  89. }
  90. bool TransitionGameObjDef::Load( ChunkLoadClass &cload )
  91. {
  92. Free_Transition_List ();
  93. while (cload.Open_Chunk()) {
  94. switch(cload.Cur_Chunk_ID()) {
  95. case CHUNKID_DEF_PARENT:
  96. BaseGameObjDef::Load( cload );
  97. break;
  98. case CHUNKID_DEF_TRANSITION:
  99. {
  100. TransitionDataClass *transition = new TransitionDataClass;
  101. transition->Load( cload );
  102. Transitions.Add( transition );
  103. }
  104. break;
  105. default:
  106. Debug_Say(( "Unrecognized TransitionDef chunkID\n" ));
  107. break;
  108. }
  109. cload.Close_Chunk();
  110. }
  111. return true;
  112. }
  113. /*
  114. **
  115. */
  116. void TransitionGameObjDef::Free_Transition_List( void )
  117. {
  118. /*
  119. ** Delete each of the transition 'definitions' in our list.
  120. */
  121. for( int index = 0; index < Transitions.Count (); index ++ ) {
  122. TransitionDataClass *transition = Transitions[index];
  123. if( transition != NULL ) {
  124. delete transition;
  125. }
  126. }
  127. Transitions.Delete_All();
  128. return ;
  129. }
  130. const PersistFactoryClass & TransitionGameObjDef::Get_Factory (void) const
  131. {
  132. return _TransitionGameObjDefPersistFactory;
  133. }
  134. /*
  135. ** TransitionGameObj
  136. */
  137. SimplePersistFactoryClass<TransitionGameObj, CHUNKID_GAME_OBJECT_TRANSITION> _TransitionGameObjPersistFactory;
  138. const PersistFactoryClass & TransitionGameObj::Get_Factory (void) const
  139. {
  140. return _TransitionGameObjPersistFactory;
  141. }
  142. TransitionGameObj::TransitionGameObj() :
  143. LadderIndex( -1 )
  144. {
  145. }
  146. TransitionGameObj::~TransitionGameObj()
  147. {
  148. Destroy_Transitions();
  149. }
  150. /*
  151. **
  152. */
  153. void TransitionGameObj::Init( void )
  154. {
  155. Init( Get_Definition() );
  156. }
  157. void TransitionGameObj::Init( const TransitionGameObjDef & definition )
  158. {
  159. BaseGameObj::Init( definition );
  160. Create_Transitions();
  161. }
  162. const TransitionGameObjDef & TransitionGameObj::Get_Definition( void ) const
  163. {
  164. return (const TransitionGameObjDef &)BaseGameObj::Get_Definition();
  165. }
  166. /*
  167. ** TransitionGameObj Save and Load
  168. */
  169. enum {
  170. CHUNKID_PARENT = 1111991206,
  171. CHUNKID_VARIABLES,
  172. MICROCHUNKID_TRANSFORM = 1,
  173. MICROCHUNKID_LADDER_INDEX
  174. };
  175. bool TransitionGameObj::Save( ChunkSaveClass & csave )
  176. {
  177. csave.Begin_Chunk( CHUNKID_PARENT );
  178. BaseGameObj::Save( csave );
  179. csave.End_Chunk();
  180. csave.Begin_Chunk( CHUNKID_VARIABLES );
  181. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_TRANSFORM, TM );
  182. WRITE_MICRO_CHUNK( csave, MICROCHUNKID_LADDER_INDEX, LadderIndex );
  183. csave.End_Chunk();
  184. return true;
  185. }
  186. bool TransitionGameObj::Load( ChunkLoadClass &cload )
  187. {
  188. while (cload.Open_Chunk()) {
  189. switch(cload.Cur_Chunk_ID()) {
  190. case CHUNKID_PARENT:
  191. BaseGameObj::Load( cload );
  192. break;
  193. case CHUNKID_VARIABLES:
  194. {
  195. while (cload.Open_Micro_Chunk()) {
  196. switch(cload.Cur_Micro_Chunk_ID()) {
  197. READ_MICRO_CHUNK( cload, MICROCHUNKID_TRANSFORM, TM );
  198. READ_MICRO_CHUNK( cload, MICROCHUNKID_LADDER_INDEX, LadderIndex );
  199. default:
  200. Debug_Say(( "Unrecognized TransitionGameObj Variable chunkID\n" ));
  201. break;
  202. }
  203. cload.Close_Micro_Chunk();
  204. }
  205. break;
  206. }
  207. default:
  208. Debug_Say(( "Unrecognized Transition chunkID\n" ));
  209. break;
  210. }
  211. cload.Close_Chunk();
  212. }
  213. SaveLoadSystemClass::Register_Post_Load_Callback(this);
  214. return true;
  215. }
  216. void TransitionGameObj::On_Post_Load( void )
  217. {
  218. BaseGameObj::On_Post_Load();
  219. Create_Transitions();
  220. return ;
  221. }
  222. void TransitionGameObj::Set_Transform(const Matrix3D & tm)
  223. {
  224. TM = tm;
  225. }
  226. const Matrix3D & TransitionGameObj::Get_Transform(void) const
  227. {
  228. return TM;
  229. }
  230. void TransitionGameObj::Create_Transitions( void )
  231. {
  232. Destroy_Transitions();
  233. // Only create the transitions if they haven't already been created
  234. WWASSERT( TransitionInstances.Count() == 0 );
  235. if( TransitionInstances.Count() == 0 ) {
  236. const TRANSITION_DATA_LIST & trans_data_list = Get_Definition().Get_Transition_List();
  237. Matrix3D tm = Get_Transform();
  238. for ( int i = 0; i < trans_data_list.Count(); i++ ) {
  239. // make new instance
  240. TransitionInstanceClass * trans = new TransitionInstanceClass( *trans_data_list[i] );
  241. // setup
  242. trans->Set_Parent_Transform( tm );
  243. // add to our list
  244. TransitionInstances.Add( trans );
  245. // add to master list
  246. TransitionManager::Add( trans );
  247. }
  248. }
  249. //
  250. // Ensure all the transition instances know what ladder they belong to (may
  251. // not apply for other transitions other than ladder).
  252. //
  253. Set_Ladder_Index( LadderIndex );
  254. return ;
  255. }
  256. void TransitionGameObj::Destroy_Transitions( void )
  257. {
  258. while ( TransitionInstances.Count() ) {
  259. TransitionManager::Destroy( TransitionInstances[0] );
  260. TransitionInstances.Delete( 0 );
  261. }
  262. // They get put in a list, so flush the list.
  263. TransitionManager::Destroy_Pending();
  264. }
  265. void TransitionGameObj::Update_Transitions( void )
  266. {
  267. Matrix3D tm = Get_Transform();
  268. // Update all transitions
  269. for ( int i = 0; i < TransitionInstances.Count(); i++ ) {
  270. TransitionInstances[i]->Set_Parent_Transform( tm );
  271. TransitionInstances[i]->Set_Ladder_Index( LadderIndex );
  272. }
  273. }
  274. void TransitionGameObj::Set_Ladder_Index( int ladder_index )
  275. {
  276. LadderIndex = ladder_index;
  277. // Update all transitions
  278. for ( int i = 0; i < TransitionInstances.Count(); i++ ) {
  279. TransitionInstances[i]->Set_Ladder_Index( LadderIndex );
  280. }
  281. return ;
  282. }