CreateObjectDie.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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: CreateObjectDie.cpp ///////////////////////////////////////////////////////////////////////////
  24. // Author: Michael S. Booth, January 2002
  25. // Desc: Create an object upon this object's death
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #define DEFINE_OBJECT_STATUS_NAMES
  29. #include "GameLogic/Module/AIUpdate.h"
  30. #include "Common/ThingFactory.h"
  31. #include "Common/Xfer.h"
  32. #include "GameLogic/GameLogic.h"
  33. #include "GameLogic/Module/CreateObjectDie.h"
  34. #include "GameLogic/Object.h"
  35. #include "GameLogic/ObjectCreationList.h"
  36. #include "GameLogic/Module/BodyModule.h"
  37. #ifdef _INTERNAL
  38. // for occasional debugging...
  39. //#pragma optimize("", off)
  40. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  41. #endif
  42. // ------------------------------------------------------------------------------------------------
  43. // ------------------------------------------------------------------------------------------------
  44. CreateObjectDieModuleData::CreateObjectDieModuleData()
  45. {
  46. m_ocl = NULL;
  47. m_transferPreviousHealth = FALSE;
  48. }
  49. // ------------------------------------------------------------------------------------------------
  50. // ------------------------------------------------------------------------------------------------
  51. /*static*/ void CreateObjectDieModuleData::buildFieldParse(MultiIniFieldParse& p)
  52. {
  53. DieModuleData::buildFieldParse(p);
  54. static const FieldParse dataFieldParse[] =
  55. {
  56. { "CreationList", INI::parseObjectCreationList, NULL, offsetof( CreateObjectDieModuleData, m_ocl ) },
  57. { "TransferPreviousHealth", INI::parseBool, NULL ,offsetof( CreateObjectDieModuleData, m_transferPreviousHealth ) },
  58. { 0, 0, 0, 0 }
  59. };
  60. p.add(dataFieldParse);
  61. }
  62. ///////////////////////////////////////////////////////////////////////////////////////////////////
  63. ///////////////////////////////////////////////////////////////////////////////////////////////////
  64. ///////////////////////////////////////////////////////////////////////////////////////////////////
  65. //-------------------------------------------------------------------------------------------------
  66. //-------------------------------------------------------------------------------------------------
  67. CreateObjectDie::CreateObjectDie( Thing *thing, const ModuleData* moduleData ) : DieModule( thing, moduleData )
  68. {
  69. }
  70. //-------------------------------------------------------------------------------------------------
  71. //-------------------------------------------------------------------------------------------------
  72. CreateObjectDie::~CreateObjectDie( void )
  73. {
  74. }
  75. //-------------------------------------------------------------------------------------------------
  76. /** The die callback. */
  77. //-------------------------------------------------------------------------------------------------
  78. void CreateObjectDie::onDie( const DamageInfo * damageInfo )
  79. {
  80. const CreateObjectDieModuleData *data = getCreateObjectDieModuleData();
  81. if (!isDieApplicable(damageInfo))
  82. return;
  83. Object *damageDealer = TheGameLogic->findObjectByID( damageInfo->in.m_sourceID );
  84. Object *newObject = ObjectCreationList::create( data->m_ocl, getObject(), damageDealer );
  85. //If we're transferring previous health, we're transfering the last known
  86. //health before we died. In the case of the sneak attack tunnel network, it
  87. //is killed after the lifetime update expires.
  88. if( newObject && data->m_transferPreviousHealth )
  89. {
  90. //Convert old health to new health.
  91. Object *oldObject = getObject();
  92. BodyModuleInterface *oldBody = oldObject->getBodyModule();
  93. BodyModuleInterface *newBody = newObject->getBodyModule();
  94. if( oldBody && newBody )
  95. {
  96. //First transfer subdual damage
  97. DamageInfo damInfo;
  98. Real subdualDamageAmount = oldBody->getCurrentSubdualDamageAmount();
  99. if( subdualDamageAmount > 0.0f )
  100. {
  101. damInfo.in.m_amount = subdualDamageAmount;
  102. damInfo.in.m_damageType = DAMAGE_SUBDUAL_UNRESISTABLE;
  103. damInfo.in.m_sourceID = INVALID_ID;
  104. newBody->attemptDamage( &damInfo );
  105. }
  106. //Now transfer the previous health from the old object to the new.
  107. damInfo.in.m_amount = oldBody->getMaxHealth() - oldBody->getPreviousHealth();
  108. damInfo.in.m_damageType = DAMAGE_UNRESISTABLE;
  109. damInfo.in.m_sourceID = oldBody->getLastDamageInfo()->in.m_sourceID;
  110. if( damInfo.in.m_amount > 0.0f )
  111. {
  112. newBody->attemptDamage( &damInfo );
  113. }
  114. }
  115. //Transfer attackers.
  116. for( Object *obj = TheGameLogic->getFirstObject(); obj; obj = obj->getNextObject() )
  117. {
  118. AIUpdateInterface* ai = obj->getAI();
  119. if (!ai)
  120. continue;
  121. ai->transferAttack( oldObject->getID(), newObject->getID() );
  122. }
  123. }
  124. } // end onDie
  125. // ------------------------------------------------------------------------------------------------
  126. /** CRC */
  127. // ------------------------------------------------------------------------------------------------
  128. void CreateObjectDie::crc( Xfer *xfer )
  129. {
  130. // extend base class
  131. DieModule::crc( xfer );
  132. } // end crc
  133. // ------------------------------------------------------------------------------------------------
  134. /** Xfer method
  135. * Version Info:
  136. * 1: Initial version */
  137. // ------------------------------------------------------------------------------------------------
  138. void CreateObjectDie::xfer( Xfer *xfer )
  139. {
  140. // version
  141. XferVersion currentVersion = 1;
  142. XferVersion version = currentVersion;
  143. xfer->xferVersion( &version, currentVersion );
  144. // extend base class
  145. DieModule::xfer( xfer );
  146. } // end xfer
  147. // ------------------------------------------------------------------------------------------------
  148. /** Load post process */
  149. // ------------------------------------------------------------------------------------------------
  150. void CreateObjectDie::loadPostProcess( void )
  151. {
  152. // extend base class
  153. DieModule::loadPostProcess();
  154. } // end loadPostProcess