1

Topic: CSmoothTime

Hello,
I don't understand what CSmoothTime does. Can somebody explain what purpose this class serves and how it works?
Thank you

2

Re: CSmoothTime

kiwon0905 wrote:

Hello,
I don't understand what CSmoothTime does. Can somebody explain what purpose this class serves and how it works?
Thank you

hi kiwon0905,
this class is used to maintain the server tick time in client side, in teeworld's source code, it used to declare two variables: m_GameTime and m_PredictedTime.
when the client side received two snapshots from server , it will init it.

if(m_RecivedSnapshots == 2)
{
    // start at 200ms and work from there
    m_PredictedTime.Init(GameTick*time_freq()/50);
    m_PredictedTime.SetAdjustSpeed(1, 1000.0f);
    m_GameTime.Init((GameTick-1)*time_freq()/50);
    m_aSnapshots[SNAP_PREV] = m_SnapshotStorage.m_pFirst;
    m_aSnapshots[SNAP_CURRENT] = m_SnapshotStorage.m_pLast;
    m_LocalStartTime = time_get();
    SetState(IClient::STATE_ONLINE);
    DemoRecorder_HandleAutoStart();
}

as the game running, it will calibrate the time through a specific method. just like this:

if(m_RecivedSnapshots > 2)
{
    int64 Now = m_GameTime.Get(time_get());
    int64 TickStart = GameTick*time_freq()/50;
    int64 TimeLeft = (TickStart-Now)*1000 / time_freq();
    m_GameTime.Update(&m_GametimeMarginGraph, (GameTick-1)*time_freq()/50, TimeLeft, 0);
}

and this:

else if(Msg == NETMSG_INPUTTIMING)
{
    int InputPredTick = Unpacker.GetInt();
    int TimeLeft = Unpacker.GetInt();
    // adjust our prediction time
    int64 Target = 0;
    for(int k = 0; k < 200; k++)
    {
        if(m_aInputs[k].m_Tick == InputPredTick)
        {
            Target = m_aInputs[k].m_PredictedTime + (time_get() - m_aInputs[k].m_Time);
            Target = Target - (int64)(((TimeLeft-PREDICTION_MARGIN)/1000.0f)*time_freq());
            break;
        }
    }

    if (Target)
    {
        m_PredictedTime.Update(&m_InputtimeMarginGraph, Target, TimeLeft, 1);
    }
}

unfortunately, the CSmoothTime is relative with the game's synchronization mechanism, if you want to have a complete understanding how it works, you should also know the game's synchronization mechanism.

ok, in teeworlds, it use snapshot to synchronize the state of game entities.you can read the code: src/engine/shared/snapshot.h/cpp, perhaps a little hard to understand at first, as reading the more and more code, you will be have a better understanding for it. smile

for snapshot synchronization, i will recommend you to read this article:
Snapshots and Interpolation
Snapshot Compression

this article will help you to understand the synchronization mechanism in teeworlds's source code.

3

Re: CSmoothTime

thank you crazyatom,

I understand the snapshot system and I understand CSmoothTime is used to synchronize the time, but I don't understand the inner workings of it.

From what I understand, m_GameTime is adjusted whenever the client receives a snapshot from the server and m_PredictedTime is adjusted when the client receives a NETMSG_INPUTTIMING packet.

Do you know how CSmoothTime::update() and CSmoothTime::get() works?
If not, is there some other person or place I can ask this question?