BuildingNode.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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/BuildingNode.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 9/13/01 9:44a $*
  29. * *
  30. * $Revision:: 7 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "buildingnode.h"
  37. #include "buildingchildnode.h"
  38. #include "sceneeditor.h"
  39. #include "filemgr.h"
  40. #include "_assetmgr.h"
  41. #include "editorassetmgr.h"
  42. #include "w3d_file.h"
  43. #include "cameramgr.h"
  44. #include "collisiongroups.h"
  45. #include "persistfactory.h"
  46. #include "preset.h"
  47. #include "editorchunkids.h"
  48. #include "modelutils.h"
  49. #include "vehiclefactorygameobj.h"
  50. #include "refinerygameobj.h"
  51. #include "nodemgr.h"
  52. //////////////////////////////////////////////////////////////////////////////
  53. // Persist factory
  54. //////////////////////////////////////////////////////////////////////////////
  55. SimplePersistFactoryClass<BuildingNodeClass, CHUNKID_NODE_BUILDING> _BuildingNodePersistFactory;
  56. enum
  57. {
  58. CHUNKID_VARIABLES = 0x09071125,
  59. CHUNKID_BASE_CLASS,
  60. };
  61. enum
  62. {
  63. VARID_VISOBJECTID = 0x01,
  64. VARID_VISSECTORID,
  65. VARID_CHILD_NODE
  66. };
  67. //////////////////////////////////////////////////////////////////////////////
  68. //
  69. // BuildingNodeClass
  70. //
  71. //////////////////////////////////////////////////////////////////////////////
  72. BuildingNodeClass::BuildingNodeClass (PresetClass *preset)
  73. : m_PhysObj (NULL),
  74. ObjectNodeClass (preset)
  75. {
  76. return ;
  77. }
  78. //////////////////////////////////////////////////////////////////////////////
  79. //
  80. // BuildingNodeClass
  81. //
  82. //////////////////////////////////////////////////////////////////////////////
  83. BuildingNodeClass::BuildingNodeClass (const BuildingNodeClass &src)
  84. : m_PhysObj (NULL),
  85. ObjectNodeClass (NULL)
  86. {
  87. (*this) = src;
  88. return ;
  89. }
  90. //////////////////////////////////////////////////////////////////////////////
  91. //
  92. // ~BuildingNodeClass
  93. //
  94. //////////////////////////////////////////////////////////////////////////////
  95. BuildingNodeClass::~BuildingNodeClass (void)
  96. {
  97. Free_Child_Nodes ();
  98. Remove_From_Scene ();
  99. MEMBER_RELEASE (m_PhysObj);
  100. return ;
  101. }
  102. //////////////////////////////////////////////////////////////////////////////
  103. //
  104. // Initialize
  105. //
  106. // Note: This may be called more than once. It is used as an 'initialize'
  107. // and a 're-initialize'.
  108. //
  109. //////////////////////////////////////////////////////////////////////////////
  110. void
  111. BuildingNodeClass::Initialize (void)
  112. {
  113. MEMBER_RELEASE (m_PhysObj);
  114. //
  115. // Load the model we want to use to identify the building controller
  116. //
  117. RenderObjClass *render_obj = ::Create_Render_Obj ("BUILDINGICON");
  118. if (render_obj != NULL) {
  119. //
  120. // Create the new physics object
  121. //
  122. m_PhysObj = new DecorationPhysClass;
  123. m_PhysObj->Set_Model (render_obj);
  124. m_PhysObj->Set_Collision_Group (EDITOR_COLLISION_GROUP);
  125. m_PhysObj->Peek_Model ()->Set_User_Data ((PVOID)&m_HitTestInfo, FALSE);
  126. m_PhysObj->Set_Transform (m_Transform);
  127. ::Set_Model_Collision_Type (m_PhysObj->Peek_Model (), COLLISION_TYPE_0);
  128. render_obj->Release_Ref ();
  129. }
  130. ObjectNodeClass::Initialize ();
  131. return ;
  132. }
  133. ////////////////////////////////////////////////////////////////
  134. //
  135. // Get_Factory
  136. //
  137. ////////////////////////////////////////////////////////////////
  138. const PersistFactoryClass &
  139. BuildingNodeClass::Get_Factory (void) const
  140. {
  141. return _BuildingNodePersistFactory;
  142. }
  143. /////////////////////////////////////////////////////////////////
  144. //
  145. // operator=
  146. //
  147. /////////////////////////////////////////////////////////////////
  148. const BuildingNodeClass &
  149. BuildingNodeClass::operator= (const BuildingNodeClass &src)
  150. {
  151. //
  152. // Start fresh
  153. //
  154. Free_Child_Nodes ();
  155. //
  156. // Copy the child node list
  157. //
  158. for (int index = 0; index < src.m_ChildNodes.Count (); index ++) {
  159. NodeClass *child_node = src.m_ChildNodes[index];
  160. if (child_node != NULL) {
  161. Add_Child_Node (child_node->Get_Transform ());
  162. }
  163. }
  164. NodeClass::operator= (src);
  165. return *this;
  166. }
  167. /////////////////////////////////////////////////////////////////
  168. //
  169. // Save
  170. //
  171. /////////////////////////////////////////////////////////////////
  172. bool
  173. BuildingNodeClass::Save (ChunkSaveClass &csave)
  174. {
  175. csave.Begin_Chunk (CHUNKID_BASE_CLASS);
  176. ObjectNodeClass::Save (csave);
  177. csave.End_Chunk ();
  178. csave.Begin_Chunk (CHUNKID_VARIABLES);
  179. //
  180. // Save the list of child node transforms
  181. //
  182. for (int index = 0; index < m_ChildNodes.Count (); index ++) {
  183. NodeClass *child_node = m_ChildNodes[index];
  184. if (child_node != NULL) {
  185. Matrix3D tm = child_node->Get_Transform ();
  186. WRITE_MICRO_CHUNK (csave, VARID_CHILD_NODE, tm);
  187. }
  188. }
  189. csave.End_Chunk ();
  190. return true;
  191. }
  192. /////////////////////////////////////////////////////////////////
  193. //
  194. // Load
  195. //
  196. /////////////////////////////////////////////////////////////////
  197. bool
  198. BuildingNodeClass::Load (ChunkLoadClass &cload)
  199. {
  200. while (cload.Open_Chunk ()) {
  201. switch (cload.Cur_Chunk_ID ()) {
  202. case CHUNKID_BASE_CLASS:
  203. ObjectNodeClass::Load (cload);
  204. break;
  205. case CHUNKID_VARIABLES:
  206. Load_Variables (cload);
  207. break;
  208. }
  209. cload.Close_Chunk ();
  210. }
  211. return true;
  212. }
  213. /////////////////////////////////////////////////////////////////
  214. //
  215. // Load_Variables
  216. //
  217. /////////////////////////////////////////////////////////////////
  218. bool
  219. BuildingNodeClass::Load_Variables (ChunkLoadClass &cload)
  220. {
  221. while (cload.Open_Micro_Chunk ()) {
  222. switch (cload.Cur_Micro_Chunk_ID ()) {
  223. case VARID_CHILD_NODE:
  224. {
  225. //
  226. // Read the child node's transfrom from the chunk
  227. //
  228. Matrix3D tm;
  229. cload.Read (&tm, sizeof (tm));
  230. //
  231. // Add a new child node at this location
  232. //
  233. Add_Child_Node (tm);
  234. }
  235. break;
  236. }
  237. cload.Close_Micro_Chunk ();
  238. }
  239. return true;
  240. }
  241. //////////////////////////////////////////////////////////////////////
  242. //
  243. // Pre_Export
  244. //
  245. //////////////////////////////////////////////////////////////////////
  246. void
  247. BuildingNodeClass::Pre_Export (void)
  248. {
  249. Add_Ref ();
  250. if (m_PhysObj != NULL && m_IsInScene) {
  251. ::Get_Scene_Editor ()->Remove_Object (m_PhysObj);
  252. //
  253. // Configure the building object (if necessary)
  254. //
  255. BuildingGameObj *building = Get_Building ();
  256. if (building != NULL) {
  257. //
  258. // What type of building is this?
  259. //
  260. if (building->As_VehicleFactoryGameObj () != NULL) {
  261. VehicleFactoryGameObj *airstrip = building->As_VehicleFactoryGameObj ();
  262. //
  263. // Configure the factory's creation transform
  264. //
  265. if (m_ChildNodes.Count () > 0) {
  266. airstrip->Set_Creation_TM (m_ChildNodes[0]->Get_Transform ());
  267. }
  268. } else if (building->As_RefineryGameObj () != NULL) {
  269. RefineryGameObj *refinery = building->As_RefineryGameObj ();
  270. //
  271. // Configure the refinery's docking transform
  272. //
  273. if (m_ChildNodes.Count () > 0) {
  274. refinery->Set_Dock_TM (m_ChildNodes[0]->Get_Transform ());
  275. }
  276. }
  277. }
  278. //
  279. // Pass the Pre_Export call onto any child nodes as well
  280. //
  281. for (int index = 0; index < m_ChildNodes.Count (); index ++) {
  282. NodeClass *child_node = m_ChildNodes[index];
  283. EditorLineClass *line = m_ChildLines[index];
  284. //
  285. // Remove the line from the scene
  286. //
  287. ::Get_Scene_Editor ()->Remove_Object (line);
  288. }
  289. }
  290. return ;
  291. }
  292. //////////////////////////////////////////////////////////////////////
  293. //
  294. // Post_Export
  295. //
  296. //////////////////////////////////////////////////////////////////////
  297. void
  298. BuildingNodeClass::Post_Export (void)
  299. {
  300. if (m_PhysObj != NULL && m_IsInScene) {
  301. ::Get_Scene_Editor ()->Add_Dynamic_Object (m_PhysObj);
  302. //
  303. // Pass the Post_Export call onto any child nodes as well
  304. //
  305. for (int index = 0; index < m_ChildNodes.Count (); index ++) {
  306. NodeClass *child_node = m_ChildNodes[index];
  307. EditorLineClass *line = m_ChildLines[index];
  308. //
  309. // Add the line back into the world
  310. //
  311. ::Get_Scene_Editor ()->Add_Dynamic_Object (line);
  312. }
  313. }
  314. Release_Ref ();
  315. return ;
  316. }
  317. //////////////////////////////////////////////////////////////////////
  318. //
  319. // Add_Child_Node
  320. //
  321. //////////////////////////////////////////////////////////////////////
  322. NodeClass *
  323. BuildingNodeClass::Add_Child_Node (const Matrix3D &tm)
  324. {
  325. BuildingChildNodeClass *child_node = new BuildingChildNodeClass;
  326. child_node->Initialize ();
  327. child_node->Set_Transform (tm);
  328. child_node->Set_Building (this);
  329. NodeMgrClass::Setup_Node_Identity (*child_node);
  330. m_ChildNodes.Add (child_node);
  331. //
  332. // Create and add the line from the building to the child node
  333. //
  334. EditorLineClass *line = new EditorLineClass;
  335. line->Reset (m_Transform.Get_Translation (), tm.Get_Translation ());
  336. m_ChildLines.Add (line);
  337. //
  338. // Add the objects to the scene if necessary
  339. //
  340. if (m_IsInScene) {
  341. child_node->Add_To_Scene ();
  342. ::Get_Scene_Editor ()->Add_Dynamic_Object (line);
  343. }
  344. return child_node;
  345. }
  346. //////////////////////////////////////////////////////////////////////
  347. //
  348. // Can_Add_Child_Nodes
  349. //
  350. //////////////////////////////////////////////////////////////////////
  351. bool
  352. BuildingNodeClass::Can_Add_Child_Nodes (void) const
  353. {
  354. bool retval = false;
  355. BuildingGameObj *building = Get_Building ();
  356. if (building != NULL) {
  357. //
  358. // Is this one of the types of buildings which need
  359. // child nodes?
  360. //
  361. if (building->As_VehicleFactoryGameObj () != NULL) {
  362. //
  363. // This type of building can only have one child node
  364. //
  365. if (m_ChildNodes.Count () == 0) {
  366. retval = true;
  367. }
  368. } else if (building->As_RefineryGameObj () != NULL) {
  369. //
  370. // This type of building can only have one child node
  371. //
  372. if (m_ChildNodes.Count () == 0) {
  373. retval = true;
  374. }
  375. }
  376. }
  377. return retval;
  378. }
  379. //////////////////////////////////////////////////////////////////////
  380. //
  381. // Remove_Child_Node
  382. //
  383. //////////////////////////////////////////////////////////////////////
  384. void
  385. BuildingNodeClass::Remove_Child_Node (NodeClass *child_node)
  386. {
  387. //
  388. // Try to find the child node
  389. //
  390. for (int index = 0; index < m_ChildNodes.Count (); index ++) {
  391. if (child_node == m_ChildNodes[index]) {
  392. //
  393. // Free the child node
  394. //
  395. MEMBER_RELEASE (child_node);
  396. m_ChildNodes.Delete (index);
  397. //
  398. // Remove and free the line to the child node
  399. //
  400. ::Get_Scene_Editor ()->Remove_Object (m_ChildLines[index]);
  401. m_ChildLines[index]->Release_Ref ();
  402. m_ChildLines.Delete (index);
  403. break;
  404. }
  405. }
  406. return ;
  407. }
  408. //////////////////////////////////////////////////////////////////////
  409. //
  410. // Free_Child_Nodes
  411. //
  412. //////////////////////////////////////////////////////////////////////
  413. void
  414. BuildingNodeClass::Free_Child_Nodes (void)
  415. {
  416. SceneEditorClass *scene = ::Get_Scene_Editor ();
  417. //
  418. // Release our hold on each child node
  419. //
  420. for (int index = 0; index < m_ChildNodes.Count (); index ++) {
  421. NodeClass *child_node = m_ChildNodes[index];
  422. EditorLineClass *line = m_ChildLines[index];
  423. scene->Remove_Object (line);
  424. child_node->Remove_From_Scene ();
  425. MEMBER_RELEASE (child_node);
  426. MEMBER_RELEASE (line);
  427. }
  428. //
  429. // Remove all the child nodes from the list
  430. //
  431. m_ChildNodes.Delete_All ();
  432. m_ChildLines.Delete_All ();
  433. return ;
  434. }
  435. //////////////////////////////////////////////////////////////////////////////
  436. //
  437. // Add_To_Scene
  438. //
  439. //////////////////////////////////////////////////////////////////////////////
  440. void
  441. BuildingNodeClass::Add_To_Scene (void)
  442. {
  443. SceneEditorClass *scene = ::Get_Scene_Editor ();
  444. //
  445. // Add all the waypoints to the scene
  446. //
  447. for (int index = 0; index < m_ChildNodes.Count (); index ++) {
  448. NodeClass *child_node = m_ChildNodes[index];
  449. EditorLineClass *line = m_ChildLines[index];
  450. child_node->Add_To_Scene ();
  451. scene->Add_Dynamic_Object (line);
  452. }
  453. NodeClass::Add_To_Scene ();
  454. return ;
  455. }
  456. //////////////////////////////////////////////////////////////////////////////
  457. //
  458. // Remove_From_Scene
  459. //
  460. //////////////////////////////////////////////////////////////////////////////
  461. void
  462. BuildingNodeClass::Remove_From_Scene (void)
  463. {
  464. SceneEditorClass *scene = ::Get_Scene_Editor ();
  465. if (scene != NULL && m_IsInScene) {
  466. //
  467. // Remove all the waypoints from the scene
  468. //
  469. for (int index = 0; index < m_ChildNodes.Count (); index ++) {
  470. NodeClass *child_node = m_ChildNodes[index];
  471. EditorLineClass *line = m_ChildLines[index];
  472. child_node->Remove_From_Scene ();
  473. scene->Remove_Object (line);
  474. }
  475. }
  476. NodeClass::Remove_From_Scene ();
  477. return ;
  478. }
  479. //////////////////////////////////////////////////////////////////////////////
  480. //
  481. // Update_Lines
  482. //
  483. //////////////////////////////////////////////////////////////////////////////
  484. void
  485. BuildingNodeClass::Update_Lines (void)
  486. {
  487. for (int index = 0; index < m_ChildNodes.Count (); index ++) {
  488. NodeClass *child_node = m_ChildNodes[index];
  489. EditorLineClass *line = m_ChildLines[index];
  490. //
  491. // Update the line between the node and its child
  492. //
  493. line->Reset (Get_Transform ().Get_Translation (), child_node->Get_Position ());
  494. }
  495. return ;
  496. }
  497. //////////////////////////////////////////////////////////////////////////////
  498. //
  499. // Hide
  500. //
  501. //////////////////////////////////////////////////////////////////////////////
  502. void
  503. BuildingNodeClass::Hide (bool hide)
  504. {
  505. //
  506. // Apply the same operation to all the child nodes and lines
  507. //
  508. for (int index = 0; index < m_ChildNodes.Count (); index ++) {
  509. NodeClass *child_node = m_ChildNodes[index];
  510. EditorLineClass *line = m_ChildLines[index];
  511. child_node->Hide (hide);
  512. line->Hide (hide);
  513. }
  514. NodeClass::Hide (hide);
  515. return ;
  516. }
  517. //////////////////////////////////////////////////////////////////////////////
  518. //
  519. // Get_Sub_Node
  520. //
  521. //////////////////////////////////////////////////////////////////////////////
  522. NodeClass *
  523. BuildingNodeClass::Get_Sub_Node (int index)
  524. {
  525. return m_ChildNodes[index];
  526. }