Upgrade.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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: Upgrade.cpp //////////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, March 2002
  25. // Desc: Upgrade system for players
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #define DEFINE_VETERANCY_NAMES
  30. #include "Common/Upgrade.h"
  31. #include "Common/Player.h"
  32. #include "Common/Xfer.h"
  33. #include "GameClient/InGameUI.h"
  34. #include "GameClient/Image.h"
  35. const char *TheUpgradeTypeNames[] =
  36. {
  37. "PLAYER",
  38. "OBJECT",
  39. NULL
  40. };
  41. // PUBLIC /////////////////////////////////////////////////////////////////////////////////////////
  42. class UpgradeCenter *TheUpgradeCenter = NULL;
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////
  44. // UPGRADE ////////////////////////////////////////////////////////////////////////////////////////
  45. ///////////////////////////////////////////////////////////////////////////////////////////////////
  46. //-------------------------------------------------------------------------------------------------
  47. //-------------------------------------------------------------------------------------------------
  48. Upgrade::Upgrade( const UpgradeTemplate *upgradeTemplate )
  49. {
  50. m_template = upgradeTemplate;
  51. m_status = UPGRADE_STATUS_INVALID;
  52. m_next = NULL;
  53. m_prev = NULL;
  54. } // end Upgrade
  55. //-------------------------------------------------------------------------------------------------
  56. //-------------------------------------------------------------------------------------------------
  57. Upgrade::~Upgrade( void )
  58. {
  59. } // end ~Upgrade
  60. // ------------------------------------------------------------------------------------------------
  61. /** CRC */
  62. // ------------------------------------------------------------------------------------------------
  63. void Upgrade::crc( Xfer *xfer )
  64. {
  65. } // end crc
  66. // ------------------------------------------------------------------------------------------------
  67. /** Xfer method
  68. * Version Info:
  69. * 1: Initial version */
  70. // ------------------------------------------------------------------------------------------------
  71. void Upgrade::xfer( Xfer *xfer )
  72. {
  73. // version
  74. XferVersion currentVersion = 1;
  75. XferVersion version = currentVersion;
  76. xfer->xferVersion( &version, currentVersion );
  77. // status
  78. xfer->xferUser( &m_status, sizeof( UpgradeStatusType ) );
  79. } // end xfer
  80. // ------------------------------------------------------------------------------------------------
  81. /** Load post process */
  82. // ------------------------------------------------------------------------------------------------
  83. void Upgrade::loadPostProcess( void )
  84. {
  85. } // end loadPostProcess
  86. ///////////////////////////////////////////////////////////////////////////////////////////////////
  87. // UPGRADE TEMPLATE ///////////////////////////////////////////////////////////////////////////////
  88. ///////////////////////////////////////////////////////////////////////////////////////////////////
  89. //-------------------------------------------------------------------------------------------------
  90. //-------------------------------------------------------------------------------------------------
  91. const FieldParse UpgradeTemplate::m_upgradeFieldParseTable[] =
  92. {
  93. { "DisplayName", INI::parseAsciiString, NULL, offsetof( UpgradeTemplate, m_displayNameLabel ) },
  94. { "Type", INI::parseIndexList, TheUpgradeTypeNames, offsetof( UpgradeTemplate, m_type ) },
  95. { "BuildTime", INI::parseReal, NULL, offsetof( UpgradeTemplate, m_buildTime ) },
  96. { "BuildCost", INI::parseInt, NULL, offsetof( UpgradeTemplate, m_cost ) },
  97. { "ButtonImage", INI::parseAsciiString, NULL, offsetof( UpgradeTemplate, m_buttonImageName ) },
  98. { "ResearchSound", INI::parseAudioEventRTS, NULL, offsetof( UpgradeTemplate, m_researchSound ) },
  99. { "UnitSpecificSound", INI::parseAudioEventRTS, NULL, offsetof( UpgradeTemplate, m_unitSpecificSound ) },
  100. { "AcademyClassify", INI::parseIndexList, TheAcademyClassificationTypeNames, offsetof( UpgradeTemplate, m_academyClassificationType ) },
  101. { NULL, NULL, NULL, 0 } // keep this last
  102. };
  103. //-------------------------------------------------------------------------------------------------
  104. //-------------------------------------------------------------------------------------------------
  105. UpgradeTemplate::UpgradeTemplate( void )
  106. {
  107. //Added By Sadullah Nader
  108. //Initialization(s) inserted
  109. m_cost = 0;
  110. //
  111. m_type = UPGRADE_TYPE_PLAYER;
  112. m_nameKey = NAMEKEY_INVALID;
  113. m_buildTime = 0.0f;
  114. m_next = NULL;
  115. m_prev = NULL;
  116. m_buttonImage = NULL;
  117. m_academyClassificationType = ACT_NONE;
  118. } // end UpgradeTemplate
  119. //-------------------------------------------------------------------------------------------------
  120. //-------------------------------------------------------------------------------------------------
  121. UpgradeTemplate::~UpgradeTemplate( void )
  122. {
  123. } // end ~UpgradeTemplate
  124. //-------------------------------------------------------------------------------------------------
  125. /** Calculate the time it takes (in logic frames) for a player to build this UpgradeTemplate */
  126. //-------------------------------------------------------------------------------------------------
  127. Int UpgradeTemplate::calcTimeToBuild( Player *player ) const
  128. {
  129. #if defined(_DEBUG) || defined(_INTERNAL) || defined(_ALLOW_DEBUG_CHEATS_IN_RELEASE)
  130. if( player->buildsInstantly() )
  131. {
  132. return 1;
  133. }
  134. #endif
  135. ///@todo modify this by power state of player
  136. return m_buildTime * LOGICFRAMES_PER_SECOND;
  137. } // end calcTimeToBuild
  138. //-------------------------------------------------------------------------------------------------
  139. /** Calculate the cost takes this player to build this upgrade */
  140. //-------------------------------------------------------------------------------------------------
  141. Int UpgradeTemplate::calcCostToBuild( Player *player ) const
  142. {
  143. ///@todo modify this by any player handicaps
  144. return m_cost;
  145. } // end calcCostToBuild
  146. //-------------------------------------------------------------------------------------------------
  147. //-------------------------------------------------------------------------------------------------
  148. static AsciiString getVetUpgradeName(VeterancyLevel v)
  149. {
  150. AsciiString tmp;
  151. tmp = "Upgrade_Veterancy_";
  152. tmp.concat(TheVeterancyNames[v]);
  153. return tmp;
  154. }
  155. //-------------------------------------------------------------------------------------------------
  156. //-------------------------------------------------------------------------------------------------
  157. void UpgradeTemplate::friend_makeVeterancyUpgrade(VeterancyLevel v)
  158. {
  159. m_type = UPGRADE_TYPE_OBJECT; // veterancy "upgrades" are always per-object, not per-player
  160. m_name = getVetUpgradeName(v);
  161. m_nameKey = TheNameKeyGenerator->nameToKey( m_name );
  162. m_displayNameLabel.clear(); // should never be displayed
  163. m_buildTime = 0.0f;
  164. m_cost = 0.0f;
  165. // leave this alone.
  166. //m_upgradeMask = ???;
  167. }
  168. //-------------------------------------------------------------------------------------------------
  169. //-------------------------------------------------------------------------------------------------
  170. void UpgradeTemplate::cacheButtonImage()
  171. {
  172. if( m_buttonImageName.isNotEmpty() )
  173. {
  174. m_buttonImage = TheMappedImageCollection->findImageByName( m_buttonImageName );
  175. DEBUG_ASSERTCRASH( m_buttonImage, ("UpgradeTemplate: %s is looking for button image %s but can't find it. Skipping...", m_name.str(), m_buttonImageName.str() ) );
  176. m_buttonImageName.clear(); // we're done with this, so nuke it
  177. }
  178. }
  179. ///////////////////////////////////////////////////////////////////////////////////////////////////
  180. // UPGRADE CENTER /////////////////////////////////////////////////////////////////////////////////
  181. ///////////////////////////////////////////////////////////////////////////////////////////////////
  182. //-------------------------------------------------------------------------------------------------
  183. //-------------------------------------------------------------------------------------------------
  184. UpgradeCenter::UpgradeCenter( void )
  185. {
  186. m_upgradeList = NULL;
  187. m_nextTemplateMaskBit = 0;
  188. buttonImagesCached = FALSE;
  189. } // end UpgradeCenter
  190. //-------------------------------------------------------------------------------------------------
  191. //-------------------------------------------------------------------------------------------------
  192. UpgradeCenter::~UpgradeCenter( void )
  193. {
  194. // delete all the upgrades loaded from the INI database
  195. UpgradeTemplate *next;
  196. while( m_upgradeList )
  197. {
  198. // get next
  199. next = m_upgradeList->friend_getNext();
  200. // delete head of list
  201. m_upgradeList->deleteInstance();
  202. // set head to next element
  203. m_upgradeList = next;
  204. } // end while
  205. } // end ~UpgradeCenter
  206. //-------------------------------------------------------------------------------------------------
  207. /** Upgrade center initialization */
  208. //-------------------------------------------------------------------------------------------------
  209. void UpgradeCenter::init( void )
  210. {
  211. UpgradeTemplate* up;
  212. // name will be overridden by friend_makeVeterancyUpgrade
  213. // no, there ISN'T an upgrade for this one...
  214. //up = newUpgrade("");
  215. //up->friend_makeVeterancyUpgrade(LEVEL_REGULAR);
  216. up = newUpgrade("");
  217. up->friend_makeVeterancyUpgrade(LEVEL_VETERAN);
  218. up = newUpgrade("");
  219. up->friend_makeVeterancyUpgrade(LEVEL_ELITE);
  220. up = newUpgrade("");
  221. up->friend_makeVeterancyUpgrade(LEVEL_HEROIC);
  222. }
  223. //-------------------------------------------------------------------------------------------------
  224. /** Upgrade center system reset */
  225. //-------------------------------------------------------------------------------------------------
  226. void UpgradeCenter::reset( void )
  227. {
  228. if( TheMappedImageCollection && !buttonImagesCached )
  229. {
  230. UpgradeTemplate *upgrade;
  231. for( upgrade = m_upgradeList; upgrade; upgrade = upgrade->friend_getNext() )
  232. {
  233. upgrade->cacheButtonImage();
  234. }
  235. buttonImagesCached = TRUE;
  236. }
  237. }
  238. //-------------------------------------------------------------------------------------------------
  239. /** Find upgrade matching name key */
  240. //-------------------------------------------------------------------------------------------------
  241. const UpgradeTemplate *UpgradeCenter::findVeterancyUpgrade( VeterancyLevel level ) const
  242. {
  243. AsciiString tmp = getVetUpgradeName(level);
  244. return findUpgrade(tmp);
  245. }
  246. //-------------------------------------------------------------------------------------------------
  247. /** Find upgrade matching name key */
  248. //-------------------------------------------------------------------------------------------------
  249. UpgradeTemplate *UpgradeCenter::findNonConstUpgradeByKey( NameKeyType key )
  250. {
  251. UpgradeTemplate *upgrade;
  252. // search list
  253. for( upgrade = m_upgradeList; upgrade; upgrade = upgrade->friend_getNext() )
  254. if( upgrade->getUpgradeNameKey() == key )
  255. return upgrade;
  256. // item not found
  257. return NULL;
  258. }
  259. // ------------------------------------------------------------------------------------------------
  260. /** Return the first upgrade template */
  261. // ------------------------------------------------------------------------------------------------
  262. UpgradeTemplate *UpgradeCenter::firstUpgradeTemplate( void )
  263. {
  264. return m_upgradeList;
  265. } // end firstUpgradeTemplate
  266. //-------------------------------------------------------------------------------------------------
  267. /** Find upgrade matching name key */
  268. //-------------------------------------------------------------------------------------------------
  269. const UpgradeTemplate *UpgradeCenter::findUpgradeByKey( NameKeyType key ) const
  270. {
  271. const UpgradeTemplate *upgrade;
  272. // search list
  273. for( upgrade = m_upgradeList; upgrade; upgrade = upgrade->friend_getNext() )
  274. if( upgrade->getUpgradeNameKey() == key )
  275. return upgrade;
  276. // item not found
  277. return NULL;
  278. }
  279. //-------------------------------------------------------------------------------------------------
  280. /** Find upgrade matching name */
  281. //-------------------------------------------------------------------------------------------------
  282. const UpgradeTemplate *UpgradeCenter::findUpgrade( const AsciiString& name ) const
  283. {
  284. return findUpgradeByKey( TheNameKeyGenerator->nameToKey( name ) );
  285. } // end findUpgrade
  286. //-------------------------------------------------------------------------------------------------
  287. /** Allocate a new upgrade template */
  288. //-------------------------------------------------------------------------------------------------
  289. UpgradeTemplate *UpgradeCenter::newUpgrade( const AsciiString& name )
  290. {
  291. UpgradeTemplate *newUpgrade = newInstance(UpgradeTemplate);
  292. // copy data from the default upgrade
  293. const UpgradeTemplate *defaultUpgrade = findUpgrade( "DefaultUpgrade" );
  294. if( defaultUpgrade )
  295. *newUpgrade = *defaultUpgrade;
  296. // assign name and starting data
  297. newUpgrade->setUpgradeName( name );
  298. newUpgrade->setUpgradeNameKey( TheNameKeyGenerator->nameToKey( name ) );
  299. // Make a unique bitmask for this new template by keeping track of what bits have been assigned
  300. // damn MSFT! proper ANSI syntax for a proper 64-bit constant is "1LL", but MSVC doesn't recognize it
  301. UpgradeMaskType newMask;
  302. newMask.set( m_nextTemplateMaskBit );
  303. //Int64 newMask = 1i64 << m_nextTemplateMaskBit;
  304. m_nextTemplateMaskBit++;
  305. DEBUG_ASSERTCRASH( m_nextTemplateMaskBit < UPGRADE_MAX_COUNT, ("Can't have over %d types of Upgrades and have a Bitfield function.", UPGRADE_MAX_COUNT) );
  306. newUpgrade->friend_setUpgradeMask( newMask );
  307. // link upgrade
  308. linkUpgrade( newUpgrade );
  309. // return new upgrade
  310. return newUpgrade;
  311. } // end newUnlinkedUpgrade
  312. //-------------------------------------------------------------------------------------------------
  313. /** Link an upgrade to our list */
  314. //-------------------------------------------------------------------------------------------------
  315. void UpgradeCenter::linkUpgrade( UpgradeTemplate *upgrade )
  316. {
  317. // sanity
  318. if( upgrade == NULL )
  319. return;
  320. // link
  321. upgrade->friend_setPrev( NULL );
  322. upgrade->friend_setNext( m_upgradeList );
  323. if( m_upgradeList )
  324. m_upgradeList->friend_setPrev( upgrade );
  325. m_upgradeList = upgrade;
  326. } // end linkUpgrade
  327. //-------------------------------------------------------------------------------------------------
  328. /** Unlink an upgrade from our list */
  329. //-------------------------------------------------------------------------------------------------
  330. void UpgradeCenter::unlinkUpgrade( UpgradeTemplate *upgrade )
  331. {
  332. // sanity
  333. if( upgrade == NULL )
  334. return;
  335. if( upgrade->friend_getNext() )
  336. upgrade->friend_getNext()->friend_setPrev( upgrade->friend_getPrev() );
  337. if( upgrade->friend_getPrev() )
  338. upgrade->friend_getPrev()->friend_setNext( upgrade->friend_getNext() );
  339. else
  340. m_upgradeList = upgrade->friend_getNext();
  341. } // end unlinkUpgrade
  342. //-------------------------------------------------------------------------------------------------
  343. /** does this player have all the necessary things to make this upgrade */
  344. //-------------------------------------------------------------------------------------------------
  345. Bool UpgradeCenter::canAffordUpgrade( Player *player, const UpgradeTemplate *upgradeTemplate, Bool displayReason ) const
  346. {
  347. // sanity
  348. if( player == NULL || upgradeTemplate == NULL )
  349. return FALSE;
  350. // money check
  351. Money *money = player->getMoney();
  352. if( money->countMoney() < upgradeTemplate->calcCostToBuild( player ) )
  353. {
  354. //Post reason why we can't make upgrade!
  355. if( displayReason )
  356. {
  357. TheInGameUI->message( "GUI:NotEnoughMoneyToUpgrade" );
  358. }
  359. return FALSE;
  360. }
  361. /// @todo maybe have prereq checks for upgrades???
  362. return TRUE; // all is well
  363. } // end canAffordUpgrade
  364. //-------------------------------------------------------------------------------------------------
  365. /** generate a list of upgrade names for WorldBuilder */
  366. //-------------------------------------------------------------------------------------------------
  367. std::vector<AsciiString> UpgradeCenter::getUpgradeNames( void ) const
  368. {
  369. std::vector<AsciiString> upgradeNames;
  370. for( UpgradeTemplate *upgrade = m_upgradeList; upgrade; upgrade = upgrade->friend_getNext() )
  371. upgradeNames.push_back(upgrade->getUpgradeName());
  372. return upgradeNames;
  373. } // end getUpgradeNames
  374. //-------------------------------------------------------------------------------------------------
  375. /** Parse an upgrade definition */
  376. //-------------------------------------------------------------------------------------------------
  377. void UpgradeCenter::parseUpgradeDefinition( INI *ini )
  378. {
  379. // read the name
  380. const char* c = ini->getNextToken();
  381. AsciiString name = c;
  382. // find existing item if present
  383. UpgradeTemplate* upgrade = TheUpgradeCenter->findNonConstUpgradeByKey( NAMEKEY(name) );
  384. if( upgrade == NULL )
  385. {
  386. // allocate a new item
  387. upgrade = TheUpgradeCenter->newUpgrade( name );
  388. } // end if
  389. // sanity
  390. DEBUG_ASSERTCRASH( upgrade, ("parseUpgradeDefinition: Unable to allocate upgrade '%s'\n", name.str()) );
  391. // parse the ini definition
  392. ini->initFromINI( upgrade, upgrade->getFieldParse() );
  393. }