SwayClientUpdate.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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: SwayClientUpdate.cpp //////////////////////////////////////////////////////////////////
  24. // Author: Matthew D. Campbell, May 2002
  25. // Desc: Tree sway client update module
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "GameClient/Drawable.h"
  30. #include "GameClient/Module/SwayClientUpdate.h"
  31. #include "Common/Player.h"
  32. #include "Common/PlayerList.h"
  33. #include "Common/ThingFactory.h"
  34. #include "Common/ThingTemplate.h"
  35. #include "Common/RandomValue.h"
  36. #include "Common/PerfTimer.h"
  37. #include "Common/Xfer.h"
  38. #include "GameLogic/Object.h"
  39. #include "GameLogic/ScriptEngine.h"
  40. #include "GameLogic/GameLogic.h"
  41. #ifdef _INTERNAL
  42. // for occasional debugging...
  43. //#pragma optimize("", off)
  44. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  45. #endif
  46. //-------------------------------------------------------------------------------------------------
  47. //-------------------------------------------------------------------------------------------------
  48. SwayClientUpdate::SwayClientUpdate( Thing *thing, const ModuleData* moduleData ) :
  49. ClientUpdateModule( thing, moduleData ),
  50. m_curDelta(0),
  51. m_curValue(0),
  52. m_curAngle(0),
  53. m_curAngleLimit(0),
  54. m_leanAngle(0),
  55. m_swaying(true),
  56. m_unused(false),
  57. m_curVersion(-1) // so that we never match the first time
  58. {
  59. // don't do updateSway here; wait till the first time we go thru our update loop.
  60. //updateSway();
  61. }
  62. //-------------------------------------------------------------------------------------------------
  63. //-------------------------------------------------------------------------------------------------
  64. SwayClientUpdate::~SwayClientUpdate( void )
  65. {
  66. }
  67. //-------------------------------------------------------------------------------------------------
  68. // Update the sway parameters.
  69. //-------------------------------------------------------------------------------------------------
  70. void SwayClientUpdate::updateSway()
  71. {
  72. const BreezeInfo& info = TheScriptEngine->getBreezeInfo();
  73. if (info.m_randomness == 0.0f)
  74. {
  75. m_curValue = 0;
  76. }
  77. Real delta = info.m_randomness * 0.5f;
  78. m_curAngleLimit = info.m_intensity * GameClientRandomValueReal(1.0f-delta, 1.0f+delta);
  79. m_curDelta = 2*PI/info.m_breezePeriod * GameClientRandomValueReal(1.0f-delta, 1.0f+delta);
  80. m_leanAngle = info.m_lean * GameClientRandomValueReal(1.0f-delta, 1.0f+delta);
  81. m_curVersion = info.m_breezeVersion;
  82. }
  83. //-------------------------------------------------------------------------------------------------
  84. //-------------------------------------------------------------------------------------------------
  85. //-------------------------------------------------------------------------------------------------
  86. /** The client update callback. */
  87. //-------------------------------------------------------------------------------------------------
  88. void SwayClientUpdate::clientUpdate( void )
  89. {
  90. if( !m_swaying )
  91. return;
  92. Drawable *draw = getDrawable();
  93. // if breeze changes, always process the full update, even if not visible,
  94. // so that things offscreen won't 'pop' when first viewed
  95. const BreezeInfo& info = TheScriptEngine->getBreezeInfo();
  96. if (info.m_breezeVersion != m_curVersion)
  97. {
  98. updateSway();
  99. }
  100. else
  101. {
  102. // Otherwise, only update visible drawables
  103. if (!draw || !draw->isVisible())
  104. return;
  105. }
  106. m_curValue += m_curDelta;
  107. if (m_curValue > 2*PI)
  108. m_curValue -= 2*PI;
  109. Real cosine = Cos(m_curValue);
  110. Real targetAngle = cosine * m_curAngleLimit + m_leanAngle;
  111. Real deltaAngle = targetAngle - m_curAngle;
  112. Matrix3D xfrm = *draw->getInstanceMatrix();
  113. xfrm.In_Place_Pre_Rotate_X(-deltaAngle * info.m_directionVec.x);
  114. xfrm.In_Place_Pre_Rotate_Y(deltaAngle * info.m_directionVec.y);
  115. draw->setInstanceMatrix(&xfrm);
  116. m_curAngle = targetAngle;
  117. // burned things don't sway.
  118. Object* obj = draw->getObject();
  119. if( obj && obj->getStatusBits().test( OBJECT_STATUS_BURNED ) )
  120. stopSway();
  121. }
  122. // ------------------------------------------------------------------------------------------------
  123. /** CRC */
  124. // ------------------------------------------------------------------------------------------------
  125. void SwayClientUpdate::crc( Xfer *xfer )
  126. {
  127. // extend base class
  128. ClientUpdateModule::crc( xfer );
  129. } // end crc
  130. // ------------------------------------------------------------------------------------------------
  131. /** Xfer method
  132. * Version Info:
  133. * 1: Initial version */
  134. // ------------------------------------------------------------------------------------------------
  135. void SwayClientUpdate::xfer( Xfer *xfer )
  136. {
  137. // version
  138. XferVersion currentVersion = 1;
  139. XferVersion version = currentVersion;
  140. xfer->xferVersion( &version, currentVersion );
  141. // extend base class
  142. ClientUpdateModule::xfer( xfer );
  143. // cur value
  144. xfer->xferReal( &m_curValue );
  145. // cur angle
  146. xfer->xferReal( &m_curAngle );
  147. // cur delta
  148. xfer->xferReal( &m_curDelta );
  149. // cur angle limit
  150. xfer->xferReal( &m_curAngleLimit );
  151. // lean angle
  152. xfer->xferReal( &m_leanAngle );
  153. // cur version
  154. xfer->xferShort( &m_curVersion );
  155. // swaying
  156. xfer->xferBool( &m_swaying );
  157. } // end xfer
  158. // ------------------------------------------------------------------------------------------------
  159. /** Load post process */
  160. // ------------------------------------------------------------------------------------------------
  161. void SwayClientUpdate::loadPostProcess( void )
  162. {
  163. // extend base class
  164. ClientUpdateModule::loadPostProcess();
  165. updateSway();
  166. } // end loadPostProcess