ScoreKeeper.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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: ScoreKeeper.cpp /////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Electronic Arts Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2002 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // created: Jun 2002
  34. //
  35. // Filename: ScoreKeeper.cpp
  36. //
  37. // author: Chris Huybregts
  38. //
  39. // purpose: Score Keeper class will be an object attached to each player
  40. // that will maintain accurate counts for the various stats we
  41. // want to show on the score screen. The information in here
  42. // could also be used for the observer screen
  43. //
  44. //-----------------------------------------------------------------------------
  45. ///////////////////////////////////////////////////////////////////////////////
  46. //-----------------------------------------------------------------------------
  47. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  48. //-----------------------------------------------------------------------------
  49. //-----------------------------------------------------------------------------
  50. // USER INCLUDES //////////////////////////////////////////////////////////////
  51. //-----------------------------------------------------------------------------
  52. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  53. #include "Common/GameState.h"
  54. #include "Common/KindOf.h"
  55. #include "Common/Player.h"
  56. #include "Common/ScoreKeeper.h"
  57. #include "Common/ThingFactory.h"
  58. #include "Common/ThingTemplate.h"
  59. #include "Common/Xfer.h"
  60. #include "GameLogic/Object.h"
  61. #include "GameLogic/GameLogic.h"
  62. //-----------------------------------------------------------------------------
  63. // DEFINES ////////////////////////////////////////////////////////////////////
  64. //-----------------------------------------------------------------------------
  65. //-----------------------------------------------------------------------------
  66. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  67. //-----------------------------------------------------------------------------
  68. ScoreKeeper::ScoreKeeper( void )
  69. {
  70. reset(0);
  71. }
  72. ScoreKeeper::~ScoreKeeper( void )
  73. {
  74. }
  75. static KindOfMaskType scoringBuildingMask;
  76. static KindOfMaskType scoringBuildingDestroyMask;
  77. static KindOfMaskType scoringBuildingCreateMask;
  78. void ScoreKeeper::reset( Int playerIdx )
  79. {
  80. scoringBuildingMask.set(KINDOF_STRUCTURE);
  81. scoringBuildingMask.set(KINDOF_SCORE);
  82. scoringBuildingCreateMask.set(KINDOF_STRUCTURE);
  83. scoringBuildingCreateMask.set(KINDOF_SCORE_CREATE);
  84. scoringBuildingDestroyMask.set(KINDOF_STRUCTURE);
  85. scoringBuildingDestroyMask.set(KINDOF_SCORE_DESTROY);
  86. m_totalMoneyEarned = m_totalMoneySpent = 0;
  87. m_totalUnitsLost = m_totalUnitsBuilt = 0;
  88. m_totalBuildingsLost = m_totalBuildingsBuilt = 0;
  89. //Added By Sadullah Nader
  90. //Initializtion(s) inserted
  91. m_totalFactionBuildingsCaptured = m_totalTechBuildingsCaptured = 0;
  92. //
  93. m_currentScore = 0;
  94. m_objectsBuilt.clear();
  95. m_objectsCaptured.clear();
  96. m_objectsLost.clear();
  97. for(int i = 0; i < MAX_PLAYER_COUNT; ++i)
  98. {
  99. m_objectsDestroyed[i].clear();
  100. m_totalBuildingsDestroyed[i] = m_totalUnitsDestroyed[i] = 0;
  101. }
  102. m_myPlayerIdx = playerIdx;
  103. }
  104. void ScoreKeeper::addObjectBuilt( const Object *o)
  105. {
  106. Bool addToCount = FALSE;
  107. if (TheGameLogic->isScoringEnabled() == FALSE) {
  108. return;
  109. }
  110. if(o->getTemplate()->isKindOfMulti(scoringBuildingMask, KINDOFMASK_NONE))
  111. {
  112. ++m_totalBuildingsBuilt;
  113. addToCount = TRUE;
  114. }
  115. else if (o->getTemplate()->isKindOfMulti(scoringBuildingCreateMask, KINDOFMASK_NONE))
  116. {
  117. ++m_totalBuildingsBuilt;
  118. addToCount = TRUE;
  119. }
  120. else if(o->getTemplate()->isKindOf(KINDOF_INFANTRY) || o->getTemplate()->isKindOf(KINDOF_VEHICLE))
  121. {
  122. if (o->getTemplate()->isKindOf(KINDOF_SCORE) || o->getTemplate()->isKindOf(KINDOF_SCORE_CREATE))
  123. {
  124. ++m_totalUnitsBuilt;
  125. addToCount = TRUE;
  126. }
  127. }
  128. if(addToCount)
  129. {
  130. Int existingCount = 0;
  131. ObjectCountMapIt it = m_objectsBuilt.find(o->getTemplate());
  132. if (it != m_objectsBuilt.end())
  133. existingCount = it->second;
  134. m_objectsBuilt[o->getTemplate()] = existingCount + 1;
  135. }
  136. }
  137. Int ScoreKeeper::getTotalUnitsBuilt( KindOfMaskType validMask, KindOfMaskType invalidMask )
  138. {
  139. Int count = 0;
  140. for (ObjectCountMapIt it = m_objectsBuilt.begin(); it != m_objectsBuilt.end(); ++it)
  141. {
  142. const ThingTemplate *theTemplate = it->first;
  143. Int numBuilt = it->second;
  144. if (theTemplate && theTemplate->isKindOfMulti(validMask, invalidMask))
  145. count += numBuilt;
  146. }
  147. return count;
  148. }
  149. Int ScoreKeeper::getTotalObjectsBuilt( const ThingTemplate *pTemplate )
  150. {
  151. Int count = 0;
  152. for (ObjectCountMapIt it = m_objectsBuilt.begin(); it != m_objectsBuilt.end(); ++it)
  153. {
  154. const ThingTemplate *theTemplate = it->first;
  155. if (theTemplate->isEquivalentTo(pTemplate))
  156. ++count;
  157. }
  158. return count;
  159. }
  160. void ScoreKeeper::removeObjectBuilt( const Object *o)
  161. {
  162. if (TheGameLogic->isScoringEnabled() == FALSE) {
  163. return;
  164. }
  165. Bool removeFromCount = FALSE;
  166. if (o->getTemplate()->isKindOfMulti(scoringBuildingMask, KINDOFMASK_NONE))
  167. {
  168. --m_totalBuildingsBuilt;
  169. removeFromCount = TRUE;
  170. }
  171. else if (o->getTemplate()->isKindOfMulti(scoringBuildingCreateMask, KINDOFMASK_NONE))
  172. {
  173. --m_totalBuildingsBuilt;
  174. removeFromCount = TRUE;
  175. }
  176. else if (o->getTemplate()->isKindOf(KINDOF_INFANTRY) || o->getTemplate()->isKindOf(KINDOF_VEHICLE))
  177. {
  178. if (o->getTemplate()->isKindOf(KINDOF_SCORE) || o->getTemplate()->isKindOf(KINDOF_SCORE_CREATE))
  179. {
  180. --m_totalUnitsBuilt;
  181. removeFromCount = TRUE;
  182. }
  183. }
  184. if (removeFromCount)
  185. {
  186. Int existingCount = 0;
  187. ObjectCountMapIt it = m_objectsBuilt.find(o->getTemplate());
  188. if (it != m_objectsBuilt.end())
  189. existingCount = it->second;
  190. m_objectsBuilt[o->getTemplate()] = existingCount - 1;
  191. }
  192. }
  193. void ScoreKeeper::addObjectCaptured( const Object *o )
  194. {
  195. if (TheGameLogic->isScoringEnabled() == FALSE) {
  196. return;
  197. }
  198. Bool addToCount = FALSE;
  199. if(o->getTemplate()->isKindOf(KINDOF_STRUCTURE))
  200. {
  201. if (o->getTemplate()->isKindOf(KINDOF_SCORE))
  202. {
  203. ++m_totalFactionBuildingsCaptured;
  204. }
  205. else
  206. {
  207. ++m_totalTechBuildingsCaptured;
  208. }
  209. addToCount = TRUE;
  210. }
  211. if(addToCount)
  212. {
  213. Int existingCount = 0;
  214. ObjectCountMapIt it = m_objectsCaptured.find(o->getTemplate());
  215. if (it != m_objectsCaptured.end())
  216. existingCount = it->second;
  217. m_objectsCaptured[o->getTemplate()] = existingCount + 1;
  218. }
  219. }
  220. void ScoreKeeper::addObjectDestroyed( const Object *o)
  221. {
  222. if (TheGameLogic->isScoringEnabled() == FALSE) {
  223. return;
  224. }
  225. Int playerIdx = o->getControllingPlayer()->getPlayerIndex();
  226. Bool addToCount = FALSE;
  227. if(o->getTemplate()->isKindOfMulti(scoringBuildingMask, KINDOFMASK_NONE))
  228. {
  229. if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
  230. ++m_totalBuildingsDestroyed[playerIdx];
  231. addToCount = TRUE;
  232. }
  233. }
  234. else if (o->getTemplate()->isKindOfMulti(scoringBuildingDestroyMask, KINDOFMASK_NONE))
  235. {
  236. if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
  237. ++m_totalBuildingsDestroyed[playerIdx];
  238. addToCount = TRUE;
  239. }
  240. }
  241. else if(o->getTemplate()->isKindOf(KINDOF_INFANTRY) || o->getTemplate()->isKindOf(KINDOF_VEHICLE))
  242. {
  243. if (o->getTemplate()->isKindOf(KINDOF_SCORE) || o->getTemplate()->isKindOf(KINDOF_SCORE_DESTROY))
  244. {
  245. if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
  246. m_totalUnitsDestroyed[playerIdx]++;
  247. addToCount = TRUE;
  248. }
  249. }
  250. }
  251. if(addToCount)
  252. {
  253. Int existingCount = 0;
  254. ObjectCountMapIt it = m_objectsDestroyed[playerIdx].find(o->getTemplate());
  255. if (it != m_objectsDestroyed[playerIdx].end())
  256. existingCount = it->second;
  257. m_objectsDestroyed[playerIdx][o->getTemplate()] = existingCount + 1;
  258. }
  259. }
  260. void ScoreKeeper::addObjectLost( const Object *o )
  261. {
  262. if (TheGameLogic->isScoringEnabled() == FALSE) {
  263. return;
  264. }
  265. Bool addToCount = FALSE;
  266. if(o->getTemplate()->isKindOfMulti(scoringBuildingMask, KINDOFMASK_NONE))
  267. {
  268. if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
  269. ++m_totalBuildingsLost;
  270. addToCount = TRUE;
  271. }
  272. }
  273. else if (o->getTemplate()->isKindOfMulti(scoringBuildingDestroyMask, KINDOFMASK_NONE))
  274. {
  275. if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
  276. ++m_totalBuildingsLost;
  277. addToCount = TRUE;
  278. }
  279. }
  280. else if(o->getTemplate()->isKindOf(KINDOF_INFANTRY) || o->getTemplate()->isKindOf(KINDOF_VEHICLE))
  281. {
  282. if (o->getTemplate()->isKindOf(KINDOF_SCORE) || o->getTemplate()->isKindOf(KINDOF_SCORE_DESTROY))
  283. {
  284. if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
  285. ++m_totalUnitsLost;
  286. addToCount = TRUE;
  287. }
  288. }
  289. }
  290. if(addToCount)
  291. {
  292. Int existingCount = 0;
  293. ObjectCountMapIt it = m_objectsLost.find(o->getTemplate());
  294. if (it != m_objectsLost.end())
  295. existingCount = it->second;
  296. m_objectsLost[o->getTemplate()] = existingCount + 1;
  297. }
  298. }
  299. Int ScoreKeeper::calculateScore( void )
  300. {
  301. Int score = 0;
  302. score += m_totalUnitsBuilt * 100;
  303. score += m_totalMoneyEarned;
  304. score += m_totalBuildingsBuilt * 100;
  305. for (Int i = 0; i < MAX_PLAYER_COUNT; ++i)
  306. {
  307. if(i == m_myPlayerIdx)
  308. continue;
  309. score += m_totalUnitsDestroyed[i] * 100;
  310. score += m_totalBuildingsDestroyed[i] * 100;
  311. }
  312. m_currentScore = score;
  313. return m_currentScore;
  314. }
  315. //-----------------------------------------------------------------------------
  316. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  317. //-----------------------------------------------------------------------------
  318. Int ScoreKeeper::getTotalBuildingsDestroyed( void )
  319. {
  320. int count = 0;
  321. for (int i = 0; i< MAX_PLAYER_COUNT; ++i)
  322. {
  323. // Design change, display even if we killed our own
  324. // if(i == m_myPlayerIdx)
  325. // continue;
  326. count += m_totalBuildingsDestroyed[i];
  327. //for (ObjectCountMapIt it = m_objectsDestroyed[i].begin(); it != m_objectsDestroyed[i].end(); ++it)
  328. // {
  329. //
  330. // count += it->second;
  331. // }
  332. }
  333. return count;
  334. }
  335. Int ScoreKeeper::getTotalUnitsDestroyed( void )
  336. {
  337. int count = 0;
  338. for (int i = 0; i< MAX_PLAYER_COUNT; ++i)
  339. {
  340. // Design change, display even if we killed our own
  341. // if(i == m_myPlayerIdx)
  342. // continue;
  343. count += m_totalUnitsDestroyed[i];
  344. // for (ObjectCountMapIt it = m_objectsDestroyed[i].begin(); it != m_objectsDestroyed[i].end(); ++it)
  345. // {
  346. // }
  347. }
  348. return count;
  349. }
  350. // ------------------------------------------------------------------------------------------------
  351. /** CRC */
  352. // ------------------------------------------------------------------------------------------------
  353. void ScoreKeeper::crc( Xfer *xfer )
  354. {
  355. } // end ScoreKeeper
  356. // ------------------------------------------------------------------------------------------------
  357. /** Xfer of an object count map
  358. * Version Info:
  359. * 1: Initial version */
  360. // ------------------------------------------------------------------------------------------------
  361. void ScoreKeeper::xferObjectCountMap( Xfer *xfer, ObjectCountMap *map )
  362. {
  363. // sanity
  364. if( map == NULL )
  365. {
  366. DEBUG_CRASH(( "xferObjectCountMap - Invalid map parameter\n" ));
  367. throw SC_INVALID_DATA;
  368. } // end if
  369. // version info
  370. XferVersion currentVersion = 1;
  371. XferVersion version = currentVersion;
  372. xfer->xferVersion( &version, currentVersion );
  373. // size of the map
  374. UnsignedShort mapSize = map->size();
  375. xfer->xferUnsignedShort( &mapSize );
  376. // map data
  377. Int count;
  378. const ThingTemplate *thingTemplate;
  379. AsciiString thingTemplateName;
  380. if( xfer->getXferMode() == XFER_SAVE )
  381. {
  382. ObjectCountMapIt it;
  383. // save all entries
  384. for( it = map->begin(); it != map->end(); ++it )
  385. {
  386. // thing template
  387. thingTemplate = it->first;
  388. thingTemplateName = thingTemplate->getName();
  389. xfer->xferAsciiString( &thingTemplateName );
  390. // the count
  391. count = it->second;
  392. xfer->xferInt( &count );
  393. } // end for, it
  394. } // end if, save
  395. else
  396. {
  397. // read all entries
  398. for( UnsignedShort i = 0; i < mapSize; ++i )
  399. {
  400. // read thing template name
  401. xfer->xferAsciiString( &thingTemplateName );
  402. thingTemplate = TheThingFactory->findTemplate( thingTemplateName );
  403. if( thingTemplate == NULL )
  404. {
  405. DEBUG_CRASH(( "xferObjectCountMap - Unknown thing template '%s'\n", thingTemplateName.str() ));
  406. throw SC_INVALID_DATA;
  407. } // end if
  408. // read count
  409. xfer->xferInt( &count );
  410. // add to map
  411. (*map)[ thingTemplate ] = count;
  412. } // end for, i
  413. } // end else
  414. } // end xferObjectCountMap
  415. // ------------------------------------------------------------------------------------------------
  416. /** Xfer method
  417. * Version Info:
  418. * 1: Initial version */
  419. // ------------------------------------------------------------------------------------------------
  420. void ScoreKeeper::xfer( Xfer *xfer )
  421. {
  422. // version
  423. XferVersion currentVersion = 1;
  424. XferVersion version = currentVersion;
  425. xfer->xferVersion( &version, currentVersion );
  426. // money earned
  427. xfer->xferInt( &m_totalMoneyEarned );
  428. // money spent
  429. xfer->xferInt( &m_totalMoneySpent );
  430. // units destroyed
  431. xfer->xferUser( m_totalUnitsDestroyed, sizeof( Int ) * MAX_PLAYER_COUNT );
  432. // units built
  433. xfer->xferInt( &m_totalUnitsBuilt );
  434. // units lost
  435. xfer->xferInt( &m_totalUnitsLost );
  436. // buildings destroyed
  437. xfer->xferUser( m_totalBuildingsDestroyed, sizeof( Int ) * MAX_PLAYER_COUNT );
  438. // buildings built
  439. xfer->xferInt( &m_totalBuildingsBuilt );
  440. // buildings lost
  441. xfer->xferInt( &m_totalBuildingsLost );
  442. // tech buildings captured
  443. xfer->xferInt( &m_totalTechBuildingsCaptured );
  444. // faction buildings captured
  445. xfer->xferInt( &m_totalFactionBuildingsCaptured );
  446. // current score
  447. xfer->xferInt( &m_currentScore );
  448. // player index
  449. xfer->xferInt( &m_myPlayerIdx );
  450. // objects built
  451. xferObjectCountMap( xfer, &m_objectsBuilt );
  452. // objects destroyed
  453. UnsignedShort destroyedArraySize = MAX_PLAYER_COUNT;
  454. xfer->xferUnsignedShort( &destroyedArraySize );
  455. if( destroyedArraySize != MAX_PLAYER_COUNT )
  456. {
  457. DEBUG_CRASH(( "ScoreKeeper::xfer - size of objects destroyed array has changed\n" ));
  458. throw SC_INVALID_DATA;
  459. } // end if
  460. for( UnsignedShort i = 0; i < destroyedArraySize; ++i )
  461. {
  462. // xfer map data
  463. xferObjectCountMap( xfer, &m_objectsDestroyed[ i ] );
  464. } // end for i
  465. // objects lost
  466. xferObjectCountMap( xfer, &m_objectsLost );
  467. // objects captured
  468. xferObjectCountMap( xfer, &m_objectsCaptured );
  469. } // end xfer
  470. // ------------------------------------------------------------------------------------------------
  471. /** Load post process */
  472. // ------------------------------------------------------------------------------------------------
  473. void ScoreKeeper::loadPostProcess( void )
  474. {
  475. } // end loadPostProcess