decophys.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/decophys.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 1/05/02 5:12p $*
  29. * *
  30. * $Revision:: 20 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "decophys.h"
  36. #include "rendobj.h"
  37. #include "persistfactory.h"
  38. #include "simpledefinitionfactory.h"
  39. #include "wwphysids.h"
  40. #include "wwhack.h"
  41. #include "part_emt.h"
  42. #include "physinttest.h"
  43. DECLARE_FORCE_LINK(decophys);
  44. /****************************************************************************************************
  45. **
  46. ** DecorationPhysClass Implementation
  47. **
  48. ****************************************************************************************************/
  49. /*
  50. ** Persist factory for DecorationPhysClass
  51. */
  52. SimplePersistFactoryClass<DecorationPhysClass,PHYSICS_CHUNKID_DECORATIONPHYS> _DecoPhysFactory;
  53. /*
  54. ** Chunk-ID's used by DecoPhys
  55. */
  56. enum
  57. {
  58. DECOPHYS_CHUNK_PHYS = 0x005060000, // old parent class data
  59. DECOPHYS_CHUNK_DYNAMICPHYS, // current parent class data
  60. };
  61. DecorationPhysClass::DecorationPhysClass(void)
  62. {
  63. ObjSpaceWorldBox.Center.Set(0,0,0);
  64. ObjSpaceWorldBox.Extent.Set(1,1,1);
  65. }
  66. void DecorationPhysClass::Init(const DecorationPhysDefClass & def)
  67. {
  68. DynamicPhysClass::Init(def);
  69. }
  70. void DecorationPhysClass::Set_Model(RenderObjClass * model)
  71. {
  72. DynamicPhysClass::Set_Model(model);
  73. // Initialize our copy of the world box
  74. if (Model != NULL) {
  75. RenderObjClass * box = Model->Get_Sub_Object_By_Name("WORLDBOX");
  76. if (box) {
  77. // Get the box when the model has an identity transform
  78. Matrix3D old_transform = Model->Get_Transform();
  79. Model->Set_Transform(Matrix3D(1));
  80. ObjSpaceWorldBox = box->Get_Bounding_Box();
  81. Model->Set_Transform(old_transform);
  82. box->Release_Ref();
  83. } else {
  84. Model->Get_Obj_Space_Bounding_Box(ObjSpaceWorldBox);
  85. }
  86. }
  87. }
  88. const AABoxClass & DecorationPhysClass::Get_Bounding_Box(void) const
  89. {
  90. assert(Model);
  91. return Model->Get_Bounding_Box();
  92. }
  93. const Matrix3D & DecorationPhysClass::Get_Transform(void) const
  94. {
  95. assert(Model);
  96. return Model->Get_Transform();
  97. }
  98. void DecorationPhysClass::Set_Transform(const Matrix3D & m)
  99. {
  100. // Note: this kind of object never causes collisions so we
  101. // can just warp it to the users desired position. However,
  102. // we do need to tell the scene that we moved so that
  103. // it can update us in the culling system
  104. WWASSERT(Model);
  105. Model->Set_Transform(m);
  106. Update_Cull_Box();
  107. Update_Visibility_Status();
  108. }
  109. void DecorationPhysClass::Get_Shadow_Blob_Box(AABoxClass * set_obj_space_box)
  110. {
  111. if (set_obj_space_box != NULL) {
  112. *set_obj_space_box = ObjSpaceWorldBox;
  113. }
  114. }
  115. bool DecorationPhysClass::Intersection_Test(PhysAABoxIntersectionTestClass & test)
  116. {
  117. WWASSERT(Model);
  118. if (Model->Intersect_AABox(test)) {
  119. test.Add_Intersected_Object(this);
  120. return true;
  121. }
  122. return false;
  123. }
  124. bool DecorationPhysClass::Intersection_Test(PhysOBBoxIntersectionTestClass & test)
  125. {
  126. WWASSERT(Model);
  127. if (Model->Intersect_OBBox(test)) {
  128. test.Add_Intersected_Object(this);
  129. return true;
  130. }
  131. return false;
  132. }
  133. bool DecorationPhysClass::Cast_Ray(PhysRayCollisionTestClass & raytest)
  134. {
  135. WWASSERT(Model);
  136. if (Model->Cast_Ray(raytest)) {
  137. raytest.CollidedPhysObj = this;
  138. return true;
  139. }
  140. return false;
  141. }
  142. bool DecorationPhysClass::Cast_AABox(PhysAABoxCollisionTestClass & boxtest)
  143. {
  144. WWASSERT(Model);
  145. if (Model->Cast_AABox(boxtest)) {
  146. boxtest.CollidedPhysObj = this;
  147. return true;
  148. }
  149. return false;
  150. }
  151. bool DecorationPhysClass::Cast_OBBox(PhysOBBoxCollisionTestClass & boxtest)
  152. {
  153. WWASSERT(Model);
  154. if (Model->Cast_OBBox(boxtest)) {
  155. boxtest.CollidedPhysObj = this;
  156. return true;
  157. }
  158. return false;
  159. }
  160. const PersistFactoryClass & DecorationPhysClass::Get_Factory (void) const
  161. {
  162. return _DecoPhysFactory;
  163. }
  164. bool DecorationPhysClass::Save (ChunkSaveClass &csave)
  165. {
  166. csave.Begin_Chunk(DECOPHYS_CHUNK_DYNAMICPHYS);
  167. DynamicPhysClass::Save(csave);
  168. csave.End_Chunk();
  169. return true;
  170. }
  171. bool DecorationPhysClass::Load (ChunkLoadClass &cload)
  172. {
  173. while (cload.Open_Chunk()) {
  174. switch(cload.Cur_Chunk_ID())
  175. {
  176. case DECOPHYS_CHUNK_PHYS:
  177. PhysClass::Load(cload);
  178. break;
  179. case DECOPHYS_CHUNK_DYNAMICPHYS:
  180. DynamicPhysClass::Load(cload);
  181. break;
  182. default:
  183. WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  184. break;
  185. }
  186. cload.Close_Chunk();
  187. }
  188. SaveLoadSystemClass::Register_Post_Load_Callback(this);
  189. return true;
  190. }
  191. void DecorationPhysClass::On_Post_Load(void)
  192. {
  193. DynamicPhysClass::On_Post_Load();
  194. WWASSERT(Model);
  195. if (Model) {
  196. Set_Cull_Box(Model->Get_Bounding_Box());
  197. if (Model->Class_ID () == RenderObjClass::CLASSID_PARTICLEEMITTER) {
  198. Model->Set_User_Data (this);
  199. ((ParticleEmitterClass *)Model)->Start ();
  200. }
  201. }
  202. }
  203. /****************************************************************************************************
  204. **
  205. ** DecorationPhysDefClass Implementation
  206. **
  207. ****************************************************************************************************/
  208. /*
  209. ** Persist factory for DecorationPhysDefClass's
  210. */
  211. SimplePersistFactoryClass<DecorationPhysDefClass,PHYSICS_CHUNKID_DECOPHYSDEF> _DecorationPhysDefFactory;
  212. /*
  213. ** Definition factory for DecorationPhysDefClass. This makes it show up in the editor
  214. */
  215. DECLARE_DEFINITION_FACTORY(DecorationPhysDefClass, CLASSID_DECOPHYSDEF, "DecorationPhys") _DecorationPhysDefDefFactory;
  216. /*
  217. ** Chunk ID's used by DecorationPhysDefClass
  218. */
  219. enum
  220. {
  221. DECORATIONPHYSDEF_CHUNK_PHYSDEF = 0x01070003, // old parent class
  222. DECORATIONPHYSDEF_CHUNK_DYNAMICPHYSDEF, // current parent class
  223. };
  224. DecorationPhysDefClass::DecorationPhysDefClass(void)
  225. {
  226. }
  227. uint32 DecorationPhysDefClass::Get_Class_ID (void) const
  228. {
  229. return CLASSID_DECOPHYSDEF;
  230. }
  231. PersistClass * DecorationPhysDefClass::Create(void) const
  232. {
  233. DecorationPhysClass * new_obj = NEW_REF(DecorationPhysClass,());
  234. new_obj->Init(*this);
  235. return new_obj;
  236. }
  237. const char * DecorationPhysDefClass::Get_Type_Name(void)
  238. {
  239. return "DecorationPhysDef";
  240. }
  241. bool DecorationPhysDefClass::Is_Type(const char * type_name)
  242. {
  243. if (stricmp(type_name,DecorationPhysDefClass::Get_Type_Name()) == 0) {
  244. return true;
  245. } else {
  246. return DynamicPhysDefClass::Is_Type(type_name);
  247. }
  248. }
  249. const PersistFactoryClass & DecorationPhysDefClass::Get_Factory (void) const
  250. {
  251. return _DecorationPhysDefFactory;
  252. }
  253. bool DecorationPhysDefClass::Save(ChunkSaveClass &csave)
  254. {
  255. csave.Begin_Chunk(DECORATIONPHYSDEF_CHUNK_DYNAMICPHYSDEF);
  256. DynamicPhysDefClass::Save(csave);
  257. csave.End_Chunk();
  258. return true;
  259. }
  260. bool DecorationPhysDefClass::Load(ChunkLoadClass &cload)
  261. {
  262. while (cload.Open_Chunk()) {
  263. switch(cload.Cur_Chunk_ID()) {
  264. case DECORATIONPHYSDEF_CHUNK_PHYSDEF:
  265. PhysDefClass::Load(cload);
  266. break;
  267. case DECORATIONPHYSDEF_CHUNK_DYNAMICPHYSDEF:
  268. DynamicPhysDefClass::Load(cload);
  269. break;
  270. default:
  271. WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  272. break;
  273. }
  274. cload.Close_Chunk();
  275. }
  276. return true;
  277. }