SpecialPower.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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: SpecialPower.cpp /////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, April 2002
  25. // Desc: Special power templates and the system that holds them
  26. // Edited: Kris Morness -- July 2002 (added BitFlag system)
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. // USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
  29. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  30. #include "Common/Player.h"
  31. #include "Common/Science.h"
  32. #include "Common/SpecialPower.h"
  33. #include "GameLogic/Object.h"
  34. #include "Common/BitFlagsIO.h"
  35. #ifdef _INTERNAL
  36. // for occasional debugging...
  37. //#pragma optimize("", off)
  38. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  39. #endif
  40. // GLOBAL /////////////////////////////////////////////////////////////////////////////////////////
  41. SpecialPowerStore *TheSpecialPowerStore = NULL;
  42. #define DEFAULT_DEFECTION_DETECTION_PROTECTION_TIME_LIMIT (LOGICFRAMES_PER_SECOND * 10)
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////
  44. ///////////////////////////////////////////////////////////////////////////////////////////////////
  45. ///////////////////////////////////////////////////////////////////////////////////////////////////
  46. // Externs ////////////////////////////////////////////////////////////////////////////////////////
  47. const char* SpecialPowerMaskType::s_bitNameList[] =
  48. {
  49. "SPECIAL_INVALID",
  50. //Superweapons
  51. "SPECIAL_DAISY_CUTTER",
  52. "SPECIAL_PARADROP_AMERICA",
  53. "SPECIAL_CARPET_BOMB",
  54. "SPECIAL_CLUSTER_MINES",
  55. "SPECIAL_EMP_PULSE",
  56. "SPECIAL_NAPALM_STRIKE",
  57. "SPECIAL_CASH_HACK",
  58. "SPECIAL_NEUTRON_MISSILE",
  59. "SPECIAL_SPY_SATELLITE",
  60. "SPECIAL_DEFECTOR",
  61. "SPECIAL_TERROR_CELL",
  62. "SPECIAL_AMBUSH",
  63. "SPECIAL_BLACK_MARKET_NUKE",
  64. "SPECIAL_ANTHRAX_BOMB",
  65. "SPECIAL_SCUD_STORM",
  66. #ifdef ALLOW_DEMORALIZE
  67. "SPECIAL_DEMORALIZE",
  68. #else
  69. "SPECIAL_DEMORALIZE_OBSOLETE",
  70. #endif
  71. "SPECIAL_CRATE_DROP",
  72. "SPECIAL_A10_THUNDERBOLT_STRIKE",
  73. "SPECIAL_DETONATE_DIRTY_NUKE",
  74. "SPECIAL_ARTILLERY_BARRAGE",
  75. //Special abilities
  76. "SPECIAL_MISSILE_DEFENDER_LASER_GUIDED_MISSILES",
  77. "SPECIAL_REMOTE_CHARGES",
  78. "SPECIAL_TIMED_CHARGES",
  79. "SPECIAL_HACKER_DISABLE_BUILDING",
  80. "SPECIAL_TANKHUNTER_TNT_ATTACK",
  81. "SPECIAL_BLACKLOTUS_CAPTURE_BUILDING",
  82. "SPECIAL_BLACKLOTUS_DISABLE_VEHICLE_HACK",
  83. "SPECIAL_BLACKLOTUS_STEAL_CASH_HACK",
  84. "SPECIAL_INFANTRY_CAPTURE_BUILDING",
  85. "SPECIAL_RADAR_VAN_SCAN",
  86. "SPECIAL_SPY_DRONE",
  87. "SPECIAL_DISGUISE_AS_VEHICLE",
  88. "SPECIAL_REPAIR_VEHICLES",
  89. "SPECIAL_PARTICLE_UPLINK_CANNON",
  90. "SPECIAL_CASH_BOUNTY",
  91. "SPECIAL_CHANGE_BATTLE_PLANS",
  92. "SPECIAL_CIA_INTELLIGENCE",
  93. "SPECIAL_CLEANUP_AREA",
  94. "SPECIAL_LAUNCH_BAIKONUR_ROCKET",
  95. NULL
  96. };
  97. //-------------------------------------------------------------------------------------------------
  98. //-------------------------------------------------------------------------------------------------
  99. void SpecialPowerStore::parseSpecialPowerDefinition( INI *ini )
  100. {
  101. // read the name
  102. AsciiString name = ini->getNextToken();
  103. SpecialPowerTemplate* specialPower = TheSpecialPowerStore->findSpecialPowerTemplatePrivate( name );
  104. if ( ini->getLoadType() == INI_LOAD_CREATE_OVERRIDES )
  105. {
  106. if (specialPower)
  107. {
  108. SpecialPowerTemplate* child = (SpecialPowerTemplate*)specialPower->friend_getFinalOverride();
  109. specialPower = newInstance(SpecialPowerTemplate);
  110. *specialPower = *child;
  111. child->setNextOverride(specialPower);
  112. specialPower->markAsOverride();
  113. //TheSpecialPowerStore->m_specialPowerTemplates.push_back(specialPower); // nope, do NOT do this
  114. }
  115. else
  116. {
  117. specialPower = newInstance(SpecialPowerTemplate);
  118. const SpecialPowerTemplate *defaultTemplate = TheSpecialPowerStore->findSpecialPowerTemplate( "DefaultSpecialPower" );
  119. if( defaultTemplate )
  120. *specialPower = *defaultTemplate;
  121. specialPower->friend_setNameAndID(name, ++TheSpecialPowerStore->m_nextSpecialPowerID);
  122. specialPower->markAsOverride();
  123. TheSpecialPowerStore->m_specialPowerTemplates.push_back(specialPower);
  124. }
  125. }
  126. else
  127. {
  128. if (specialPower)
  129. {
  130. throw INI_INVALID_DATA;
  131. }
  132. else
  133. {
  134. specialPower = newInstance(SpecialPowerTemplate);
  135. const SpecialPowerTemplate *defaultTemplate = TheSpecialPowerStore->findSpecialPowerTemplate( "DefaultSpecialPower" );
  136. if( defaultTemplate )
  137. *specialPower = *defaultTemplate;
  138. specialPower->friend_setNameAndID(name, ++TheSpecialPowerStore->m_nextSpecialPowerID);
  139. TheSpecialPowerStore->m_specialPowerTemplates.push_back(specialPower);
  140. }
  141. }
  142. // parse the ini definition
  143. if (specialPower)
  144. ini->initFromINI( specialPower, specialPower->getFieldParse() );
  145. }
  146. //-------------------------------------------------------------------------------------------------
  147. //-------------------------------------------------------------------------------------------------
  148. /* static */ const FieldParse SpecialPowerTemplate::m_specialPowerFieldParse[] =
  149. {
  150. { "ReloadTime", INI::parseDurationUnsignedInt, NULL, offsetof( SpecialPowerTemplate, m_reloadTime ) },
  151. { "RequiredScience", INI::parseScience, NULL, offsetof( SpecialPowerTemplate, m_requiredScience ) },
  152. { "InitiateSound", INI::parseAudioEventRTS, NULL, offsetof( SpecialPowerTemplate, m_initiateSound ) },
  153. { "InitiateAtLocationSound", INI::parseAudioEventRTS, NULL, offsetof( SpecialPowerTemplate, m_initiateAtLocationSound ) },
  154. { "PublicTimer", INI::parseBool, NULL, offsetof( SpecialPowerTemplate, m_publicTimer ) },
  155. { "Enum", INI::parseIndexList, SpecialPowerMaskType::getBitNames(), offsetof( SpecialPowerTemplate, m_type ) },
  156. { "DetectionTime", INI::parseDurationUnsignedInt, NULL, offsetof( SpecialPowerTemplate, m_detectionTime ) },
  157. { "SharedSyncedTimer", INI::parseBool, NULL, offsetof( SpecialPowerTemplate, m_sharedNSync ) },
  158. { "ViewObjectDuration", INI::parseDurationUnsignedInt, NULL, offsetof( SpecialPowerTemplate, m_viewObjectDuration ) },
  159. { "ViewObjectRange", INI::parseReal, NULL, offsetof( SpecialPowerTemplate, m_viewObjectRange ) },
  160. { "RadiusCursorRadius", INI::parseReal, NULL, offsetof( SpecialPowerTemplate, m_radiusCursorRadius ) },
  161. { NULL, NULL, NULL, 0 } // keep this last
  162. };
  163. //-------------------------------------------------------------------------------------------------
  164. //-------------------------------------------------------------------------------------------------
  165. SpecialPowerTemplate::SpecialPowerTemplate()
  166. {
  167. m_id = 0;
  168. m_type = SPECIAL_INVALID;
  169. m_reloadTime = 0;
  170. m_requiredScience = SCIENCE_INVALID;
  171. m_publicTimer = FALSE;
  172. m_detectionTime = DEFAULT_DEFECTION_DETECTION_PROTECTION_TIME_LIMIT;
  173. m_sharedNSync = FALSE;
  174. m_viewObjectDuration = 0;
  175. m_viewObjectRange = 0;
  176. m_radiusCursorRadius = 0;
  177. } // end SpecialPowerTemplate
  178. //-------------------------------------------------------------------------------------------------
  179. //-------------------------------------------------------------------------------------------------
  180. SpecialPowerTemplate::~SpecialPowerTemplate()
  181. {
  182. } // end ~SpecialPowerTemplate
  183. ///////////////////////////////////////////////////////////////////////////////////////////////////
  184. ///////////////////////////////////////////////////////////////////////////////////////////////////
  185. ///////////////////////////////////////////////////////////////////////////////////////////////////
  186. //-------------------------------------------------------------------------------------------------
  187. //-------------------------------------------------------------------------------------------------
  188. SpecialPowerStore::SpecialPowerStore( void )
  189. {
  190. m_nextSpecialPowerID = 0;
  191. } // end SpecialPowerStore
  192. //-------------------------------------------------------------------------------------------------
  193. //-------------------------------------------------------------------------------------------------
  194. SpecialPowerStore::~SpecialPowerStore( void )
  195. {
  196. // delete all templates
  197. for( Int i = 0; i < m_specialPowerTemplates.size(); ++i )
  198. m_specialPowerTemplates[ i ]->deleteInstance();
  199. // erase the list
  200. m_specialPowerTemplates.clear();
  201. // set our count to zero
  202. m_nextSpecialPowerID = 0;
  203. } // end ~SpecialPowerStore
  204. //-------------------------------------------------------------------------------------------------
  205. //-------------------------------------------------------------------------------------------------
  206. SpecialPowerTemplate* SpecialPowerStore::findSpecialPowerTemplatePrivate( AsciiString name )
  207. {
  208. // search the template list for matching name
  209. for( Int i = 0; i < m_specialPowerTemplates.size(); ++i )
  210. if( m_specialPowerTemplates[ i ]->getName() == name )
  211. return m_specialPowerTemplates[ i ];
  212. return NULL; // not found
  213. }
  214. //-------------------------------------------------------------------------------------------------
  215. /** Find a special power template given unique ID */
  216. //-------------------------------------------------------------------------------------------------
  217. const SpecialPowerTemplate *SpecialPowerStore::findSpecialPowerTemplateByID( UnsignedInt id )
  218. {
  219. // search the template list for matching name
  220. for( Int i = 0; i < m_specialPowerTemplates.size(); ++i )
  221. if( m_specialPowerTemplates[ i ]->getID() == id )
  222. return m_specialPowerTemplates[ i ];
  223. return NULL; // not found
  224. }
  225. //-------------------------------------------------------------------------------------------------
  226. /** Find a special power template given index (WB) */
  227. //-------------------------------------------------------------------------------------------------
  228. const SpecialPowerTemplate *SpecialPowerStore::getSpecialPowerTemplateByIndex( UnsignedInt index )
  229. {
  230. if (index >= 0 && index < m_specialPowerTemplates.size())
  231. return m_specialPowerTemplates[ index ];
  232. return NULL; // not found
  233. } // end getSpecialPowerTemplateByIndex
  234. //-------------------------------------------------------------------------------------------------
  235. /** Return the size of the store (WB) */
  236. //-------------------------------------------------------------------------------------------------
  237. Int SpecialPowerStore::getNumSpecialPowers( void )
  238. {
  239. return m_specialPowerTemplates.size();
  240. } // end getNumSpecialPowers
  241. //-------------------------------------------------------------------------------------------------
  242. /** does the object (and therefore the player) meet all the requirements to use this power */
  243. //-------------------------------------------------------------------------------------------------
  244. Bool SpecialPowerStore::canUseSpecialPower( Object *obj, const SpecialPowerTemplate *specialPowerTemplate )
  245. {
  246. // sanity
  247. if( obj == NULL || specialPowerTemplate == NULL )
  248. return FALSE;
  249. // as a first sanity check, the object must have a module capable of executing the power
  250. if( obj->getSpecialPowerModule( specialPowerTemplate ) == NULL )
  251. return FALSE;
  252. //
  253. // in order to execute the special powers we have attached special power modules to the objects
  254. // that can use them. However, just because an object has a module that is capable of
  255. // doing the power, does not mean the object and the player can actually execute the
  256. // power because some powers require a specialized science that the player must select and
  257. // they cannot have all of them.
  258. //
  259. // check for requried science
  260. ScienceType requiredScience = specialPowerTemplate->getRequiredScience();
  261. if( requiredScience != SCIENCE_INVALID )
  262. {
  263. Player *player = obj->getControllingPlayer();
  264. if( player->hasScience( requiredScience ) == FALSE )
  265. return FALSE;
  266. } // end if
  267. // I THINK THIS IS WHERE WE BAIL OUT IF A DIFFERENT CONYARD IS ALREADY CHARGIN THIS SPECIAL RIGHT NOW //LORENZEN
  268. // all is well
  269. return TRUE;
  270. } // end canUseSpecialPower
  271. //-------------------------------------------------------------------------------------------------
  272. /** Reset */
  273. //-------------------------------------------------------------------------------------------------
  274. void SpecialPowerStore::reset( void )
  275. {
  276. for (SpecialPowerTemplatePtrVector::iterator it = m_specialPowerTemplates.begin(); it != m_specialPowerTemplates.end(); /*++it*/)
  277. {
  278. SpecialPowerTemplate* si = *it;
  279. Overridable* temp = si->deleteOverrides();
  280. if (temp == NULL)
  281. {
  282. it = m_specialPowerTemplates.erase(it);
  283. }
  284. else
  285. {
  286. ++it;
  287. }
  288. }
  289. } // end reset