2011-02-11 06:00:12 +00:00
|
|
|
/* (c) Redix and Sushi */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <engine/shared/config.h>
|
|
|
|
#include <engine/serverbrowser.h>
|
|
|
|
#include <engine/storage.h>
|
|
|
|
|
|
|
|
#include "menus.h"
|
|
|
|
#include "race_demo.h"
|
|
|
|
|
|
|
|
CRaceDemo::CRaceDemo()
|
|
|
|
{
|
|
|
|
m_RaceState = RACE_NONE;
|
|
|
|
m_RecordStopTime = 0;
|
|
|
|
m_Time = 0;
|
|
|
|
m_DemoStartTick = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRaceDemo::OnRender()
|
|
|
|
{
|
2011-04-09 06:41:31 +00:00
|
|
|
if(!g_Config.m_ClAutoRaceRecord || !m_pClient->m_Snap.m_pGameInfoObj || m_pClient->m_Snap.m_SpecInfo.m_Active || Client()->State() != IClient::STATE_ONLINE)
|
2011-02-11 06:00:12 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// start the demo
|
2011-02-16 08:11:00 +00:00
|
|
|
if(m_DemoStartTick < Client()->GameTick())
|
2011-02-11 06:00:12 +00:00
|
|
|
{
|
2011-02-16 08:11:00 +00:00
|
|
|
bool start = false;
|
|
|
|
std::list < int > Indices = m_pClient->Collision()->GetMapIndices(m_pClient->m_PredictedPrevChar.m_Pos, m_pClient->m_LocalCharacterPos);
|
|
|
|
if(!Indices.empty())
|
|
|
|
for(std::list < int >::iterator i = Indices.begin(); i != Indices.end(); i++)
|
|
|
|
if(m_pClient->Collision()->GetTileIndex(*i) == TILE_BEGIN) start = true;
|
|
|
|
else
|
|
|
|
start = m_pClient->Collision()->GetTileIndex(m_pClient->Collision()->GetPureMapIndex(m_pClient->m_LocalCharacterPos)) == TILE_BEGIN;
|
2011-02-11 06:00:12 +00:00
|
|
|
|
2011-02-16 08:11:00 +00:00
|
|
|
if(start)
|
|
|
|
{
|
|
|
|
OnReset();
|
|
|
|
m_pMap = Client()->RaceRecordStart("tmp");
|
|
|
|
m_DemoStartTick = Client()->GameTick() + Client()->GameTickSpeed();
|
|
|
|
m_RaceState = RACE_STARTED;
|
|
|
|
}
|
2011-02-11 06:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop the demo
|
|
|
|
if(m_RaceState == RACE_FINISHED && m_RecordStopTime < Client()->GameTick() && m_Time > 0)
|
|
|
|
{
|
|
|
|
CheckDemo();
|
|
|
|
OnReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRaceDemo::OnReset()
|
|
|
|
{
|
2011-02-16 08:11:00 +00:00
|
|
|
if(Client()->State() != IClient::STATE_ONLINE)
|
|
|
|
return;
|
|
|
|
|
2011-02-11 06:00:12 +00:00
|
|
|
if(Client()->DemoIsRecording())
|
|
|
|
Client()->RaceRecordStop();
|
|
|
|
|
|
|
|
char aFilename[512];
|
|
|
|
str_format(aFilename, sizeof(aFilename), "demos/%s_tmp.demo", m_pMap);
|
|
|
|
Storage()->RemoveFile(aFilename, IStorage::TYPE_SAVE);
|
|
|
|
|
|
|
|
m_Time = 0;
|
|
|
|
m_RaceState = RACE_NONE;
|
|
|
|
m_RecordStopTime = 0;
|
|
|
|
m_DemoStartTick = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRaceDemo::OnShutdown()
|
|
|
|
{
|
|
|
|
if(Client()->DemoIsRecording())
|
|
|
|
Client()->RaceRecordStop();
|
|
|
|
|
|
|
|
char aFilename[512];
|
|
|
|
str_format(aFilename, sizeof(aFilename), "demos/%s_tmp.demo", m_pMap);
|
|
|
|
Storage()->RemoveFile(aFilename, IStorage::TYPE_SAVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRaceDemo::OnMessage(int MsgType, void *pRawMsg)
|
|
|
|
{
|
2011-04-09 06:41:31 +00:00
|
|
|
if(!g_Config.m_ClAutoRaceRecord || Client()->State() != IClient::STATE_ONLINE || m_pClient->m_Snap.m_SpecInfo.m_Active)
|
2011-02-11 06:00:12 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// check for messages from server
|
|
|
|
if(MsgType == NETMSGTYPE_SV_KILLMSG)
|
|
|
|
{
|
|
|
|
CNetMsg_Sv_KillMsg *pMsg = (CNetMsg_Sv_KillMsg *)pRawMsg;
|
2011-02-13 05:35:13 +00:00
|
|
|
if(pMsg->m_Victim == m_pClient->m_Snap.m_LocalClientID && m_RaceState == RACE_FINISHED)
|
2011-02-11 06:00:12 +00:00
|
|
|
{
|
|
|
|
// check for new record
|
|
|
|
CheckDemo();
|
|
|
|
OnReset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(MsgType == NETMSGTYPE_SV_CHAT)
|
|
|
|
{
|
|
|
|
CNetMsg_Sv_Chat *pMsg = (CNetMsg_Sv_Chat *)pRawMsg;
|
2011-02-13 05:35:13 +00:00
|
|
|
if(pMsg->m_ClientID == -1 && m_RaceState == RACE_STARTED)
|
2011-02-11 06:00:12 +00:00
|
|
|
{
|
|
|
|
const char* pMessage = pMsg->m_pMessage;
|
|
|
|
|
|
|
|
int Num = 0;
|
|
|
|
while(str_comp_num(pMessage, " finished in: ", 14))
|
|
|
|
{
|
|
|
|
pMessage++;
|
|
|
|
Num++;
|
|
|
|
if(!pMessage[0])
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// store the name
|
|
|
|
char aName[64];
|
|
|
|
str_copy(aName, pMsg->m_pMessage, Num+1);
|
|
|
|
|
|
|
|
// prepare values and state for saving
|
|
|
|
int Minutes;
|
|
|
|
float Seconds;
|
2011-02-13 05:35:13 +00:00
|
|
|
if(!str_comp(aName, m_pClient->m_aClients[m_pClient->m_Snap.m_LocalClientID].m_aName) && sscanf(pMessage, " finished in: %d minute(s) %f", &Minutes, &Seconds) == 2)
|
2011-02-11 06:00:12 +00:00
|
|
|
{
|
|
|
|
m_RaceState = RACE_FINISHED;
|
|
|
|
m_RecordStopTime = Client()->GameTick() + Client()->GameTickSpeed();
|
|
|
|
m_Time = Minutes*60 + Seconds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRaceDemo::CheckDemo()
|
|
|
|
{
|
|
|
|
// stop the demo recording
|
|
|
|
Client()->RaceRecordStop();
|
|
|
|
|
|
|
|
char aTmpDemoName[128];
|
|
|
|
str_format(aTmpDemoName, sizeof(aTmpDemoName), "%s_tmp", m_pMap);
|
|
|
|
|
|
|
|
// loop through demo files
|
|
|
|
m_pClient->m_pMenus->DemolistPopulate();
|
|
|
|
for(int i = 0; i < m_pClient->m_pMenus->m_lDemos.size(); i++)
|
|
|
|
{
|
|
|
|
if(!str_comp_num(m_pClient->m_pMenus->m_lDemos[i].m_aName, m_pMap, str_length(m_pMap)) && str_comp_num(m_pClient->m_pMenus->m_lDemos[i].m_aName, aTmpDemoName, str_length(aTmpDemoName)))
|
|
|
|
{
|
|
|
|
const char *pDemo = m_pClient->m_pMenus->m_lDemos[i].m_aName;
|
|
|
|
|
|
|
|
// set cursor
|
|
|
|
pDemo += str_length(m_pMap)+1;
|
|
|
|
float DemoTime = str_tofloat(pDemo);
|
|
|
|
if(m_Time < DemoTime)
|
|
|
|
{
|
|
|
|
// save new record
|
|
|
|
SaveDemo(m_pMap);
|
|
|
|
|
|
|
|
// delete old demo
|
|
|
|
char aFilename[512];
|
|
|
|
str_format(aFilename, sizeof(aFilename), "demos/%s.demo", m_pClient->m_pMenus->m_lDemos[i].m_aName);
|
|
|
|
Storage()->RemoveFile(aFilename, IStorage::TYPE_SAVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Time = 0;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// save demo if there is none
|
|
|
|
SaveDemo(m_pMap);
|
|
|
|
|
|
|
|
m_Time = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRaceDemo::SaveDemo(const char* pDemo)
|
|
|
|
{
|
|
|
|
char aNewFilename[512];
|
|
|
|
char aOldFilename[512];
|
|
|
|
if(g_Config.m_ClDemoName)
|
|
|
|
{
|
|
|
|
char aPlayerName[MAX_NAME_LENGTH];
|
2011-02-13 05:35:13 +00:00
|
|
|
str_copy(aPlayerName, m_pClient->m_aClients[m_pClient->m_Snap.m_LocalClientID].m_aName, sizeof(aPlayerName));
|
2011-02-11 06:00:12 +00:00
|
|
|
|
|
|
|
// check the player name
|
|
|
|
for(int i = 0; i < MAX_NAME_LENGTH; i++)
|
|
|
|
{
|
|
|
|
if(!aPlayerName[i])
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(aPlayerName[i] == '\\' || aPlayerName[i] == '/' || aPlayerName[i] == '|' || aPlayerName[i] == ':' || aPlayerName[i] == '*' || aPlayerName[i] == '?' || aPlayerName[i] == '<' || aPlayerName[i] == '>' || aPlayerName[i] == '"')
|
|
|
|
aPlayerName[i] = '%';
|
|
|
|
|
|
|
|
str_format(aNewFilename, sizeof(aNewFilename), "demos/%s_%5.2f_%s.demo", pDemo, m_Time, aPlayerName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str_format(aNewFilename, sizeof(aNewFilename), "demos/%s_%5.2f.demo", pDemo, m_Time);
|
|
|
|
|
|
|
|
str_format(aOldFilename, sizeof(aOldFilename), "demos/%s_tmp.demo", m_pMap);
|
|
|
|
|
|
|
|
Storage()->RenameFile(aOldFilename, aNewFilename, IStorage::TYPE_SAVE);
|
2011-02-16 08:11:00 +00:00
|
|
|
|
|
|
|
dbg_msg("racedemo", "Saved better demo");
|
|
|
|
}
|