AutoHealBehavior.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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: AutoHealBehavior.cpp ///////////////////////////////////////////////////////////////////////
  24. // Author:
  25. // Desc:
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/Thing.h"
  30. #include "Common/ThingTemplate.h"
  31. #include "Common/INI.h"
  32. #include "Common/Player.h"
  33. #include "Common/Xfer.h"
  34. #include "GameClient/ParticleSys.h"
  35. #include "GameClient/Anim2D.h"
  36. #include "GameClient/InGameUI.h"
  37. #include "GameLogic/Module/AutoHealBehavior.h"
  38. #include "GameLogic/Module/BodyModule.h"
  39. #include "GameLogic/GameLogic.h"
  40. #include "GameLogic/Object.h"
  41. #include "GameLogic/PartitionManager.h"
  42. //-------------------------------------------------------------------------------------------------
  43. //-------------------------------------------------------------------------------------------------
  44. struct AutoHealPlayerScanHelper
  45. {
  46. KindOfMaskType m_kindOfToTest;
  47. Object *m_theHealer;
  48. ObjectPointerList *m_objectList;
  49. };
  50. static void checkForAutoHeal( Object *testObj, void *userData )
  51. {
  52. AutoHealPlayerScanHelper *helper = (AutoHealPlayerScanHelper*)userData;
  53. ObjectPointerList *listToAddTo = helper->m_objectList;
  54. if( testObj->isEffectivelyDead() )
  55. return;
  56. if( testObj->getControllingPlayer() != helper->m_theHealer->getControllingPlayer() )
  57. return;
  58. if( testObj->isOffMap() )
  59. return;
  60. if( !testObj->isAnyKindOf(helper->m_kindOfToTest) )
  61. return;
  62. if( testObj->getBodyModule()->getHealth() >= testObj->getBodyModule()->getMaxHealth() )
  63. return;
  64. listToAddTo->push_back(testObj);
  65. }
  66. //-------------------------------------------------------------------------------------------------
  67. //-------------------------------------------------------------------------------------------------
  68. AutoHealBehavior::AutoHealBehavior( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  69. {
  70. const AutoHealBehaviorModuleData *d = getAutoHealBehaviorModuleData();
  71. m_radiusParticleSystemID = INVALID_PARTICLE_SYSTEM_ID;
  72. m_soonestHealFrame = 0;
  73. m_stopped = false;
  74. Object *obj = getObject();
  75. {
  76. if( d->m_radiusParticleSystemTmpl )
  77. {
  78. ParticleSystem *particleSystem;
  79. particleSystem = TheParticleSystemManager->createParticleSystem( d->m_radiusParticleSystemTmpl );
  80. if( particleSystem )
  81. {
  82. particleSystem->setPosition( obj->getPosition() );
  83. m_radiusParticleSystemID = particleSystem->getSystemID();
  84. }
  85. }
  86. }
  87. if (d->m_initiallyActive)
  88. {
  89. giveSelfUpgrade();
  90. // start these guys with random phasings so that we don't
  91. // have all of 'em check on the same frame.
  92. UnsignedInt delay = getAutoHealBehaviorModuleData()->m_healingDelay;
  93. setWakeFrame(getObject(), UPDATE_SLEEP(GameLogicRandomValue(1, delay)));
  94. }
  95. else
  96. {
  97. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  98. }
  99. }
  100. //-------------------------------------------------------------------------------------------------
  101. //-------------------------------------------------------------------------------------------------
  102. AutoHealBehavior::~AutoHealBehavior( void )
  103. {
  104. if( m_radiusParticleSystemID != INVALID_PARTICLE_SYSTEM_ID )
  105. TheParticleSystemManager->destroyParticleSystemByID( m_radiusParticleSystemID );
  106. }
  107. //-------------------------------------------------------------------------------------------------
  108. void AutoHealBehavior::stopHealing()
  109. {
  110. m_stopped = true;
  111. m_soonestHealFrame = FOREVER;
  112. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  113. }
  114. //-------------------------------------------------------------------------------------------------
  115. void AutoHealBehavior::undoUpgrade()
  116. {
  117. m_soonestHealFrame = 0;
  118. setUpgradeExecuted( FALSE );
  119. }
  120. //-------------------------------------------------------------------------------------------------
  121. /** Damage has been dealt, this is an opportunity to reach to that damage */
  122. //-------------------------------------------------------------------------------------------------
  123. void AutoHealBehavior::onDamage( DamageInfo *damageInfo )
  124. {
  125. if (m_stopped)
  126. return;
  127. const AutoHealBehaviorModuleData *d = getAutoHealBehaviorModuleData();
  128. if (isUpgradeActive() && d->m_radius == 0.0f)
  129. {
  130. // if this is nonzero, getting damaged resets our healing process. so go to
  131. // sleep for this long.
  132. if (d->m_startHealingDelay > 0)
  133. {
  134. setWakeFrame(getObject(), UPDATE_SLEEP(d->m_startHealingDelay));
  135. }
  136. else if( TheGameLogic->getFrame() > m_soonestHealFrame )
  137. {
  138. // We can only force an immediate wake if we are ready to heal. Otherwise we will
  139. // heal on a timer AND at every damage input.
  140. setWakeFrame(getObject(), UPDATE_SLEEP_NONE);
  141. }
  142. }
  143. }
  144. //-------------------------------------------------------------------------------------------------
  145. /** The update callback. */
  146. //-------------------------------------------------------------------------------------------------
  147. UpdateSleepTime AutoHealBehavior::update( void )
  148. {
  149. if (m_stopped)
  150. return UPDATE_SLEEP_FOREVER;
  151. Object *obj = getObject();
  152. const AutoHealBehaviorModuleData *d = getAutoHealBehaviorModuleData();
  153. // do not heal if our status bit is not on.
  154. // do not heal if our status is effectively dead. There ain't no coming back, man!
  155. if (!isUpgradeActive() || obj->isEffectivelyDead())
  156. {
  157. DEBUG_ASSERTCRASH(isUpgradeActive(), ("hmm, this should not be possible"));
  158. return UPDATE_SLEEP_FOREVER;
  159. }
  160. //DEBUG_LOG(("doing auto heal %d\n",TheGameLogic->getFrame()));
  161. if( d->m_affectsWholePlayer )
  162. {
  163. // Even newer system, I can ignore radius and iterate objects on the owning player. Faster than scanning range 10,000,000
  164. ObjectPointerList objectsToHeal;
  165. Player *owningPlayer = getObject()->getControllingPlayer();
  166. if( owningPlayer )
  167. {
  168. AutoHealPlayerScanHelper helper;
  169. helper.m_kindOfToTest = getAutoHealBehaviorModuleData()->m_kindOf;
  170. helper.m_objectList = &objectsToHeal;
  171. helper.m_theHealer = getObject();
  172. // Smack all objects with this function, and we will end up with a list of Objects deserving of pulseHealObject
  173. owningPlayer->iterateObjects( checkForAutoHeal, &helper );
  174. for( ObjectPointerListIterator iter = objectsToHeal.begin(); iter != objectsToHeal.end(); ++iter )
  175. {
  176. pulseHealObject(*iter);
  177. }
  178. objectsToHeal.clear();
  179. }
  180. return UPDATE_SLEEP(d->m_healingDelay);
  181. }
  182. else if( d->m_radius == 0.0f )
  183. {
  184. //ORIGINAL SYSTEM -- JUST HEAL SELF!
  185. // do not heal if we are at max health already
  186. BodyModuleInterface *body = obj->getBodyModule();
  187. if( body->getHealth() < body->getMaxHealth() )
  188. {
  189. pulseHealObject( obj );
  190. return UPDATE_SLEEP(d->m_healingDelay);
  191. }
  192. else
  193. {
  194. // go to sleep forever -- we'll wake back up when we are damaged again
  195. return UPDATE_SLEEP_FOREVER;
  196. }
  197. }
  198. else
  199. {
  200. //EXPANDED SYSTEM -- HEAL FRIENDLIES IN RADIUS
  201. // setup scan filters
  202. PartitionFilterRelationship relationship( obj, PartitionFilterRelationship::ALLOW_ALLIES );
  203. PartitionFilterSameMapStatus filterMapStatus(obj);
  204. PartitionFilterAlive filterAlive;
  205. PartitionFilter *filters[] = { &relationship, &filterAlive, &filterMapStatus, NULL };
  206. // scan objects in our region
  207. ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( obj->getPosition(), d->m_radius, FROM_CENTER_2D, filters );
  208. MemoryPoolObjectHolder hold( iter );
  209. for( obj = iter->first(); obj; obj = iter->next() )
  210. {
  211. // do not heal if we are at max health already
  212. BodyModuleInterface *body = obj->getBodyModule();
  213. if( body->getHealth() < body->getMaxHealth() && obj->isAnyKindOf( d->m_kindOf ) )
  214. {
  215. pulseHealObject( obj );
  216. if( d->m_singleBurst && TheGameLogic->getDrawIconUI() )
  217. {
  218. if( TheAnim2DCollection && TheGlobalData->m_getHealedAnimationName.isEmpty() == FALSE )
  219. {
  220. Anim2DTemplate *animTemplate = TheAnim2DCollection->findTemplate( TheGlobalData->m_getHealedAnimationName );
  221. if ( animTemplate )
  222. {
  223. Coord3D iconPosition;
  224. iconPosition.set(obj->getPosition()->x,
  225. obj->getPosition()->y,
  226. obj->getPosition()->z + obj->getGeometryInfo().getMaxHeightAbovePosition() );
  227. TheInGameUI->addWorldAnimation( animTemplate, &iconPosition, WORLD_ANIM_FADE_ON_EXPIRE,
  228. TheGlobalData->m_getHealedAnimationDisplayTimeInSeconds,
  229. TheGlobalData->m_getHealedAnimationZRisePerSecond);
  230. }
  231. }
  232. }
  233. }
  234. } // end for obj
  235. return UPDATE_SLEEP( d->m_singleBurst ? UPDATE_SLEEP_FOREVER : d->m_healingDelay );
  236. }
  237. }
  238. //-------------------------------------------------------------------------------------------------
  239. //-------------------------------------------------------------------------------------------------
  240. void AutoHealBehavior::pulseHealObject( Object *obj )
  241. {
  242. if (m_stopped)
  243. return;
  244. const AutoHealBehaviorModuleData *data = getAutoHealBehaviorModuleData();
  245. if ( data->m_radius == 0.0f )
  246. obj->attemptHealing(data->m_healingAmount, getObject());
  247. else
  248. obj->attemptHealingFromSoleBenefactor( data->m_healingAmount, getObject(), data->m_healingDelay );
  249. if( data->m_unitHealPulseParticleSystemTmpl )
  250. {
  251. ParticleSystem *system = TheParticleSystemManager->createParticleSystem( data->m_unitHealPulseParticleSystemTmpl );
  252. if( system )
  253. {
  254. system->setPosition( obj->getPosition() );
  255. }
  256. }
  257. m_soonestHealFrame = TheGameLogic->getFrame() + data->m_healingDelay;// In case onDamage tries to wake us up early
  258. }
  259. // ------------------------------------------------------------------------------------------------
  260. /** CRC */
  261. // ------------------------------------------------------------------------------------------------
  262. void AutoHealBehavior::crc( Xfer *xfer )
  263. {
  264. // extend base class
  265. UpdateModule::crc( xfer );
  266. // extend base class
  267. UpgradeMux::upgradeMuxCRC( xfer );
  268. } // end crc
  269. // ------------------------------------------------------------------------------------------------
  270. /** Xfer method
  271. * Version Info:
  272. * 1: Initial version */
  273. // ------------------------------------------------------------------------------------------------
  274. void AutoHealBehavior::xfer( Xfer *xfer )
  275. {
  276. // version
  277. XferVersion currentVersion = 1;
  278. XferVersion version = currentVersion;
  279. xfer->xferVersion( &version, currentVersion );
  280. // extend base class
  281. UpdateModule::xfer( xfer );
  282. // extend base class
  283. UpgradeMux::upgradeMuxXfer( xfer );
  284. // particle system id
  285. xfer->xferUser( &m_radiusParticleSystemID, sizeof( ParticleSystemID ) );
  286. // Timer safety
  287. xfer->xferUnsignedInt( &m_soonestHealFrame );
  288. // stopped
  289. xfer->xferBool( &m_stopped );
  290. } // end xfer
  291. // ------------------------------------------------------------------------------------------------
  292. /** Load post process */
  293. // ------------------------------------------------------------------------------------------------
  294. void AutoHealBehavior::loadPostProcess( void )
  295. {
  296. // extend base class
  297. UpdateModule::loadPostProcess();
  298. // extend base class
  299. UpgradeMux::upgradeMuxLoadPostProcess();
  300. } // end loadPostProcess