EMPUpdate.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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: EMPUpdate.cpp ///////////////////////////////////////////////////////////////////////
  24. // Author: Mark Lorenzen Sept. 2002
  25. // Desc: Update that makes the electromagnetic pulse field grow, fade and disable junk
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/Thing.h"
  30. #include "Common/ThingTemplate.h"
  31. #include "Common/INI.h"
  32. #include "Common/RandomValue.h"
  33. #include "Common/Player.h"
  34. #include "GameLogic/GameLogic.h"
  35. #include "GameLogic/Module/EMPUpdate.h"
  36. #include "GameLogic/ObjectIter.h"
  37. #include "GameLogic/PartitionManager.h"
  38. #include "GameLogic/Module/AIUpdate.h"
  39. #include "GameLogic/Module/ContainModule.h"
  40. #include "GameLogic/Module/PhysicsUpdate.h"
  41. #include "GameLogic/Object.h"
  42. #include "GameClient/Drawable.h"
  43. #include "Common/KindOf.h"
  44. #include "GameClient/ParticleSys.h"
  45. #ifdef _INTERNAL
  46. // for occasional debugging...
  47. //#pragma optimize("", off)
  48. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  49. #endif
  50. ///////////////////////////////////////////////////////////////////////////////////////////
  51. // Static member initialization
  52. ///////////////////////////////////////////////////////////////////////////////////////////
  53. //Bool EMPUpdate::s_lastInstanceSpunPositive = FALSE;
  54. //-------------------------------------------------------------------------------------------------
  55. static void saturateRGB(RGBColor& color, Real factor)
  56. {
  57. color.red *= factor;
  58. color.green *= factor;
  59. color.blue *= factor;
  60. Real halfFactor = factor * 0.5f;
  61. color.red -= halfFactor;
  62. color.green -= halfFactor;
  63. color.blue -= halfFactor;
  64. }
  65. //-------------------------------------------------------------------------------------------------
  66. //-------------------------------------------------------------------------------------------------
  67. EMPUpdate::EMPUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  68. {
  69. //s_lastInstanceSpunPositive = !s_lastInstanceSpunPositive; //TOGGLES STATIC BOOL
  70. const EMPUpdateModuleData *data = getEMPUpdateModuleData();
  71. if ( data )
  72. {
  73. //SANITY
  74. DEBUG_ASSERTCRASH( TheGameLogic, ("EMPUpdate::EMPUpdate - TheGameLogic is NULL\n" ) );
  75. UnsignedInt now = TheGameLogic->getFrame();
  76. m_currentScale = data->m_startScale;
  77. m_dieFrame = REAL_TO_UNSIGNEDINT( now + data->m_lifeFrames );
  78. m_tintEnvPlayFrame = REAL_TO_UNSIGNEDINT( now + data->m_startFadeFrame );
  79. m_tintEnvFadeFrames = m_dieFrame - m_tintEnvPlayFrame;
  80. //m_spinRate = GameLogicRandomValueReal(data->m_spinRateMax * 0.5f, data->m_spinRateMax);
  81. m_targetScale = GameLogicRandomValueReal(data->m_targetScaleMin, data->m_targetScaleMax);
  82. //if (s_lastInstanceSpunPositive)
  83. //{
  84. // m_spinRate *= -1.0f;
  85. //}
  86. getObject()->setOrientation(GameLogicRandomValueReal(-PI,PI));
  87. DEBUG_ASSERTCRASH( m_tintEnvPlayFrame < m_dieFrame, ("EMPUpdate::EMPUpdate - you cant play fade after death\n" ) );
  88. return;
  89. }
  90. //SANITY
  91. DEBUG_ASSERTCRASH( data, ("EMPUpdate::EMPUpdate - getEMPUpdateModuleData is NULL\n" ) );
  92. m_currentScale = 1.0f;
  93. m_dieFrame = 0;
  94. m_tintEnvFadeFrames = 0;
  95. m_tintEnvPlayFrame = 0;
  96. //m_spinRate = 0;
  97. m_targetScale = 1;
  98. }
  99. //-------------------------------------------------------------------------------------------------
  100. //-------------------------------------------------------------------------------------------------
  101. EMPUpdate::~EMPUpdate( void )
  102. {
  103. }
  104. //-------------------------------------------------------------------------------------------------
  105. //-------------------------------------------------------------------------------------------------
  106. UpdateSleepTime EMPUpdate::update( void )
  107. {
  108. /// @todo srj use SLEEPY_UPDATE here
  109. Object *obj = getObject();
  110. const EMPUpdateModuleData *data = getEMPUpdateModuleData();
  111. Drawable *dr = obj->getDrawable();
  112. UnsignedInt now = TheGameLogic->getFrame();
  113. m_currentScale += ( m_targetScale - m_currentScale ) * 0.05f;
  114. dr->setInstanceScale( m_currentScale );
  115. if ( now < m_tintEnvPlayFrame)
  116. {
  117. RGBColor start = data->m_startColor;
  118. saturateRGB( start, 2 );
  119. dr->colorTint( &start );
  120. }
  121. if ( now == m_tintEnvPlayFrame)
  122. {
  123. RGBColor end = data->m_endColor;
  124. saturateRGB( end, 5 );
  125. dr->colorFlash( &end, 9999, m_tintEnvFadeFrames, TRUE );
  126. doDisableAttack();
  127. }
  128. //Real curSpin = obj->getOrientation();
  129. //curSpin += m_spinRate;
  130. //curSpin = normalizeAngle(curSpin);
  131. //obj->setOrientation( curSpin );
  132. if( now >= m_dieFrame )
  133. obj->kill();
  134. return UPDATE_SLEEP_NONE;
  135. }
  136. //-------------------------------------------------------------------------------------------------
  137. //-------------------------------------------------------------------------------------------------
  138. void EMPUpdate::doDisableAttack( void )
  139. {
  140. Object *object = getObject();
  141. const EMPUpdateModuleData *data = getEMPUpdateModuleData();
  142. if( !object || !data )
  143. return; //sanity
  144. Real radius = data->m_effectRadius;
  145. Real curVictimDistSqr;
  146. const Coord3D *pos = object->getPosition();
  147. //Kris -- October 28, 2003 -- Patch 1.01
  148. //If the EMP hits an airborne target, then don't allow the EMP
  149. //blast to effect anything on the ground.
  150. Object *producer = TheGameLogic->findObjectByID( object->getProducerID() );
  151. Object *intendedVictim = NULL;
  152. Bool onlyEffectAirborne = FALSE;
  153. Bool intendedVictimProcessed = FALSE;
  154. if( producer && producer->getAI() )
  155. {
  156. intendedVictim = producer->getAI()->getCurrentVictim();
  157. if( intendedVictim && intendedVictim->isAirborneTarget() )
  158. {
  159. onlyEffectAirborne = TRUE;
  160. }
  161. }
  162. SimpleObjectIterator *iter;
  163. Object *curVictim;
  164. if (radius > 0.0f)
  165. {
  166. iter = ThePartitionManager->iterateObjectsInRange(pos,
  167. radius, FROM_BOUNDINGSPHERE_3D);
  168. curVictim = iter->firstWithNumeric(&curVictimDistSqr);
  169. }
  170. MemoryPoolObjectHolder hold(iter);
  171. for ( ; curVictim != NULL; curVictim = iter ? iter->nextWithNumeric(&curVictimDistSqr) : NULL)
  172. {
  173. if ( curVictim != object)
  174. {
  175. //Kris -- October 28, 2003 -- Patch 1.01
  176. //If the EMP hits an airborne target, then don't allow the EMP
  177. //blast to effect anything on the ground.
  178. if( onlyEffectAirborne && !curVictim->isAirborneTarget() )
  179. {
  180. continue;
  181. }
  182. //Some EMP attacks don't affect our own buildings.
  183. if( data->m_doesNotAffectMyOwnBuildings && curVictim->isKindOf( KINDOF_STRUCTURE ) )
  184. {
  185. if( curVictim->getControllingPlayer() == object->getControllingPlayer() )
  186. {
  187. continue;
  188. }
  189. }
  190. ////////////// // must match our kindof flags (if any)
  191. ////////////// if (data && !curVictim->isKindOfMulti(data->m_victimKindOf, data->m_victimKindOfNot))
  192. ////////////// continue;
  193. if ( !curVictim->isKindOf( KINDOF_VEHICLE ) && !curVictim->isKindOf(KINDOF_STRUCTURE) && !curVictim->isKindOf(KINDOF_SPAWNS_ARE_THE_WEAPONS) )
  194. {
  195. //DONT DISABLE PEOPLE, EXCEPT FOR STINGER SOLDIERS
  196. continue;
  197. }
  198. else if ( curVictim->isKindOf( KINDOF_AIRCRAFT ) && curVictim->isAirborneTarget() )// is in the sky
  199. {
  200. // WITHIN THE SET OF ALL FLYING THINGS, WE WANT TO EXEMPT SUPERWEAPON TRANSPORTS
  201. // if ( curVictim->isKindOf( KINDOF_TRANSPORT ) ) // is transport kindof
  202. // if ( curVictim->getContain() ) // does carry stuff
  203. // if ( curVictim->getContain()->getContainCount() > 0 ) // is carrying something
  204. // if ( ! curVictim->isKindOf( KINDOF_PRODUCED_AT_HELIPAD ) ) // but not a helicopter
  205. // continue;
  206. if ( curVictim->isKindOf( KINDOF_EMP_HARDENED ) ) // self-explanitory
  207. continue;
  208. curVictim->kill();// @todo this should use some sort of DEADSTICK DIE or something...
  209. Drawable *drw = curVictim->getDrawable();
  210. if ( drw )
  211. {
  212. drw->setTintStatus( TINT_STATUS_DISABLED );// paint it black
  213. }
  214. continue;
  215. }
  216. else if ( curVictim->isKindOf( KINDOF_STRUCTURE ) )
  217. {
  218. if ( ! curVictim->isFactionStructure() )
  219. continue;
  220. }
  221. // handle cases where we do not want allies to be hit by it's own EMP weapons
  222. else if ( (data->m_rejectMask & WEAPON_AFFECTS_ALLIES) && curVictim->getRelationship( object ) == ALLIES)
  223. {
  224. continue;
  225. }
  226. //Disable the target for a specified amount of time.
  227. curVictim->setDisabledUntil( DISABLED_EMP, TheGameLogic->getFrame() + data->m_disabledDuration );
  228. //Kris -- October 28, 2003 -- Patch 1.01
  229. if( intendedVictim == curVictim )
  230. {
  231. //We know the intended victim was in range. This will catch an edge case
  232. //where the intended target is hit, but the range is off enough to not effect it.
  233. intendedVictimProcessed = TRUE;
  234. }
  235. Drawable *drw = curVictim->getDrawable();
  236. if ( drw )
  237. {
  238. const ParticleSystemTemplate *tmp = data->m_disableFXParticleSystem;
  239. if (tmp)
  240. {
  241. Real victimHeight = curVictim->getGeometryInfo().getMaxHeightAbovePosition();
  242. Real victimFootprintArea = curVictim->getGeometryInfo().getFootprintArea();
  243. Real victimVolume = victimFootprintArea * MIN(victimHeight, 10.0f);
  244. UnsignedInt emitterCount = MAX(15, REAL_TO_INT_CEIL(data->m_sparksPerCubicFoot * victimVolume));
  245. for (Int e = 0 ; e < emitterCount; ++e)
  246. {
  247. ParticleSystem *sys = TheParticleSystemManager->createParticleSystem(tmp);
  248. if (sys)
  249. {
  250. Coord3D offs = {0,0,0};
  251. curVictim->getGeometryInfo().makeRandomOffsetWithinFootprint( offs );
  252. offs.z = GameLogicRandomValue(3, victimHeight);
  253. //This puts all the sparks within a quadrahemicycloid (rectangular dome) volume
  254. //The same shape as a four cornered camping dome tent, for those with less Greek
  255. if (offs.length() > victimHeight)
  256. {
  257. Real resoreX = offs.x;
  258. Real resoreY = offs.y;
  259. offs.normalize();
  260. offs.z *= victimHeight;
  261. offs.x = resoreX;
  262. offs.y = resoreY;
  263. }
  264. sys->attachToObject(curVictim);
  265. sys->setPosition( &offs );
  266. sys->setSystemLifetime(MAX(0, data->m_disabledDuration - 30));
  267. sys->setInitialDelay(GameLogicRandomValue(1,100));
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }
  274. //Kris -- October 28, 2003 -- Patch 1.01
  275. //Handle edge case when the EMP explodes, but "misses" the intended target.
  276. if( intendedVictim && !intendedVictimProcessed && intendedVictim->isKindOf( KINDOF_AIRCRAFT ) )
  277. {
  278. if( !intendedVictim->isKindOf( KINDOF_EMP_HARDENED ) )
  279. {
  280. //Victim position
  281. Coord3D coord;
  282. coord.set( intendedVictim->getPosition() );
  283. //Subtract this object (distance from missile to victim's previous position)
  284. coord.sub( pos );
  285. Real lengthSqr = coord.lengthSqr();
  286. if( lengthSqr <= radius * 2.0f || lengthSqr <= 40.0f * 40.0f )
  287. {
  288. //Disable the target for a specified amount of time.
  289. intendedVictim->setDisabledUntil( DISABLED_EMP, TheGameLogic->getFrame() + data->m_disabledDuration );
  290. }
  291. }
  292. }
  293. }
  294. // ------------------------------------------------------------------------------------------------
  295. /** CRC */
  296. // ------------------------------------------------------------------------------------------------
  297. void EMPUpdate::crc( Xfer *xfer )
  298. {
  299. } // end crc
  300. // ------------------------------------------------------------------------------------------------
  301. /** Xfer method
  302. * Version Info:
  303. * 1: Initial version */
  304. // ------------------------------------------------------------------------------------------------
  305. void EMPUpdate::xfer( Xfer *xfer )
  306. {
  307. // version
  308. XferVersion currentVersion = 1;
  309. XferVersion version = currentVersion;
  310. xfer->xferVersion( &version, currentVersion );
  311. } // end xfer
  312. // ------------------------------------------------------------------------------------------------
  313. /** Load post process */
  314. // ------------------------------------------------------------------------------------------------
  315. void EMPUpdate::loadPostProcess( void )
  316. {
  317. } // end loadPostProcess
  318. ///
  319. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  320. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  321. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////^
  322. // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////^^
  323. // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////^^^^^^
  324. // ////////////////////////////////////////////////////////////////////////////////////////////////^^^^^^^^^^^^^^
  325. /////////////////////////////////////////////////////////////////////////^^^^^^^^^^^^^
  326. ///////////////////////////////////////////////////^^^^^^^^^^^^
  327. /////////////////////////////////////^^^^^^^^^^
  328. //////////////////////////^^^^^
  329. // ////////////////^^^
  330. // /////////////^
  331. // ///////////
  332. //-------------------------------------------------------------------------------------------------
  333. //-------------------------------------------------------------------------------------------------
  334. LeafletDropBehavior::LeafletDropBehavior( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  335. {
  336. m_fxFired = FALSE;
  337. //s_lastInstanceSpunPositive = !s_lastInstanceSpunPositive; //TOGGLES STATIC BOOL
  338. const LeafletDropBehaviorModuleData *data = getLeafletDropBehaviorModuleData();
  339. if ( data )
  340. {
  341. //SANITY
  342. DEBUG_ASSERTCRASH( TheGameLogic, ("LeafletDropBehavior::LeafletDropBehavior - TheGameLogic is NULL\n" ) );
  343. UnsignedInt now = TheGameLogic->getFrame();
  344. m_startFrame = now + data->m_delayFrames;
  345. return;
  346. }
  347. //SANITY
  348. DEBUG_ASSERTCRASH( data, ("LeafletDropBehavior::LeafletDropBehavior - getLeafletDropBehaviorModuleData is NULL\n" ) );
  349. m_startFrame = TheGameLogic->getFrame() + 1;
  350. }
  351. //-------------------------------------------------------------------------------------------------
  352. //-------------------------------------------------------------------------------------------------
  353. LeafletDropBehavior::~LeafletDropBehavior( void )
  354. {
  355. }
  356. //-------------------------------------------------------------------------------------------------
  357. //-------------------------------------------------------------------------------------------------
  358. UpdateSleepTime LeafletDropBehavior::update( void )
  359. {
  360. if ( ! m_fxFired )
  361. {
  362. // start shoveling out those leaflets, boys.
  363. const LeafletDropBehaviorModuleData *data = getLeafletDropBehaviorModuleData();
  364. const ParticleSystemTemplate *tmp = data->m_leafletFXParticleSystem;
  365. if (tmp)
  366. {
  367. ParticleSystem *sys = TheParticleSystemManager->createParticleSystem(tmp);
  368. if (sys)
  369. sys->attachToObject(getObject());
  370. }
  371. m_fxFired = TRUE; // hey, at least we tried.
  372. }
  373. if( TheGameLogic->getFrame() < m_startFrame )
  374. {
  375. // TheGameLogic->destroyObject( getObject() );
  376. return UPDATE_SLEEP_FOREVER;
  377. }
  378. doDisableAttack();
  379. return UPDATE_SLEEP_NONE;
  380. }
  381. //-------------------------------------------------------------------------------------------------
  382. //-------------------------------------------------------------------------------------------------
  383. void LeafletDropBehavior::onDie( const DamageInfo *damageInfo )
  384. {
  385. // the dieModule callback
  386. doDisableAttack();
  387. }
  388. //-------------------------------------------------------------------------------------------------
  389. //-------------------------------------------------------------------------------------------------
  390. void LeafletDropBehavior::doDisableAttack( void )
  391. {
  392. Object *object = getObject();
  393. const LeafletDropBehaviorModuleData *data = getLeafletDropBehaviorModuleData();
  394. if( !object || !data )
  395. return; //sanity
  396. Real radius = data->m_radius;
  397. Real curVictimDistSqr;
  398. const Coord3D *pos = object->getPosition();
  399. SimpleObjectIterator *iter;
  400. Object *curVictim;
  401. if (radius > 0.0f)
  402. {
  403. iter = ThePartitionManager->iterateObjectsInRange(pos,
  404. radius, FROM_BOUNDINGSPHERE_3D);
  405. curVictim = iter->firstWithNumeric(&curVictimDistSqr);
  406. }
  407. MemoryPoolObjectHolder hold(iter);
  408. for ( ; curVictim != NULL; curVictim = iter ? iter->nextWithNumeric(&curVictimDistSqr) : NULL)
  409. {
  410. if ( curVictim != object)
  411. {
  412. if ( ! (curVictim->isKindOf( KINDOF_INFANTRY) || curVictim->isKindOf( KINDOF_VEHICLE ) ) ) // both commuters and pedestrians
  413. continue;
  414. if ( curVictim->getRelationship( object ) != ENEMIES ) // only enemies
  415. continue;
  416. //Disable the target for a specified amount of time.
  417. curVictim->setDisabledUntil( DISABLED_EMP, TheGameLogic->getFrame() + data->m_disabledDuration );
  418. }
  419. }
  420. }
  421. // ------------------------------------------------------------------------------------------------
  422. /** CRC */
  423. // ------------------------------------------------------------------------------------------------
  424. void LeafletDropBehavior::crc( Xfer *xfer )
  425. {
  426. } // end crc
  427. // ------------------------------------------------------------------------------------------------
  428. /** Xfer method
  429. * Version Info:
  430. * 1: Initial version */
  431. // ------------------------------------------------------------------------------------------------
  432. void LeafletDropBehavior::xfer( Xfer *xfer )
  433. {
  434. // version
  435. XferVersion currentVersion = 1;
  436. XferVersion version = currentVersion;
  437. xfer->xferVersion( &version, currentVersion );
  438. xfer->xferUnsignedInt( &m_startFrame );
  439. } // end xfer
  440. // ------------------------------------------------------------------------------------------------
  441. /** Load post process */
  442. // ------------------------------------------------------------------------------------------------
  443. void LeafletDropBehavior::loadPostProcess( void )
  444. {
  445. } // end loadPostProcess