FiringTracker.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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: FiringTracker.cpp //////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, February 2002
  25. // Desc: Keeps track of shots fired and people targeted for weapons that want a history of such a thing
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #include "Common/AudioHandleSpecialValues.h"
  29. #include "Common/GameType.h"
  30. #include "Common/GameAudio.h"
  31. #include "Common/PerfTimer.h"
  32. #include "Common/ThingTemplate.h"
  33. #include "Common/Xfer.h"
  34. #include "GameLogic/FiringTracker.h"
  35. #include "GameLogic/GameLogic.h"
  36. #include "GameLogic/Module/ObjectHelper.h"
  37. #include "GameLogic/Object.h"
  38. #include "GameLogic/Weapon.h"
  39. #ifdef _INTERNAL
  40. // for occasional debugging...
  41. //#pragma optimize("", off)
  42. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  43. #endif
  44. //-------------------------------------------------------------------------------------------------
  45. FiringTracker::FiringTracker(Thing* thing, const ModuleData *modData) : UpdateModule( thing, modData )
  46. {
  47. m_consecutiveShots = 0;
  48. m_victimID = INVALID_ID;
  49. m_frameToStartCooldown = 0;
  50. m_frameToForceReload = 0;
  51. m_frameToStopLoopingSound = 0;
  52. m_audioHandle = AHSV_NoSound;
  53. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  54. }
  55. //-------------------------------------------------------------------------------------------------
  56. FiringTracker::~FiringTracker()
  57. {
  58. // no need to protect this.
  59. TheAudio->removeAudioEvent( m_audioHandle );
  60. m_audioHandle = AHSV_NoSound;
  61. }
  62. //-------------------------------------------------------------------------------------------------
  63. Int FiringTracker::getNumConsecutiveShotsAtVictim( const Object *victim ) const
  64. {
  65. if( victim == NULL )
  66. return 0;// safety, this function is for asking about shots at a victim
  67. if( victim->getID() != m_victimID )
  68. return 0;// nope, not shooting at him
  69. return m_consecutiveShots;// this is how any times I have shot at this hoser right now
  70. }
  71. //-------------------------------------------------------------------------------------------------
  72. void FiringTracker::shotFired(const Weapon* weaponFired, ObjectID victimID)
  73. {
  74. UnsignedInt now = TheGameLogic->getFrame();
  75. Object *me = getObject();
  76. const Object *victim = TheGameLogic->findObjectByID(victimID); // May be null for ground shot
  77. if( victim && victim->testStatus(OBJECT_STATUS_FAERIE_FIRE) )
  78. {
  79. if( !me->testWeaponBonusCondition(WEAPONBONUSCONDITION_TARGET_FAERIE_FIRE) )
  80. {
  81. // We shoot faster at guys marked thusly
  82. me->setWeaponBonusCondition(WEAPONBONUSCONDITION_TARGET_FAERIE_FIRE);
  83. }
  84. }
  85. else
  86. {
  87. // A ground shot or the lack of the status on the target will clear this
  88. if( me->testWeaponBonusCondition(WEAPONBONUSCONDITION_TARGET_FAERIE_FIRE) )
  89. {
  90. me->clearWeaponBonusCondition(WEAPONBONUSCONDITION_TARGET_FAERIE_FIRE);
  91. }
  92. }
  93. if( victimID == m_victimID )
  94. {
  95. // Shooting at the same guy
  96. ++m_consecutiveShots;
  97. }
  98. else if( now < m_frameToStartCooldown )
  99. {
  100. // Switching targets within the coast time is valid, and we will not spin down
  101. ++m_consecutiveShots;
  102. m_victimID = victimID;
  103. }
  104. else
  105. {
  106. // Start the count over for the new guy
  107. m_consecutiveShots = 1;
  108. m_victimID = victimID;
  109. }
  110. // Push back the time that we should force a reload with each shot
  111. UnsignedInt autoReloadDelay = weaponFired->getAutoReloadWhenIdleFrames();
  112. if( autoReloadDelay > 0 )
  113. m_frameToForceReload = now + autoReloadDelay;
  114. UnsignedInt coast = weaponFired->getContinuousFireCoastFrames();
  115. if (coast)
  116. m_frameToStartCooldown = weaponFired->getPossibleNextShotFrame() + coast;
  117. else
  118. m_frameToStartCooldown = 0;
  119. Int shotsNeededOne = weaponFired->getContinuousFireOneShotsNeeded();
  120. Int shotsNeededTwo = weaponFired->getContinuousFireTwoShotsNeeded();
  121. if( me->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN ) )
  122. {
  123. // Can either go up or down from here.
  124. if( m_consecutiveShots < shotsNeededOne )
  125. coolDown();
  126. else if( m_consecutiveShots > shotsNeededTwo )
  127. speedUp();
  128. }
  129. else if( me->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST ) )
  130. {
  131. // Only place I can go here from here is all the way down
  132. if( m_consecutiveShots < shotsNeededTwo )
  133. {
  134. coolDown();
  135. }
  136. }
  137. else
  138. {
  139. // Check to go up
  140. if( m_consecutiveShots > shotsNeededOne )
  141. speedUp();
  142. }
  143. UnsignedInt fireSoundLoopTime = weaponFired->getFireSoundLoopTime();
  144. if (fireSoundLoopTime != 0)
  145. {
  146. // If the sound has stopped playing, then we need to re-add it.
  147. if (m_frameToStopLoopingSound == 0 || !TheAudio->isCurrentlyPlaying(m_audioHandle))
  148. {
  149. AudioEventRTS audio = weaponFired->getFireSound();
  150. audio.setObjectID(getObject()->getID());
  151. m_audioHandle = TheAudio->addAudioEvent( &audio );
  152. }
  153. m_frameToStopLoopingSound = now + fireSoundLoopTime;
  154. }
  155. else
  156. {
  157. AudioEventRTS fireAndForgetSound = weaponFired->getFireSound();
  158. fireAndForgetSound.setObjectID(getObject()->getID());
  159. TheAudio->addAudioEvent(&fireAndForgetSound);
  160. m_frameToStopLoopingSound = 0;
  161. }
  162. setWakeFrame(me, calcTimeToSleep());
  163. }
  164. //-------------------------------------------------------------------------------------------------
  165. UpdateSleepTime FiringTracker::update()
  166. {
  167. //DEBUG_ASSERTCRASH(m_frameToStartCooldown != 0 || m_frameToStopLoopingSound != 0, ("hmm, should be asleep"));
  168. UnsignedInt now = TheGameLogic->getFrame();
  169. // I have been idle long enough that I should reload, so I do not hang around with a near empty clip forever.
  170. if( m_frameToForceReload != 0 && now >= m_frameToForceReload )
  171. {
  172. getObject()->reloadAllAmmo(TRUE);
  173. m_frameToForceReload = 0;
  174. }
  175. // If it has been too long since I fired. I have to start over.
  176. // (don't call if we don't need to cool down... it's expensive!)
  177. if (m_frameToStopLoopingSound != 0)
  178. {
  179. if (now >= m_frameToStopLoopingSound)
  180. {
  181. TheAudio->removeAudioEvent( m_audioHandle );
  182. m_audioHandle = AHSV_NoSound;
  183. m_frameToStopLoopingSound = 0;
  184. }
  185. }
  186. if( m_frameToStartCooldown != 0 && now > m_frameToStartCooldown )
  187. {
  188. m_frameToStartCooldown = now + LOGICFRAMES_PER_SECOND;
  189. coolDown();// if this is the coolest call to cooldown, it will set m_frameToStartCooldown to zero
  190. return UPDATE_SLEEP(LOGICFRAMES_PER_SECOND);
  191. }
  192. UpdateSleepTime sleepTime = calcTimeToSleep();
  193. return sleepTime;
  194. }
  195. //-------------------------------------------------------------------------------------------------
  196. UpdateSleepTime FiringTracker::calcTimeToSleep()
  197. {
  198. // Figure out the longest amount of time we can sleep as unneeded
  199. // If all the timers are off, then we aren't needed at all
  200. if (m_frameToStopLoopingSound == 0 && m_frameToStartCooldown == 0 && m_frameToForceReload == 0)
  201. return UPDATE_SLEEP_FOREVER;
  202. // Otherwise, we need to wake up to service the shortest timer
  203. UnsignedInt now = TheGameLogic->getFrame();
  204. UnsignedInt sleepTime = UPDATE_SLEEP_FOREVER;
  205. if( m_frameToStopLoopingSound != 0 )
  206. {
  207. if( m_frameToStopLoopingSound <= now )
  208. sleepTime = UPDATE_SLEEP_NONE;
  209. else if( (m_frameToStopLoopingSound - now) < sleepTime )
  210. sleepTime = m_frameToStopLoopingSound - now;
  211. }
  212. if( m_frameToStartCooldown != 0 )
  213. {
  214. if( m_frameToStartCooldown <= now )
  215. sleepTime = UPDATE_SLEEP_NONE;
  216. else if( (m_frameToStartCooldown - now) < sleepTime )
  217. sleepTime = m_frameToStartCooldown - now;
  218. }
  219. if( m_frameToForceReload != 0 )
  220. {
  221. if( m_frameToForceReload <= now )
  222. sleepTime = UPDATE_SLEEP_NONE;
  223. else if( (m_frameToForceReload - now) < sleepTime )
  224. sleepTime = m_frameToForceReload - now;
  225. }
  226. return UPDATE_SLEEP(sleepTime);
  227. }
  228. //-------------------------------------------------------------------------------------------------
  229. void FiringTracker::speedUp()
  230. {
  231. ModelConditionFlags clr, set;
  232. Object *self = getObject();
  233. if( self->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST ) )
  234. {
  235. //self->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN );
  236. //self->clearModelConditionState( MODELCONDITION_CONTINUOUS_FIRE_MEAN );
  237. //self->clearModelConditionState( MODELCONDITION_CONTINUOUS_FIRE_SLOW );
  238. }
  239. else if(self->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN ) )
  240. {
  241. const AudioEventRTS *soundToPlayPtr = self->getTemplate()->getPerUnitSound( "VoiceRapidFire" );
  242. AudioEventRTS soundToPlay = *soundToPlayPtr;
  243. soundToPlay.setObjectID( self->getID() );
  244. TheAudio->addAudioEvent( &soundToPlay );
  245. // These flags are exclusive, not cumulative
  246. self->setWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST );
  247. set.set(MODELCONDITION_CONTINUOUS_FIRE_FAST);
  248. // These flags are exclusive, not cumulative
  249. self->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN );
  250. clr.set(MODELCONDITION_CONTINUOUS_FIRE_MEAN);
  251. clr.set(MODELCONDITION_CONTINUOUS_FIRE_SLOW);
  252. }
  253. else
  254. {
  255. self->setWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN );
  256. set.set(MODELCONDITION_CONTINUOUS_FIRE_MEAN);
  257. // These flags are exclusive, not cumulative
  258. self->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST );
  259. clr.set(MODELCONDITION_CONTINUOUS_FIRE_FAST);
  260. clr.set(MODELCONDITION_CONTINUOUS_FIRE_SLOW);
  261. }
  262. self->clearAndSetModelConditionFlags(clr, set);
  263. }
  264. //-------------------------------------------------------------------------------------------------
  265. void FiringTracker::coolDown()
  266. {
  267. ModelConditionFlags clr, set;
  268. if( getObject()->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST )
  269. || getObject()->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN ))
  270. {
  271. // Straight to zero from wherever it is
  272. set.set(MODELCONDITION_CONTINUOUS_FIRE_SLOW);
  273. getObject()->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST );
  274. getObject()->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN );
  275. clr.set(MODELCONDITION_CONTINUOUS_FIRE_FAST);
  276. clr.set(MODELCONDITION_CONTINUOUS_FIRE_MEAN);
  277. }
  278. else // we stop, now
  279. {
  280. getObject()->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST );
  281. getObject()->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN );
  282. // Just chillin, nothing to change
  283. clr.set(MODELCONDITION_CONTINUOUS_FIRE_FAST);
  284. clr.set(MODELCONDITION_CONTINUOUS_FIRE_MEAN);
  285. clr.set(MODELCONDITION_CONTINUOUS_FIRE_SLOW);
  286. m_frameToStartCooldown = 0;
  287. }
  288. getObject()->clearAndSetModelConditionFlags(clr, set);
  289. // Start everything over
  290. m_consecutiveShots = 0;
  291. m_victimID = INVALID_ID;
  292. }
  293. // ------------------------------------------------------------------------------------------------
  294. /** CRC */
  295. // ------------------------------------------------------------------------------------------------
  296. void FiringTracker::crc( Xfer *xfer )
  297. {
  298. // object helper base class
  299. UpdateModule::crc( xfer );
  300. } // end crc
  301. // ------------------------------------------------------------------------------------------------
  302. /** Xfer method
  303. * Version Info:
  304. * 1: Initial version */
  305. // ------------------------------------------------------------------------------------------------
  306. void FiringTracker::xfer( Xfer *xfer )
  307. {
  308. // version
  309. XferVersion currentVersion = 1;
  310. XferVersion version = currentVersion;
  311. xfer->xferVersion( &version, currentVersion );
  312. // object helper base class
  313. UpdateModule::xfer( xfer );
  314. // consecutive shots
  315. xfer->xferInt( &m_consecutiveShots );
  316. // victim id
  317. xfer->xferObjectID( &m_victimID );
  318. // frame to start cooldown
  319. xfer->xferUnsignedInt( &m_frameToStartCooldown );
  320. } // end xfer
  321. // ------------------------------------------------------------------------------------------------
  322. /** Load post process */
  323. // ------------------------------------------------------------------------------------------------
  324. void FiringTracker::loadPostProcess( void )
  325. {
  326. // object helper back class
  327. UpdateModule::loadPostProcess();
  328. } // end loadPostProcess