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.
This commit is contained in:
Robert Müller 2024-08-04 14:03:54 +02:00
parent 5ac3bb506c
commit 19105e224b
6 changed files with 54 additions and 32 deletions

View file

@ -216,7 +216,7 @@ void CGhost::CheckStartLocal(bool Predicted)
vec2 PrevPos = m_pClient->m_PredictedPrevChar.m_Pos; vec2 PrevPos = m_pClient->m_PredictedPrevChar.m_Pos;
vec2 Pos = m_pClient->m_PredictedChar.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 if(m_Rendering && !m_RenderingStartedByServer) // race restarted: stop rendering
StopRender(); StopRender();
@ -240,7 +240,7 @@ void CGhost::CheckStartLocal(bool Predicted)
int TickDiff = CurTick - PrevTick; int TickDiff = CurTick - PrevTick;
for(int i = 0; i < TickDiff; i++) 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; RecordTick = PrevTick + i + 1;
if(!m_AllowRestart) if(!m_AllowRestart)

View file

@ -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 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); 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) if(m_RaceState == RACE_STARTED)
Client()->RaceRecord_Stop(); Client()->RaceRecord_Stop();

View file

@ -470,21 +470,7 @@ void CGameClient::OnConnected()
m_Layers.Init(Kernel()); m_Layers.Init(Kernel());
m_Collision.Init(Layers()); m_Collision.Init(Layers());
m_GameWorld.m_Core.InitSwitchers(m_Collision.m_HighestSwitchNumber); m_GameWorld.m_Core.InitSwitchers(m_Collision.m_HighestSwitchNumber);
m_RaceHelper.Init(this);
CRaceHelper::ms_aFlagIndex[0] = -1;
CRaceHelper::ms_aFlagIndex[1] = -1;
CTile *pGameTiles = static_cast<CTile *>(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;
}
// render loading before going through all components // render loading before going through all components
m_Menus.RenderLoading(pConnectCaption, pLoadMapContent, 0, false); m_Menus.RenderLoading(pConnectCaption, pLoadMapContent, 0, false);

View file

@ -16,6 +16,7 @@
#include <game/teamscore.h> #include <game/teamscore.h>
#include <game/client/prediction/gameworld.h> #include <game/client/prediction/gameworld.h>
#include <game/client/race.h>
// components // components
#include "components/background.h" #include "components/background.h"
@ -182,6 +183,7 @@ private:
CLayers m_Layers; CLayers m_Layers;
CCollision m_Collision; CCollision m_Collision;
CUi m_UI; CUi m_UI;
CRaceHelper m_RaceHelper;
void ProcessEvents(); void ProcessEvents();
void UpdatePositions(); void UpdatePositions();
@ -244,6 +246,8 @@ public:
class CRenderTools *RenderTools() { return &m_RenderTools; } class CRenderTools *RenderTools() { return &m_RenderTools; }
class CLayers *Layers() { return &m_Layers; } class CLayers *Layers() { return &m_Layers; }
CCollision *Collision() { return &m_Collision; } 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 IEditor *Editor() { return m_pEditor; }
class IFriends *Friends() { return m_pFriends; } class IFriends *Friends() { return m_pFriends; }
class IFriends *Foes() { return m_pFoes; } class IFriends *Foes() { return m_pFoes; }

View file

@ -2,11 +2,38 @@
#include <vector> #include <vector>
#include <game/client/gameclient.h> #include <game/client/gameclient.h>
#include <game/collision.h>
#include <game/mapitems.h> #include <game/mapitems.h>
#include "race.h" #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) 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)); 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(m_pGameClient->m_GameInfo.m_FlagStartsRace)
if(pClient->m_GameInfo.m_FlagStartsRace)
{ {
int EnemyTeam = pClient->m_aClients[pClient->m_Snap.m_LocalClientId].m_Team ^ 1; int EnemyTeam = m_pGameClient->m_aClients[m_pGameClient->m_Snap.m_LocalClientId].m_Team ^ 1;
return ms_aFlagIndex[EnemyTeam] != -1 && distance(Pos, pCollision->GetPos(ms_aFlagIndex[EnemyTeam])) < 32; return m_aFlagIndex[EnemyTeam] != -1 && distance(Pos, m_pGameClient->Collision()->GetPos(m_aFlagIndex[EnemyTeam])) < 32;
} }
else else
{ {
std::vector<int> vIndices = pCollision->GetMapIndices(Prev, Pos); std::vector<int> vIndices = m_pGameClient->Collision()->GetMapIndices(Prev, Pos);
if(!vIndices.empty()) if(!vIndices.empty())
{ {
for(const int Index : vIndices) for(const int Index : vIndices)
{ {
if(pCollision->GetTileIndex(Index) == TILE_START) if(m_pGameClient->Collision()->GetTileIndex(Index) == TILE_START)
return true; return true;
if(pCollision->GetFTileIndex(Index) == TILE_START) if(m_pGameClient->Collision()->GetFTileIndex(Index) == TILE_START)
return true; return true;
} }
} }
else else
{ {
const int Index = pCollision->GetPureMapIndex(Pos); const int Index = m_pGameClient->Collision()->GetPureMapIndex(Pos);
if(pCollision->GetTileIndex(Index) == TILE_START) if(m_pGameClient->Collision()->GetTileIndex(Index) == TILE_START)
return true; return true;
if(pCollision->GetFTileIndex(Index) == TILE_START) if(m_pGameClient->Collision()->GetFTileIndex(Index) == TILE_START)
return true; return true;
} }
} }

View file

@ -3,17 +3,23 @@
#include <base/vmath.h> #include <base/vmath.h>
class CGameClient;
class CRaceHelper class CRaceHelper
{ {
const CGameClient *m_pGameClient;
int m_aFlagIndex[2] = {-1, -1};
public: public:
static int ms_aFlagIndex[2]; void Init(const CGameClient *pGameClient);
// these functions return the time in milliseconds, time -1 is invalid // these functions return the time in milliseconds, time -1 is invalid
static int TimeFromSecondsStr(const char *pStr); // x.xxx static int TimeFromSecondsStr(const char *pStr); // x.xxx
static int TimeFromStr(const char *pStr); // x minute(s) x.xxx second(s) 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 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 #endif // GAME_CLIENT_RACE_H