BaseRenerateUpdate.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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: BaseRegenerateUpdate.cpp /////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, July 2002
  25. // Desc: Update module for base objects automatically regenerating health
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/GlobalData.h"
  30. #include "Common/Xfer.h"
  31. #include "GameLogic/GameLogic.h"
  32. #include "GameLogic/Module/BaseRegenerateUpdate.h"
  33. #include "GameLogic/Module/BodyModule.h"
  34. #include "GameLogic/Object.h"
  35. ///////////////////////////////////////////////////////////////////////////////////////////////////
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////
  37. ///////////////////////////////////////////////////////////////////////////////////////////////////
  38. // ------------------------------------------------------------------------------------------------
  39. // ------------------------------------------------------------------------------------------------
  40. BaseRegenerateUpdateModuleData::BaseRegenerateUpdateModuleData()
  41. {
  42. }
  43. //-------------------------------------------------------------------------------------------------
  44. // ------------------------------------------------------------------------------------------------
  45. void BaseRegenerateUpdateModuleData::buildFieldParse( MultiIniFieldParse &p )
  46. {
  47. UpdateModuleData::buildFieldParse( p );
  48. static const FieldParse dataFieldParse[] =
  49. {
  50. { 0, 0, 0, 0 }
  51. };
  52. p.add( dataFieldParse );
  53. }
  54. ///////////////////////////////////////////////////////////////////////////////////////////////////
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////
  56. ///////////////////////////////////////////////////////////////////////////////////////////////////
  57. //-------------------------------------------------------------------------------------------------
  58. //-------------------------------------------------------------------------------------------------
  59. BaseRegenerateUpdate::BaseRegenerateUpdate( Thing *thing, const ModuleData* moduleData )
  60. : UpdateModule( thing, moduleData )
  61. {
  62. if( TheGlobalData->m_baseRegenHealthPercentPerSecond == 0.0f )
  63. {
  64. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  65. }
  66. else
  67. {
  68. setWakeFrame(getObject(), UPDATE_SLEEP_NONE);
  69. }
  70. }
  71. //-------------------------------------------------------------------------------------------------
  72. //-------------------------------------------------------------------------------------------------
  73. BaseRegenerateUpdate::~BaseRegenerateUpdate( void )
  74. {
  75. }
  76. //-------------------------------------------------------------------------------------------------
  77. /** Damage has been dealt, this is an opportunity to reach to that damage */
  78. //-------------------------------------------------------------------------------------------------
  79. void BaseRegenerateUpdate::onDamage( DamageInfo *damageInfo )
  80. {
  81. if (TheGlobalData->m_baseRegenHealthPercentPerSecond > 0.0 &&
  82. damageInfo->in.m_damageType != DAMAGE_HEALING)
  83. {
  84. setWakeFrame(getObject(), UPDATE_SLEEP(TheGlobalData->m_baseRegenDelay));
  85. }
  86. else
  87. {
  88. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  89. }
  90. }
  91. //-------------------------------------------------------------------------------------------------
  92. /** The update callback, this is pretty similar to AutoHealUpdate, but that module is supposed
  93. * to be used in concert with an upgrade and doesn't have any of the "only regenerate
  94. * if we haven't been damaged recently" restrictions */
  95. //-------------------------------------------------------------------------------------------------
  96. UpdateSleepTime BaseRegenerateUpdate::update( void )
  97. {
  98. // this is us!
  99. Object *me = getObject();
  100. if( me->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION) )
  101. {
  102. return UPDATE_SLEEP_NONE;
  103. }
  104. if( me->testStatus(OBJECT_STATUS_SOLD) )
  105. {
  106. return UPDATE_SLEEP_FOREVER;
  107. }
  108. // do not heal if we are at max health already
  109. BodyModuleInterface *body = me->getBodyModule();
  110. if( body->getMaxHealth() == body->getHealth() )
  111. {
  112. return UPDATE_SLEEP_FOREVER; // sleep till we get damaged again
  113. }
  114. else
  115. {
  116. // we don't really need to heal every frame. do it every 3 frames or so.
  117. const Int HEAL_RATE = 3;
  118. // do some healing
  119. Real amount = HEAL_RATE * (body->getMaxHealth() * TheGlobalData->m_baseRegenHealthPercentPerSecond) /
  120. LOGICFRAMES_PER_SECOND;
  121. me->attemptHealing(amount, me);
  122. return UPDATE_SLEEP(HEAL_RATE);
  123. }
  124. } // end update
  125. // ------------------------------------------------------------------------------------------------
  126. /** CRC */
  127. // ------------------------------------------------------------------------------------------------
  128. void BaseRegenerateUpdate::crc( Xfer *xfer )
  129. {
  130. // extend base class
  131. UpdateModule::crc( xfer );
  132. } // end crc
  133. // ------------------------------------------------------------------------------------------------
  134. /** Xfer method
  135. * Version Info:
  136. * 1: Initial version */
  137. // ------------------------------------------------------------------------------------------------
  138. void BaseRegenerateUpdate::xfer( Xfer *xfer )
  139. {
  140. // version
  141. XferVersion currentVersion = 1;
  142. XferVersion version = currentVersion;
  143. xfer->xferVersion( &version, currentVersion );
  144. // extend base class
  145. UpdateModule::xfer( xfer );
  146. } // end xfer
  147. // ------------------------------------------------------------------------------------------------
  148. /** Load post process */
  149. // ------------------------------------------------------------------------------------------------
  150. void BaseRegenerateUpdate::loadPostProcess( void )
  151. {
  152. // extend base class
  153. UpdateModule::loadPostProcess();
  154. } // end loadPostProcess