1#include "race.h"
2
3#include <base/str.h>
4
5#include <game/client/gameclient.h>
6#include <game/collision.h>
7#include <game/mapitems.h>
8
9#include <vector>
10
11void CRaceHelper::Init(const CGameClient *pGameClient)
12{
13 m_pGameClient = pGameClient;
14
15 m_aFlagIndex[TEAM_RED] = -1;
16 m_aFlagIndex[TEAM_BLUE] = -1;
17
18 const CTile *pGameTiles = m_pGameClient->Collision()->GameLayer();
19 const int MapSize = m_pGameClient->Collision()->GetWidth() * m_pGameClient->Collision()->GetHeight();
20 for(int Index = 0; Index < MapSize; Index++)
21 {
22 const int EntityIndex = pGameTiles[Index].m_Index - ENTITY_OFFSET;
23 if(EntityIndex == ENTITY_FLAGSTAND_RED)
24 {
25 m_aFlagIndex[TEAM_RED] = Index;
26 if(m_aFlagIndex[TEAM_BLUE] != -1)
27 break; // Found both flags
28 }
29 else if(EntityIndex == ENTITY_FLAGSTAND_BLUE)
30 {
31 m_aFlagIndex[TEAM_BLUE] = Index;
32 if(m_aFlagIndex[TEAM_RED] != -1)
33 break; // Found both flags
34 }
35 Index += pGameTiles[Index].m_Skip;
36 }
37}
38
39int CRaceHelper::TimeFromSecondsStr(const char *pStr)
40{
41 while(*pStr == ' ') // skip leading spaces
42 pStr++;
43 if(!str_isnum(c: *pStr))
44 return -1;
45 int Time = str_toint(str: pStr) * 1000;
46 while(str_isnum(c: *pStr))
47 pStr++;
48 if(*pStr == '.' || *pStr == ',')
49 {
50 pStr++;
51 static const int s_aMult[3] = {100, 10, 1};
52 for(size_t i = 0; i < std::size(s_aMult) && str_isnum(c: pStr[i]); i++)
53 Time += (pStr[i] - '0') * s_aMult[i];
54 }
55 return Time;
56}
57
58int CRaceHelper::TimeFromStr(const char *pStr)
59{
60 static constexpr const char *MINUTES_STR = " minute(s) ";
61 static constexpr const char *SECONDS_STR = " second(s)";
62
63 const char *pSeconds = str_find(haystack: pStr, needle: SECONDS_STR);
64 if(!pSeconds)
65 return -1;
66
67 const char *pMinutes = str_find(haystack: pStr, needle: MINUTES_STR);
68 if(pMinutes)
69 {
70 while(*pStr == ' ') // skip leading spaces
71 pStr++;
72 int SecondsTime = TimeFromSecondsStr(pStr: pMinutes + str_length(str: MINUTES_STR));
73 if(SecondsTime == -1 || !str_isnum(c: *pStr))
74 return -1;
75 return str_toint(str: pStr) * 60 * 1000 + SecondsTime;
76 }
77 else
78 {
79 return TimeFromSecondsStr(pStr);
80 }
81}
82
83int CRaceHelper::TimeFromFinishMessage(const char *pStr, char *pNameBuf, int NameBufSize)
84{
85 static const char *const s_pFinishedStr = " finished in: ";
86 const char *pFinished = str_find(haystack: pStr, needle: s_pFinishedStr);
87 if(!pFinished)
88 return -1;
89
90 int FinishedPos = pFinished - pStr;
91 if(FinishedPos == 0 || FinishedPos >= NameBufSize)
92 return -1;
93
94 str_copy(dst: pNameBuf, src: pStr, dst_size: FinishedPos + 1);
95
96 return TimeFromStr(pStr: pFinished + str_length(str: s_pFinishedStr));
97}
98
99bool CRaceHelper::IsStart(vec2 Prev, vec2 Pos) const
100{
101 if(m_pGameClient->m_GameInfo.m_FlagStartsRace)
102 {
103 int EnemyTeam = m_pGameClient->m_aClients[m_pGameClient->m_Snap.m_LocalClientId].m_Team ^ 1;
104 return m_aFlagIndex[EnemyTeam] != -1 && distance(a: Pos, b: m_pGameClient->Collision()->GetPos(Index: m_aFlagIndex[EnemyTeam])) < 32;
105 }
106 else
107 {
108 std::vector<int> vIndices = m_pGameClient->Collision()->GetMapIndices(PrevPos: Prev, Pos);
109 if(!vIndices.empty())
110 {
111 for(const int Index : vIndices)
112 {
113 if(m_pGameClient->Collision()->GetTileIndex(Index) == TILE_START)
114 return true;
115 if(m_pGameClient->Collision()->GetFrontTileIndex(Index) == TILE_START)
116 return true;
117 }
118 }
119 else
120 {
121 const int Index = m_pGameClient->Collision()->GetPureMapIndex(Pos);
122 if(m_pGameClient->Collision()->GetTileIndex(Index) == TILE_START)
123 return true;
124 if(m_pGameClient->Collision()->GetFrontTileIndex(Index) == TILE_START)
125 return true;
126 }
127 }
128 return false;
129}
130