PathfindSector.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/wwphys/PathfindSector.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 4/23/01 11:52a $*
  29. * *
  30. * $Revision:: 12 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "pathfindsector.h"
  36. #include "phys3.h"
  37. #include "boxrobj.h"
  38. #include "chunkio.h"
  39. #include "wwphysids.h"
  40. #include "pscene.h"
  41. #include "decophys.h"
  42. #include "pathfind.h"
  43. #include "waypath.h"
  44. #include "pathfindportal.h"
  45. ///////////////////////////////////////////////////////////////////////////
  46. // Save/Load stuff
  47. ///////////////////////////////////////////////////////////////////////////
  48. enum
  49. {
  50. CHUNKID_VARIABLES = 0x01060643,
  51. CHUNKID_WP_VARIABLES = 0x01030550,
  52. CHUNKID_WP_PARENT
  53. };
  54. enum
  55. {
  56. VARID_BOUNDING_BOX = 1,
  57. VARID_PORTAL_ID,
  58. VARID_WAYPATH_ID = 1,
  59. };
  60. ////////////////////////////////////////////////////////////////////////////////////
  61. //
  62. // ~PathfindSectorClass
  63. //
  64. ////////////////////////////////////////////////////////////////////////////////////
  65. PathfindSectorClass::~PathfindSectorClass (void)
  66. {
  67. Reset_Portal_List ();
  68. return ;
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////////
  71. //
  72. // Reset_Portal_List
  73. //
  74. ////////////////////////////////////////////////////////////////////////////////////
  75. void
  76. PathfindSectorClass::Reset_Portal_List (void)
  77. {
  78. m_PortalList.Delete_All ();
  79. return ;
  80. }
  81. ///////////////////////////////////////////////////////////////////////////
  82. //
  83. // Save
  84. //
  85. ///////////////////////////////////////////////////////////////////////////
  86. bool
  87. PathfindSectorClass::Save (ChunkSaveClass &csave)
  88. {
  89. csave.Begin_Chunk (CHUNKID_VARIABLES);
  90. //
  91. // Write the bounding box out to the chunk
  92. //
  93. AABoxClass bounding_box = Get_Cull_Box ();
  94. WRITE_MICRO_CHUNK (csave, VARID_BOUNDING_BOX, bounding_box);
  95. //
  96. // Write the portal-id for each portal out to the chunk
  97. //
  98. for (int index = 0; index < m_PortalList.Count (); index ++) {
  99. int portal_id = m_PortalList[index];
  100. if (portal_id < PathfindClass::TEMP_PORTAL_ID_START) {
  101. WRITE_MICRO_CHUNK (csave, VARID_PORTAL_ID, portal_id);
  102. }
  103. }
  104. csave.End_Chunk ();
  105. return true;
  106. }
  107. ///////////////////////////////////////////////////////////////////////////
  108. //
  109. // Load
  110. //
  111. ///////////////////////////////////////////////////////////////////////////
  112. bool
  113. PathfindSectorClass::Load (ChunkLoadClass &cload)
  114. {
  115. Reset_Portal_List ();
  116. //
  117. // Read all the chunks...
  118. //
  119. while (cload.Open_Chunk ()) {
  120. switch (cload.Cur_Chunk_ID ()) {
  121. case CHUNKID_VARIABLES:
  122. Load_Variables (cload);
  123. break;
  124. default:
  125. WWDEBUG_SAY (("Unknown chunk ID 0x%X\n", cload.Cur_Chunk_ID ()));
  126. break;
  127. }
  128. cload.Close_Chunk ();
  129. }
  130. return true;
  131. }
  132. ///////////////////////////////////////////////////////////////////////////
  133. //
  134. // Load_Variables
  135. //
  136. ///////////////////////////////////////////////////////////////////////////
  137. bool
  138. PathfindSectorClass::Load_Variables (ChunkLoadClass &cload)
  139. {
  140. AABoxClass bounding_box;
  141. PathfindSectorClass *old_ptr = NULL;
  142. //
  143. // Read all the micro chunks...
  144. //
  145. while (cload.Open_Micro_Chunk ()) {
  146. switch (cload.Cur_Micro_Chunk_ID ()) {
  147. READ_MICRO_CHUNK (cload, VARID_BOUNDING_BOX, bounding_box);
  148. case VARID_PORTAL_ID:
  149. {
  150. uint32 portal_id = 0;
  151. cload.Read (&portal_id, sizeof (portal_id));
  152. if (portal_id < PathfindClass::TEMP_PORTAL_ID_START) {
  153. m_PortalList.Add (portal_id);
  154. }
  155. }
  156. break;
  157. default:
  158. WWDEBUG_SAY (("Unknown micro chunk ID 0x%X\n", cload.Cur_Micro_Chunk_ID ()));
  159. break;
  160. }
  161. cload.Close_Micro_Chunk ();
  162. }
  163. //
  164. // Register our old ptr so other objects can remap to us
  165. //
  166. if (old_ptr != NULL) {
  167. SaveLoadSystemClass::Register_Pointer (old_ptr, this);
  168. }
  169. Set_Bounding_Box (bounding_box);
  170. return true;
  171. }
  172. ///////////////////////////////////////////////////////////////////////////
  173. //
  174. // Peek_Portal
  175. //
  176. ///////////////////////////////////////////////////////////////////////////
  177. PathfindPortalClass *
  178. PathfindSectorClass::Peek_Portal (int index)
  179. {
  180. int portal_id = m_PortalList[index];
  181. return PathfindClass::Get_Instance ()->Peek_Portal (portal_id);
  182. }
  183. ///////////////////////////////////////////////////////////////////////////
  184. //
  185. // Remove_Portal
  186. //
  187. ///////////////////////////////////////////////////////////////////////////
  188. void
  189. PathfindSectorClass::Remove_Portal (uint32 portal_id)
  190. {
  191. //
  192. // Loop over all the portals until we've found the one
  193. // we want to remove -- then remove it.
  194. //
  195. for (int index = 0; index < m_PortalList.Count (); index ++) {
  196. if (portal_id == m_PortalList[index]) {
  197. m_PortalList.Delete (index);
  198. break;
  199. }
  200. }
  201. return ;
  202. }
  203. ///////////////////////////////////////////////////////////////////////////
  204. //
  205. // Save
  206. //
  207. ///////////////////////////////////////////////////////////////////////////
  208. bool
  209. PathfindWaypathSectorClass::Save (ChunkSaveClass &csave)
  210. {
  211. csave.Begin_Chunk (CHUNKID_WP_PARENT);
  212. PathfindSectorClass::Save (csave);
  213. csave.End_Chunk ();
  214. csave.Begin_Chunk (CHUNKID_WP_VARIABLES);
  215. WRITE_MICRO_CHUNK (csave, VARID_WAYPATH_ID, WaypathID);
  216. csave.End_Chunk ();
  217. return true;
  218. }
  219. ///////////////////////////////////////////////////////////////////////////
  220. //
  221. // Load
  222. //
  223. ///////////////////////////////////////////////////////////////////////////
  224. bool
  225. PathfindWaypathSectorClass::Load (ChunkLoadClass &cload)
  226. {
  227. //
  228. // Read all the chunks...
  229. //
  230. while (cload.Open_Chunk ()) {
  231. switch (cload.Cur_Chunk_ID ()) {
  232. case CHUNKID_WP_PARENT:
  233. PathfindSectorClass::Load (cload);
  234. break;
  235. case CHUNKID_WP_VARIABLES:
  236. Load_Variables (cload);
  237. break;
  238. default:
  239. WWDEBUG_SAY (("Unknown chunk ID 0x%X\n", cload.Cur_Chunk_ID ()));
  240. break;
  241. }
  242. cload.Close_Chunk ();
  243. }
  244. return true;
  245. }
  246. ///////////////////////////////////////////////////////////////////////////
  247. //
  248. // Load_Variables
  249. //
  250. ///////////////////////////////////////////////////////////////////////////
  251. bool
  252. PathfindWaypathSectorClass::Load_Variables (ChunkLoadClass &cload)
  253. {
  254. //
  255. // Read all the micro chunks...
  256. //
  257. while (cload.Open_Micro_Chunk ()) {
  258. switch (cload.Cur_Micro_Chunk_ID ()) {
  259. READ_MICRO_CHUNK (cload, VARID_WAYPATH_ID, WaypathID);
  260. default:
  261. WWDEBUG_SAY (("Unknown micro chunk ID 0x%X\n", cload.Cur_Micro_Chunk_ID ()));
  262. break;
  263. }
  264. cload.Close_Micro_Chunk ();
  265. }
  266. return true;
  267. }
  268. ///////////////////////////////////////////////////////////////////////////
  269. //
  270. // Can_Access_Portal
  271. //
  272. ///////////////////////////////////////////////////////////////////////////
  273. bool
  274. PathfindWaypathSectorClass::Can_Access_Portal
  275. (
  276. PathfindPortalClass *last_portal,
  277. PathfindPortalClass *test_portal
  278. )
  279. {
  280. bool retval = false;
  281. //
  282. // Check to see if the base class requirements are met
  283. //
  284. if (PathfindSectorClass::Can_Access_Portal (last_portal, test_portal)) {
  285. //
  286. // Check to see if the waypath is one-way, if so we need to make sure
  287. // the portal we are testing is "ahead" of the portal
  288. //
  289. WaypathClass *waypath = PathfindClass::Get_Instance ()->Find_Waypath (WaypathID);
  290. if (waypath != NULL && waypath->Get_Flag (WaypathClass::FLAG_TWO_WAY) == false) {
  291. //
  292. // Check to see if the portal we entered is "ahead" of the portal
  293. // we are testing
  294. //
  295. for (int index = 0; index < m_PortalList.Count (); index ++) {
  296. if (m_PortalList[index] == last_portal->Get_ID ()) {
  297. retval = true;
  298. break;
  299. } else if (m_PortalList[index] == test_portal->Get_ID ()) {
  300. break;
  301. }
  302. }
  303. } else {
  304. //
  305. // On a two-way waypath, we don't care which direction the portal
  306. // is from the one we entered
  307. //
  308. retval = true;
  309. }
  310. }
  311. return retval;
  312. }