EnemyNearUpdate.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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: EnemyNearUpdate.cpp ///////////////////////////////////////////////////////////////////////////
  24. // Author: Matthew D. Campbell, December 2002
  25. // Desc: Reacts when an enemy is within range
  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 "GameClient/Drawable.h"
  33. #include "GameLogic/Module/EnemyNearUpdate.h"
  34. #include "GameLogic/Object.h"
  35. #include "GameLogic/AI.h"
  36. #include "GameLogic/Module/AIUpdate.h"
  37. //-------------------------------------------------------------------------------------------------
  38. //-------------------------------------------------------------------------------------------------
  39. //-------------------------------------------------------------------------------------------------
  40. EnemyNearUpdate::EnemyNearUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData ),
  41. m_enemyNear(false),
  42. m_enemyScanDelay(0)
  43. {
  44. // bias a random amount so everyone doesn't spike at once
  45. m_enemyScanDelay += GameLogicRandomValue(0, getEnemyNearUpdateModuleData()->m_enemyScanDelayTime);
  46. }
  47. //-------------------------------------------------------------------------------------------------
  48. //-------------------------------------------------------------------------------------------------
  49. EnemyNearUpdate::~EnemyNearUpdate( void )
  50. {
  51. }
  52. //-------------------------------------------------------------------------------------------------
  53. /**
  54. * Look around us for enemies.
  55. */
  56. void EnemyNearUpdate::checkForEnemies( void )
  57. {
  58. // periodic enemy checks
  59. if (m_enemyScanDelay == 0)
  60. {
  61. m_enemyScanDelay = getEnemyNearUpdateModuleData()->m_enemyScanDelayTime;
  62. Real visionRange = getObject()->getVisionRange();
  63. Object* enemy = TheAI->findClosestEnemy( getObject(), visionRange, AI::CAN_SEE );
  64. m_enemyNear = (enemy != NULL);
  65. }
  66. else
  67. {
  68. --m_enemyScanDelay;
  69. }
  70. }
  71. //-------------------------------------------------------------------------------------------------
  72. ///< Sit around until an enemy gets near.
  73. //-------------------------------------------------------------------------------------------------
  74. UpdateSleepTime EnemyNearUpdate::update()
  75. {
  76. /// @todo srj use SLEEPY_UPDATE here
  77. Bool enemyWasNear = m_enemyNear;
  78. checkForEnemies();
  79. if (m_enemyNear && !enemyWasNear)
  80. {
  81. // change the state of the art to an "enemy near" state
  82. Drawable *draw = getObject()->getDrawable();
  83. if( draw )
  84. draw->setModelConditionState( MODELCONDITION_ENEMYNEAR );
  85. }
  86. else if (!m_enemyNear && enemyWasNear)
  87. {
  88. // change the state of the art to an idle state
  89. Drawable *draw = getObject()->getDrawable();
  90. if( draw )
  91. draw->clearModelConditionState( MODELCONDITION_ENEMYNEAR );
  92. }
  93. return UPDATE_SLEEP_NONE;
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. /** CRC */
  97. // ------------------------------------------------------------------------------------------------
  98. void EnemyNearUpdate::crc( Xfer *xfer )
  99. {
  100. // extend base class
  101. UpdateModule::crc( xfer );
  102. } // end crc
  103. // ------------------------------------------------------------------------------------------------
  104. /** Xfer method
  105. * Version Info:
  106. * 1: Initial version */
  107. // ------------------------------------------------------------------------------------------------
  108. void EnemyNearUpdate::xfer( Xfer *xfer )
  109. {
  110. // version
  111. XferVersion currentVersion = 1;
  112. XferVersion version = currentVersion;
  113. xfer->xferVersion( &version, currentVersion );
  114. // extend base class
  115. UpdateModule::xfer( xfer );
  116. // enemy scan delay
  117. xfer->xferUnsignedInt( &m_enemyScanDelay );
  118. // enemy near
  119. xfer->xferBool( &m_enemyNear );
  120. } // end xfer
  121. // ------------------------------------------------------------------------------------------------
  122. /** Load post process */
  123. // ------------------------------------------------------------------------------------------------
  124. void EnemyNearUpdate::loadPostProcess( void )
  125. {
  126. // extend base class
  127. UpdateModule::loadPostProcess();
  128. } // end loadPostProcess