COMBAT.CPP 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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/COMBAT.CPP 1 3/03/97 10:24a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : COMBAT.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : September 19, 1994 *
  30. * *
  31. * Last Update : July 26, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Combat_Anim -- Determines explosion animation to play. *
  36. * Explosion_Damage -- Inflict an explosion damage affect. *
  37. * Modify_Damage -- Adjusts damage to reflect the nature of the target. *
  38. * Wide_Area_Damage -- Apply wide area damage to the map. *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "function.h"
  41. /***********************************************************************************************
  42. * Modify_Damage -- Adjusts damage to reflect the nature of the target. *
  43. * *
  44. * This routine is the core of combat tactics. It implements the *
  45. * affect various armor types have against various weapon types. By *
  46. * careful exploitation of this table, tactical advantage can be *
  47. * obtained. *
  48. * *
  49. * INPUT: damage -- The damage points to process. *
  50. * *
  51. * warhead -- The source of the damage points. *
  52. * *
  53. * armor -- The type of armor defending against the damage. *
  54. * *
  55. * distance -- The distance (in leptons) from the source of the damage. *
  56. * *
  57. * OUTPUT: Returns with the adjusted damage points to inflict upon the *
  58. * target. *
  59. * *
  60. * WARNINGS: none *
  61. * *
  62. * HISTORY: *
  63. * 04/16/1994 JLB : Created. *
  64. * 04/17/1994 JLB : Always does a minimum of damage. *
  65. * 01/01/1995 JLB : Takes into account distance from damage source. *
  66. * 04/11/1996 JLB : Changed damage fall-off formula for less damage fall-off. *
  67. *=============================================================================================*/
  68. int Modify_Damage(int damage, WarheadType warhead, ArmorType armor, int distance)
  69. {
  70. if (!damage) return(damage);
  71. /*
  72. ** If there is no raw damage value to start with, then
  73. ** there can be no modified damage either.
  74. */
  75. if (Special.IsInert || !damage || warhead == WARHEAD_NONE) return(0);
  76. /*
  77. ** Negative damage (i.e., heal) is always applied full strength, but only if the heal
  78. ** effect is close enough.
  79. */
  80. if (damage < 0) {
  81. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  82. if (distance < 0x008) {
  83. if(warhead != WARHEAD_MECHANICAL && armor == ARMOR_NONE) return(damage);
  84. if(warhead == WARHEAD_MECHANICAL && armor != ARMOR_NONE) return(damage);
  85. }
  86. #else
  87. if (distance < 0x008 && armor == ARMOR_NONE) return(damage);
  88. #endif
  89. return(0);
  90. }
  91. WarheadTypeClass const * whead = WarheadTypeClass::As_Pointer(warhead);
  92. // WarheadTypeClass const * whead = &Warheads[warhead];
  93. damage = damage * whead->Modifier[armor];
  94. /*
  95. ** Reduce damage according to the distance from the impact point.
  96. */
  97. if (damage) {
  98. if (!whead->SpreadFactor) {
  99. distance /= PIXEL_LEPTON_W/4;
  100. } else {
  101. distance /= whead->SpreadFactor * (PIXEL_LEPTON_W/2);
  102. }
  103. distance = Bound(distance, 0, 16);
  104. if (distance) {
  105. damage = damage / distance;
  106. }
  107. /*
  108. ** Allow damage to drop to zero only if the distance would have
  109. ** reduced damage to less than 1/4 full damage. Otherwise, ensure
  110. ** that at least one damage point is done.
  111. */
  112. if (distance < 4) {
  113. damage = max(damage, Rule.MinDamage);
  114. }
  115. }
  116. damage = min(damage, Rule.MaxDamage);
  117. return(damage);
  118. }
  119. /***********************************************************************************************
  120. * Explosion_Damage -- Inflict an explosion damage affect. *
  121. * *
  122. * Processes the collateral damage affects typically caused by an *
  123. * explosion. *
  124. * *
  125. * INPUT: coord -- The coordinate of ground zero. *
  126. * *
  127. * strength -- Raw damage points at ground zero. *
  128. * *
  129. * source -- Source of the explosion (who is responsible). *
  130. * *
  131. * warhead -- The kind of explosion to process. *
  132. * *
  133. * OUTPUT: none *
  134. * *
  135. * WARNINGS: This routine can consume some time and will affect the AI *
  136. * of nearby enemy units (possibly). *
  137. * *
  138. * HISTORY: *
  139. * 08/16/1991 JLB : Created. *
  140. * 11/30/1991 JLB : Uses coordinate system. *
  141. * 12/27/1991 JLB : Radius of explosion damage effect. *
  142. * 04/13/1994 JLB : Streamlined. *
  143. * 04/16/1994 JLB : Warhead damage type modifier. *
  144. * 04/17/1994 JLB : Cleaned up. *
  145. * 06/20/1994 JLB : Uses object pointers to distribute damage. *
  146. * 06/20/1994 JLB : Source is a pointer. *
  147. * 06/18/1996 JLB : Strength could be negative for healing effects. *
  148. *=============================================================================================*/
  149. void Explosion_Damage(COORDINATE coord, int strength, TechnoClass * source, WarheadType warhead)
  150. {
  151. CELL cell; // Cell number under explosion.
  152. ObjectClass * object; // Working object pointer.
  153. ObjectClass * objects[32]; // Maximum number of objects that can be damaged.
  154. int distance; // Distance to unit.
  155. int range; // Damage effect radius.
  156. int count; // Number of vehicle IDs in list.
  157. if (!strength || Special.IsInert || warhead == WARHEAD_NONE) return;
  158. WarheadTypeClass const * whead = WarheadTypeClass::As_Pointer(warhead);
  159. // WarheadTypeClass const * whead = &Warheads[warhead];
  160. // range = ICON_LEPTON_W*2;
  161. range = ICON_LEPTON_W + (ICON_LEPTON_W >> 1);
  162. cell = Coord_Cell(coord);
  163. if ((unsigned)cell >= MAP_CELL_TOTAL) return;
  164. CellClass * cellptr = &Map[cell];
  165. ObjectClass * impacto = cellptr->Cell_Occupier();
  166. /*
  167. ** Fill the list of unit IDs that will have damage
  168. ** assessed upon them. The units can be lifted from
  169. ** the cell data directly.
  170. */
  171. count = 0;
  172. for (FacingType i = FACING_NONE; i < FACING_COUNT; i++) {
  173. /*
  174. ** Fetch a pointer to the cell to examine. This is either
  175. ** an adjacent cell or the center cell. Damage never spills
  176. ** further than one cell away.
  177. */
  178. if (i != FACING_NONE) {
  179. cellptr = &Map[cell].Adjacent_Cell(i);
  180. }
  181. /*
  182. ** Add all objects in this cell to the list of objects to possibly apply
  183. ** damage to. The list stops building when the object pointer list becomes
  184. ** full. Do not include overlapping objects; selection state can affect
  185. ** the overlappers, and this causes multiplayer games to go out of sync.
  186. */
  187. object = cellptr->Cell_Occupier();
  188. while (object) {
  189. if (!object->IsToDamage && object != source) {
  190. object->IsToDamage = true;
  191. objects[count++] = object;
  192. if (count >= ARRAY_SIZE(objects)) break;
  193. }
  194. object = object->Next;
  195. }
  196. if (count >= ARRAY_SIZE(objects)) break;
  197. }
  198. /*
  199. ** Sweep through the units to be damaged and damage them. When damaging
  200. ** buildings, consider a hit on any cell the building occupies as if it
  201. ** were a direct hit on the building's center.
  202. */
  203. for (int index = 0; index < count; index++) {
  204. object = objects[index];
  205. object->IsToDamage = false;
  206. if (object->IsActive) {
  207. if (object->What_Am_I() == RTTI_BUILDING && impacto == object) {
  208. distance = 0;
  209. } else {
  210. distance = Distance(coord, object->Center_Coord());
  211. }
  212. if (object->IsDown && !object->IsInLimbo && distance < range) {
  213. int damage = strength;
  214. object->Take_Damage(damage, distance, warhead, source);
  215. }
  216. }
  217. }
  218. /*
  219. ** If there is a wall present at this location, it may be destroyed. Check to
  220. ** make sure that the warhead is of the kind that can destroy walls.
  221. */
  222. cellptr = &Map[cell];
  223. if (cellptr->Overlay != OVERLAY_NONE) {
  224. OverlayTypeClass const * optr = &OverlayTypeClass::As_Reference(cellptr->Overlay);
  225. if (optr->IsTiberium && whead->IsTiberiumDestroyer) {
  226. cellptr->Reduce_Tiberium(strength / 10);
  227. }
  228. if (optr->IsWall) {
  229. if (whead->IsWallDestroyer || (whead->IsWoodDestroyer && optr->IsWooden)) {
  230. Map[cell].Reduce_Wall(strength);
  231. }
  232. }
  233. }
  234. /*
  235. ** If there is a bridge at this location, then it may be destroyed by the
  236. ** combat damage.
  237. */
  238. if (cellptr->TType == TEMPLATE_BRIDGE1 || cellptr->TType == TEMPLATE_BRIDGE2 ||
  239. cellptr->TType == TEMPLATE_BRIDGE1H || cellptr->TType == TEMPLATE_BRIDGE2H ||
  240. cellptr->TType == TEMPLATE_BRIDGE_1A || cellptr->TType == TEMPLATE_BRIDGE_1B ||
  241. cellptr->TType == TEMPLATE_BRIDGE_2A || cellptr->TType == TEMPLATE_BRIDGE_2B ||
  242. cellptr->TType == TEMPLATE_BRIDGE_3A || cellptr->TType == TEMPLATE_BRIDGE_3B ) {
  243. if (((warhead == WARHEAD_AP || warhead == WARHEAD_HE) && Random_Pick(1, Rule.BridgeStrength) < strength)) {
  244. Map.Destroy_Bridge_At(cell);
  245. }
  246. }
  247. }
  248. /***********************************************************************************************
  249. * Combat_Anim -- Determines explosion animation to play. *
  250. * *
  251. * This routine is called when a projectile impacts. This routine will determine what *
  252. * animation should be played. *
  253. * *
  254. * INPUT: damage -- The amount of damage this warhead possess (warhead size). *
  255. * *
  256. * warhead -- The type of warhead. *
  257. * *
  258. * land -- The land type that this explosion is over. Sometimes, this makes *
  259. * a difference (especially over water). *
  260. * *
  261. * OUTPUT: Returns with the animation to play. If no animation is to be played, then *
  262. * ANIM_NONE is returned. *
  263. * *
  264. * WARNINGS: none *
  265. * *
  266. * HISTORY: *
  267. * 05/19/1996 JLB : Created. *
  268. *=============================================================================================*/
  269. AnimType Combat_Anim(int damage, WarheadType warhead, LandType land)
  270. {
  271. /*
  272. ** For cases of no damage or invalid warhead, don't have any
  273. ** animation effect at all.
  274. */
  275. if (damage == 0 || warhead == WARHEAD_NONE) {
  276. return(ANIM_NONE);
  277. }
  278. static AnimType _aplist[] = {
  279. ANIM_VEH_HIT3, // Small fragment throwing explosion -- burn/exp mix.
  280. ANIM_VEH_HIT2, // Small fragment throwing explosion -- pop & sparkles.
  281. ANIM_FRAG1, // Medium fragment throwing explosion -- short decay.
  282. ANIM_FBALL1, // Large fireball explosion (bulges rightward).
  283. };
  284. static AnimType _helist[] = {
  285. ANIM_VEH_HIT1, // Small fireball explosion (bulges rightward).
  286. ANIM_VEH_HIT2, // Small fragment throwing explosion -- pop & sparkles.
  287. ANIM_ART_EXP1, // Large fragment throwing explosion -- many sparkles.
  288. ANIM_FBALL1, // Large fireball explosion (bulges rightward).
  289. };
  290. static AnimType _firelist[] = {
  291. ANIM_NAPALM1, // Small napalm burn.
  292. ANIM_NAPALM2, // Medium napalm burn.
  293. ANIM_NAPALM3, // Large napalm burn.
  294. };
  295. static AnimType _waterlist[] = {
  296. ANIM_WATER_EXP3,
  297. ANIM_WATER_EXP2,
  298. ANIM_WATER_EXP1,
  299. };
  300. WarheadTypeClass const * wptr = WarheadTypeClass::As_Pointer(warhead);
  301. // WarheadTypeClass const * wptr = &Warheads[warhead];
  302. switch (wptr->ExplosionSet) {
  303. case 6:
  304. return(ANIM_ATOM_BLAST);
  305. case 2:
  306. if (damage > 15) {
  307. return(ANIM_PIFFPIFF);
  308. }
  309. return(ANIM_PIFF);
  310. case 4:
  311. if (land == LAND_NONE) return(ANIM_FLAK);
  312. // Fixed math error
  313. if (land == LAND_WATER) return(_waterlist[(ARRAY_SIZE(_waterlist)-1) * fixed(min(damage, 90), 90)]);
  314. return(_aplist[(ARRAY_SIZE(_aplist)-1) * fixed(min(damage, 90), 90)]);
  315. case 5:
  316. if (land == LAND_NONE) return(ANIM_FLAK);
  317. if (land == LAND_WATER) return(_waterlist[(ARRAY_SIZE(_waterlist)-1) * fixed(min(damage, 130), 130)]);
  318. return(_helist[(ARRAY_SIZE(_helist)-1) * fixed(min(damage, 130), 130)]);
  319. case 3:
  320. if (land == LAND_NONE) return(ANIM_FLAK);
  321. if (land == LAND_WATER) return(_waterlist[(ARRAY_SIZE(_waterlist)-1) * fixed(min(damage, 150), 150)]);
  322. return(_firelist[(ARRAY_SIZE(_firelist)-1) * fixed(min(damage, 150), 150)]);
  323. case 1:
  324. return(ANIM_PIFF);
  325. default:
  326. break;
  327. }
  328. return(ANIM_NONE);
  329. }
  330. /***********************************************************************************************
  331. * Wide_Area_Damage -- Apply wide area damage to the map. *
  332. * *
  333. * This routine will apply damage to a very wide area on the map. The damage will be *
  334. * spread out from the coordinate specified by the radius specified. The amount of damage *
  335. * will attenuate according to the distance from center. *
  336. * *
  337. * INPUT: coord -- The coordinate that the explosion damage will center about. *
  338. * *
  339. * radius -- The radius of the explosion. *
  340. * *
  341. * damage -- The amount of damage to apply at the center location. *
  342. * *
  343. * source -- Pointer to the purpetrator of the damage (if any). *
  344. * *
  345. * warhead -- The type of warhead that is causing the damage. *
  346. * *
  347. * OUTPUT: none *
  348. * *
  349. * WARNINGS: none *
  350. * *
  351. * HISTORY: *
  352. * 07/26/1996 JLB : Created. *
  353. *=============================================================================================*/
  354. void Wide_Area_Damage(COORDINATE coord, LEPTON radius, int rawdamage, TechnoClass * source, WarheadType warhead)
  355. {
  356. int cell_radius = (radius + CELL_LEPTON_W-1) / CELL_LEPTON_W;
  357. CELL cell = Coord_Cell(coord);
  358. for (int x = -cell_radius; x <= cell_radius; x++) {
  359. for (int y = -cell_radius; y <= cell_radius; y++) {
  360. int xpos = Cell_X(cell) + x;
  361. int ypos = Cell_Y(cell) + y;
  362. /*
  363. ** If the potential damage cell is outside of the map bounds,
  364. ** then don't process it. This unusual check method ensures that
  365. ** damage won't wrap from one side of the map to the other.
  366. */
  367. if ((unsigned)xpos > MAP_CELL_W) {
  368. continue;
  369. }
  370. if ((unsigned)ypos > MAP_CELL_H) {
  371. continue;
  372. }
  373. CELL tcell = XY_Cell(xpos, ypos);
  374. if (!Map.In_Radar(tcell)) continue;
  375. int dist_from_center = Distance(XY_Coord(x+cell_radius, y+cell_radius), XY_Coord(cell_radius, cell_radius));
  376. int damage = rawdamage * Inverse(fixed(cell_radius, dist_from_center));
  377. Explosion_Damage(Cell_Coord(tcell), damage, source, warhead);
  378. if (warhead == WARHEAD_FIRE && damage > 100) {
  379. new SmudgeClass(Random_Pick(SMUDGE_SCORCH1, SMUDGE_SCORCH6), Cell_Coord(tcell));
  380. }
  381. }
  382. }
  383. }