MissileLauncherBuildingUpdate.cpp 11 KB

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