From 19105e224bbda3f139f320dc15f724fef77e1017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 4 Aug 2024 14:03:54 +0200 Subject: [PATCH] Make `CRaceHelper` a member of `CGameClient` Avoid static functions and global static variables for flag indices. Avoid passing pointer to `CGameClient` separately to `IsStart` function. --- src/game/client/components/ghost.cpp | 4 +- src/game/client/components/race_demo.cpp | 2 +- src/game/client/gameclient.cpp | 16 +------- src/game/client/gameclient.h | 4 ++ src/game/client/race.cpp | 50 ++++++++++++++++++------ src/game/client/race.h | 10 ++++- 6 files changed, 54 insertions(+), 32 deletions(-) diff --git a/src/game/client/components/ghost.cpp b/src/game/client/components/ghost.cpp index 38cfec851..61e73e9eb 100644 --- a/src/game/client/components/ghost.cpp +++ b/src/game/client/components/ghost.cpp @@ -216,7 +216,7 @@ void CGhost::CheckStartLocal(bool Predicted) vec2 PrevPos = m_pClient->m_PredictedPrevChar.m_Pos; vec2 Pos = m_pClient->m_PredictedChar.m_Pos; - if(((!m_Rendering && RenderTick == -1) || m_AllowRestart) && CRaceHelper::IsStart(m_pClient, PrevPos, Pos)) + if(((!m_Rendering && RenderTick == -1) || m_AllowRestart) && GameClient()->RaceHelper()->IsStart(PrevPos, Pos)) { if(m_Rendering && !m_RenderingStartedByServer) // race restarted: stop rendering StopRender(); @@ -240,7 +240,7 @@ void CGhost::CheckStartLocal(bool Predicted) int TickDiff = CurTick - PrevTick; for(int i = 0; i < TickDiff; i++) { - if(CRaceHelper::IsStart(m_pClient, mix(PrevPos, Pos, (float)i / TickDiff), mix(PrevPos, Pos, (float)(i + 1) / TickDiff))) + if(GameClient()->RaceHelper()->IsStart(mix(PrevPos, Pos, (float)i / TickDiff), mix(PrevPos, Pos, (float)(i + 1) / TickDiff))) { RecordTick = PrevTick + i + 1; if(!m_AllowRestart) diff --git a/src/game/client/components/race_demo.cpp b/src/game/client/components/race_demo.cpp index 3caef0108..d1de5ef46 100644 --- a/src/game/client/components/race_demo.cpp +++ b/src/game/client/components/race_demo.cpp @@ -79,7 +79,7 @@ void CRaceDemo::OnNewSnapshot() vec2 PrevPos = vec2(m_pClient->m_Snap.m_pLocalPrevCharacter->m_X, m_pClient->m_Snap.m_pLocalPrevCharacter->m_Y); vec2 Pos = vec2(m_pClient->m_Snap.m_pLocalCharacter->m_X, m_pClient->m_Snap.m_pLocalCharacter->m_Y); - if(ForceStart || (!ServerControl && CRaceHelper::IsStart(m_pClient, PrevPos, Pos))) + if(ForceStart || (!ServerControl && GameClient()->RaceHelper()->IsStart(PrevPos, Pos))) { if(m_RaceState == RACE_STARTED) Client()->RaceRecord_Stop(); diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index fdd6b5161..c625a3a33 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -470,21 +470,7 @@ void CGameClient::OnConnected() m_Layers.Init(Kernel()); m_Collision.Init(Layers()); m_GameWorld.m_Core.InitSwitchers(m_Collision.m_HighestSwitchNumber); - - CRaceHelper::ms_aFlagIndex[0] = -1; - CRaceHelper::ms_aFlagIndex[1] = -1; - - CTile *pGameTiles = static_cast(Layers()->Map()->GetData(Layers()->GameLayer()->m_Data)); - - // get flag positions - for(int i = 0; i < m_Collision.GetWidth() * m_Collision.GetHeight(); i++) - { - if(pGameTiles[i].m_Index - ENTITY_OFFSET == ENTITY_FLAGSTAND_RED) - CRaceHelper::ms_aFlagIndex[TEAM_RED] = i; - else if(pGameTiles[i].m_Index - ENTITY_OFFSET == ENTITY_FLAGSTAND_BLUE) - CRaceHelper::ms_aFlagIndex[TEAM_BLUE] = i; - i += pGameTiles[i].m_Skip; - } + m_RaceHelper.Init(this); // render loading before going through all components m_Menus.RenderLoading(pConnectCaption, pLoadMapContent, 0, false); diff --git a/src/game/client/gameclient.h b/src/game/client/gameclient.h index 0d4eae079..0aac36a48 100644 --- a/src/game/client/gameclient.h +++ b/src/game/client/gameclient.h @@ -16,6 +16,7 @@ #include #include +#include // components #include "components/background.h" @@ -182,6 +183,7 @@ private: CLayers m_Layers; CCollision m_Collision; CUi m_UI; + CRaceHelper m_RaceHelper; void ProcessEvents(); void UpdatePositions(); @@ -244,6 +246,8 @@ public: class CRenderTools *RenderTools() { return &m_RenderTools; } class CLayers *Layers() { return &m_Layers; } CCollision *Collision() { return &m_Collision; } + const CCollision *Collision() const { return &m_Collision; } + const CRaceHelper *RaceHelper() const { return &m_RaceHelper; } class IEditor *Editor() { return m_pEditor; } class IFriends *Friends() { return m_pFriends; } class IFriends *Foes() { return m_pFoes; } diff --git a/src/game/client/race.cpp b/src/game/client/race.cpp index e9593226c..2b5c5f4ef 100644 --- a/src/game/client/race.cpp +++ b/src/game/client/race.cpp @@ -2,11 +2,38 @@ #include #include +#include #include #include "race.h" -int CRaceHelper::ms_aFlagIndex[2] = {-1, -1}; +void CRaceHelper::Init(const CGameClient *pGameClient) +{ + m_pGameClient = pGameClient; + + m_aFlagIndex[TEAM_RED] = -1; + m_aFlagIndex[TEAM_BLUE] = -1; + + const CTile *pGameTiles = m_pGameClient->Collision()->GameLayer(); + const int MapSize = m_pGameClient->Collision()->GetWidth() * m_pGameClient->Collision()->GetHeight(); + for(int Index = 0; Index < MapSize; Index++) + { + const int EntityIndex = pGameTiles[Index].m_Index - ENTITY_OFFSET; + if(EntityIndex == ENTITY_FLAGSTAND_RED) + { + m_aFlagIndex[TEAM_RED] = Index; + if(m_aFlagIndex[TEAM_BLUE] != -1) + break; // Found both flags + } + else if(EntityIndex == ENTITY_FLAGSTAND_BLUE) + { + m_aFlagIndex[TEAM_BLUE] = Index; + if(m_aFlagIndex[TEAM_RED] != -1) + break; // Found both flags + } + Index += pGameTiles[Index].m_Skip; + } +} int CRaceHelper::TimeFromSecondsStr(const char *pStr) { @@ -66,33 +93,32 @@ int CRaceHelper::TimeFromFinishMessage(const char *pStr, char *pNameBuf, int Nam return TimeFromStr(pFinished + str_length(s_pFinishedStr)); } -bool CRaceHelper::IsStart(CGameClient *pClient, vec2 Prev, vec2 Pos) +bool CRaceHelper::IsStart(vec2 Prev, vec2 Pos) const { - CCollision *pCollision = pClient->Collision(); - if(pClient->m_GameInfo.m_FlagStartsRace) + if(m_pGameClient->m_GameInfo.m_FlagStartsRace) { - int EnemyTeam = pClient->m_aClients[pClient->m_Snap.m_LocalClientId].m_Team ^ 1; - return ms_aFlagIndex[EnemyTeam] != -1 && distance(Pos, pCollision->GetPos(ms_aFlagIndex[EnemyTeam])) < 32; + int EnemyTeam = m_pGameClient->m_aClients[m_pGameClient->m_Snap.m_LocalClientId].m_Team ^ 1; + return m_aFlagIndex[EnemyTeam] != -1 && distance(Pos, m_pGameClient->Collision()->GetPos(m_aFlagIndex[EnemyTeam])) < 32; } else { - std::vector vIndices = pCollision->GetMapIndices(Prev, Pos); + std::vector vIndices = m_pGameClient->Collision()->GetMapIndices(Prev, Pos); if(!vIndices.empty()) { for(const int Index : vIndices) { - if(pCollision->GetTileIndex(Index) == TILE_START) + if(m_pGameClient->Collision()->GetTileIndex(Index) == TILE_START) return true; - if(pCollision->GetFTileIndex(Index) == TILE_START) + if(m_pGameClient->Collision()->GetFTileIndex(Index) == TILE_START) return true; } } else { - const int Index = pCollision->GetPureMapIndex(Pos); - if(pCollision->GetTileIndex(Index) == TILE_START) + const int Index = m_pGameClient->Collision()->GetPureMapIndex(Pos); + if(m_pGameClient->Collision()->GetTileIndex(Index) == TILE_START) return true; - if(pCollision->GetFTileIndex(Index) == TILE_START) + if(m_pGameClient->Collision()->GetFTileIndex(Index) == TILE_START) return true; } } diff --git a/src/game/client/race.h b/src/game/client/race.h index ae82461b4..b9b67ec64 100644 --- a/src/game/client/race.h +++ b/src/game/client/race.h @@ -3,17 +3,23 @@ #include +class CGameClient; + class CRaceHelper { + const CGameClient *m_pGameClient; + + int m_aFlagIndex[2] = {-1, -1}; + public: - static int ms_aFlagIndex[2]; + void Init(const CGameClient *pGameClient); // these functions return the time in milliseconds, time -1 is invalid static int TimeFromSecondsStr(const char *pStr); // x.xxx static int TimeFromStr(const char *pStr); // x minute(s) x.xxx second(s) static int TimeFromFinishMessage(const char *pStr, char *pNameBuf, int NameBufSize); // xxx finished in: x minute(s) x.xxx second(s) - static bool IsStart(class CGameClient *pClient, vec2 Prev, vec2 Pos); + bool IsStart(vec2 Prev, vec2 Pos) const; }; #endif // GAME_CLIENT_RACE_H