FACE.CPP 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/FACE.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 : FACE.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 03/08/96 *
  30. * *
  31. * Last Update : March 8, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Desired_Facing8 -- Determines facing from one coordinate to another. *
  36. * Desired_Facing256 -- Determines facing from one coordinate to another. *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "function.h"
  39. /***********************************************************************************************
  40. * Desired_Facing8 -- Determines facing from one coordinate to another. *
  41. * *
  42. * This routine will find the facing (compass direction) from one location to another. *
  43. * Typical use of this is in find path and other 'monster' movement logic. *
  44. * *
  45. * INPUT: x1,y1 -- X and Y coordinates for the source location. The coordinate 0,0 is *
  46. * presumed to be the northwest corner of the map. *
  47. * *
  48. * x2,y2 -- X and Y coordinates for the destination (target) location. *
  49. * *
  50. * OUTPUT: Returns with the facing from the first coordinate to the second coordinate. The *
  51. * value returned will range from 0 being North, increasing clockwise until reaching *
  52. * 255 which is just shy of North in a Westerly direction. *
  53. * *
  54. * WARNINGS: This routine is only accurate to the 8 primary compass directions. It is much *
  55. * faster than the Desired_Facing256() function so it should be used where speed *
  56. * is more important than accuracy. *
  57. * *
  58. * HISTORY: *
  59. * 03/08/1996 JLB : Created. *
  60. *=============================================================================================*/
  61. DirType Desired_Facing8(int x1, int y1, int x2, int y2)
  62. {
  63. int index = 0; // Facing composite value.
  64. /*
  65. ** Figure the absolute X difference. This determines
  66. ** if the facing is leftward or not.
  67. */
  68. int xdiff = x2-x1;
  69. if (xdiff < 0) {
  70. index |= 0x00C0;
  71. xdiff = -xdiff;
  72. }
  73. /*
  74. ** Figure the absolute Y difference. This determines
  75. ** if the facing is downward or not. This also clarifies
  76. ** exactly which quadrant the facing lies.
  77. */
  78. int ydiff = y1-y2;
  79. if (ydiff < 0) {
  80. index ^= 0x0040;
  81. ydiff = -ydiff;
  82. }
  83. /*
  84. ** Determine which of the two direction offsets it bigger. The
  85. ** offset direction that is bigger (X or Y) will indicate which
  86. ** orthogonal direction the facing is closer to.
  87. */
  88. unsigned bigger;
  89. unsigned smaller;
  90. if (xdiff < ydiff) {
  91. smaller = xdiff;
  92. bigger = ydiff;
  93. } else {
  94. smaller = ydiff;
  95. bigger = xdiff;
  96. }
  97. /*
  98. ** If on the diagonal, then incorporate this into the facing
  99. ** and then bail. The facing is known.
  100. */
  101. if (((bigger+1)/2) <= smaller) {
  102. index += 0x0020;
  103. return(DirType(index));
  104. }
  105. /*
  106. ** Determine if the facing is closer to the Y axis or
  107. ** the X axis.
  108. */
  109. int adder = (index & 0x0040);
  110. if (xdiff == bigger) {
  111. adder ^= 0x0040;
  112. }
  113. index += adder;
  114. return(DirType(index));
  115. }
  116. /***********************************************************************************************
  117. * Desired_Facing256 -- Determines facing from one coordinate to another. *
  118. * *
  119. * This routine will figure the facing from the source coordinate toward the destination *
  120. * coordinate. Typically, this routine is used for movement and other 'monster' logic. It *
  121. * is more accurate than the corresponding Desired_Facing8() function, but is slower. *
  122. * *
  123. * INPUT: srcx, srcy -- The source coordinate to determine the facing from. *
  124. * *
  125. * dstx, dsty -- The destination (or target) coordinate to determine the facing *
  126. * toward. *
  127. * *
  128. * OUTPUT: Returns with the facing from the source coordinate toward the destination *
  129. * coordinate with 0 being North increasing in a clockwise direction. 64 is East, *
  130. * 128 is South, etc. *
  131. * *
  132. * WARNINGS: The coordinate 0,0 is presumed to be in the Northwest corner of the map. *
  133. * Although this routine is fast, it is not as fast as Desired_Facing8(). *
  134. * *
  135. * HISTORY: *
  136. * 03/08/1996 JLB : Created. *
  137. *=============================================================================================*/
  138. DirType Desired_Facing256(int srcx, int srcy, int dstx, int dsty)
  139. {
  140. int composite=0; // Facing built from intermediate calculations.
  141. /*
  142. ** Fetch the absolute X difference. This also gives a clue as
  143. ** to which hemisphere the direction lies.
  144. */
  145. int xdiff = dstx - srcx;
  146. if (xdiff < 0) {
  147. composite |= 0x00C0;
  148. xdiff = -xdiff;
  149. }
  150. /*
  151. ** Fetch the absolute Y difference. This clarifies the exact
  152. ** quadrant that the direction lies.
  153. */
  154. int ydiff = srcy - dsty;
  155. if (ydiff < 0) {
  156. composite ^= 0x0040;
  157. ydiff = -ydiff;
  158. }
  159. /*
  160. ** Bail early if the coordinates are the same. This check also
  161. ** has the added bonus of ensuring that checking for division
  162. ** by zero is not needed in the following section.
  163. */
  164. if (xdiff == 0 && ydiff == 0) return(DirType(0xFF));
  165. /*
  166. ** Determine which of the two direction offsets it bigger. The
  167. ** offset direction that is bigger (X or Y) will indicate which
  168. ** orthogonal direction the facing is closer to.
  169. */
  170. unsigned bigger;
  171. unsigned smaller;
  172. if (xdiff < ydiff) {
  173. smaller = xdiff;
  174. bigger = ydiff;
  175. } else {
  176. smaller = ydiff;
  177. bigger = xdiff;
  178. }
  179. /*
  180. ** Now that the quadrant is known, we need to determine how far
  181. ** from the orthogonal directions, the facing lies. This value
  182. ** is calculated as a ratio from 0 (matches orthogonal) to 31
  183. ** (matches diagonal).
  184. */
  185. //lint -e414 Division by zero cannot occur here.
  186. int frac = (smaller * 32U) / bigger;
  187. /*
  188. ** Given the quadrant and knowing whether the facing is closer
  189. ** to the X or Y axis, we must make an adjustment toward the
  190. ** subsequent quadrant if necessary.
  191. */
  192. int adder = (composite & 0x0040);
  193. if (xdiff > ydiff) {
  194. adder ^= 0x0040;
  195. }
  196. if (adder) {
  197. frac = (adder - frac)-1;
  198. }
  199. /*
  200. ** Integrate the fraction value into the quadrant.
  201. */
  202. composite += frac;
  203. /*
  204. ** Return with the final facing value.
  205. */
  206. return(DirType(composite & 0x00FF));
  207. }