FINDPATH.CPP 52 KB

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