From bfea8023eb9b432165f810a6eb870cafb88043e2 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:07:35 +0200 Subject: [PATCH] Reduce duplicate code Make a method for setting the host state. --- r5dev/engine/host_state.cpp | 46 +++++++++++++++++++------------------ r5dev/engine/host_state.h | 3 ++- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/r5dev/engine/host_state.cpp b/r5dev/engine/host_state.cpp index bd343431..6c7c86a9 100644 --- a/r5dev/engine/host_state.cpp +++ b/r5dev/engine/host_state.cpp @@ -390,6 +390,24 @@ void CHostState::LoadConfig(void) const } } +//----------------------------------------------------------------------------- +// Purpose: set state machine +// Input : newState - +// clearNext - +//----------------------------------------------------------------------------- +void CHostState::SetState(const HostStates_t newState) +{ + m_iCurrentState = newState; + + // If our next state isn't a shutdown, or its a forced shutdown then set + // next state to run. + if (m_iNextState != HostStates_t::HS_SHUTDOWN || + !host_hasIrreversibleShutdown->GetBool()) + { + m_iNextState = newState; + } +} + //----------------------------------------------------------------------------- // Purpose: shutdown active game //----------------------------------------------------------------------------- @@ -415,7 +433,7 @@ void CHostState::State_NewGame(void) LARGE_INTEGER time{}; #ifndef CLIENT_DLL - bool bSplitScreenConnect = m_bSplitScreenConnect; + const bool bSplitScreenConnect = m_bSplitScreenConnect; m_bSplitScreenConnect = 0; if (!g_pServerGameClients) // Init Game if it ain't valid. @@ -436,13 +454,7 @@ void CHostState::State_NewGame(void) } #endif // !CLIENT_DLL - m_iCurrentState = HostStates_t::HS_RUN; // Set current state to run. - - // If our next state isn't a shutdown or its a forced shutdown then set next state to run. - if (m_iNextState != HostStates_t::HS_SHUTDOWN || !host_hasIrreversibleShutdown->GetBool()) - { - m_iNextState = HostStates_t::HS_RUN; - } + SetState(HostStates_t::HS_RUN); } //----------------------------------------------------------------------------- @@ -462,13 +474,8 @@ void CHostState::State_ChangeLevelSP(void) Error(eDLL_T::ENGINE, NO_ERROR, "%s: Unable to find level: '%s'\n", __FUNCTION__, m_levelName); } - m_iCurrentState = HostStates_t::HS_RUN; // Set current state to run. - - // If our next state isn't a shutdown or its a forced shutdown then set next state to run. - if (m_iNextState != HostStates_t::HS_SHUTDOWN || !host_hasIrreversibleShutdown->GetBool()) - { - m_iNextState = HostStates_t::HS_RUN; - } + // Set current state to run. + SetState(HostStates_t::HS_RUN); } //----------------------------------------------------------------------------- @@ -494,13 +501,8 @@ void CHostState::State_ChangeLevelMP(void) Error(eDLL_T::ENGINE, NO_ERROR, "%s: Unable to find level: '%s'\n", __FUNCTION__, m_levelName); } - m_iCurrentState = HostStates_t::HS_RUN; // Set current state to run. - - // If our next state isn't a shutdown or its a forced shutdown then set next state to run. - if (m_iNextState != HostStates_t::HS_SHUTDOWN || !host_hasIrreversibleShutdown->GetBool()) - { - m_iNextState = HostStates_t::HS_RUN; - } + // Set current state to run. + SetState(HostStates_t::HS_RUN); } //----------------------------------------------------------------------------- diff --git a/r5dev/engine/host_state.h b/r5dev/engine/host_state.h index 01089ec0..398ed62f 100644 --- a/r5dev/engine/host_state.h +++ b/r5dev/engine/host_state.h @@ -23,9 +23,10 @@ public: void Setup(void); void Think(void) const; + void SetState(const HostStates_t newState); void GameShutDown(void); - void State_NewGame(void); + void State_NewGame(void); void State_ChangeLevelSP(void); void State_ChangeLevelMP(void);