BVB Source Codes

CRYENGINE Show GameRulesSystem.cpp Source code

Return Download CRYENGINE: download GameRulesSystem.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4.    -------------------------------------------------------------------------
  5.    $Id$
  6.    $DateTime$
  7.    Description:
  8.  
  9.    -------------------------------------------------------------------------
  10.    History:
  11.    - 15:9:2004   10:30 : Created by Mathieu Pinard
  12.  
  13. *************************************************************************/
  14. #include "StdAfx.h"
  15. #include "GameObjects/GameObjectSystem.h"
  16. #include "GameObjects/GameObject.h"
  17. #include "GameRulesSystem.h"
  18. #include "Network/GameServerNub.h"
  19.  
  20. #include <list>
  21.  
  22. #if CRY_PLATFORM_WINDOWS && CRY_PLATFORM_64BIT
  23.         #pragma warning ( disable : 4244 )
  24. #endif
  25.  
  26. #define GAMERULES_GLOBAL_VARIABLE   ("g_gameRules")
  27. #define GAMERULESID_GLOBAL_VARIABLE ("g_gameRulesId")
  28.  
  29. //------------------------------------------------------------------------
  30. CGameRulesSystem::CGameRulesSystem(ISystem* pSystem, IGameFramework* pGameFW)
  31.         : m_pGameFW(pGameFW),
  32.         m_pGameRules(0),
  33.         m_currentGameRules(0)
  34. {
  35. }
  36.  
  37. //------------------------------------------------------------------------
  38. CGameRulesSystem::~CGameRulesSystem()
  39. {
  40. }
  41.  
  42. //------------------------------------------------------------------------
  43. bool CGameRulesSystem::RegisterGameRules(const char* rulesName, const char* extensionName, bool bUseScript)
  44. {
  45.         IEntityClassRegistry::SEntityClassDesc ruleClass;
  46.  
  47.         char scriptName[1024];
  48.         if (bUseScript)
  49.         {
  50.                 cry_sprintf(scriptName, "Scripts/GameRules/%s.lua", rulesName);
  51.                 ruleClass.sScriptFile = scriptName;
  52.         }
  53.  
  54.         ruleClass.sName = rulesName;
  55.         ruleClass.pUserProxyCreateFunc = CreateGameObject;
  56.         ruleClass.pUserProxyData = this;
  57.         ruleClass.flags |= ECLF_INVISIBLE;
  58.  
  59.         gEnv->pEntitySystem->GetClassRegistry()->RegisterStdClass(ruleClass);
  60.  
  61.         std::pair<TGameRulesMap::iterator, bool> rit = m_GameRules.insert(TGameRulesMap::value_type(rulesName, SGameRulesDef()));
  62.         rit.first->second.extension = extensionName;
  63.  
  64.         // Automatically register scheduling profile
  65.         gEnv->pGameFramework->GetIGameObjectSystem()->RegisterSchedulingProfile(ruleClass.sName, "rule", nullptr);
  66.  
  67.         return true;
  68. }
  69.  
  70. //------------------------------------------------------------------------
  71. bool CGameRulesSystem::CreateGameRules(const char* rulesName)
  72. {
  73.         const char* name = GetGameRulesName(rulesName);
  74.         TGameRulesMap::iterator it = m_GameRules.find(name);
  75.         if (it == m_GameRules.end())
  76.                 return false;
  77.  
  78.         // If a rule is currently being used, ask the entity system to remove it
  79.         DestroyGameRules();
  80.  
  81.         SEntitySpawnParams params;
  82.  
  83.         params.sName = "GameRules";
  84.         params.pClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass(name);
  85.         params.nFlags |= ENTITY_FLAG_NO_PROXIMITY | ENTITY_FLAG_UNREMOVABLE;
  86.         params.id = 1;
  87.  
  88.         IEntity* pEntity = gEnv->pEntitySystem->SpawnEntity(params);
  89.         CRY_ASSERT(pEntity);
  90.  
  91.         if (pEntity == NULL)
  92.                 return false;
  93.  
  94.         pEntity->Activate(true);
  95.  
  96.         if (pEntity->GetScriptTable())
  97.         {
  98.                 IScriptSystem* pSS = gEnv->pScriptSystem;
  99.  
  100.                 pSS->SetGlobalValue(GAMERULES_GLOBAL_VARIABLE, pEntity->GetScriptTable());
  101.                 pSS->SetGlobalValue(GAMERULESID_GLOBAL_VARIABLE, ScriptHandle((UINT_PTR)m_currentGameRules));
  102.         }
  103.  
  104.         //since we re-instantiating game rules, let's get rid of everything related to previous match
  105.  
  106.         if (IGameplayRecorder* pGameplayRecorder = CCryAction::GetCryAction()->GetIGameplayRecorder())
  107.                 pGameplayRecorder->Event(pEntity, GameplayEvent(eGE_GameReset));
  108.  
  109.         if (gEnv->bServer)
  110.         {
  111.                 if (CGameServerNub* pGameServerNub = CCryAction::GetCryAction()->GetGameServerNub())
  112.                         pGameServerNub->ResetOnHoldChannels();
  113.         }
  114.  
  115.         return true;
  116. }
  117.  
  118. //------------------------------------------------------------------------
  119. bool CGameRulesSystem::DestroyGameRules()
  120. {
  121.         // If a rule is currently being used, ask the entity system to remove it
  122.         if (m_currentGameRules)
  123.         {
  124.                 IEntity* pEntity = gEnv->pEntitySystem->GetEntity(m_currentGameRules);
  125.                 if (pEntity)
  126.                         pEntity->ClearFlags(ENTITY_FLAG_UNREMOVABLE);
  127.  
  128.                 gEnv->pEntitySystem->RemoveEntity(m_currentGameRules, true);
  129.                 SetCurrentGameRules(0);
  130.  
  131.                 IScriptSystem* pSS = gEnv->pScriptSystem;
  132.  
  133.                 pSS->SetGlobalToNull(GAMERULES_GLOBAL_VARIABLE);
  134.                 pSS->SetGlobalToNull(GAMERULESID_GLOBAL_VARIABLE);
  135.         }
  136.  
  137.         return true;
  138. }
  139.  
  140. //------------------------------------------------------------------------
  141. bool CGameRulesSystem::HaveGameRules(const char* rulesName)
  142. {
  143.         const char* name = GetGameRulesName(rulesName);
  144.         if (!name || !gEnv->pEntitySystem->GetClassRegistry()->FindClass(name))
  145.                 return false;
  146.  
  147.         if (m_GameRules.find(name) == m_GameRules.end())
  148.                 return false;
  149.  
  150.         return true;
  151. }
  152.  
  153. //------------------------------------------------------------------------
  154. void CGameRulesSystem::AddGameRulesAlias(const char* gamerules, const char* alias)
  155. {
  156.         if (SGameRulesDef* def = GetGameRulesDef(gamerules))
  157.                 def->aliases.push_back(alias);
  158. }
  159.  
  160. //------------------------------------------------------------------------
  161. void CGameRulesSystem::AddGameRulesLevelLocation(const char* gamerules, const char* mapLocation)
  162. {
  163.         if (SGameRulesDef* def = GetGameRulesDef(gamerules))
  164.                 def->maplocs.push_back(mapLocation);
  165. }
  166.  
  167. //------------------------------------------------------------------------
  168. const char* CGameRulesSystem::GetGameRulesLevelLocation(const char* gamerules, int i)
  169. {
  170.         if (SGameRulesDef* def = GetGameRulesDef(GetGameRulesName(gamerules)))
  171.         {
  172.                 if (i >= 0 && i < def->maplocs.size())
  173.                         return def->maplocs[i].c_str();
  174.         }
  175.         return 0;
  176. }
  177.  
  178. //------------------------------------------------------------------------
  179. void CGameRulesSystem::SetCurrentGameRules(IGameRules* pGameRules)
  180. {
  181.         m_pGameRules = pGameRules;
  182.  
  183.         m_currentGameRules = m_pGameRules ? m_pGameRules->GetEntityId() : 0;
  184. }
  185.  
  186. //------------------------------------------------------------------------
  187. IGameRules* CGameRulesSystem::GetCurrentGameRules() const
  188. {
  189.         return m_pGameRules;
  190. }
  191.  
  192. //------------------------------------------------------------------------
  193. const char* CGameRulesSystem::GetGameRulesName(const char* alias) const
  194. {
  195.         for (TGameRulesMap::const_iterator it = m_GameRules.begin(); it != m_GameRules.end(); ++it)
  196.         {
  197.                 if (!stricmp(it->first.c_str(), alias))
  198.                         return it->first.c_str();
  199.  
  200.                 for (std::vector<string>::const_iterator ait = it->second.aliases.begin(); ait != it->second.aliases.end(); ++ait)
  201.                 {
  202.                         if (!stricmp(ait->c_str(), alias))
  203.                                 return it->first.c_str();
  204.                 }
  205.         }
  206.  
  207.         return 0;
  208. }
  209.  
  210. //------------------------------------------------------------------------
  211. /*
  212.    string& CGameRulesSystem::GetCurrentGameRules()
  213.    {
  214.    return
  215.    }
  216.  */
  217.  
  218. CGameRulesSystem::SGameRulesDef* CGameRulesSystem::GetGameRulesDef(const char* name)
  219. {
  220.         TGameRulesMap::iterator it = m_GameRules.find(name);
  221.         if (it == m_GameRules.end())
  222.                 return 0;
  223.  
  224.         return &it->second;
  225. }
  226.  
  227. //------------------------------------------------------------------------
  228. IEntityComponent* CGameRulesSystem::CreateGameObject(IEntity* pEntity, SEntitySpawnParams& params, void* pUserData)
  229. {
  230.         CGameRulesSystem* pThis = static_cast<CGameRulesSystem*>(pUserData);
  231.         CRY_ASSERT(pThis);
  232.         TGameRulesMap::iterator it = pThis->m_GameRules.find(params.pClass->GetName());
  233.         CRY_ASSERT(it != pThis->m_GameRules.end());
  234.  
  235.         auto pGameObject = pEntity->CreateComponentClass<CGameObject>();
  236.  
  237.         if (!it->second.extension.empty())
  238.         {
  239.                 if (!pGameObject->ActivateExtension(it->second.extension.c_str()))
  240.                 {
  241.                         pEntity->RemoveComponent(pGameObject);
  242.                         return nullptr;
  243.                 }
  244.         }
  245.  
  246.         return pGameObject;
  247. }
  248.  
  249. void CGameRulesSystem::GetMemoryStatistics(ICrySizer* s)
  250. {
  251.         s->Add(*this);
  252.         s->AddContainer(m_GameRules);
  253. }
  254.  
downloadGameRulesSystem.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top