JetSlowDeathBehavior.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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: JetSlowDeathBehavior.cpp /////////////////////////////////////////////////////////////////
  24. // Author: Colin Day
  25. // Desc: Death sequence for jets
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/GlobalData.h"
  30. #include "Common/ThingTemplate.h"
  31. #include "Common/Xfer.h"
  32. #include "GameClient/FXList.h"
  33. #include "GameClient/InGameUI.h"
  34. #include "GameLogic/GameLogic.h"
  35. #include "GameLogic/Locomotor.h"
  36. #include "GameLogic/Module/AIUpdate.h"
  37. #include "GameLogic/Module/JetSlowDeathBehavior.h"
  38. #include "GameLogic/Module/PhysicsUpdate.h"
  39. #include "GameLogic/Object.h"
  40. #include "GameLogic/ObjectCreationList.h"
  41. #ifdef _INTERNAL
  42. // for occasional debugging...
  43. //#pragma optimize("", off)
  44. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  45. #endif
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////
  47. ///////////////////////////////////////////////////////////////////////////////////////////////////
  48. ///////////////////////////////////////////////////////////////////////////////////////////////////
  49. // ------------------------------------------------------------------------------------------------
  50. // ------------------------------------------------------------------------------------------------
  51. JetSlowDeathBehaviorModuleData::JetSlowDeathBehaviorModuleData( void )
  52. {
  53. m_fxOnGroundDeath = NULL;
  54. m_oclOnGroundDeath = NULL;
  55. m_fxInitialDeath = NULL;
  56. m_oclInitialDeath = NULL;
  57. m_delaySecondaryFromInitialDeath = 0;
  58. m_fxSecondary = NULL;
  59. m_oclSecondary = NULL;
  60. m_fxHitGround = NULL;
  61. m_oclHitGround = NULL;
  62. m_delayFinalBlowUpFromHitGround = 0;
  63. m_fxFinalBlowUp = NULL;
  64. m_oclFinalBlowUp = NULL;
  65. m_rollRate = 0.0f;
  66. m_rollRateDelta = 1.0f;
  67. m_pitchRate = 0.0f;
  68. m_fallHowFast = 0.0f;
  69. } // end JetSlowDeathBehaviorModuleData
  70. // ------------------------------------------------------------------------------------------------
  71. // ------------------------------------------------------------------------------------------------
  72. /*static*/ void JetSlowDeathBehaviorModuleData::buildFieldParse( MultiIniFieldParse &p )
  73. {
  74. SlowDeathBehaviorModuleData::buildFieldParse( p );
  75. static const FieldParse dataFieldParse[] =
  76. {
  77. { "FXOnGroundDeath", INI::parseFXList, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_fxOnGroundDeath ) },
  78. { "OCLOnGroundDeath", INI::parseObjectCreationList, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_oclOnGroundDeath ) },
  79. { "FXInitialDeath", INI::parseFXList, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_fxInitialDeath ) },
  80. { "OCLInitialDeath", INI::parseObjectCreationList, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_oclInitialDeath ) },
  81. { "DelaySecondaryFromInitialDeath", INI::parseDurationUnsignedInt, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_delaySecondaryFromInitialDeath ) },
  82. { "FXSecondary", INI::parseFXList, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_fxSecondary ) },
  83. { "OCLSecondary", INI::parseObjectCreationList, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_oclSecondary ) },
  84. { "FXHitGround", INI::parseFXList, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_fxHitGround ) },
  85. { "OCLHitGround", INI::parseObjectCreationList, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_oclHitGround ) },
  86. { "DelayFinalBlowUpFromHitGround", INI::parseDurationUnsignedInt, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_delayFinalBlowUpFromHitGround ) },
  87. { "FXFinalBlowUp", INI::parseFXList, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_fxFinalBlowUp ) },
  88. { "OCLFinalBlowUp", INI::parseObjectCreationList, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_oclFinalBlowUp ) },
  89. { "DeathLoopSound", INI::parseAudioEventRTS, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_deathLoopSound ) },
  90. // @todo srj -- RollRate and RollRateDelta and PitchRate should use parseAngularVelocityReal
  91. { "RollRate", INI::parseReal, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_rollRate ) },
  92. { "RollRateDelta", INI::parsePercentToReal, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_rollRateDelta ) },
  93. { "PitchRate", INI::parseReal, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_pitchRate ) },
  94. { "FallHowFast", INI::parsePercentToReal, NULL, offsetof( JetSlowDeathBehaviorModuleData, m_fallHowFast ) },
  95. { 0, 0, 0, 0 }
  96. };
  97. p.add( dataFieldParse );
  98. } // end buildFieldParse
  99. ///////////////////////////////////////////////////////////////////////////////////////////////////
  100. ///////////////////////////////////////////////////////////////////////////////////////////////////
  101. ///////////////////////////////////////////////////////////////////////////////////////////////////
  102. // ------------------------------------------------------------------------------------------------
  103. // ------------------------------------------------------------------------------------------------
  104. JetSlowDeathBehavior::JetSlowDeathBehavior( Thing *thing, const ModuleData *moduleData )
  105. : SlowDeathBehavior( thing, moduleData )
  106. {
  107. m_timerDeathFrame = 0;
  108. m_timerOnGroundFrame = 0;
  109. m_rollRate = 0.0f;
  110. } // end JetSlowDeathBehavior
  111. // ------------------------------------------------------------------------------------------------
  112. // ------------------------------------------------------------------------------------------------
  113. JetSlowDeathBehavior::~JetSlowDeathBehavior( void )
  114. {
  115. } // end ~JetSlowDeathBehavior
  116. // ------------------------------------------------------------------------------------------------
  117. // ------------------------------------------------------------------------------------------------
  118. void JetSlowDeathBehavior::onDie( const DamageInfo *damageInfo )
  119. {
  120. Object *us = getObject();
  121. // if the jet is on the ground we do just our ground fx death
  122. if( us->isSignificantlyAboveTerrain() == FALSE )
  123. {
  124. const JetSlowDeathBehaviorModuleData *modData = getJetSlowDeathBehaviorModuleData();
  125. // execute fx
  126. FXList::doFXObj( modData->m_fxOnGroundDeath, us );
  127. // execute ocl
  128. ObjectCreationList::create( modData->m_oclOnGroundDeath, us, NULL );
  129. // destroy object
  130. TheGameLogic->destroyObject( us );
  131. } // end if
  132. else
  133. {
  134. // extend base class for slow death and begin the slow death behavior
  135. SlowDeathBehavior::onDie( damageInfo );
  136. } // end else
  137. } // end onDie
  138. // ------------------------------------------------------------------------------------------------
  139. // ------------------------------------------------------------------------------------------------
  140. void JetSlowDeathBehavior::beginSlowDeath( const DamageInfo *damageInfo )
  141. {
  142. // extend functionality
  143. SlowDeathBehavior::beginSlowDeath( damageInfo );
  144. // get our info
  145. Object *us = getObject();
  146. const JetSlowDeathBehaviorModuleData *modData = getJetSlowDeathBehaviorModuleData();
  147. // record the frame we died on
  148. m_timerDeathFrame = TheGameLogic->getFrame();
  149. // do some effects
  150. FXList::doFXObj( modData->m_fxInitialDeath, us );
  151. ObjectCreationList::create( modData->m_oclInitialDeath, us, NULL );
  152. // start audio loop playing
  153. m_deathLoopSound = modData->m_deathLoopSound;
  154. if( m_deathLoopSound.getEventName().isEmpty() == FALSE )
  155. {
  156. m_deathLoopSound.setObjectID( us->getID() );
  157. m_deathLoopSound.setPlayingHandle( TheAudio->addAudioEvent( &m_deathLoopSound ) );
  158. } // end if
  159. // initialize our roll rate to that defined as the initial value in the module data
  160. m_rollRate = modData->m_rollRate;
  161. // set the locomotor so that the plane starts falling
  162. Locomotor *locomotor = us->getAIUpdateInterface()->getCurLocomotor();
  163. locomotor->setMaxLift( -TheGlobalData->m_gravity * (1.0f - modData->m_fallHowFast) );
  164. // do not allow the jet to turn anymore
  165. locomotor->setMaxTurnRate( 0.0f );
  166. } // end beginSlowDeath
  167. // ------------------------------------------------------------------------------------------------
  168. // ------------------------------------------------------------------------------------------------
  169. UpdateSleepTime JetSlowDeathBehavior::update( void )
  170. {
  171. // extend functionality of base class
  172. SlowDeathBehavior::update();
  173. // if the death is not activated, do nothing else
  174. if( isSlowDeathActivated() == FALSE )
  175. return UPDATE_SLEEP_NONE;
  176. // get object info
  177. Object *us = getObject();
  178. const JetSlowDeathBehaviorModuleData *modData = getJetSlowDeathBehaviorModuleData();
  179. // roll us around in the air
  180. PhysicsBehavior *physics = us->getPhysics();
  181. DEBUG_ASSERTCRASH( physics, ("JetSlowDeathBehavior::beginSlowDeath - '%s' has no physics\n",
  182. us->getTemplate()->getName().str()) );
  183. if( physics )
  184. physics->setRollRate( m_rollRate );
  185. // adjust the roll rate over time
  186. m_rollRate *= modData->m_rollRateDelta;
  187. // do effects for death while in the air
  188. if( m_timerOnGroundFrame == 0 )
  189. {
  190. PathfindLayerEnum layer = TheTerrainLogic->getLayerForDestination(us->getPosition());
  191. us->setLayer(layer);
  192. Real height;
  193. if (layer == LAYER_GROUND)
  194. {
  195. // (this is more efficient than getGroundHeight because the info is cached)
  196. height = us->getHeightAboveTerrain();
  197. }
  198. else
  199. {
  200. Real layerHeight = TheTerrainLogic->getLayerHeight( us->getPosition()->x, us->getPosition()->y, layer );
  201. height = us->getPosition()->z - layerHeight;
  202. // slop a little bit for bridges, since we tend to end up fractionally
  203. // above 'em, and it's easier to just slop it here
  204. if (height >= 0.0f && height <= 1.0f)
  205. height = 0.0f;
  206. }
  207. Bool hitATree = FALSE;
  208. // Here we want to make sure we crash if we collide with a tree on the way down
  209. PhysicsBehavior *phys = us->getPhysics();
  210. if ( m_timerOnGroundFrame == 0 && phys )
  211. {
  212. ObjectID treeID = phys->getLastCollidee();
  213. Object *tree = TheGameLogic->findObjectByID( treeID );
  214. if ( tree )
  215. {
  216. if (tree->isKindOf( KINDOF_SHRUBBERY ) )
  217. hitATree = TRUE;
  218. }
  219. }
  220. // when we've hit the ground, we're totally done
  221. if( height <= 0.0f || hitATree )
  222. {
  223. // stop the death looping sound at the right time
  224. TheAudio->removeAudioEvent( m_deathLoopSound.getPlayingHandle() );
  225. // do some effects
  226. FXList::doFXObj( modData->m_fxHitGround, us );
  227. ObjectCreationList::create( modData->m_oclHitGround, us, NULL );
  228. // we are now on the ground
  229. m_timerOnGroundFrame = TheGameLogic->getFrame();
  230. // start us rolling on another axis too
  231. if( physics )
  232. physics->setPitchRate( modData->m_pitchRate );
  233. } // end if
  234. // timers for the secondary effect
  235. if( m_timerDeathFrame != 0 &&
  236. TheGameLogic->getFrame() - m_timerDeathFrame >= modData->m_delaySecondaryFromInitialDeath )
  237. {
  238. // do some effects
  239. FXList::doFXObj( modData->m_fxSecondary, us );
  240. ObjectCreationList::create( modData->m_oclSecondary, us, NULL );
  241. // clear the death frame timer since we've already executed the event now
  242. m_timerDeathFrame = 0;
  243. } //end if
  244. } // end if
  245. else
  246. {
  247. // we are on the ground, pay attention to the final explosion timers
  248. if( TheGameLogic->getFrame() - m_timerOnGroundFrame >= modData->m_delayFinalBlowUpFromHitGround )
  249. {
  250. // do some effects
  251. FXList::doFXObj( modData->m_fxFinalBlowUp, us );
  252. ObjectCreationList::create( modData->m_oclFinalBlowUp, us, NULL );
  253. // we're all done now
  254. TheGameLogic->destroyObject( us );
  255. } // end if
  256. } // end else
  257. return UPDATE_SLEEP_NONE;
  258. } // end update
  259. // ------------------------------------------------------------------------------------------------
  260. /** CRC */
  261. // ------------------------------------------------------------------------------------------------
  262. void JetSlowDeathBehavior::crc( Xfer *xfer )
  263. {
  264. // extend base class
  265. SlowDeathBehavior::crc( xfer );
  266. } // end crc
  267. // ------------------------------------------------------------------------------------------------
  268. /** Xfer method
  269. * Version Info:
  270. * 1: Initial version */
  271. // ------------------------------------------------------------------------------------------------
  272. void JetSlowDeathBehavior::xfer( Xfer *xfer )
  273. {
  274. // version
  275. XferVersion currentVersion = 1;
  276. XferVersion version = currentVersion;
  277. xfer->xferVersion( &version, currentVersion );
  278. // extend base class
  279. SlowDeathBehavior::xfer( xfer );
  280. // timer death frame
  281. xfer->xferUnsignedInt( &m_timerDeathFrame );
  282. // on ground frame
  283. xfer->xferUnsignedInt( &m_timerOnGroundFrame );
  284. // roll rate
  285. xfer->xferReal( &m_rollRate );
  286. } // end xfer
  287. // ------------------------------------------------------------------------------------------------
  288. /** Load post process */
  289. // ------------------------------------------------------------------------------------------------
  290. void JetSlowDeathBehavior::loadPostProcess( void )
  291. {
  292. // extend base class
  293. SlowDeathBehavior::loadPostProcess();
  294. } // end loadPostProcess