SpyVisionUpdate.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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: SpyVisionUpdate.cpp /////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, September 2002
  25. // Desc: Special Power will spy on the vision of all enemy players.
  26. // Putting a SpecialPower in a behavior takes a big huge amount of code duplication and
  27. // has no precedent.
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////
  29. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  30. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  31. #include "Common/Player.h"
  32. #include "Common/PlayerList.h"
  33. #include "Common/Xfer.h"
  34. #include "GameLogic/GameLogic.h"
  35. #include "GameLogic/Module/SpyVisionUpdate.h"
  36. #ifdef _INTERNAL
  37. // for occasional debugging...
  38. //#pragma optimize("", off)
  39. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  40. #endif
  41. //-------------------------------------------------------------------------------------------------
  42. //-------------------------------------------------------------------------------------------------
  43. SpyVisionUpdate::SpyVisionUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  44. {
  45. m_deactivateFrame = 0;
  46. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  47. m_currentlyActive = FALSE;
  48. m_resetTimersNextUpdate = FALSE;
  49. m_disabledUntilFrame = 0;
  50. }
  51. //-------------------------------------------------------------------------------------------------
  52. //-------------------------------------------------------------------------------------------------
  53. SpyVisionUpdate::~SpyVisionUpdate( void )
  54. {
  55. }
  56. //-------------------------------------------------------------------------------------------------
  57. //-------------------------------------------------------------------------------------------------
  58. void SpyVisionUpdate::activateSpyVision( UnsignedInt duration )
  59. {
  60. UnsignedInt now = TheGameLogic->getFrame();
  61. if( duration == 0 )
  62. m_deactivateFrame = UINT_MAX;
  63. else
  64. m_deactivateFrame = now + duration;
  65. doActivationWork( getObject()->getControllingPlayer(), TRUE );
  66. if( duration == 0 )
  67. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  68. else
  69. setWakeFrame( getObject(), UPDATE_SLEEP(duration) );
  70. }
  71. //-------------------------------------------------------------------------------------------------
  72. //-------------------------------------------------------------------------------------------------
  73. void SpyVisionUpdate::setDisabledUntilFrame( UnsignedInt frame )
  74. {
  75. UnsignedInt now = TheGameLogic->getFrame();
  76. if( frame > now )
  77. {
  78. //Turn off the spy vision now since we are disabled
  79. if( m_currentlyActive )
  80. {
  81. doActivationWork( getObject()->getControllingPlayer(), FALSE );
  82. }
  83. //When should we wake up?
  84. m_disabledUntilFrame = frame;
  85. //Mark it so we can turn it on again on next update.
  86. m_resetTimersNextUpdate = TRUE;
  87. //Now sleep until the disabled has expired. If it's expired again when we come back due to another
  88. //sabotage or something else... we'll do the same thing over again.
  89. setWakeFrame( getObject(), (UpdateSleepTime)(m_disabledUntilFrame - now) );
  90. }
  91. else
  92. {
  93. // Else it is a wakeup message. Update does the turning on.
  94. m_disabledUntilFrame = now;
  95. m_resetTimersNextUpdate = TRUE;
  96. setWakeFrame(getObject(), UPDATE_SLEEP_NONE);
  97. }
  98. }
  99. //-------------------------------------------------------------------------------------------------
  100. //-------------------------------------------------------------------------------------------------
  101. void SpyVisionUpdate::onCapture( Player *oldOwner, Player *newOwner )
  102. {
  103. if( m_currentlyActive )
  104. {
  105. // If on, flick the switch real fast to get everything to update for the new owner
  106. doActivationWork( oldOwner, FALSE );
  107. doActivationWork( newOwner, TRUE );
  108. }
  109. }
  110. //-------------------------------------------------------------------------------------------------
  111. //-------------------------------------------------------------------------------------------------
  112. void SpyVisionUpdate::onDisabledEdge( Bool nowDisabled )
  113. {
  114. if( nowDisabled )
  115. setDisabledUntilFrame(UINT_MAX);
  116. else
  117. setDisabledUntilFrame(0);
  118. }
  119. //-------------------------------------------------------------------------------------------------
  120. //-------------------------------------------------------------------------------------------------
  121. UpdateSleepTime SpyVisionUpdate::update( void )
  122. {
  123. const SpyVisionUpdateModuleData *data = getSpyVisionUpdateModuleData();
  124. UnsignedInt now = TheGameLogic->getFrame();
  125. //Once disable has complete and we are waking up from it, then reset the interval so
  126. //it has to wait before going on again. If it doesn't have an interval, we turn it on
  127. //immediately, but only for self powered objects.
  128. if( m_resetTimersNextUpdate )
  129. {
  130. m_resetTimersNextUpdate = FALSE;
  131. if( data->m_selfPowered )
  132. {
  133. if( data->m_selfPoweredInterval == 0 )
  134. {
  135. //It's an always on self-powered special. So turn it back on now.
  136. doActivationWork( getObject()->getControllingPlayer(), TRUE );
  137. return UPDATE_SLEEP( UPDATE_SLEEP_FOREVER );
  138. }
  139. else
  140. {
  141. //Reset the interval timer via sleeping (so we have to wait a longer time before going on)
  142. return UPDATE_SLEEP( data->m_selfPoweredInterval );
  143. }
  144. }
  145. }
  146. if( m_currentlyActive && (m_deactivateFrame <= TheGameLogic->getFrame()) )
  147. {
  148. // Turn off SpyVision.
  149. doActivationWork( getObject()->getControllingPlayer(), FALSE );
  150. m_deactivateFrame = 0;
  151. }
  152. else if( !m_currentlyActive && data->m_selfPowered )
  153. {
  154. doActivationWork( getObject()->getControllingPlayer(), TRUE );
  155. if( data->m_selfPoweredDuration == 0 )
  156. m_deactivateFrame = UINT_MAX;
  157. else
  158. m_deactivateFrame = now + data->m_selfPoweredDuration;
  159. }
  160. if( data->m_selfPowered )
  161. {
  162. if( m_currentlyActive )
  163. return UPDATE_SLEEP(data->m_selfPoweredDuration);
  164. else
  165. return UPDATE_SLEEP(data->m_selfPoweredInterval);
  166. }
  167. return UPDATE_SLEEP_FOREVER;
  168. }
  169. //-------------------------------------------------------------------------------------------------
  170. void SpyVisionUpdate::doActivationWork( Player *playerToSetFor, Bool setting )
  171. {
  172. const SpyVisionUpdateModuleData *data = getSpyVisionUpdateModuleData();
  173. if( playerToSetFor == NULL || ThePlayerList == NULL )
  174. return;
  175. for (Int i=0; i < ThePlayerList->getPlayerCount(); ++i)
  176. {
  177. Player *player = ThePlayerList->getNthPlayer(i);
  178. if( playerToSetFor->getRelationship(player->getDefaultTeam()) == ENEMIES )
  179. {
  180. player->setUnitsVisionSpied( setting, data->m_spyOnKindof, playerToSetFor->getPlayerIndex() );
  181. }
  182. }
  183. m_currentlyActive = setting;
  184. }
  185. //-------------------------------------------------------------------------------------------------
  186. void SpyVisionUpdate::onDelete( void )
  187. {
  188. // If I was left on at the time of death, then turn me off.
  189. if( m_currentlyActive )
  190. {
  191. doActivationWork( getObject()->getControllingPlayer(), FALSE );
  192. }
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. void SpyVisionUpdate::upgradeImplementation()
  196. {
  197. const SpyVisionUpdateModuleData *data = getSpyVisionUpdateModuleData();
  198. if( data->m_needsUpgrade && !isAlreadyUpgraded() )
  199. {
  200. activateSpyVision(data->m_selfPoweredDuration);// If zero, will turn on permanently. And it does the wake up setting
  201. }
  202. }
  203. // ------------------------------------------------------------------------------------------------
  204. /** CRC */
  205. // ------------------------------------------------------------------------------------------------
  206. void SpyVisionUpdate::crc( Xfer *xfer )
  207. {
  208. // extend base class
  209. UpdateModule::crc( xfer );
  210. } // end crc
  211. // ------------------------------------------------------------------------------------------------
  212. /** Xfer method
  213. * Version Info:
  214. * 1: Initial version */
  215. // ------------------------------------------------------------------------------------------------
  216. void SpyVisionUpdate::xfer( Xfer *xfer )
  217. {
  218. // version
  219. XferVersion currentVersion = 2;
  220. XferVersion version = currentVersion;
  221. xfer->xferVersion( &version, currentVersion );
  222. // extend base class
  223. UpdateModule::xfer( xfer );
  224. // deactivate frame
  225. xfer->xferUnsignedInt( &m_deactivateFrame );
  226. xfer->xferBool( &m_currentlyActive );
  227. if( version >= 2 )
  228. {
  229. xfer->xferBool( &m_resetTimersNextUpdate );
  230. xfer->xferUnsignedInt( &m_disabledUntilFrame );
  231. }
  232. } // end xfer
  233. // ------------------------------------------------------------------------------------------------
  234. /** Load post process */
  235. // ------------------------------------------------------------------------------------------------
  236. void SpyVisionUpdate::loadPostProcess( void )
  237. {
  238. // extend base class
  239. UpdateModule::loadPostProcess();
  240. } // end loadPostProcess