PoisonedBehavior.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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: PoisonedBehavior.cpp /////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, July 2002
  25. // Desc: Behavior that reacts to poison Damage by continuously damaging us further in an Update
  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 "GameClient/Drawable.h"
  31. #include "GameLogic/Module/PoisonedBehavior.h"
  32. #include "GameLogic/Damage.h"
  33. #include "GameLogic/GameLogic.h"
  34. #include "GameLogic/Object.h"
  35. #ifdef _INTERNAL
  36. // for occasional debugging...
  37. //#pragma optimize("", off)
  38. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  39. #endif
  40. // tinting is all handled in drawable, now, Graham look near the bottom of Drawable::UpdateDrawable()
  41. //static const RGBColor poisonedTint = {0.0f, 1.0f, 0.0f};
  42. //-------------------------------------------------------------------------------------------------
  43. PoisonedBehaviorModuleData::PoisonedBehaviorModuleData()
  44. {
  45. m_poisonDamageIntervalData = 0; // How often I retake poison damage dealt me
  46. m_poisonDurationData = 0; // And how long after the last poison dose I am poisoned
  47. }
  48. //-------------------------------------------------------------------------------------------------
  49. /*static*/ void PoisonedBehaviorModuleData::buildFieldParse(MultiIniFieldParse& p)
  50. {
  51. static const FieldParse dataFieldParse[] =
  52. {
  53. { "PoisonDamageInterval", INI::parseDurationUnsignedInt, NULL, offsetof(PoisonedBehaviorModuleData, m_poisonDamageIntervalData) },
  54. { "PoisonDuration", INI::parseDurationUnsignedInt, NULL, offsetof(PoisonedBehaviorModuleData, m_poisonDurationData) },
  55. { 0, 0, 0, 0 }
  56. };
  57. UpdateModuleData::buildFieldParse(p);
  58. p.add(dataFieldParse);
  59. }
  60. //-------------------------------------------------------------------------------------------------
  61. //-------------------------------------------------------------------------------------------------
  62. PoisonedBehavior::PoisonedBehavior( Thing *thing, const ModuleData* moduleData ) : UpdateModule( thing, moduleData )
  63. {
  64. m_poisonDamageFrame = 0;
  65. m_poisonOverallStopFrame = 0;
  66. m_poisonDamageAmount = 0.0f;
  67. m_deathType = DEATH_POISONED;
  68. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  69. }
  70. //-------------------------------------------------------------------------------------------------
  71. //-------------------------------------------------------------------------------------------------
  72. PoisonedBehavior::~PoisonedBehavior( void )
  73. {
  74. }
  75. //-------------------------------------------------------------------------------------------------
  76. /** Damage has been dealt, this is an opportunity to react to that damage */
  77. //-------------------------------------------------------------------------------------------------
  78. void PoisonedBehavior::onDamage( DamageInfo *damageInfo )
  79. {
  80. if( damageInfo->in.m_damageType == DAMAGE_POISON )
  81. startPoisonedEffects( damageInfo );
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. // ------------------------------------------------------------------------------------------------
  85. void PoisonedBehavior::onHealing( DamageInfo *damageInfo )
  86. {
  87. stopPoisonedEffects();
  88. setWakeFrame(getObject(), UPDATE_SLEEP_FOREVER);
  89. }
  90. // ------------------------------------------------------------------------------------------------
  91. // ------------------------------------------------------------------------------------------------
  92. UpdateSleepTime PoisonedBehavior::update()
  93. {
  94. const PoisonedBehaviorModuleData* d = getPoisonedBehaviorModuleData();
  95. UnsignedInt now = TheGameLogic->getFrame();
  96. if( m_poisonOverallStopFrame == 0 )
  97. {
  98. DEBUG_CRASH(("hmm, this should not happen"));
  99. return UPDATE_SLEEP_FOREVER;
  100. //we aren't poisoned, so nevermind
  101. }
  102. if (m_poisonDamageFrame != 0 && now >= m_poisonDamageFrame)
  103. {
  104. // If it is time to do damage, then do it and reset the damage timer
  105. DamageInfo damage;
  106. damage.in.m_amount = m_poisonDamageAmount;
  107. damage.in.m_sourceID = INVALID_ID;
  108. damage.in.m_damageType = DAMAGE_UNRESISTABLE; // Not poison, as that will infect us again
  109. damage.in.m_damageFXOverride = DAMAGE_POISON; // but this will ensure that the right effect is played
  110. damage.in.m_deathType = m_deathType;
  111. getObject()->attemptDamage( &damage );
  112. m_poisonDamageFrame = now + d->m_poisonDamageIntervalData;
  113. }
  114. // If we are now at zero we need to turn off our special effects...
  115. // unless the poison killed us, then we continue to be a pulsating toxic pus ball
  116. if( m_poisonOverallStopFrame != 0 &&
  117. now >= m_poisonOverallStopFrame &&
  118. !getObject()->isEffectivelyDead())
  119. {
  120. stopPoisonedEffects();
  121. }
  122. return calcSleepTime();
  123. }
  124. // ------------------------------------------------------------------------------------------------
  125. // ------------------------------------------------------------------------------------------------
  126. UpdateSleepTime PoisonedBehavior::calcSleepTime()
  127. {
  128. // UPDATE_SLEEP requires a count-of-frames, not an absolute-frame, so subtract 'now'
  129. UnsignedInt now = TheGameLogic->getFrame();
  130. if (m_poisonOverallStopFrame == 0 || m_poisonOverallStopFrame == now)
  131. return UPDATE_SLEEP_FOREVER;
  132. return frameToSleepTime(m_poisonDamageFrame, m_poisonOverallStopFrame);
  133. }
  134. // ------------------------------------------------------------------------------------------------
  135. // ------------------------------------------------------------------------------------------------
  136. void PoisonedBehavior::startPoisonedEffects( const DamageInfo *damageInfo )
  137. {
  138. const PoisonedBehaviorModuleData* d = getPoisonedBehaviorModuleData();
  139. UnsignedInt now = TheGameLogic->getFrame();
  140. // We are going to take the damage dealt by the original poisoner every so often for a while.
  141. m_poisonDamageAmount = damageInfo->out.m_actualDamageDealt;
  142. m_poisonOverallStopFrame = now + d->m_poisonDurationData;
  143. // If we are getting re-poisoned, don't reset the damage counter if running, but do set it if unset
  144. if( m_poisonDamageFrame != 0 )
  145. m_poisonDamageFrame = min( m_poisonDamageFrame, now + d->m_poisonDamageIntervalData );
  146. else
  147. m_poisonDamageFrame = now + d->m_poisonDamageIntervalData;
  148. m_deathType = damageInfo->in.m_deathType;
  149. Drawable *myDrawable = getObject()->getDrawable();
  150. if( myDrawable )
  151. myDrawable->setTintStatus( TINT_STATUS_POISONED );// Graham, It has changed, see UpdateDrawable()
  152. setWakeFrame(getObject(), calcSleepTime());
  153. }
  154. // ------------------------------------------------------------------------------------------------
  155. // ------------------------------------------------------------------------------------------------
  156. void PoisonedBehavior::stopPoisonedEffects()
  157. {
  158. m_poisonDamageFrame = 0;
  159. m_poisonOverallStopFrame = 0;
  160. m_poisonDamageAmount = 0.0f;
  161. Drawable *myDrawable = getObject()->getDrawable();
  162. if( myDrawable )
  163. myDrawable->clearTintStatus( TINT_STATUS_POISONED );// Graham, It has changed, see UpdateDrawable()
  164. }
  165. // ------------------------------------------------------------------------------------------------
  166. /** CRC */
  167. // ------------------------------------------------------------------------------------------------
  168. void PoisonedBehavior::crc( Xfer *xfer )
  169. {
  170. // extend base class
  171. UpdateModule::crc( xfer );
  172. } // end crc
  173. // ------------------------------------------------------------------------------------------------
  174. /** Xfer method
  175. * Version Info:
  176. * 1: Initial version */
  177. // ------------------------------------------------------------------------------------------------
  178. void PoisonedBehavior::xfer( Xfer *xfer )
  179. {
  180. // version
  181. const XferVersion currentVersion = 2;
  182. XferVersion version = currentVersion;
  183. xfer->xferVersion( &version, currentVersion );
  184. // extend base class
  185. UpdateModule::xfer( xfer );
  186. // poisoned damage frame
  187. xfer->xferUnsignedInt( &m_poisonDamageFrame );
  188. // poison overall stop frame
  189. xfer->xferUnsignedInt( &m_poisonOverallStopFrame );
  190. // poison damage amount
  191. xfer->xferReal( &m_poisonDamageAmount );
  192. if (version >= 2)
  193. {
  194. xfer->xferUser(&m_deathType, sizeof(m_deathType));
  195. }
  196. } // end xfer
  197. // ------------------------------------------------------------------------------------------------
  198. /** Load post process */
  199. // ------------------------------------------------------------------------------------------------
  200. void PoisonedBehavior::loadPostProcess( void )
  201. {
  202. // extend base class
  203. UpdateModule::loadPostProcess();
  204. } // end loadPostProcess