PoisonedBehavior.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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: 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_deathType = m_deathType;
  110. getObject()->attemptDamage( &damage );
  111. m_poisonDamageFrame = now + d->m_poisonDamageIntervalData;
  112. }
  113. // If we are now at zero we need to turn off our special effects...
  114. // unless the poison killed us, then we continue to be a pulsating toxic pus ball
  115. if( m_poisonOverallStopFrame != 0 &&
  116. now >= m_poisonOverallStopFrame &&
  117. !getObject()->isEffectivelyDead())
  118. {
  119. stopPoisonedEffects();
  120. }
  121. return calcSleepTime();
  122. }
  123. // ------------------------------------------------------------------------------------------------
  124. // ------------------------------------------------------------------------------------------------
  125. UpdateSleepTime PoisonedBehavior::calcSleepTime()
  126. {
  127. // UPDATE_SLEEP requires a count-of-frames, not an absolute-frame, so subtract 'now'
  128. UnsignedInt now = TheGameLogic->getFrame();
  129. if (m_poisonOverallStopFrame == 0 || m_poisonOverallStopFrame == now)
  130. return UPDATE_SLEEP_FOREVER;
  131. return frameToSleepTime(m_poisonDamageFrame, m_poisonOverallStopFrame);
  132. }
  133. // ------------------------------------------------------------------------------------------------
  134. // ------------------------------------------------------------------------------------------------
  135. void PoisonedBehavior::startPoisonedEffects( const DamageInfo *damageInfo )
  136. {
  137. const PoisonedBehaviorModuleData* d = getPoisonedBehaviorModuleData();
  138. UnsignedInt now = TheGameLogic->getFrame();
  139. // We are going to take the damage dealt by the original poisoner every so often for a while.
  140. m_poisonDamageAmount = damageInfo->out.m_actualDamageDealt;
  141. m_poisonOverallStopFrame = now + d->m_poisonDurationData;
  142. // If we are getting re-poisoned, don't reset the damage counter if running, but do set it if unset
  143. if( m_poisonDamageFrame != 0 )
  144. m_poisonDamageFrame = min( m_poisonDamageFrame, now + d->m_poisonDamageIntervalData );
  145. else
  146. m_poisonDamageFrame = now + d->m_poisonDamageIntervalData;
  147. m_deathType = damageInfo->in.m_deathType;
  148. Drawable *myDrawable = getObject()->getDrawable();
  149. if( myDrawable )
  150. myDrawable->setTintStatus( TINT_STATUS_POISONED );// Graham, It has changed, see UpdateDrawable()
  151. setWakeFrame(getObject(), calcSleepTime());
  152. }
  153. // ------------------------------------------------------------------------------------------------
  154. // ------------------------------------------------------------------------------------------------
  155. void PoisonedBehavior::stopPoisonedEffects()
  156. {
  157. m_poisonDamageFrame = 0;
  158. m_poisonOverallStopFrame = 0;
  159. m_poisonDamageAmount = 0.0f;
  160. Drawable *myDrawable = getObject()->getDrawable();
  161. if( myDrawable )
  162. myDrawable->clearTintStatus( TINT_STATUS_POISONED );// Graham, It has changed, see UpdateDrawable()
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. /** CRC */
  166. // ------------------------------------------------------------------------------------------------
  167. void PoisonedBehavior::crc( Xfer *xfer )
  168. {
  169. // extend base class
  170. UpdateModule::crc( xfer );
  171. } // end crc
  172. // ------------------------------------------------------------------------------------------------
  173. /** Xfer method
  174. * Version Info:
  175. * 1: Initial version */
  176. // ------------------------------------------------------------------------------------------------
  177. void PoisonedBehavior::xfer( Xfer *xfer )
  178. {
  179. // version
  180. const XferVersion currentVersion = 2;
  181. XferVersion version = currentVersion;
  182. xfer->xferVersion( &version, currentVersion );
  183. // extend base class
  184. UpdateModule::xfer( xfer );
  185. // poisoned damage frame
  186. xfer->xferUnsignedInt( &m_poisonDamageFrame );
  187. // poison overall stop frame
  188. xfer->xferUnsignedInt( &m_poisonOverallStopFrame );
  189. // poison damage amount
  190. xfer->xferReal( &m_poisonDamageAmount );
  191. if (version >= 2)
  192. {
  193. xfer->xferUser(&m_deathType, sizeof(m_deathType));
  194. }
  195. } // end xfer
  196. // ------------------------------------------------------------------------------------------------
  197. /** Load post process */
  198. // ------------------------------------------------------------------------------------------------
  199. void PoisonedBehavior::loadPostProcess( void )
  200. {
  201. // extend base class
  202. UpdateModule::loadPostProcess();
  203. } // end loadPostProcess