TerrainSectionPersist.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : LevelEdit *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/LevelEdit/TerrainSectionPersist.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 11/29/01 12:22p $*
  29. * *
  30. * $Revision:: 9 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "terrainsectionpersist.h"
  37. #include "node.h"
  38. #include "chunkio.h"
  39. #include "staticphys.h"
  40. #include "lightnode.h"
  41. #include "terrainnode.h"
  42. #include "nodemgr.h"
  43. //////////////////////////////////////////////////////////////////////////////
  44. // Constants
  45. //////////////////////////////////////////////////////////////////////////////
  46. enum
  47. {
  48. CHUNKID_VARIABLES = 0x10251130,
  49. CHUNKID_TERRAIN_SECTION_INFO,
  50. CHUNKID_SECTION_PERSISTDATA
  51. };
  52. enum
  53. {
  54. VARID_POSITION = 0x01,
  55. VARID_DEF_ID,
  56. VARID_INSTANCE_ID,
  57. VARID_VIS_OBJ_ID,
  58. VARID_VIS_SECTOR_ID,
  59. VARID_NAME,
  60. VARID_CULLLINK
  61. };
  62. //////////////////////////////////////////////////////////////////////////////
  63. //
  64. // TerrainSectionPersistClass
  65. //
  66. //////////////////////////////////////////////////////////////////////////////
  67. TerrainSectionPersistClass::TerrainSectionPersistClass (void)
  68. : m_Position (0, 0, 0),
  69. m_DefinitionID (0),
  70. m_InstanceID (0),
  71. m_VisObjectID (0),
  72. m_VisSectorID (0),
  73. m_CullLink (-1)
  74. {
  75. return ;
  76. }
  77. //////////////////////////////////////////////////////////////////////////////
  78. //
  79. // ~TerrainSectionPersistClass
  80. //
  81. //////////////////////////////////////////////////////////////////////////////
  82. TerrainSectionPersistClass::~TerrainSectionPersistClass (void)
  83. {
  84. return ;
  85. }
  86. //////////////////////////////////////////////////////////////////////////////
  87. //
  88. // Apply
  89. //
  90. //////////////////////////////////////////////////////////////////////////////
  91. void
  92. TerrainSectionPersistClass::Apply (NodeClass *node)
  93. {
  94. SANITY_CHECK (node != NULL)
  95. {
  96. return ;
  97. }
  98. //
  99. // Assign the id to the section
  100. //
  101. node->Set_ID (Get_Instance_ID ());
  102. node->Set_Cull_Link (m_CullLink);
  103. //
  104. // Assign the other IDs (these differ based on type)
  105. //
  106. if (node->Get_Type () == NODE_TYPE_TERRAIN) {
  107. m_TerrainSectionInfo.Assign_Section_IDs (reinterpret_cast<TerrainNodeClass *>(node));
  108. } else {
  109. //
  110. // Assign this physics object its vis ID (if applicable)
  111. //
  112. PhysClass *phys_obj = node->Peek_Physics_Obj ();
  113. if (phys_obj != NULL) {
  114. if (phys_obj->As_StaticPhysClass () != NULL) {
  115. ((StaticPhysClass *)phys_obj)->Set_Vis_Object_ID (Get_Vis_Obj_ID ());
  116. ((StaticPhysClass *)phys_obj)->Set_Vis_Sector_ID (Get_Vis_Sector_ID ());
  117. } else if (node->Get_Type () == NODE_TYPE_LIGHT) {
  118. ((LightNodeClass *)node)->Set_Vis_Sector_ID (Get_Vis_Sector_ID ());
  119. }
  120. }
  121. }
  122. return ;
  123. }
  124. //////////////////////////////////////////////////////////////////////////////
  125. //
  126. // Initialize
  127. //
  128. //////////////////////////////////////////////////////////////////////////////
  129. void
  130. TerrainSectionPersistClass::Initialize (NodeClass *node)
  131. {
  132. SANITY_CHECK (node != NULL)
  133. {
  134. return ;
  135. }
  136. m_Position = node->Get_Position ();
  137. m_InstanceID = node->Get_ID ();
  138. m_Name = node->Get_Model_Name ();
  139. node->Update_Cached_Cull_Link ();
  140. m_CullLink = node->Get_Cull_Link ();
  141. //
  142. // Lookup the node's definition ID
  143. //
  144. PresetClass *preset = node->Get_Preset ();
  145. if (preset != NULL) {
  146. m_DefinitionID = preset->Get_Definition ()->Get_ID ();
  147. }
  148. if (node->Get_Type () == NODE_TYPE_LIGHT) {
  149. //
  150. // For lights we have to get the ID straight from the light object (not its phys obj)
  151. //
  152. m_VisSectorID = ((LightNodeClass *)node)->Get_Vis_Sector_ID ();
  153. } else if (node->Get_Type () == NODE_TYPE_TERRAIN) {
  154. //
  155. // For terrain nodes we need to save (recursively possibly) information
  156. // about all of its sections
  157. //
  158. m_TerrainSectionInfo.Build_List (((TerrainNodeClass *)node)->m_Sections);
  159. } else {
  160. //
  161. // Lookup the vis IDs for this object (if it has any)
  162. //
  163. PhysClass *phys_obj = node->Peek_Physics_Obj ();
  164. if (phys_obj != NULL) {
  165. StaticPhysClass *static_phys_obj = phys_obj->As_StaticPhysClass ();
  166. if (static_phys_obj != NULL) {
  167. m_VisObjectID = static_phys_obj->Get_Vis_Object_ID ();
  168. m_VisSectorID = static_phys_obj->Get_Vis_Sector_ID ();
  169. //
  170. // Record the cull-link index from the static object
  171. //
  172. AABTreeLinkClass *link = (AABTreeLinkClass *)static_phys_obj->Get_Cull_Link ();
  173. if (link != NULL) {
  174. m_CullLink = link->Node->Index;
  175. }
  176. }
  177. }
  178. }
  179. return ;
  180. }
  181. /////////////////////////////////////////////////////////////////
  182. //
  183. // Save
  184. //
  185. /////////////////////////////////////////////////////////////////
  186. bool
  187. TerrainSectionPersistClass::Save (ChunkSaveClass &csave)
  188. {
  189. csave.Begin_Chunk (CHUNKID_VARIABLES);
  190. Save_Variables (csave);
  191. csave.End_Chunk ();
  192. csave.Begin_Chunk (CHUNKID_TERRAIN_SECTION_INFO);
  193. m_TerrainSectionInfo.Save (csave);
  194. csave.End_Chunk ();
  195. return true;
  196. }
  197. /////////////////////////////////////////////////////////////////
  198. //
  199. // Load
  200. //
  201. /////////////////////////////////////////////////////////////////
  202. bool
  203. TerrainSectionPersistClass::Load (ChunkLoadClass &cload)
  204. {
  205. while (cload.Open_Chunk ()) {
  206. switch (cload.Cur_Chunk_ID ()) {
  207. case CHUNKID_VARIABLES:
  208. Load_Variables (cload);
  209. break;
  210. case CHUNKID_TERRAIN_SECTION_INFO:
  211. m_TerrainSectionInfo.Load (cload);
  212. break;
  213. }
  214. cload.Close_Chunk ();
  215. }
  216. return true;
  217. }
  218. /////////////////////////////////////////////////////////////////
  219. //
  220. // Save_Variables
  221. //
  222. /////////////////////////////////////////////////////////////////
  223. bool
  224. TerrainSectionPersistClass::Save_Variables (ChunkSaveClass &csave)
  225. {
  226. WRITE_MICRO_CHUNK (csave, VARID_POSITION, m_Position);
  227. WRITE_MICRO_CHUNK (csave, VARID_DEF_ID, m_DefinitionID);
  228. WRITE_MICRO_CHUNK (csave, VARID_INSTANCE_ID, m_InstanceID);
  229. WRITE_MICRO_CHUNK (csave, VARID_VIS_OBJ_ID, m_VisObjectID);
  230. WRITE_MICRO_CHUNK (csave, VARID_VIS_SECTOR_ID, m_VisSectorID);
  231. WRITE_MICRO_CHUNK (csave, VARID_CULLLINK, m_CullLink);
  232. WRITE_MICRO_CHUNK_WWSTRING (csave, VARID_NAME, m_Name);
  233. return true;
  234. }
  235. /////////////////////////////////////////////////////////////////
  236. //
  237. // Load_Variables
  238. //
  239. /////////////////////////////////////////////////////////////////
  240. bool
  241. TerrainSectionPersistClass::Load_Variables (ChunkLoadClass &cload)
  242. {
  243. while (cload.Open_Micro_Chunk ()) {
  244. switch (cload.Cur_Micro_Chunk_ID ()) {
  245. READ_MICRO_CHUNK (cload, VARID_POSITION, m_Position);
  246. READ_MICRO_CHUNK (cload, VARID_DEF_ID, m_DefinitionID);
  247. READ_MICRO_CHUNK (cload, VARID_INSTANCE_ID, m_InstanceID);
  248. READ_MICRO_CHUNK (cload, VARID_VIS_OBJ_ID, m_VisObjectID);
  249. READ_MICRO_CHUNK (cload, VARID_VIS_SECTOR_ID, m_VisSectorID);
  250. READ_MICRO_CHUNK (cload, VARID_CULLLINK, m_CullLink);
  251. READ_MICRO_CHUNK_WWSTRING (cload, VARID_NAME, m_Name);
  252. }
  253. cload.Close_Micro_Chunk ();
  254. }
  255. return true;
  256. }
  257. /////////////////////////////////////////////////////////////////
  258. //
  259. // Build_List
  260. //
  261. /////////////////////////////////////////////////////////////////
  262. void
  263. TerrainSectionPersistListClass::Build_List (DynamicVectorClass<NodeClass *> &node_list)
  264. {
  265. Free_List ();
  266. //
  267. // Build an identifier for each of the sections (based on position
  268. // and type).
  269. //
  270. for (int index = 0; index < node_list.Count (); index ++) {
  271. NodeClass *node = node_list[index];
  272. //
  273. // Create a new perist object to store the node's data
  274. //
  275. TerrainSectionPersistClass *persist_obj = new TerrainSectionPersistClass;
  276. persist_obj->Initialize (node);
  277. Add (persist_obj);
  278. }
  279. return ;
  280. }
  281. //////////////////////////////////////////////////////////////////////////////
  282. //
  283. // Free_List
  284. //
  285. //////////////////////////////////////////////////////////////////////////////
  286. void
  287. TerrainSectionPersistListClass::Free_List (void)
  288. {
  289. //
  290. // Free each persist object
  291. //
  292. for (int index = 0; index < Count (); index ++) {
  293. SAFE_DELETE ((*this)[index]);
  294. }
  295. Delete_All ();
  296. return ;
  297. }
  298. /////////////////////////////////////////////////////////////////
  299. //
  300. // Save
  301. //
  302. /////////////////////////////////////////////////////////////////
  303. void
  304. TerrainSectionPersistListClass::Save (ChunkSaveClass &csave)
  305. {
  306. //
  307. // Write a chunk for each section so we can store instance
  308. // specific data.
  309. //
  310. for (int index = 0; index < Count (); index ++) {
  311. TerrainSectionPersistClass *persist_obj = (*this)[index];
  312. csave.Begin_Chunk (CHUNKID_SECTION_PERSISTDATA);
  313. persist_obj->Save (csave);
  314. csave.End_Chunk ();
  315. }
  316. return ;
  317. }
  318. /////////////////////////////////////////////////////////////////
  319. //
  320. // Load
  321. //
  322. /////////////////////////////////////////////////////////////////
  323. void
  324. TerrainSectionPersistListClass::Load (ChunkLoadClass &cload)
  325. {
  326. Free_List ();
  327. while (cload.Open_Chunk ()) {
  328. switch (cload.Cur_Chunk_ID ()) {
  329. case CHUNKID_SECTION_PERSISTDATA:
  330. {
  331. //
  332. // Create a persist object and get it to load the data
  333. //
  334. TerrainSectionPersistClass *persist_info = new TerrainSectionPersistClass;
  335. persist_info->Load (cload);
  336. Add (persist_info);
  337. }
  338. break;
  339. }
  340. cload.Close_Chunk ();
  341. }
  342. return ;
  343. }
  344. ////////////////////////////////////////////////////////////////
  345. //
  346. // Initialize_Virgin_Sections
  347. //
  348. ////////////////////////////////////////////////////////////////
  349. void
  350. TerrainSectionPersistListClass::Initialize_Virgin_Sections (void)
  351. {
  352. //
  353. // Give each section a unique ID if we couldn't match it up with
  354. // its saved version.
  355. //
  356. for (int index = 0; index < m_VirginSections.Count (); index ++) {
  357. NodeClass *node = m_VirginSections[index];
  358. if (node != NULL) {
  359. node->Set_ID (NodeMgrClass::Get_Node_ID (node->Get_Type ()));
  360. NodeMgrClass::Setup_Node_Identity (*node);
  361. }
  362. }
  363. //
  364. // Recurse into each sub-section
  365. //
  366. for (index = 0; index < Count (); index ++) {
  367. (*this)[index]->Initialize_Virgin_Sections ();
  368. }
  369. m_VirginSections.Delete_All ();
  370. return ;
  371. }
  372. ////////////////////////////////////////////////////////////////
  373. //
  374. // Assign_Section_IDs
  375. //
  376. ////////////////////////////////////////////////////////////////
  377. void
  378. TerrainSectionPersistListClass::Assign_Section_IDs (TerrainNodeClass *node)
  379. {
  380. m_VirginSections = node->m_Sections;
  381. for (int index = 0; index < Count (); index ++) {
  382. TerrainSectionPersistClass *persist_obj = (*this)[index];
  383. //
  384. // Try to match this ID up with
  385. //
  386. bool found = false;
  387. for (int node_index = 0; node_index < m_VirginSections.Count () && !found; node_index ++) {
  388. NodeClass *sub_node = m_VirginSections[node_index];
  389. StringClass model_name = sub_node->Get_Model_Name ();
  390. //
  391. // Do the types match up?
  392. //
  393. if ( sub_node->Get_Preset_ID () == persist_obj->Get_Def_ID () &&
  394. ::lstrcmpi (model_name, persist_obj->Get_Name ()) == 0)
  395. {
  396. //
  397. // Do the positions match up?
  398. //
  399. const Vector3 &pos = sub_node->Get_Position ();
  400. float delta = (pos - persist_obj->Get_Position ()).Length2 ();
  401. if (delta < 1.0F) {
  402. //
  403. // Success! Assign the ID(s)
  404. //
  405. persist_obj->Apply (sub_node);
  406. m_VirginSections.Delete (node_index);
  407. found = true;
  408. }
  409. }
  410. }
  411. //
  412. // Warn the user
  413. //
  414. if (!found) {
  415. CString message;
  416. message.Format ( "Unable to match static ID %d (%s) with a terrain section!\r\n",
  417. persist_obj->Get_Instance_ID (),
  418. (LPCTSTR)persist_obj->Get_Name ());
  419. ::Output_Message (message);
  420. }
  421. }
  422. return ;
  423. }