JetSlowDeathBehavior.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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: 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 || us->getStatusBits().test( OBJECT_STATUS_DECK_HEIGHT_OFFSET ) )
  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. getObject()->clearStatus( MAKE_OBJECT_STATUS_MASK( OBJECT_STATUS_DECK_HEIGHT_OFFSET ) );
  138. } // end onDie
  139. // ------------------------------------------------------------------------------------------------
  140. // ------------------------------------------------------------------------------------------------
  141. void JetSlowDeathBehavior::beginSlowDeath( const DamageInfo *damageInfo )
  142. {
  143. // extend functionality
  144. SlowDeathBehavior::beginSlowDeath( damageInfo );
  145. // get our info
  146. Object *us = getObject();
  147. const JetSlowDeathBehaviorModuleData *modData = getJetSlowDeathBehaviorModuleData();
  148. // record the frame we died on
  149. m_timerDeathFrame = TheGameLogic->getFrame();
  150. // do some effects
  151. FXList::doFXObj( modData->m_fxInitialDeath, us );
  152. ObjectCreationList::create( modData->m_oclInitialDeath, us, NULL );
  153. // start audio loop playing
  154. m_deathLoopSound = modData->m_deathLoopSound;
  155. if( m_deathLoopSound.getEventName().isEmpty() == FALSE )
  156. {
  157. m_deathLoopSound.setObjectID( us->getID() );
  158. m_deathLoopSound.setPlayingHandle( TheAudio->addAudioEvent( &m_deathLoopSound ) );
  159. } // end if
  160. // initialize our roll rate to that defined as the initial value in the module data
  161. m_rollRate = modData->m_rollRate;
  162. // set the locomotor so that the plane starts falling
  163. Locomotor *locomotor = us->getAIUpdateInterface()->getCurLocomotor();
  164. locomotor->setMaxLift( -TheGlobalData->m_gravity * (1.0f - modData->m_fallHowFast) );
  165. // do not allow the jet to turn anymore
  166. locomotor->setMaxTurnRate( 0.0f );
  167. } // end beginSlowDeath
  168. // ------------------------------------------------------------------------------------------------
  169. // ------------------------------------------------------------------------------------------------
  170. UpdateSleepTime JetSlowDeathBehavior::update( void )
  171. {
  172. // extend functionality of base class
  173. SlowDeathBehavior::update();
  174. // if the death is not activated, do nothing else
  175. if( isSlowDeathActivated() == FALSE )
  176. return UPDATE_SLEEP_NONE;
  177. // get object info
  178. Object *us = getObject();
  179. const JetSlowDeathBehaviorModuleData *modData = getJetSlowDeathBehaviorModuleData();
  180. // roll us around in the air
  181. PhysicsBehavior *physics = us->getPhysics();
  182. DEBUG_ASSERTCRASH( physics, ("JetSlowDeathBehavior::beginSlowDeath - '%s' has no physics\n",
  183. us->getTemplate()->getName().str()) );
  184. if( physics )
  185. physics->setRollRate( m_rollRate );
  186. // adjust the roll rate over time
  187. m_rollRate *= modData->m_rollRateDelta;
  188. // do effects for death while in the air
  189. if( m_timerOnGroundFrame == 0 )
  190. {
  191. PathfindLayerEnum layer = TheTerrainLogic->getLayerForDestination(us->getPosition());
  192. us->setLayer(layer);
  193. Real height;
  194. if (layer == LAYER_GROUND)
  195. {
  196. // (this is more efficient than getGroundHeight because the info is cached)
  197. height = us->getHeightAboveTerrain();
  198. }
  199. else
  200. {
  201. Real layerHeight = TheTerrainLogic->getLayerHeight( us->getPosition()->x, us->getPosition()->y, layer );
  202. height = us->getPosition()->z - layerHeight;
  203. // slop a little bit for bridges, since we tend to end up fractionally
  204. // above 'em, and it's easier to just slop it here
  205. if (height >= 0.0f && height <= 1.0f)
  206. height = 0.0f;
  207. }
  208. Bool hitATree = FALSE;
  209. // Here we want to make sure we crash if we collide with a tree on the way down
  210. PhysicsBehavior *phys = us->getPhysics();
  211. if ( m_timerOnGroundFrame == 0 && phys )
  212. {
  213. ObjectID treeID = phys->getLastCollidee();
  214. Object *tree = TheGameLogic->findObjectByID( treeID );
  215. if ( tree )
  216. {
  217. if (tree->isKindOf( KINDOF_SHRUBBERY ) )
  218. hitATree = TRUE;
  219. }
  220. }
  221. // when we've hit the ground, we're totally done
  222. if( height <= 0.0f || hitATree )
  223. {
  224. // stop the death looping sound at the right time
  225. TheAudio->removeAudioEvent( m_deathLoopSound.getPlayingHandle() );
  226. // do some effects
  227. FXList::doFXObj( modData->m_fxHitGround, us );
  228. ObjectCreationList::create( modData->m_oclHitGround, us, NULL );
  229. // we are now on the ground
  230. m_timerOnGroundFrame = TheGameLogic->getFrame();
  231. // start us rolling on another axis too
  232. if( physics )
  233. physics->setPitchRate( modData->m_pitchRate );
  234. } // end if
  235. // timers for the secondary effect
  236. if( m_timerDeathFrame != 0 &&
  237. TheGameLogic->getFrame() - m_timerDeathFrame >= modData->m_delaySecondaryFromInitialDeath )
  238. {
  239. // do some effects
  240. FXList::doFXObj( modData->m_fxSecondary, us );
  241. ObjectCreationList::create( modData->m_oclSecondary, us, NULL );
  242. // clear the death frame timer since we've already executed the event now
  243. m_timerDeathFrame = 0;
  244. } //end if
  245. } // end if
  246. else
  247. {
  248. // we are on the ground, pay attention to the final explosion timers
  249. if( TheGameLogic->getFrame() - m_timerOnGroundFrame >= modData->m_delayFinalBlowUpFromHitGround )
  250. {
  251. // do some effects
  252. FXList::doFXObj( modData->m_fxFinalBlowUp, us );
  253. ObjectCreationList::create( modData->m_oclFinalBlowUp, us, NULL );
  254. // we're all done now
  255. TheGameLogic->destroyObject( us );
  256. } // end if
  257. } // end else
  258. return UPDATE_SLEEP_NONE;
  259. } // end update
  260. // ------------------------------------------------------------------------------------------------
  261. /** CRC */
  262. // ------------------------------------------------------------------------------------------------
  263. void JetSlowDeathBehavior::crc( Xfer *xfer )
  264. {
  265. // extend base class
  266. SlowDeathBehavior::crc( xfer );
  267. } // end crc
  268. // ------------------------------------------------------------------------------------------------
  269. /** Xfer method
  270. * Version Info:
  271. * 1: Initial version */
  272. // ------------------------------------------------------------------------------------------------
  273. void JetSlowDeathBehavior::xfer( Xfer *xfer )
  274. {
  275. // version
  276. XferVersion currentVersion = 1;
  277. XferVersion version = currentVersion;
  278. xfer->xferVersion( &version, currentVersion );
  279. // extend base class
  280. SlowDeathBehavior::xfer( xfer );
  281. // timer death frame
  282. xfer->xferUnsignedInt( &m_timerDeathFrame );
  283. // on ground frame
  284. xfer->xferUnsignedInt( &m_timerOnGroundFrame );
  285. // roll rate
  286. xfer->xferReal( &m_rollRate );
  287. } // end xfer
  288. // ------------------------------------------------------------------------------------------------
  289. /** Load post process */
  290. // ------------------------------------------------------------------------------------------------
  291. void JetSlowDeathBehavior::loadPostProcess( void )
  292. {
  293. // extend base class
  294. SlowDeathBehavior::loadPostProcess();
  295. } // end loadPostProcess