ObjectNode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/ObjectNode.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 5/22/01 11:17a $*
  29. * *
  30. * $Revision:: 20 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "objectnode.h"
  37. #include "sceneeditor.h"
  38. #include "tiledefinition.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 "editorchunkids.h"
  47. #include "preset.h"
  48. #include "editscript.h"
  49. #include "nodeinfopage.h"
  50. #include "nodescriptsproppage.h"
  51. #include "editorpropsheet.h"
  52. #include "positionpage.h"
  53. #include "modelutils.h"
  54. //////////////////////////////////////////////////////////////////////////////
  55. // Persist factory
  56. //////////////////////////////////////////////////////////////////////////////
  57. SimplePersistFactoryClass<ObjectNodeClass, CHUNKID_NODE_OBJECTS> _ObjectNodePersistFactory;
  58. enum
  59. {
  60. CHUNKID_VARIABLES = 0x10271102,
  61. CHUNKID_BASE_CLASS,
  62. CHUNKID_SCRIPT
  63. };
  64. //////////////////////////////////////////////////////////////////////////////
  65. //
  66. // ObjectNodeClass
  67. //
  68. //////////////////////////////////////////////////////////////////////////////
  69. ObjectNodeClass::ObjectNodeClass (PresetClass *preset)
  70. : m_GameObj (NULL),
  71. NodeClass (preset)
  72. {
  73. return ;
  74. }
  75. //////////////////////////////////////////////////////////////////////////////
  76. //
  77. // ObjectNodeClass
  78. //
  79. //////////////////////////////////////////////////////////////////////////////
  80. ObjectNodeClass::ObjectNodeClass (const ObjectNodeClass &src)
  81. : m_GameObj (NULL),
  82. NodeClass (NULL)
  83. {
  84. *this = src;
  85. return ;
  86. }
  87. //////////////////////////////////////////////////////////////////////////////
  88. //
  89. // ~ObjectNodeClass
  90. //
  91. //////////////////////////////////////////////////////////////////////////////
  92. ObjectNodeClass::~ObjectNodeClass (void)
  93. {
  94. Remove_From_Scene ();
  95. Destroy_Game_Obj ();
  96. Free_Scripts ();
  97. return ;
  98. }
  99. //////////////////////////////////////////////////////////////////////////////
  100. //
  101. // Initialize
  102. //
  103. // Note: This may be called more than once. It is used as an 'initialize'
  104. // and a 're-initialize'.
  105. //
  106. //////////////////////////////////////////////////////////////////////////////
  107. void
  108. ObjectNodeClass::Initialize (void)
  109. {
  110. Destroy_Game_Obj ();
  111. if (m_Preset != NULL) {
  112. m_Preset->Load_All_Assets ();
  113. Create_Game_Obj ();
  114. }
  115. return ;
  116. }
  117. ////////////////////////////////////////////////////////////////
  118. //
  119. // Get_Factory
  120. //
  121. ////////////////////////////////////////////////////////////////
  122. const PersistFactoryClass &
  123. ObjectNodeClass::Get_Factory (void) const
  124. {
  125. return _ObjectNodePersistFactory;
  126. }
  127. ////////////////////////////////////////////////////////////////
  128. //
  129. // Show_Settings_Dialog
  130. //
  131. ////////////////////////////////////////////////////////////////
  132. bool
  133. ObjectNodeClass::Show_Settings_Dialog (void)
  134. {
  135. NodeInfoPageClass info_tab (this);
  136. PositionPageClass pos_tab (this);
  137. NodeScriptsPropPage scripts_tab (&m_Scripts);
  138. EditorPropSheetClass prop_sheet;
  139. prop_sheet.Add_Page (&info_tab);
  140. prop_sheet.Add_Page (&pos_tab);
  141. prop_sheet.Add_Page (&scripts_tab);
  142. // Show the property sheet
  143. UINT ret_code = prop_sheet.DoModal ();
  144. if (ret_code == IDOK) {
  145. //
  146. // If the scripts changed, then we need to
  147. // reload the object and assign it the new
  148. // set of scripts.
  149. //
  150. Reload ();
  151. }
  152. // Return true if the user clicked OK
  153. return (ret_code == IDOK);
  154. }
  155. /////////////////////////////////////////////////////////////////
  156. //
  157. // Save
  158. //
  159. /////////////////////////////////////////////////////////////////
  160. bool
  161. ObjectNodeClass::Save (ChunkSaveClass &csave)
  162. {
  163. csave.Begin_Chunk (CHUNKID_BASE_CLASS);
  164. NodeClass::Save (csave);
  165. csave.End_Chunk ();
  166. //
  167. // Save the list of scripts
  168. //
  169. for (int index = 0; index < m_Scripts.Count (); index ++) {
  170. EditScriptClass *script = m_Scripts[index];
  171. //
  172. // Have this script save itself
  173. //
  174. csave.Begin_Chunk (CHUNKID_SCRIPT);
  175. script->Save (csave);
  176. csave.End_Chunk ();
  177. }
  178. return true;
  179. }
  180. /////////////////////////////////////////////////////////////////
  181. //
  182. // Load
  183. //
  184. /////////////////////////////////////////////////////////////////
  185. bool
  186. ObjectNodeClass::Load (ChunkLoadClass &cload)
  187. {
  188. Free_Scripts ();
  189. while (cload.Open_Chunk ()) {
  190. switch (cload.Cur_Chunk_ID ()) {
  191. case CHUNKID_BASE_CLASS:
  192. NodeClass::Load (cload);
  193. break;
  194. case CHUNKID_SCRIPT:
  195. {
  196. EditScriptClass *script = new EditScriptClass;
  197. if (script->Load (cload)) {
  198. m_Scripts.Add (script);
  199. }
  200. }
  201. break;
  202. }
  203. cload.Close_Chunk ();
  204. }
  205. return true;
  206. }
  207. /////////////////////////////////////////////////////////////////
  208. //
  209. // Free_Scripts
  210. //
  211. /////////////////////////////////////////////////////////////////
  212. void
  213. ObjectNodeClass::Free_Scripts (void)
  214. {
  215. for (int index = 0; index < m_Scripts.Count (); index ++) {
  216. EditScriptClass *script = m_Scripts[index];
  217. SAFE_DELETE (script);
  218. }
  219. m_Scripts.Delete_All ();
  220. return ;
  221. }
  222. /////////////////////////////////////////////////////////////////
  223. //
  224. // Assign_Scripts
  225. //
  226. /////////////////////////////////////////////////////////////////
  227. void
  228. ObjectNodeClass::Assign_Scripts (void)
  229. {
  230. if (m_GameObj != NULL) {
  231. for (int index = 0; index < m_Scripts.Count (); index ++) {
  232. EditScriptClass *script = m_Scripts[index];
  233. //
  234. // Create the game script and pass it onto the game obj
  235. //
  236. ScriptClass *game_script = script->Create_Script ();
  237. if (game_script != NULL) {
  238. m_GameObj->Add_Observer (game_script);
  239. }
  240. }
  241. }
  242. return ;
  243. }
  244. /////////////////////////////////////////////////////////////////
  245. //
  246. // Copy_Scripts
  247. //
  248. /////////////////////////////////////////////////////////////////
  249. void
  250. ObjectNodeClass::Copy_Scripts (const ObjectNodeClass &src)
  251. {
  252. Free_Scripts ();
  253. //
  254. // Loop over all the scripts in the src object and copy them.
  255. //
  256. for (int index = 0; index < src.m_Scripts.Count (); index ++) {
  257. EditScriptClass *script = src.m_Scripts[index];
  258. if (script != NULL) {
  259. //
  260. // Make ourselves a copy of the script
  261. //
  262. EditScriptClass *our_copy = new EditScriptClass (*script);
  263. m_Scripts.Add (our_copy);
  264. }
  265. }
  266. Assign_Scripts ();
  267. return ;
  268. }
  269. /////////////////////////////////////////////////////////////////
  270. //
  271. // operator=
  272. //
  273. /////////////////////////////////////////////////////////////////
  274. const ObjectNodeClass &
  275. ObjectNodeClass::operator= (const ObjectNodeClass &src)
  276. {
  277. //
  278. // Copy the script list from the source object
  279. //
  280. Copy_Scripts (src);
  281. NodeClass::operator= (src);
  282. return *this;
  283. }
  284. //////////////////////////////////////////////////////////////////////
  285. //
  286. // Add_To_Scene
  287. //
  288. //////////////////////////////////////////////////////////////////////
  289. void
  290. ObjectNodeClass::Add_To_Scene (void)
  291. {
  292. Create_Game_Obj ();
  293. NodeClass::Add_To_Scene ();
  294. return ;
  295. }
  296. //////////////////////////////////////////////////////////////////////
  297. //
  298. // Remove_From_Scene
  299. //
  300. //////////////////////////////////////////////////////////////////////
  301. void
  302. ObjectNodeClass::Remove_From_Scene (void)
  303. {
  304. Destroy_Game_Obj ();
  305. NodeClass::Remove_From_Scene ();
  306. return ;
  307. }
  308. //////////////////////////////////////////////////////////////////////
  309. //
  310. // Create_Game_Obj
  311. //
  312. //////////////////////////////////////////////////////////////////////
  313. void
  314. ObjectNodeClass::Create_Game_Obj (void)
  315. {
  316. if (m_GameObj == NULL) {
  317. DefinitionClass *definition = m_Preset->Get_Definition ();
  318. if (definition != NULL) {
  319. //
  320. // Create the game object
  321. //
  322. m_GameObj = (ScriptableGameObj *)::Instance_Definition (definition);
  323. //
  324. // Assign 'hit-test' information to this game object
  325. //
  326. if (m_GameObj != NULL) {
  327. PhysClass *phys_obj = Peek_Physics_Obj ();
  328. RenderObjClass *render_obj = phys_obj->Peek_Model ();
  329. if (render_obj != NULL) {
  330. render_obj->Set_User_Data ((PVOID)&m_HitTestInfo, FALSE);
  331. ::Set_Model_Collision_Type (render_obj, COLLISION_TYPE_6);
  332. }
  333. //
  334. // Make sure the physics object has the correct position
  335. //
  336. Set_Transform (m_Transform);
  337. m_GameObj->Set_ID (m_ID);
  338. PhysicalGameObj *game_obj = Peek_Game_Obj ();
  339. if (game_obj != NULL) {
  340. game_obj->Startup ();
  341. }
  342. //
  343. // Make sure we don't put the object into the scene prematurely
  344. //
  345. if (phys_obj->Get_Culling_System () != NULL) {
  346. ::Get_Scene_Editor ()->Remove_Object (phys_obj);
  347. }
  348. }
  349. Assign_Scripts ();
  350. }
  351. }
  352. return ;
  353. }
  354. //////////////////////////////////////////////////////////////////////
  355. //
  356. // Destroy_Game_Obj
  357. //
  358. //////////////////////////////////////////////////////////////////////
  359. void
  360. ObjectNodeClass::Destroy_Game_Obj (void)
  361. {
  362. if (m_GameObj != NULL) {
  363. m_GameObj->Set_Delete_Pending ();
  364. m_GameObj = NULL;
  365. }
  366. return ;
  367. }
  368. /////////////////////////////////////////////////////////////////
  369. //
  370. // Set_ID
  371. //
  372. /////////////////////////////////////////////////////////////////
  373. void
  374. ObjectNodeClass::Set_ID (uint32 id)
  375. {
  376. NodeClass::Set_ID (id);
  377. if (m_GameObj != NULL) {
  378. m_GameObj->Set_ID (m_ID);
  379. }
  380. return ;
  381. }