RepairDockUpdate.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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: RepairDockUpdate.h ///////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, June 2002
  25. // Desc: The action of docking with a structure for repairs
  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/Object.h"
  31. #include "GameLogic/Module/BodyModule.h"
  32. #include "GameLogic/Module/RepairDockUpdate.h"
  33. // ------------------------------------------------------------------------------------------------
  34. // ------------------------------------------------------------------------------------------------
  35. RepairDockUpdateModuleData::RepairDockUpdateModuleData( void )
  36. {
  37. m_framesForFullHeal = 1.0f; // 1 frame, instant heal by default (keeps away from divide by 0's)
  38. } // end RepairDockUpdateModuleData
  39. // ------------------------------------------------------------------------------------------------
  40. // ------------------------------------------------------------------------------------------------
  41. /*static*/ void RepairDockUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  42. {
  43. DockUpdateModuleData::buildFieldParse( p );
  44. static const FieldParse dataFieldParse[] =
  45. {
  46. { "TimeForFullHeal", INI::parseDurationReal, NULL, offsetof( RepairDockUpdateModuleData, m_framesForFullHeal ) },
  47. { 0, 0, 0, 0 }
  48. };
  49. p.add(dataFieldParse);
  50. } // end buildFieldParse
  51. ///////////////////////////////////////////////////////////////////////////////////////////////////
  52. ///////////////////////////////////////////////////////////////////////////////////////////////////
  53. ///////////////////////////////////////////////////////////////////////////////////////////////////
  54. // ------------------------------------------------------------------------------------------------
  55. // ------------------------------------------------------------------------------------------------
  56. RepairDockUpdate::RepairDockUpdate( Thing *thing, const ModuleData* moduleData )
  57. : DockUpdate( thing, moduleData )
  58. {
  59. m_lastRepair = INVALID_ID;
  60. m_healthToAddPerFrame = 0.0f;
  61. } // end RepairDockUpdate
  62. // ------------------------------------------------------------------------------------------------
  63. // ------------------------------------------------------------------------------------------------
  64. RepairDockUpdate::~RepairDockUpdate( void )
  65. {
  66. } // end ~RepairDockUpdate
  67. // ------------------------------------------------------------------------------------------------
  68. /** Do the action while docked
  69. * Return TRUE to continue the docking process
  70. * Return FALSE to complete the dockin process */
  71. // ------------------------------------------------------------------------------------------------
  72. Bool RepairDockUpdate::action( Object *docker, Object *drone )
  73. {
  74. // sanity
  75. if( docker == NULL )
  76. return FALSE;
  77. // get our module data
  78. const RepairDockUpdateModuleData *modData = getRepairDockUpdateModuleData();
  79. // get the body module for the docker
  80. BodyModuleInterface *body = docker->getBodyModule();
  81. //
  82. // no matter if the object is damaged just a little bit, or on the brink of death, we will
  83. // heal the object over a fixed amount of *TIME* that is specified in the INI file. Whenever
  84. // we get a new docker, given its current health we figure out how much health we will add
  85. // to this docked object each frame so that it is fully healed after the correct amount
  86. // of time has passed
  87. //
  88. if( m_lastRepair == 0 )
  89. {
  90. // save ID of this docker as the last docker
  91. m_lastRepair = docker->getID();
  92. //
  93. // figure out how much health we need to add each frame to this object so that it's
  94. // fully healed at the right time
  95. //
  96. m_healthToAddPerFrame = (body->getMaxHealth() - body->getHealth()) / modData->m_framesForFullHeal;
  97. } // end if
  98. // if we're at max health we're done
  99. if( body->getHealth() >= body->getMaxHealth() )
  100. {
  101. // repair is complete, clear our last docker
  102. m_lastRepair = INVALID_ID;
  103. // returning false will complete the docking process
  104. return FALSE;
  105. } // end if
  106. // give us some health buddy
  107. DamageInfo healingInfo;
  108. healingInfo.in.m_amount = m_healthToAddPerFrame;
  109. healingInfo.in.m_sourceID = getObject()->getID();
  110. healingInfo.in.m_damageType = DAMAGE_HEALING;
  111. healingInfo.in.m_deathType = DEATH_NONE;
  112. body->attemptHealing( &healingInfo );
  113. if( drone )
  114. {
  115. body = drone->getBodyModule();
  116. healingInfo.in.m_amount = body->getMaxHealth();
  117. body->attemptHealing( &healingInfo );
  118. }
  119. // stay docked
  120. return TRUE;
  121. } // end action
  122. // ------------------------------------------------------------------------------------------------
  123. /** CRC */
  124. // ------------------------------------------------------------------------------------------------
  125. void RepairDockUpdate::crc( Xfer *xfer )
  126. {
  127. // extend base class
  128. DockUpdate::crc( xfer );
  129. } // end crc
  130. // ------------------------------------------------------------------------------------------------
  131. /** Xfer method
  132. * Version Info:
  133. * 1: Initial version */
  134. // ------------------------------------------------------------------------------------------------
  135. void RepairDockUpdate::xfer( Xfer *xfer )
  136. {
  137. // version
  138. XferVersion currentVersion = 1;
  139. XferVersion version = currentVersion;
  140. xfer->xferVersion( &version, currentVersion );
  141. // extend base class
  142. DockUpdate::xfer( xfer );
  143. // last repair
  144. xfer->xferObjectID( &m_lastRepair );
  145. // health to add per frame
  146. xfer->xferReal( &m_healthToAddPerFrame );
  147. } // end xfer
  148. // ------------------------------------------------------------------------------------------------
  149. /** Load post process */
  150. // ------------------------------------------------------------------------------------------------
  151. void RepairDockUpdate::loadPostProcess( void )
  152. {
  153. // extend base class
  154. DockUpdate::loadPostProcess();
  155. } // end loadPostProcess