GrantStealthBehavior.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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: GrantStealthBehavior.cpp ///////////////////////////////////////////////////////////////////////
  24. // Author: Lorenzen
  25. ///////////////////////////////////////////////////////////////////////////////////////////////////
  26. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #include "Common/Thing.h"
  29. #include "Common/ThingTemplate.h"
  30. #include "Common/INI.h"
  31. #include "Common/Player.h"
  32. #include "Common/Xfer.h"
  33. #include "GameClient/ParticleSys.h"
  34. #include "GameClient/Anim2D.h"
  35. #include "GameClient/InGameUI.h"
  36. #include "GameLogic/Module/ContainModule.h"
  37. #include "GameLogic/Module/GrantStealthBehavior.h"
  38. #include "GameLogic/Module/StealthUpdate.h"
  39. #include "GameLogic/Module/BodyModule.h"
  40. #include "GameLogic/GameLogic.h"
  41. #include "GameLogic/Object.h"
  42. #include "GameLogic/PartitionManager.h"
  43. #ifdef _INTERNAL
  44. // for occasional debugging...
  45. //#pragma optimize("", off)
  46. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  47. #endif
  48. //-------------------------------------------------------------------------------------------------
  49. //-------------------------------------------------------------------------------------------------
  50. struct GrantStealthPlayerScanHelper
  51. {
  52. KindOfMaskType m_kindOfToTest;
  53. Object *m_theGrantor;
  54. ObjectPointerList *m_objectList;
  55. };
  56. static void checkForGrantStealth( Object *testObj, void *userData )
  57. {
  58. GrantStealthPlayerScanHelper *helper = (GrantStealthPlayerScanHelper*)userData;
  59. ObjectPointerList *listToAddTo = helper->m_objectList;
  60. if( testObj->isEffectivelyDead() )
  61. return;
  62. if( testObj->getControllingPlayer() != helper->m_theGrantor->getControllingPlayer() )
  63. return;
  64. if( testObj->isOffMap() )
  65. return;
  66. if( !testObj->isAnyKindOf(helper->m_kindOfToTest) )
  67. return;
  68. listToAddTo->push_back(testObj);
  69. if( testObj->getContain() )
  70. {
  71. // have to tag visible riders too, or they will float around and look silly.
  72. Object *rider = (Object*)testObj->getContain()->friend_getRider();
  73. if( rider )
  74. {
  75. listToAddTo->push_back(rider);
  76. }
  77. }
  78. }
  79. //-------------------------------------------------------------------------------------------------
  80. //-------------------------------------------------------------------------------------------------
  81. GrantStealthBehavior::GrantStealthBehavior( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  82. {
  83. const GrantStealthBehaviorModuleData *d = getGrantStealthBehaviorModuleData();
  84. m_radiusParticleSystemID = INVALID_PARTICLE_SYSTEM_ID;
  85. m_currentScanRadius = d->m_startRadius;
  86. Object *obj = getObject();
  87. {
  88. if( d->m_radiusParticleSystemTmpl )
  89. {
  90. ParticleSystem *particleSystem;
  91. particleSystem = TheParticleSystemManager->createParticleSystem( d->m_radiusParticleSystemTmpl );
  92. if( particleSystem )
  93. {
  94. particleSystem->setPosition( obj->getPosition() );
  95. m_radiusParticleSystemID = particleSystem->getSystemID();
  96. }
  97. }
  98. }
  99. setWakeFrame( getObject(), UPDATE_SLEEP_NONE );
  100. }
  101. //-------------------------------------------------------------------------------------------------
  102. //-------------------------------------------------------------------------------------------------
  103. GrantStealthBehavior::~GrantStealthBehavior( void )
  104. {
  105. if( m_radiusParticleSystemID != INVALID_PARTICLE_SYSTEM_ID )
  106. TheParticleSystemManager->destroyParticleSystemByID( m_radiusParticleSystemID );
  107. }
  108. //-------------------------------------------------------------------------------------------------
  109. /** The update callback. */
  110. //-------------------------------------------------------------------------------------------------
  111. UpdateSleepTime GrantStealthBehavior::update( void )
  112. {
  113. Object *self = getObject();
  114. if ( self->isEffectivelyDead())
  115. return UPDATE_SLEEP_FOREVER;
  116. const GrantStealthBehaviorModuleData *d = getGrantStealthBehaviorModuleData();
  117. // setup scan filters
  118. PartitionFilterRelationship relationship( self, PartitionFilterRelationship::ALLOW_ALLIES );
  119. PartitionFilterSameMapStatus filterMapStatus( self );
  120. PartitionFilterAlive filterAlive;
  121. PartitionFilter *filters[] = { &relationship, &filterAlive, &filterMapStatus, NULL };
  122. m_currentScanRadius += d->m_radiusGrowRate;
  123. Bool thisIsFinalScan = FALSE;
  124. if ( m_currentScanRadius >= d->m_finalRadius )
  125. {
  126. m_currentScanRadius = d->m_finalRadius;
  127. thisIsFinalScan = TRUE;
  128. }
  129. // scan objects in our region
  130. ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( self->getPosition(), m_currentScanRadius, FROM_CENTER_2D, filters );
  131. MemoryPoolObjectHolder hold( iter );
  132. // GRANT STEALTH TO FRIENDLIES IN RADIUS
  133. for( Object *obj = iter->first(); obj; obj = iter->next() )
  134. grantStealthToObject( obj );
  135. if ( thisIsFinalScan )
  136. {
  137. TheGameLogic->destroyObject( self );
  138. return UPDATE_SLEEP_FOREVER;
  139. }
  140. return UPDATE_SLEEP_NONE;
  141. }
  142. //-------------------------------------------------------------------------------------------------
  143. //-------------------------------------------------------------------------------------------------
  144. void GrantStealthBehavior::grantStealthToObject( Object *obj )
  145. {
  146. if ( obj == getObject() )
  147. return;
  148. const GrantStealthBehaviorModuleData *d = getGrantStealthBehaviorModuleData();
  149. if ( ! obj->isAnyKindOf( d->m_kindOf ) )
  150. return;
  151. StealthUpdate* stealth = obj->getStealth();
  152. if( stealth )
  153. {
  154. stealth->receiveGrant();
  155. Drawable *draw = obj->getDrawable();
  156. if ( draw )
  157. {
  158. draw->flashAsSelected();
  159. }
  160. }
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. /** CRC */
  164. // ------------------------------------------------------------------------------------------------
  165. void GrantStealthBehavior::crc( Xfer *xfer )
  166. {
  167. // extend base class
  168. UpdateModule::crc( xfer );
  169. } // end crc
  170. // ------------------------------------------------------------------------------------------------
  171. /** Xfer method
  172. * Version Info:
  173. * 1: Initial version */
  174. // ------------------------------------------------------------------------------------------------
  175. void GrantStealthBehavior::xfer( Xfer *xfer )
  176. {
  177. // version
  178. XferVersion currentVersion = 1;
  179. XferVersion version = currentVersion;
  180. xfer->xferVersion( &version, currentVersion );
  181. // extend base class
  182. UpdateModule::xfer( xfer );
  183. // particle system id
  184. xfer->xferUser( &m_radiusParticleSystemID, sizeof( ParticleSystemID ) );
  185. // Timer safety
  186. xfer->xferReal( &m_currentScanRadius );
  187. } // end xfer
  188. // ------------------------------------------------------------------------------------------------
  189. /** Load post process */
  190. // ------------------------------------------------------------------------------------------------
  191. void GrantStealthBehavior::loadPostProcess( void )
  192. {
  193. // extend base class
  194. UpdateModule::loadPostProcess();
  195. } // end loadPostProcess