pathmgr.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/pathmgr.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 12/10/01 12:40p $*
  29. * *
  30. * $Revision:: 8 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "pathmgr.h"
  36. #include "pathsolve.h"
  37. #include "chunkio.h"
  38. #include "win.h"
  39. #include "wwmemlog.h"
  40. #include "systimer.h"
  41. ////////////////////////////////////////////////////////////////
  42. // Save/Load constants
  43. ////////////////////////////////////////////////////////////////
  44. enum
  45. {
  46. CHUNKID_PATH_SOLVE_OBJECT = 0x11070935,
  47. };
  48. /////////////////////////////////////////////////////////////////////////
  49. // Static member initialization
  50. /////////////////////////////////////////////////////////////////////////
  51. DynamicVectorClass<PathSolveClass *> PathMgrClass::AvailablePathList;
  52. DynamicVectorClass<PathSolveClass *> PathMgrClass::UsedPathList;
  53. PathSolveClass * PathMgrClass::ActivePath = NULL;
  54. __int64 PathMgrClass::TicksPerMilliSec = 0;
  55. /////////////////////////////////////////////////////////////////////////
  56. // Constants
  57. /////////////////////////////////////////////////////////////////////////
  58. static const int DEFAULT_OBJ_COUNT = 15;
  59. /////////////////////////////////////////////////////////////////////////
  60. //
  61. // Initialize
  62. //
  63. /////////////////////////////////////////////////////////////////////////
  64. void
  65. PathMgrClass::Initialize (void)
  66. {
  67. Allocate_Objects ();
  68. //
  69. // Determine what the resolution of our timer is
  70. //
  71. if (TicksPerMilliSec == 0) {
  72. ::QueryPerformanceFrequency ((LARGE_INTEGER *)&TicksPerMilliSec);
  73. TicksPerMilliSec /= 1000;
  74. }
  75. return ;
  76. }
  77. /////////////////////////////////////////////////////////////////////////
  78. //
  79. // Shutdown
  80. //
  81. /////////////////////////////////////////////////////////////////////////
  82. void
  83. PathMgrClass::Shutdown (void)
  84. {
  85. Free_Objects ();
  86. return ;
  87. }
  88. /////////////////////////////////////////////////////////////////////////
  89. //
  90. // Free_Objects
  91. //
  92. /////////////////////////////////////////////////////////////////////////
  93. void
  94. PathMgrClass::Free_Objects (void)
  95. {
  96. if (ActivePath != NULL) {
  97. ActivePath->Unlink_Pathfind_Hooks ();
  98. ActivePath = NULL;
  99. }
  100. //
  101. // Free the list of available objects
  102. //
  103. for (int index = 0; index < AvailablePathList.Count (); index ++) {
  104. PathSolveClass *path = AvailablePathList[index];
  105. REF_PTR_RELEASE (path);
  106. }
  107. //
  108. // Free the list of used objects
  109. //
  110. for (index = 0; index < UsedPathList.Count (); index ++) {
  111. PathSolveClass *path = UsedPathList[index];
  112. REF_PTR_RELEASE (path);
  113. }
  114. //
  115. // Reset the lists
  116. //
  117. AvailablePathList.Delete_All ();
  118. UsedPathList.Delete_All ();
  119. return ;
  120. }
  121. /////////////////////////////////////////////////////////////////////////
  122. //
  123. // Allocate_Objects
  124. //
  125. /////////////////////////////////////////////////////////////////////////
  126. void
  127. PathMgrClass::Allocate_Objects (void)
  128. {
  129. Free_Objects ();
  130. //
  131. // Allocate a default number of path objects
  132. //
  133. for (int index = 0; index < DEFAULT_OBJ_COUNT; index ++) {
  134. AvailablePathList.Add (new PathSolveClass);
  135. }
  136. return ;
  137. }
  138. /////////////////////////////////////////////////////////////////////////
  139. //
  140. // Request_Path_Object
  141. //
  142. /////////////////////////////////////////////////////////////////////////
  143. PathSolveClass *
  144. PathMgrClass::Request_Path_Object (void)
  145. {
  146. WWMEMLOG(MEM_PATHFIND);
  147. PathSolveClass *path_object = NULL;
  148. int avail_count = AvailablePathList.Count ();
  149. if (avail_count > 0) {
  150. //
  151. // Return the path object from the end of the list. Note: this is a
  152. // little more efficient when working with DynamicVectorClass objects.
  153. //
  154. path_object = AvailablePathList[avail_count - 1];
  155. AvailablePathList.Delete (avail_count - 1);
  156. } else {
  157. //
  158. // Allocate a new object (note: we should get this object back later so
  159. // it will be added to our internal list at that time).
  160. //
  161. path_object = new PathSolveClass;
  162. }
  163. //
  164. // Add this object to the used path list
  165. //
  166. if (path_object != NULL) {
  167. UsedPathList.Add (path_object);
  168. }
  169. //
  170. // Return the path object to the caller
  171. //
  172. return path_object;
  173. }
  174. /////////////////////////////////////////////////////////////////////////
  175. //
  176. // Return_Path_Object
  177. //
  178. /////////////////////////////////////////////////////////////////////////
  179. void
  180. PathMgrClass::Return_Path_Object (PathSolveClass *path)
  181. {
  182. WWASSERT (path != NULL);
  183. if (path != NULL) {
  184. //
  185. // Make sure the object doesn't already exist in our list
  186. //
  187. int index = AvailablePathList.ID (path);
  188. WWASSERT (index == -1);
  189. if (index == -1) {
  190. //
  191. // Find out where the object exists in the used list
  192. //
  193. int used_index = UsedPathList.ID (path);
  194. WWASSERT (used_index != -1);
  195. if (used_index != -1) {
  196. //
  197. // Reset our active path pointer (if necessary)
  198. //
  199. if (path == ActivePath) {
  200. ActivePath->Unlink_Pathfind_Hooks ();
  201. ActivePath = NULL;
  202. }
  203. //
  204. // Remove the object from the used list
  205. //
  206. UsedPathList.Delete (used_index);
  207. //
  208. // Add the path object to our list (its assumed we
  209. // will inherit the ref-count from the caller)
  210. //
  211. path->Reset_Lists ();
  212. AvailablePathList.Add (path);
  213. }
  214. }
  215. }
  216. return ;
  217. }
  218. ////////////////////////////////////////////////////////////////////////////////////////////
  219. //
  220. // Save
  221. //
  222. ////////////////////////////////////////////////////////////////////////////////////////////
  223. void
  224. PathMgrClass::Save (ChunkSaveClass &csave)
  225. {
  226. //
  227. // Save each of the path objects
  228. //
  229. for (int index = 0; index < UsedPathList.Count (); index ++) {
  230. PathSolveClass *path = UsedPathList[index];
  231. csave.Begin_Chunk (CHUNKID_PATH_SOLVE_OBJECT);
  232. path->Save (csave);
  233. csave.End_Chunk ();
  234. }
  235. return ;
  236. }
  237. ////////////////////////////////////////////////////////////////////////////////////////////
  238. //
  239. // Load
  240. //
  241. ////////////////////////////////////////////////////////////////////////////////////////////
  242. void
  243. PathMgrClass::Load (ChunkLoadClass &cload)
  244. {
  245. Free_Objects ();
  246. while (cload.Open_Chunk ()) {
  247. switch (cload.Cur_Chunk_ID ()) {
  248. case CHUNKID_PATH_SOLVE_OBJECT:
  249. {
  250. //
  251. // Allocate the path object, load its state, and add it to our list
  252. //
  253. PathSolveClass *path_object = new PathSolveClass;
  254. path_object->Load (cload);
  255. UsedPathList.Add (path_object);
  256. }
  257. break;
  258. }
  259. cload.Close_Chunk ();
  260. }
  261. return ;
  262. }
  263. ///////////////////////////////////////////////////////////////////////////
  264. //
  265. // Get_Time
  266. //
  267. ///////////////////////////////////////////////////////////////////////////
  268. static inline __int64
  269. Get_Time (void)
  270. {
  271. __int64 curr_time = 0;
  272. ::QueryPerformanceCounter ((LARGE_INTEGER *)&curr_time);
  273. return curr_time;
  274. }
  275. ////////////////////////////////////////////////////////////////////////////////////////////
  276. //
  277. // Resolve_Paths
  278. //
  279. ////////////////////////////////////////////////////////////////////////////////////////////
  280. void
  281. PathMgrClass::Resolve_Paths (const Vector3 &camera_pos, uint32 milliseconds)
  282. {
  283. __int64 start_time = Get_Time ();
  284. __int64 end_time = start_time + (((__int64)milliseconds) * TicksPerMilliSec);
  285. WWMEMLOG(MEM_PATHFIND);
  286. //
  287. // Keep processing path's until we've used up our timeslice
  288. //
  289. do
  290. {
  291. //
  292. // Find a path that needs to be solved
  293. //
  294. if (ActivePath == NULL) {
  295. Activate_New_Priority_Path (camera_pos);
  296. }
  297. //
  298. // Do we have any paths to solve?
  299. //
  300. if (ActivePath != NULL) {
  301. //
  302. // Let this path think for (up to) the remainder of our timeslice
  303. //
  304. uint32 time_slice = uint32((end_time - Get_Time ()) / TicksPerMilliSec);
  305. PathSolveClass::STATE_DESC result = ActivePath->Timestep (time_slice);
  306. //
  307. // If the path finished solving, then reset the active path
  308. //
  309. if (result != PathSolveClass::THINKING) {
  310. ActivePath->Unlink_Pathfind_Hooks ();
  311. ActivePath = NULL;
  312. }
  313. } else {
  314. break;
  315. }
  316. } while (Get_Time () < end_time);
  317. return ;
  318. }
  319. ////////////////////////////////////////////////////////////////////////////////////////////
  320. //
  321. // Activate_New_Priority_Path
  322. //
  323. ////////////////////////////////////////////////////////////////////////////////////////////
  324. void
  325. PathMgrClass::Activate_New_Priority_Path (const Vector3 &camera_pos)
  326. {
  327. ActivePath = NULL;
  328. float best_priority = 0;
  329. //
  330. // Find the highest priority path that needs solving
  331. //
  332. for (int index = 0; index < UsedPathList.Count (); index ++) {
  333. PathSolveClass *path = UsedPathList[index];
  334. //
  335. // Don't bother with paths that are already solved
  336. //
  337. if (path->Get_State () == PathSolveClass::THINKING) {
  338. //
  339. // Get the different priority factors for this path
  340. //
  341. float dist = (path->Get_Start_Pos () - camera_pos).Length ();
  342. float pos_priority = 1.0F - WWMath::Clamp (dist / 20.0F, 0.0F, 1.0F);
  343. float path_priority = WWMath::Clamp (path->Get_Priority (), 0.0F, 1.0F);
  344. float time_priority = (TIMEGETTIME () - path->Get_Birth_Time ()) / 5000.0F;
  345. //
  346. // Calculate a final priority based on these factors
  347. //
  348. float priority = (path_priority * 0.5F) + (pos_priority * 0.5F) + time_priority;
  349. //
  350. // If this is best path so far, then choose it
  351. //
  352. if (priority > best_priority) {
  353. best_priority = priority ;
  354. ActivePath = path;
  355. }
  356. }
  357. }
  358. //
  359. // Kick off the pathfind
  360. //
  361. if (ActivePath != NULL) {
  362. ActivePath->Process_Initial_Sector ();
  363. }
  364. return ;
  365. }