MissileLauncherBuildingUpdate.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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: MissileLauncherBuildingUpdate.cpp /////////////////////////////////////////////////////////////////////////
  24. // Author: Matthew D. Campbell, April 2002
  25. // Desc: Update will change model state conditions based on special power state
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #include "Common/GameAudio.h"
  29. #include "Common/GlobalData.h"
  30. #include "Common/Player.h"
  31. #include "Common/SpecialPower.h"
  32. #include "Common/Xfer.h"
  33. #include "GameLogic/GameLogic.h"
  34. #include "GameLogic/Object.h"
  35. #include "GameLogic/Module/AIUpdate.h"
  36. #include "GameLogic/Module/MissileLauncherBuildingUpdate.h"
  37. #include "GameLogic/Module/SpecialPowerModule.h"
  38. #include "GameClient/Drawable.h"
  39. #include "GameClient/FXList.h"
  40. #ifdef _INTERNAL
  41. // for occasional debugging...
  42. //#pragma optimize("", off)
  43. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  44. #endif
  45. //-------------------------------------------------------------------------------------------------
  46. MissileLauncherBuildingUpdate::MissileLauncherBuildingUpdate( Thing *thing, const ModuleData* moduleData ) : SpecialPowerUpdateModule( thing, moduleData )
  47. {
  48. m_doorState = DOOR_CLOSED;
  49. m_timeoutState = DOOR_CLOSED;
  50. m_timeoutFrame = 0;
  51. m_openIdleAudio = getMissileLauncherBuildingUpdateModuleData()->m_openIdleAudio;
  52. m_openIdleAudio.setObjectID(getObject()->getID());
  53. }
  54. //-------------------------------------------------------------------------------------------------
  55. MissileLauncherBuildingUpdate::~MissileLauncherBuildingUpdate( void )
  56. {
  57. if (m_openIdleAudio.isCurrentlyPlaying())
  58. {
  59. TheAudio->removeAudioEvent(m_openIdleAudio.getPlayingHandle());
  60. }
  61. }
  62. //-------------------------------------------------------------------------------------------------
  63. void MissileLauncherBuildingUpdate::switchToState(DoorStateType dst)
  64. {
  65. if (m_doorState == dst)
  66. return;
  67. const MissileLauncherBuildingUpdateModuleData* d = getMissileLauncherBuildingUpdateModuleData();
  68. ModelConditionFlags clr, set;
  69. UnsignedInt now = TheGameLogic->getFrame();
  70. switch (dst)
  71. {
  72. case DOOR_CLOSED:
  73. DEBUG_LOG(("switch to state DOOR_CLOSED at %d\n",now));
  74. /// @todo srj -- for now, this assumes at most one door
  75. clr.set(MODELCONDITION_DOOR_1_WAITING_TO_CLOSE);
  76. clr.set(MODELCONDITION_DOOR_1_CLOSING);
  77. clr.set(MODELCONDITION_DOOR_1_OPENING);
  78. clr.set(MODELCONDITION_DOOR_1_WAITING_OPEN);
  79. m_timeoutFrame = 0;
  80. m_timeoutState = DOOR_CLOSED;
  81. if (d->m_closedFX)
  82. {
  83. const Coord3D *pos = getObject()->getPosition();
  84. FXList::doFXPos(d->m_closedFX, pos);
  85. }
  86. if (m_openIdleAudio.isCurrentlyPlaying())
  87. {
  88. TheAudio->removeAudioEvent(m_openIdleAudio.getPlayingHandle());
  89. }
  90. break;
  91. case DOOR_OPENING:
  92. DEBUG_LOG(("switch to state DOOR_OPENING at %d\n",now));
  93. /// @todo srj -- for now, this assumes at most one door
  94. clr.set(MODELCONDITION_DOOR_1_WAITING_TO_CLOSE);
  95. clr.set(MODELCONDITION_DOOR_1_CLOSING);
  96. clr.set(MODELCONDITION_DOOR_1_WAITING_OPEN);
  97. set.set(MODELCONDITION_DOOR_1_OPENING);
  98. // we want this to be done BEFORE the power is ready, so
  99. // end it one frame ahead.
  100. m_timeoutFrame = m_specialPowerModule->getReadyFrame() - 1;
  101. m_timeoutState = DOOR_OPEN;
  102. if (d->m_openingFX)
  103. {
  104. const Coord3D *pos = getObject()->getPosition();
  105. FXList::doFXPos(d->m_openingFX, pos);
  106. }
  107. if (m_openIdleAudio.isCurrentlyPlaying())
  108. {
  109. TheAudio->removeAudioEvent(m_openIdleAudio.getPlayingHandle());
  110. }
  111. break;
  112. case DOOR_OPEN:
  113. DEBUG_LOG(("switch to state DOOR_OPEN at %d\n",now));
  114. /// @todo srj -- for now, this assumes at most one door
  115. clr.set(MODELCONDITION_DOOR_1_WAITING_TO_CLOSE);
  116. clr.set(MODELCONDITION_DOOR_1_CLOSING);
  117. clr.set(MODELCONDITION_DOOR_1_OPENING);
  118. set.set(MODELCONDITION_DOOR_1_WAITING_OPEN);
  119. m_timeoutFrame = 0;
  120. m_timeoutState = DOOR_OPEN;
  121. if (d->m_openFX)
  122. {
  123. const Coord3D *pos = getObject()->getPosition();
  124. FXList::doFXPos(d->m_openFX, pos);
  125. }
  126. if (!m_openIdleAudio.isCurrentlyPlaying())
  127. {
  128. m_openIdleAudio.setPlayingHandle(TheAudio->addAudioEvent(&m_openIdleAudio));
  129. }
  130. break;
  131. case DOOR_WAITING_TO_CLOSE:
  132. DEBUG_LOG(("switch to state DOOR_WAITING_TO_CLOSE at %d\n",now));
  133. /// @todo srj -- for now, this assumes at most one door
  134. clr.set(MODELCONDITION_DOOR_1_CLOSING);
  135. clr.set(MODELCONDITION_DOOR_1_OPENING);
  136. clr.set(MODELCONDITION_DOOR_1_WAITING_OPEN);
  137. set.set(MODELCONDITION_DOOR_1_WAITING_TO_CLOSE);
  138. m_timeoutFrame = now + d->m_doorWaitOpenTime;
  139. m_timeoutState = DOOR_CLOSING;
  140. if (d->m_waitingToCloseFX)
  141. {
  142. const Coord3D *pos = getObject()->getPosition();
  143. FXList::doFXPos(d->m_waitingToCloseFX, pos);
  144. }
  145. if (m_openIdleAudio.isCurrentlyPlaying())
  146. {
  147. TheAudio->removeAudioEvent(m_openIdleAudio.getPlayingHandle());
  148. }
  149. break;
  150. case DOOR_CLOSING:
  151. DEBUG_LOG(("switch to state DOOR_CLOSING at %d\n",now));
  152. /// @todo srj -- for now, this assumes at most one door
  153. clr.set(MODELCONDITION_DOOR_1_WAITING_TO_CLOSE);
  154. clr.set(MODELCONDITION_DOOR_1_WAITING_OPEN);
  155. clr.set(MODELCONDITION_DOOR_1_OPENING);
  156. set.set(MODELCONDITION_DOOR_1_CLOSING);
  157. m_timeoutFrame = now + d->m_doorClosingTime;
  158. {
  159. Int delta = m_specialPowerModule->getReadyFrame() - now;
  160. if (m_timeoutFrame > now + delta/2)
  161. m_timeoutFrame = now + delta/2;
  162. }
  163. m_timeoutState = DOOR_CLOSED;
  164. if (d->m_closingFX)
  165. {
  166. const Coord3D *pos = getObject()->getPosition();
  167. FXList::doFXPos(d->m_closingFX, pos);
  168. }
  169. if (m_openIdleAudio.isCurrentlyPlaying())
  170. {
  171. TheAudio->removeAudioEvent(m_openIdleAudio.getPlayingHandle());
  172. }
  173. break;
  174. #ifdef _DEBUG
  175. default:
  176. DEBUG_CRASH(("unknown state"));
  177. break;
  178. #endif
  179. }
  180. m_doorState = dst;
  181. getObject()->clearAndSetModelConditionFlags(clr, set);
  182. if (m_timeoutFrame > 0)
  183. {
  184. Drawable* draw = getObject()->getDrawable();
  185. if (draw)
  186. draw->setAnimationLoopDuration(m_timeoutFrame - now);
  187. }
  188. }
  189. //-------------------------------------------------------------------------------------------------
  190. Bool MissileLauncherBuildingUpdate::initiateIntentToDoSpecialPower( const SpecialPowerTemplate *specialPowerTemplate, const Object *targetObj, const Coord3D *targetPos, const Waypoint *way, UnsignedInt commandOptions )
  191. {
  192. if( m_specialPowerModule->getSpecialPowerTemplate() != specialPowerTemplate )
  193. {
  194. return FALSE;
  195. }
  196. DEBUG_ASSERTCRASH(!TheGlobalData->m_specialPowerUsesDelay || m_doorState == DOOR_OPEN, ("door is not fully open when specialpower is fired!"));
  197. switchToState(DOOR_WAITING_TO_CLOSE);
  198. // getObject()->getControllingPlayer()->getAcademyStats()->recordSpecialPowerUsed( specialPowerTemplate );
  199. return TRUE;
  200. }
  201. //-------------------------------------------------------------------------------------------------
  202. Bool MissileLauncherBuildingUpdate::isPowerCurrentlyInUse( const CommandButton *command ) const
  203. {
  204. //@todo -- Implement me in such a way that it still works with Ctrl+s cheat key...
  205. //In fact, this code doesn't really care because in legit mode, the game will
  206. //never require this check to disable the cameo button.
  207. return false;
  208. }
  209. //-------------------------------------------------------------------------------------------------
  210. UpdateSleepTime MissileLauncherBuildingUpdate::update( void )
  211. {
  212. const MissileLauncherBuildingUpdateModuleData* d = getMissileLauncherBuildingUpdateModuleData();
  213. UnsignedInt now = TheGameLogic->getFrame();
  214. // If we are under construction, any decision we make about door status could be wrong.
  215. // Our special power module is randomly going to be initialized or not (which would result
  216. // in him reporting a 0 frame ready, which means we will start open).
  217. if( getObject()->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION) )
  218. return UPDATE_SLEEP_NONE;
  219. if (!m_specialPowerModule)
  220. {
  221. m_specialPowerModule = getObject()->getSpecialPowerModule(d->m_specialPowerTemplate);
  222. DEBUG_ASSERTCRASH(m_specialPowerModule, ("Missing special power"));
  223. }
  224. if (m_specialPowerModule)
  225. {
  226. UnsignedInt readyFrame = m_specialPowerModule->getReadyFrame();
  227. UnsignedInt whenToStartOpening = (readyFrame >= d->m_doorOpenTime) ? (readyFrame - d->m_doorOpenTime) : 0;
  228. if (m_timeoutFrame != 0 && now > m_timeoutFrame)
  229. {
  230. switchToState(m_timeoutState);
  231. }
  232. DEBUG_ASSERTCRASH(!TheGlobalData->m_specialPowerUsesDelay || !(m_specialPowerModule->isReady() && m_doorState != DOOR_OPEN), ("door is not fully open when specialpower is ready!"));
  233. if (m_doorState != DOOR_OPEN && m_specialPowerModule->isReady())
  234. {
  235. DEBUG_LOG(("*** had to POP the door open!\n"));
  236. switchToState(DOOR_OPEN);
  237. }
  238. else if (m_doorState == DOOR_CLOSED && now >= whenToStartOpening)
  239. {
  240. switchToState(DOOR_OPENING);
  241. }
  242. }
  243. return UPDATE_SLEEP_NONE;
  244. }
  245. //-------------------------------------------------------------------------------------------------
  246. SpecialPowerTemplate* MissileLauncherBuildingUpdate::getTemplate() const
  247. {
  248. const MissileLauncherBuildingUpdateModuleData* data = getMissileLauncherBuildingUpdateModuleData();
  249. return data->m_specialPowerTemplate;
  250. }
  251. // ------------------------------------------------------------------------------------------------
  252. /** CRC */
  253. // ------------------------------------------------------------------------------------------------
  254. void MissileLauncherBuildingUpdate::crc( Xfer *xfer )
  255. {
  256. // extend base class
  257. UpdateModule::crc( xfer );
  258. } // end crc
  259. // ------------------------------------------------------------------------------------------------
  260. /** Xfer method
  261. * Version Info:
  262. * 1: Initial version */
  263. // ------------------------------------------------------------------------------------------------
  264. void MissileLauncherBuildingUpdate::xfer( Xfer *xfer )
  265. {
  266. // version
  267. XferVersion currentVersion = 1;
  268. XferVersion version = currentVersion;
  269. xfer->xferVersion( &version, currentVersion );
  270. // extend base class
  271. UpdateModule::xfer( xfer );
  272. // do not need to tie the m_specialPowerModule pointer cause it gets tied
  273. // SpecialPowerModuleInterface *m_specialPowerModule;
  274. // door state
  275. xfer->xferUser( &m_doorState, sizeof( DoorStateType ) );
  276. // timeout state
  277. xfer->xferUser( &m_timeoutState, sizeof( DoorStateType ) );
  278. // timeout frame
  279. xfer->xferUnsignedInt( &m_timeoutFrame );
  280. } // end xfer
  281. // ------------------------------------------------------------------------------------------------
  282. /** Load post process */
  283. // ------------------------------------------------------------------------------------------------
  284. void MissileLauncherBuildingUpdate::loadPostProcess( void )
  285. {
  286. // extend base class
  287. UpdateModule::loadPostProcess();
  288. } // end loadPostProcess