FINDPATH.CPP 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/FINDPATH.CPP 1 3/03/97 10:24a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : FINDPATH.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : September 10, 1993 *
  30. * *
  31. * Last Update : May 25, 1995 [PWG] *
  32. * *
  33. * The path algorithm works by following a LOS path to the target. If it *
  34. * collides with an impassable spot, it uses an Edge following routine to *
  35. * get around it. The edge follower moves along the edge in a clockwise or *
  36. * counter clockwise fashion until finding the destination spot. The *
  37. * destination is determined by Find_Path. It is the first passable that *
  38. * can be reached (so it will handle the doughnut case, where there is *
  39. * a passable in the center of an unreachable area). *
  40. * *
  41. *---------------------------------------------------------------------------------------------*
  42. * Functions: *
  43. * Clear_Path_Overlap -- clears the path overlap list *
  44. * Find_Path -- Find a path from point a to point b. *
  45. * Find_Path_Cell -- Finds a given cell on a specified path *
  46. * Follow_Edge -- Follow an edge to get around an impassable spot. *
  47. * FootClass::Unravel_Loop -- Unravels a loop in the movement path *
  48. * Get_New_XY -- Get the new x,y based on current position and direction. *
  49. * Optimize_Moves -- Optimize the move list. *
  50. * Set_Path_Overlap -- Sets the overlap bit for given cell *
  51. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  52. #include "function.h"
  53. //#include <string.h>
  54. /*
  55. ** When an edge search is started, it can be performed CLOCKwise or
  56. ** COUNTERCLOCKwise direction.
  57. */
  58. #define CLOCK (FacingType)1 // Clockwise.
  59. #define COUNTERCLOCK (FacingType)-1 // Counterclockwise.
  60. /*
  61. ** If defined, diagonal moves are allowed, else no diagonals.
  62. */
  63. #define DIAGONAL
  64. /*
  65. ** This is the marker to signify the end of the path list.
  66. */
  67. #define END FACING_NONE
  68. /*
  69. ** "- 1" test for bit manipulation.
  70. */
  71. #define TEST
  72. /*
  73. ** If memory is more important than speed, set this define to
  74. ** true. It will then perform intermediate optimizations to get the most
  75. ** milage out of a limited movement list staging area. If this value
  76. ** is true then it figures paths a bit more intelligently.
  77. */
  78. #define SAVEMEM true
  79. /*
  80. ** Modify this macro so that given two cell values, it will return
  81. ** a value between 0 and 7, with 0 being North and moving
  82. ** clockwise (just like map degrees).
  83. */
  84. #define CELL_FACING(a, b) Dir_Facing(::Direction((a), (b)))
  85. /*-------------------------------------------------------------------------*/
  86. /*
  87. ** Cells values are really indexes into the 'map'. The following value is
  88. ** the X width of the map.
  89. */
  90. #define MODULO MAP_CELL_W
  91. /*
  92. ** Maximum lookahead cells. Twice this value in bytes will be
  93. ** reserved on the stack. The smaller this number, the faster the processing.
  94. */
  95. #define MAX_MLIST_SIZE 300
  96. #define THREAT_THRESHOLD 5
  97. #define MAX_PATH_EDGE_FOLLOW 400
  98. #ifdef NEVER
  99. typedef enum {
  100. FACING_N, // North
  101. FACING_NE, // North-East
  102. FACING_E, // East
  103. FACING_SE, // South-East
  104. FACING_S, // South
  105. FACING_SW, // South-West
  106. FACING_W, // West
  107. FACING_NW, // North-West
  108. FACING_COUNT // Total of 8 directions (0..7).
  109. } FacingType;
  110. #endif
  111. /*-------------------------------------------------------------------------*/
  112. //static bool DrawPath;
  113. inline FacingType Opposite(FacingType face)
  114. {
  115. return( (FacingType) (face ^ 4));
  116. }
  117. inline static FacingType Next_Direction(FacingType facing, FacingType dir)
  118. {
  119. facing = facing + dir;
  120. #ifndef DIAGONAL
  121. facing = (FacingType)(facing & 0x06);
  122. #endif
  123. return(facing);
  124. }
  125. /*=========================================================================*/
  126. /* Define a couple of variables which are private to the module they are */
  127. /* declared in. */
  128. /*=========================================================================*/
  129. static unsigned long MainOverlap[MAP_CELL_TOTAL/32]; // overlap list for the main path
  130. static unsigned long LeftOverlap[MAP_CELL_TOTAL/32]; // overlap list for the left path
  131. static unsigned long RightOverlap[MAP_CELL_TOTAL/32]; // overlap list for the right path
  132. //static CELL MoveMask = 0;
  133. static CELL DestLocation;
  134. static CELL StartLocation;
  135. /***************************************************************************
  136. * Point_Relative_To_Line -- Relation between a point and a line *
  137. * *
  138. * If a point is on a line then the following function holds true: *
  139. * (x - x2)(z1 - z2) = (z - z2)(x1 - x2) given x,z a point on the *
  140. * line (x1,z1),(x2,z2). *
  141. * If the right side is > then the left side then the point is on one *
  142. * side of the line and if the right side is < the the left side, then*
  143. * the point is on the other side of the line. By subtracting one side*
  144. * from the other we can determine on what side (if any) the point is on*
  145. * by testing the side of the resulting subtraction. *
  146. * *
  147. * INPUT: *
  148. * int x - x pos of point. *
  149. * int z - z pos of point. *
  150. * int x1 - x pos of first end of line segment. *
  151. * int z1 - z pos of first end of line segment. *
  152. * int x1 - x pos of second end of line segment. *
  153. * int z1 - z pos of second end of line segment. *
  154. * *
  155. * OUTPUT: *
  156. * Assuming (x1,z1) is north, (x2,z2) is south: *
  157. * 0 : point is on line. *
  158. * > 0 : point is east of line. *
  159. * < 0 : point is west of line. *
  160. * *
  161. * WARNINGS: *
  162. * Remember that int means that assumes 16 bits of precision. *
  163. * *
  164. * HISTORY: *
  165. * 10/28/1994 SKB : Created. *
  166. *=========================================================================*/
  167. int Point_Relative_To_Line(int x, int z, int x1, int z1, int x2, int z2)
  168. {
  169. return((((long)x - (long)x2) * ((long)z1 - (long)z2)) - (((long)z - (long)z2) * ((long)x1 - (long)x2)));
  170. }
  171. /***************************************************************************
  172. * FootClass::Unravel_Loop -- Unravels a loop in the movement path *
  173. * *
  174. * While in the midst of the Follow Edge logic, it is possible (due to the *
  175. * fact that we support diagonal movement) to begin looping around a *
  176. * column of some type. The Unravel loop function will scan backward *
  177. * through the list and fixup the path to try to prevent the loop. *
  178. * *
  179. * INPUT: path - pointer to the generated path so we can pull the *
  180. * commands out of it. *
  181. * cell - the cell we tried to enter that generated the *
  182. * double overlap condition. *
  183. * dir - the direction we tried to enter from when we *
  184. * generated the double overlap condition *
  185. * startx - the start x position of this path segment *
  186. * starty - the start y position of this path segment *
  187. * destx - the dest x position for this path segment *
  188. * desty - the dest y position for this path segment *
  189. * *
  190. * OUTPUT: TRUE - loop has been successfully unravelled *
  191. * FALSE - loop can not be unravelled so abort follow edge *
  192. * *
  193. * WARNINGS: none *
  194. * *
  195. * HISTORY: *
  196. * 05/25/1995 PWG : Created. *
  197. *=========================================================================*/
  198. bool FootClass::Unravel_Loop(PathType * path, CELL &cell, FacingType &dir, int sx, int sy, int dx, int dy, MoveType threshhold)
  199. {
  200. /*
  201. ** Walk back to the actual cell before we advanced our position
  202. */
  203. FacingType curr_dir = dir;
  204. CELL curr_pos = Adjacent_Cell(cell, Opposite(curr_dir));
  205. int idx = path->Length; // start at the last position
  206. FacingType * list = &path->Command[idx-1]; // point to the last command
  207. int checkx;
  208. int checky;
  209. int last_was_line = false;
  210. /*
  211. ** loop backward through the list searching for a point that is
  212. ** on the line. If the point was a diagonal move then adjust
  213. ** it.
  214. */
  215. while (idx) {
  216. checkx = Cell_X(curr_pos);
  217. checky = Cell_Y(curr_pos);
  218. if (!Point_Relative_To_Line(checkx, checky, sx, sy, dx, dy) || last_was_line) {
  219. /*
  220. ** We have now found a point on the line. Now we must check to see
  221. ** if we left the line on a diagonal. If we did then we need to fix
  222. ** it up.
  223. */
  224. if (curr_dir & 1 && curr_pos != path->LastFixup) {
  225. cell = curr_pos;
  226. dir = *(list-1);
  227. path->Length = idx;
  228. path->LastFixup = curr_pos;
  229. return(true);
  230. }
  231. last_was_line = !last_was_line;
  232. }
  233. /*
  234. ** Since this cell will not be in the list, then pull out its cost
  235. */
  236. path->Cost -= Passable_Cell(curr_pos, *list, -1, threshhold);
  237. /*
  238. ** Remove this cells flag from the overlap list for the path
  239. */
  240. #ifdef TEST
  241. path->Overlap[curr_pos >> 5] &= ~(1 << ((curr_pos & 31)));
  242. #else
  243. path->Overlap[curr_pos >> 5] &= ~(1 << ((curr_pos & 31) - 1));
  244. #endif
  245. /*
  246. ** Adjust to the next list position and direction.
  247. */
  248. curr_dir = *list--;
  249. curr_pos = Adjacent_Cell(curr_pos, Opposite(curr_dir));
  250. idx--;
  251. }
  252. /*
  253. ** If we can't modify the list to eliminate the problem, then we have
  254. ** a larger problem in that we have deleted all of the cells in the
  255. ** list.
  256. */
  257. return(false);
  258. }
  259. /***************************************************************************
  260. * Register_Cell -- registers a cell on our path and check for backtrack *
  261. * *
  262. * This function adds a new cell to our path. If the cell has already *
  263. * been recorded as part of our path, then this function moves back down *
  264. * the list truncating it at the point we registered that cell. This *
  265. * function will eliminate all backtracking from the list. *
  266. * *
  267. * INPUT: long * list - the list to set the overlap bit for *
  268. * CELL cell - the cell to mark on the overlap list *
  269. * *
  270. * OUTPUT: BOOL - TRUE if bit has been set, FALSE if bit already set *
  271. * *
  272. * HISTORY: *
  273. * 05/23/1995 PWG : Created. *
  274. *=========================================================================*/
  275. bool FootClass::Register_Cell(PathType * path, CELL cell, FacingType dir, int cost, MoveType threshhold)
  276. {
  277. FacingType * list;
  278. int pos = cell >> 5;
  279. #ifdef TEST
  280. int bit = (cell & 31);
  281. #else
  282. int bit = (cell & 31) - 1;
  283. #endif
  284. /*
  285. ** See if this point has already been registered as on the list. If so
  286. ** we need to truncate the list back to this point and register the
  287. ** new direction.
  288. */
  289. if (path->Overlap[pos] & (1 << bit)) {
  290. /*
  291. ** If this is not a case of immediate back tracking then handle
  292. ** by searching the list to see what we find. However is this is
  293. ** an immediate back track, then pop of the last direction
  294. ** and unflag the cell we are in (not the cell we are moving to).
  295. ** Note: That we do not check for a zero length cell because we
  296. ** could not have a duplicate unless there are cells in the list.
  297. */
  298. if (path->Command[path->Length - 1] == Opposite(dir)) {
  299. CELL pos = Adjacent_Cell(cell, Opposite(dir));
  300. #ifdef TEST
  301. path->Overlap[pos >> 5] &= ~(1 << ((pos & 31)));
  302. #else
  303. path->Overlap[pos >> 5] &= ~(1 << ((pos & 31) - 1));
  304. #endif
  305. path->Length--;
  306. } else {
  307. /*
  308. ** If this overlap is in the same place as we had our last overlap
  309. ** then we are in a loop condition. We need to signify that we
  310. ** cannot register this cell.
  311. */
  312. if (path->LastOverlap == cell) {
  313. return(false);
  314. } else {
  315. path->LastOverlap = cell;
  316. }
  317. CELL pos = path->Start;
  318. int newlen = 0;
  319. int idx = 0;
  320. list = path->Command;
  321. /*
  322. ** Note that the cell has to be in this list, so theres no sense
  323. ** in checking whether we found it (famous last words).
  324. **
  325. ** PWG 8/16/95 - However there is no sense searching the list if
  326. ** the cell we have overlapped on is the cell we
  327. ** started in.
  328. */
  329. if (pos != cell) {
  330. while (idx < path->Length) {
  331. pos = Adjacent_Cell(pos, *list);
  332. if (pos == cell) {
  333. idx++;
  334. list++;
  335. break;
  336. }
  337. idx++;
  338. list++;
  339. }
  340. newlen = idx;
  341. }
  342. /*
  343. ** Now we are pointing at the next command in the list. From here on
  344. ** out we need to unmark the fact that we have entered these cells and
  345. ** adjust the cost of our path to reflect that we have not entered
  346. ** then.
  347. */
  348. while (idx < path->Length) {
  349. pos = Adjacent_Cell(pos, *list);
  350. path->Cost -= Passable_Cell(pos, *list, -1, threshhold);
  351. #ifdef TEST
  352. path->Overlap[pos >> 5] &= ~(1 << ((pos & 31)));
  353. #else
  354. path->Overlap[pos >> 5] &= ~(1 << ((pos & 31) - 1));
  355. #endif
  356. idx++;
  357. list++;
  358. }
  359. path->Length = newlen;
  360. }
  361. } else {
  362. /*
  363. ** Now we need to register the new direction, updating the cell structure
  364. ** and the cost.
  365. */
  366. int cpos = path->Length++;
  367. path->Command[cpos] = dir; // save of the direction we moved
  368. path->Cost += cost; // figure new cost for cell
  369. path->Overlap[pos] |= (1 << bit); // mark the we have entered point
  370. }
  371. return(true);
  372. }
  373. /***********************************************************************************************
  374. * Find_Path -- Find a path from point a to point b. *
  375. * *
  376. * INPUT: int source x,y, int destination x,y, char *final moves *
  377. * array to store moves, int maximum moves we may attempt *
  378. * *
  379. * OUTPUT: int number of moves it took (IMPOSSIBLE_MOVES if we could *
  380. * not reach the destination *
  381. * *
  382. * WARNINGS: This algorithm assumes that the target is NOT situated *
  383. * inside an impassable. If this case may arise, the do-while *
  384. * statement inside the inner while (true) must be changed *
  385. * to include a check to se if the next_x,y is equal to the *
  386. * dest_x,y. If it is, then return(IMPOSSIBLE_MOVES). *
  387. * *
  388. * HISTORY: *
  389. * 07/08/1991 CY : Created. *
  390. *=============================================================================================*/
  391. PathType * FootClass::Find_Path(CELL dest, FacingType * final_moves, int maxlen, MoveType threshhold)
  392. {
  393. CELL source = Coord_Cell(Coord); // Source expressed as cell
  394. static PathType path; // Main path control.
  395. CELL next; // Next cell to enter
  396. CELL startcell; // Cell we started in
  397. FacingType direction; // Working direction of look ahead.
  398. FacingType newdir; // Tentative facing value.
  399. bool left=false, // Was leftward path legal?
  400. right=false; // Was rightward path legal?
  401. int len; // Length of detour command list.
  402. int unit_threat; // Calculated unit threat rating
  403. int cost; // Cost to enter the square
  404. FacingType moves_left[MAX_MLIST_SIZE+2], // Counterclockwise move list.
  405. moves_right[MAX_MLIST_SIZE+2]; // Clockwise move list.
  406. PathType pleft,pright; // Path control structures.
  407. PathType * which; // Which path to actually use.
  408. int threat;
  409. int threat_stage;
  410. /*
  411. ** If we have been provided an illegal place to store our final moves
  412. ** then forget it.
  413. */
  414. if (!final_moves) return(NULL);
  415. BStart(BENCH_FINDPATH);
  416. PathCount++;
  417. if (Team && Team->Class->IsRoundAbout) {
  418. unit_threat = (Team) ? Team->Risk : Risk();
  419. threat_stage = 0;
  420. threat = 0;
  421. } else {
  422. unit_threat = threat = -1;
  423. }
  424. StartLocation = source;
  425. DestLocation = dest;
  426. /*
  427. ** Initialize the path structure so that we can keep track of the
  428. ** path.
  429. */
  430. path.Start = source;
  431. path.Cost = 0;
  432. path.Length = 0;
  433. path.Command = final_moves;
  434. path.Command[0] = END;
  435. path.Overlap = MainOverlap;
  436. path.LastOverlap = -1;
  437. path.LastFixup = -1;
  438. memset(path.Overlap, 0, sizeof(MainOverlap));
  439. /*
  440. ** Clear the over lap list and then make sure that our starting position is marked
  441. ** on the overlap list. (Otherwise the harvesters will drive in circles... )
  442. */
  443. #ifdef TEST
  444. path.Overlap[source >> 5] |= (1 << ((source & 31)));
  445. #else
  446. path.Overlap[source >> 5] |= (1 << ((source & 31) - 1));
  447. #endif
  448. startcell = source;
  449. /*
  450. ** Account for trailing end of list command, so reduce the maximum
  451. ** allowed legal commands to reflect this.
  452. */
  453. maxlen--;
  454. /*
  455. ** As long as there is room to put commands in the movement command list,
  456. ** then put commands in it. We build the path using the following
  457. ** methodology.
  458. **
  459. ** 1. Scan through the desired straight line path until we either hit an
  460. ** impassable or have created a valid path.
  461. **
  462. ** 2. If we have hit an impassable, walk through the impassable to make
  463. ** sure that there is a passable on the other side. If there is not
  464. ** and we can not change the impassable, then this list is dead.
  465. **
  466. ** 3. Walk around the impassable on both the left and right edges and
  467. ** take the shorter of the two paths.
  468. **
  469. ** 4. Taking the new location as our start location start again with
  470. ** step #1.
  471. */
  472. while (path.Length < maxlen) {
  473. top_of_list:
  474. /*
  475. ** Have we reached the destination already? If so abort any further
  476. ** command building.
  477. */
  478. if (startcell == dest) {
  479. break;
  480. }
  481. /*
  482. ** Find the absolute correct direction to reach the next straight
  483. ** line cell and what cell it is.
  484. */
  485. direction = CELL_FACING(startcell, dest);
  486. next = Adjacent_Cell(startcell, direction);
  487. /*
  488. ** If we can move here, then make this our next move.
  489. */
  490. cost = Passable_Cell(next, direction, threat, threshhold);
  491. if (cost) {
  492. Register_Cell(&path, next, direction, cost, threshhold);
  493. } else {
  494. /*
  495. ** If the impassable location is actually the destination,
  496. ** then stop here and consider this "good enough".
  497. */
  498. if (next == dest) break;
  499. /*
  500. ** We could not move to the next cell, so follow through the
  501. ** impassable until we find a passable spot that can be reached.
  502. ** Once we find a passable, figure out the shortest path to it.
  503. ** Since we have variable passable conditions this is not as
  504. ** simple as it used to be. The limiter loop below allows us to
  505. ** step through ten doughnuts before we give up.
  506. */
  507. for (int limiter = 0; limiter < 5; limiter++) {
  508. /*
  509. ** Get the next passable position by zipping through the
  510. ** impassable positions until a passable position is found
  511. ** or the destination is reached.
  512. */
  513. for (;;) {
  514. /*
  515. ** Move one step closer toward destination.
  516. */
  517. newdir = CELL_FACING(next, dest);
  518. next = Adjacent_Cell(next, newdir);
  519. /*
  520. ** If the cell is passable then we have been completely
  521. ** successful. If the cell is not passable then continue.
  522. */
  523. if (Passable_Cell(next, FACING_NONE, threat, threshhold)) {
  524. // if ((Passable_Cell(next, FACING_NONE, threat, threshhold)) || (next == dest)) {
  525. break;
  526. }
  527. /*
  528. ** If we reached destination while in this loop, we
  529. ** know that either the destination is impassible (if
  530. ** we are ignoring) or that we need to up our threat
  531. ** tolerance and try again.
  532. */
  533. if (next == dest) {
  534. if (threat != -1) {
  535. switch (threat_stage++) {
  536. case 0:
  537. threat = unit_threat >> 1;
  538. break;
  539. case 1:
  540. threat += unit_threat;
  541. break;
  542. case 2:
  543. threat = -1;
  544. break;
  545. }
  546. goto top_of_list;
  547. }
  548. goto end_of_list;
  549. }
  550. }
  551. /*
  552. ** Try to find a path to the passable position by following
  553. ** the edge of the blocking object in both CLOCKwise and
  554. ** COUNTERCLOCKwise fashions.
  555. */
  556. int follow_len = maxlen + (maxlen >> 1);
  557. Mem_Copy(&path, &pleft, sizeof(PathType));
  558. pleft.Command = &moves_left[0];
  559. pleft.Overlap = LeftOverlap;
  560. Mem_Copy(path.Command, pleft.Command, path.Length);
  561. Mem_Copy(path.Overlap, pleft.Overlap, sizeof(LeftOverlap));
  562. left = Follow_Edge(startcell, next, &pleft, COUNTERCLOCK, direction, threat, threat_stage, sizeof(moves_left)/sizeof(moves_left[0]), threshhold);
  563. // left = Follow_Edge(startcell, next, &pleft, COUNTERCLOCK, direction, threat, threat_stage, follow_len, threshhold);
  564. if (left) {
  565. follow_len = min(maxlen, pleft.Length + (pleft.Length >> 1));
  566. }
  567. Mem_Copy(&path, &pright, sizeof(PathType));
  568. pright.Command = &moves_right[0];
  569. pright.Overlap = RightOverlap;
  570. Mem_Copy(path.Command, pright.Command, path.Length);
  571. Mem_Copy(path.Overlap, pright.Overlap, sizeof(RightOverlap));
  572. right = Follow_Edge(startcell, next, &pright, CLOCK, direction, threat, threat_stage, sizeof(moves_right)/sizeof(moves_right[0]), threshhold);
  573. // right = Follow_Edge(startcell, next, &pright, CLOCK, direction, threat, threat_stage, follow_len, threshhold);
  574. /*
  575. ** If we could find a path, break from this loop. Otherwise this
  576. ** means that we have found a "hole" of passable terrain that
  577. ** cannot be reached by normal means. Scan forward looking for
  578. ** the other side of the "doughnut".
  579. */
  580. if (left || right) break;
  581. /*
  582. ** If no path can be found to the intermediate cell, then
  583. ** presume we have found a doughnut of some sort. Scan
  584. ** forward until the next impassable is found and then
  585. ** process this loop again.
  586. */
  587. do {
  588. /*
  589. ** If we reached destination while in this loop, we
  590. ** know that either the destination is impassible (if
  591. ** we are ignoring) or that we need to up our threat
  592. ** tolerance and try again.
  593. */
  594. if (next == dest) {
  595. if (threat != -1) {
  596. switch (threat_stage++) {
  597. case 0:
  598. threat = unit_threat >> 1;
  599. break;
  600. case 1:
  601. threat += unit_threat;
  602. break;
  603. case 2:
  604. threat = -1;
  605. break;
  606. }
  607. goto top_of_list;
  608. }
  609. goto end_of_list;
  610. }
  611. newdir = CELL_FACING(next, dest);
  612. next = Adjacent_Cell(next, newdir);
  613. } while (Passable_Cell(next, newdir, threat, threshhold));
  614. }
  615. if (!left && !right) break;
  616. /*
  617. ** We found a path around the impassable locations, so figure out
  618. ** which one was the smallest and copy those moves into the
  619. ** path.Command array.
  620. */
  621. which = &pleft;
  622. if (right) {
  623. which = &pright;
  624. if (left) {
  625. if (pleft.Length < pright.Length) {
  626. which = &pleft;
  627. } else {
  628. which = &pright;
  629. }
  630. }
  631. }
  632. /*
  633. ** Record as much as possible of the shorter of the two
  634. ** paths. The trailing EOL command is not copied because
  635. ** this may not be the end of the find path logic.
  636. */
  637. len = which->Length;
  638. len = min(len, maxlen);
  639. if (len > 0) {
  640. memcpy(&path.Overlap[0], &which->Overlap[0], sizeof(LeftOverlap));
  641. memcpy(&path.Command[0], &which->Command[0], len * sizeof(FacingType));
  642. path.Length = len;
  643. path.Cost = which->Cost;
  644. path.LastOverlap = -1;
  645. path.LastFixup = -1;
  646. } else {
  647. break;
  648. }
  649. }
  650. startcell = next;
  651. }
  652. end_of_list:
  653. /*
  654. ** Poke in the stop command.
  655. */
  656. if (path.Length < maxlen) {
  657. path.Command[path.Length++] = END;
  658. }
  659. /*
  660. ** Optimize the move list but only necessary if
  661. ** diagonal moves are allowed.
  662. */
  663. #ifdef DIAGONAL
  664. Optimize_Moves(&path, threshhold);
  665. #endif
  666. BEnd(BENCH_FINDPATH);
  667. return(&path);
  668. }
  669. /***********************************************************************************************
  670. * Follow_Edge -- Follow an edge to get around an impassable spot. *
  671. * *
  672. * INPUT: start -- cell to head from *
  673. * *
  674. * target -- Target cell to head to. *
  675. * *
  676. * path -- Pointer to path list structure. *
  677. * *
  678. * search -- Direction of search (1=clock, -1=counterclock). *
  679. * *
  680. * olddir -- Facing impassible direction from start. *
  681. * *
  682. * callback -- Function pointer for determining if a cell is *
  683. * passable or not. *
  684. * *
  685. * OUTPUT: bool: Could a path be found to the desired cell? *
  686. * *
  687. * WARNINGS: none *
  688. * *
  689. * HISTORY: *
  690. * 07/08/1991 CY : Created. *
  691. * 06/01/1992 JLB : Optimized & commented. *
  692. *=============================================================================================*/
  693. bool FootClass::Follow_Edge(CELL start, CELL target, PathType * path, FacingType search, FacingType olddir, int threat, int , int max_cells, MoveType threshhold)
  694. {
  695. FacingType newdir; // Direction of facing before surrounding cell check.
  696. CELL oldcell, // Current cell.
  697. newcell; // Tentative new cell.
  698. int cost; // Working cost value.
  699. int startx;
  700. int starty;
  701. int online=true;
  702. int targetx;
  703. int targety;
  704. int oldval = 0;
  705. int cellcount=0;
  706. int forceout = false;
  707. FacingType firstdir = (FacingType)-1;
  708. CELL firstcell = -1;
  709. bool stepped_off_line = false;
  710. startx = Cell_X(start);
  711. starty = Cell_Y(start);
  712. targetx = Cell_X(target);
  713. targety = Cell_Y(target);
  714. if (!path) return(false);
  715. path->LastOverlap = -1;
  716. path->LastFixup = -1;
  717. #ifndef DIAGONAL
  718. /*
  719. ** The edge following algorithm doesn't "do" diagonals. Force initial facing
  720. ** to be an even 90 degree value. Adjust it in the direction it should be
  721. ** rotating.
  722. */
  723. if (olddir & 0x01) {
  724. olddir = Next_Direction(olddir, search);
  725. }
  726. #endif
  727. newdir = Next_Direction(olddir, search);
  728. oldcell = start;
  729. newcell = Adjacent_Cell(oldcell, newdir);
  730. /*
  731. ** Continue until we find our target, find our original starting spot,
  732. ** or run out of moves.
  733. */
  734. while (path->Length < max_cells) {
  735. /*
  736. ** Look in all the adjacent cells to determine a passable one that
  737. ** most closely matches the desired direction (working in the specified
  738. ** direction).
  739. */
  740. newdir = olddir;
  741. for (;;) {
  742. bool forcefail; // Is failure forced?
  743. forcefail = false;
  744. #ifdef DIAGONAL
  745. /*
  746. ** Rotate 45/90 degrees in desired direction.
  747. */
  748. newdir = Next_Direction(newdir, search);
  749. /*
  750. ** If facing a diagonal we must check the next 90 degree location
  751. ** to make sure that we don't walk right by the destination. This
  752. ** will happen if the destination it is at the corner edge of an
  753. ** impassable that we are moving around.
  754. */
  755. if (newdir & FACING_NE) {
  756. CELL checkcell; // Non-diagonal check cell.
  757. //int x,y;
  758. checkcell = Adjacent_Cell(oldcell, Next_Direction(newdir, search));
  759. if (checkcell == target) {
  760. /*
  761. ** This only works if in fact, it is possible to move to the
  762. ** cell from the current location.
  763. */
  764. cost = Passable_Cell(checkcell, Next_Direction(newdir, search), threat, threshhold);
  765. if (cost) {
  766. /*
  767. ** YES! The destination is at the corner of an impassable, so
  768. ** set the direction to point directly at it and then the
  769. ** scanning will terminate later.
  770. */
  771. newdir = Next_Direction(newdir, search);
  772. newcell = Adjacent_Cell(oldcell, newdir);
  773. break;
  774. }
  775. }
  776. /*
  777. ** Perform special diagonal check. If the edge follower would cross the
  778. ** diagonal or fall on the diagonal line from the source, then consider
  779. ** that cell impassible. Otherwise, the find path algorithm will fail
  780. ** when there are two impassible locations located on a diagonal
  781. ** that is lined up between the source and destination location.
  782. **
  783. ** P.S. It might help if you check the right cell rather than using
  784. ** the value that just happened to be in checkcell.
  785. */
  786. checkcell = Adjacent_Cell(oldcell, newdir);
  787. int checkx = Cell_X(checkcell);
  788. int checky = Cell_Y(checkcell);
  789. int checkval = Point_Relative_To_Line(checkx, checky, startx, starty, targetx, targety);
  790. if (checkval && !online) {
  791. forcefail = ((checkval ^ oldval) < 0);
  792. } else {
  793. forcefail = false;
  794. }
  795. /*
  796. ** The only exception to the above is when we are directly backtracking
  797. ** because we could be trying to escape from a culdesack!
  798. */
  799. if (forcefail && path->Length > 0 && (FacingType)(newdir ^ 4) == path->Command[path->Length - 1]) {
  800. forcefail = false;
  801. }
  802. }
  803. #else
  804. newdir = Next_Direction(newdir, search*2);
  805. #endif
  806. /*
  807. ** If we have just checked the same heading we started with,
  808. ** we are surrounded by impassable characters and we exit.
  809. */
  810. if (newdir == olddir) {
  811. return(false);
  812. }
  813. /*
  814. ** Get the new cell.
  815. */
  816. newcell = Adjacent_Cell(oldcell, newdir);
  817. /*
  818. ** If we found a passable position, this is where we should move.
  819. */
  820. if (!forcefail && ((cost = Passable_Cell(newcell, newdir, threat, threshhold)) != 0)) {
  821. break;
  822. } else {
  823. if (newcell == target) {
  824. forceout = true;
  825. break;
  826. }
  827. }
  828. }
  829. /*
  830. ** Record the direction.
  831. */
  832. if (!forceout) {
  833. /*
  834. ** Mark the cell because this is where we need to be. If register
  835. ** cell fails then the list has been shortened and we need to adjust
  836. ** the new direction.
  837. */
  838. if (!Register_Cell(path, newcell, newdir, cost, threshhold)) {
  839. /*
  840. ** The only reason we could not register a cell is that we are in
  841. ** a looping situation. So we need to try and unravel the loop if
  842. ** we can.
  843. */
  844. if (!Unravel_Loop(path, newcell, newdir, startx, starty, targetx, targety, threshhold)) {
  845. return(false);
  846. }
  847. /*
  848. ** Since we need to eliminate a diagonal we must pretend the upon
  849. ** attaining this square, we were moving turned further in the
  850. ** search direction then we really were.
  851. */
  852. newdir = Next_Direction(newdir, (FacingType)(search*2));
  853. }
  854. /*
  855. ** Find out which side of the line this cell is on. If it is on
  856. ** a side, then store off that side.
  857. */
  858. int newx = Cell_X(newcell);
  859. int newy = Cell_Y(newcell);
  860. int val = Point_Relative_To_Line(newx, newy, startx, starty, targetx, targety);
  861. if (val) {
  862. oldval = val;
  863. online = false;
  864. } else {
  865. online = true;
  866. }
  867. cellcount++;
  868. if (cellcount == MAX_PATH_EDGE_FOLLOW) {
  869. return(false);
  870. }
  871. }
  872. /*
  873. ** If we have found the target spot, we are done.
  874. */
  875. if (newcell == target) {
  876. path->Command[path->Length] = END;
  877. return(true);
  878. }
  879. /*
  880. ** If we make a full circle back to our original spot, get out.
  881. */
  882. if (newcell == firstcell && newdir == firstdir) {
  883. return(false);
  884. }
  885. if (firstcell == -1) {
  886. firstcell = newcell;
  887. firstdir = newdir;
  888. }
  889. /*
  890. ** Because we moved, our facing is now incorrect. We want to face toward
  891. ** the impassable edge we are following (well, not actually toward, but
  892. ** a little past so that we can turn corners). We have to turn 45/90 degrees
  893. ** more than expected in anticipation of the pending 45/90 degree turn at
  894. ** the start of this loop.
  895. */
  896. #ifdef DIAGONAL
  897. olddir = Next_Direction(newdir, (FacingType)(-(int)search*3));
  898. #else
  899. olddir = Next_Direction(newdir, (FacingType)(-(int)search*4));
  900. #endif
  901. oldcell = newcell;
  902. }
  903. /*
  904. ** The maximum search path is exhausted... abort with a failure.
  905. */
  906. return(false);
  907. }
  908. /***********************************************************************************************
  909. * Optimize_Moves -- Optimize the move list. *
  910. * *
  911. * INPUT: char *moves to optimize *
  912. * *
  913. * OUTPUT: none (list is optimized) *
  914. * *
  915. * WARNINGS: EMPTY moves are used to hold the place of eliminated *
  916. * commands. Also, NEVER call this routine with a list that *
  917. * contains illegal commands. The list MUST be terminated *
  918. * with a EOL command *
  919. * *
  920. * HISTORY: *
  921. * 07/08/1991 CY : Created. *
  922. * 06/01/1992 JLB : Optimized and commented. *
  923. *=============================================================================================*/
  924. #define EMPTY (FacingType)-2
  925. int FootClass::Optimize_Moves(PathType * path, MoveType threshhold)
  926. //int Optimize_Moves(PathType *path, int (*callback)(CELL, FacingType), int threshold)
  927. {
  928. /*
  929. ** Facing command pair adjustment table. Compare the facing difference between
  930. ** the two commands. 0 means no optimization is possible. 3 means backtracking
  931. ** so eliminate both commands. Any other value adjusts the first command facing.
  932. */
  933. #ifdef DIAGONAL
  934. static FacingType _trans[FACING_COUNT] = {(FacingType)0, (FacingType)0, (FacingType)1, (FacingType)2, (FacingType)3, (FacingType)-2, (FacingType)-1, (FacingType)0}; // Smoothing.
  935. #else
  936. static FacingType _trans[FACING_COUNT] = {(FacingType)0, (FacingType)0, (FacingType)0, (FacingType)2, (FacingType)3, (FacingType)-2, (FacingType)0, (FacingType)0};
  937. #endif
  938. FacingType * cmd1, // Floating first command pointer.
  939. * cmd2, // Floating second command pointer.
  940. newcmd; // Calculated new optimized command.
  941. FacingType newdir; // Tentative new direction for smoothing.
  942. CELL cell; // Working cell (as it moves along path).
  943. /*
  944. ** Abort if there is any illegal parameter.
  945. */
  946. if (!path || !path->Command) return(0);
  947. /*
  948. ** Optimization loop -- start scanning with the
  949. ** first pair of commands (if there are at least two
  950. ** in the command list).
  951. */
  952. path->Command[path->Length] = END; // Force end of list.
  953. if (path->Length == 0) return(0);
  954. cell = path->Start;
  955. if (path->Length > 1) {
  956. cmd2 = path->Command + 1;
  957. while (*cmd2 != END) {
  958. /*
  959. ** Set the cmd1 pointer to point to the valid command closest, but
  960. ** previous to cmd2. Be sure not to go previous to the head of the
  961. ** command list.
  962. */
  963. cmd1 = cmd2-1;
  964. while (*cmd1 == EMPTY && cmd1 != path->Command) {
  965. cmd1--;
  966. }
  967. /*
  968. ** If there isn't any valid previous command, then bump the
  969. ** cmd pointers to the next command pair and continue...
  970. */
  971. if (*cmd1 == EMPTY) {
  972. cmd2++;
  973. continue;
  974. }
  975. /*
  976. ** Fetch precalculated command change value. 0 means leave
  977. ** command set alone, 3 means backtrack and eliminate two
  978. ** commands. Any other value is new direction and eliminate
  979. ** one command.
  980. */
  981. newcmd = (FacingType)(*cmd2 - *cmd1);
  982. if (newcmd < FACING_N) newcmd = (FacingType)(newcmd + FACING_COUNT);
  983. newcmd = _trans[newcmd];
  984. /*
  985. ** Check for backtracking. If this occurs, then eliminate the
  986. ** two commands. This is the easiest optimization.
  987. */
  988. if (newcmd == FACING_SE) {
  989. *cmd1 = EMPTY;
  990. *cmd2++ = EMPTY;
  991. continue;
  992. }
  993. /*
  994. ** If an optimization code was found the process it. The command is a facing
  995. ** offset to more directly travel toward the immediate destination cell.
  996. */
  997. if (newcmd) {
  998. /*
  999. ** Optimizations differ when dealing with diagonals. Especially when dealing
  1000. ** with diagonals of 90 degrees. In such a case, 90 degree optimizations can
  1001. ** only be optimized if the intervening cell is passable. The distance travelled
  1002. ** is the same, but the path is less circuitous.
  1003. */
  1004. if (*cmd1 & FACING_NE) {
  1005. /*
  1006. ** Diagonal optimizations are always only 45
  1007. ** degree adjustments.
  1008. */
  1009. newdir = Next_Direction(*cmd1, (newcmd < FACING_N) ? (FacingType)-1 : (FacingType)1);
  1010. /*
  1011. ** Diagonal 90 degree changes can be smoothed, although
  1012. ** the path isn't any shorter.
  1013. */
  1014. if (ABS((int)newcmd) == 1) {
  1015. if (Passable_Cell(Adjacent_Cell(cell, newdir), newdir, -1, threshhold)) {
  1016. *cmd2 = newdir;
  1017. *cmd1 = newdir;
  1018. }
  1019. // BOB 16.12.92
  1020. cell = Adjacent_Cell(cell, *cmd1);
  1021. cmd2++;
  1022. continue;
  1023. }
  1024. } else {
  1025. newdir = Next_Direction(*cmd1, newcmd);
  1026. }
  1027. /*
  1028. ** Allow shortening turn only on right angle moves that are based on
  1029. ** 90 degrees. Always allow 135 degree optimizations.
  1030. */
  1031. *cmd2 = newdir;
  1032. *cmd1 = EMPTY;
  1033. /*
  1034. ** Backup what it thinks is the current cell.
  1035. */
  1036. while (*cmd1 == EMPTY && cmd1 != path->Command) {
  1037. cmd1--;
  1038. }
  1039. if (*cmd1 != EMPTY) {
  1040. cell = Adjacent_Cell(cell, Next_Direction(*cmd1, FACING_S));
  1041. } else {
  1042. cell = path->Start;
  1043. }
  1044. continue;
  1045. }
  1046. /*
  1047. ** Since we could not make an optimization, we move our
  1048. ** head pointer forward.
  1049. */
  1050. cell = Adjacent_Cell(cell, *cmd1);
  1051. cmd2++;
  1052. }
  1053. }
  1054. /*
  1055. ** Pack the command list to remove any EMPTY command entries.
  1056. */
  1057. cmd1 = path->Command;
  1058. cmd2 = path->Command;
  1059. cell = path->Start;
  1060. path->Cost = 0;
  1061. path->Length = 0;
  1062. while (*cmd2 != END) {
  1063. if (*cmd2 != EMPTY) {
  1064. cell = Adjacent_Cell(cell, *cmd2);
  1065. path->Cost+= Passable_Cell(cell, *cmd2, -1, threshhold);
  1066. path->Length++;
  1067. *cmd1++ = *cmd2;
  1068. }
  1069. cmd2++;
  1070. }
  1071. path->Length++;
  1072. *cmd1 = END;
  1073. return(path->Length);
  1074. }
  1075. CELL FootClass::Safety_Point(CELL src, CELL dst, int start, int max)
  1076. {
  1077. FacingType dir;
  1078. CELL next;
  1079. int lp;
  1080. dir = (FacingType)(CELL_FACING(src, dst) ^ 4) - 1;
  1081. /*
  1082. ** Loop through the different acceptable distances.
  1083. */
  1084. for (int dist = start; dist < max; dist ++) {
  1085. /*
  1086. ** Move to the starting location.
  1087. */
  1088. next = dst;
  1089. for (lp = 0; lp < dist; lp ++) {
  1090. next = Adjacent_Cell(next, dir);
  1091. }
  1092. if (dir & 1) {
  1093. /*
  1094. ** If our direction is diagonal than we need to check
  1095. ** only one side which is as long as both of the old sides
  1096. ** together.
  1097. */
  1098. for (lp = 0; lp < dist << 1; lp ++) {
  1099. next = Adjacent_Cell(next, dir + 3);
  1100. if (!Can_Enter_Cell(next)) {
  1101. return(next);
  1102. }
  1103. }
  1104. } else {
  1105. /*
  1106. ** If our direction is not diagonal than we need to check two
  1107. ** sides so that we are checking a corner like location.
  1108. */
  1109. for (lp = 0; lp < dist; lp ++) {
  1110. next = Adjacent_Cell(next, dir + 2);
  1111. if (!Can_Enter_Cell(next)) {
  1112. return(next);
  1113. }
  1114. }
  1115. for (lp = 0; lp < dist; lp ++) {
  1116. next = Adjacent_Cell(next, dir + 4);
  1117. if (!Can_Enter_Cell(next)) {
  1118. return(next);
  1119. }
  1120. }
  1121. }
  1122. }
  1123. return(-1);
  1124. }
  1125. int FootClass::Passable_Cell(CELL cell, FacingType face, int threat, MoveType threshhold)
  1126. {
  1127. MoveType move = Can_Enter_Cell(cell, face);
  1128. if (move < MOVE_MOVING_BLOCK && Distance(Cell_Coord(cell)) > 0x0100) threshhold = MOVE_MOVING_BLOCK;
  1129. if (move > threshhold) return(0);
  1130. if (Session.Type == GAME_NORMAL) {
  1131. if (threat != -1) {
  1132. if (::Distance(Cell_Coord(cell), Cell_Coord(DestLocation)) > (THREAT_THRESHOLD * CELL_LEPTON_W)) {
  1133. // if (Map.Cell_Distance(cell, DestLocation) > THREAT_THRESHOLD) {
  1134. if (Map.Cell_Threat(cell, Owner()) > threat)
  1135. return(0);
  1136. }
  1137. }
  1138. }
  1139. static int _value[MOVE_COUNT] = {
  1140. 1, // MOVE_OK
  1141. 1, // MOVE_CLOAK
  1142. 3, // MOVE_MOVING_BLOCK
  1143. 8, // MOVE_DESTROYABLE
  1144. 10, // MOVE_TEMP
  1145. 0 // MOVE_NO
  1146. };
  1147. return(_value[move]);
  1148. }