floodfillbox.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/floodfillbox.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 5/03/01 5:29p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "floodfillbox.h"
  36. #include "pathfindsector.h"
  37. #include "pathfindportal.h"
  38. #include "pathfind.h"
  39. //////////////////////////////////////////////////////////////////////////
  40. // Static member initialization
  41. //////////////////////////////////////////////////////////////////////////
  42. FloodfillBoxClass *FloodfillBoxClass::m_First = NULL;
  43. FloodfillBoxClass *FloodfillBoxClass::m_Last = NULL;
  44. ///////////////////////////////////////////////////////////////////////////
  45. //
  46. // Is_Two_Way_Traversible
  47. //
  48. ///////////////////////////////////////////////////////////////////////////
  49. bool
  50. FloodfillBoxClass::Is_Two_Way_Traversible (PATHFIND_DIR dir)
  51. {
  52. bool retval = false;
  53. FloodfillBoxClass *neighbor = Peek_Neighbor (dir);
  54. if (neighbor != NULL) {
  55. retval = (neighbor->Peek_Neighbor (::Inverse_Pathfind_Dir (dir)) == this);
  56. }
  57. return retval;
  58. }
  59. ///////////////////////////////////////////////////////////////////////////
  60. //
  61. // Is_New_Portal
  62. //
  63. ///////////////////////////////////////////////////////////////////////////
  64. bool
  65. FloodfillBoxClass::Is_New_Portal (PATHFIND_DIR dir, PathfindSectorClass *dest_sector)
  66. {
  67. bool retval = false;
  68. //
  69. // Basically this box is part of a new 'portal' if:
  70. //
  71. // a) Its not already part of a portal.
  72. // b) It has a neighbor in the specified direction.
  73. // c) Its neighbor doesn't belong to the same sector OR
  74. // c) Its neighbor is the sector we are looking for
  75. //
  76. if ( (Part_Of_Portal (dir) == false) &&
  77. (m_Sector != NULL) &&
  78. (m_Neighbors[dir] != NULL) &&
  79. (Get_Traversible (dir))) {
  80. if ( (dest_sector != NULL) &&
  81. (m_Neighbors[dir]->m_Sector == dest_sector) &&
  82. (m_Neighbors[dir]->m_Sector->Is_Valid ()))
  83. {
  84. retval = true;
  85. } else if ( (dest_sector == NULL) &&
  86. (m_Neighbors[dir]->m_Sector != m_Sector) &&
  87. (m_Neighbors[dir]->m_Sector != NULL) &&
  88. m_Neighbors[dir]->m_Sector->Is_Valid ())
  89. {
  90. retval = true;
  91. }
  92. }
  93. return retval;
  94. }
  95. ///////////////////////////////////////////////////////////////////////////
  96. //
  97. // Make_Portal
  98. //
  99. ///////////////////////////////////////////////////////////////////////////
  100. PathfindPortalClass *
  101. FloodfillBoxClass::Make_Portal
  102. (
  103. PATHFIND_DIR dir,
  104. const Vector3 & box_size,
  105. float z_pos,
  106. float min_acceptable_size
  107. )
  108. {
  109. PATHFIND_DIR slide_dir1 = PATHFIND_DIR((dir + 1) & 1);
  110. PATHFIND_DIR slide_dir2 = PATHFIND_DIR(slide_dir1 + 2);
  111. WWASSERT (m_Neighbors[dir] != NULL);
  112. PathfindSectorClass *dest_sector = m_Neighbors[dir]->m_Sector;
  113. FloodfillBoxClass *curr_box = this;
  114. Vector3 min_point (100000.0F, 100000.0F, 100000.0F);
  115. Vector3 max_point (-100000.0F, -100000.0F, -100000.0F);
  116. Vector3 offset (0.0F, 0.0F, 0.0F);
  117. Vector3 thickness (0.0F, 0.0F, box_size.Z * 1.1F);
  118. if (dir == DIR_FORWARD) {
  119. offset.X = box_size.X * 0.5F;
  120. thickness.X = box_size.X * 0.05F;
  121. thickness.Y = box_size.Y * 0.5F;
  122. } else if (dir == DIR_BACKWARD) {
  123. offset.X = box_size.X * -0.5F;
  124. thickness.X = box_size.X * -0.05F;
  125. thickness.Y = box_size.Y * 0.5F;
  126. } else if (dir == DIR_RIGHT) {
  127. offset.Y = box_size.Y * -0.5F;
  128. thickness.Y = box_size.Y * 0.05F;
  129. thickness.X = box_size.X * 0.5F;
  130. } else {
  131. offset.Y = box_size.Y * 0.5F;
  132. thickness.Y = box_size.Y * -0.05F;
  133. thickness.X = box_size.X * 0.5F;
  134. }
  135. DynamicVectorClass<FloodfillBoxClass *> portal_list;
  136. //
  137. // Determine if this portal is going to be a one way or a two way...
  138. //
  139. bool is_two_way = Is_Two_Way_Traversible (dir);
  140. //
  141. // Calculate the height of the portal...
  142. //
  143. min_point.Z = z_pos;
  144. max_point.Z = z_pos + (box_size.Z * 1.1F);
  145. if (is_two_way) {
  146. PathfindSectorClass *sector_from = m_Sector;
  147. PathfindSectorClass *sector_to = dest_sector;
  148. const AABoxClass &box_from = sector_from->Get_Bounding_Box ();
  149. const AABoxClass &box_to = sector_to->Get_Bounding_Box ();
  150. float min_z1 = box_from.Center.Z - box_from.Extent.Z;
  151. float min_z2 = box_to.Center.Z - box_to.Extent.Z;
  152. float max_z1 = box_from.Center.Z + box_from.Extent.Z;
  153. float max_z2 = box_to.Center.Z + box_to.Extent.Z;
  154. min_point.Z = max (min_z1, min_z2);
  155. max_point.Z = min (max_z1, max_z2);
  156. }
  157. bool keep_going = true;
  158. //
  159. // Find the last box in the row that is a portal to the given sector.
  160. // Note: This is inclusive of the starting box.
  161. //
  162. while (keep_going) {
  163. portal_list.Add (curr_box);
  164. Vector3 position = curr_box->Get_Position ();
  165. position += offset;
  166. min_point.X = min (min_point.X, position.X);
  167. min_point.Y = min (min_point.Y, position.Y);
  168. min_point.X = min (min_point.X, position.X + thickness.X);
  169. min_point.Y = min (min_point.Y, position.Y + thickness.Y);
  170. min_point.X = min (min_point.X, position.X - thickness.X);
  171. min_point.Y = min (min_point.Y, position.Y - thickness.Y);
  172. max_point.X = max (max_point.X, position.X);
  173. max_point.Y = max (max_point.Y, position.Y);
  174. max_point.X = max (max_point.X, position.X + thickness.X);
  175. max_point.Y = max (max_point.Y, position.Y + thickness.Y);
  176. max_point.X = max (max_point.X, position.X - thickness.X);
  177. max_point.Y = max (max_point.Y, position.Y - thickness.Y);
  178. //
  179. // Should we keep going?
  180. //
  181. keep_going = ((curr_box = curr_box->Peek_Neighbor (slide_dir1)) != NULL) &&
  182. (curr_box->m_Sector == m_Sector) &&
  183. (curr_box->Is_New_Portal (dir, dest_sector)) &&
  184. (curr_box->Is_Two_Way_Traversible (dir) == is_two_way);
  185. }
  186. //
  187. // Find the first box in the row that is a portal to the given sector.
  188. //
  189. curr_box = this;
  190. while ( ((curr_box = curr_box->Peek_Neighbor (slide_dir2)) != NULL) &&
  191. (curr_box->m_Sector == m_Sector) &&
  192. (curr_box->Is_New_Portal (dir, dest_sector)) &&
  193. (curr_box->Is_Two_Way_Traversible (dir) == is_two_way))
  194. {
  195. portal_list.Add (curr_box);
  196. Vector3 position = curr_box->Get_Position ();
  197. position += offset;
  198. min_point.X = min (min_point.X, position.X);
  199. min_point.Y = min (min_point.Y, position.Y);
  200. min_point.X = min (min_point.X, position.X + thickness.X);
  201. min_point.Y = min (min_point.Y, position.Y + thickness.Y);
  202. min_point.X = min (min_point.X, position.X - thickness.X);
  203. min_point.Y = min (min_point.Y, position.Y - thickness.Y);
  204. max_point.X = max (max_point.X, position.X);
  205. max_point.Y = max (max_point.Y, position.Y);
  206. max_point.X = max (max_point.X, position.X + thickness.X);
  207. max_point.Y = max (max_point.Y, position.Y + thickness.Y);
  208. max_point.X = max (max_point.X, position.X - thickness.X);
  209. max_point.Y = max (max_point.Y, position.Y - thickness.Y);
  210. }
  211. PathfindPortalClass *portal = NULL;
  212. //
  213. // Choose the largest dimension
  214. //
  215. float test1 = (max_point.X - min_point.X);
  216. float test2 = (max_point.Y - min_point.Y);
  217. float test_value = max (test1, test2);
  218. //
  219. // Is this portal large enough?
  220. //
  221. if (test_value >= min_acceptable_size) {
  222. //
  223. // Let each box know that it is being bound into this portal
  224. //
  225. for (int index = 0; index < portal_list.Count (); index ++) {
  226. FloodfillBoxClass *portal_box = portal_list[index];
  227. portal_box->Part_Of_Portal (dir, true);
  228. if (is_two_way) {
  229. portal_box->m_Neighbors[dir]->Part_Of_Portal (::Inverse_Pathfind_Dir (dir), true);
  230. }
  231. }
  232. //
  233. // Create the new portal
  234. //
  235. portal = new PathfindPortalClass;
  236. portal->Add_Dest_Sector (dest_sector);
  237. if (is_two_way) {
  238. portal->Add_Dest_Sector (m_Sector);
  239. }
  240. //
  241. // Set the portal's bounding box
  242. //
  243. AABoxClass bounding_box;
  244. bounding_box.Center = min_point + ((max_point - min_point) / 2.0F);
  245. bounding_box.Extent.X = (max_point.X - min_point.X) / 2.0F;
  246. bounding_box.Extent.Y = (max_point.Y - min_point.Y) / 2.0F;
  247. bounding_box.Extent.Z = (max_point.Z - min_point.Z) / 2.0F;
  248. portal->Set_Bounding_Box (bounding_box);
  249. //
  250. // Register the portal with the system
  251. //
  252. int portal_index = PathfindClass::Get_Instance ()->Add_Portal (portal);
  253. m_Sector->Add_Portal (portal_index);
  254. if (is_two_way) {
  255. dest_sector->Add_Portal (portal_index);
  256. }
  257. }
  258. return portal;
  259. }