1#include "envelope_state.h"
2
3#include <base/time.h>
4
5#include <game/client/gameclient.h>
6#include <game/localization.h>
7
8#include <chrono>
9
10using namespace std::chrono_literals;
11
12CEnvelopeState::CEnvelopeState(IMap *pMap, bool OnlineOnly) :
13 m_pMap(pMap)
14{
15 m_pEnvelopePoints = std::make_shared<CMapBasedEnvelopePointAccess>(args&: m_pMap);
16 m_OnlineOnly = OnlineOnly;
17}
18
19void CEnvelopeState::EnvelopeEval(int TimeOffsetMillis, int EnvelopeIndex, ColorRGBA &Result, size_t Channels)
20{
21 using namespace std::chrono;
22
23 if(!m_pMap)
24 return;
25
26 int EnvelopeStart, EnvelopeNum;
27 m_pMap->GetType(Type: MAPITEMTYPE_ENVELOPE, pStart: &EnvelopeStart, pNum: &EnvelopeNum);
28 if(EnvelopeIndex < 0 || EnvelopeIndex >= EnvelopeNum)
29 return;
30
31 const CMapItemEnvelope *pItem = (CMapItemEnvelope *)m_pMap->GetItem(Index: EnvelopeStart + EnvelopeIndex);
32 if(pItem->m_Channels <= 0)
33 return;
34 Channels = minimum<size_t>(a: Channels, b: pItem->m_Channels, c: CEnvPoint::MAX_CHANNELS);
35
36 m_pEnvelopePoints->SetPointsRange(StartPoint: pItem->m_StartPoint, NumPoints: pItem->m_NumPoints);
37 if(m_pEnvelopePoints->NumPoints() == 0)
38 return;
39
40 nanoseconds Time;
41
42 // offline rendering (like menu background) relies on local time
43 if(!m_OnlineOnly)
44 {
45 Time = time_get_nanoseconds();
46 }
47 else
48 {
49 // online rendering
50 if(GameClient()->m_Snap.m_pGameInfoObj)
51 {
52 static const nanoseconds s_NanosPerTick = nanoseconds(1s) / static_cast<int64_t>(Client()->GameTickSpeed());
53
54 // get the lerp of the current tick and prev
55 int EnvelopeTick;
56 double TickRatio;
57 if(Client()->State() == IClient::STATE_DEMOPLAYBACK || !g_Config.m_ClPredict ||
58 (GameClient()->m_Snap.m_SpecInfo.m_Active && GameClient()->m_Snap.m_SpecInfo.m_SpectatorId != SPEC_FREEVIEW))
59 {
60 EnvelopeTick = Client()->PrevGameTick(Conn: g_Config.m_ClDummy) - GameClient()->m_Snap.m_pGameInfoObj->m_RoundStartTick;
61 const int CurTick = Client()->GameTick(Conn: g_Config.m_ClDummy) - GameClient()->m_Snap.m_pGameInfoObj->m_RoundStartTick;
62 TickRatio = mix<double>(a: 0, b: CurTick - EnvelopeTick, amount: (double)Client()->IntraGameTick(Conn: g_Config.m_ClDummy));
63 }
64 else
65 {
66 EnvelopeTick = Client()->PredGameTick(Conn: g_Config.m_ClDummy) - 1 - GameClient()->m_Snap.m_pGameInfoObj->m_RoundStartTick;
67 TickRatio = (double)Client()->PredIntraGameTick(Conn: g_Config.m_ClDummy);
68 }
69 Time = duration_cast<nanoseconds>(d: TickRatio * s_NanosPerTick) + EnvelopeTick * s_NanosPerTick;
70 }
71 else
72 {
73 Time = nanoseconds::zero();
74 }
75 }
76
77 CRenderMap::RenderEvalEnvelope(pPoints: m_pEnvelopePoints.get(), TimeNanos: Time + milliseconds(TimeOffsetMillis), Result, Channels);
78}
79