OverchargeBehavior.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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: OverchargeBehavior.cpp ///////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, June 2002
  25. // Desc: Objects with this behavior module will get the ability to produce more power
  26. // for a short amount of time, during this "overcharge" state object health is
  27. // slowly reduced
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////
  29. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  30. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  31. #include "Common/Player.h"
  32. #include "Common/PlayerList.h"
  33. #include "Common/Radar.h"
  34. #include "Common/Xfer.h"
  35. #include "GameLogic/GameLogic.h"
  36. #include "GameLogic/Object.h"
  37. #include "GameLogic/Module/BodyModule.h"
  38. #include "GameLogic/Module/OverchargeBehavior.h"
  39. #include "GameLogic/Module/PowerPlantUpdate.h"
  40. #include "GameClient/InGameUI.h"
  41. #ifdef _INTERNAL
  42. // for occasional debugging...
  43. //#pragma optimize("", off)
  44. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  45. #endif
  46. //-------------------------------------------------------------------------------------------------
  47. // ------------------------------------------------------------------------------------------------
  48. OverchargeBehaviorModuleData::OverchargeBehaviorModuleData( void )
  49. {
  50. m_healthPercentToDrainPerSecond = 0.0f;
  51. m_notAllowedWhenHealthBelowPercent = 0.0f;
  52. } // end OverchargeBehaviorModuleData
  53. //-------------------------------------------------------------------------------------------------
  54. // ------------------------------------------------------------------------------------------------
  55. /*static*/ void OverchargeBehaviorModuleData::buildFieldParse( MultiIniFieldParse &p )
  56. {
  57. UpdateModuleData::buildFieldParse( p );
  58. static const FieldParse dataFieldParse[] =
  59. {
  60. { "HealthPercentToDrainPerSecond", INI::parsePercentToReal, NULL, offsetof( OverchargeBehaviorModuleData, m_healthPercentToDrainPerSecond ) },
  61. { "NotAllowedWhenHealthBelowPercent", INI::parsePercentToReal, NULL, offsetof( OverchargeBehaviorModuleData, m_notAllowedWhenHealthBelowPercent ) },
  62. { 0, 0, 0, 0 }
  63. };
  64. p.add( dataFieldParse );
  65. } // end buildFieldParse
  66. ///////////////////////////////////////////////////////////////////////////////////////////////////
  67. ///////////////////////////////////////////////////////////////////////////////////////////////////
  68. ///////////////////////////////////////////////////////////////////////////////////////////////////
  69. //-------------------------------------------------------------------------------------------------
  70. //-------------------------------------------------------------------------------------------------
  71. OverchargeBehavior::OverchargeBehavior( Thing *thing, const ModuleData* moduleData )
  72. : UpdateModule( thing, moduleData )
  73. {
  74. m_overchargeActive = FALSE;
  75. // start off sleeping forever until we become active
  76. setWakeFrame( getObject(), UPDATE_SLEEP_FOREVER );
  77. } // end OverchargeBehavior
  78. //-------------------------------------------------------------------------------------------------
  79. //-------------------------------------------------------------------------------------------------
  80. OverchargeBehavior::~OverchargeBehavior( void )
  81. {
  82. } // end ~OverchargeBehavior
  83. //-------------------------------------------------------------------------------------------------
  84. //-------------------------------------------------------------------------------------------------
  85. UpdateSleepTime OverchargeBehavior::update( void )
  86. {
  87. // if the overcharge is active we need to take away some life
  88. if( m_overchargeActive )
  89. {
  90. Object *us = getObject();
  91. // get mod data
  92. const OverchargeBehaviorModuleData *modData = getOverchargeBehaviorModuleData();
  93. // do some damage
  94. BodyModuleInterface *body = us->getBodyModule();
  95. DamageInfo damageInfo;
  96. damageInfo.in.m_amount = (body->getMaxHealth() * modData->m_healthPercentToDrainPerSecond) / LOGICFRAMES_PER_SECOND;
  97. damageInfo.in.m_sourceID = us->getID();
  98. damageInfo.in.m_damageType = DAMAGE_PENALTY;
  99. damageInfo.in.m_deathType = DEATH_NORMAL;
  100. us->attemptDamage( &damageInfo );
  101. // see if our health is below the allowable threshold
  102. if( body->getHealth() < body->getMaxHealth() * modData->m_notAllowedWhenHealthBelowPercent )
  103. {
  104. // turn off the overcharge
  105. enable( FALSE );
  106. // do some UI info for the local user if this is theirs
  107. if( ThePlayerList->getLocalPlayer() == us->getControllingPlayer() )
  108. {
  109. // print msg
  110. TheInGameUI->message( "GUI:OverchargeExhausted" );
  111. // do radar event
  112. TheRadar->createEvent( us->getPosition(), RADAR_EVENT_INFORMATION );
  113. } // end of
  114. // do nothing else
  115. return UPDATE_SLEEP_NONE;
  116. } // end if
  117. } // end if
  118. return UPDATE_SLEEP_NONE;
  119. } // end update
  120. // ------------------------------------------------------------------------------------------------
  121. //-------------------------------------------------------------------------------------------------
  122. void OverchargeBehavior::onDamage( DamageInfo *damageInfo )
  123. {
  124. } // end onDie
  125. // ------------------------------------------------------------------------------------------------
  126. /** Flip the state of our 'overcharge-ness' */
  127. // ------------------------------------------------------------------------------------------------
  128. void OverchargeBehavior::toggle( void )
  129. {
  130. // just toggle using enable()
  131. enable( !m_overchargeActive );
  132. } // end toggle
  133. // ------------------------------------------------------------------------------------------------
  134. /** Enable or disable an overcharge */
  135. // ------------------------------------------------------------------------------------------------
  136. void OverchargeBehavior::enable( Bool enable )
  137. {
  138. Object *us = getObject();
  139. if( enable == FALSE )
  140. {
  141. // if we're turned on, turn off
  142. if( m_overchargeActive == TRUE )
  143. {
  144. // make sure to NOT extend rods for purpose of maintaining proper model condition
  145. PowerPlantUpdateInterface *ppui;
  146. for( BehaviorModule **umi = getObject()->getBehaviorModules(); *umi; ++umi)
  147. {
  148. ppui = (*umi)->getPowerPlantUpdateInterface();
  149. if( ppui )
  150. ppui->extendRods(FALSE);
  151. }
  152. Player *player = us->getControllingPlayer();
  153. if ( player )
  154. player->removePowerBonus(us);
  155. // we are no longer active
  156. m_overchargeActive = FALSE;
  157. // sleep forever
  158. setWakeFrame( us, UPDATE_SLEEP_FOREVER );
  159. } // end if
  160. } // end if
  161. else
  162. {
  163. // if we're turned off, turn on
  164. if( m_overchargeActive == FALSE )
  165. {
  166. // make sure to extend rods for purpose of maintaining proper model condition
  167. PowerPlantUpdateInterface *ppui;
  168. for( BehaviorModule **umi = getObject()->getBehaviorModules(); *umi; ++umi)
  169. {
  170. ppui = (*umi)->getPowerPlantUpdateInterface();
  171. if( ppui )
  172. ppui->extendRods(TRUE);
  173. }
  174. // add the power bonus
  175. Player *player = us->getControllingPlayer();
  176. if ( player )
  177. player->addPowerBonus(us);
  178. // we are now active
  179. m_overchargeActive = TRUE;
  180. // need to update every frame now
  181. setWakeFrame( us, UPDATE_SLEEP_NONE );
  182. } // end if
  183. } // end else
  184. } // end enable
  185. //-------------------------------------------------------------------------------------------------
  186. //-------------------------------------------------------------------------------------------------
  187. void OverchargeBehavior::onDelete( void )
  188. {
  189. // if we haven't been upgraded there is nothing to clean up
  190. if( m_overchargeActive == FALSE )
  191. return;
  192. // remove the power bonus from the player
  193. Player *player = getObject()->getControllingPlayer();
  194. if( player )
  195. player->removePowerBonus( getObject() );
  196. m_overchargeActive = FALSE;
  197. } // end onDelete
  198. //-------------------------------------------------------------------------------------------------
  199. //-------------------------------------------------------------------------------------------------
  200. void OverchargeBehavior::onCapture( Player *oldOwner, Player *newOwner )
  201. {
  202. // do nothing if we haven't upgraded yet
  203. if( m_overchargeActive == FALSE )
  204. return;
  205. if (getObject()->isDisabled())
  206. return;
  207. // remove power bonus from old owner
  208. if( oldOwner )
  209. oldOwner->removePowerBonus( getObject() );
  210. // add power bonus to the new owner
  211. if( newOwner )
  212. newOwner->addPowerBonus( getObject() );
  213. } // end onCapture
  214. // ------------------------------------------------------------------------------------------------
  215. /** CRC */
  216. // ------------------------------------------------------------------------------------------------
  217. void OverchargeBehavior::crc( Xfer *xfer )
  218. {
  219. // extend base class
  220. UpdateModule::crc( xfer );
  221. } // end crc
  222. // ------------------------------------------------------------------------------------------------
  223. /** Xfer method
  224. * Version Info:
  225. * 1: Initial version */
  226. // ------------------------------------------------------------------------------------------------
  227. void OverchargeBehavior::xfer( Xfer *xfer )
  228. {
  229. // version
  230. XferVersion currentVersion = 1;
  231. XferVersion version = currentVersion;
  232. xfer->xferVersion( &version, currentVersion );
  233. // extend base class
  234. UpdateModule::xfer( xfer );
  235. // overcharge active
  236. xfer->xferBool( &m_overchargeActive );
  237. } // end xfer
  238. // ------------------------------------------------------------------------------------------------
  239. /** Load post process */
  240. // ------------------------------------------------------------------------------------------------
  241. void OverchargeBehavior::loadPostProcess( void )
  242. {
  243. // extend base class
  244. UpdateModule::loadPostProcess();
  245. // Our effect is a fire and forget effect, not an upgrade state that is itself saved, so need to re-fire.
  246. if( m_overchargeActive && getObject()->getControllingPlayer() )
  247. getObject()->getControllingPlayer()->addPowerBonus( getObject() );
  248. } // end loadPostProcess