StealthDetectorUpdate.cpp 16 KB

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