COMBAT.CPP 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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: F:\projects\c&c\vcs\code\combat.cpv 2.17 16 Oct 1995 16:48:32 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 : COMBAT.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : September 19, 1994 *
  26. * *
  27. * Last Update : January 1, 1995 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * Explosion_Damage -- Inflict an explosion damage affect. *
  32. * Modify_Damage -- Adjusts damage to reflect the nature of the target. *
  33. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  34. #include "function.h"
  35. int Modify_Damage(int damage, WarheadType warhead, ArmorType armor);
  36. void Explosion_Damage(COORDINATE coord, unsigned strength, TechnoClass *source, WarheadType warhead);
  37. /***********************************************************************************************
  38. * Modify_Damage -- Adjusts damage to reflect the nature of the target. *
  39. * *
  40. * This routine is the core of combat tactics. It implements the *
  41. * affect various armor types have against various weapon types. By *
  42. * careful exploitation of this table, tactical advantage can be *
  43. * obtained. *
  44. * *
  45. * INPUT: damage -- The damage points to process. *
  46. * *
  47. * warhead -- The source of the damage points. *
  48. * *
  49. * armor -- The type of armor defending against the damage. *
  50. * *
  51. * distance -- The distance (in leptons) from the source of the damage. *
  52. * *
  53. * OUTPUT: Returns with the adjusted damage points to inflict upon the *
  54. * target. *
  55. * *
  56. * WARNINGS: none *
  57. * *
  58. * HISTORY: *
  59. * 04/16/1994 JLB : Created. *
  60. * 04/17/1994 JLB : Always does a minimum of damage. *
  61. * 01/01/1995 JLB : Takes into account distance from damage source. *
  62. *=============================================================================================*/
  63. int Modify_Damage(int damage, WarheadType warhead, ArmorType armor, int distance)
  64. {
  65. /*
  66. ** If there is no raw damage value to start with, then
  67. ** there can be no modified damage either.
  68. */
  69. if (Special.IsInert || !damage || warhead == WARHEAD_NONE) return(0);
  70. WarheadTypeClass const * whead = &Warheads[warhead];
  71. damage = Fixed_To_Cardinal(damage, whead->Modifier[armor]);
  72. /*
  73. ** Reduce damage according to the distance from the impact point.
  74. */
  75. if (damage) {
  76. // if (distance < 0x0010) damage *= 2; // Double damage for direct hits.
  77. distance >>= whead->SpreadFactor;
  78. distance = Bound(distance, 0, 16);
  79. damage >>= distance;
  80. }
  81. /*
  82. ** If damage was indicated, then it should never drop below one damage point regardless
  83. ** of modifiers. This allows a very weak attacker to eventually destroy anything it
  84. ** fires upon, given enough time.
  85. */
  86. return(damage);
  87. }
  88. /***********************************************************************************************
  89. * Explosion_Damage -- Inflict an explosion damage affect. *
  90. * *
  91. * Processes the collateral damage affects typically caused by an *
  92. * explosion. *
  93. * *
  94. * INPUT: coord -- The coordinate of ground zero. *
  95. * *
  96. * strength -- Raw damage points at ground zero. *
  97. * *
  98. * source -- Source of the explosion (who is responsible). *
  99. * *
  100. * warhead -- The kind of explosion to process. *
  101. * *
  102. * OUTPUT: none *
  103. * *
  104. * WARNINGS: This routine can consume some time and will affect the AI *
  105. * of nearby enemy units (possibly). *
  106. * *
  107. * HISTORY: *
  108. * 08/16/1991 JLB : Created. *
  109. * 11/30/1991 JLB : Uses coordinate system. *
  110. * 12/27/1991 JLB : Radius of explosion damage effect. *
  111. * 04/13/1994 JLB : Streamlined. *
  112. * 04/16/1994 JLB : Warhead damage type modifier. *
  113. * 04/17/1994 JLB : Cleaned up. *
  114. * 06/20/1994 JLB : Uses object pointers to distribute damage. *
  115. * 06/20/1994 JLB : Source is a pointer. *
  116. *=============================================================================================*/
  117. void Explosion_Damage(COORDINATE coord, unsigned strength, TechnoClass * source, WarheadType warhead)
  118. {
  119. CELL cell; // Cell number under explosion.
  120. ObjectClass * object; // Working object pointer.
  121. ObjectClass * objects[32]; // Maximum number of objects that can be damaged.
  122. int distance; // Distance to unit.
  123. int range; // Damage effect radius.
  124. int index;
  125. int count; // Number of vehicle IDs in list.
  126. if (!strength || Special.IsInert || warhead == WARHEAD_NONE) return;
  127. WarheadTypeClass const * whead = &Warheads[warhead];
  128. range = ICON_LEPTON_W + (ICON_LEPTON_W >> 1);
  129. cell = Coord_Cell(coord);
  130. if ((unsigned)cell >= MAP_CELL_TOTAL) return;
  131. // if (!Map.In_Radar(cell)) return;
  132. CellClass * cellptr = &Map[cell];
  133. ObjectClass * impacto = cellptr->Cell_Occupier();
  134. /*
  135. ** Fill the list of unit IDs that will have damage
  136. ** assessed upon them. The units can be lifted from
  137. ** the cell data directly.
  138. */
  139. count = 0;
  140. for (FacingType i = FACING_NONE; i < FACING_COUNT; i++) {
  141. /*
  142. ** Fetch a pointer to the cell to examine. This is either
  143. ** an adjacent cell or the center cell. Damage never spills
  144. ** further than one cell away.
  145. */
  146. if (i != FACING_NONE) {
  147. cellptr = Map[cell].Adjacent_Cell(i);
  148. if (!cellptr) continue;
  149. }
  150. /*
  151. ** Add all objects in this cell to the list of objects to possibly apply
  152. ** damage to. The list stops building when the object pointer list becomes
  153. ** full. Do not include overlapping objects; selection state can affect
  154. ** the overlappers, and this causes multiplayer games to go out of sync.
  155. */
  156. object = cellptr->Cell_Occupier();
  157. while (object) {
  158. if (!object->IsToDamage && object != source) {
  159. object->IsToDamage = true;
  160. objects[count++] = object;
  161. if (count >= (sizeof(objects)/sizeof(objects[0]))) break;
  162. }
  163. object = object->Next;
  164. }
  165. if (count >= (sizeof(objects)/sizeof(objects[0]))) break;
  166. }
  167. /*
  168. ** Sweep through the units to be damaged and damage them. When damaging
  169. ** buildings, consider a hit on any cell the building occupies as if it
  170. ** were a direct hit on the building's center.
  171. */
  172. for (index = 0; index < count; index++) {
  173. object = objects[index];
  174. object->IsToDamage = false;
  175. if (object->What_Am_I() == RTTI_BUILDING && impacto == object) {
  176. distance = 0;
  177. } else {
  178. distance = Distance(coord, object->Center_Coord());
  179. }
  180. if (object->IsDown && !object->IsInLimbo && distance < range) {
  181. int damage = strength;
  182. /*
  183. ** High explosive does double damage against aircraft.
  184. */
  185. if (warhead == WARHEAD_HE && object->What_Am_I() == RTTI_AIRCRAFT) {
  186. damage *= 2;
  187. }
  188. /*
  189. ** Apply the damage to the object.
  190. */
  191. if (damage) {
  192. object->Take_Damage(damage, distance, warhead, source);
  193. }
  194. }
  195. }
  196. /*
  197. ** If there is a wall present at this location, it may be destroyed. Check to
  198. ** make sure that the warhead is of the kind that can destroy walls.
  199. */
  200. cellptr = &Map[cell];
  201. cellptr->Reduce_Tiberium(strength / 10);
  202. if (cellptr->Overlay != OVERLAY_NONE) {
  203. OverlayTypeClass const * optr = &OverlayTypeClass::As_Reference(cellptr->Overlay);
  204. if (optr->IsWall) {
  205. if (whead->IsWallDestroyer || (whead->IsWoodDestroyer && optr->IsWooden)) {
  206. Map[cell].Reduce_Wall(strength);
  207. }
  208. }
  209. }
  210. }