StealthDetectorUpdate.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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: StealthDetectorUpdate.cpp ////////////////////////////////////////////////////////////////////////
  24. // Author: Kris Morness, May 2002
  25. // Desc: An update that checks for a status bit to stealth the owning object
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #define DEFINE_STEALTHLEVEL_NAMES
  29. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  30. #include "Common/MiscAudio.h"
  31. #include "Common/Radar.h"
  32. #include "Common/ThingTemplate.h"
  33. #include "Common/Xfer.h"
  34. #include "GameClient/Drawable.h"
  35. #include "GameClient/Eva.h"
  36. #include "GameClient/GameText.h"
  37. #include "GameClient/InGameUI.h"
  38. #include "GameClient/ParticleSys.h"
  39. #include "GameLogic/Damage.h"
  40. #include "GameLogic/Object.h"
  41. #include "GameLogic/PartitionManager.h"
  42. #include "GameLogic/Module/ContainModule.h"
  43. #include "GameLogic/Module/StealthUpdate.h"
  44. #include "GameLogic/Module/StealthDetectorUpdate.h"
  45. #include "Common/BitFlagsIO.h"
  46. #include "Common/PlayerList.h"
  47. #include "Common/Player.h"
  48. #ifdef _INTERNAL
  49. // for occasional debugging...
  50. //#pragma optimize("", off)
  51. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  52. #endif
  53. //-------------------------------------------------------------------------------------------------
  54. void StealthDetectorUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  55. {
  56. UpdateModuleData::buildFieldParse(p);
  57. static const FieldParse dataFieldParse[] =
  58. {
  59. { "DetectionRate", INI::parseDurationUnsignedInt, NULL, offsetof( StealthDetectorUpdateModuleData, m_updateRate ) },
  60. { "DetectionRange", INI::parseReal, NULL, offsetof( StealthDetectorUpdateModuleData, m_detectionRange ) },
  61. { "InitiallyDisabled", INI::parseBool, NULL, offsetof( StealthDetectorUpdateModuleData, m_initiallyDisabled ) },
  62. { "PingSound", INI::parseAudioEventRTS, NULL, offsetof( StealthDetectorUpdateModuleData, m_pingSound ) },
  63. { "LoudPingSound", INI::parseAudioEventRTS, NULL, offsetof( StealthDetectorUpdateModuleData, m_loudPingSound ) },
  64. { "IRBeaconParticleSysName", INI::parseParticleSystemTemplate, NULL, offsetof( StealthDetectorUpdateModuleData, m_IRBeaconParticleSysTmpl ) },
  65. { "IRParticleSysName", INI::parseParticleSystemTemplate, NULL, offsetof( StealthDetectorUpdateModuleData, m_IRParticleSysTmpl ) },
  66. { "IRBrightParticleSysName", INI::parseParticleSystemTemplate, NULL, offsetof( StealthDetectorUpdateModuleData, m_IRBrightParticleSysTmpl ) },
  67. { "IRGridParticleSysName", INI::parseParticleSystemTemplate, NULL, offsetof( StealthDetectorUpdateModuleData, m_IRGridParticleSysTmpl ) },
  68. { "IRParticleSysBone", INI::parseAsciiString, NULL, offsetof( StealthDetectorUpdateModuleData, m_IRParticleSysBone ) },
  69. { "ExtraRequiredKindOf", KindOfMaskType::parseFromINI, NULL, offsetof( StealthDetectorUpdateModuleData, m_extraDetectKindof ) },
  70. { "ExtraForbiddenKindOf", KindOfMaskType::parseFromINI, NULL, offsetof( StealthDetectorUpdateModuleData, m_extraDetectKindofNot ) },
  71. { "CanDetectWhileGarrisoned", INI::parseBool, NULL, offsetof( StealthDetectorUpdateModuleData, m_canDetectWhileGarrisoned ) },
  72. { "CanDetectWhileContained", INI::parseBool, NULL, offsetof( StealthDetectorUpdateModuleData, m_canDetectWhileTransported ) },
  73. { 0, 0, 0, 0 }
  74. };
  75. p.add(dataFieldParse);
  76. }
  77. //-------------------------------------------------------------------------------------------------
  78. //-------------------------------------------------------------------------------------------------
  79. StealthDetectorUpdate::StealthDetectorUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  80. {
  81. const StealthDetectorUpdateModuleData *data = getStealthDetectorUpdateModuleData();
  82. m_enabled = !data->m_initiallyDisabled;
  83. // start these guys with random phasings so that we don't
  84. // have all of 'em check on the same frame.
  85. setWakeFrame(getObject(), m_enabled ? UPDATE_SLEEP(GameLogicRandomValue(1, data->m_updateRate)) : UPDATE_SLEEP_FOREVER);
  86. }
  87. //-------------------------------------------------------------------------------------------------
  88. //-------------------------------------------------------------------------------------------------
  89. StealthDetectorUpdate::~StealthDetectorUpdate( void )
  90. {
  91. }
  92. //-------------------------------------------------------------------------------------------------
  93. //-------------------------------------------------------------------------------------------------
  94. void StealthDetectorUpdate::setSDEnabled( Bool enabled )
  95. {
  96. m_enabled = enabled;
  97. setWakeFrame(getObject(), m_enabled ? UPDATE_SLEEP_NONE : UPDATE_SLEEP_FOREVER);
  98. }
  99. //-------------------------------------------------------------------------------------------------
  100. //-------------------------------------------------------------------------------------------------
  101. class PartitionFilterStealthedOrStealthGarrisoned : public PartitionFilter
  102. {
  103. public:
  104. PartitionFilterStealthedOrStealthGarrisoned() { }
  105. virtual Bool allow(Object *objOther);
  106. #if defined(_DEBUG) || defined(_INTERNAL)
  107. virtual const char* debugGetName() { return "PartitionFilterStealthedOrStealthGarrisoned"; }
  108. #endif
  109. };
  110. Bool PartitionFilterStealthedOrStealthGarrisoned::allow( Object *objOther)
  111. {
  112. if( ! objOther )
  113. return FALSE;
  114. if( objOther->getStatusBits().test( OBJECT_STATUS_STEALTHED ) )
  115. return TRUE;
  116. ContainModuleInterface *contain = objOther->getContain();
  117. if( contain && contain->isGarrisonable() && contain->getStealthUnitsContained() )
  118. return TRUE;
  119. return FALSE;
  120. }
  121. //-------------------------------------------------------------------------------------------------
  122. /** The update callback. */
  123. //-------------------------------------------------------------------------------------------------
  124. UpdateSleepTime StealthDetectorUpdate::update( void )
  125. {
  126. const StealthDetectorUpdateModuleData *data = getStealthDetectorUpdateModuleData();
  127. Object* self = getObject();
  128. if (self->isEffectivelyDead())
  129. return UPDATE_SLEEP_FOREVER;
  130. // We have to wait until we are fully constructed, but we will detect the moment we finish
  131. if( self->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION) )
  132. return UPDATE_SLEEP_NONE;
  133. // We turn off forever the moment we are sold.
  134. if( self->testStatus(OBJECT_STATUS_SOLD) )
  135. return UPDATE_SLEEP_FOREVER;
  136. //Are we contained by anything?
  137. Object *containedBy = self->getContainedBy();
  138. if( containedBy )
  139. {
  140. ContainModuleInterface *contain = containedBy->getContain();
  141. if( contain )
  142. {
  143. //Are we eligible to detect stealth while in a container?
  144. if( contain->isGarrisonable() )
  145. {
  146. //We are in a garrisonable structure.
  147. if( !data->m_canDetectWhileGarrisoned )
  148. {
  149. //But we can't detect stuff while inside.
  150. return UPDATE_SLEEP(data->m_updateRate);
  151. }
  152. }
  153. else if( !data->m_canDetectWhileTransported )
  154. {
  155. //We are in a normal container and can't detect!
  156. return UPDATE_SLEEP(data->m_updateRate);
  157. }
  158. }
  159. }
  160. // only consider items that are currently stealthed.
  161. PartitionFilterStealthedOrStealthGarrisoned filterStealthOrStealthGarrisoned;
  162. //PartitionFilterAcceptByObjectStatus filterStatus(OBJECT_STATUS_STEALTHED, 0);
  163. PartitionFilterRelationship filterTeam(self, PartitionFilterRelationship::ALLOW_ENEMIES | PartitionFilterRelationship::ALLOW_NEUTRAL );
  164. PartitionFilterAcceptByKindOf filterKindof(data->m_extraDetectKindof, data->m_extraDetectKindofNot);
  165. PartitionFilterSameMapStatus filterMapStatus(getObject());
  166. PartitionFilter* filters[] = { &filterStealthOrStealthGarrisoned, &filterTeam, &filterKindof, &filterMapStatus, NULL };
  167. Real visionRange = self->getVisionRange();
  168. if( data->m_detectionRange > 0.0f )
  169. {
  170. visionRange = data->m_detectionRange;
  171. }
  172. Bool foundSomeone = FALSE;
  173. SimpleObjectIterator *iter = ThePartitionManager->iterateObjectsInRange(
  174. self, visionRange, FROM_CENTER_2D, filters);
  175. MemoryPoolObjectHolder hold(iter);
  176. for (Object *them = iter->first(); them; them = iter->next())
  177. {
  178. if ( them->isEffectivelyDead() )
  179. continue;
  180. StealthUpdate* stealth = them->getStealth();
  181. if ( stealth )
  182. {
  183. // we have found someone
  184. foundSomeone = TRUE;
  185. //
  186. // if this object was not previously detected it is now being revealed and we
  187. // want to do some UI feedback
  188. //
  189. if( them->testStatus( OBJECT_STATUS_DETECTED ) == FALSE )
  190. {
  191. // for the player revealing the stealth unit do some UI feedback
  192. if( ThePlayerList->getLocalPlayer() == self->getControllingPlayer() &&
  193. self->getRelationship( them ) != ALLIES )
  194. {
  195. Bool doFeedback = TRUE;
  196. //Kris: Aug 18, 2003 -- This whole system is no longer viable with the inclusion of so many
  197. //stealth units. It's possible to get spammed hundreds of times in a row in MD_GLA02 because
  198. //we're blindly creating radar events for each detection (unless they are mines). The best thing
  199. //to do is tryEvent for everything...
  200. doFeedback = TheRadar->tryEvent( RADAR_EVENT_STEALTH_DISCOVERED, them->getPosition() );
  201. //OLD CODE:
  202. /*
  203. // do a radar event, for mines we only make events if there weren't other
  204. // mine events within close proximity and time to other mines
  205. //
  206. if( them->isKindOf( KINDOF_MINE ) || them->isKindOf( KINDOF_BOOBY_TRAP ) || them->isKindOf( KINDOF_DEMOTRAP ) )
  207. doFeedback = TheRadar->tryEvent( RADAR_EVENT_STEALTH_DISCOVERED, them->getPosition() );
  208. else
  209. TheRadar->createEvent( them->getPosition(), RADAR_EVENT_STEALTH_DISCOVERED );
  210. */
  211. // do audio and UI message if we need to do feedback
  212. if( doFeedback )
  213. {
  214. // audio msg
  215. static AudioEventRTS discoveredSound = TheAudio->getMiscAudio()->m_stealthDiscoveredSound;
  216. discoveredSound.setPlayerIndex( self->getControllingPlayer()->getPlayerIndex() );
  217. TheAudio->addAudioEvent( &discoveredSound );
  218. // ui msg
  219. TheInGameUI->message( TheGameText->fetch( "MESSAGE:StealthDiscovered" ) );
  220. // If revealing this unit is suppose to cause an Eva event, do it
  221. EvaMessage message = stealth->getEnemyDetectionEvaEvent();
  222. if ( message != EVA_Invalid && TheEva != NULL )
  223. {
  224. TheEva->setShouldPlay( message );
  225. }
  226. } // end if
  227. } // end if
  228. // for the unit being revealed, do some UI feedback
  229. if( ThePlayerList->getLocalPlayer() == them->getControllingPlayer() &&
  230. self->getRelationship( them ) != ALLIES )
  231. {
  232. Bool doFeedback = TRUE;
  233. //
  234. // do a radar event, for mines we only make events if there weren't other
  235. // mine events within close proximity and time to other mines
  236. //
  237. if( them->isKindOf( KINDOF_MINE ) || them->isKindOf( KINDOF_BOOBY_TRAP ) || them->isKindOf( KINDOF_DEMOTRAP ) )
  238. doFeedback = TheRadar->tryEvent( RADAR_EVENT_STEALTH_NEUTRALIZED, them->getPosition() );
  239. else
  240. TheRadar->createEvent( them->getPosition(), RADAR_EVENT_STEALTH_NEUTRALIZED );
  241. // do audio and UI message if we need to do feedback
  242. if( doFeedback )
  243. {
  244. // audio msg
  245. static AudioEventRTS neutralizedSound = TheAudio->getMiscAudio()->m_stealthNeutralizedSound;
  246. neutralizedSound.setPlayerIndex( them->getControllingPlayer()->getPlayerIndex() );
  247. TheAudio->addAudioEvent( &neutralizedSound );
  248. // ui msg
  249. TheInGameUI->message( TheGameText->fetch( "MESSAGE:StealthNeutralized" ) );
  250. // If revealing this unit is suppose to cause an Eva event, do it
  251. EvaMessage message = stealth->getOwnDetectionEvaEvent();
  252. if ( message != EVA_Invalid && TheEva != NULL )
  253. {
  254. TheEva->setShouldPlay( message );
  255. }
  256. } // end if
  257. } // end if
  258. } // end if, them was not previously detected
  259. // updateRate PLUS 1 is necessary to ensure it stays detected 'till we are called again...
  260. stealth->markAsDetected(data->m_updateRate + 1);
  261. /** @todo srj -- evil hack here... this whole heat-vision thing is fucked.
  262. don't want it on mines but no good way to do that. hack for now. */
  263. Drawable *theirDraw = them->getDrawable();
  264. if ( theirDraw && !them->isKindOf(KINDOF_MINE))
  265. {
  266. theirDraw->setSecondMaterialPassOpacity( 1.0f );
  267. }
  268. if (data->m_IRGridParticleSysTmpl)
  269. {
  270. const ParticleSystemTemplate *gridTemplate = data->m_IRGridParticleSysTmpl;
  271. if (gridTemplate)
  272. {
  273. ParticleSystem *sys = TheParticleSystemManager->createParticleSystem( gridTemplate );//GRID
  274. if (sys)
  275. {
  276. Coord3D gridPosition = *them->getPosition();
  277. gridPosition.z = self->getPosition()->z + 17;
  278. gridPosition.x -= ((Int)gridPosition.x)%12;
  279. gridPosition.y -= ((Int)gridPosition.y)%12;
  280. sys->setPosition( &gridPosition );
  281. }
  282. }
  283. }
  284. }//end if them has stealthupdate
  285. else // perhaps they are garrisoning something stealthy, eh?
  286. {
  287. ContainModuleInterface *contain = them->getContain();
  288. if( contain && contain->isGarrisonable() && contain->getStealthUnitsContained() )
  289. {
  290. Object* rider = NULL;
  291. for(ContainedItemsList::const_iterator it = contain->getContainedItemsList()->begin(); it != contain->getContainedItemsList()->end(); ++it)
  292. {
  293. rider = *it;
  294. StealthUpdate *stealth = rider->getStealth();
  295. if ( stealth )
  296. {
  297. // we have found someone
  298. foundSomeone = TRUE;
  299. if( self->getControllingPlayer() != rider->getControllingPlayer() && self->getRelationship( rider ) != ALLIES )
  300. {
  301. stealth->markAsDetected(data->m_updateRate + 2);
  302. }
  303. }
  304. }
  305. }
  306. }
  307. }
  308. const Player *localPlayer = ThePlayerList->getLocalPlayer();
  309. //Make sure the detector is visible to the local player before we add effects or sounds.
  310. if ( self->getShroudedStatus( localPlayer->getPlayerIndex() ) <= OBJECTSHROUD_PARTIAL_CLEAR )
  311. {
  312. if ( self->testStatus( OBJECT_STATUS_STEALTHED ) == FALSE || self->getControllingPlayer() == localPlayer )
  313. {
  314. Drawable *myDraw = self->getDrawable();
  315. Coord3D bonePosition = {-1.66f,5.5f,15};
  316. if (myDraw)
  317. myDraw->getPristineBonePositions( data->m_IRParticleSysBone.str(), 0, &bonePosition, NULL, 1);
  318. const ParticleSystemTemplate *pingTemplate;
  319. if ( foundSomeone )
  320. pingTemplate = data->m_IRBrightParticleSysTmpl;
  321. else
  322. pingTemplate = data->m_IRParticleSysTmpl;
  323. if (pingTemplate)
  324. {
  325. ParticleSystem *sys = TheParticleSystemManager->createParticleSystem( pingTemplate );
  326. if (sys)
  327. {
  328. if (myDraw)
  329. sys->attachToDrawable( myDraw );
  330. else
  331. sys->attachToObject( self );
  332. sys->setPosition( &bonePosition );
  333. }
  334. }
  335. const ParticleSystemTemplate *beaconTemplate = data->m_IRBeaconParticleSysTmpl;
  336. if (beaconTemplate)
  337. {
  338. ParticleSystem *sys = TheParticleSystemManager->createParticleSystem( beaconTemplate );//BEACON
  339. if (sys)
  340. {
  341. if (myDraw)
  342. sys->attachToDrawable( myDraw );
  343. else
  344. sys->attachToObject( self );
  345. sys->setPosition( &bonePosition );
  346. }
  347. }
  348. AudioEventRTS IRPingSound;
  349. if (foundSomeone)
  350. IRPingSound = data->m_loudPingSound;
  351. else
  352. IRPingSound = data->m_pingSound;
  353. IRPingSound.setObjectID( self->getID() );
  354. TheAudio->addAudioEvent(&IRPingSound);
  355. }
  356. } // end if doIRFX
  357. return UPDATE_SLEEP(data->m_updateRate);
  358. }
  359. // ------------------------------------------------------------------------------------------------
  360. /** CRC */
  361. // ------------------------------------------------------------------------------------------------
  362. void StealthDetectorUpdate::crc( Xfer *xfer )
  363. {
  364. // extend base class
  365. UpdateModule::crc( xfer );
  366. } // end crc
  367. // ------------------------------------------------------------------------------------------------
  368. /** Xfer method
  369. * Version Info:
  370. * 1: Initial version */
  371. // ------------------------------------------------------------------------------------------------
  372. void StealthDetectorUpdate::xfer( Xfer *xfer )
  373. {
  374. // version
  375. XferVersion currentVersion = 1;
  376. XferVersion version = currentVersion;
  377. xfer->xferVersion( &version, currentVersion );
  378. // extend base class
  379. UpdateModule::xfer( xfer );
  380. // enabled
  381. xfer->xferBool( &m_enabled );
  382. } // end xfer
  383. // ------------------------------------------------------------------------------------------------
  384. /** Load post process */
  385. // ------------------------------------------------------------------------------------------------
  386. void StealthDetectorUpdate::loadPostProcess( void )
  387. {
  388. // extend base class
  389. UpdateModule::loadPostProcess();
  390. } // end loadPostProcess