INLINE.H 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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/INLINE.H 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 : INLINE.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 08/21/96 *
  30. * *
  31. * Last Update : September 30, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. *
  36. * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. *
  37. * Cell_Coord -- Convert a cell to a coordinate value. *
  38. * Cell_To_Lepton -- Convert a cell distance into a lepton distance. *
  39. * Cell_X -- Fetch the X cell component from the cell value. *
  40. * Cell_Y -- Fetch the Y cell component from the cell value specified. *
  41. * Coord_Add -- Adds coordinates together. *
  42. * Coord_Fraction -- Discards all but the sub-cell components of the coordinate. *
  43. * Coord_Mid -- Finds the midpoint between two coordinates. *
  44. * Coord_Snap -- Coerce coordinate to refer to center of a cell. *
  45. * Coord_Sub -- Subtracts one coordinate from another. *
  46. * Coord_Whole -- Discards the sub-cell components of the coordinate. *
  47. * Coord_X -- Fetches the X lepton component from a coordinate value. *
  48. * Coord_XCell -- Fetch the X cell component from a coordinate value. *
  49. * Coord_XLepton -- Fetch the X sub-cell lepton component from the coordinate. *
  50. * Coord_Y -- Fetch the Y lepton component from the coordinate value. *
  51. * Coord_YCell -- Fetch the Y cell component from a coordinate. *
  52. * Coord_YLepton -- Fetches the Y lepton sub-cell component from the coordinate. *
  53. * Dir_Facing -- Convert a DirType into a FacingType value. *
  54. * Dir_To_16 -- Convert a facing to a 0..15 value. *
  55. * Dir_To_32 -- Convert a DirType into a 0..31 value. *
  56. * Dir_To_8 -- Convert a DirType into a value from 0 to 7. *
  57. * Direction -- Calculates the DirType from one cell to another. *
  58. * Direction -- Determines the facing value from one coordinate to another. *
  59. * Direction256 -- Calculate the facing value from one coordinate to another. *
  60. * Direction8 -- Fetches the direction from one coordinate to another. *
  61. * Distance -- Finds the distance between two arbitrary points. *
  62. * Facing_Dir -- Convert a FacingType into a DirType. *
  63. * Lepton_To_Cell -- Convert lepton distance to cell distance. *
  64. * Lepton_To_Pixel -- Convert a lepton value into pixel value. *
  65. * Percent_Chance -- Calculate a percentage chance event. *
  66. * Pixel_To_Lepton -- Convert pixel value into lepton equivalent. *
  67. * Random_Pick -- Pick a random number in a specified range. *
  68. * Sim_Percent_Chance -- Calculates a percentage chance event for local events. *
  69. * Sim_Random_Pick -- Picks a random number that will not affect the game. *
  70. * Text_String -- Convert a text number into a text pointer. *
  71. * XYP_COORD -- Convert pixel components into a coordinate value. *
  72. * XYP_Coord -- Combine pixel values into a coordinate. *
  73. * XY_Cell -- Create a cell from X and Y cell components. *
  74. * XY_Coord -- Convert X Y lepton components into a COORD. *
  75. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  76. #ifndef INLINE_H
  77. #define INLINE_H
  78. /***********************************************************************************************
  79. * Lepton_To_Pixel -- Convert a lepton value into pixel value. *
  80. * *
  81. * Use this routine to convert the specified lepton value into it's pixel corresponding *
  82. * value. The pixel value returned will be the closest pixel value to the lepton value. It *
  83. * will round up or down as necessary. *
  84. * *
  85. * INPUT: lepton -- The lepton value to convert into a pixel value. *
  86. * *
  87. * OUTPUT: Returns with the lepton value rounded to the nearest pixel component. *
  88. * *
  89. * WARNINGS: Precision is not maintained by this routine. Thus, if a value is converted to *
  90. * pixel and then back to leptons, the value will probably not be the same. *
  91. * *
  92. * HISTORY: *
  93. * 08/21/1996 JLB : Created. *
  94. *=============================================================================================*/
  95. inline int Lepton_To_Pixel(LEPTON lepton)
  96. {
  97. return (((int)(signed short)lepton * ICON_PIXEL_W) + (ICON_LEPTON_W / 2)) / ICON_LEPTON_W;
  98. }
  99. /***********************************************************************************************
  100. * Pixel_To_Lepton -- Convert pixel value into lepton equivalent. *
  101. * *
  102. * This routine will take the specified pixel value and convert it into lepton value. *
  103. * *
  104. * INPUT: pixel -- The pixel value to convert. *
  105. * *
  106. * OUTPUT: Returns with the lepton equivalent of the pixel value specified. *
  107. * *
  108. * WARNINGS: none *
  109. * *
  110. * HISTORY: *
  111. * 08/21/1996 JLB : Created. *
  112. *=============================================================================================*/
  113. inline LEPTON Pixel_To_Lepton(int pixel)
  114. {
  115. return (LEPTON)(((pixel * ICON_LEPTON_W) + (ICON_PIXEL_W / 2)) / ICON_PIXEL_W);
  116. }
  117. /***********************************************************************************************
  118. * XY_Coord -- Convert X Y lepton components into a COORD. *
  119. * *
  120. * This routine will take the specified X and Y lepton components and combine them into *
  121. * a coordinate value. *
  122. * *
  123. * INPUT: x,y -- The X and Y lepton components to combine. *
  124. * *
  125. * OUTPUT: Returns with a coordinate value that is created from the X and Y lepton components.*
  126. * *
  127. * WARNINGS: none *
  128. * *
  129. * HISTORY: *
  130. * 08/21/1996 JLB : Created. *
  131. *=============================================================================================*/
  132. inline COORDINATE XY_Coord(LEPTON x, LEPTON y)
  133. {
  134. COORD_COMPOSITE coord;
  135. coord.Sub.X.Raw = x;
  136. coord.Sub.Y.Raw = y;
  137. return(coord.Coord);
  138. }
  139. /***********************************************************************************************
  140. * XYP_COORD -- Convert pixel components into a coordinate value. *
  141. * *
  142. * This routine will take the specified pixel components and convert and combine them into *
  143. * a coordinate value. *
  144. * *
  145. * INPUT: x,y -- The X and Y pixel components to coerce into a coordinate value. *
  146. * *
  147. * OUTPUT: Returns with the coordinate value that matches the pixel values specified. *
  148. * *
  149. * WARNINGS: none *
  150. * *
  151. * HISTORY: *
  152. * 08/21/1996 JLB : Created. *
  153. *=============================================================================================*/
  154. inline COORDINATE XYP_COORD(int x, int y)
  155. {
  156. return(XY_Coord(Pixel_To_Lepton(x), Pixel_To_Lepton(y)));
  157. }
  158. /***********************************************************************************************
  159. * Coord_XCell -- Fetch the X cell component from a coordinate value. *
  160. * *
  161. * This routine will extract the X cell component from the coordinate value specified and *
  162. * return the value. *
  163. * *
  164. * INPUT: coord -- The coordinate value to extract the X component from. *
  165. * *
  166. * OUTPUT: Returns with the X cell component of the coordinate value. *
  167. * *
  168. * WARNINGS: none *
  169. * *
  170. * HISTORY: *
  171. * 08/21/1996 JLB : Created. *
  172. *=============================================================================================*/
  173. inline CELL Coord_XCell(COORDINATE coord)
  174. {
  175. return(((COORD_COMPOSITE &)coord).Sub.X.Sub.Cell);
  176. }
  177. /***********************************************************************************************
  178. * Coord_YCell -- Fetch the Y cell component from a coordinate. *
  179. * *
  180. * This routine will extract the Y cell component from the coordinate value specified. *
  181. * *
  182. * INPUT: coord -- The coordinate to extract the Y cell from. *
  183. * *
  184. * OUTPUT: Returns with just the Y cell component of the coordinate value. *
  185. * *
  186. * WARNINGS: none *
  187. * *
  188. * HISTORY: *
  189. * 08/21/1996 JLB : Created. *
  190. *=============================================================================================*/
  191. inline CELL Coord_YCell(COORDINATE coord)
  192. {
  193. return(((COORD_COMPOSITE &)coord).Sub.Y.Sub.Cell);
  194. }
  195. /***********************************************************************************************
  196. * XY_Cell -- Create a cell from X and Y cell components. *
  197. * *
  198. * This routine will construct a cell value by taking the X and Y cell value components *
  199. * and combining them appropriately. *
  200. * *
  201. * INPUT: x,y -- The X and Y cell components to combine. *
  202. * *
  203. * OUTPUT: Returns with the CELL value created from the specified components. *
  204. * *
  205. * WARNINGS: none *
  206. * *
  207. * HISTORY: *
  208. * 08/21/1996 JLB : Created. *
  209. *=============================================================================================*/
  210. inline CELL XY_Cell(int x, int y)
  211. {
  212. CELL_COMPOSITE cell;
  213. cell.Cell = 0;
  214. cell.Sub.X = x;
  215. cell.Sub.Y = y;
  216. return(cell.Cell);
  217. }
  218. /***********************************************************************************************
  219. * Cell_To_Lepton -- Convert a cell distance into a lepton distance. *
  220. * *
  221. * This routine will take the cell distance specified and convert it into a lepton distance.*
  222. * *
  223. * INPUT: cell_distance -- The distance in cells to convert. *
  224. * *
  225. * OUTPUT: Returns with the lepton equivalent of the cell distance specified. *
  226. * *
  227. * WARNINGS: none *
  228. * *
  229. * HISTORY: *
  230. * 08/21/1996 JLB : Created. *
  231. *=============================================================================================*/
  232. inline LEPTON Cell_To_Lepton(int cell_distance)
  233. {
  234. LEPTON_COMPOSITE lepton;
  235. lepton.Sub.Cell = (unsigned char)cell_distance;
  236. lepton.Sub.Lepton = 0;
  237. return(lepton.Raw);
  238. }
  239. /***********************************************************************************************
  240. * Lepton_To_Cell -- Convert lepton distance to cell distance. *
  241. * *
  242. * This routine will convert the specified lepton distance into the closest cell distance *
  243. * possible. This might require rounding up or down as necessary. *
  244. * *
  245. * INPUT: lepton_distance -- The lepton distance to convert. *
  246. * *
  247. * OUTPUT: Returns with the cell distance that most closely corresponds to the lepton *
  248. * distance specified. *
  249. * *
  250. * WARNINGS: none *
  251. * *
  252. * HISTORY: *
  253. * 08/21/1996 JLB : Created. *
  254. *=============================================================================================*/
  255. inline int Lepton_To_Cell(LEPTON lepton_distance)
  256. {
  257. if (((LEPTON_COMPOSITE &)lepton_distance).Sub.Lepton >= (CELL_LEPTON_W/2)) {
  258. return(((LEPTON_COMPOSITE &)lepton_distance).Sub.Cell + 1);
  259. }
  260. return(((LEPTON_COMPOSITE &)lepton_distance).Sub.Cell);
  261. }
  262. /***********************************************************************************************
  263. * Coord_X -- Fetches the X lepton component from a coordinate value. *
  264. * *
  265. * This routine will extract the X lepton component from the coordinate. *
  266. * *
  267. * INPUT: coord -- The coordinate to extract the X lepton equivalent from. *
  268. * *
  269. * OUTPUT: Returns with the X lepton portion of the coordinate value specified. *
  270. * *
  271. * WARNINGS: none *
  272. * *
  273. * HISTORY: *
  274. * 08/21/1996 JLB : Created. *
  275. *=============================================================================================*/
  276. inline LEPTON Coord_X(COORDINATE coord)
  277. {
  278. return(((COORD_COMPOSITE &)coord).Sub.X.Raw);
  279. }
  280. /***********************************************************************************************
  281. * Coord_Y -- Fetch the Y lepton component from the coordinate value. *
  282. * *
  283. * This routine will extract the Y lepton component from the coordinate value specified. *
  284. * *
  285. * INPUT: coord -- The coordinate value to dissect. *
  286. * *
  287. * OUTPUT: Returns with the Y lepton component from the specified coordinate value. *
  288. * *
  289. * WARNINGS: none *
  290. * *
  291. * HISTORY: *
  292. * 08/21/1996 JLB : Created. *
  293. *=============================================================================================*/
  294. inline LEPTON Coord_Y(COORDINATE coord)
  295. {
  296. return(((COORD_COMPOSITE &)coord).Sub.Y.Raw);
  297. }
  298. /***********************************************************************************************
  299. * Cell_X -- Fetch the X cell component from the cell value. *
  300. * *
  301. * This routine will extract the X cell component from the cell value specified. *
  302. * *
  303. * INPUT: cell -- The cell to extract. *
  304. * *
  305. * OUTPUT: Returns with the X cell component portion of the cell value specified. *
  306. * *
  307. * WARNINGS: none *
  308. * *
  309. * HISTORY: *
  310. * 08/21/1996 JLB : Created. *
  311. *=============================================================================================*/
  312. inline int Cell_X(CELL cell)
  313. {
  314. return(((CELL_COMPOSITE &)cell).Sub.X);
  315. }
  316. /***********************************************************************************************
  317. * Cell_Y -- Fetch the Y cell component from the cell value specified. *
  318. * *
  319. * This routine will extract the Y cell component from the cell value. *
  320. * *
  321. * INPUT: cell -- The cell value to extract from. *
  322. * *
  323. * OUTPUT: Returns with the Y cell component of the cell value specified. *
  324. * *
  325. * WARNINGS: none *
  326. * *
  327. * HISTORY: *
  328. * 08/21/1996 JLB : Created. *
  329. *=============================================================================================*/
  330. inline int Cell_Y(CELL cell)
  331. {
  332. return(((CELL_COMPOSITE &)cell).Sub.Y);
  333. }
  334. /***********************************************************************************************
  335. * Coord_XLepton -- Fetch the X sub-cell lepton component from the coordinate. *
  336. * *
  337. * This routine will extract just the X sub cell lepton component from the coordinate *
  338. * specified. *
  339. * *
  340. * INPUT: coord -- The coordinate value to extract from. *
  341. * *
  342. * OUTPUT: Returns with the X lepton component of the coordinate that is part of the cell. *
  343. * Thus, a coordinate that exactly lines up on the left edge of a cell would return *
  344. * zero. One that exactly lines up on the right edge would return CELL_LEPTON_W. *
  345. * *
  346. * WARNINGS: none *
  347. * *
  348. * HISTORY: *
  349. * 08/21/1996 JLB : Created. *
  350. *=============================================================================================*/
  351. inline int Coord_XLepton(COORDINATE coord)
  352. {
  353. return(((COORD_COMPOSITE &)coord).Sub.X.Sub.Lepton);
  354. }
  355. /***********************************************************************************************
  356. * Coord_YLepton -- Fetches the Y lepton sub-cell component from the coordinate. *
  357. * *
  358. * This routine will extract the sub-cell Y lepton portion of the coordinate. *
  359. * *
  360. * INPUT: coord -- The coordinate to dissect. *
  361. * *
  362. * OUTPUT: Returns with just the Y lepton portion of the coordinate and only for the sub-cell *
  363. * it refers to. *
  364. * *
  365. * WARNINGS: none *
  366. * *
  367. * HISTORY: *
  368. * 08/23/1996 JLB : Created. *
  369. *=============================================================================================*/
  370. inline int Coord_YLepton(COORDINATE coord)
  371. {
  372. return(((COORD_COMPOSITE &)coord).Sub.Y.Sub.Lepton);
  373. }
  374. /***********************************************************************************************
  375. * XYP_Coord -- Combine pixel values into a coordinate. *
  376. * *
  377. * This will convert X and Y pixel values into a coordinate. Primarily this is used for *
  378. * converting mouse clicks into coordinate values. *
  379. * *
  380. * INPUT: x,y -- The X and Y pixel coordinates to convert. Origin is upper left corner. *
  381. * *
  382. * OUTPUT: Returns with the coordinate that most closely corresponds to the pixel values *
  383. * specified. *
  384. * *
  385. * WARNINGS: The coordinate is relative to the upper left corner (0,0). To conver the *
  386. * coordinate to a game relative one, it must be biased by the display coordinate *
  387. * of the tactical map and the screen position of the tactical display. *
  388. * *
  389. * HISTORY: *
  390. * 08/23/1996 JLB : Created. *
  391. *=============================================================================================*/
  392. inline COORDINATE XYP_Coord(int x, int y)
  393. {
  394. COORD_COMPOSITE coord;
  395. coord.Sub.X.Raw = Pixel_To_Lepton(x);
  396. coord.Sub.Y.Raw = Pixel_To_Lepton(y);
  397. return(coord.Coord);
  398. }
  399. /***********************************************************************************************
  400. * Cell_Coord -- Convert a cell to a coordinate value. *
  401. * *
  402. * This routine will convert the specified cell into a coordinat value. The coordinate *
  403. * will refer to the center of the cell specified. *
  404. * *
  405. * INPUT: cell -- The cell to convert into a coordinate. *
  406. * *
  407. * OUTPUT: Returns with the coordinate that refers to the center of the cell specified. *
  408. * *
  409. * WARNINGS: none *
  410. * *
  411. * HISTORY: *
  412. * 08/23/1996 JLB : Created. *
  413. *=============================================================================================*/
  414. inline COORDINATE Cell_Coord(CELL cell)
  415. {
  416. COORD_COMPOSITE coord;
  417. coord.Sub.X.Sub.Cell = (unsigned char)(((CELL_COMPOSITE &)cell).Sub.X);
  418. coord.Sub.X.Sub.Lepton = (unsigned char)(CELL_LEPTON_W / 2);
  419. coord.Sub.Y.Sub.Cell = (unsigned char)(((CELL_COMPOSITE &)cell).Sub.Y);
  420. coord.Sub.Y.Sub.Lepton = (unsigned char)(CELL_LEPTON_W / 2);
  421. return(coord.Coord);
  422. }
  423. /***********************************************************************************************
  424. * Coord_Snap -- Coerce coordinate to refer to center of a cell. *
  425. * *
  426. * This routine will take the specified coordinate and force it to refer to the center of *
  427. * the cell. *
  428. * *
  429. * INPUT: coord -- The coordinate to modify. *
  430. * *
  431. * OUTPUT: Returns with the specified coordinate after it has been modified to refer to the *
  432. * center of the cell. *
  433. * *
  434. * WARNINGS: none *
  435. * *
  436. * HISTORY: *
  437. * 08/23/1996 JLB : Created. *
  438. *=============================================================================================*/
  439. inline COORDINATE Coord_Snap(COORDINATE coord)
  440. {
  441. ((COORD_COMPOSITE &)coord).Sub.X.Sub.Lepton = CELL_LEPTON_W/2;
  442. ((COORD_COMPOSITE &)coord).Sub.Y.Sub.Lepton = CELL_LEPTON_W/2;
  443. return(coord);
  444. }
  445. /***********************************************************************************************
  446. * Coord_Fraction -- Discards all but the sub-cell components of the coordinate. *
  447. * *
  448. * Use this routine to discard the cell components of the coordinate, leaving only the *
  449. * sub-cell component. *
  450. * *
  451. * INPUT: coord -- The coordinate to modify. *
  452. * *
  453. * OUTPUT: Returns with just the sub-cell components intact from the supplied coordinate. *
  454. * *
  455. * WARNINGS: none *
  456. * *
  457. * HISTORY: *
  458. * 08/23/1996 JLB : Created. *
  459. *=============================================================================================*/
  460. inline COORDINATE Coord_Fraction(COORDINATE coord)
  461. {
  462. ((COORD_COMPOSITE &)coord).Sub.X.Sub.Cell = 0;
  463. ((COORD_COMPOSITE &)coord).Sub.Y.Sub.Cell = 0;
  464. return(coord);
  465. }
  466. /***********************************************************************************************
  467. * Coord_Whole -- Discards the sub-cell components of the coordinate. *
  468. * *
  469. * This routine will discard the sub-cell components, leaving only the whole cell portion. *
  470. * *
  471. * INPUT: coord -- The coordinate to modify. *
  472. * *
  473. * OUTPUT: Returns with only the whole cell components of the coordinate intact. The *
  474. * resulting coordinate will refer to the upper left corner of the cell. *
  475. * *
  476. * WARNINGS: none *
  477. * *
  478. * HISTORY: *
  479. * 08/23/1996 JLB : Created. *
  480. *=============================================================================================*/
  481. inline COORDINATE Coord_Whole(COORDINATE coord)
  482. {
  483. ((COORD_COMPOSITE &)coord).Sub.X.Sub.Lepton = 0;
  484. ((COORD_COMPOSITE &)coord).Sub.Y.Sub.Lepton = 0;
  485. return(coord);
  486. }
  487. /***********************************************************************************************
  488. * Coord_Add -- Adds coordinates together. *
  489. * *
  490. * This routine will add one coordinate to another. Actually, it adds the X and Y components*
  491. * separately (signed) and then recombines them back into a coordinate. *
  492. * *
  493. * INPUT: coord1 -- One coordinate to add. *
  494. * *
  495. * coord2 -- The other coordinate to add. *
  496. * *
  497. * OUTPUT: Returns with the logical add of the two coordinates. *
  498. * *
  499. * WARNINGS: none *
  500. * *
  501. * HISTORY: *
  502. * 08/23/1996 JLB : Created. *
  503. *=============================================================================================*/
  504. inline COORDINATE Coord_Add(COORDINATE coord1, COORDINATE coord2)
  505. {
  506. COORD_COMPOSITE coord;
  507. coord.Sub.X.Raw = (LEPTON)((int)(short)((COORD_COMPOSITE &)coord1).Sub.X.Raw + (int)(short)((COORD_COMPOSITE &)coord2).Sub.X.Raw);
  508. coord.Sub.Y.Raw = (LEPTON)((int)(short)((COORD_COMPOSITE &)coord1).Sub.Y.Raw + (int)(short)((COORD_COMPOSITE &)coord2).Sub.Y.Raw);
  509. return(coord.Coord);
  510. }
  511. /***********************************************************************************************
  512. * Coord_Sub -- Subtracts one coordinate from another. *
  513. * *
  514. * This routine will subtract one coordinate from the other. The coordinates are broken *
  515. * up into their X and Y components, the subtraction is performed, and then they are *
  516. * recombined back into a coordinate to be returned. *
  517. * *
  518. * INPUT: coord1 -- The coordinate to be subtracted from. *
  519. * *
  520. * coord2 -- The coordinate to subtract. *
  521. * *
  522. * OUTPUT: Returns with the result of subtracting coord2 from coord1. *
  523. * *
  524. * WARNINGS: none *
  525. * *
  526. * HISTORY: *
  527. * 08/23/1996 JLB : Created. *
  528. *=============================================================================================*/
  529. inline COORDINATE Coord_Sub(COORDINATE coord1, COORDINATE coord2)
  530. {
  531. COORD_COMPOSITE coord;
  532. coord.Sub.X.Raw = (LEPTON)((int)(short)((COORD_COMPOSITE &)coord1).Sub.X.Raw - (int)(short)((COORD_COMPOSITE &)coord2).Sub.X.Raw);
  533. coord.Sub.Y.Raw = (LEPTON)((int)(short)((COORD_COMPOSITE &)coord1).Sub.Y.Raw - (int)(short)((COORD_COMPOSITE &)coord2).Sub.Y.Raw);
  534. return(coord.Coord);
  535. }
  536. /***********************************************************************************************
  537. * Coord_Mid -- Finds the midpoint between two coordinates. *
  538. * *
  539. * This will find the coordinate that is exactly between the two coordinates specified. *
  540. * *
  541. * INPUT: coord1 -- The first coordinate. *
  542. * *
  543. * coord2 -- The second coordinate. *
  544. * *
  545. * OUTPUT: Returns with the midpoint between the two coordinates. *
  546. * *
  547. * WARNINGS: none *
  548. * *
  549. * HISTORY: *
  550. * 08/23/1996 JLB : Created. *
  551. *=============================================================================================*/
  552. inline COORDINATE Coord_Mid(COORDINATE coord1, COORDINATE coord2)
  553. {
  554. COORD_COMPOSITE coord;
  555. coord.Sub.X.Raw = (LEPTON)(((int)((COORD_COMPOSITE &)coord1).Sub.X.Raw + (int)((COORD_COMPOSITE &)coord2).Sub.X.Raw) / 2);
  556. coord.Sub.Y.Raw = (LEPTON)(((int)((COORD_COMPOSITE &)coord1).Sub.Y.Raw + (int)((COORD_COMPOSITE &)coord2).Sub.Y.Raw) / 2);
  557. return(coord.Coord);
  558. }
  559. /***********************************************************************************************
  560. * Dir_Facing -- Convert a DirType into a FacingType value. *
  561. * *
  562. * Use this routine to convert the specified DirType value into the closest FacingType *
  563. * value that matches it. *
  564. * *
  565. * INPUT: facing -- The DirType to convert. *
  566. * *
  567. * OUTPUT: Returns with a FacingType value that most closely matches the DirType specified. *
  568. * *
  569. * WARNINGS: Precision of direction is lost by this transformation. *
  570. * *
  571. * HISTORY: *
  572. * 08/26/1996 JLB : Created. *
  573. *=============================================================================================*/
  574. inline FacingType Dir_Facing(DirType facing)
  575. {
  576. return (FacingType)(((unsigned char)((int)facing+0x10)&0xFF)>>5);
  577. }
  578. /***********************************************************************************************
  579. * Facing_Dir -- Convert a FacingType into a DirType. *
  580. * *
  581. * This will conver the specified FacingType value into the DirType that exactly matches *
  582. * it. *
  583. * *
  584. * INPUT: facing -- The FacingType to convert. *
  585. * *
  586. * OUTPUT: Returns with the DirType that exactly matches the facing. *
  587. * *
  588. * WARNINGS: none *
  589. * *
  590. * HISTORY: *
  591. * 08/26/1996 JLB : Created. *
  592. *=============================================================================================*/
  593. inline DirType Facing_Dir(FacingType facing)
  594. {
  595. return (DirType)((int)facing << 5);
  596. }
  597. /***********************************************************************************************
  598. * Dir_To_16 -- Convert a facing to a 0..15 value. *
  599. * *
  600. * Use this routine to convert a DirType into a 0 through 15 value. *
  601. * *
  602. * INPUT: facing -- The DirType to convert into a 0..15 value. *
  603. * *
  604. * OUTPUT: Returns with the facing converted into a value where 0 equals North, 4 equals *
  605. * East, 8 equals South, etc. *
  606. * *
  607. * WARNINGS: none *
  608. * *
  609. * HISTORY: *
  610. * 08/26/1996 JLB : Created. *
  611. *=============================================================================================*/
  612. inline int Dir_To_16(DirType facing)
  613. {
  614. return Facing16[facing];
  615. }
  616. /***********************************************************************************************
  617. * Dir_To_32 -- Convert a DirType into a 0..31 value. *
  618. * *
  619. * This will convert the DirType specified, into a 0 through 31 value where zero is North, *
  620. * and rotates clockwise. The return value is baised to take into consideration the *
  621. * distortion caused by 3D studio upon the game vehicle objects. *
  622. * *
  623. * INPUT: facing -- The DirType to convert. *
  624. * *
  625. * OUTPUT: Returns with the facing converted into a value from zero to 31. *
  626. * *
  627. * WARNINGS: none *
  628. * *
  629. * HISTORY: *
  630. * 08/26/1996 JLB : Created. *
  631. *=============================================================================================*/
  632. inline int Dir_To_32(DirType facing)
  633. {
  634. return Facing32[facing];
  635. }
  636. /***********************************************************************************************
  637. * Direction256 -- Calculate the facing value from one coordinate to another. *
  638. * *
  639. * This will calculate the facing from the first coordinate to the second. *
  640. * *
  641. * INPUT: coord1 -- The first coordinate that facing will be calculated from. *
  642. * *
  643. * coord2 -- The second coordinate that facing will be calcuated to. *
  644. * *
  645. * OUTPUT: Returns with the DirType that is the facing from coord1 to coord2. *
  646. * *
  647. * WARNINGS: none *
  648. * *
  649. * HISTORY: *
  650. * 08/26/1996 JLB : Created. *
  651. *=============================================================================================*/
  652. inline DirType Direction256(COORDINATE coord1, COORDINATE coord2)
  653. {
  654. return (Desired_Facing256(Coord_X(coord1), Coord_Y(coord1), Coord_X(coord2), Coord_Y(coord2)));
  655. }
  656. /***********************************************************************************************
  657. * Direction -- Determines the facing value from one coordinate to another. *
  658. * *
  659. * This will determine the DirType from the first coordinate to the second. *
  660. * *
  661. * INPUT: coord1 -- The first coordinate that facing will be calculated from. *
  662. * *
  663. * coord2 -- The second coordinate to calculate facing to. *
  664. * *
  665. * OUTPUT: Returns with the DirType that represents the facing from coordinate 1 to coordinate*
  666. * 2. *
  667. * *
  668. * WARNINGS: none *
  669. * *
  670. * HISTORY: *
  671. * 08/26/1996 JLB : Created. *
  672. *=============================================================================================*/
  673. inline DirType Direction(COORDINATE coord1, COORDINATE coord2)
  674. {
  675. return (Desired_Facing256(Coord_X(coord1), Coord_Y(coord1), Coord_X(coord2), Coord_Y(coord2)));
  676. }
  677. /***********************************************************************************************
  678. * Direction8 -- Fetches the direction from one coordinate to another. *
  679. * *
  680. * This will calculate the facing from the first coordinate to the second. The return value *
  681. * is of limited accuracy, but the calculation is fast. *
  682. * *
  683. * INPUT: coord1 -- The coordinate to calculate the facing from. *
  684. * *
  685. * coord2 -- The coordinate to figure the facing to. *
  686. * *
  687. * OUTPUT: Returns with the DirType to get from coordinate 1 to coordinate 2. *
  688. * *
  689. * WARNINGS: The return DirType is only accurate to the 8 primary compass rose directions. *
  690. * *
  691. * HISTORY: *
  692. * 08/26/1996 JLB : Created. *
  693. *=============================================================================================*/
  694. inline DirType Direction8(COORDINATE coord1, COORDINATE coord2)
  695. {
  696. return (Desired_Facing8(Coord_X(coord1), Coord_Y(coord1), Coord_X(coord2), Coord_Y(coord2)));
  697. }
  698. /***********************************************************************************************
  699. * Direction -- Calculates the DirType from one cell to another. *
  700. * *
  701. * This routine will calculate the facing to get from one cell to another. Since dealing *
  702. * with cells is much less precise than with coordinates, the return value is only *
  703. * accurate to 8 facings. *
  704. * *
  705. * INPUT: cell1 -- The cell to calculate the DirType from. *
  706. * *
  707. * cell2 -- The cell to calculate the DirType to. *
  708. * *
  709. * OUTPUT: Returns with the DirType to get from the first cell to the second. *
  710. * *
  711. * WARNINGS: The return value is only accurate to the 8 primary compass rose directions. *
  712. * *
  713. * HISTORY: *
  714. * 08/26/1996 JLB : Created. *
  715. *=============================================================================================*/
  716. inline DirType Direction(CELL cell1, CELL cell2)
  717. {
  718. return (Desired_Facing8(Cell_X(cell1), Cell_Y(cell1), Cell_X(cell2), Cell_Y(cell2)));
  719. }
  720. /***********************************************************************************************
  721. * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. *
  722. * *
  723. * This will coerce the coordinate specified so that it will refer to the immediately *
  724. * adjacent cell in the direction specified. *
  725. * *
  726. * INPUT: coord -- The coordinate to calculate the adjacency from. *
  727. * *
  728. * dir -- The direction to travel to calculate the adjacent cell. *
  729. * *
  730. * OUTPUT: Returns with the coordinate the refers to the adjacent cell in the direciton *
  731. * specified. *
  732. * *
  733. * WARNINGS: none *
  734. * *
  735. * HISTORY: *
  736. * 08/26/1996 JLB : Created. *
  737. *=============================================================================================*/
  738. inline COORDINATE Adjacent_Cell(COORDINATE coord, FacingType dir)
  739. {
  740. return (Coord_Snap(Coord_Add(AdjacentCoord[(int)dir & 0x07], coord)));
  741. }
  742. /***********************************************************************************************
  743. * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. *
  744. * *
  745. * This will coerce the coordinate specified so that it will refer to the immediately *
  746. * adjacent cell in the direction specified. *
  747. * *
  748. * INPUT: coord -- The coordinate to calculate the adjacency from. *
  749. * *
  750. * dir -- The direction to travel to calculate the adjacent cell. *
  751. * *
  752. * OUTPUT: Returns with the coordinate the refers to the adjacent cell in the direciton *
  753. * specified. *
  754. * *
  755. * WARNINGS: none *
  756. * *
  757. * HISTORY: *
  758. * 08/26/1996 JLB : Created. *
  759. *=============================================================================================*/
  760. inline COORDINATE Adjacent_Cell(COORDINATE coord, DirType dir)
  761. {
  762. return Adjacent_Cell(coord, Dir_Facing(dir));
  763. }
  764. /***********************************************************************************************
  765. * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. *
  766. * *
  767. * This routine will take the specified cell and coerce it to refer to the immediately *
  768. * adjacent cell in the direction specified. *
  769. * *
  770. * INPUT: cell -- The cell to coerce into an adjacent cell. *
  771. * *
  772. * dir -- The direction to determine the adjacent cell. *
  773. * *
  774. * OUTPUT: Returns with the cell value that represents the adjacent cell in the direction *
  775. * specified. *
  776. * *
  777. * WARNINGS: none *
  778. * *
  779. * HISTORY: *
  780. * 08/26/1996 JLB : Created. *
  781. *=============================================================================================*/
  782. inline CELL Adjacent_Cell(CELL cell, FacingType dir)
  783. {
  784. return (CELL)(cell + AdjacentCell[dir]);
  785. }
  786. /***********************************************************************************************
  787. * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. *
  788. * *
  789. * This routine will take the specified cell and coerce it to refer to the immediately *
  790. * adjacent cell in the direction specified. *
  791. * *
  792. * INPUT: cell -- The cell to coerce into an adjacent cell. *
  793. * *
  794. * dir -- The direction to determine the adjacent cell. *
  795. * *
  796. * OUTPUT: Returns with the cell value that represents the adjacent cell in the direction *
  797. * specified. *
  798. * *
  799. * WARNINGS: none *
  800. * *
  801. * HISTORY: *
  802. * 08/26/1996 JLB : Created. *
  803. *=============================================================================================*/
  804. inline CELL Adjacent_Cell(CELL cell, DirType dir)
  805. {
  806. return (CELL)(cell + AdjacentCell[Dir_Facing(dir)]);
  807. }
  808. /***********************************************************************************************
  809. * Dir_To_8 -- Convert a DirType into a value from 0 to 7. *
  810. * *
  811. * This routine will convert a DirType value into a facing number from 0 to 7. *
  812. * *
  813. * INPUT: facing -- The DirType to convert. *
  814. * *
  815. * OUTPUT: Returns with the DirType converted to a number from 0 to 7 with 0 being North and *
  816. * rotating clockwise. *
  817. * *
  818. * WARNINGS: none *
  819. * *
  820. * HISTORY: *
  821. * 08/26/1996 JLB : Created. *
  822. *=============================================================================================*/
  823. inline FacingType Dir_To_8(DirType facing)
  824. {
  825. return (FacingType)(((unsigned char)((int)facing|0x10))>>5);
  826. }
  827. /***********************************************************************************************
  828. * Text_String -- Convert a text number into a text pointer. *
  829. * *
  830. * This routine will convert text numbers (as generated elsewhere) into an actual text *
  831. * pointer that can be used for normal purposes. *
  832. * *
  833. * INPUT: string -- The text number to extract a pointer to. *
  834. * *
  835. * OUTPUT: Returns with a pointer to the text that represents the text number specified. *
  836. * *
  837. * WARNINGS: none *
  838. * *
  839. * HISTORY: *
  840. * 08/26/1996 JLB : Created. *
  841. *=============================================================================================*/
  842. inline char const * Text_String(int string)
  843. {
  844. #ifdef FIXIT_NAME_OVERRIDE
  845. if (string < 0 && abs(string) < ARRAY_SIZE(NameOverride)) {
  846. return(NameOverride[-(string+1)]);
  847. }
  848. #endif
  849. if (string < 1000) return(Extract_String(SystemStrings, string));
  850. return(Extract_String(DebugStrings, string-1000));
  851. }
  852. /***********************************************************************************************
  853. * Random_Pick -- Pick a random number in a specified range. *
  854. * *
  855. * This routine is used to pick a game influencing random number between (inclusive) the *
  856. * range specified. *
  857. * *
  858. * INPUT: a -- Low limit of range to pick from. *
  859. * *
  860. * b -- High limit of range to pick from. *
  861. * *
  862. * OUTPUT: Returns with a random number picked between (inclusive) the range of values *
  863. * specified. *
  864. * *
  865. * WARNINGS: none *
  866. * *
  867. * HISTORY: *
  868. * 09/30/1996 JLB : Created. *
  869. *=============================================================================================*/
  870. template<class T> inline T Random_Pick(T a, T b)
  871. {
  872. return T(Scen.RandomNumber((int)a, (int)b));
  873. };
  874. /***********************************************************************************************
  875. * Percent_Chance -- Calculate a percentage chance event. *
  876. * *
  877. * This will calculate a percentage chance and return with 'true' as likely as the *
  878. * chance value would occur (or less) on a random pick from 1 to 100. Thus a *
  879. * Percent_Chance(50) would return 'true' 50 percent of the time. Percent_Chance(25) would *
  880. * return 'true' 25% of the time, etc. *
  881. * *
  882. * INPUT: percent -- The percent value to calculate the chance upon. *
  883. * *
  884. * OUTPUT: Returns with 'true' in the same percentage as the percentage number supplied. *
  885. * *
  886. * WARNINGS: This affects the game syncronization random number generator and should be used *
  887. * for those events that could affect the game engine. *
  888. * *
  889. * HISTORY: *
  890. * 08/26/1996 JLB : Created. *
  891. *=============================================================================================*/
  892. inline bool Percent_Chance(int percent)
  893. {
  894. return (Scen.RandomNumber(0, 99) < percent);
  895. }
  896. /***********************************************************************************************
  897. * Sim_Random_Pick -- Picks a random number that will not affect the game. *
  898. * *
  899. * Use this routine to pick a random number such that it will be used so that it won't *
  900. * actually affect the outcome of the game. It is critical to use this routine for any *
  901. * random need that won't be needed on other machines in a multiplayer game. The result *
  902. * can be freely used as long as it doesn't affect the outcome of the game. *
  903. * *
  904. * INPUT: a -- Low range of the random number to pick. *
  905. * *
  906. * b -- High range of the random number to pick. *
  907. * *
  908. * OUTPUT: Returns with a random number between (inclusive) the range limit values *
  909. * specified. *
  910. * *
  911. * WARNINGS: none *
  912. * *
  913. * HISTORY: *
  914. * 09/30/1996 JLB : Created. *
  915. *=============================================================================================*/
  916. extern RandomClass NonCriticalRandomNumber;
  917. template<class T> inline T Sim_Random_Pick(T a, T b)
  918. {
  919. return T(NonCriticalRandomNumber((int)a, (int)b));
  920. };
  921. /***********************************************************************************************
  922. * Sim_Percent_Chance -- Calculates a percentage chance event for local events. *
  923. * *
  924. * This routine is similar to the normal Percent_Chance() routine except that it doesn't *
  925. * alter the main random number gerenator sequence. As such, this routine should be used *
  926. * for those events that should have a random character, but will either not affect the *
  927. * game engine or are only calculated on one machine in a multiplayer game. *
  928. * *
  929. * INPUT: percent -- The percent chance to calculate the possible return of 'true' on. *
  930. * *
  931. * OUTPUT: Returns 'true' with the same percentage chance as the percent number specified. *
  932. * A percent value of 50 means 50%, 25 means 25%, etc. *
  933. * *
  934. * WARNINGS: none *
  935. * *
  936. * HISTORY: *
  937. * 08/26/1996 JLB : Created. *
  938. *=============================================================================================*/
  939. inline bool Sim_Percent_Chance(int percent)
  940. {
  941. return (NonCriticalRandomNumber(0, 99) < percent);
  942. }
  943. /***********************************************************************************************
  944. * Distance -- Finds the distance between two arbitrary points. *
  945. * *
  946. * This finds the (Dragon Strike) distance between two arbitrary points in flat space. *
  947. * It does this by adding 1/2 the smaller absolute axis difference to the other absolute *
  948. * axis distance. The result is rough but quick to calculate. *
  949. * *
  950. * INPUT: x1,y1 -- Coordinate location for point 1. *
  951. * *
  952. * x2,y2 -- Coordinate location for point 2. *
  953. * *
  954. * OUTPUT: Returns with the rough distance between the two points. The value returned is *
  955. * expressed in the same units as the parameters were specified in. *
  956. * *
  957. * WARNINGS: none *
  958. * *
  959. * HISTORY: *
  960. * 09/30/1996 JLB : Created. *
  961. *=============================================================================================*/
  962. inline int Distance(int x1, int y1, int x2, int y2)
  963. {
  964. int diff1 = y1 - y2;
  965. if (diff1 < 0) diff1 = -diff1;
  966. int diff2 = x1 - x2;
  967. if (diff2 < 0) diff2 = -diff2;
  968. if (diff1 > diff2) {
  969. return(diff1 + ((unsigned)diff2 / 2));
  970. }
  971. return(diff2 + ((unsigned)diff1 / 2));
  972. }
  973. #endif