ZoneNode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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/ZoneNode.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 5/22/01 11:17a $*
  29. * *
  30. * $Revision:: 8 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "zonenode.h"
  37. #include "sceneeditor.h"
  38. #include "filemgr.h"
  39. #include "_assetmgr.h"
  40. #include "editorassetmgr.h"
  41. #include "w3d_file.h"
  42. #include "cameramgr.h"
  43. #include "collisiongroups.h"
  44. #include "persistfactory.h"
  45. #include "editorchunkids.h"
  46. #include "preset.h"
  47. #include "presetmgr.h"
  48. #include "decophys.h"
  49. #include "scriptzone.h"
  50. #include "chunkio.h"
  51. #include "nodemgr.h"
  52. //////////////////////////////////////////////////////////////////////////////
  53. // Persist factory
  54. //////////////////////////////////////////////////////////////////////////////
  55. SimplePersistFactoryClass<ZoneNodeClass, CHUNKID_NODE_ZONE> _ZoneNodePersistFactory;
  56. enum
  57. {
  58. CHUNKID_VARIABLES = 0x11110255,
  59. CHUNKID_BASE_CLASS
  60. };
  61. enum
  62. {
  63. VARID_ZONE_SIZE = 1
  64. };
  65. //////////////////////////////////////////////////////////////////////////////
  66. //
  67. // ZoneNodeClass
  68. //
  69. //////////////////////////////////////////////////////////////////////////////
  70. ZoneNodeClass::ZoneNodeClass (PresetClass *preset)
  71. : m_PhysObj (NULL),
  72. m_CachedSize (1, 1, 1),
  73. ObjectNodeClass (preset)
  74. {
  75. return ;
  76. }
  77. //////////////////////////////////////////////////////////////////////////////
  78. //
  79. // ZoneNodeClass
  80. //
  81. //////////////////////////////////////////////////////////////////////////////
  82. ZoneNodeClass::ZoneNodeClass (const ZoneNodeClass &src)
  83. : m_PhysObj (NULL),
  84. m_CachedSize (1, 1, 1),
  85. ObjectNodeClass (NULL)
  86. {
  87. *this = src;
  88. return ;
  89. }
  90. //////////////////////////////////////////////////////////////////////////////
  91. //
  92. // ~ZoneNodeClass
  93. //
  94. //////////////////////////////////////////////////////////////////////////////
  95. ZoneNodeClass::~ZoneNodeClass (void)
  96. {
  97. Remove_From_Scene ();
  98. MEMBER_RELEASE (m_PhysObj);
  99. return ;
  100. }
  101. //////////////////////////////////////////////////////////////////////////////
  102. //
  103. // Initialize
  104. //
  105. // Note: This may be called more than once. It is used as an 'initialize'
  106. // and a 're-initialize'.
  107. //
  108. //////////////////////////////////////////////////////////////////////////////
  109. void
  110. ZoneNodeClass::Initialize (void)
  111. {
  112. MEMBER_RELEASE (m_PhysObj);
  113. if (m_GameObj != NULL) {
  114. m_GameObj->Set_Delete_Pending ();
  115. m_GameObj = NULL;
  116. }
  117. DefinitionClass *definition = m_Preset->Get_Definition ();
  118. if (definition != NULL) {
  119. //
  120. // Create the game object
  121. //
  122. m_GameObj = (PhysicalGameObj *)definition->Create ();
  123. //
  124. // Assign 'hit-test' information to this game object
  125. //
  126. if (m_GameObj != NULL) {
  127. //
  128. // Create the new box physics object
  129. //
  130. m_PhysObj = new Box3DPhysClass;
  131. m_PhysObj->Set_Collision_Group (EDITOR_COLLISION_GROUP);
  132. m_PhysObj->Peek_Model ()->Set_User_Data ((PVOID)&m_HitTestInfo, FALSE);
  133. m_PhysObj->Peek_Model ()->Set_Collision_Type (COLLISION_TYPE_6);
  134. m_PhysObj->Set_Transform (m_Transform);
  135. m_PhysObj->Get_Box ()->Set_Color (((ScriptZoneGameObjDef *)definition)->Get_Color ());
  136. m_PhysObj->Get_Box ()->Set_Dimensions (m_CachedSize);
  137. //
  138. // Make sure the physics object has the correct position
  139. //
  140. Set_Transform (m_Transform);
  141. //
  142. // Update the zone object we embed
  143. //
  144. Update_Zone_Obj ();
  145. Get_Zone_Obj ()->Set_ID (m_ID);
  146. }
  147. Assign_Scripts ();
  148. }
  149. return ;
  150. }
  151. ////////////////////////////////////////////////////////////////
  152. //
  153. // Get_Factory
  154. //
  155. ////////////////////////////////////////////////////////////////
  156. const PersistFactoryClass &
  157. ZoneNodeClass::Get_Factory (void) const
  158. {
  159. return _ZoneNodePersistFactory;
  160. }
  161. /////////////////////////////////////////////////////////////////
  162. //
  163. // operator=
  164. //
  165. /////////////////////////////////////////////////////////////////
  166. const ZoneNodeClass &
  167. ZoneNodeClass::operator= (const ZoneNodeClass &src)
  168. {
  169. m_CachedSize = src.m_CachedSize;
  170. ObjectNodeClass::operator= (src);
  171. return *this;
  172. }
  173. //////////////////////////////////////////////////////////////////////
  174. //
  175. // Add_To_Scene
  176. //
  177. //////////////////////////////////////////////////////////////////////
  178. void
  179. ZoneNodeClass::Add_To_Scene (void)
  180. {
  181. m_GrabHandles.Position_Around_Node (this);
  182. ObjectNodeClass::Add_To_Scene ();
  183. return ;
  184. }
  185. //////////////////////////////////////////////////////////////////////
  186. //
  187. // Remove_From_Scene
  188. //
  189. //////////////////////////////////////////////////////////////////////
  190. void
  191. ZoneNodeClass::Remove_From_Scene (void)
  192. {
  193. m_GrabHandles.Remove_From_Scene ();
  194. ObjectNodeClass::Remove_From_Scene ();
  195. return ;
  196. }
  197. //////////////////////////////////////////////////////////////////////
  198. //
  199. // On_Vertex_Drag_Begin
  200. //
  201. //////////////////////////////////////////////////////////////////////
  202. void
  203. ZoneNodeClass::On_Vertex_Drag_Begin (int vertex_index)
  204. {
  205. //
  206. // Store the location of the 'locked' vertex, so we can
  207. // use this when resizing the box
  208. //
  209. m_FirstPoint = m_PhysObj->Get_Box ()->Get_Vertex_Lock_Position (vertex_index);
  210. return ;
  211. }
  212. //////////////////////////////////////////////////////////////////////
  213. //
  214. // On_Vertex_Drag
  215. //
  216. //////////////////////////////////////////////////////////////////////
  217. void
  218. ZoneNodeClass::On_Vertex_Drag
  219. (
  220. int vertex_index,
  221. POINT point
  222. )
  223. {
  224. //
  225. // Resize the box render object based on the new point
  226. //
  227. Box3DClass *box = m_PhysObj->Get_Box ();
  228. if (::GetKeyState (VK_SHIFT) < 0) {
  229. box->Drag_VertexZ (vertex_index, point, m_FirstPoint);
  230. } else {
  231. box->Drag_VertexXY (vertex_index, point, m_FirstPoint);
  232. }
  233. //
  234. // Position the grab handles around ourselves
  235. //
  236. m_GrabHandles.Position_Around_Node (this);
  237. //
  238. // Update the zone's bounding box
  239. //
  240. m_Transform = box->Get_Transform ();
  241. Update_Zone_Obj ();
  242. //
  243. // Cache the size of the box
  244. //
  245. m_CachedSize = box->Get_Dimensions ();
  246. return ;
  247. }
  248. /////////////////////////////////////////////////////////////////
  249. //
  250. // Update_Zone_Obj
  251. //
  252. /////////////////////////////////////////////////////////////////
  253. void
  254. ZoneNodeClass::Update_Zone_Obj (void)
  255. {
  256. ScriptZoneGameObj *zone_obj = Get_Zone_Obj ();
  257. Box3DClass *box = m_PhysObj->Get_Box ();
  258. //
  259. // Update the zone's bounding box
  260. //
  261. if (box != NULL && zone_obj != NULL) {
  262. Vector3 pos = box->Get_Transform ().Get_Translation ();
  263. Vector3 size = box->Get_Dimensions () / 2;
  264. OBBoxClass obbox;
  265. obbox.Center = pos;
  266. obbox.Extent = size;
  267. obbox.Basis.Set (m_Transform);
  268. zone_obj->Set_Bounding_Box (obbox);
  269. }
  270. return ;
  271. }
  272. /////////////////////////////////////////////////////////////////
  273. //
  274. // Save
  275. //
  276. /////////////////////////////////////////////////////////////////
  277. bool
  278. ZoneNodeClass::Save (ChunkSaveClass &csave)
  279. {
  280. csave.Begin_Chunk (CHUNKID_BASE_CLASS);
  281. ObjectNodeClass::Save (csave);
  282. csave.End_Chunk ();
  283. csave.Begin_Chunk (CHUNKID_VARIABLES);
  284. WRITE_MICRO_CHUNK (csave, VARID_ZONE_SIZE, m_CachedSize);
  285. csave.End_Chunk ();
  286. return true;
  287. }
  288. /////////////////////////////////////////////////////////////////
  289. //
  290. // Load
  291. //
  292. /////////////////////////////////////////////////////////////////
  293. bool
  294. ZoneNodeClass::Load (ChunkLoadClass &cload)
  295. {
  296. while (cload.Open_Chunk ()) {
  297. switch (cload.Cur_Chunk_ID ()) {
  298. case CHUNKID_BASE_CLASS:
  299. ObjectNodeClass::Load (cload);
  300. break;
  301. case CHUNKID_VARIABLES:
  302. {
  303. //
  304. // Read all the variables from their micro-chunks
  305. //
  306. while (cload.Open_Micro_Chunk ()) {
  307. switch (cload.Cur_Micro_Chunk_ID ()) {
  308. READ_MICRO_CHUNK (cload, VARID_ZONE_SIZE, m_CachedSize);
  309. }
  310. cload.Close_Micro_Chunk ();
  311. }
  312. }
  313. break;
  314. }
  315. cload.Close_Chunk ();
  316. }
  317. return true;
  318. }
  319. //////////////////////////////////////////////////////////////////////
  320. //
  321. // Pre_Export
  322. //
  323. //////////////////////////////////////////////////////////////////////
  324. void
  325. ZoneNodeClass::Pre_Export (void)
  326. {
  327. //
  328. // Make sure the game object's size and position is up to date
  329. //
  330. Update_Zone_Obj ();
  331. //
  332. // Remove ourselves from the 'system' so we don't get accidentally
  333. // saved during the export.
  334. //
  335. Add_Ref ();
  336. if (m_PhysObj != NULL && m_IsInScene) {
  337. ::Get_Scene_Editor ()->Remove_Object (m_PhysObj);
  338. m_GrabHandles.Remove_From_Scene ();
  339. }
  340. return ;
  341. }
  342. //////////////////////////////////////////////////////////////////////
  343. //
  344. // Post_Export
  345. //
  346. //////////////////////////////////////////////////////////////////////
  347. void
  348. ZoneNodeClass::Post_Export (void)
  349. {
  350. //
  351. // Put ourselves back into the system
  352. //
  353. if (m_PhysObj != NULL && m_IsInScene) {
  354. ::Get_Scene_Editor ()->Add_Dynamic_Object (m_PhysObj);
  355. m_GrabHandles.Position_Around_Node (this);
  356. }
  357. Release_Ref ();
  358. return ;
  359. }
  360. //////////////////////////////////////////////////////////////////////
  361. //
  362. // Hide
  363. //
  364. //////////////////////////////////////////////////////////////////////
  365. void
  366. ZoneNodeClass::Hide (bool hide)
  367. {
  368. m_GrabHandles.Hide (hide);
  369. NodeClass::Hide (hide);
  370. return ;
  371. }