PageRenderTime 63ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/GameSDK/GameDll/GameRulesModules/GameRulesStandardScoring.cpp

https://gitlab.com/blackbird91/CS188_AI_Game
C++ | 391 lines | 311 code | 56 blank | 24 comment | 68 complexity | c28d669ba41afeb42a3085da4af4ef1b MD5 | raw file
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. // Original file Copyright Crytek GMBH or its affiliates, used under license.
  13. // Description : Game rules module to handle scoring points values
  14. #include "StdAfx.h"
  15. #include "GameRulesStandardScoring.h"
  16. #include "IXml.h"
  17. #include "GameRules.h"
  18. #include "IGameRulesPlayerStatsModule.h"
  19. #include "IGameRulesStateModule.h"
  20. #include "GameRulesTypes.h"
  21. #include "Utility/DesignerWarning.h"
  22. #include "Utility/CryDebugLog.h"
  23. #include "Player.h"
  24. #include "StatsRecordingMgr.h"
  25. AUTOENUM_BUILDNAMEARRAY(CGameRulesStandardScoring::s_gamerulesScoreType, EGRSTList);
  26. #define STANDARD_SCORING_STATE_ASPECT eEA_GameServerA
  27. //-------------------------------------------------------------------------
  28. CGameRulesStandardScoring::CGameRulesStandardScoring()
  29. {
  30. m_maxTeamScore = 0;
  31. m_startTeamScore = 0;
  32. m_useScoreAsTime = false;
  33. m_bAttackingTeamWonAllRounds = true;
  34. m_deathScoringModifier = 0;
  35. for(int i = 0; i < EGRST_Num; i++)
  36. {
  37. m_playerScorePoints[i] = 0;
  38. m_playerScoreXP[i] = 0;
  39. m_teamScorePoints[i] = 0;
  40. }
  41. }
  42. //-------------------------------------------------------------------------
  43. CGameRulesStandardScoring::~CGameRulesStandardScoring()
  44. {
  45. }
  46. //-------------------------------------------------------------------------
  47. void CGameRulesStandardScoring::Init( XmlNodeRef xml )
  48. {
  49. int numScoreCategories = xml->getChildCount();
  50. for (int i = 0; i < numScoreCategories; ++ i)
  51. {
  52. XmlNodeRef categoryXml = xml->getChild(i);
  53. const char *categoryTag = categoryXml->getTag();
  54. if (!stricmp(categoryTag, "Player"))
  55. {
  56. InitScoreData(categoryXml, &m_playerScorePoints[0], m_playerScoreXP);
  57. }
  58. else if (!stricmp(categoryTag, "Team"))
  59. {
  60. InitScoreData(categoryXml, &m_teamScorePoints[0], NULL);
  61. categoryXml->getAttr("maxScore", m_maxTeamScore);
  62. categoryXml->getAttr("startTeamScore", m_startTeamScore);
  63. categoryXml->getAttr("useScoreAsTime", m_useScoreAsTime);
  64. }
  65. }
  66. }
  67. bool CGameRulesStandardScoring::NetSerialize( TSerialize ser, EEntityAspects aspect, uint8 profile, int flags )
  68. {
  69. if (aspect == STANDARD_SCORING_STATE_ASPECT)
  70. {
  71. int deathScoringModifierWas=m_deathScoringModifier;
  72. ser.Value("deathScoreModifier", m_deathScoringModifier, 'ui10'); // max 1023
  73. }
  74. return true;
  75. }
  76. void CGameRulesStandardScoring::InitScoreData(XmlNodeRef categoryXml, TGameRulesScoreInt *scoringData, TGameRulesScoreInt *xpData)
  77. {
  78. CRY_ASSERT(scoringData);
  79. int numScoreValues = categoryXml->getChildCount();
  80. for (int j = 0; j < numScoreValues; ++ j)
  81. {
  82. XmlNodeRef childXml = categoryXml->getChild(j);
  83. const char *scoringTag = childXml->getTag();
  84. if (!stricmp(scoringTag, "Event"))
  85. {
  86. int points = 0;
  87. if (childXml->getAttr("points", points))
  88. {
  89. int type = EGRST_Unknown;
  90. const char* pChar = NULL;
  91. if (childXml->getAttr("type", &pChar))
  92. {
  93. bool typeOk = AutoEnum_GetEnumValFromString(pChar, s_gamerulesScoreType, EGRST_Num, &type);
  94. if (typeOk)
  95. {
  96. DesignerWarning( points < SGameRulesScoreInfo::SCORE_MAX && points > SGameRulesScoreInfo::SCORE_MIN, "Adding score for player which is out of net-serialize bounds (%d is not within [%d .. %d])", points, SGameRulesScoreInfo::SCORE_MIN, SGameRulesScoreInfo::SCORE_MAX );
  97. scoringData[type] = static_cast<TGameRulesScoreInt>(points);
  98. if (xpData)
  99. {
  100. int xp = points; // Default XP to be the same as points if not specified
  101. childXml->getAttr("xp", xp);
  102. xpData[type] = static_cast<TGameRulesScoreInt>(xp);
  103. }
  104. }
  105. else
  106. {
  107. CryLogAlways("GameRulesStandardScoring::Init() : Scoring Event type not recognised: %s.", pChar);
  108. }
  109. }
  110. else
  111. {
  112. CryLogAlways("GameRulesStandardScoring::Init() : Scoring Event has no type declared.");
  113. }
  114. }
  115. else
  116. {
  117. CryLogAlways("GameRulesStandardScoring::Init() : Scoring Event has no points declared.");
  118. }
  119. }
  120. }
  121. }
  122. //-------------------------------------------------------------------------
  123. TGameRulesScoreInt CGameRulesStandardScoring::GetPlayerPointsByType(EGRST pointsType) const
  124. {
  125. return GetPointsByType(&m_playerScorePoints[0], pointsType);
  126. }
  127. //-------------------------------------------------------------------------
  128. TGameRulesScoreInt CGameRulesStandardScoring::GetPlayerXPByType(EGRST pointsType) const
  129. {
  130. return GetPointsByType(m_playerScoreXP, pointsType);
  131. }
  132. //-------------------------------------------------------------------------
  133. TGameRulesScoreInt CGameRulesStandardScoring::GetTeamPointsByType(EGRST pointsType) const
  134. {
  135. return GetPointsByType(&m_teamScorePoints[0], pointsType);
  136. }
  137. //-------------------------------------------------------------------------
  138. TGameRulesScoreInt CGameRulesStandardScoring::GetPointsByType(const TGameRulesScoreInt *scoringData, EGRST pointsType) const
  139. {
  140. CRY_ASSERT_MESSAGE(pointsType > EGRST_Unknown && pointsType < EGRST_Num, "Out of range parameter passed into CGameRulesStandardScoring::GetPointsByType");
  141. const TGameRulesScoreInt &scoreData = scoringData[pointsType];
  142. if(scoreData == 0)
  143. {
  144. CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_COMMENT, "Scoring not setup for %s in gamemode %s", s_gamerulesScoreType[pointsType], gEnv->pConsole->GetCVar("sv_gamerules")->GetString());
  145. }
  146. return scoreData;
  147. }
  148. void CGameRulesStandardScoring::DoScoringForDeath(IActor *pTargetActor, EntityId shooterId, int damage, int material, int hit_type)
  149. {
  150. CGameRules *pGameRules = g_pGame->GetGameRules();
  151. IActor *pShooterActor = g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(shooterId);
  152. if (pGameRules != NULL && pTargetActor != NULL && pShooterActor != NULL)
  153. {
  154. if (pGameRules->HasGameActuallyStarted() == false)
  155. {
  156. return;
  157. }
  158. // No scoring at game end
  159. IGameRulesStateModule *pStateModule = pGameRules->GetStateModule();
  160. if (pStateModule != NULL && pStateModule->GetGameState() == IGameRulesStateModule::EGRS_PostGame)
  161. {
  162. return;
  163. }
  164. EntityId targetId = pTargetActor->GetEntityId();
  165. bool bTeamGame = (pGameRules->GetTeamCount() > 1);
  166. int targetTeam = pGameRules->GetTeam(targetId);
  167. int shooterTeam = pGameRules->GetTeam(shooterId);
  168. int playerPoints = 0, playerXP = 0;
  169. EGRST scoreType;
  170. bool useModifier=true;
  171. if (pTargetActor == pShooterActor)
  172. {
  173. scoreType = EGRST_Suicide;
  174. useModifier = false;
  175. }
  176. else if (bTeamGame && (targetTeam == shooterTeam))
  177. {
  178. scoreType = EGRST_PlayerTeamKill;
  179. }
  180. else
  181. {
  182. scoreType = EGRST_PlayerKill;
  183. }
  184. // map reason from one enum to other
  185. EXPReason reason=EXPReasonFromEGRST(scoreType);
  186. playerPoints = GetPlayerPointsByType(scoreType);
  187. playerXP = GetPlayerXPByType(scoreType);
  188. if (useModifier)
  189. {
  190. playerPoints += m_deathScoringModifier;
  191. playerXP += m_deathScoringModifier;
  192. }
  193. if(pShooterActor->IsPlayer())
  194. {
  195. playerXP = static_cast<CPlayer*>(pShooterActor)->GetXPBonusModifiedXP(playerXP);
  196. }
  197. SGameRulesScoreInfo scoreInfo((EGameRulesScoreType) scoreType, playerPoints, playerXP, reason);
  198. scoreInfo.AttachVictim(targetId);
  199. CryLog("About to call pGameRules->IncreasePoints, pGameRules=%p", pGameRules);
  200. INDENT_LOG_DURING_SCOPE();
  201. OnTeamScoringEvent(shooterTeam, scoreType);
  202. pGameRules->IncreasePoints(shooterId, scoreInfo);
  203. }
  204. }
  205. bool CGameRulesStandardScoring::ShouldScore(CGameRules *pGameRules) const
  206. {
  207. if (!pGameRules)
  208. return false;
  209. if (pGameRules->HasGameActuallyStarted() == false)
  210. {
  211. return false;
  212. }
  213. // No scoring at game end
  214. IGameRulesStateModule *pStateModule = pGameRules->GetStateModule();
  215. if (pStateModule != NULL && pStateModule->GetGameState() == IGameRulesStateModule::EGRS_PostGame)
  216. {
  217. return false;
  218. }
  219. return true;
  220. }
  221. void CGameRulesStandardScoring::OnPlayerScoringEvent( EntityId playerId, EGRST pointsType)
  222. {
  223. CRY_ASSERT(pointsType != EGRST_Unknown);
  224. CGameRules *pGameRules = g_pGame->GetGameRules();
  225. if(ShouldScore(pGameRules))
  226. {
  227. int playerPoints = GetPlayerPointsByType(pointsType);
  228. int playerXP = GetPlayerXPByType(pointsType);
  229. IActor* pActor = g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(playerId);
  230. if(pActor && pActor->IsPlayer())
  231. {
  232. playerXP = static_cast<CPlayer*>(pActor)->GetXPBonusModifiedXP(playerXP);
  233. }
  234. if(playerPoints != 0)
  235. {
  236. EXPReason reason=EXPReasonFromEGRST(pointsType);
  237. SGameRulesScoreInfo scoreInfo(pointsType, playerPoints, playerXP, reason);
  238. pGameRules->IncreasePoints(playerId, scoreInfo);
  239. }
  240. }
  241. }
  242. void CGameRulesStandardScoring::OnPlayerScoringEventWithInfo(EntityId playerId, SGameRulesScoreInfo* scoreInfo)
  243. {
  244. CRY_ASSERT(scoreInfo);
  245. CRY_ASSERT(scoreInfo->type != EGRST_Unknown);
  246. CGameRules *pGameRules = g_pGame->GetGameRules();
  247. if(ShouldScore(pGameRules))
  248. {
  249. if(scoreInfo->score != 0)
  250. {
  251. SGameRulesScoreInfo newScoreInfo(*scoreInfo);
  252. pGameRules->IncreasePoints(playerId, newScoreInfo);
  253. }
  254. }
  255. }
  256. void CGameRulesStandardScoring::OnPlayerScoringEventToAllTeamWithInfo(const int teamId, SGameRulesScoreInfo* scoreInfo)
  257. {
  258. CRY_ASSERT(gEnv->bServer);
  259. CGameRules::TPlayers teamPlayers;
  260. g_pGame->GetGameRules()->GetTeamPlayers(teamId, teamPlayers);
  261. CGameRules::TPlayers::const_iterator it = teamPlayers.begin();
  262. CGameRules::TPlayers::const_iterator end = teamPlayers.end();
  263. for (; it!=end; ++it)
  264. {
  265. CPlayer *loopPlr = static_cast< CPlayer* >( gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(*it) );
  266. OnPlayerScoringEventWithInfo(loopPlr->GetEntityId(), scoreInfo);
  267. }
  268. }
  269. void CGameRulesStandardScoring::OnTeamScoringEvent( int teamId, EGRST pointsType)
  270. {
  271. CRY_ASSERT(pointsType != EGRST_Unknown);
  272. CGameRules *pGameRules = g_pGame->GetGameRules();
  273. if(ShouldScore(pGameRules))
  274. {
  275. bool bTeamGame = (pGameRules->GetTeamCount() > 1);
  276. if (bTeamGame)
  277. {
  278. int teamPoints = GetTeamPointsByType(pointsType);
  279. if (teamPoints)
  280. {
  281. int teamScore = pGameRules->GetTeamsScore(teamId) + teamPoints;
  282. if (m_maxTeamScore)
  283. {
  284. if (m_useScoreAsTime)
  285. {
  286. float gameTime = pGameRules->GetCurrentGameTime();
  287. int maxActualScore = (int)floor(gameTime + m_maxTeamScore);
  288. if (teamScore > maxActualScore)
  289. {
  290. teamScore = maxActualScore;
  291. }
  292. }
  293. else
  294. {
  295. if (teamScore > m_maxTeamScore)
  296. {
  297. teamScore = m_maxTeamScore;
  298. }
  299. }
  300. }
  301. pGameRules->SetTeamsScore(teamId, teamScore);
  302. CStatsRecordingMgr* pRecordingMgr = g_pGame->GetStatsRecorder();
  303. if (pRecordingMgr)
  304. {
  305. pRecordingMgr->OnTeamScore(teamId, teamPoints, pointsType);
  306. }
  307. }
  308. }
  309. }
  310. }
  311. void CGameRulesStandardScoring::SvResetTeamScore(int teamId)
  312. {
  313. CRY_ASSERT(gEnv->bServer);
  314. CGameRules *pGameRules = g_pGame->GetGameRules();
  315. if(ShouldScore(pGameRules))
  316. {
  317. bool bTeamGame = (pGameRules->GetTeamCount() > 1);
  318. CRY_ASSERT_MESSAGE(bTeamGame, "we can't reset team score in a non-team game");
  319. if (bTeamGame)
  320. {
  321. pGameRules->SetTeamsScore(teamId, 0);
  322. }
  323. }
  324. }
  325. // IGameRulesScoringModule
  326. void CGameRulesStandardScoring::SvSetDeathScoringModifier(TGameRulesScoreInt inModifier)
  327. {
  328. CRY_ASSERT(gEnv->bServer);
  329. if (inModifier != m_deathScoringModifier)
  330. {
  331. m_deathScoringModifier = inModifier;
  332. CGameRules *pGameRules = g_pGame->GetGameRules();
  333. CHANGED_NETWORK_STATE(pGameRules, STANDARD_SCORING_STATE_ASPECT);
  334. }
  335. }