FiringTracker.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. ** Command & Conquer Generals(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. if( victimID == m_victimID )
  76. {
  77. // Shooting at the same guy
  78. ++m_consecutiveShots;
  79. }
  80. else if( now < m_frameToStartCooldown )
  81. {
  82. // Switching targets within the coast time is valid, and we will not spin down
  83. ++m_consecutiveShots;
  84. m_victimID = victimID;
  85. }
  86. else
  87. {
  88. // Start the count over for the new guy
  89. m_consecutiveShots = 1;
  90. m_victimID = victimID;
  91. }
  92. // Push back the time that we should force a reload with each shot
  93. UnsignedInt autoReloadDelay = weaponFired->getAutoReloadWhenIdleFrames();
  94. if( autoReloadDelay > 0 )
  95. m_frameToForceReload = now + autoReloadDelay;
  96. UnsignedInt coast = weaponFired->getContinuousFireCoastFrames();
  97. if (coast)
  98. m_frameToStartCooldown = weaponFired->getPossibleNextShotFrame() + coast;
  99. else
  100. m_frameToStartCooldown = 0;
  101. Int shotsNeededOne = weaponFired->getContinuousFireOneShotsNeeded();
  102. Int shotsNeededTwo = weaponFired->getContinuousFireTwoShotsNeeded();
  103. if( getObject()->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN ) )
  104. {
  105. // Can either go up or down from here.
  106. if( m_consecutiveShots < shotsNeededOne )
  107. coolDown();
  108. else if( m_consecutiveShots > shotsNeededTwo )
  109. speedUp();
  110. }
  111. else if( getObject()->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST ) )
  112. {
  113. // Only place I can go here from here is all the way down
  114. if( m_consecutiveShots < shotsNeededTwo )
  115. {
  116. coolDown();
  117. }
  118. }
  119. else
  120. {
  121. // Check to go up
  122. if( m_consecutiveShots > shotsNeededOne )
  123. speedUp();
  124. }
  125. UnsignedInt fireSoundLoopTime = weaponFired->getFireSoundLoopTime();
  126. if (fireSoundLoopTime != 0)
  127. {
  128. // If the sound has stopped playing, then we need to re-add it.
  129. if (m_frameToStopLoopingSound == 0 || !TheAudio->isCurrentlyPlaying(m_audioHandle))
  130. {
  131. AudioEventRTS audio = weaponFired->getFireSound();
  132. audio.setObjectID(getObject()->getID());
  133. m_audioHandle = TheAudio->addAudioEvent( &audio );
  134. }
  135. m_frameToStopLoopingSound = now + fireSoundLoopTime;
  136. }
  137. else
  138. {
  139. AudioEventRTS fireAndForgetSound = weaponFired->getFireSound();
  140. fireAndForgetSound.setObjectID(getObject()->getID());
  141. TheAudio->addAudioEvent(&fireAndForgetSound);
  142. m_frameToStopLoopingSound = 0;
  143. }
  144. setWakeFrame(getObject(), calcTimeToSleep());
  145. }
  146. //-------------------------------------------------------------------------------------------------
  147. UpdateSleepTime FiringTracker::update()
  148. {
  149. //DEBUG_ASSERTCRASH(m_frameToStartCooldown != 0 || m_frameToStopLoopingSound != 0, ("hmm, should be asleep"));
  150. UnsignedInt now = TheGameLogic->getFrame();
  151. // I have been idle long enough that I should reload, so I do not hang around with a near empty clip forever.
  152. if( m_frameToForceReload != 0 && now >= m_frameToForceReload )
  153. {
  154. getObject()->reloadAllAmmo(TRUE);
  155. m_frameToForceReload = 0;
  156. }
  157. // If it has been too long since I fired. I have to start over.
  158. // (don't call if we don't need to cool down... it's expensive!)
  159. if (m_frameToStopLoopingSound != 0)
  160. {
  161. if (now >= m_frameToStopLoopingSound)
  162. {
  163. TheAudio->removeAudioEvent( m_audioHandle );
  164. m_audioHandle = AHSV_NoSound;
  165. m_frameToStopLoopingSound = 0;
  166. }
  167. }
  168. if( m_frameToStartCooldown != 0 && now > m_frameToStartCooldown )
  169. {
  170. m_frameToStartCooldown = now + LOGICFRAMES_PER_SECOND;
  171. coolDown();// if this is the coolest call to cooldown, it will set m_frameToStartCooldown to zero
  172. return UPDATE_SLEEP(LOGICFRAMES_PER_SECOND);
  173. }
  174. UpdateSleepTime sleepTime = calcTimeToSleep();
  175. return sleepTime;
  176. }
  177. //-------------------------------------------------------------------------------------------------
  178. UpdateSleepTime FiringTracker::calcTimeToSleep()
  179. {
  180. // Figure out the longest amount of time we can sleep as unneeded
  181. // If all the timers are off, then we aren't needed at all
  182. if (m_frameToStopLoopingSound == 0 && m_frameToStartCooldown == 0 && m_frameToForceReload == 0)
  183. return UPDATE_SLEEP_FOREVER;
  184. // Otherwise, we need to wake up to service the shortest timer
  185. UnsignedInt now = TheGameLogic->getFrame();
  186. UnsignedInt sleepTime = UPDATE_SLEEP_FOREVER;
  187. if( m_frameToStopLoopingSound != 0 )
  188. {
  189. if( m_frameToStopLoopingSound <= now )
  190. sleepTime = UPDATE_SLEEP_NONE;
  191. else if( (m_frameToStopLoopingSound - now) < sleepTime )
  192. sleepTime = m_frameToStopLoopingSound - now;
  193. }
  194. if( m_frameToStartCooldown != 0 )
  195. {
  196. if( m_frameToStartCooldown <= now )
  197. sleepTime = UPDATE_SLEEP_NONE;
  198. else if( (m_frameToStartCooldown - now) < sleepTime )
  199. sleepTime = m_frameToStartCooldown - now;
  200. }
  201. if( m_frameToForceReload != 0 )
  202. {
  203. if( m_frameToForceReload <= now )
  204. sleepTime = UPDATE_SLEEP_NONE;
  205. else if( (m_frameToForceReload - now) < sleepTime )
  206. sleepTime = m_frameToForceReload - now;
  207. }
  208. return UPDATE_SLEEP(sleepTime);
  209. }
  210. //-------------------------------------------------------------------------------------------------
  211. void FiringTracker::speedUp()
  212. {
  213. ModelConditionFlags clr, set;
  214. Object *self = getObject();
  215. if( self->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST ) )
  216. {
  217. //self->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN );
  218. //self->clearModelConditionState( MODELCONDITION_CONTINUOUS_FIRE_MEAN );
  219. //self->clearModelConditionState( MODELCONDITION_CONTINUOUS_FIRE_SLOW );
  220. }
  221. else if(self->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN ) )
  222. {
  223. const AudioEventRTS *soundToPlayPtr = self->getTemplate()->getPerUnitSound( "VoiceRapidFire" );
  224. AudioEventRTS soundToPlay = *soundToPlayPtr;
  225. soundToPlay.setObjectID( self->getID() );
  226. TheAudio->addAudioEvent( &soundToPlay );
  227. // These flags are exclusive, not cumulative
  228. self->setWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST );
  229. set.set(MODELCONDITION_CONTINUOUS_FIRE_FAST);
  230. // These flags are exclusive, not cumulative
  231. self->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN );
  232. clr.set(MODELCONDITION_CONTINUOUS_FIRE_MEAN);
  233. clr.set(MODELCONDITION_CONTINUOUS_FIRE_SLOW);
  234. }
  235. else
  236. {
  237. self->setWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN );
  238. set.set(MODELCONDITION_CONTINUOUS_FIRE_MEAN);
  239. // These flags are exclusive, not cumulative
  240. self->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST );
  241. clr.set(MODELCONDITION_CONTINUOUS_FIRE_FAST);
  242. clr.set(MODELCONDITION_CONTINUOUS_FIRE_SLOW);
  243. }
  244. self->clearAndSetModelConditionFlags(clr, set);
  245. }
  246. //-------------------------------------------------------------------------------------------------
  247. void FiringTracker::coolDown()
  248. {
  249. ModelConditionFlags clr, set;
  250. if( getObject()->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST )
  251. || getObject()->testWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN ))
  252. {
  253. // Straight to zero from wherever it is
  254. set.set(MODELCONDITION_CONTINUOUS_FIRE_SLOW);
  255. getObject()->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST );
  256. getObject()->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN );
  257. clr.set(MODELCONDITION_CONTINUOUS_FIRE_FAST);
  258. clr.set(MODELCONDITION_CONTINUOUS_FIRE_MEAN);
  259. }
  260. else // we stop, now
  261. {
  262. getObject()->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_FAST );
  263. getObject()->clearWeaponBonusCondition( WEAPONBONUSCONDITION_CONTINUOUS_FIRE_MEAN );
  264. // Just chillin, nothing to change
  265. clr.set(MODELCONDITION_CONTINUOUS_FIRE_FAST);
  266. clr.set(MODELCONDITION_CONTINUOUS_FIRE_MEAN);
  267. clr.set(MODELCONDITION_CONTINUOUS_FIRE_SLOW);
  268. m_frameToStartCooldown = 0;
  269. }
  270. getObject()->clearAndSetModelConditionFlags(clr, set);
  271. // Start everything over
  272. m_consecutiveShots = 0;
  273. m_victimID = INVALID_ID;
  274. }
  275. // ------------------------------------------------------------------------------------------------
  276. /** CRC */
  277. // ------------------------------------------------------------------------------------------------
  278. void FiringTracker::crc( Xfer *xfer )
  279. {
  280. // object helper base class
  281. UpdateModule::crc( xfer );
  282. } // end crc
  283. // ------------------------------------------------------------------------------------------------
  284. /** Xfer method
  285. * Version Info:
  286. * 1: Initial version */
  287. // ------------------------------------------------------------------------------------------------
  288. void FiringTracker::xfer( Xfer *xfer )
  289. {
  290. // version
  291. XferVersion currentVersion = 1;
  292. XferVersion version = currentVersion;
  293. xfer->xferVersion( &version, currentVersion );
  294. // object helper base class
  295. UpdateModule::xfer( xfer );
  296. // consecutive shots
  297. xfer->xferInt( &m_consecutiveShots );
  298. // victim id
  299. xfer->xferObjectID( &m_victimID );
  300. // frame to start cooldown
  301. xfer->xferUnsignedInt( &m_frameToStartCooldown );
  302. } // end xfer
  303. // ------------------------------------------------------------------------------------------------
  304. /** Load post process */
  305. // ------------------------------------------------------------------------------------------------
  306. void FiringTracker::loadPostProcess( void )
  307. {
  308. // object helper back class
  309. UpdateModule::loadPostProcess();
  310. } // end loadPostProcess