Module.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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: Module.cpp ///////////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, September 2001
  25. // Desc: Object and drawable modules and actions. These are simply just class
  26. // instances that we can assign to objects, drawables, and things to contain
  27. // data and code for specific events, or just to hold data
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////
  29. #include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
  30. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  31. #include "Common/Module.h"
  32. #include "Common/Thing.h"
  33. #include "Common/INI.h"
  34. #include "Common/ThingTemplate.h"
  35. #include "Common/Upgrade.h"
  36. #include "Common/Xfer.h"
  37. #include "GameLogic/Object.h"
  38. #include "GameLogic/GameLogic.h"
  39. #include "GameLogic/Module/BodyModule.h"
  40. #include "GameLogic/Module/CollideModule.h"
  41. #include "GameLogic/Module/ContainModule.h"
  42. #include "GameLogic/Module/DamageModule.h"
  43. #include "GameLogic/Module/DieModule.h"
  44. #include "GameLogic/Module/UpdateModule.h"
  45. #include "GameLogic/Module/UpgradeModule.h"
  46. #ifdef _INTERNAL
  47. // for occasional debugging...
  48. //#pragma optimize("", off)
  49. //#pragma message("************************************** WARNING, optimization disabled for debugging purposes")
  50. #endif
  51. ///////////////////////////////////////////////////////////////////////////////////////////////////
  52. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
  53. ///////////////////////////////////////////////////////////////////////////////////////////////////
  54. //-------------------------------------------------------------------------------------------------
  55. //-------------------------------------------------------------------------------------------------
  56. // this method should NEVER be overridden by user code, only via the MAKE_STANDARD_MODULE_xxx macros!
  57. // it should also NEVER be called directly; it's only for use by ModuleFactory!
  58. /*static*/ ModuleData* Module::friend_newModuleData(INI* ini)
  59. {
  60. ModuleData* data = MSGNEW("Module::friend_newModuleData") ModuleData; // no need to memorypool these since we never allocate more than one of each
  61. if (ini)
  62. ini->initFromINI(data, 0); // this is just so that an "end" token is required
  63. return data;
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. // ------------------------------------------------------------------------------------------------
  67. Module::~Module()
  68. {
  69. } // end ~Module
  70. // ------------------------------------------------------------------------------------------------
  71. /** CRC */
  72. // ------------------------------------------------------------------------------------------------
  73. void Module::crc( Xfer *xfer )
  74. {
  75. } // end crc
  76. // ------------------------------------------------------------------------------------------------
  77. /** Xfer method
  78. * Version Info:
  79. * 1: Initial version */
  80. // ------------------------------------------------------------------------------------------------
  81. void Module::xfer( Xfer *xfer )
  82. {
  83. // version
  84. XferVersion currentVersion = 1;
  85. XferVersion version = currentVersion;
  86. xfer->xferVersion( &version, currentVersion );
  87. } // end xfer
  88. // ------------------------------------------------------------------------------------------------
  89. /** load post process */
  90. // ------------------------------------------------------------------------------------------------
  91. void Module::loadPostProcess( void )
  92. {
  93. } // end loadPostProcess
  94. //-------------------------------------------------------------------------------------------------
  95. //-------------------------------------------------------------------------------------------------
  96. ObjectModule::ObjectModule( Thing *thing, const ModuleData* moduleData ) : Module(moduleData)
  97. {
  98. if (!moduleData)
  99. {
  100. DEBUG_CRASH(("module data may not be null\n"));
  101. throw INI_INVALID_DATA;
  102. }
  103. DEBUG_ASSERTCRASH( thing, ("Thing passed to ObjectModule is NULL!\n") );
  104. m_object = AsObject(thing);
  105. DEBUG_ASSERTCRASH( m_object, ("Thing passed to ObjectModule is not an Object!\n") );
  106. } // end ObjectModule
  107. //-------------------------------------------------------------------------------------------------
  108. //-------------------------------------------------------------------------------------------------
  109. ObjectModule::~ObjectModule( void )
  110. {
  111. } // end ~ObjectModule
  112. // ------------------------------------------------------------------------------------------------
  113. /** CRC */
  114. // ------------------------------------------------------------------------------------------------
  115. void ObjectModule::crc( Xfer *xfer )
  116. {
  117. // extend base class
  118. Module::crc( xfer );
  119. } // end crc
  120. // ------------------------------------------------------------------------------------------------
  121. /** Xfer method
  122. * Version Info:
  123. * 1: Initial version */
  124. // ------------------------------------------------------------------------------------------------
  125. void ObjectModule::xfer( Xfer *xfer )
  126. {
  127. // version
  128. XferVersion currentVersion = 1;
  129. XferVersion version = currentVersion;
  130. xfer->xferVersion( &version, currentVersion );
  131. // extend base class
  132. Module::xfer( xfer );
  133. } // end xfer
  134. // ------------------------------------------------------------------------------------------------
  135. /** load post process */
  136. // ------------------------------------------------------------------------------------------------
  137. void ObjectModule::loadPostProcess( void )
  138. {
  139. // extend base class
  140. Module::loadPostProcess();
  141. } // end loadPostProcess
  142. //-------------------------------------------------------------------------------------------------
  143. //-------------------------------------------------------------------------------------------------
  144. DrawableModule::DrawableModule( Thing *thing, const ModuleData* moduleData ) : Module(moduleData)
  145. {
  146. if (!moduleData)
  147. {
  148. DEBUG_CRASH(("module data may not be null\n"));
  149. throw INI_INVALID_DATA;
  150. }
  151. DEBUG_ASSERTCRASH( thing, ("Thing passed to DrawableModule is NULL!\n") );
  152. m_drawable = AsDrawable(thing);
  153. DEBUG_ASSERTCRASH( m_drawable, ("Thing passed to DrawableModule is not a Drawable!\n") );
  154. } // end ~DrawableModule
  155. //-------------------------------------------------------------------------------------------------
  156. //-------------------------------------------------------------------------------------------------
  157. DrawableModule::~DrawableModule( void )
  158. {
  159. } // end ~DrawableModule
  160. // ------------------------------------------------------------------------------------------------
  161. /** CRC */
  162. // ------------------------------------------------------------------------------------------------
  163. void DrawableModule::crc( Xfer *xfer )
  164. {
  165. // extend base class
  166. Module::crc( xfer );
  167. } // end crc
  168. // ------------------------------------------------------------------------------------------------
  169. /** Xfer method
  170. * Version Info:
  171. * 1: Initial version */
  172. // ------------------------------------------------------------------------------------------------
  173. void DrawableModule::xfer( Xfer *xfer )
  174. {
  175. // version
  176. XferVersion currentVersion = 1;
  177. XferVersion version = currentVersion;
  178. xfer->xferVersion( &version, currentVersion );
  179. // extend base class
  180. Module::xfer( xfer );
  181. } // end xfer
  182. // ------------------------------------------------------------------------------------------------
  183. /** load post process */
  184. // ------------------------------------------------------------------------------------------------
  185. void DrawableModule::loadPostProcess( void )
  186. {
  187. // extend base class
  188. Module::loadPostProcess();
  189. } // end loadPostProcess
  190. //-------------------------------------------------------------------------------------------------
  191. //-------------------------------------------------------------------------------------------------
  192. //-------------------------------------------------------------------------------------------------
  193. //-------------------------------------------------------------------------------------------------
  194. void UpgradeMuxData::performUpgradeFX(Object* obj) const
  195. {
  196. if (m_fxListUpgrade)
  197. {
  198. FXList::doFXObj(m_fxListUpgrade, obj);
  199. }
  200. }
  201. //-------------------------------------------------------------------------------------------------
  202. void UpgradeMuxData::muxDataProcessUpgradeRemoval(Object* obj) const
  203. {
  204. if( !m_removalUpgradeNames.empty() )
  205. {
  206. std::vector<AsciiString>::const_iterator it;
  207. for( it = m_removalUpgradeNames.begin();
  208. it != m_removalUpgradeNames.end();
  209. it++)
  210. {
  211. const UpgradeTemplate* theTemplate = TheUpgradeCenter->findUpgrade( *it );
  212. if( !theTemplate && !it->isEmpty() && !it->isNone())
  213. {
  214. DEBUG_CRASH(("An upgrade module references %s, which is not an Upgrade", it->str()));
  215. throw INI_INVALID_DATA;
  216. }
  217. obj->removeUpgrade(theTemplate);
  218. }
  219. }
  220. }
  221. //-------------------------------------------------------------------------------------------------
  222. Bool UpgradeMuxData::isTriggeredBy(const std::string &upgrade) const
  223. {
  224. std::vector<AsciiString>::const_iterator it;
  225. for( it = m_triggerUpgradeNames.begin(); it != m_triggerUpgradeNames.end(); ++it)
  226. {
  227. AsciiString trigger = *it;
  228. if (stricmp(trigger.str(), upgrade.c_str()) == 0)
  229. {
  230. return TRUE;
  231. }
  232. }
  233. return FALSE;
  234. }
  235. //-------------------------------------------------------------------------------------------------
  236. void UpgradeMuxData::getUpgradeActivationMasks(UpgradeMaskType& activation, UpgradeMaskType& conflicting) const
  237. {
  238. // already computed.
  239. if (!m_activationUpgradeNames.empty() || !m_conflictingUpgradeNames.empty())
  240. {
  241. m_activationMask.clear();
  242. m_conflictingMask.clear();
  243. std::vector<AsciiString>::const_iterator it;
  244. for( it = m_activationUpgradeNames.begin();
  245. it != m_activationUpgradeNames.end();
  246. it++)
  247. {
  248. const UpgradeTemplate* theTemplate = TheUpgradeCenter->findUpgrade( *it );
  249. if( !theTemplate && !it->isEmpty() && !it->isNone())
  250. {
  251. DEBUG_CRASH(("An upgrade module references %s, which is not an Upgrade", it->str()));
  252. throw INI_INVALID_DATA;
  253. }
  254. m_activationMask.set( theTemplate->getUpgradeMask() );
  255. }
  256. for( it = m_conflictingUpgradeNames.begin();
  257. it != m_conflictingUpgradeNames.end();
  258. it++)
  259. {
  260. const UpgradeTemplate* theTemplate = TheUpgradeCenter->findUpgrade( *it );
  261. if( !theTemplate && !it->isEmpty() && !it->isNone())
  262. {
  263. DEBUG_CRASH(("An upgrade module references %s, which is not an Upgrade", it->str()));
  264. throw INI_INVALID_DATA;
  265. }
  266. m_conflictingMask.set( theTemplate->getUpgradeMask() );
  267. }
  268. // We set the trigger upgrade names with the activationUpgradeNames entries to be used later.
  269. // We have to do this because the activationUpgradeNames are toasted just below.
  270. m_triggerUpgradeNames = m_activationUpgradeNames;
  271. //Clear the names now that we've cached the values!
  272. m_activationUpgradeNames.clear();
  273. m_conflictingUpgradeNames.clear();
  274. }
  275. activation = m_activationMask;
  276. conflicting = m_conflictingMask;
  277. }