| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- /*
- ** Command & Conquer Generals Zero Hour(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // (c) 2001-2003 Electronic Arts Inc. //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- // FILE: ScoreKeeper.cpp /////////////////////////////////////////////////
- //-----------------------------------------------------------------------------
- //
- // Electronic Arts Pacific.
- //
- // Confidential Information
- // Copyright (C) 2002 - All Rights Reserved
- //
- //-----------------------------------------------------------------------------
- //
- // created: Jun 2002
- //
- // Filename: ScoreKeeper.cpp
- //
- // author: Chris Huybregts
- //
- // purpose: Score Keeper class will be an object attached to each player
- // that will maintain accurate counts for the various stats we
- // want to show on the score screen. The information in here
- // could also be used for the observer screen
- //
- //-----------------------------------------------------------------------------
- ///////////////////////////////////////////////////////////////////////////////
- //-----------------------------------------------------------------------------
- // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- // USER INCLUDES //////////////////////////////////////////////////////////////
- //-----------------------------------------------------------------------------
- #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
- #include "Common/GameState.h"
- #include "Common/KindOf.h"
- #include "Common/Player.h"
- #include "Common/ScoreKeeper.h"
- #include "Common/ThingFactory.h"
- #include "Common/ThingTemplate.h"
- #include "Common/Xfer.h"
- #include "GameLogic/Object.h"
- #include "GameLogic/GameLogic.h"
- //-----------------------------------------------------------------------------
- // DEFINES ////////////////////////////////////////////////////////////////////
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
- //-----------------------------------------------------------------------------
- ScoreKeeper::ScoreKeeper( void )
- {
- reset(0);
- }
- ScoreKeeper::~ScoreKeeper( void )
- {
- }
- static KindOfMaskType scoringBuildingMask;
- static KindOfMaskType scoringBuildingDestroyMask;
- static KindOfMaskType scoringBuildingCreateMask;
- void ScoreKeeper::reset( Int playerIdx )
- {
- scoringBuildingMask.set(KINDOF_STRUCTURE);
- scoringBuildingMask.set(KINDOF_SCORE);
- scoringBuildingCreateMask.set(KINDOF_STRUCTURE);
- scoringBuildingCreateMask.set(KINDOF_SCORE_CREATE);
- scoringBuildingDestroyMask.set(KINDOF_STRUCTURE);
- scoringBuildingDestroyMask.set(KINDOF_SCORE_DESTROY);
- m_totalMoneyEarned = m_totalMoneySpent = 0;
- m_totalUnitsLost = m_totalUnitsBuilt = 0;
- m_totalBuildingsLost = m_totalBuildingsBuilt = 0;
- //Added By Sadullah Nader
- //Initializtion(s) inserted
- m_totalFactionBuildingsCaptured = m_totalTechBuildingsCaptured = 0;
- //
- m_currentScore = 0;
- m_objectsBuilt.clear();
- m_objectsCaptured.clear();
- m_objectsLost.clear();
- for(int i = 0; i < MAX_PLAYER_COUNT; ++i)
- {
- m_objectsDestroyed[i].clear();
- m_totalBuildingsDestroyed[i] = m_totalUnitsDestroyed[i] = 0;
- }
- m_myPlayerIdx = playerIdx;
- }
-
- void ScoreKeeper::addObjectBuilt( const Object *o)
- {
- Bool addToCount = FALSE;
- if (TheGameLogic->isScoringEnabled() == FALSE) {
- return;
- }
- if(o->getTemplate()->isKindOfMulti(scoringBuildingMask, KINDOFMASK_NONE))
- {
- ++m_totalBuildingsBuilt;
- addToCount = TRUE;
- }
- else if (o->getTemplate()->isKindOfMulti(scoringBuildingCreateMask, KINDOFMASK_NONE))
- {
- ++m_totalBuildingsBuilt;
- addToCount = TRUE;
- }
- else if(o->getTemplate()->isKindOf(KINDOF_INFANTRY) || o->getTemplate()->isKindOf(KINDOF_VEHICLE))
- {
- if (o->getTemplate()->isKindOf(KINDOF_SCORE) || o->getTemplate()->isKindOf(KINDOF_SCORE_CREATE))
- {
- ++m_totalUnitsBuilt;
- addToCount = TRUE;
- }
- }
-
- if(addToCount)
- {
- Int existingCount = 0;
- ObjectCountMapIt it = m_objectsBuilt.find(o->getTemplate());
- if (it != m_objectsBuilt.end())
- existingCount = it->second;
- m_objectsBuilt[o->getTemplate()] = existingCount + 1;
- }
- }
- Int ScoreKeeper::getTotalUnitsBuilt( KindOfMaskType validMask, KindOfMaskType invalidMask )
- {
- Int count = 0;
- for (ObjectCountMapIt it = m_objectsBuilt.begin(); it != m_objectsBuilt.end(); ++it)
- {
- const ThingTemplate *theTemplate = it->first;
- Int numBuilt = it->second;
- if (theTemplate && theTemplate->isKindOfMulti(validMask, invalidMask))
- count += numBuilt;
- }
- return count;
- }
- Int ScoreKeeper::getTotalObjectsBuilt( const ThingTemplate *pTemplate )
- {
- Int count = 0;
- for (ObjectCountMapIt it = m_objectsBuilt.begin(); it != m_objectsBuilt.end(); ++it)
- {
- const ThingTemplate *theTemplate = it->first;
- if (theTemplate->isEquivalentTo(pTemplate))
- ++count;
- }
- return count;
- }
- void ScoreKeeper::removeObjectBuilt( const Object *o)
- {
- if (TheGameLogic->isScoringEnabled() == FALSE) {
- return;
- }
- Bool removeFromCount = FALSE;
- if (o->getTemplate()->isKindOfMulti(scoringBuildingMask, KINDOFMASK_NONE))
- {
- --m_totalBuildingsBuilt;
- removeFromCount = TRUE;
- }
- else if (o->getTemplate()->isKindOfMulti(scoringBuildingCreateMask, KINDOFMASK_NONE))
- {
- --m_totalBuildingsBuilt;
- removeFromCount = TRUE;
- }
- else if (o->getTemplate()->isKindOf(KINDOF_INFANTRY) || o->getTemplate()->isKindOf(KINDOF_VEHICLE))
- {
- if (o->getTemplate()->isKindOf(KINDOF_SCORE) || o->getTemplate()->isKindOf(KINDOF_SCORE_CREATE))
- {
- --m_totalUnitsBuilt;
- removeFromCount = TRUE;
- }
- }
- if (removeFromCount)
- {
- Int existingCount = 0;
- ObjectCountMapIt it = m_objectsBuilt.find(o->getTemplate());
- if (it != m_objectsBuilt.end())
- existingCount = it->second;
- m_objectsBuilt[o->getTemplate()] = existingCount - 1;
- }
- }
- void ScoreKeeper::addObjectCaptured( const Object *o )
- {
- if (TheGameLogic->isScoringEnabled() == FALSE) {
- return;
- }
- Bool addToCount = FALSE;
- if(o->getTemplate()->isKindOf(KINDOF_STRUCTURE))
- {
- if (o->getTemplate()->isKindOf(KINDOF_SCORE))
- {
- ++m_totalFactionBuildingsCaptured;
- }
- else
- {
- ++m_totalTechBuildingsCaptured;
- }
- addToCount = TRUE;
- }
-
- if(addToCount)
- {
- Int existingCount = 0;
- ObjectCountMapIt it = m_objectsCaptured.find(o->getTemplate());
- if (it != m_objectsCaptured.end())
- existingCount = it->second;
- m_objectsCaptured[o->getTemplate()] = existingCount + 1;
- }
- }
- void ScoreKeeper::addObjectDestroyed( const Object *o)
- {
- if (TheGameLogic->isScoringEnabled() == FALSE) {
- return;
- }
- Int playerIdx = o->getControllingPlayer()->getPlayerIndex();
- Bool addToCount = FALSE;
- if(o->getTemplate()->isKindOfMulti(scoringBuildingMask, KINDOFMASK_NONE))
- {
- if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
- ++m_totalBuildingsDestroyed[playerIdx];
- addToCount = TRUE;
- }
- }
- else if (o->getTemplate()->isKindOfMulti(scoringBuildingDestroyMask, KINDOFMASK_NONE))
- {
- if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
- ++m_totalBuildingsDestroyed[playerIdx];
- addToCount = TRUE;
- }
- }
- else if(o->getTemplate()->isKindOf(KINDOF_INFANTRY) || o->getTemplate()->isKindOf(KINDOF_VEHICLE))
- {
- if (o->getTemplate()->isKindOf(KINDOF_SCORE) || o->getTemplate()->isKindOf(KINDOF_SCORE_DESTROY))
- {
- if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
- m_totalUnitsDestroyed[playerIdx]++;
- addToCount = TRUE;
- }
- }
- }
-
- if(addToCount)
- {
- Int existingCount = 0;
- ObjectCountMapIt it = m_objectsDestroyed[playerIdx].find(o->getTemplate());
- if (it != m_objectsDestroyed[playerIdx].end())
- existingCount = it->second;
- m_objectsDestroyed[playerIdx][o->getTemplate()] = existingCount + 1;
- }
- }
- void ScoreKeeper::addObjectLost( const Object *o )
- {
- if (TheGameLogic->isScoringEnabled() == FALSE) {
- return;
- }
- Bool addToCount = FALSE;
- if(o->getTemplate()->isKindOfMulti(scoringBuildingMask, KINDOFMASK_NONE))
- {
- if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
- ++m_totalBuildingsLost;
- addToCount = TRUE;
- }
- }
- else if (o->getTemplate()->isKindOfMulti(scoringBuildingDestroyMask, KINDOFMASK_NONE))
- {
- if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
- ++m_totalBuildingsLost;
- addToCount = TRUE;
- }
- }
- else if(o->getTemplate()->isKindOf(KINDOF_INFANTRY) || o->getTemplate()->isKindOf(KINDOF_VEHICLE))
- {
- if (o->getTemplate()->isKindOf(KINDOF_SCORE) || o->getTemplate()->isKindOf(KINDOF_SCORE_DESTROY))
- {
- if (!(o->testStatus(OBJECT_STATUS_UNDER_CONSTRUCTION))) {
- ++m_totalUnitsLost;
- addToCount = TRUE;
- }
- }
- }
-
- if(addToCount)
- {
- Int existingCount = 0;
- ObjectCountMapIt it = m_objectsLost.find(o->getTemplate());
- if (it != m_objectsLost.end())
- existingCount = it->second;
- m_objectsLost[o->getTemplate()] = existingCount + 1;
- }
- }
- Int ScoreKeeper::calculateScore( void )
- {
- Int score = 0;
- score += m_totalUnitsBuilt * 100;
- score += m_totalMoneyEarned;
- score += m_totalBuildingsBuilt * 100;
- for (Int i = 0; i < MAX_PLAYER_COUNT; ++i)
- {
- if(i == m_myPlayerIdx)
- continue;
- score += m_totalUnitsDestroyed[i] * 100;
- score += m_totalBuildingsDestroyed[i] * 100;
- }
- m_currentScore = score;
- return m_currentScore;
- }
- //-----------------------------------------------------------------------------
- // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
- //-----------------------------------------------------------------------------
- Int ScoreKeeper::getTotalBuildingsDestroyed( void )
- {
- int count = 0;
- for (int i = 0; i< MAX_PLAYER_COUNT; ++i)
- {
- // Design change, display even if we killed our own
- // if(i == m_myPlayerIdx)
- // continue;
- count += m_totalBuildingsDestroyed[i];
- //for (ObjectCountMapIt it = m_objectsDestroyed[i].begin(); it != m_objectsDestroyed[i].end(); ++it)
- // {
- //
- // count += it->second;
- // }
-
- }
- return count;
- }
- Int ScoreKeeper::getTotalUnitsDestroyed( void )
- {
- int count = 0;
- for (int i = 0; i< MAX_PLAYER_COUNT; ++i)
- {
- // Design change, display even if we killed our own
- // if(i == m_myPlayerIdx)
- // continue;
- count += m_totalUnitsDestroyed[i];
- // for (ObjectCountMapIt it = m_objectsDestroyed[i].begin(); it != m_objectsDestroyed[i].end(); ++it)
- // {
- // }
- }
- return count;
- }
- // ------------------------------------------------------------------------------------------------
- /** CRC */
- // ------------------------------------------------------------------------------------------------
- void ScoreKeeper::crc( Xfer *xfer )
- {
- } // end ScoreKeeper
- // ------------------------------------------------------------------------------------------------
- /** Xfer of an object count map
- * Version Info:
- * 1: Initial version */
- // ------------------------------------------------------------------------------------------------
- void ScoreKeeper::xferObjectCountMap( Xfer *xfer, ObjectCountMap *map )
- {
- // sanity
- if( map == NULL )
- {
- DEBUG_CRASH(( "xferObjectCountMap - Invalid map parameter\n" ));
- throw SC_INVALID_DATA;
- } // end if
- // version info
- XferVersion currentVersion = 1;
- XferVersion version = currentVersion;
- xfer->xferVersion( &version, currentVersion );
- // size of the map
- UnsignedShort mapSize = map->size();
- xfer->xferUnsignedShort( &mapSize );
- // map data
- Int count;
- const ThingTemplate *thingTemplate;
- AsciiString thingTemplateName;
- if( xfer->getXferMode() == XFER_SAVE )
- {
- ObjectCountMapIt it;
-
- // save all entries
- for( it = map->begin(); it != map->end(); ++it )
- {
- // thing template
- thingTemplate = it->first;
- thingTemplateName = thingTemplate->getName();
- xfer->xferAsciiString( &thingTemplateName );
- // the count
- count = it->second;
- xfer->xferInt( &count );
- } // end for, it
- } // end if, save
- else
- {
- // read all entries
- for( UnsignedShort i = 0; i < mapSize; ++i )
- {
- // read thing template name
- xfer->xferAsciiString( &thingTemplateName );
- thingTemplate = TheThingFactory->findTemplate( thingTemplateName );
- if( thingTemplate == NULL )
- {
- DEBUG_CRASH(( "xferObjectCountMap - Unknown thing template '%s'\n", thingTemplateName.str() ));
- throw SC_INVALID_DATA;
- } // end if
- // read count
- xfer->xferInt( &count );
- // add to map
- (*map)[ thingTemplate ] = count;
- } // end for, i
- } // end else
- } // end xferObjectCountMap
- // ------------------------------------------------------------------------------------------------
- /** Xfer method
- * Version Info:
- * 1: Initial version */
- // ------------------------------------------------------------------------------------------------
- void ScoreKeeper::xfer( Xfer *xfer )
- {
- // version
- XferVersion currentVersion = 1;
- XferVersion version = currentVersion;
- xfer->xferVersion( &version, currentVersion );
- // money earned
- xfer->xferInt( &m_totalMoneyEarned );
- // money spent
- xfer->xferInt( &m_totalMoneySpent );
- // units destroyed
- xfer->xferUser( m_totalUnitsDestroyed, sizeof( Int ) * MAX_PLAYER_COUNT );
- // units built
- xfer->xferInt( &m_totalUnitsBuilt );
- // units lost
- xfer->xferInt( &m_totalUnitsLost );
- // buildings destroyed
- xfer->xferUser( m_totalBuildingsDestroyed, sizeof( Int ) * MAX_PLAYER_COUNT );
- // buildings built
- xfer->xferInt( &m_totalBuildingsBuilt );
- // buildings lost
- xfer->xferInt( &m_totalBuildingsLost );
- // tech buildings captured
- xfer->xferInt( &m_totalTechBuildingsCaptured );
- // faction buildings captured
- xfer->xferInt( &m_totalFactionBuildingsCaptured );
- // current score
- xfer->xferInt( &m_currentScore );
- // player index
- xfer->xferInt( &m_myPlayerIdx );
- // objects built
- xferObjectCountMap( xfer, &m_objectsBuilt );
- // objects destroyed
- UnsignedShort destroyedArraySize = MAX_PLAYER_COUNT;
- xfer->xferUnsignedShort( &destroyedArraySize );
- if( destroyedArraySize != MAX_PLAYER_COUNT )
- {
- DEBUG_CRASH(( "ScoreKeeper::xfer - size of objects destroyed array has changed\n" ));
- throw SC_INVALID_DATA;
- } // end if
- for( UnsignedShort i = 0; i < destroyedArraySize; ++i )
- {
- // xfer map data
- xferObjectCountMap( xfer, &m_objectsDestroyed[ i ] );
- } // end for i
- // objects lost
- xferObjectCountMap( xfer, &m_objectsLost );
- // objects captured
- xferObjectCountMap( xfer, &m_objectsCaptured );
- } // end xfer
- // ------------------------------------------------------------------------------------------------
- /** Load post process */
- // ------------------------------------------------------------------------------------------------
- void ScoreKeeper::loadPostProcess( void )
- {
- } // end loadPostProcess
|