DamageZoneNode.cpp 12 KB

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