OCLUpdate.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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: OCLUpdate.cpp /////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, August2002
  25. // Desc: Update Spits out an OCL on a timer
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
  29. #include "Common/RandomValue.h"
  30. #include "Common/Xfer.h"
  31. #include "GameLogic/GameLogic.h"
  32. #include "GameLogic/Object.h"
  33. #include "GameLogic/ObjectCreationList.h"
  34. #include "GameLogic/PartitionManager.h"
  35. #include "GameLogic/Module/OCLUpdate.h"
  36. #include "GameLogic/TerrainLogic.h"
  37. //-------------------------------------------------------------------------------------------------
  38. OCLUpdateModuleData::OCLUpdateModuleData()
  39. {
  40. m_minDelay = 0;
  41. m_maxDelay = 0;
  42. m_ocl = NULL;
  43. m_isCreateAtEdge = FALSE;
  44. }
  45. //-------------------------------------------------------------------------------------------------
  46. /*static*/ void OCLUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  47. {
  48. UpdateModuleData::buildFieldParse(p);
  49. static const FieldParse dataFieldParse[] =
  50. {
  51. { "OCL", INI::parseObjectCreationList, NULL, offsetof( OCLUpdateModuleData, m_ocl ) },
  52. { "MinDelay", INI::parseDurationUnsignedInt, NULL, offsetof( OCLUpdateModuleData, m_minDelay ) },
  53. { "MaxDelay", INI::parseDurationUnsignedInt, NULL, offsetof( OCLUpdateModuleData, m_maxDelay ) },
  54. { "CreateAtEdge", INI::parseBool, NULL, offsetof( OCLUpdateModuleData, m_isCreateAtEdge ) },
  55. { 0, 0, 0, 0 }
  56. };
  57. p.add(dataFieldParse);
  58. }
  59. //-------------------------------------------------------------------------------------------------
  60. //-------------------------------------------------------------------------------------------------
  61. OCLUpdate::OCLUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  62. {
  63. m_nextCreationFrame = 0;
  64. m_timerStartedFrame = 0;
  65. }
  66. //-------------------------------------------------------------------------------------------------
  67. //-------------------------------------------------------------------------------------------------
  68. OCLUpdate::~OCLUpdate( void )
  69. {
  70. }
  71. //-------------------------------------------------------------------------------------------------
  72. //-------------------------------------------------------------------------------------------------
  73. UpdateSleepTime OCLUpdate::update( void )
  74. {
  75. /// @todo srj use SLEEPY_UPDATE here
  76. if( shouldCreate() )
  77. {
  78. if( m_nextCreationFrame == 0 )
  79. {
  80. // You don't get to actually spread the first try, you start on a timer, then go
  81. setNextCreationFrame();
  82. return UPDATE_SLEEP_NONE;
  83. }
  84. setNextCreationFrame();
  85. Coord3D creationCoord;
  86. if( getOCLUpdateModuleData()->m_isCreateAtEdge )
  87. creationCoord = TheTerrainLogic->findClosestEdgePoint( getObject()->getPosition() );
  88. else
  89. creationCoord = *getObject()->getPosition();
  90. ObjectCreationList::create( getOCLUpdateModuleData()->m_ocl, getObject(), &creationCoord, getObject()->getPosition() );
  91. }
  92. return UPDATE_SLEEP_NONE;
  93. }
  94. // ------------------------------------------------------------------------------------------------
  95. // ------------------------------------------------------------------------------------------------
  96. Bool OCLUpdate::shouldCreate()
  97. {
  98. if( TheGameLogic->getFrame() < m_nextCreationFrame )
  99. return FALSE;//too soon
  100. if( BitTest( getObject()->getStatusBits(), OBJECT_STATUS_UNDER_CONSTRUCTION ) == TRUE )
  101. return FALSE;// not built yet
  102. return TRUE;
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. // ------------------------------------------------------------------------------------------------
  106. void OCLUpdate::setNextCreationFrame()
  107. {
  108. UnsignedInt delay = GameLogicRandomValue( getOCLUpdateModuleData()->m_minDelay,
  109. getOCLUpdateModuleData()->m_maxDelay );
  110. m_timerStartedFrame = TheGameLogic->getFrame();
  111. m_nextCreationFrame = m_timerStartedFrame + delay;
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. // ------------------------------------------------------------------------------------------------
  115. Real OCLUpdate::getCountdownPercent() const
  116. {
  117. UnsignedInt now = TheGameLogic->getFrame();
  118. return 1.0f - (( m_nextCreationFrame - now ) / (float)( m_nextCreationFrame - m_timerStartedFrame ));
  119. }
  120. // ------------------------------------------------------------------------------------------------
  121. // ------------------------------------------------------------------------------------------------
  122. UnsignedInt OCLUpdate::getRemainingFrames() const
  123. {
  124. UnsignedInt now = TheGameLogic->getFrame();
  125. return ( m_nextCreationFrame - now );
  126. }
  127. // ------------------------------------------------------------------------------------------------
  128. /** CRC */
  129. // ------------------------------------------------------------------------------------------------
  130. void OCLUpdate::crc( Xfer *xfer )
  131. {
  132. // extend base class
  133. UpdateModule::crc( xfer );
  134. } // end crc
  135. // ------------------------------------------------------------------------------------------------
  136. /** Xfer method
  137. * Version Info:
  138. * 1: Initial version */
  139. // ------------------------------------------------------------------------------------------------
  140. void OCLUpdate::xfer( Xfer *xfer )
  141. {
  142. // version
  143. XferVersion currentVersion = 1;
  144. XferVersion version = currentVersion;
  145. xfer->xferVersion( &version, currentVersion );
  146. // extend base class
  147. UpdateModule::xfer( xfer );
  148. // next creation frame
  149. xfer->xferUnsignedInt( &m_nextCreationFrame );
  150. // timer stated frame
  151. xfer->xferUnsignedInt( &m_timerStartedFrame );
  152. } // end xfer
  153. // ------------------------------------------------------------------------------------------------
  154. /** Load post process */
  155. // ------------------------------------------------------------------------------------------------
  156. void OCLUpdate::loadPostProcess( void )
  157. {
  158. // extend base class
  159. UpdateModule::loadPostProcess();
  160. } // end loadPostProcess