2017-08-30 22:43:46 +00:00
|
|
|
#include <ctype.h>
|
2017-09-09 18:28:38 +00:00
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#include <base/math.h>
|
2017-09-09 18:54:49 +00:00
|
|
|
#include <engine/serverbrowser.h>
|
|
|
|
#include <game/client/gameclient.h>
|
2017-09-09 18:28:38 +00:00
|
|
|
#include <game/mapitems.h>
|
2017-08-30 22:43:46 +00:00
|
|
|
|
2017-08-30 22:08:54 +00:00
|
|
|
#include "race.h"
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
int CRaceHelper::ms_aFlagIndex[2] = {-1, -1};
|
2017-09-09 18:54:49 +00:00
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
int CRaceHelper::TimeFromSecondsStr(const char *pStr)
|
2017-08-30 22:08:54 +00:00
|
|
|
{
|
2017-08-30 22:43:46 +00:00
|
|
|
while(*pStr == ' ') // skip leading spaces
|
|
|
|
pStr++;
|
|
|
|
if(!isdigit(*pStr))
|
|
|
|
return -1;
|
2017-08-30 22:08:54 +00:00
|
|
|
int Time = str_toint(pStr) * 1000;
|
2017-08-30 22:43:46 +00:00
|
|
|
while(isdigit(*pStr))
|
2017-08-30 22:08:54 +00:00
|
|
|
pStr++;
|
|
|
|
if(*pStr == '.' || *pStr == ',')
|
|
|
|
{
|
|
|
|
pStr++;
|
2020-09-26 19:41:58 +00:00
|
|
|
static const int s_aMult[3] = {100, 10, 1};
|
2017-08-30 22:43:46 +00:00
|
|
|
for(int i = 0; isdigit(pStr[i]) && i < 3; i++)
|
2017-08-30 22:08:54 +00:00
|
|
|
Time += (pStr[i] - '0') * s_aMult[i];
|
|
|
|
}
|
|
|
|
return Time;
|
|
|
|
}
|
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
int CRaceHelper::TimeFromStr(const char *pStr)
|
2017-08-30 22:08:54 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
static const char *const s_pMinutesStr = " minute(s) ";
|
|
|
|
static const char *const s_pSecondsStr = " second(s)";
|
2017-08-30 22:08:54 +00:00
|
|
|
|
|
|
|
const char *pSeconds = str_find(pStr, s_pSecondsStr);
|
|
|
|
if(!pSeconds)
|
2017-08-30 22:43:46 +00:00
|
|
|
return -1;
|
2017-08-30 22:08:54 +00:00
|
|
|
|
|
|
|
const char *pMinutes = str_find(pStr, s_pMinutesStr);
|
|
|
|
if(pMinutes)
|
2017-08-30 22:43:46 +00:00
|
|
|
{
|
|
|
|
while(*pStr == ' ') // skip leading spaces
|
|
|
|
pStr++;
|
|
|
|
int SecondsTime = TimeFromSecondsStr(pMinutes + str_length(s_pMinutesStr));
|
|
|
|
if(SecondsTime == -1 || !isdigit(*pStr))
|
|
|
|
return -1;
|
|
|
|
return str_toint(pStr) * 60 * 1000 + SecondsTime;
|
|
|
|
}
|
2017-08-30 22:08:54 +00:00
|
|
|
else
|
|
|
|
return TimeFromSecondsStr(pStr);
|
|
|
|
}
|
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
int CRaceHelper::TimeFromFinishMessage(const char *pStr, char *pNameBuf, int NameBufSize)
|
2017-08-30 22:08:54 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
static const char *const s_pFinishedStr = " finished in: ";
|
2017-08-30 22:08:54 +00:00
|
|
|
const char *pFinished = str_find(pStr, s_pFinishedStr);
|
|
|
|
if(!pFinished)
|
2017-08-30 22:43:46 +00:00
|
|
|
return -1;
|
2017-08-30 22:08:54 +00:00
|
|
|
|
|
|
|
int FinishedPos = pFinished - pStr;
|
|
|
|
if(FinishedPos == 0 || FinishedPos >= NameBufSize)
|
2017-08-30 22:43:46 +00:00
|
|
|
return -1;
|
2017-08-30 22:08:54 +00:00
|
|
|
|
|
|
|
str_copy(pNameBuf, pStr, FinishedPos + 1);
|
|
|
|
|
|
|
|
return TimeFromStr(pFinished + str_length(s_pFinishedStr));
|
|
|
|
}
|
2017-09-09 18:28:38 +00:00
|
|
|
|
2017-09-09 18:54:49 +00:00
|
|
|
bool CRaceHelper::IsStart(CGameClient *pClient, vec2 Prev, vec2 Pos)
|
2017-09-09 18:28:38 +00:00
|
|
|
{
|
2017-09-09 18:54:49 +00:00
|
|
|
CCollision *pCollision = pClient->Collision();
|
2019-06-03 19:52:14 +00:00
|
|
|
if(pClient->m_GameInfo.m_FlagStartsRace)
|
2017-09-09 18:54:49 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
std::list<int> Indices = pCollision->GetMapIndices(Prev, Pos);
|
2017-09-09 18:54:49 +00:00
|
|
|
if(!Indices.empty())
|
2020-10-26 13:11:11 +00:00
|
|
|
for(int &Indice : Indices)
|
2017-09-09 18:54:49 +00:00
|
|
|
{
|
2020-10-26 13:11:11 +00:00
|
|
|
if(pCollision->GetTileIndex(Indice) == TILE_START)
|
2017-09-09 18:54:49 +00:00
|
|
|
return true;
|
2020-10-26 13:11:11 +00:00
|
|
|
if(pCollision->GetFTileIndex(Indice) == TILE_START)
|
2017-09-09 18:54:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
2017-09-09 18:28:38 +00:00
|
|
|
{
|
2020-09-13 20:00:49 +00:00
|
|
|
if(pCollision->GetTileIndex(pCollision->GetPureMapIndex(Pos)) == TILE_START)
|
2017-09-09 18:28:38 +00:00
|
|
|
return true;
|
2020-09-13 20:00:49 +00:00
|
|
|
if(pCollision->GetFTileIndex(pCollision->GetPureMapIndex(Pos)) == TILE_START)
|
2017-09-09 18:28:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|