TARGET.CPP 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. ** Command & Conquer(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: F:\projects\c&c\vcs\code\target.cpv 2.17 16 Oct 1995 16:51:06 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 : TARGET.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : September 10, 1993 *
  30. * *
  31. * Last Update : August 27, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * As_Aircraft -- Converts the target value into an aircraft pointer. *
  36. * As_Animation -- Converts target value into animation pointer. *
  37. * As_Building -- Converts a target value into a building object pointer. *
  38. * As_Bullet -- Converts the target into a bullet pointer. *
  39. * As_Cell -- Converts a target value into a cell number. *
  40. * As_Coord -- Converts a target value into a coordinate value. *
  41. * As_Infantry -- If the target is infantry, return a pointer to it. *
  42. * As_Movement_Coord -- Fetches coordinate if trying to move to this target. *
  43. * As_Object -- Converts a target value into an object pointer. *
  44. * As_Team -- Converts a target number into a team pointer. *
  45. * As_TeamType -- Converts a target into a team type pointer. *
  46. * As_Techno -- Converts a target value into a TechnoClass pointer. *
  47. * As_Trigger -- Converts specified target into a trigger pointer. *
  48. * As_Unit -- Converts a target value into a unit pointer. *
  49. * Target_Legal -- Determines if the specified target is legal. *
  50. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  51. #include "function.h"
  52. #include "target.h"
  53. /***********************************************************************************************
  54. * As_Trigger -- Converts specified target into a trigger pointer. *
  55. * *
  56. * This routine will convert the specified target number into a trigger pointer. *
  57. * *
  58. * INPUT: target -- The target number to convert. *
  59. * *
  60. * OUTPUT: Returns with the trigger pointer that the specified target number represents. If *
  61. * it doesn't represent a legal trigger object, then NULL is returned. *
  62. * *
  63. * WARNINGS: none *
  64. * *
  65. * HISTORY: *
  66. * 07/08/1995 JLB : Created. *
  67. *=============================================================================================*/
  68. TriggerClass * As_Trigger(TARGET target)
  69. {
  70. return(Is_Target_Trigger(target) ? Triggers.Raw_Ptr(Target_Value(target)) : NULL);
  71. }
  72. /***********************************************************************************************
  73. * As_Team -- Converts a target number into a team pointer. *
  74. * *
  75. * This routine will convert the specified target number into a team pointer. *
  76. * *
  77. * INPUT: target -- The target number to convert. *
  78. * *
  79. * OUTPUT: Returns with the team object that the specified target number represents. If it *
  80. * doesn't represent a legal team then NULL is returned. *
  81. * *
  82. * WARNINGS: none *
  83. * *
  84. * HISTORY: *
  85. * 07/08/1995 JLB : Created. *
  86. *=============================================================================================*/
  87. TeamClass * As_Team(TARGET target)
  88. {
  89. return(Is_Target_Team(target) ? Teams.Raw_Ptr(Target_Value(target)) : NULL);
  90. }
  91. /***********************************************************************************************
  92. * As_TeamType -- Converts a target into a team type pointer. *
  93. * *
  94. * This routine will convert the specified target number into a team type pointer. *
  95. * *
  96. * INPUT: target -- The target number to convert. *
  97. * *
  98. * OUTPUT: Returns with a pointer to the team type represented by the target number. If the *
  99. * target number doesn't represent a legal team type, then NULL is returned. *
  100. * *
  101. * WARNINGS: none *
  102. * *
  103. * HISTORY: *
  104. * 07/08/1995 JLB : Created. *
  105. *=============================================================================================*/
  106. TeamTypeClass * As_TeamType(TARGET target)
  107. {
  108. return(Is_Target_TeamType(target) ? TeamTypes.Raw_Ptr(Target_Value(target)) : NULL);
  109. }
  110. /***********************************************************************************************
  111. * As_Animation -- Converts target value into animation pointer. *
  112. * *
  113. * This routine will convert the specified target number into an animation pointer. *
  114. * *
  115. * INPUT: target -- The target number to convert into an animation pointer. *
  116. * *
  117. * OUTPUT: Returns with a pointer to the legal animation that this target represents. If it *
  118. * doesn't represent a legal animation, then NULL is returned. *
  119. * *
  120. * WARNINGS: none *
  121. * *
  122. * HISTORY: *
  123. * 07/08/1995 JLB : Created. *
  124. *=============================================================================================*/
  125. AnimClass * As_Animation(TARGET target)
  126. {
  127. return(Is_Target_Animation(target) ? Anims.Raw_Ptr(Target_Value(target)) : NULL);
  128. }
  129. /***********************************************************************************************
  130. * As_Bullet -- Converts the target into a bullet pointer. *
  131. * *
  132. * This routine will convert the specified target number into a bullet pointer. *
  133. * *
  134. * INPUT: target -- The target number to convert. *
  135. * *
  136. * OUTPUT: Returns with a pointer to the bullet it specifies. If the target doesn't refer to *
  137. * a legal bullet, then NULL is returned. *
  138. * *
  139. * WARNINGS: none *
  140. * *
  141. * HISTORY: *
  142. * 07/08/1995 JLB : Created. *
  143. *=============================================================================================*/
  144. BulletClass * As_Bullet(TARGET target)
  145. {
  146. return(Is_Target_Bullet(target) ? Bullets.Raw_Ptr(Target_Value(target)) : NULL);
  147. }
  148. /***********************************************************************************************
  149. * As_Aircraft -- Converts the target value into an aircraft pointer. *
  150. * *
  151. * This routine will convert the specified target value into an aircraft object pointer. *
  152. * *
  153. * INPUT: target -- The target value to convert. *
  154. * *
  155. * OUTPUT: Returns with a pointer to the aircraft that this target value represents. If the *
  156. * specified target value doesn't represent an aircraft, then NULL is returned. *
  157. * *
  158. * WARNINGS: none *
  159. * *
  160. * HISTORY: *
  161. * 08/27/1995 JLB : Created. *
  162. *=============================================================================================*/
  163. AircraftClass * As_Aircraft(TARGET target)
  164. {
  165. return(Is_Target_Aircraft(target) ? Aircraft.Raw_Ptr(Target_Value(target)) : NULL);
  166. }
  167. /***********************************************************************************************
  168. * As_Techno -- Converts a target value into a TechnoClass pointer. *
  169. * *
  170. * This routine will take the target value specified and convert it into a TechnoClass *
  171. * pointer if the target represents an object that has a TechnoClass. *
  172. * *
  173. * INPUT: target -- The target value to convert into a TechnoClass pointer. *
  174. * *
  175. * OUTPUT: Returns with a pointer to the associated object's TechnoClass. If the target *
  176. * cannot be converted into a TechnoClass pointer, then NULL is returned. *
  177. * *
  178. * WARNINGS: none *
  179. * *
  180. * HISTORY: *
  181. * 06/02/1994 JLB : Created. *
  182. *=============================================================================================*/
  183. TechnoClass * As_Techno(TARGET target)
  184. {
  185. ObjectClass * obj = As_Object(target);
  186. if (obj && obj->Is_Techno()) {
  187. return(TechnoClass *)obj;
  188. }
  189. return(NULL);
  190. }
  191. /***********************************************************************************************
  192. * As_Object -- Converts a target value into an object pointer. *
  193. * *
  194. * This routine is used to convert the target value specified into an object pointer. If *
  195. * the target doesn't represent an object or the target value is illegal, then NULL is *
  196. * returned. *
  197. * *
  198. * INPUT: target -- The target value to convert from. *
  199. * *
  200. * OUTPUT: Returns with a pointer to the object it represent, or NULL if not an object. *
  201. * *
  202. * WARNINGS: none *
  203. * *
  204. * HISTORY: *
  205. * 05/27/1994 JLB : Created. *
  206. *=============================================================================================*/
  207. ObjectClass * As_Object(TARGET target)
  208. {
  209. int val = Target_Value(target);
  210. ObjectClass * object = NULL;
  211. switch (Target_Kind(target)) {
  212. case KIND_INFANTRY:
  213. object = Infantry.Raw_Ptr(val);
  214. break;
  215. case KIND_UNIT:
  216. object = Units.Raw_Ptr(val);
  217. break;
  218. case KIND_BUILDING:
  219. object = Buildings.Raw_Ptr(val);
  220. break;
  221. case KIND_AIRCRAFT:
  222. object = Aircraft.Raw_Ptr(val);
  223. break;
  224. case KIND_TERRAIN:
  225. object = Terrains.Raw_Ptr(val);
  226. break;
  227. case KIND_BULLET:
  228. object = Bullets.Raw_Ptr(val);
  229. break;
  230. case KIND_ANIMATION:
  231. object = Anims.Raw_Ptr(val);
  232. break;
  233. default:
  234. break;
  235. }
  236. #if (0)
  237. /*
  238. ** Special check to ensure that a target value that references an
  239. ** invalid object will not be converted back into an object pointer.
  240. ** This condition is rare, but could occur in a network game if the
  241. ** object it refers to is destroyed between the time an event message
  242. ** is sent and when it is received.
  243. */
  244. if (object != NULL && !object->IsActive) {
  245. object = NULL;
  246. }
  247. #endif //(0)
  248. return(object);
  249. }
  250. /***********************************************************************************************
  251. * As_Unit -- Converts a target value into a unit pointer. *
  252. * *
  253. * This routine is used to convert the target value specified into a pointer to a unit *
  254. * object. *
  255. * *
  256. * INPUT: target -- The target value to convert into a unit pointer. *
  257. * *
  258. * OUTPUT: Returns with a pointer to the unit the target value represents or NULL if not *
  259. * a unit. *
  260. * *
  261. * WARNINGS: none *
  262. * *
  263. * HISTORY: *
  264. * 05/27/1994 JLB : Created. *
  265. *=============================================================================================*/
  266. UnitClass * As_Unit(TARGET target)
  267. {
  268. return(Is_Target_Unit(target) ? Units.Raw_Ptr(Target_Value(target)) : NULL);
  269. }
  270. /***********************************************************************************************
  271. * As_Infantry -- If the target is infantry, return a pointer to it. *
  272. * *
  273. * This routine will translate the specified target value into an infantry pointer if the *
  274. * target actually represents an infantry object. *
  275. * *
  276. * INPUT: target -- The target to convert to a pointer. *
  277. * *
  278. * OUTPUT: Returns a pointer to the infantry object that this target value represents. If *
  279. * the target doesn't represent an infantry object, then return NULL. *
  280. * *
  281. * WARNINGS: none *
  282. * *
  283. * HISTORY: *
  284. * 10/17/1994 JLB : Created. *
  285. *=============================================================================================*/
  286. InfantryClass * As_Infantry(TARGET target)
  287. {
  288. return(Is_Target_Infantry(target) ? Infantry.Raw_Ptr(Target_Value(target)) : NULL);
  289. }
  290. #ifdef NEVER
  291. TerrainClass * As_Terrain(TARGET target)
  292. {
  293. return(Is_Target_Terrain(target) ? &Terrains[Target_Value(target)] : NULL);
  294. }
  295. #endif
  296. /***********************************************************************************************
  297. * As_Building -- Converts a target value into a building object pointer. *
  298. * *
  299. * This routine is used to convert the target value specified into a building pointer. *
  300. * *
  301. * INPUT: target -- The target value to convert from. *
  302. * *
  303. * OUTPUT: Returns with a pointer to the building object that the target value represents. *
  304. * If it doesn't represent a building, then return NULL. *
  305. * *
  306. * WARNINGS: none *
  307. * *
  308. * HISTORY: *
  309. * 05/27/1994 JLB : Created. *
  310. *=============================================================================================*/
  311. BuildingClass * As_Building(TARGET target)
  312. {
  313. return(Is_Target_Building(target) ? Buildings.Raw_Ptr(Target_Value(target)) : NULL);
  314. }
  315. #ifdef NEVER
  316. /***********************************************************************************************
  317. * Target_Legal -- Determines if the specified target is legal. *
  318. * *
  319. * This routine is used to check for the legality of the target value specified. It is *
  320. * necessary to call this routine if there is doubt about the the legality of the target. *
  321. * It is possible for the unit that a target value represents to be eliminated and thus *
  322. * rendering the target value invalid. *
  323. * *
  324. * INPUT: target -- The target value to check. *
  325. * *
  326. * OUTPUT: bool; Is the target value legal? *
  327. * *
  328. * WARNINGS: none *
  329. * *
  330. * HISTORY: *
  331. * 05/27/1994 JLB : Created. *
  332. *=============================================================================================*/
  333. bool Target_Legal(TARGET target)
  334. {
  335. if (!Target_Legal(target)) return(false);
  336. ObjectClass * obj = As_Object(target);
  337. if (obj) {
  338. return(obj->Get_Strength() > 0 && obj->IsActive && !obj->IsInLimbo && obj->Class_Of().IsLegalTarget);
  339. }
  340. return(true);
  341. }
  342. #endif
  343. /***********************************************************************************************
  344. * As_Cell -- Converts a target value into a cell number. *
  345. * *
  346. * This routine is used to convert the target value specified, into a cell value. This is *
  347. * necessary for find path and other procedures that need a cell value. *
  348. * *
  349. * INPUT: target -- The target value to convert to a cell value. *
  350. * *
  351. * OUTPUT: Returns with the target value expressed as a cell location. *
  352. * *
  353. * WARNINGS: none *
  354. * *
  355. * HISTORY: *
  356. * 05/27/1994 JLB : Created. *
  357. *=============================================================================================*/
  358. CELL As_Cell(TARGET target)
  359. {
  360. return(Coord_Cell(As_Coord(target)));
  361. }
  362. /***********************************************************************************************
  363. * As_Coord -- Converts a target value into a coordinate value. *
  364. * *
  365. * This routine is used to convert the target value specified into a coordinate value. It *
  366. * is necessary for those procedures that require a coordinate value. *
  367. * *
  368. * INPUT: target -- The target value to convert. *
  369. * *
  370. * OUTPUT: Returns with the target expressed as a COORD value. *
  371. * *
  372. * WARNINGS: none *
  373. * *
  374. * HISTORY: *
  375. * 05/27/1994 JLB : Created. *
  376. * 11/16/1994 JLB : Simplified. *
  377. *=============================================================================================*/
  378. COORDINATE As_Coord(TARGET target)
  379. {
  380. if (Target_Legal(target)) {
  381. /*
  382. ** Cell target values are handled as a special case. The value of the target number is
  383. ** actually the cell index number.
  384. */
  385. if (Is_Target_Cell(target)) {
  386. return(Cell_Coord((CELL)Target_Value(target)));
  387. }
  388. /*
  389. ** Normal targets correspond to game objects. Fetch the object pointer and then ask it
  390. ** for the center coordinate. Return the center coordinate as the target's coordinate.
  391. */
  392. ObjectClass * obj = As_Object(target);
  393. if (obj) {
  394. /*
  395. ** If this is invalid memory or the object is dead then return 0
  396. ** This is a kludge to fix the problem of team target objects being assigned after
  397. ** the object is already destroyed - 1/15/97 3:13PM
  398. */
  399. if (IsBadReadPtr ((void*)obj, sizeof (ObjectClass) ) || !obj->IsActive){
  400. //OutputDebugString ("C&C95 - As_Coord called for invalid target object\m");
  401. return(0x00000000L);
  402. }
  403. return(obj->Target_Coord());
  404. }
  405. }
  406. /*
  407. ** An unrecognized target value results in a null coordinate value.
  408. */
  409. return(0x00000000L);
  410. }
  411. /***********************************************************************************************
  412. * As_Movement_Coord -- Fetches coordinate if trying to move to this target. *
  413. * *
  414. * This routine will convert the specified target into a coordinate location. This location *
  415. * is used when moving to the target specified. For cells, this is the center of the cell. *
  416. * For special buildings that allow docking, it is the center location of the docking *
  417. * bay. *
  418. * *
  419. * INPUT: target -- The target to convert into a coordinate value. *
  420. * *
  421. * OUTPUT: Returns with the docking coordinate of the target value specified. *
  422. * *
  423. * WARNINGS: none *
  424. * *
  425. * HISTORY: *
  426. * 08/27/1995 JLB : Created. *
  427. *=============================================================================================*/
  428. COORDINATE As_Movement_Coord(TARGET target)
  429. {
  430. if (Target_Legal(target)) {
  431. /*
  432. ** Cell target values are handled as a special case. The value of the target number is
  433. ** actually the cell index number.
  434. */
  435. if (Is_Target_Cell(target)) {
  436. return(Cell_Coord((CELL)Target_Value(target)));
  437. }
  438. /*
  439. ** Normal targets correspond to game objects. Fetch the object pointer and then ask it
  440. ** for the center coordinate. Return the center coordinate as the target's coordinate.
  441. */
  442. ObjectClass * obj = As_Object(target);
  443. if (obj) {
  444. return(obj->Docking_Coord());
  445. }
  446. }
  447. /*
  448. ** An unrecognized target value results in a null coordinate value.
  449. */
  450. return(0x00000000L);
  451. }