SpecialPower.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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: 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_HELIX_NAPALM_BOMB",
  80. "SPECIAL_HACKER_DISABLE_BUILDING",
  81. "SPECIAL_TANKHUNTER_TNT_ATTACK",
  82. "SPECIAL_BLACKLOTUS_CAPTURE_BUILDING",
  83. "SPECIAL_BLACKLOTUS_DISABLE_VEHICLE_HACK",
  84. "SPECIAL_BLACKLOTUS_STEAL_CASH_HACK",
  85. "SPECIAL_INFANTRY_CAPTURE_BUILDING",
  86. "SPECIAL_RADAR_VAN_SCAN",
  87. "SPECIAL_SPY_DRONE",
  88. "SPECIAL_DISGUISE_AS_VEHICLE",
  89. "SPECIAL_BOOBY_TRAP",
  90. "SPECIAL_REPAIR_VEHICLES",
  91. "SPECIAL_PARTICLE_UPLINK_CANNON",
  92. "SPECIAL_CASH_BOUNTY",
  93. "SPECIAL_CHANGE_BATTLE_PLANS",
  94. "SPECIAL_CIA_INTELLIGENCE",
  95. "SPECIAL_CLEANUP_AREA",
  96. "SPECIAL_LAUNCH_BAIKONUR_ROCKET",
  97. "SPECIAL_SPECTRE_GUNSHIP",
  98. "SPECIAL_GPS_SCRAMBLER",
  99. "SPECIAL_FRENZY",
  100. "SPECIAL_SNEAK_ATTACK",
  101. "SPECIAL_CHINA_CARPET_BOMB",
  102. "EARLY_SPECIAL_CHINA_CARPET_BOMB",
  103. "SPECIAL_LEAFLET_DROP",
  104. "EARLY_SPECIAL_LEAFLET_DROP",
  105. "EARLY_SPECIAL_FRENZY",
  106. "SPECIAL_COMMUNICATIONS_DOWNLOAD",
  107. "EARLY_SPECIAL_REPAIR_VEHICLES",
  108. "SPECIAL_TANK_PARADROP",
  109. "SUPW_SPECIAL_PARTICLE_UPLINK_CANNON",
  110. "AIRF_SPECIAL_DAISY_CUTTER",
  111. "NUKE_SPECIAL_CLUSTER_MINES",
  112. "NUKE_SPECIAL_NEUTRON_MISSILE",
  113. "AIRF_SPECIAL_A10_THUNDERBOLT_STRIKE",
  114. "AIRF_SPECIAL_SPECTRE_GUNSHIP",
  115. "INFA_SPECIAL_PARADROP_AMERICA",
  116. "SLTH_SPECIAL_GPS_SCRAMBLER",
  117. "AIRF_SPECIAL_CARPET_BOMB",
  118. "SUPR_SPECIAL_CRUISE_MISSILE",
  119. "LAZR_SPECIAL_PARTICLE_UPLINK_CANNON",
  120. "SUPW_SPECIAL_NEUTRON_MISSILE",
  121. "SPECIAL_BATTLESHIP_BOMBARDMENT",
  122. NULL
  123. };
  124. //-------------------------------------------------------------------------------------------------
  125. //-------------------------------------------------------------------------------------------------
  126. void SpecialPowerStore::parseSpecialPowerDefinition( INI *ini )
  127. {
  128. // read the name
  129. AsciiString name = ini->getNextToken();
  130. SpecialPowerTemplate* specialPower = TheSpecialPowerStore->findSpecialPowerTemplatePrivate( name );
  131. if ( ini->getLoadType() == INI_LOAD_CREATE_OVERRIDES )
  132. {
  133. if (specialPower)
  134. {
  135. SpecialPowerTemplate* child = (SpecialPowerTemplate*)specialPower->friend_getFinalOverride();
  136. specialPower = newInstance(SpecialPowerTemplate);
  137. *specialPower = *child;
  138. child->setNextOverride(specialPower);
  139. specialPower->markAsOverride();
  140. //TheSpecialPowerStore->m_specialPowerTemplates.push_back(specialPower); // nope, do NOT do this
  141. }
  142. else
  143. {
  144. specialPower = newInstance(SpecialPowerTemplate);
  145. const SpecialPowerTemplate *defaultTemplate = TheSpecialPowerStore->findSpecialPowerTemplate( "DefaultSpecialPower" );
  146. if( defaultTemplate )
  147. *specialPower = *defaultTemplate;
  148. specialPower->friend_setNameAndID(name, ++TheSpecialPowerStore->m_nextSpecialPowerID);
  149. specialPower->markAsOverride();
  150. TheSpecialPowerStore->m_specialPowerTemplates.push_back(specialPower);
  151. }
  152. }
  153. else
  154. {
  155. if (specialPower)
  156. {
  157. throw INI_INVALID_DATA;
  158. }
  159. else
  160. {
  161. specialPower = newInstance(SpecialPowerTemplate);
  162. const SpecialPowerTemplate *defaultTemplate = TheSpecialPowerStore->findSpecialPowerTemplate( "DefaultSpecialPower" );
  163. if( defaultTemplate )
  164. *specialPower = *defaultTemplate;
  165. specialPower->friend_setNameAndID(name, ++TheSpecialPowerStore->m_nextSpecialPowerID);
  166. TheSpecialPowerStore->m_specialPowerTemplates.push_back(specialPower);
  167. }
  168. }
  169. // parse the ini definition
  170. if (specialPower)
  171. ini->initFromINI( specialPower, specialPower->getFieldParse() );
  172. }
  173. //-------------------------------------------------------------------------------------------------
  174. //-------------------------------------------------------------------------------------------------
  175. /* static */ const FieldParse SpecialPowerTemplate::m_specialPowerFieldParse[] =
  176. {
  177. { "ReloadTime", INI::parseDurationUnsignedInt, NULL, offsetof( SpecialPowerTemplate, m_reloadTime ) },
  178. { "RequiredScience", INI::parseScience, NULL, offsetof( SpecialPowerTemplate, m_requiredScience ) },
  179. { "InitiateSound", INI::parseAudioEventRTS, NULL, offsetof( SpecialPowerTemplate, m_initiateSound ) },
  180. { "InitiateAtLocationSound", INI::parseAudioEventRTS, NULL, offsetof( SpecialPowerTemplate, m_initiateAtLocationSound ) },
  181. { "PublicTimer", INI::parseBool, NULL, offsetof( SpecialPowerTemplate, m_publicTimer ) },
  182. { "Enum", INI::parseIndexList, SpecialPowerMaskType::getBitNames(), offsetof( SpecialPowerTemplate, m_type ) },
  183. { "DetectionTime", INI::parseDurationUnsignedInt, NULL, offsetof( SpecialPowerTemplate, m_detectionTime ) },
  184. { "SharedSyncedTimer", INI::parseBool, NULL, offsetof( SpecialPowerTemplate, m_sharedNSync ) },
  185. { "ViewObjectDuration", INI::parseDurationUnsignedInt, NULL, offsetof( SpecialPowerTemplate, m_viewObjectDuration ) },
  186. { "ViewObjectRange", INI::parseReal, NULL, offsetof( SpecialPowerTemplate, m_viewObjectRange ) },
  187. { "RadiusCursorRadius", INI::parseReal, NULL, offsetof( SpecialPowerTemplate, m_radiusCursorRadius ) },
  188. { "ShortcutPower", INI::parseBool, NULL, offsetof( SpecialPowerTemplate, m_shortcutPower ) },
  189. { "AcademyClassify", INI::parseIndexList, TheAcademyClassificationTypeNames, offsetof( SpecialPowerTemplate, m_academyClassificationType ) },
  190. { NULL, NULL, NULL, 0 } // keep this last
  191. };
  192. //-------------------------------------------------------------------------------------------------
  193. //-------------------------------------------------------------------------------------------------
  194. SpecialPowerTemplate::SpecialPowerTemplate()
  195. {
  196. m_id = 0;
  197. m_type = SPECIAL_INVALID;
  198. m_reloadTime = 0;
  199. m_requiredScience = SCIENCE_INVALID;
  200. m_publicTimer = FALSE;
  201. m_detectionTime = DEFAULT_DEFECTION_DETECTION_PROTECTION_TIME_LIMIT;
  202. m_sharedNSync = FALSE;
  203. m_viewObjectDuration = 0;
  204. m_viewObjectRange = 0;
  205. m_radiusCursorRadius = 0;
  206. m_shortcutPower = FALSE;
  207. } // end SpecialPowerTemplate
  208. //-------------------------------------------------------------------------------------------------
  209. //-------------------------------------------------------------------------------------------------
  210. SpecialPowerTemplate::~SpecialPowerTemplate()
  211. {
  212. } // end ~SpecialPowerTemplate
  213. ///////////////////////////////////////////////////////////////////////////////////////////////////
  214. ///////////////////////////////////////////////////////////////////////////////////////////////////
  215. ///////////////////////////////////////////////////////////////////////////////////////////////////
  216. //-------------------------------------------------------------------------------------------------
  217. //-------------------------------------------------------------------------------------------------
  218. SpecialPowerStore::SpecialPowerStore( void )
  219. {
  220. m_nextSpecialPowerID = 0;
  221. } // end SpecialPowerStore
  222. //-------------------------------------------------------------------------------------------------
  223. //-------------------------------------------------------------------------------------------------
  224. SpecialPowerStore::~SpecialPowerStore( void )
  225. {
  226. // delete all templates
  227. for( Int i = 0; i < m_specialPowerTemplates.size(); ++i )
  228. m_specialPowerTemplates[ i ]->deleteInstance();
  229. // erase the list
  230. m_specialPowerTemplates.clear();
  231. // set our count to zero
  232. m_nextSpecialPowerID = 0;
  233. } // end ~SpecialPowerStore
  234. //-------------------------------------------------------------------------------------------------
  235. //-------------------------------------------------------------------------------------------------
  236. SpecialPowerTemplate* SpecialPowerStore::findSpecialPowerTemplatePrivate( AsciiString name )
  237. {
  238. // search the template list for matching name
  239. for( Int i = 0; i < m_specialPowerTemplates.size(); ++i )
  240. if( m_specialPowerTemplates[ i ]->getName() == name )
  241. return m_specialPowerTemplates[ i ];
  242. return NULL; // not found
  243. }
  244. //-------------------------------------------------------------------------------------------------
  245. /** Find a special power template given unique ID */
  246. //-------------------------------------------------------------------------------------------------
  247. const SpecialPowerTemplate *SpecialPowerStore::findSpecialPowerTemplateByID( UnsignedInt id )
  248. {
  249. // search the template list for matching name
  250. for( Int i = 0; i < m_specialPowerTemplates.size(); ++i )
  251. if( m_specialPowerTemplates[ i ]->getID() == id )
  252. return m_specialPowerTemplates[ i ];
  253. return NULL; // not found
  254. }
  255. //-------------------------------------------------------------------------------------------------
  256. /** Find a special power template given index (WB) */
  257. //-------------------------------------------------------------------------------------------------
  258. const SpecialPowerTemplate *SpecialPowerStore::getSpecialPowerTemplateByIndex( UnsignedInt index )
  259. {
  260. if (index >= 0 && index < m_specialPowerTemplates.size())
  261. return m_specialPowerTemplates[ index ];
  262. return NULL; // not found
  263. } // end getSpecialPowerTemplateByIndex
  264. //-------------------------------------------------------------------------------------------------
  265. /** Return the size of the store (WB) */
  266. //-------------------------------------------------------------------------------------------------
  267. Int SpecialPowerStore::getNumSpecialPowers( void )
  268. {
  269. return m_specialPowerTemplates.size();
  270. } // end getNumSpecialPowers
  271. //-------------------------------------------------------------------------------------------------
  272. /** does the object (and therefore the player) meet all the requirements to use this power */
  273. //-------------------------------------------------------------------------------------------------
  274. Bool SpecialPowerStore::canUseSpecialPower( Object *obj, const SpecialPowerTemplate *specialPowerTemplate )
  275. {
  276. // sanity
  277. if( obj == NULL || specialPowerTemplate == NULL )
  278. return FALSE;
  279. // as a first sanity check, the object must have a module capable of executing the power
  280. if( obj->getSpecialPowerModule( specialPowerTemplate ) == NULL )
  281. return FALSE;
  282. //
  283. // in order to execute the special powers we have attached special power modules to the objects
  284. // that can use them. However, just because an object has a module that is capable of
  285. // doing the power, does not mean the object and the player can actually execute the
  286. // power because some powers require a specialized science that the player must select and
  287. // they cannot have all of them.
  288. //
  289. // check for requried science
  290. ScienceType requiredScience = specialPowerTemplate->getRequiredScience();
  291. if( requiredScience != SCIENCE_INVALID )
  292. {
  293. Player *player = obj->getControllingPlayer();
  294. if( player->hasScience( requiredScience ) == FALSE )
  295. return FALSE;
  296. } // end if
  297. // I THINK THIS IS WHERE WE BAIL OUT IF A DIFFERENT CONYARD IS ALREADY CHARGIN THIS SPECIAL RIGHT NOW //LORENZEN
  298. // all is well
  299. return TRUE;
  300. } // end canUseSpecialPower
  301. //-------------------------------------------------------------------------------------------------
  302. /** Reset */
  303. //-------------------------------------------------------------------------------------------------
  304. void SpecialPowerStore::reset( void )
  305. {
  306. for (SpecialPowerTemplatePtrVector::iterator it = m_specialPowerTemplates.begin(); it != m_specialPowerTemplates.end(); /*++it*/)
  307. {
  308. SpecialPowerTemplate* si = *it;
  309. Overridable* temp = si->deleteOverrides();
  310. if (temp == NULL)
  311. {
  312. it = m_specialPowerTemplates.erase(it);
  313. }
  314. else
  315. {
  316. ++it;
  317. }
  318. }
  319. } // end reset