HealContain.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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: HealContain.cpp //////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day
  25. // Desc: Objects that are contained inside a heal contain ... get healed! oh my!
  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/Damage.h"
  31. #include "GameLogic/GameLogic.h"
  32. #include "GameLogic/Object.h"
  33. #include "GameLogic/Module/BodyModule.h"
  34. #include "GameLogic/Module/HealContain.h"
  35. #include "GameLogic/Module/UpdateModule.h"
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////
  37. ///////////////////////////////////////////////////////////////////////////////////////////////////
  38. ///////////////////////////////////////////////////////////////////////////////////////////////////
  39. // ------------------------------------------------------------------------------------------------
  40. // ------------------------------------------------------------------------------------------------
  41. HealContainModuleData::HealContainModuleData( void )
  42. {
  43. m_framesForFullHeal = 0;
  44. } // end HealContainModuleData
  45. // ------------------------------------------------------------------------------------------------
  46. // ------------------------------------------------------------------------------------------------
  47. /*static*/ void HealContainModuleData::buildFieldParse(MultiIniFieldParse& p)
  48. {
  49. OpenContainModuleData::buildFieldParse( p );
  50. static const FieldParse dataFieldParse[] =
  51. {
  52. { "TimeForFullHeal", INI::parseDurationUnsignedInt, NULL, offsetof( HealContainModuleData, m_framesForFullHeal ) },
  53. { 0, 0, 0, 0 }
  54. };
  55. p.add(dataFieldParse);
  56. } // end buildFieldParse
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////
  58. ///////////////////////////////////////////////////////////////////////////////////////////////////
  59. ///////////////////////////////////////////////////////////////////////////////////////////////////
  60. // ------------------------------------------------------------------------------------------------
  61. // ------------------------------------------------------------------------------------------------
  62. HealContain::HealContain( Thing *thing, const ModuleData *moduleData )
  63. : OpenContain( thing, moduleData )
  64. {
  65. } // end HealContain
  66. // ------------------------------------------------------------------------------------------------
  67. // ------------------------------------------------------------------------------------------------
  68. HealContain::~HealContain( void )
  69. {
  70. } // end ~HealContain
  71. // ------------------------------------------------------------------------------------------------
  72. /** Per frame update */
  73. // ------------------------------------------------------------------------------------------------
  74. UpdateSleepTime HealContain::update( void )
  75. {
  76. // extending functionality
  77. /*UpdateSleepTime result =*/ OpenContain::update();
  78. // get the module data
  79. const HealContainModuleData *modData = getHealContainModuleData();
  80. //
  81. // for each of our objects, we give them a little health each frame so that when the
  82. // the TimeTillHealed is up, the object will exit and be fully healed
  83. //
  84. Bool doneHealing;
  85. Object *obj;
  86. ContainedItemsList::const_iterator it = getContainList().begin();
  87. while( it != getContainList().end() )
  88. {
  89. // get the object
  90. obj = *it;
  91. // increment the iterator, which allows failure to not cause an infinite loop
  92. ++it;
  93. // do the healing on this object
  94. doneHealing = doHeal( obj, modData->m_framesForFullHeal );
  95. // if we're done healing, we need to remove us from the healing container
  96. if( doneHealing == TRUE )
  97. {
  98. ExitDoorType exitDoor = reserveDoorForExit(obj->getTemplate(), obj);
  99. if (exitDoor != DOOR_NONE_AVAILABLE)
  100. exitObjectViaDoor( obj, exitDoor );
  101. } // end if
  102. } // end for, it
  103. return UPDATE_SLEEP_NONE;
  104. } // end update
  105. // ------------------------------------------------------------------------------------------------
  106. /** Do the healing for a single object for a single frame. */
  107. // ------------------------------------------------------------------------------------------------
  108. Bool HealContain::doHeal( Object *obj, UnsignedInt framesForFullHeal )
  109. {
  110. Bool doneHealing = FALSE;
  111. // setup the healing damageInfo structure with all but the amount
  112. DamageInfo healInfo;
  113. healInfo.in.m_damageType = DAMAGE_HEALING;
  114. healInfo.in.m_deathType = DEATH_NONE;
  115. healInfo.in.m_sourceID = getObject()->getID();
  116. // get body module of the thing to heal
  117. BodyModuleInterface *body = obj->getBodyModule();
  118. // if we've been in here long enough ... set our health to max
  119. if( TheGameLogic->getFrame() - obj->getContainedByFrame() >= framesForFullHeal )
  120. {
  121. // set the amount to max just to be sure we're at the top
  122. healInfo.in.m_amount = body->getMaxHealth();
  123. // set max health
  124. body->attemptHealing( &healInfo );
  125. // we're done healing
  126. doneHealing = TRUE;
  127. } // end if
  128. else
  129. {
  130. //
  131. // given the *whole* time it would take to heal this object, lets pretend that the
  132. // object is at zero health ... and give it a sliver of health as if it were at 0 health
  133. // and would be fully healed at 'framesForFullHeal'
  134. //
  135. healInfo.in.m_amount = body->getMaxHealth() / (Real)framesForFullHeal;
  136. // do the healing
  137. body->attemptHealing( &healInfo );
  138. } // end else
  139. // return if we're done healing
  140. return doneHealing;
  141. } // end doHeal
  142. // ------------------------------------------------------------------------------------------------
  143. /** CRC */
  144. // ------------------------------------------------------------------------------------------------
  145. void HealContain::crc( Xfer *xfer )
  146. {
  147. // extend base class
  148. OpenContain::crc( xfer );
  149. } // end crc
  150. // ------------------------------------------------------------------------------------------------
  151. /** Xfer method
  152. * Version Info:
  153. * 1: Initial version */
  154. // ------------------------------------------------------------------------------------------------
  155. void HealContain::xfer( Xfer *xfer )
  156. {
  157. // version
  158. XferVersion currentVersion = 1;
  159. XferVersion version = currentVersion;
  160. xfer->xferVersion( &version, currentVersion );
  161. // extend base class
  162. OpenContain::xfer( xfer );
  163. } // end xfer
  164. // ------------------------------------------------------------------------------------------------
  165. /** Load post process */
  166. // ------------------------------------------------------------------------------------------------
  167. void HealContain::loadPostProcess( void )
  168. {
  169. // extend base class
  170. OpenContain::loadPostProcess();
  171. } // end loadPostProcess