INLINE.H 77 KB

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