SupplyWarehouseCripplingBehavior.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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: SupplyWarehouseCripplingBehavior.cpp /////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, Septemmber 2002
  25. // Desc: Behavior that Disables the building on ReallyDamaged edge state, and manages an Update timer to heal
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/Xfer.h"
  30. #include "GameLogic/Module/SupplyWarehouseCripplingBehavior.h"
  31. #include "GameLogic/GameLogic.h"
  32. #include "GameLogic/Object.h"
  33. #include "GameLogic/Module/BodyModule.h"
  34. //-------------------------------------------------------------------------------------------------
  35. SupplyWarehouseCripplingBehaviorModuleData::SupplyWarehouseCripplingBehaviorModuleData()
  36. {
  37. m_selfHealSupression = 0; ///< Time since last damage until I can start to heal
  38. m_selfHealDelay = 0; ///< Once I am okay to heal, how often to do so
  39. m_selfHealAmount = 0; ///< And how much
  40. }
  41. //-------------------------------------------------------------------------------------------------
  42. /*static*/ void SupplyWarehouseCripplingBehaviorModuleData::buildFieldParse(MultiIniFieldParse& p)
  43. {
  44. static const FieldParse dataFieldParse[] =
  45. {
  46. { "SelfHealSupression", INI::parseDurationUnsignedInt, NULL, offsetof(SupplyWarehouseCripplingBehaviorModuleData, m_selfHealSupression) },
  47. { "SelfHealDelay", INI::parseDurationUnsignedInt, NULL, offsetof(SupplyWarehouseCripplingBehaviorModuleData, m_selfHealDelay) },
  48. { "SelfHealAmount", INI::parseReal, NULL, offsetof(SupplyWarehouseCripplingBehaviorModuleData, m_selfHealAmount) },
  49. { 0, 0, 0, 0 }
  50. };
  51. UpdateModuleData::buildFieldParse(p);
  52. p.add(dataFieldParse);
  53. }
  54. //-------------------------------------------------------------------------------------------------
  55. //-------------------------------------------------------------------------------------------------
  56. SupplyWarehouseCripplingBehavior::SupplyWarehouseCripplingBehavior( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  57. {
  58. m_healingSupressedUntilFrame = 0;
  59. m_nextHealingFrame = 0;
  60. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  61. }
  62. //-------------------------------------------------------------------------------------------------
  63. //-------------------------------------------------------------------------------------------------
  64. SupplyWarehouseCripplingBehavior::~SupplyWarehouseCripplingBehavior( void )
  65. {
  66. }
  67. //-------------------------------------------------------------------------------------------------
  68. /** Damage has been dealt, this is an opportunity to react to that damage */
  69. //-------------------------------------------------------------------------------------------------
  70. void SupplyWarehouseCripplingBehavior::onDamage( DamageInfo *damageInfo )
  71. {
  72. UnsignedInt now = TheGameLogic->getFrame();
  73. resetSelfHealSupression();
  74. setWakeFrame(getObject(), UPDATE_SLEEP(m_healingSupressedUntilFrame - now));// we got hit, time to get up for work after a quick snooze
  75. }
  76. //-------------------------------------------------------------------------------------------------
  77. void SupplyWarehouseCripplingBehavior::onBodyDamageStateChange(const DamageInfo* damageInfo, BodyDamageType oldState, BodyDamageType newState)
  78. {
  79. if( newState == BODY_REALLYDAMAGED )
  80. startCrippledEffects();
  81. else if( oldState == BODY_REALLYDAMAGED )
  82. stopCrippledEffects();
  83. }
  84. // ------------------------------------------------------------------------------------------------
  85. // ------------------------------------------------------------------------------------------------
  86. UpdateSleepTime SupplyWarehouseCripplingBehavior::update()
  87. {
  88. // Supression is handled by sleeping the module, so if I am here, I know it is time to heal.
  89. const SupplyWarehouseCripplingBehaviorModuleData* md = getSupplyWarehouseCripplingBehaviorModuleData();
  90. UnsignedInt now = TheGameLogic->getFrame();
  91. m_nextHealingFrame = now + md->m_selfHealDelay;
  92. getObject()->attemptHealing(md->m_selfHealAmount, NULL);
  93. if( getObject()->getBodyModule()->getHealth() == getObject()->getBodyModule()->getMaxHealth() )
  94. return UPDATE_SLEEP_FOREVER;// this can't be in onHealing, as the healing comes from here
  95. // in the update, and sleep settings in onHealing would be overridden by my return value.
  96. // Delay between heals is also handled by sleeping the module. How cool is that?
  97. return UPDATE_SLEEP(m_nextHealingFrame - now);
  98. }
  99. // ------------------------------------------------------------------------------------------------
  100. void SupplyWarehouseCripplingBehavior::resetSelfHealSupression()
  101. {
  102. const SupplyWarehouseCripplingBehaviorModuleData* md = getSupplyWarehouseCripplingBehaviorModuleData();
  103. UnsignedInt now = TheGameLogic->getFrame();
  104. m_healingSupressedUntilFrame = now + md->m_selfHealSupression;
  105. m_nextHealingFrame = m_healingSupressedUntilFrame;
  106. }
  107. // ------------------------------------------------------------------------------------------------
  108. void SupplyWarehouseCripplingBehavior::startCrippledEffects()
  109. {
  110. DockUpdateInterface *myDock = getObject()->getDockUpdateInterface();
  111. myDock->setDockCrippled( TRUE );
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. void SupplyWarehouseCripplingBehavior::stopCrippledEffects()
  115. {
  116. DockUpdateInterface *myDock = getObject()->getDockUpdateInterface();
  117. myDock->setDockCrippled( FALSE );
  118. }
  119. // ------------------------------------------------------------------------------------------------
  120. /** CRC */
  121. // ------------------------------------------------------------------------------------------------
  122. void SupplyWarehouseCripplingBehavior::crc( Xfer *xfer )
  123. {
  124. // extend base class
  125. UpdateModule::crc( xfer );
  126. } // end crc
  127. // ------------------------------------------------------------------------------------------------
  128. /** Xfer method
  129. * Version Info:
  130. * 1: Initial version */
  131. // ------------------------------------------------------------------------------------------------
  132. void SupplyWarehouseCripplingBehavior::xfer( Xfer *xfer )
  133. {
  134. // version
  135. XferVersion currentVersion = 1;
  136. XferVersion version = currentVersion;
  137. xfer->xferVersion( &version, currentVersion );
  138. // extend base class
  139. UpdateModule::xfer( xfer );
  140. // healing supressed until frame
  141. xfer->xferUnsignedInt( &m_healingSupressedUntilFrame );
  142. // next healing frame
  143. xfer->xferUnsignedInt( &m_nextHealingFrame );
  144. } // end xfer
  145. // ------------------------------------------------------------------------------------------------
  146. /** Load post process */
  147. // ------------------------------------------------------------------------------------------------
  148. void SupplyWarehouseCripplingBehavior::loadPostProcess( void )
  149. {
  150. // extend base class
  151. UpdateModule::loadPostProcess();
  152. } // end loadPostProcess