ReplaceObjectUpgrade.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: ReplaceObjectUpgrade.cpp /////////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, July 2003
  25. // Desc: UpgradeModule that creates a new Object in our exact location and then deletes our object
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #include "GameLogic/Module/ReplaceObjectUpgrade.h"
  29. #include "Common/Player.h"
  30. #include "Common/ThingFactory.h"
  31. #include "Common/ThingTemplate.h"
  32. #include "GameLogic/AI.h"
  33. #include "GameLogic/AIPathfind.h"
  34. #include "GameLogic/GameLogic.h"
  35. #include "GameLogic/Module/CreateModule.h"
  36. #include "GameLogic/Object.h"
  37. // ------------------------------------------------------------------------------------------------
  38. // ------------------------------------------------------------------------------------------------
  39. void ReplaceObjectUpgradeModuleData::buildFieldParse(MultiIniFieldParse& p)
  40. {
  41. UpgradeModuleData::buildFieldParse(p);
  42. static const FieldParse dataFieldParse[] =
  43. {
  44. { "ReplaceObject", INI::parseAsciiString, NULL, offsetof( ReplaceObjectUpgradeModuleData, m_replaceObjectName ) },
  45. { 0, 0, 0, 0 }
  46. };
  47. p.add(dataFieldParse);
  48. }
  49. //-------------------------------------------------------------------------------------------------
  50. //-------------------------------------------------------------------------------------------------
  51. ReplaceObjectUpgrade::ReplaceObjectUpgrade( Thing *thing, const ModuleData* moduleData ) : UpgradeModule( thing, moduleData )
  52. {
  53. }
  54. //-------------------------------------------------------------------------------------------------
  55. //-------------------------------------------------------------------------------------------------
  56. ReplaceObjectUpgrade::~ReplaceObjectUpgrade( void )
  57. {
  58. }
  59. //-------------------------------------------------------------------------------------------------
  60. //-------------------------------------------------------------------------------------------------
  61. void ReplaceObjectUpgrade::upgradeImplementation( )
  62. {
  63. const ReplaceObjectUpgradeModuleData *data = getReplaceObjectUpgradeModuleData();
  64. Object *me = getObject();
  65. Matrix3D myMatrix = *me->getTransformMatrix();
  66. Team *myTeam = me->getTeam();// Team implies player. It is a subset.
  67. const ThingTemplate *replacementTemplate = TheThingFactory->findTemplate(data->m_replaceObjectName);
  68. if( replacementTemplate == NULL )
  69. {
  70. DEBUG_ASSERTCRASH(replacementTemplate != NULL, ("No such object '%s' in ReplaceObjectUpgrade.", data->m_replaceObjectName.str() ) );
  71. return;
  72. }
  73. // Remove us first since occupation of cells is apparently not a refcount, but a flag. If I don't remove, then the new
  74. // thing will be placed, and then on deletion I will remove "his" marks.
  75. TheAI->pathfinder()->removeObjectFromPathfindMap( me );
  76. TheGameLogic->destroyObject(me);
  77. Object *replacementObject = TheThingFactory->newObject(replacementTemplate, myTeam);
  78. replacementObject->setTransformMatrix(&myMatrix);
  79. TheAI->pathfinder()->addObjectToPathfindMap( replacementObject );
  80. // Now onCreates were called at the constructor. This magically created
  81. // thing needs to be considered as Built for Game specific stuff.
  82. for (BehaviorModule** m = replacementObject->getBehaviorModules(); *m; ++m)
  83. {
  84. CreateModuleInterface* create = (*m)->getCreate();
  85. if (!create)
  86. continue;
  87. create->onBuildComplete();
  88. }
  89. if( replacementObject->getControllingPlayer() )
  90. {
  91. replacementObject->getControllingPlayer()->onStructureConstructionComplete(me, replacementObject, FALSE);
  92. }
  93. }
  94. // ------------------------------------------------------------------------------------------------
  95. /** CRC */
  96. // ------------------------------------------------------------------------------------------------
  97. void ReplaceObjectUpgrade::crc( Xfer *xfer )
  98. {
  99. // extend base class
  100. UpgradeModule::crc( xfer );
  101. } // end crc
  102. // ------------------------------------------------------------------------------------------------
  103. /** Xfer method
  104. * Version Info:
  105. * 1: Initial version */
  106. // ------------------------------------------------------------------------------------------------
  107. void ReplaceObjectUpgrade::xfer( Xfer *xfer )
  108. {
  109. // version
  110. XferVersion currentVersion = 1;
  111. XferVersion version = currentVersion;
  112. xfer->xferVersion( &version, currentVersion );
  113. // extend base class
  114. UpgradeModule::xfer( xfer );
  115. } // end xfer
  116. // ------------------------------------------------------------------------------------------------
  117. /** Load post process */
  118. // ------------------------------------------------------------------------------------------------
  119. void ReplaceObjectUpgrade::loadPostProcess( void )
  120. {
  121. // extend base class
  122. UpgradeModule::loadPostProcess();
  123. } // end loadPostProcess