CountermeasuresBehavior.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: CountermeasuresBehavior.h /////////////////////////////////////////////////////////////////////////
  24. // Author: Kris Morness, April 2003
  25. // Desc: Handles countermeasure firing when under missile threat, and responsible
  26. // for diverting missiles to the flares.
  27. //------------------------------------------
  28. #pragma once
  29. #ifndef __COUNTERMEASURES_BEHAVIOR_H
  30. #define __COUNTERMEASURES_BEHAVIOR_H
  31. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  32. #include "GameClient/ParticleSys.h"
  33. #include "GameLogic/Module/BehaviorModule.h"
  34. #include "GameLogic/Module/UpgradeModule.h"
  35. #include "GameLogic/Module/UpdateModule.h"
  36. #include "GameLogic/Module/DamageModule.h"
  37. #include "Common/BitFlagsIO.h"
  38. class ParticleSystem;
  39. class ParticleSystemTemplate;
  40. //-------------------------------------------------------------------------------------------------
  41. class CountermeasuresBehaviorModuleData : public UpdateModuleData
  42. {
  43. public:
  44. UpgradeMuxData m_upgradeMuxData;
  45. AsciiString m_flareTemplateName;
  46. AsciiString m_flareBoneBaseName;
  47. Real m_evasionRate;
  48. UnsignedInt m_volleySize;
  49. Real m_volleyArcAngle;
  50. Real m_volleyVelocityFactor;
  51. UnsignedInt m_framesBetweenVolleys;
  52. UnsignedInt m_numberOfVolleys;
  53. UnsignedInt m_reloadFrames;
  54. UnsignedInt m_missileDecoyFrames;
  55. UnsignedInt m_countermeasureReactionFrames;
  56. Bool m_mustReloadAtAirfield;
  57. CountermeasuresBehaviorModuleData()
  58. {
  59. m_volleySize = 0;
  60. m_volleyArcAngle = 0.0f;
  61. m_framesBetweenVolleys = 0;
  62. m_numberOfVolleys = 0;
  63. m_reloadFrames = 0;
  64. m_evasionRate = 0.0f;
  65. m_mustReloadAtAirfield = FALSE;
  66. m_missileDecoyFrames = 0;
  67. m_volleyVelocityFactor = 1.0f;
  68. }
  69. static void buildFieldParse(MultiIniFieldParse& p)
  70. {
  71. static const FieldParse dataFieldParse[] =
  72. {
  73. { "FlareTemplateName", INI::parseAsciiString, NULL, offsetof( CountermeasuresBehaviorModuleData, m_flareTemplateName ) },
  74. { "FlareBoneBaseName", INI::parseAsciiString, NULL, offsetof( CountermeasuresBehaviorModuleData, m_flareBoneBaseName ) },
  75. { "VolleySize", INI::parseUnsignedInt, NULL, offsetof( CountermeasuresBehaviorModuleData, m_volleySize ) },
  76. { "VolleyArcAngle", INI::parseAngleReal, NULL, offsetof( CountermeasuresBehaviorModuleData, m_volleyArcAngle ) },
  77. { "VolleyVelocityFactor", INI::parseReal, NULL, offsetof( CountermeasuresBehaviorModuleData, m_volleyVelocityFactor ) },
  78. { "DelayBetweenVolleys", INI::parseDurationUnsignedInt, NULL, offsetof( CountermeasuresBehaviorModuleData, m_framesBetweenVolleys ) },
  79. { "NumberOfVolleys", INI::parseUnsignedInt, NULL, offsetof( CountermeasuresBehaviorModuleData, m_numberOfVolleys ) },
  80. { "ReloadTime", INI::parseDurationUnsignedInt, NULL, offsetof( CountermeasuresBehaviorModuleData, m_reloadFrames ) },
  81. { "EvasionRate", INI::parsePercentToReal, NULL, offsetof( CountermeasuresBehaviorModuleData, m_evasionRate ) },
  82. { "MustReloadAtAirfield", INI::parseBool, NULL, offsetof( CountermeasuresBehaviorModuleData, m_mustReloadAtAirfield ) },
  83. { "MissileDecoyDelay", INI::parseDurationUnsignedInt, NULL, offsetof( CountermeasuresBehaviorModuleData, m_missileDecoyFrames ) },
  84. { "ReactionLaunchLatency", INI::parseDurationUnsignedInt, NULL, offsetof( CountermeasuresBehaviorModuleData, m_countermeasureReactionFrames ) },
  85. { 0, 0, 0, 0 }
  86. };
  87. UpdateModuleData::buildFieldParse(p);
  88. p.add(dataFieldParse);
  89. p.add(UpgradeMuxData::getFieldParse(), offsetof( CountermeasuresBehaviorModuleData, m_upgradeMuxData ));
  90. }
  91. };
  92. // ------------------------------------------------------------------------------------------------
  93. // ------------------------------------------------------------------------------------------------
  94. class CountermeasuresBehaviorInterface
  95. {
  96. public:
  97. virtual void reportMissileForCountermeasures( Object *missile ) = 0;
  98. virtual ObjectID calculateCountermeasureToDivertTo( const Object& victim ) = 0;
  99. virtual void reloadCountermeasures() = 0;
  100. virtual Bool isActive() const = 0;
  101. };
  102. typedef std::vector<ObjectID> CountermeasuresVec;
  103. //-------------------------------------------------------------------------------------------------
  104. //-------------------------------------------------------------------------------------------------
  105. class CountermeasuresBehavior : public UpdateModule, public UpgradeMux, public CountermeasuresBehaviorInterface
  106. {
  107. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( CountermeasuresBehavior, "CountermeasuresBehavior" )
  108. MAKE_STANDARD_MODULE_MACRO_WITH_MODULE_DATA( CountermeasuresBehavior, CountermeasuresBehaviorModuleData )
  109. public:
  110. CountermeasuresBehavior( Thing *thing, const ModuleData* moduleData );
  111. // virtual destructor prototype provided by memory pool declaration
  112. // module methods
  113. static Int getInterfaceMask() { return UpdateModule::getInterfaceMask() | MODULEINTERFACE_UPGRADE; }
  114. // BehaviorModule
  115. virtual UpgradeModuleInterface* getUpgrade() { return this; }
  116. virtual CountermeasuresBehaviorInterface* getCountermeasuresBehaviorInterface() { return this; }
  117. virtual const CountermeasuresBehaviorInterface* getCountermeasuresBehaviorInterface() const { return this; }
  118. // UpdateModuleInterface
  119. virtual UpdateSleepTime update();
  120. virtual DisabledMaskType getDisabledTypesToProcess() const { return MAKE_DISABLED_MASK( DISABLED_HELD ); }
  121. // CountermeasuresBehaviorInterface
  122. virtual void reportMissileForCountermeasures( Object *missile );
  123. virtual ObjectID calculateCountermeasureToDivertTo( const Object& victim );
  124. virtual void reloadCountermeasures();
  125. virtual Bool isActive() const;
  126. protected:
  127. virtual void upgradeImplementation()
  128. {
  129. setWakeFrame(getObject(), UPDATE_SLEEP_NONE);
  130. }
  131. virtual void getUpgradeActivationMasks(UpgradeMaskType& activation, UpgradeMaskType& conflicting) const
  132. {
  133. getCountermeasuresBehaviorModuleData()->m_upgradeMuxData.getUpgradeActivationMasks(activation, conflicting);
  134. }
  135. virtual void performUpgradeFX()
  136. {
  137. getCountermeasuresBehaviorModuleData()->m_upgradeMuxData.performUpgradeFX(getObject());
  138. }
  139. virtual void processUpgradeRemoval()
  140. {
  141. // I can't take it any more. Let the record show that I think the UpgradeMux multiple inheritence is CRAP.
  142. getCountermeasuresBehaviorModuleData()->m_upgradeMuxData.muxDataProcessUpgradeRemoval(getObject());
  143. }
  144. virtual Bool requiresAllActivationUpgrades() const
  145. {
  146. return getCountermeasuresBehaviorModuleData()->m_upgradeMuxData.m_requiresAllTriggers;
  147. }
  148. inline Bool isUpgradeActive() const { return isAlreadyUpgraded(); }
  149. virtual Bool isSubObjectsUpgrade() { return false; }
  150. void launchVolley();
  151. private:
  152. CountermeasuresVec m_counterMeasures; //vector of countermeasures in the world.
  153. UnsignedInt m_availableCountermeasures; //number of countermeasures that can be launched to divert missiles.
  154. UnsignedInt m_activeCountermeasures; //number of countermeasures currently able to divert missiles.
  155. UnsignedInt m_divertedMissiles; //number of missiles that have been diverted to countermeasures.
  156. UnsignedInt m_incomingMissiles; //grand total of all missiles that were ever fired at me.
  157. UnsignedInt m_reactionFrame; //The frame countermeasures will be launched after initial hostile act.
  158. UnsignedInt m_nextVolleyFrame; //Frame the next volley is fired.
  159. UnsignedInt m_reloadFrame; //The frame countermeasures will be ready to use again.
  160. };
  161. #endif // __COUNTERMEASURES_BEHAVIOR_H