HiveStructureBody.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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: HiveStructureBody.cpp ////////////////////////////////////////////////////////////////////////
  24. // Desc: Hive structure bodies are structure bodies with the ability to propagate specified
  25. // damage types to slaves when available. If there are no slaves, the the structure
  26. // will take the damage.
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  29. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  30. #include "Common/Xfer.h"
  31. #include "Common/ThingTemplate.h"
  32. #include "GameLogic/GameLogic.h"
  33. #include "GameLogic/Object.h"
  34. #include "GameLogic/Damage.h"
  35. #include "GameLogic/Module/HiveStructureBody.h"
  36. #include "GameLogic/Module/SpawnBehavior.h"
  37. #include "GameLogic/Module/ContainModule.h"
  38. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
  39. //-------------------------------------------------------------------------------------------------
  40. HiveStructureBodyModuleData::HiveStructureBodyModuleData()
  41. {
  42. m_damageTypesToPropagateToSlaves = DAMAGE_TYPE_FLAGS_NONE;
  43. m_damageTypesToSwallow = DAMAGE_TYPE_FLAGS_NONE;
  44. }
  45. //-------------------------------------------------------------------------------------------------
  46. HiveStructureBody::HiveStructureBody( Thing *thing, const ModuleData* moduleData )
  47. : StructureBody( thing, moduleData )
  48. {
  49. }
  50. //-------------------------------------------------------------------------------------------------
  51. HiveStructureBody::~HiveStructureBody( void )
  52. {
  53. }
  54. //------------------------------------------------------------------------------------------------
  55. void HiveStructureBody::attemptDamage( DamageInfo *damageInfo )
  56. {
  57. const HiveStructureBodyModuleData *data = getHiveStructureBodyModuleData();
  58. Object *hive = getObject();
  59. if( getDamageTypeFlag( data->m_damageTypesToPropagateToSlaves, damageInfo->in.m_damageType ) )
  60. {
  61. //We have the right type of damage types incoming to propagate to slaves. Do we have slaves?
  62. SpawnBehaviorInterface *spawnInterface = hive->getSpawnBehaviorInterface();
  63. if( spawnInterface )
  64. {
  65. //We found the spawn interface, now get some slaves!
  66. Object *shooter = TheGameLogic->findObjectByID( damageInfo->in.m_sourceID );
  67. if( shooter )
  68. {
  69. Object *slave = spawnInterface->getClosestSlave( shooter->getPosition() );
  70. if( slave )
  71. {
  72. //Propagate damage and return!
  73. slave->attemptDamage( damageInfo );
  74. return;
  75. }
  76. else if( getDamageTypeFlag( data->m_damageTypesToSwallow, damageInfo->in.m_damageType ) )
  77. {
  78. //no slave to give to, so eat it
  79. damageInfo->out.m_actualDamageDealt = 0.0f;
  80. damageInfo->out.m_actualDamageClipped = 0.0f;
  81. damageInfo->out.m_noEffect = true;
  82. return;
  83. }
  84. }
  85. }
  86. else if ( hive->getContain() )
  87. {
  88. ContainModuleInterface *contain = hive->getContain();
  89. //We found the spawn interface, now get some slaves!
  90. Object *shooter = TheGameLogic->findObjectByID( damageInfo->in.m_sourceID );
  91. if( shooter )
  92. {
  93. Object *rider = contain->getClosestRider( shooter->getPosition() );
  94. if( rider )
  95. {
  96. //Propagate damage and return!
  97. rider->attemptDamage( damageInfo );
  98. return;
  99. }
  100. else if( getDamageTypeFlag( data->m_damageTypesToSwallow, damageInfo->in.m_damageType ) )
  101. {
  102. //no slave to give to, so eat it
  103. damageInfo->out.m_actualDamageDealt = 0.0f;
  104. damageInfo->out.m_actualDamageClipped = 0.0f;
  105. damageInfo->out.m_noEffect = true;
  106. return;
  107. }
  108. }
  109. }
  110. else
  111. {
  112. DEBUG_CRASH( ("%s has a HiveStructureBody module, which requires a SpawnBehavior module. Thus it is unable to propagate damage to slaves.", hive->getTemplate()->getName().str() ) );
  113. }
  114. }
  115. //Nothing to propagate (either different damage type or no slaves),
  116. //so damage me instead!
  117. StructureBody::attemptDamage( damageInfo );
  118. }
  119. //------------------------------------------------------------------------------------------------
  120. void HiveStructureBody::crc( Xfer *xfer )
  121. {
  122. // extend parent class
  123. StructureBody::crc( xfer );
  124. } // end crc
  125. // ------------------------------------------------------------------------------------------------
  126. /** Xfer method
  127. * Version Info:
  128. * 1: Initial version */
  129. // ------------------------------------------------------------------------------------------------
  130. void HiveStructureBody::xfer( Xfer *xfer )
  131. {
  132. // version
  133. XferVersion currentVersion = 1;
  134. XferVersion version = currentVersion;
  135. xfer->xferVersion( &version, currentVersion );
  136. // parent class
  137. StructureBody::xfer( xfer );
  138. }
  139. //------------------------------------------------------------------------------------------------
  140. void HiveStructureBody::loadPostProcess( void )
  141. {
  142. // extend parent class
  143. StructureBody::loadPostProcess();
  144. }