AutoFindHealingUpdate.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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: AutoFindHealingUpdate.cpp //////////////////////////////////////////////////////////////////////////
  24. // Author: Kris Morness, August 2002
  25. // Desc: Update module to handle independent targeting of hazards to cleanup.
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
  29. #define DEFINE_WEAPONSLOTTYPE_NAMES
  30. #include "Common\RandomValue.h"
  31. #include "Common\ThingTemplate.h"
  32. #include "Common\Player.h"
  33. #include "Common\Xfer.h"
  34. #include "GameClient\Drawable.h"
  35. #include "GameLogic\GameLogic.h"
  36. #include "GameLogic\PartitionManager.h"
  37. #include "GameLogic\Object.h"
  38. #include "GameLogic\ObjectIter.h"
  39. #include "GameLogic\Module\AutoFindHealingUpdate.h"
  40. #include "GameLogic\Module\PhysicsUpdate.h"
  41. #include "GameLogic\Weapon.h"
  42. #include "GameLogic\WeaponSet.h"
  43. #include "GameLogic\Module\AIUpdate.h"
  44. //-------------------------------------------------------------------------------------------------
  45. //-------------------------------------------------------------------------------------------------
  46. AutoFindHealingUpdateModuleData::AutoFindHealingUpdateModuleData()
  47. {
  48. m_scanFrames = 0;
  49. m_scanRange = 0.0f;
  50. m_neverHeal = 0.95f;
  51. m_alwaysHeal = 0.25f;
  52. }
  53. //-------------------------------------------------------------------------------------------------
  54. /*static*/ void AutoFindHealingUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  55. {
  56. ModuleData::buildFieldParse(p);
  57. static const FieldParse dataFieldParse[] =
  58. {
  59. { "ScanRate", INI::parseDurationUnsignedInt, NULL, offsetof( AutoFindHealingUpdateModuleData, m_scanFrames ) },
  60. { "ScanRange", INI::parseReal, NULL, offsetof( AutoFindHealingUpdateModuleData, m_scanRange ) },
  61. { "NeverHeal", INI::parseReal, NULL, offsetof( AutoFindHealingUpdateModuleData, m_neverHeal ) },
  62. { "AlwaysHeal", INI::parseReal, NULL, offsetof( AutoFindHealingUpdateModuleData, m_alwaysHeal ) },
  63. { 0, 0, 0, 0 }
  64. };
  65. p.add(dataFieldParse);
  66. }
  67. //-------------------------------------------------------------------------------------------------
  68. AutoFindHealingUpdate::AutoFindHealingUpdate( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  69. {
  70. m_nextScanFrames = 0;
  71. }
  72. //-------------------------------------------------------------------------------------------------
  73. //-------------------------------------------------------------------------------------------------
  74. AutoFindHealingUpdate::~AutoFindHealingUpdate( void )
  75. {
  76. }
  77. //-------------------------------------------------------------------------------------------------
  78. void AutoFindHealingUpdate::onObjectCreated()
  79. {
  80. }
  81. //-------------------------------------------------------------------------------------------------
  82. /** The update callback. */
  83. //-------------------------------------------------------------------------------------------------
  84. UpdateSleepTime AutoFindHealingUpdate::update()
  85. {
  86. /// @todo srj use SLEEPY_UPDATE here
  87. Object *obj = getObject();
  88. if (obj->getControllingPlayer()->getPlayerType() == PLAYER_HUMAN) {
  89. return UPDATE_SLEEP_NONE;
  90. }
  91. const AutoFindHealingUpdateModuleData *data = getAutoFindHealingUpdateModuleData();
  92. //Optimized firing at acquired target
  93. if( m_nextScanFrames > 0 )
  94. {
  95. m_nextScanFrames--;
  96. return UPDATE_SLEEP_NONE;
  97. }
  98. m_nextScanFrames = data->m_scanFrames;
  99. AIUpdateInterface *ai = obj->getAI();
  100. if (ai==NULL) return UPDATE_SLEEP_NONE;
  101. // Check health.
  102. BodyModuleInterface *body = obj->getBodyModule();
  103. if (!body) return UPDATE_SLEEP_NONE;
  104. // If we're real healthy, don't bother looking for healing.
  105. if (body->getHealth() > body->getMaxHealth()*data->m_neverHeal) {
  106. return UPDATE_SLEEP_NONE;
  107. }
  108. if( !ai->isIdle() )
  109. {
  110. // For now, only heal if idle. jba.
  111. return UPDATE_SLEEP_NONE;
  112. // If we're > min health, and busy, keep at it.
  113. if (body->getHealth() > body->getMaxHealth()*data->m_alwaysHeal) {
  114. return UPDATE_SLEEP_NONE;
  115. }
  116. }
  117. //Periodic scanning (expensive)
  118. Object *healUnit = scanClosestTarget();
  119. if(healUnit)
  120. {
  121. ai->aiGetHealed(healUnit, CMD_FROM_AI);
  122. }
  123. return UPDATE_SLEEP_NONE;
  124. }
  125. //-------------------------------------------------------------------------------------------------
  126. Object* AutoFindHealingUpdate::scanClosestTarget()
  127. {
  128. const AutoFindHealingUpdateModuleData *data = getAutoFindHealingUpdateModuleData();
  129. Object *me = getObject();
  130. Object *bestTarget = NULL;
  131. Real closestDistSqr=0;
  132. ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( me->getPosition(), data->m_scanRange, FROM_CENTER_2D );
  133. MemoryPoolObjectHolder hold(iter);
  134. for( Object *other = iter->first(); other; other = iter->next() )
  135. {
  136. if( !other->isKindOf( KINDOF_HEAL_PAD ) )
  137. {
  138. //Not a valid target.
  139. continue;
  140. }
  141. Real fDistSqr = ThePartitionManager->getDistanceSquared( me, other, FROM_CENTER_2D ) ;
  142. if (bestTarget==NULL) {
  143. bestTarget = other;
  144. closestDistSqr = fDistSqr;
  145. continue;
  146. }
  147. if( fDistSqr < closestDistSqr )
  148. {
  149. bestTarget = other;
  150. closestDistSqr = fDistSqr;
  151. continue;
  152. }
  153. } // end for, other
  154. return bestTarget;
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. /** CRC */
  158. // ------------------------------------------------------------------------------------------------
  159. void AutoFindHealingUpdate::crc( Xfer *xfer )
  160. {
  161. // extend base class
  162. UpdateModule::crc( xfer );
  163. } // end crc
  164. // ------------------------------------------------------------------------------------------------
  165. /** Xfer method
  166. * Version Info:
  167. * 1: Initial version */
  168. // ------------------------------------------------------------------------------------------------
  169. void AutoFindHealingUpdate::xfer( Xfer *xfer )
  170. {
  171. // version
  172. XferVersion currentVersion = 1;
  173. XferVersion version = currentVersion;
  174. xfer->xferVersion( &version, currentVersion );
  175. // extend base class
  176. UpdateModule::xfer( xfer );
  177. // next scan frames
  178. xfer->xferInt( &m_nextScanFrames );
  179. } // end xfer
  180. // ------------------------------------------------------------------------------------------------
  181. /** Load post process */
  182. // ------------------------------------------------------------------------------------------------
  183. void AutoFindHealingUpdate::loadPostProcess( void )
  184. {
  185. // extend base class
  186. UpdateModule::loadPostProcess();
  187. } // end loadPostProcess