CheckpointUpdate.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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: CheckpointUpdate.cpp ///////////////////////////////////////////////////////////////////////////
  24. // Author: Mark Lorenzen, August 2002
  25. // Desc: Opens gate when (ally near && ( ! enemy near) )
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/PerfTimer.h"
  30. #include "Common/ThingTemplate.h"
  31. #include "Common/Xfer.h"
  32. #include "GameLogic/Module/CheckpointUpdate.h"
  33. #include "GameLogic/Object.h"
  34. #include "GameClient/Drawable.h"
  35. #include "GameLogic/AI.h"
  36. #include "GameLogic/Module/AIUpdate.h"
  37. //-------------------------------------------------------------------------------------------------
  38. //-------------------------------------------------------------------------------------------------
  39. //-------------------------------------------------------------------------------------------------
  40. CheckpointUpdate::CheckpointUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData ),
  41. m_allyNear(false),
  42. m_enemyNear(false),
  43. m_enemyScanDelay(0),
  44. m_maxMinorRadius(0)
  45. {
  46. m_maxMinorRadius = getObject()->getGeometryInfo().getMinorRadius();
  47. // bias a random amount so everyone doesn't spike at once
  48. m_enemyScanDelay += GameLogicRandomValue(0, getCheckpointUpdateModuleData()->m_enemyScanDelayTime);
  49. }
  50. //-------------------------------------------------------------------------------------------------
  51. //-------------------------------------------------------------------------------------------------
  52. CheckpointUpdate::~CheckpointUpdate( void )
  53. {
  54. }
  55. //-------------------------------------------------------------------------------------------------
  56. /**
  57. * Look around us for enemies.
  58. */
  59. void CheckpointUpdate::checkForAlliesAndEnemies( void )
  60. {
  61. // periodic enemy checks
  62. if (m_enemyScanDelay == 0 || TRUE)
  63. {
  64. m_enemyScanDelay = getCheckpointUpdateModuleData()->m_enemyScanDelayTime;
  65. // Kind of weird, but we have to set the geometry extents to the max extent
  66. //before checking for nearest enemies and allies, or else the stretch reaction
  67. //to finding one will oscillate states open->closed->open...
  68. Object *obj = getObject();
  69. GeometryInfo geom = obj->getGeometryInfo();
  70. Real restoreSpecialRadius = geom.getMinorRadius();
  71. geom.setMinorRadius( m_maxMinorRadius );
  72. obj->setGeometryInfo( geom );
  73. Object *enemy, *ally = NULL;
  74. Real visionRange = obj->getVisionRange();
  75. enemy = TheAI->findClosestEnemy( obj, visionRange, 0 );
  76. m_enemyNear = (enemy != NULL);
  77. ally = TheAI->findClosestAlly( obj, visionRange, 0 );
  78. m_allyNear = (ally != NULL);
  79. // here we restore the radius so that other units can path past the open gate
  80. geom.setMinorRadius( restoreSpecialRadius );
  81. obj->setGeometryInfo( geom );
  82. }
  83. else
  84. {
  85. --m_enemyScanDelay;
  86. }
  87. }
  88. //-------------------------------------------------------------------------------------------------
  89. ///< Sit around until an enemy gets near.
  90. //-------------------------------------------------------------------------------------------------
  91. UpdateSleepTime CheckpointUpdate::update()
  92. {
  93. /// @todo srj use SLEEPY_UPDATE here
  94. Bool wasAnAlly = m_allyNear;
  95. Bool wasAnEnemy = m_enemyNear;
  96. checkForAlliesAndEnemies();
  97. Bool change = ( (wasAnAlly != m_allyNear) || (wasAnEnemy != m_enemyNear) );
  98. Bool open = ( ( ! m_enemyNear) && m_allyNear );
  99. Object *obj = getObject();
  100. Drawable *draw = obj->getDrawable();
  101. if ( draw )
  102. {
  103. if ( change )
  104. {
  105. if ( open )
  106. {
  107. // change the state of the art to open the door!
  108. if( draw )
  109. {
  110. /// @todo srj -- for now, this assumes at most one door
  111. draw->clearAndSetModelConditionState( MODELCONDITION_DOOR_1_CLOSING, MODELCONDITION_DOOR_1_OPENING );
  112. }
  113. }
  114. else //closed, then
  115. {
  116. // change the state of the art to close the door!
  117. if( draw )
  118. {
  119. /// @todo srj -- for now, this assumes at most one door
  120. draw->clearAndSetModelConditionState( MODELCONDITION_DOOR_1_OPENING, MODELCONDITION_DOOR_1_CLOSING );
  121. }
  122. }
  123. }// end if change
  124. GeometryInfo geom = obj->getGeometryInfo();
  125. /// @todo ask steven about the distance covered always being zero
  126. // Real animScrubScalar = draw->getAnimationScrubScalar();
  127. // geom.setMinorRadius( m_maxMinorRadius * animScrubScalar );
  128. // THis method is more accidental than above, but it works for an unimportant thing like checkpoint
  129. Real radius = geom.getMinorRadius();
  130. if ( open )
  131. {
  132. if ( radius > 0 ) geom.setMinorRadius( radius - 0.333f );
  133. }
  134. else //closed
  135. {
  136. if ( radius < m_maxMinorRadius ) geom.setMinorRadius( radius + 0.333f );
  137. }
  138. obj->setGeometryInfo( geom );
  139. } // end if draw
  140. return UPDATE_SLEEP_NONE;
  141. }
  142. // ------------------------------------------------------------------------------------------------
  143. /** CRC */
  144. // ------------------------------------------------------------------------------------------------
  145. void CheckpointUpdate::crc( Xfer *xfer )
  146. {
  147. // extend base class
  148. UpdateModule::crc( xfer );
  149. } // end crc
  150. // ------------------------------------------------------------------------------------------------
  151. /** Xfer method
  152. * Version Info:
  153. * 1: Initial version */
  154. // ------------------------------------------------------------------------------------------------
  155. void CheckpointUpdate::xfer( Xfer *xfer )
  156. {
  157. // version
  158. XferVersion currentVersion = 1;
  159. XferVersion version = currentVersion;
  160. xfer->xferVersion( &version, currentVersion );
  161. // extend base class
  162. UpdateModule::xfer( xfer );
  163. // enemy near
  164. xfer->xferBool( &m_enemyNear );
  165. // ally near
  166. xfer->xferBool( &m_allyNear );
  167. // max minor radius
  168. xfer->xferReal( &m_maxMinorRadius );
  169. // enemy scan delay
  170. xfer->xferUnsignedInt( &m_enemyScanDelay );
  171. } // end xfer
  172. // ------------------------------------------------------------------------------------------------
  173. /** Load post process */
  174. // ------------------------------------------------------------------------------------------------
  175. void CheckpointUpdate::loadPostProcess( void )
  176. {
  177. // extend base class
  178. UpdateModule::loadPostProcess();
  179. } // end loadPostProcess