FireWeaponWhenDamagedBehavior.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: FireWeaponWhenDamagedBehavior.cpp ///////////////////////////////////////////////////////////////////////
  24. // Author:
  25. // Desc:
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #define DEFINE_SLOWDEATHPHASE_NAMES
  30. #include "Common/Thing.h"
  31. #include "Common/ThingTemplate.h"
  32. #include "Common/INI.h"
  33. #include "Common/RandomValue.h"
  34. #include "Common/Xfer.h"
  35. #include "GameClient/Drawable.h"
  36. #include "GameClient/FXList.h"
  37. #include "GameClient/InGameUI.h"
  38. #include "GameLogic/GameLogic.h"
  39. #include "GameLogic/Module/BodyModule.h"
  40. #include "GameLogic/Module/FireWeaponWhenDamagedBehavior.h"
  41. #include "GameLogic/Module/AIUpdate.h"
  42. #include "GameLogic/Object.h"
  43. #include "GameLogic/ObjectCreationList.h"
  44. #include "GameLogic/Weapon.h"
  45. #include "GameClient/Drawable.h"
  46. const Int MAX_IDX = 32;
  47. const Real BEGIN_MIDPOINT_RATIO = 0.35f;
  48. const Real END_MIDPOINT_RATIO = 0.65f;
  49. //-------------------------------------------------------------------------------------------------
  50. //-------------------------------------------------------------------------------------------------
  51. FireWeaponWhenDamagedBehavior::FireWeaponWhenDamagedBehavior( Thing *thing, const ModuleData* moduleData ) :
  52. UpdateModule( thing, moduleData ),
  53. m_reactionWeaponPristine( NULL ),
  54. m_reactionWeaponDamaged( NULL ),
  55. m_reactionWeaponReallyDamaged( NULL ),
  56. m_reactionWeaponRubble( NULL ),
  57. m_continuousWeaponPristine( NULL ),
  58. m_continuousWeaponDamaged( NULL ),
  59. m_continuousWeaponReallyDamaged( NULL ),
  60. m_continuousWeaponRubble( NULL )
  61. {
  62. const FireWeaponWhenDamagedBehaviorModuleData *d = getFireWeaponWhenDamagedBehaviorModuleData();
  63. const Object* obj = getObject();
  64. if ( d->m_reactionWeaponPristine )
  65. {
  66. m_reactionWeaponPristine = TheWeaponStore->allocateNewWeapon(
  67. d->m_reactionWeaponPristine, PRIMARY_WEAPON);
  68. m_reactionWeaponPristine->reloadAmmo( obj );
  69. }
  70. if ( d->m_reactionWeaponDamaged )
  71. {
  72. m_reactionWeaponDamaged = TheWeaponStore->allocateNewWeapon(
  73. d->m_reactionWeaponDamaged, PRIMARY_WEAPON);
  74. m_reactionWeaponDamaged->reloadAmmo( obj );
  75. }
  76. if ( d->m_reactionWeaponReallyDamaged )
  77. {
  78. m_reactionWeaponReallyDamaged = TheWeaponStore->allocateNewWeapon(
  79. d->m_reactionWeaponReallyDamaged, PRIMARY_WEAPON);
  80. m_reactionWeaponReallyDamaged->reloadAmmo( obj );
  81. }
  82. if ( d->m_reactionWeaponRubble )
  83. {
  84. m_reactionWeaponRubble = TheWeaponStore->allocateNewWeapon(
  85. d->m_reactionWeaponRubble, PRIMARY_WEAPON);
  86. m_reactionWeaponRubble->reloadAmmo( obj );
  87. }
  88. if ( d->m_continuousWeaponPristine )
  89. {
  90. m_continuousWeaponPristine = TheWeaponStore->allocateNewWeapon(
  91. d->m_continuousWeaponPristine, PRIMARY_WEAPON);
  92. m_continuousWeaponPristine->reloadAmmo( obj );
  93. }
  94. if ( d->m_continuousWeaponDamaged )
  95. {
  96. m_continuousWeaponDamaged = TheWeaponStore->allocateNewWeapon(
  97. d->m_continuousWeaponDamaged, PRIMARY_WEAPON);
  98. m_continuousWeaponDamaged->reloadAmmo( obj );
  99. }
  100. if ( d->m_continuousWeaponReallyDamaged )
  101. {
  102. m_continuousWeaponReallyDamaged = TheWeaponStore->allocateNewWeapon(
  103. d->m_continuousWeaponReallyDamaged, PRIMARY_WEAPON);
  104. m_continuousWeaponReallyDamaged->reloadAmmo( obj );
  105. }
  106. if ( d->m_continuousWeaponRubble )
  107. {
  108. m_continuousWeaponRubble = TheWeaponStore->allocateNewWeapon(
  109. d->m_continuousWeaponRubble, PRIMARY_WEAPON);
  110. m_continuousWeaponRubble->reloadAmmo( obj );
  111. }
  112. if (d->m_initiallyActive)
  113. {
  114. giveSelfUpgrade();
  115. }
  116. if (isUpgradeActive() &&
  117. (d->m_continuousWeaponPristine != NULL ||
  118. d->m_continuousWeaponDamaged != NULL ||
  119. d->m_continuousWeaponReallyDamaged != NULL ||
  120. d->m_continuousWeaponRubble != NULL))
  121. {
  122. setWakeFrame(getObject(), UPDATE_SLEEP_NONE);
  123. }
  124. else
  125. {
  126. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  127. }
  128. }
  129. //-------------------------------------------------------------------------------------------------
  130. //-------------------------------------------------------------------------------------------------
  131. FireWeaponWhenDamagedBehavior::~FireWeaponWhenDamagedBehavior( void )
  132. {
  133. if (m_reactionWeaponPristine)
  134. m_reactionWeaponPristine->deleteInstance();
  135. if (m_reactionWeaponDamaged)
  136. m_reactionWeaponDamaged->deleteInstance();
  137. if (m_reactionWeaponReallyDamaged)
  138. m_reactionWeaponReallyDamaged->deleteInstance();
  139. if (m_reactionWeaponRubble)
  140. m_reactionWeaponRubble->deleteInstance();
  141. if (m_continuousWeaponPristine)
  142. m_continuousWeaponPristine->deleteInstance();
  143. if (m_continuousWeaponDamaged)
  144. m_continuousWeaponDamaged->deleteInstance();
  145. if (m_continuousWeaponReallyDamaged)
  146. m_continuousWeaponReallyDamaged->deleteInstance();
  147. if (m_continuousWeaponRubble)
  148. m_continuousWeaponRubble->deleteInstance();
  149. }
  150. //-------------------------------------------------------------------------------------------------
  151. /** Damage has been dealt, this is an opportunity to reach to that damage */
  152. //-------------------------------------------------------------------------------------------------
  153. void FireWeaponWhenDamagedBehavior::onDamage( DamageInfo *damageInfo )
  154. {
  155. if (!isUpgradeActive())
  156. return;
  157. const FireWeaponWhenDamagedBehaviorModuleData* d = getFireWeaponWhenDamagedBehaviorModuleData();
  158. // right type?
  159. if (!getDamageTypeFlag(d->m_damageTypes, damageInfo->in.m_damageType))
  160. return;
  161. // right amount? (use actual [post-armor] damage dealt)
  162. if (damageInfo->out.m_actualDamageDealt < d->m_damageAmount)
  163. return;
  164. const Object *obj = getObject();
  165. BodyDamageType bdt = obj->getBodyModule()->getDamageState();
  166. if ( bdt == BODY_RUBBLE )
  167. {
  168. if( m_reactionWeaponRubble && m_reactionWeaponRubble->getStatus() == READY_TO_FIRE )
  169. {
  170. m_reactionWeaponRubble->forceFireWeapon( obj, obj->getPosition() );
  171. }
  172. }
  173. else if ( bdt == BODY_REALLYDAMAGED )
  174. {
  175. if( m_reactionWeaponReallyDamaged && m_reactionWeaponReallyDamaged->getStatus() == READY_TO_FIRE )
  176. {
  177. m_reactionWeaponReallyDamaged->forceFireWeapon( obj, obj->getPosition() );
  178. }
  179. }
  180. else if ( bdt == BODY_DAMAGED )
  181. {
  182. if( m_reactionWeaponDamaged && m_reactionWeaponDamaged->getStatus() == READY_TO_FIRE )
  183. {
  184. m_reactionWeaponDamaged->forceFireWeapon( obj, obj->getPosition() );
  185. }
  186. }
  187. else // not damaged yet
  188. {
  189. if( m_reactionWeaponPristine && m_reactionWeaponPristine->getStatus() == READY_TO_FIRE )
  190. {
  191. m_reactionWeaponPristine->forceFireWeapon( obj, obj->getPosition() );
  192. }
  193. }
  194. }
  195. //-------------------------------------------------------------------------------------------------
  196. /** if object fires weapon constantly, figure out which one and do it */
  197. //-------------------------------------------------------------------------------------------------
  198. UpdateSleepTime FireWeaponWhenDamagedBehavior::update( void )
  199. {
  200. if (!isUpgradeActive())
  201. {
  202. DEBUG_ASSERTCRASH(isUpgradeActive(), ("hmm, this should not be possible"));
  203. return UPDATE_SLEEP_FOREVER;
  204. }
  205. const Object *obj = getObject();
  206. BodyDamageType bdt = obj->getBodyModule()->getDamageState();
  207. if ( bdt == BODY_RUBBLE )
  208. {
  209. if( m_continuousWeaponRubble && m_continuousWeaponRubble->getStatus() == READY_TO_FIRE )
  210. {
  211. m_continuousWeaponRubble->forceFireWeapon( obj, obj->getPosition() );
  212. }
  213. }
  214. else if ( bdt == BODY_REALLYDAMAGED )
  215. {
  216. if( m_continuousWeaponReallyDamaged && m_continuousWeaponReallyDamaged->getStatus() == READY_TO_FIRE )
  217. {
  218. m_continuousWeaponReallyDamaged->forceFireWeapon( obj, obj->getPosition() );
  219. }
  220. }
  221. else if ( bdt == BODY_DAMAGED )
  222. {
  223. if( m_continuousWeaponDamaged && m_continuousWeaponDamaged->getStatus() == READY_TO_FIRE )
  224. {
  225. m_continuousWeaponDamaged->forceFireWeapon( obj, obj->getPosition() );
  226. }
  227. }
  228. else // not damaged yet
  229. {
  230. if( m_continuousWeaponPristine && m_continuousWeaponPristine->getStatus() == READY_TO_FIRE )
  231. {
  232. m_continuousWeaponPristine->forceFireWeapon( obj, obj->getPosition() );
  233. }
  234. }
  235. return UPDATE_SLEEP_NONE;
  236. }
  237. // ------------------------------------------------------------------------------------------------
  238. /** CRC */
  239. // ------------------------------------------------------------------------------------------------
  240. void FireWeaponWhenDamagedBehavior::crc( Xfer *xfer )
  241. {
  242. // extend base class
  243. UpdateModule::crc( xfer );
  244. // extend upgrade mux
  245. UpgradeMux::upgradeMuxCRC( xfer );
  246. } // end crc
  247. // ------------------------------------------------------------------------------------------------
  248. /** Xfer method
  249. * Version Info:
  250. * 1: Initial version */
  251. // ------------------------------------------------------------------------------------------------
  252. void FireWeaponWhenDamagedBehavior::xfer( Xfer *xfer )
  253. {
  254. // version
  255. XferVersion currentVersion = 1;
  256. XferVersion version = currentVersion;
  257. xfer->xferVersion( &version, currentVersion );
  258. // extend base class
  259. UpdateModule::xfer( xfer );
  260. // extend upgrade mux
  261. UpgradeMux::upgradeMuxXfer( xfer );
  262. Bool weaponPresent;
  263. // reaction pristine
  264. weaponPresent = m_reactionWeaponPristine ? TRUE : FALSE;
  265. xfer->xferBool( &weaponPresent );
  266. if( weaponPresent )
  267. xfer->xferSnapshot( m_reactionWeaponPristine );
  268. // reaction damaged
  269. weaponPresent = m_reactionWeaponDamaged ? TRUE : FALSE;
  270. xfer->xferBool( &weaponPresent );
  271. if( weaponPresent )
  272. xfer->xferSnapshot( m_reactionWeaponDamaged );
  273. // reaction really damaged
  274. weaponPresent = m_reactionWeaponReallyDamaged ? TRUE : FALSE;
  275. xfer->xferBool( &weaponPresent );
  276. if( weaponPresent )
  277. xfer->xferSnapshot( m_reactionWeaponReallyDamaged );
  278. // reaction rubble
  279. weaponPresent = m_reactionWeaponRubble ? TRUE : FALSE;
  280. xfer->xferBool( &weaponPresent );
  281. if( weaponPresent )
  282. xfer->xferSnapshot( m_reactionWeaponRubble );
  283. // continuous pristine
  284. weaponPresent = m_continuousWeaponPristine ? TRUE : FALSE;
  285. xfer->xferBool( &weaponPresent );
  286. if( weaponPresent )
  287. xfer->xferSnapshot( m_continuousWeaponPristine );
  288. // continuous damaged
  289. weaponPresent = m_continuousWeaponDamaged ? TRUE : FALSE;
  290. xfer->xferBool( &weaponPresent );
  291. if( weaponPresent )
  292. xfer->xferSnapshot( m_continuousWeaponDamaged );
  293. // continuous really damaged
  294. weaponPresent = m_continuousWeaponReallyDamaged ? TRUE : FALSE;
  295. xfer->xferBool( &weaponPresent );
  296. if( weaponPresent )
  297. xfer->xferSnapshot( m_continuousWeaponReallyDamaged );
  298. // continuous rubble
  299. weaponPresent = m_continuousWeaponRubble ? TRUE : FALSE;
  300. xfer->xferBool( &weaponPresent );
  301. if( weaponPresent )
  302. xfer->xferSnapshot( m_continuousWeaponRubble );
  303. } // end xfer
  304. // ------------------------------------------------------------------------------------------------
  305. /** Load post process */
  306. // ------------------------------------------------------------------------------------------------
  307. void FireWeaponWhenDamagedBehavior::loadPostProcess( void )
  308. {
  309. // extend base class
  310. UpdateModule::loadPostProcess();
  311. // extend upgrade mux
  312. UpgradeMux::upgradeMuxLoadPostProcess();
  313. } // end loadPostProcess