2011-02-04 17:25:04 +00:00
|
|
|
/* (c) Rajh, Redix and Sushi. */
|
|
|
|
|
2017-09-09 15:04:19 +00:00
|
|
|
#include <engine/ghost.h>
|
2011-02-04 17:25:04 +00:00
|
|
|
#include <engine/graphics.h>
|
2017-09-09 18:28:38 +00:00
|
|
|
#include <engine/serverbrowser.h>
|
2017-09-09 15:04:19 +00:00
|
|
|
#include <engine/storage.h>
|
2011-02-04 17:25:04 +00:00
|
|
|
#include <engine/shared/config.h>
|
|
|
|
|
2017-09-09 22:57:32 +00:00
|
|
|
#include "players.h"
|
2017-08-30 19:44:27 +00:00
|
|
|
#include "race.h"
|
2011-02-04 17:25:04 +00:00
|
|
|
#include "skins.h"
|
|
|
|
#include "menus.h"
|
|
|
|
#include "ghost.h"
|
|
|
|
|
2017-09-14 23:55:29 +00:00
|
|
|
CGhost::CGhost() : m_StartRenderTick(-1), m_LastDeathTick(-1), m_Recording(false), m_Rendering(false) {}
|
2011-02-04 17:25:04 +00:00
|
|
|
|
2017-09-10 22:22:35 +00:00
|
|
|
void CGhost::GetGhostSkin(CGhostSkin *pSkin, const char *pSkinName, int UseCustomColor, int ColorBody, int ColorFeet)
|
|
|
|
{
|
|
|
|
StrToInts(&pSkin->m_Skin0, 6, pSkinName);
|
|
|
|
pSkin->m_UseCustomColor = UseCustomColor;
|
|
|
|
pSkin->m_ColorBody = ColorBody;
|
|
|
|
pSkin->m_ColorFeet = ColorFeet;
|
|
|
|
}
|
|
|
|
|
2017-09-09 22:57:32 +00:00
|
|
|
void CGhost::GetGhostCharacter(CGhostCharacter *pGhostChar, const CNetObj_Character *pChar)
|
2017-09-09 16:25:32 +00:00
|
|
|
{
|
|
|
|
pGhostChar->m_X = pChar->m_X;
|
|
|
|
pGhostChar->m_Y = pChar->m_Y;
|
|
|
|
pGhostChar->m_VelX = pChar->m_VelX;
|
|
|
|
pGhostChar->m_VelY = 0;
|
|
|
|
pGhostChar->m_Angle = pChar->m_Angle;
|
|
|
|
pGhostChar->m_Direction = pChar->m_Direction;
|
|
|
|
pGhostChar->m_Weapon = pChar->m_Weapon;
|
|
|
|
pGhostChar->m_HookState = pChar->m_HookState;
|
|
|
|
pGhostChar->m_HookX = pChar->m_HookX;
|
|
|
|
pGhostChar->m_HookY = pChar->m_HookY;
|
|
|
|
pGhostChar->m_AttackTick = pChar->m_AttackTick;
|
2017-09-09 22:57:32 +00:00
|
|
|
pGhostChar->m_Tick = pChar->m_Tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGhost::GetNetObjCharacter(CNetObj_Character *pChar, const CGhostCharacter *pGhostChar)
|
|
|
|
{
|
|
|
|
mem_zero(pChar, sizeof(CNetObj_Character));
|
|
|
|
pChar->m_X = pGhostChar->m_X;
|
|
|
|
pChar->m_Y = pGhostChar->m_Y;
|
|
|
|
pChar->m_VelX = pGhostChar->m_VelX;
|
|
|
|
pChar->m_VelY = 0;
|
|
|
|
pChar->m_Angle = pGhostChar->m_Angle;
|
|
|
|
pChar->m_Direction = pGhostChar->m_Direction;
|
|
|
|
pChar->m_Weapon = pGhostChar->m_Weapon == WEAPON_GRENADE ? WEAPON_GRENADE : WEAPON_GUN;
|
|
|
|
pChar->m_HookState = pGhostChar->m_HookState;
|
|
|
|
pChar->m_HookX = pGhostChar->m_HookX;
|
|
|
|
pChar->m_HookY = pGhostChar->m_HookY;
|
|
|
|
pChar->m_AttackTick = pGhostChar->m_AttackTick;
|
|
|
|
pChar->m_HookedPlayer = -1;
|
|
|
|
pChar->m_Tick = pGhostChar->m_Tick;
|
2017-09-09 16:25:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 14:42:47 +00:00
|
|
|
CGhost::CGhostPath::CGhostPath(CGhostPath &&Other)
|
|
|
|
: m_ChunkSize(Other.m_ChunkSize), m_NumItems(Other.m_NumItems), m_lChunks(std::move(Other.m_lChunks))
|
|
|
|
{
|
|
|
|
Other.m_NumItems = 0;
|
|
|
|
Other.m_lChunks.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
CGhost::CGhostPath &CGhost::CGhostPath::operator = (CGhostPath &&Other)
|
2017-09-16 15:45:09 +00:00
|
|
|
{
|
|
|
|
Reset(Other.m_ChunkSize);
|
2017-09-28 14:42:47 +00:00
|
|
|
m_NumItems = Other.m_NumItems;
|
|
|
|
m_lChunks = std::move(Other.m_lChunks);
|
|
|
|
Other.m_NumItems = 0;
|
|
|
|
Other.m_lChunks.clear();
|
|
|
|
return *this;
|
2017-09-16 15:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGhost::CGhostPath::Reset(int ChunkSize)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < m_lChunks.size(); i++)
|
|
|
|
mem_free(m_lChunks[i]);
|
|
|
|
m_lChunks.clear();
|
|
|
|
m_ChunkSize = ChunkSize;
|
|
|
|
m_NumItems = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGhost::CGhostPath::SetSize(int Items)
|
|
|
|
{
|
|
|
|
int Chunks = m_lChunks.size();
|
|
|
|
int NeededChunks = (Items + m_ChunkSize - 1) / m_ChunkSize;
|
|
|
|
|
|
|
|
if(NeededChunks > Chunks)
|
|
|
|
{
|
2017-09-28 14:42:47 +00:00
|
|
|
m_lChunks.resize(NeededChunks);
|
2017-09-16 15:45:09 +00:00
|
|
|
for(int i = Chunks; i < NeededChunks; i++)
|
|
|
|
m_lChunks[i] = (CGhostCharacter*)mem_alloc(sizeof(CGhostCharacter) * m_ChunkSize, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_NumItems = Items;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGhost::CGhostPath::Add(CGhostCharacter Char)
|
|
|
|
{
|
|
|
|
SetSize(m_NumItems + 1);
|
|
|
|
*Get(m_NumItems - 1) = Char;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGhostCharacter *CGhost::CGhostPath::Get(int Index)
|
|
|
|
{
|
|
|
|
if(Index < 0 || Index >= m_NumItems)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int Chunk = Index / m_ChunkSize;
|
|
|
|
int Pos = Index % m_ChunkSize;
|
|
|
|
return &m_lChunks[Chunk][Pos];
|
|
|
|
}
|
|
|
|
|
2017-09-09 15:04:19 +00:00
|
|
|
void CGhost::AddInfos(const CNetObj_Character *pChar)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2017-09-16 15:45:09 +00:00
|
|
|
int NumTicks = m_CurGhost.m_Path.Size();
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 19:38:41 +00:00
|
|
|
// do not start writing to file as long as we still touch the start line
|
|
|
|
if(g_Config.m_ClRaceSaveGhost && !GhostRecorder()->IsRecording() && NumTicks > 0)
|
|
|
|
{
|
2017-09-10 22:55:11 +00:00
|
|
|
Client()->GhostRecorder_Start(m_CurGhost.m_aPlayer);
|
2017-09-09 19:38:41 +00:00
|
|
|
|
2017-09-12 15:01:32 +00:00
|
|
|
GhostRecorder()->WriteData(GHOSTDATA_TYPE_START_TICK, (const char*)&m_CurGhost.m_StartTick, sizeof(int));
|
2017-09-10 22:22:35 +00:00
|
|
|
GhostRecorder()->WriteData(GHOSTDATA_TYPE_SKIN, (const char*)&m_CurGhost.m_Skin, sizeof(CGhostSkin));
|
2017-09-09 19:38:41 +00:00
|
|
|
for(int i = 0; i < NumTicks; i++)
|
2017-09-16 15:45:09 +00:00
|
|
|
GhostRecorder()->WriteData(GHOSTDATA_TYPE_CHARACTER, (const char*)m_CurGhost.m_Path.Get(i), sizeof(CGhostCharacter));
|
2017-09-09 19:38:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
CGhostCharacter GhostChar;
|
2017-09-09 16:25:32 +00:00
|
|
|
GetGhostCharacter(&GhostChar, pChar);
|
2017-09-16 15:45:09 +00:00
|
|
|
m_CurGhost.m_Path.Add(GhostChar);
|
2017-09-09 16:25:32 +00:00
|
|
|
if(GhostRecorder()->IsRecording())
|
2017-09-09 19:38:41 +00:00
|
|
|
GhostRecorder()->WriteData(GHOSTDATA_TYPE_CHARACTER, (const char*)&GhostChar, sizeof(CGhostCharacter));
|
2017-09-09 16:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CGhost::GetSlot() const
|
|
|
|
{
|
|
|
|
for(int i = 0; i < MAX_ACTIVE_GHOSTS; i++)
|
|
|
|
if(m_aActiveGhosts[i].Empty())
|
|
|
|
return i;
|
|
|
|
return -1;
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 02:57:03 +00:00
|
|
|
int CGhost::FreeSlot() const
|
|
|
|
{
|
|
|
|
int Num = 0;
|
|
|
|
for(int i = 0; i < MAX_ACTIVE_GHOSTS; i++)
|
|
|
|
if(m_aActiveGhosts[i].Empty())
|
|
|
|
Num++;
|
|
|
|
return Num;
|
|
|
|
}
|
|
|
|
|
2011-02-04 17:25:04 +00:00
|
|
|
void CGhost::OnRender()
|
|
|
|
{
|
2017-09-09 18:28:38 +00:00
|
|
|
// only for race
|
|
|
|
CServerInfo ServerInfo;
|
|
|
|
Client()->GetServerInfo(&ServerInfo);
|
2017-09-12 20:54:29 +00:00
|
|
|
if(!IsRace(&ServerInfo) || !g_Config.m_ClRaceGhost || !m_pClient->m_Snap.m_pGameInfoObj || Client()->State() != IClient::STATE_ONLINE)
|
2011-02-04 17:25:04 +00:00
|
|
|
return;
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
if(m_pClient->m_Snap.m_pLocalCharacter && m_pClient->m_Snap.m_pLocalPrevCharacter)
|
2015-04-18 20:29:28 +00:00
|
|
|
{
|
2017-09-12 20:54:29 +00:00
|
|
|
bool RaceFlag = m_pClient->m_Snap.m_pGameInfoObj->m_GameStateFlags&GAMESTATEFLAG_RACETIME;
|
2017-09-16 13:19:26 +00:00
|
|
|
bool ServerControl = RaceFlag && g_Config.m_ClRaceGhostServerControl;
|
2017-09-12 20:54:29 +00:00
|
|
|
int RaceTick = -m_pClient->m_Snap.m_pGameInfoObj->m_WarmupTimer;
|
|
|
|
|
|
|
|
static int s_NewRenderTick = -1;
|
|
|
|
int RenderTick = s_NewRenderTick;
|
|
|
|
|
2017-09-16 13:19:26 +00:00
|
|
|
if(!ServerControl && m_pClient->m_NewPredictedTick)
|
2017-09-09 18:28:38 +00:00
|
|
|
{
|
|
|
|
vec2 PrevPos = m_pClient->m_PredictedPrevChar.m_Pos;
|
|
|
|
vec2 Pos = m_pClient->m_PredictedChar.m_Pos;
|
2017-09-12 20:54:29 +00:00
|
|
|
bool Rendering = m_Rendering || RenderTick != -1;
|
|
|
|
if((!Rendering || m_AllowRestart) && CRaceHelper::IsStart(m_pClient, PrevPos, Pos))
|
|
|
|
RenderTick = Client()->PredGameTick();
|
2017-09-09 18:28:38 +00:00
|
|
|
}
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
if(m_pClient->m_NewTick)
|
|
|
|
{
|
2017-09-12 20:54:29 +00:00
|
|
|
static int s_LastRaceTick = -1;
|
2011-02-04 17:25:04 +00:00
|
|
|
|
2017-09-16 13:19:26 +00:00
|
|
|
if(ServerControl && s_LastRaceTick != RaceTick)
|
2017-09-12 20:54:29 +00:00
|
|
|
{
|
|
|
|
if(m_Recording && s_LastRaceTick != -1)
|
|
|
|
m_AllowRestart = true;
|
|
|
|
if(GhostRecorder()->IsRecording())
|
|
|
|
GhostRecorder()->Stop(0, -1);
|
|
|
|
int StartTick = RaceTick;
|
|
|
|
if(IsDDRace(&ServerInfo))
|
|
|
|
StartTick--;
|
|
|
|
StartRecord(StartTick);
|
|
|
|
RenderTick = StartTick;
|
|
|
|
}
|
2017-09-16 13:19:26 +00:00
|
|
|
else if(!ServerControl)
|
2017-09-09 19:38:41 +00:00
|
|
|
{
|
2017-09-12 20:54:29 +00:00
|
|
|
int PrevTick = m_pClient->m_Snap.m_pLocalPrevCharacter->m_Tick;
|
|
|
|
int CurTick = m_pClient->m_Snap.m_pLocalCharacter->m_Tick;
|
|
|
|
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);
|
|
|
|
|
|
|
|
// detecting death, needed because race allows immediate respawning
|
|
|
|
if((!m_Recording || m_AllowRestart) && m_LastDeathTick < PrevTick)
|
2017-09-12 15:01:32 +00:00
|
|
|
{
|
2017-09-12 20:54:29 +00:00
|
|
|
// estimate the exact start tick
|
|
|
|
int RecordTick = -1;
|
|
|
|
int TickDiff = CurTick - PrevTick;
|
|
|
|
for(int i = 0; i < TickDiff; i++)
|
2017-09-12 15:01:32 +00:00
|
|
|
{
|
2017-09-12 20:54:29 +00:00
|
|
|
if(CRaceHelper::IsStart(m_pClient, mix(PrevPos, Pos, (float)i/TickDiff), mix(PrevPos, Pos, (float)(i+1)/TickDiff)))
|
|
|
|
{
|
|
|
|
RecordTick = PrevTick + i + 1;
|
|
|
|
if(!m_AllowRestart)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(RecordTick != -1)
|
|
|
|
{
|
|
|
|
if(GhostRecorder()->IsRecording())
|
|
|
|
GhostRecorder()->Stop(0, -1);
|
|
|
|
StartRecord(RecordTick);
|
2017-09-12 15:01:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-09 19:38:41 +00:00
|
|
|
}
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
if(m_Recording)
|
|
|
|
AddInfos(m_pClient->m_Snap.m_pLocalCharacter);
|
2017-09-12 20:54:29 +00:00
|
|
|
|
|
|
|
s_LastRaceTick = RaceFlag ? RaceTick : -1;
|
|
|
|
}
|
|
|
|
|
2017-09-16 13:19:26 +00:00
|
|
|
if((ServerControl && m_pClient->m_NewTick) || (!ServerControl && m_pClient->m_NewPredictedTick))
|
2017-09-12 20:54:29 +00:00
|
|
|
{
|
|
|
|
// only restart rendering if it did not change since last tick to prevent stuttering
|
|
|
|
if(s_NewRenderTick != -1 && s_NewRenderTick == RenderTick)
|
|
|
|
{
|
|
|
|
StartRender(RenderTick);
|
|
|
|
RenderTick = -1;
|
|
|
|
}
|
|
|
|
s_NewRenderTick = RenderTick;
|
2017-09-09 18:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-04 17:25:04 +00:00
|
|
|
|
|
|
|
// Play the ghost
|
|
|
|
if(!m_Rendering || !g_Config.m_ClRaceShowGhost)
|
|
|
|
return;
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
int ActiveGhosts = 0;
|
|
|
|
int PlaybackTick = Client()->PredGameTick() - m_StartRenderTick;
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 16:25:32 +00:00
|
|
|
for(int i = 0; i < MAX_ACTIVE_GHOSTS; i++)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2017-09-09 16:25:32 +00:00
|
|
|
CGhostItem *pGhost = &m_aActiveGhosts[i];
|
2017-09-09 18:28:38 +00:00
|
|
|
if(pGhost->Empty())
|
2011-02-11 06:00:12 +00:00
|
|
|
continue;
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
bool End = false;
|
2017-09-12 15:01:32 +00:00
|
|
|
int GhostTick = pGhost->m_StartTick + PlaybackTick;
|
2017-09-16 15:45:09 +00:00
|
|
|
while(pGhost->m_Path.Get(pGhost->m_PlaybackPos)->m_Tick < GhostTick && !End)
|
2017-09-09 18:28:38 +00:00
|
|
|
{
|
2017-09-16 15:45:09 +00:00
|
|
|
if(pGhost->m_PlaybackPos < pGhost->m_Path.Size() - 1)
|
2017-09-09 18:28:38 +00:00
|
|
|
pGhost->m_PlaybackPos++;
|
|
|
|
else
|
|
|
|
End = true;
|
|
|
|
}
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
if(End)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ActiveGhosts++;
|
|
|
|
|
|
|
|
int CurPos = pGhost->m_PlaybackPos;
|
|
|
|
int PrevPos = max(0, CurPos - 1);
|
2017-09-16 15:45:09 +00:00
|
|
|
if(pGhost->m_Path.Get(PrevPos)->m_Tick > GhostTick)
|
2017-09-12 15:01:32 +00:00
|
|
|
continue;
|
|
|
|
|
2017-09-09 22:57:32 +00:00
|
|
|
CNetObj_Character Player, Prev;
|
2017-09-16 15:45:09 +00:00
|
|
|
GetNetObjCharacter(&Player, pGhost->m_Path.Get(CurPos));
|
|
|
|
GetNetObjCharacter(&Prev, pGhost->m_Path.Get(PrevPos));
|
2017-09-09 18:28:38 +00:00
|
|
|
|
|
|
|
int TickDiff = Player.m_Tick - Prev.m_Tick;
|
|
|
|
float IntraTick = 0.f;
|
|
|
|
if(TickDiff > 0)
|
|
|
|
IntraTick = (GhostTick - Prev.m_Tick - 1 + Client()->PredIntraGameTick()) / TickDiff;
|
|
|
|
|
|
|
|
Player.m_AttackTick += Client()->GameTick() - GhostTick;
|
|
|
|
|
2017-09-09 22:57:32 +00:00
|
|
|
m_pClient->m_pPlayers->RenderHook(&Prev, &Player, &pGhost->m_RenderInfo , -2, vec2(), vec2(), IntraTick);
|
|
|
|
m_pClient->m_pPlayers->RenderPlayer(&Prev, &Player, &pGhost->m_RenderInfo, -2, vec2(), IntraTick);
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
2017-09-09 18:28:38 +00:00
|
|
|
|
|
|
|
if(!ActiveGhosts)
|
|
|
|
StopRender();
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 22:22:35 +00:00
|
|
|
void CGhost::InitRenderInfos(CGhostItem *pGhost)
|
2011-02-11 06:00:12 +00:00
|
|
|
{
|
2017-09-10 22:22:35 +00:00
|
|
|
char aSkinName[64];
|
|
|
|
IntsToStr(&pGhost->m_Skin.m_Skin0, 6, aSkinName);
|
|
|
|
CTeeRenderInfo *pRenderInfo = &pGhost->m_RenderInfo;
|
|
|
|
|
|
|
|
int SkinId = m_pClient->m_pSkins->Find(aSkinName);
|
2017-09-09 15:04:19 +00:00
|
|
|
if(SkinId < 0)
|
|
|
|
{
|
|
|
|
SkinId = m_pClient->m_pSkins->Find("default");
|
|
|
|
if(SkinId < 0)
|
|
|
|
SkinId = 0;
|
|
|
|
}
|
|
|
|
|
2017-09-10 22:22:35 +00:00
|
|
|
if(pGhost->m_Skin.m_UseCustomColor)
|
|
|
|
{
|
2017-09-09 15:04:19 +00:00
|
|
|
pRenderInfo->m_Texture = m_pClient->m_pSkins->Get(SkinId)->m_ColorTexture;
|
2017-09-10 22:22:35 +00:00
|
|
|
pRenderInfo->m_ColorBody = m_pClient->m_pSkins->GetColorV4(pGhost->m_Skin.m_ColorBody);
|
|
|
|
pRenderInfo->m_ColorFeet = m_pClient->m_pSkins->GetColorV4(pGhost->m_Skin.m_ColorFeet);
|
|
|
|
}
|
2017-09-09 15:04:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pRenderInfo->m_Texture = m_pClient->m_pSkins->Get(SkinId)->m_OrgTexture;
|
|
|
|
pRenderInfo->m_ColorBody = vec4(1, 1, 1, 1);
|
|
|
|
pRenderInfo->m_ColorFeet = vec4(1, 1, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pRenderInfo->m_ColorBody.a = 0.5f;
|
|
|
|
pRenderInfo->m_ColorFeet.a = 0.5f;
|
|
|
|
pRenderInfo->m_Size = 64;
|
2011-02-11 06:00:12 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 15:01:32 +00:00
|
|
|
void CGhost::StartRecord(int Tick)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
|
|
|
m_Recording = true;
|
2017-09-09 15:04:19 +00:00
|
|
|
m_CurGhost.Reset();
|
2017-09-12 15:01:32 +00:00
|
|
|
m_CurGhost.m_StartTick = Tick;
|
2017-09-10 22:22:35 +00:00
|
|
|
|
|
|
|
const CGameClient::CClientData *pData = &m_pClient->m_aClients[m_pClient->m_Snap.m_LocalClientID];
|
2017-09-10 22:55:11 +00:00
|
|
|
str_copy(m_CurGhost.m_aPlayer, g_Config.m_PlayerName, sizeof(m_CurGhost.m_aPlayer));
|
2017-09-10 22:22:35 +00:00
|
|
|
GetGhostSkin(&m_CurGhost.m_Skin, pData->m_aSkinName, pData->m_UseCustomColor, pData->m_ColorBody, pData->m_ColorFeet);
|
|
|
|
InitRenderInfos(&m_CurGhost);
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 16:25:32 +00:00
|
|
|
void CGhost::StopRecord(int Time)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
|
|
|
m_Recording = false;
|
2017-09-09 16:25:32 +00:00
|
|
|
bool RecordingToFile = GhostRecorder()->IsRecording();
|
|
|
|
|
|
|
|
if(RecordingToFile)
|
2017-09-16 15:45:09 +00:00
|
|
|
GhostRecorder()->Stop(m_CurGhost.m_Path.Size(), Time);
|
2017-09-09 16:25:32 +00:00
|
|
|
|
|
|
|
char aTmpFilename[128];
|
2017-09-10 22:55:11 +00:00
|
|
|
Client()->Ghost_GetPath(aTmpFilename, sizeof(aTmpFilename), m_CurGhost.m_aPlayer);
|
2017-09-09 16:25:32 +00:00
|
|
|
|
|
|
|
CMenus::CGhostItem *pOwnGhost = m_pClient->m_pMenus->GetOwnGhost();
|
|
|
|
if(Time > 0 && (!pOwnGhost || Time < pOwnGhost->m_Time))
|
|
|
|
{
|
2017-09-10 02:57:03 +00:00
|
|
|
if(pOwnGhost && pOwnGhost->Active())
|
|
|
|
Unload(pOwnGhost->m_Slot);
|
|
|
|
|
2017-09-09 16:25:32 +00:00
|
|
|
// add to active ghosts
|
2017-09-10 02:57:03 +00:00
|
|
|
int Slot = GetSlot();
|
2017-09-09 16:25:32 +00:00
|
|
|
if(Slot != -1)
|
2017-09-28 14:42:47 +00:00
|
|
|
m_aActiveGhosts[Slot] = std::move(m_CurGhost);
|
2017-09-09 16:25:32 +00:00
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
// create ghost item
|
|
|
|
CMenus::CGhostItem Item;
|
2017-09-12 15:01:32 +00:00
|
|
|
if(RecordingToFile)
|
|
|
|
Client()->Ghost_GetPath(Item.m_aFilename, sizeof(Item.m_aFilename), m_CurGhost.m_aPlayer, Time);
|
2017-09-10 22:55:11 +00:00
|
|
|
str_copy(Item.m_aPlayer, m_CurGhost.m_aPlayer, sizeof(Item.m_aPlayer));
|
2017-09-09 18:28:38 +00:00
|
|
|
Item.m_Time = Time;
|
|
|
|
Item.m_Slot = Slot;
|
2017-09-10 01:48:22 +00:00
|
|
|
|
|
|
|
// save new ghost file
|
|
|
|
if(Item.HasFile())
|
2017-09-12 15:01:32 +00:00
|
|
|
Storage()->RenameFile(aTmpFilename, Item.m_aFilename, IStorage::TYPE_SAVE);
|
2017-09-09 18:28:38 +00:00
|
|
|
|
|
|
|
// add item to menu list
|
2017-09-10 01:48:22 +00:00
|
|
|
m_pClient->m_pMenus->UpdateOwnGhost(Item);
|
2017-09-09 16:25:32 +00:00
|
|
|
}
|
|
|
|
else if(RecordingToFile) // no new record
|
|
|
|
Storage()->RemoveFile(aTmpFilename, IStorage::TYPE_SAVE);
|
|
|
|
|
|
|
|
m_CurGhost.Reset();
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 15:01:32 +00:00
|
|
|
void CGhost::StartRender(int Tick)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
|
|
|
m_Rendering = true;
|
2017-09-12 15:01:32 +00:00
|
|
|
m_StartRenderTick = Tick;
|
2017-09-09 18:28:38 +00:00
|
|
|
for(int i = 0; i < MAX_ACTIVE_GHOSTS; i++)
|
|
|
|
m_aActiveGhosts[i].m_PlaybackPos = 0;
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGhost::StopRender()
|
|
|
|
{
|
|
|
|
m_Rendering = false;
|
|
|
|
}
|
|
|
|
|
2017-09-09 16:25:32 +00:00
|
|
|
int CGhost::Load(const char *pFilename)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2017-09-09 16:25:32 +00:00
|
|
|
int Slot = GetSlot();
|
|
|
|
if(Slot == -1)
|
|
|
|
return -1;
|
2011-02-11 06:00:12 +00:00
|
|
|
|
2017-09-09 15:04:19 +00:00
|
|
|
if(!Client()->GhostLoader_Load(pFilename))
|
2017-09-09 16:25:32 +00:00
|
|
|
return -1;
|
2011-02-04 17:25:04 +00:00
|
|
|
|
2017-09-09 15:04:19 +00:00
|
|
|
const CGhostHeader *pHeader = GhostLoader()->GetHeader();
|
|
|
|
|
|
|
|
int NumTicks = GhostLoader()->GetTicks(pHeader);
|
|
|
|
int Time = GhostLoader()->GetTime(pHeader);
|
|
|
|
if(NumTicks <= 0 || Time <= 0)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2017-09-09 15:04:19 +00:00
|
|
|
GhostLoader()->Close();
|
2017-09-09 16:25:32 +00:00
|
|
|
return -1;
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 15:04:19 +00:00
|
|
|
// select ghost
|
2017-09-09 16:25:32 +00:00
|
|
|
CGhostItem *pGhost = &m_aActiveGhosts[Slot];
|
2017-09-17 22:57:21 +00:00
|
|
|
pGhost->Reset();
|
2017-09-16 15:45:09 +00:00
|
|
|
pGhost->m_Path.SetSize(NumTicks);
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-10 22:55:11 +00:00
|
|
|
str_copy(pGhost->m_aPlayer, pHeader->m_aOwner, sizeof(pGhost->m_aPlayer));
|
|
|
|
|
2011-02-11 06:00:12 +00:00
|
|
|
int Index = 0;
|
2017-09-09 15:04:19 +00:00
|
|
|
bool FoundSkin = false;
|
2017-09-09 18:28:38 +00:00
|
|
|
bool NoTick = false;
|
2017-09-10 22:22:35 +00:00
|
|
|
bool Error = false;
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 15:04:19 +00:00
|
|
|
int Type;
|
2017-09-10 22:22:35 +00:00
|
|
|
while(!Error && GhostLoader()->ReadNextType(&Type))
|
2017-09-09 15:04:19 +00:00
|
|
|
{
|
2017-09-09 18:28:38 +00:00
|
|
|
if(Index == NumTicks && (Type == GHOSTDATA_TYPE_CHARACTER || Type == GHOSTDATA_TYPE_CHARACTER_NO_TICK))
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2017-09-10 22:22:35 +00:00
|
|
|
Error = true;
|
2011-02-04 17:25:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-10 22:22:35 +00:00
|
|
|
if(Type == GHOSTDATA_TYPE_SKIN && !FoundSkin)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2017-09-10 22:22:35 +00:00
|
|
|
FoundSkin = true;
|
|
|
|
if(!GhostLoader()->ReadData(Type, (char*)&pGhost->m_Skin, sizeof(CGhostSkin)))
|
|
|
|
Error = true;
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
2017-09-09 15:04:19 +00:00
|
|
|
else if(Type == GHOSTDATA_TYPE_CHARACTER_NO_TICK)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2017-09-09 18:28:38 +00:00
|
|
|
NoTick = true;
|
2017-09-16 15:45:09 +00:00
|
|
|
if(!GhostLoader()->ReadData(Type, (char*)pGhost->m_Path.Get(Index++), sizeof(CGhostCharacter_NoTick)))
|
2017-09-10 22:22:35 +00:00
|
|
|
Error = true;
|
2017-09-09 18:28:38 +00:00
|
|
|
}
|
|
|
|
else if(Type == GHOSTDATA_TYPE_CHARACTER)
|
|
|
|
{
|
2017-09-16 15:45:09 +00:00
|
|
|
if(!GhostLoader()->ReadData(Type, (char*)pGhost->m_Path.Get(Index++), sizeof(CGhostCharacter)))
|
2017-09-10 22:22:35 +00:00
|
|
|
Error = true;
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
2017-09-12 15:01:32 +00:00
|
|
|
else if(Type == GHOSTDATA_TYPE_START_TICK)
|
|
|
|
{
|
|
|
|
if(!GhostLoader()->ReadData(Type, (char*)&pGhost->m_StartTick, sizeof(int)))
|
|
|
|
Error = true;
|
|
|
|
}
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-09 15:04:19 +00:00
|
|
|
GhostLoader()->Close();
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2017-09-10 22:22:35 +00:00
|
|
|
if(Error || Index != NumTicks)
|
2017-09-09 16:25:32 +00:00
|
|
|
{
|
|
|
|
pGhost->Reset();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-09-09 18:28:38 +00:00
|
|
|
if(NoTick)
|
|
|
|
{
|
|
|
|
int StartTick = 0;
|
|
|
|
for(int i = 1; i < NumTicks; i++) // estimate start tick
|
2017-09-16 15:45:09 +00:00
|
|
|
if(pGhost->m_Path.Get(i)->m_AttackTick != pGhost->m_Path.Get(i - 1)->m_AttackTick)
|
|
|
|
StartTick = pGhost->m_Path.Get(i)->m_AttackTick - i;
|
2017-09-09 18:28:38 +00:00
|
|
|
for(int i = 0; i < NumTicks; i++)
|
2017-09-16 15:45:09 +00:00
|
|
|
pGhost->m_Path.Get(i)->m_Tick = StartTick + i;
|
2017-09-09 18:28:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 15:01:32 +00:00
|
|
|
if(pGhost->m_StartTick == -1)
|
2017-09-16 15:45:09 +00:00
|
|
|
pGhost->m_StartTick = pGhost->m_Path.Get(0)->m_Tick;
|
2017-09-12 15:01:32 +00:00
|
|
|
|
2017-09-09 15:04:19 +00:00
|
|
|
if(!FoundSkin)
|
2017-09-10 22:22:35 +00:00
|
|
|
GetGhostSkin(&pGhost->m_Skin, "default", 0, 0, 0);
|
|
|
|
InitRenderInfos(pGhost);
|
2017-09-09 15:04:19 +00:00
|
|
|
|
2017-09-09 16:25:32 +00:00
|
|
|
return Slot;
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 16:25:32 +00:00
|
|
|
void CGhost::Unload(int Slot)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2017-09-09 16:25:32 +00:00
|
|
|
m_aActiveGhosts[Slot].Reset();
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 01:48:22 +00:00
|
|
|
void CGhost::UnloadAll()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < MAX_ACTIVE_GHOSTS; i++)
|
|
|
|
Unload(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGhost::SaveGhost(CMenus::CGhostItem *pItem)
|
|
|
|
{
|
|
|
|
int Slot = pItem->m_Slot;
|
2017-09-10 22:55:11 +00:00
|
|
|
if(!pItem->Active() || pItem->HasFile() || m_aActiveGhosts[Slot].Empty() || GhostRecorder()->IsRecording())
|
2017-09-10 01:48:22 +00:00
|
|
|
return;
|
|
|
|
|
2017-09-16 15:45:09 +00:00
|
|
|
CGhostItem *pGhost = &m_aActiveGhosts[Slot];
|
2017-09-12 15:01:32 +00:00
|
|
|
|
2017-09-16 15:45:09 +00:00
|
|
|
int NumTicks = pGhost->m_Path.Size();
|
2017-09-10 22:55:11 +00:00
|
|
|
Client()->GhostRecorder_Start(pItem->m_aPlayer, pItem->m_Time);
|
2017-09-10 01:48:22 +00:00
|
|
|
|
2017-09-12 15:01:32 +00:00
|
|
|
GhostRecorder()->WriteData(GHOSTDATA_TYPE_START_TICK, (const char*)&pGhost->m_StartTick, sizeof(int));
|
|
|
|
GhostRecorder()->WriteData(GHOSTDATA_TYPE_SKIN, (const char*)&pGhost->m_Skin, sizeof(CGhostSkin));
|
2017-09-10 01:48:22 +00:00
|
|
|
for(int i = 0; i < NumTicks; i++)
|
2017-09-16 15:45:09 +00:00
|
|
|
GhostRecorder()->WriteData(GHOSTDATA_TYPE_CHARACTER, (const char*)pGhost->m_Path.Get(i), sizeof(CGhostCharacter));
|
2017-09-10 01:48:22 +00:00
|
|
|
|
|
|
|
GhostRecorder()->Stop(NumTicks, pItem->m_Time);
|
2017-09-10 22:55:11 +00:00
|
|
|
Client()->Ghost_GetPath(pItem->m_aFilename, sizeof(pItem->m_aFilename), pItem->m_aPlayer, pItem->m_Time);
|
2017-09-10 01:48:22 +00:00
|
|
|
}
|
|
|
|
|
2011-08-13 00:11:06 +00:00
|
|
|
void CGhost::ConGPlay(IConsole::IResult *pResult, void *pUserData)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2017-09-12 15:01:32 +00:00
|
|
|
CGhost *pGhost = (CGhost *)pUserData;
|
|
|
|
pGhost->StartRender(pGhost->Client()->PredGameTick());
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGhost::OnConsoleInit()
|
|
|
|
{
|
2017-09-09 15:04:19 +00:00
|
|
|
m_pGhostLoader = Kernel()->RequestInterface<IGhostLoader>();
|
|
|
|
m_pGhostRecorder = Kernel()->RequestInterface<IGhostRecorder>();
|
|
|
|
|
2011-08-13 00:11:06 +00:00
|
|
|
Console()->Register("gplay", "", CFGFLAG_CLIENT, ConGPlay, this, "");
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGhost::OnMessage(int MsgType, void *pRawMsg)
|
|
|
|
{
|
2017-09-09 18:28:38 +00:00
|
|
|
// only for race
|
|
|
|
CServerInfo ServerInfo;
|
|
|
|
Client()->GetServerInfo(&ServerInfo);
|
|
|
|
|
2017-09-10 02:39:20 +00:00
|
|
|
if(!IsRace(&ServerInfo) || m_pClient->m_Snap.m_SpecInfo.m_Active || Client()->State() != IClient::STATE_ONLINE)
|
2011-02-04 17:25:04 +00:00
|
|
|
return;
|
2015-04-18 20:29:28 +00:00
|
|
|
|
2011-02-04 17:25:04 +00:00
|
|
|
// 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)
|
2017-09-09 18:28:38 +00:00
|
|
|
{
|
|
|
|
if(m_Recording)
|
|
|
|
StopRecord();
|
|
|
|
StopRender();
|
|
|
|
m_LastDeathTick = Client()->GameTick();
|
|
|
|
}
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
else if(MsgType == NETMSGTYPE_SV_CHAT)
|
|
|
|
{
|
|
|
|
CNetMsg_Sv_Chat *pMsg = (CNetMsg_Sv_Chat *)pRawMsg;
|
2017-09-09 15:04:19 +00:00
|
|
|
if(pMsg->m_ClientID == -1 && m_Recording)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2016-11-15 14:28:11 +00:00
|
|
|
char aName[MAX_NAME_LENGTH];
|
2017-08-30 19:44:27 +00:00
|
|
|
int Time = CRaceHelper::TimeFromFinishMessage(pMsg->m_pMessage, aName, sizeof(aName));
|
2017-08-30 22:43:46 +00:00
|
|
|
if(Time > 0 && str_comp(aName, m_pClient->m_aClients[m_pClient->m_Snap.m_LocalClientID].m_aName) == 0)
|
2011-02-04 17:25:04 +00:00
|
|
|
{
|
2017-09-09 16:25:32 +00:00
|
|
|
StopRecord(Time);
|
2017-09-09 15:04:19 +00:00
|
|
|
StopRender();
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGhost::OnReset()
|
|
|
|
{
|
|
|
|
StopRecord();
|
|
|
|
StopRender();
|
2017-09-09 18:28:38 +00:00
|
|
|
m_LastDeathTick = -1;
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGhost::OnMapLoad()
|
|
|
|
{
|
|
|
|
OnReset();
|
2017-09-10 01:48:22 +00:00
|
|
|
UnloadAll();
|
2011-02-04 17:25:04 +00:00
|
|
|
m_pClient->m_pMenus->GhostlistPopulate();
|
2017-09-12 20:54:29 +00:00
|
|
|
m_AllowRestart = false;
|
2011-02-04 17:25:04 +00:00
|
|
|
}
|