BVB Source Codes

CRYENGINE Show CET_GameRules.cpp Source code

Return Download CRYENGINE: download CET_GameRules.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4. #include "CET_GameRules.h"
  5. #include "IGameRulesSystem.h"
  6. #include "GameClientChannel.h"
  7. #include "GameServerChannel.h"
  8. #include <CryNetwork/NetHelpers.h>
  9. #include "GameContext.h"
  10. #include "ActionGame.h"
  11.  
  12. #if defined(GAME_CHANNEL_SYNC_CLIENT_SERVER_TIME)
  13.         #include "GameClientChannel.h"
  14.         #include "GameClientNub.h"
  15. #endif
  16. /*
  17.  * Create Game Rules
  18.  */
  19.  
  20. class CCET_CreateGameRules : public CCET_Base
  21. {
  22. public:
  23.         const char*                 GetName() { return "CreateGameRules"; }
  24.  
  25.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  26.         {
  27.                 if (CCryAction::GetCryAction()->GetGameContext()->GetRequestedGameRules().empty())
  28.                 {
  29.                         GameWarning("CreateGameRules: No game rules set");
  30.                         return eCETR_Failed;
  31.                 }
  32.                 return CCryAction::GetCryAction()->GetIGameRulesSystem()->CreateGameRules(CCryAction::GetCryAction()->GetGameContext()->GetRequestedGameRules()) ? eCETR_Ok : eCETR_Failed;
  33.         }
  34. };
  35.  
  36. void AddGameRulesCreation(IContextEstablisher* pEst, EContextViewState state)
  37. {
  38.         pEst->AddTask(state, new CCET_CreateGameRules);
  39. }
  40.  
  41. /*
  42.  * reset game rules
  43.  */
  44.  
  45. class CCET_GameRulesReset : public CCET_Base
  46. {
  47. public:
  48.         const char*                 GetName() { return "GameRulesReset"; }
  49.  
  50.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  51.         {
  52.                 SEntityEvent event(ENTITY_EVENT_RESET);
  53.                 event.nParam[0] = 1;
  54.  
  55.                 CCryAction::GetCryAction()->GetIGameRulesSystem()->GetCurrentGameRulesEntity()->SendEvent(event);
  56.                 return eCETR_Ok;
  57.         }
  58. };
  59.  
  60. void AddGameRulesReset(IContextEstablisher* pEst, EContextViewState state)
  61. {
  62.         pEst->AddTask(state, new CCET_GameRulesReset);
  63. }
  64.  
  65. /*
  66.  * Send Game Type
  67.  */
  68.  
  69. class CCET_SendGameType : public CCET_Base
  70. {
  71. public:
  72.         CCET_SendGameType(CClassRegistryReplicator* pRep, std::vector<SSendableHandle>* pWaitFor) : m_pRep(pRep), m_pWaitFor(pWaitFor) {}
  73.  
  74.         const char*                 GetName() { return "SendGameType"; }
  75.  
  76.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  77.         {
  78.                 // check our game rules have been registered
  79.                 if (CCryAction::GetCryAction()->GetGameContext()->GetRequestedGameRules().empty())
  80.                 {
  81.                         GameWarning("SendGameRules: No game rules set");
  82.                         return eCETR_Failed;
  83.                 }
  84.                 if (CCryAction::GetCryAction()->GetGameContext()->GetLevelName().empty())
  85.                 {
  86.                         GameWarning("SendGameRules: no level name set");
  87.                         return eCETR_Failed;
  88.                 }
  89.  
  90.                 uint16 id = ~uint16(0);
  91.  
  92.                 // game rules can be set via an alias, if we pass the alias into ClassIdFromName it will fail and disconnect the client, so we lookup the proper name here
  93.                 IGameRulesSystem* pGameRulesSystem = CCryAction::GetCryAction()->GetIGameRulesSystem();
  94.                 const char* gameRulesName = pGameRulesSystem->GetGameRulesName(CCryAction::GetCryAction()->GetGameContext()->GetRequestedGameRules());
  95.  
  96.                 if (!gameRulesName || !m_pRep->ClassIdFromName(id, string(gameRulesName)) || id == (uint16)(~uint16(0)))
  97.                 {
  98.                         GameWarning("Cannot find rules %s in network class registry", CCryAction::GetCryAction()->GetGameContext()->GetRequestedGameRules().c_str());
  99.                         return eCETR_Failed;
  100.                 }
  101.  
  102.                 // called on the server, we should send any information about the
  103.                 // game type that we need to
  104.  
  105.                 SSendableHandle* pWaitFor = 0;
  106.                 int nWaitFor = 0;
  107.                 if (m_pWaitFor && !m_pWaitFor->empty())
  108.                 {
  109.                         pWaitFor = &*m_pWaitFor->begin();
  110.                         nWaitFor = m_pWaitFor->size();
  111.                 }
  112.  
  113.                 state.pSender->AddSendable(
  114.                   new CSimpleNetMessage<SGameTypeParams>(SGameTypeParams(id, CCryAction::GetCryAction()->GetGameContext()->GetLevelName(), CCryAction::GetCryAction()->GetGameContext()->HasContextFlag(eGSF_ImmersiveMultiplayer)), CGameClientChannel::SetGameType),
  115.                   nWaitFor, pWaitFor, NULL);
  116.  
  117.                 return eCETR_Ok;
  118.         }
  119.  
  120. private:
  121.         CClassRegistryReplicator*     m_pRep;
  122.         std::vector<SSendableHandle>* m_pWaitFor;
  123. };
  124.  
  125. void AddSendGameType(IContextEstablisher* pEst, EContextViewState state, CClassRegistryReplicator* pRep, std::vector<SSendableHandle>* pWaitFor)
  126. {
  127.         pEst->AddTask(state, new CCET_SendGameType(pRep, pWaitFor));
  128. }
  129.  
  130. /*
  131.  * Send reset map
  132.  */
  133.  
  134. class CCET_SendResetMap : public CCET_Base
  135. {
  136. public:
  137.         const char*                 GetName() { return "SendResetMap"; }
  138.  
  139.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  140.         {
  141.                 CGameClientChannel::SendResetMapWith(SNoParams(), state.pSender);
  142.                 return eCETR_Ok;
  143.         }
  144. };
  145.  
  146. void AddSendResetMap(IContextEstablisher* pEst, EContextViewState state)
  147. {
  148.         pEst->AddTask(state, new CCET_SendResetMap());
  149. }
  150.  
  151. /*
  152.  * Init game immersiveness parameters
  153.  */
  154.  
  155. class CCET_InitImmersiveness : public CCET_Base
  156. {
  157. public:
  158.         const char*                 GetName() { return "InitImmersiveness"; }
  159.  
  160.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  161.         {
  162.                 CActionGame::Get()->InitImmersiveness();
  163.                 return eCETR_Ok;
  164.         }
  165. };
  166.  
  167. void AddInitImmersiveness(IContextEstablisher* pEst, EContextViewState state)
  168. {
  169.         pEst->AddTask(state, new CCET_InitImmersiveness());
  170. }
  171.  
  172. /*
  173.  * On client connect
  174.  */
  175.  
  176. class CCET_OnClient : public CCET_Base
  177. {
  178. public:
  179.         CCET_OnClient(bool(IGameRules::* func)(int, bool), const char* name, bool isReset) : m_func(func), m_name(name), m_isReset(isReset) {}
  180.  
  181.         const char*                 GetName() { return m_name; }
  182.  
  183.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  184.         {
  185.                 int channelId = 0;
  186.                 if (INetChannel* pNC = state.pSender)
  187.                         if (CGameServerChannel* pSC = (CGameServerChannel*) pNC->GetGameChannel())
  188.                                 channelId = pSC->GetChannelId();
  189.                 if (!channelId)
  190.                 {
  191.                         GameWarning("OnClient: No channel id");
  192.                         return eCETR_Failed;
  193.                 }
  194.                 IGameRules* pGameRules = CCryAction::GetCryAction()->GetIGameRulesSystem()->GetCurrentGameRules();
  195.                 if (!pGameRules)
  196.                 {
  197.                         CryLog("[GameRules]No game rules");
  198.                         return eCETR_Ok; // Editor without gamedll can have no gamerules.
  199.                 }
  200.                 //CryLog( "Spawn player for channel %d%s", channelId, m_isReset? " as reset" : "" );
  201.                 if (!(pGameRules->*m_func)(channelId, m_isReset))
  202.                         return eCETR_Failed;
  203.                 else
  204.                         return eCETR_Ok;
  205.         }
  206.  
  207. private:
  208.         bool (IGameRules::* m_func)(int, bool);
  209.         const char* m_name;
  210.         bool        m_isReset;
  211. };
  212.  
  213. void AddOnClientConnect(IContextEstablisher* pEst, EContextViewState state, bool isReset)
  214. {
  215.         if (!(gEnv->bHostMigrating && gEnv->bMultiplayer))
  216.         {
  217.                 pEst->AddTask(state, new CCET_OnClient(&IGameRules::OnClientConnect, "OnClientConnect", isReset));
  218.         }
  219. }
  220.  
  221. void AddOnClientEnteredGame(IContextEstablisher* pEst, EContextViewState state, bool isReset)
  222. {
  223.         if (!(gEnv->bHostMigrating && gEnv->bMultiplayer))
  224.         {
  225.                 pEst->AddTask(state, new CCET_OnClient(&IGameRules::OnClientEnteredGame, "OnClientEnteredGame", isReset));
  226.         }
  227. }
  228.  
  229. class CCET_ClearOnHold : public CCET_Base
  230. {
  231. public:
  232.         CCET_ClearOnHold() : m_pServerChan(nullptr) {}
  233.  
  234.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  235.         {
  236.                 if (INetChannel* pNC = state.pSender)
  237.                         if (CGameServerChannel* pSC = (CGameServerChannel*) pNC->GetGameChannel())
  238.                                 pSC->SetOnHold(false);
  239.                 return eCETR_Ok;
  240.         }
  241.  
  242.         const char* GetName() { return "ClearOnHold"; };
  243.  
  244. private:
  245.         CGameServerChannel* m_pServerChan;
  246. };
  247.  
  248. void AddClearOnHold(IContextEstablisher* pEst, EContextViewState state)
  249. {
  250.         pEst->AddTask(state, new CCET_ClearOnHold());
  251. }
  252.  
  253. /*
  254.  * pause/unpause game
  255.  */
  256.  
  257. class CCET_PauseGame : public CCET_Base
  258. {
  259. public:
  260.         CCET_PauseGame(bool pause, bool forcePause) : m_pause(pause), m_forcePause(forcePause) {}
  261.  
  262.         const char*                 GetName() { return m_pause ? "PauseGame" : "ResumeGame"; }
  263.  
  264.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  265.         {
  266.                 CCryAction::GetCryAction()->PauseGame(m_pause, m_forcePause);
  267.                 return eCETR_Ok;
  268.         }
  269.  
  270. private:
  271.         bool m_pause;
  272.         bool m_forcePause;
  273. };
  274.  
  275. void AddPauseGame(IContextEstablisher* pEst, EContextViewState state, bool pause, bool forcePause)
  276. {
  277.         pEst->AddTask(state, new CCET_PauseGame(pause, forcePause));
  278. }
  279.  
  280. /*
  281.  * wait for precaching to finish before starting the game
  282.  */
  283.  
  284. class CCET_WaitForPreCachingToFinish : public CCET_Base
  285. {
  286. public:
  287.         CCET_WaitForPreCachingToFinish(bool* pGameStart) : m_pGameStart(pGameStart) {}
  288.  
  289.         const char*                 GetName() { return "WaitForPrecachingToFinish"; }
  290.  
  291.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  292.         {
  293.                 if(gEnv->IsEditor() || (gEnv->pSystem->GetSystemGlobalState() >= ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_END) || gEnv->bMultiplayer)
  294.                 {
  295.                         *m_pGameStart = true;
  296.                         return eCETR_Ok;
  297.                 }
  298.  
  299.                 return eCETR_Wait;
  300.         }
  301.  
  302. private:
  303.         bool* m_pGameStart;
  304. };
  305.  
  306. void AddWaitForPrecachingToFinish(IContextEstablisher* pEst, EContextViewState state, bool* pGameStart)
  307. {
  308.         pEst->AddTask(state, new CCET_WaitForPreCachingToFinish(pGameStart));
  309. }
  310.  
  311. /*
  312.  * save game for reset
  313.  */
  314.  
  315. class CCET_InitialSaveGame : public CCET_Base
  316. {
  317. public:
  318.         CCET_InitialSaveGame() {}
  319.  
  320.         const char*                 GetName() { return "InitialSaveGame"; }
  321.  
  322.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  323.         {
  324.                 bool loadingSaveGame = CCryAction::GetCryAction()->IsLoadingSaveGame();
  325.                 if (!loadingSaveGame && !GetISystem()->IsSerializingFile())
  326.                 {
  327.                         CCryAction::GetCryAction()->SaveGame(CCryAction::GetCryAction()->GetStartLevelSaveGameName(), true, true, eSGR_LevelStart);
  328.                 }
  329.  
  330.                 return eCETR_Ok;
  331.         }
  332. };
  333.  
  334. void AddInitialSaveGame(IContextEstablisher* pEst, EContextViewState state)
  335. {
  336.         pEst->AddTask(state, new CCET_InitialSaveGame());
  337. }
  338.  
  339. #if defined(GAME_CHANNEL_SYNC_CLIENT_SERVER_TIME)
  340.  
  341. /*
  342.  * Attempt to sync the clocks
  343.  */
  344.  
  345. class CCET_SyncClientToServerClock : public CCET_Base
  346. {
  347. public:
  348.         CCET_SyncClientToServerClock() {}
  349.  
  350.         const char*                 GetName() { return "SyncClientToServerClock"; }
  351.  
  352.         EContextEstablishTaskResult OnStep(SContextEstablishState& state)
  353.         {
  354.                 CCryAction* pFramework = CCryAction::GetCryAction();
  355.                 if (CGameClientNub* pClientNub = pFramework->GetGameClientNub())
  356.                 {
  357.                         CGameClientChannel* pChannel = pClientNub->GetGameClientChannel();
  358.                         if (pChannel)
  359.                         {
  360.                                 switch (pChannel->GetClock().GetSyncState())
  361.                                 {
  362.                                 case CClientClock::eSS_NotDone:
  363.                                         {
  364.                                                 pChannel->GetClock().StartSync();
  365.                                         }
  366.                                         break;
  367.                                 case CClientClock::eSS_Done:
  368.                                         {
  369.                                                 return eCETR_Ok;
  370.                                         }
  371.                                         break;
  372.                                 }
  373.                         }
  374.                 }
  375.                 return eCETR_Wait;
  376.         }
  377. };
  378.  
  379. void AddClientTimeSync(IContextEstablisher* pEst, EContextViewState state)
  380. {
  381.         pEst->AddTask(state, new CCET_SyncClientToServerClock());
  382. }
  383.  
  384. #else
  385.  
  386. void AddClientTimeSync(IContextEstablisher* pEst, EContextViewState state)
  387. {
  388. }
  389.  
  390. #endif
  391.  
downloadCET_GameRules.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