Ticket #3309: t3309_cap_expected_simTime_v3.patch

File t3309_cap_expected_simTime_v3.patch, 2.1 KB (added by elexis, 9 years ago)

Rewrote the patch this way, so its easier to read it and added a comment. Today on IRC there was a discussion about this patch with Philip. We concluded that it should work this way, but it should be tested more. A lagging client should be able to fast forward for a brief amount of time (at least 2 * turn length) to catch up with the other clients.

  • source/network/NetTurnManager.cpp

     
    7777    m_CurrentTurn = newCurrentTurn;
    7878    m_ReadyTurn = newReadyTurn;
    7979    m_DeltaSimTime = 0;
    8080    size_t queuedCommandsSize = m_QueuedCommands.size();
    8181    m_QueuedCommands.clear();
    8282    m_QueuedCommands.resize(queuedCommandsSize);
    8383}
    8484
    8585void CNetTurnManager::SetPlayerID(int playerId)
    8686{
    8787    m_PlayerId = playerId;
    8888}
    8989
    9090bool CNetTurnManager::WillUpdate(float simFrameLength)
    9191{
    9292    // Keep this in sync with the return value of Update()
    9393
    9494    if (m_DeltaSimTime + simFrameLength < 0)
    9595        return false;
    9696
    9797    if (m_ReadyTurn <= m_CurrentTurn)
    9898        return false;
    9999
    100100    return true;
    101101}
    102102
    103103bool CNetTurnManager::Update(float simFrameLength, size_t maxTurns)
    104104{
    105105    m_DeltaSimTime += simFrameLength;
    106106
     107    // If the game becomes laggy, m_DeltaSimTime increases progressively.
     108    // The engine will fast forward accordingly to catch up.
     109    // To keep the game playable, stop fast forwarding after 2 turn lengths.
     110    m_DeltaSimTime = std::min(m_DeltaSimTime, 2.0f * m_TurnLength / 1000.0f);
     111
    107112    // If we haven't reached the next turn yet, do nothing
    108113    if (m_DeltaSimTime < 0)
    109114        return false;
    110115
    111116    NETTURN_LOG((L"Update current=%d ready=%d\n", m_CurrentTurn, m_ReadyTurn));
    112117
    113118    // Check that the next turn is ready for execution
    114119    if (m_ReadyTurn <= m_CurrentTurn)
    115120    {
    116121        // Oops, we wanted to start the next turn but it's not ready yet -
    117122        // there must be too much network lag.
    118123        // TODO: complain to the user.
    119124        // TODO: send feedback to the server to increase the turn length.
    120125
    121126        // Reset the next-turn timer to 0 so we try again next update but
    122127        // so we don't rush to catch up in subsequent turns.
    123128        // TODO: we should do clever rate adjustment instead of just pausing like this.
    124129        m_DeltaSimTime = 0;
    125130
    126131        return false;
    127132    }
    128133
    129134    maxTurns = std::max((size_t)1, maxTurns); // always do at least one turn
    130135
    131136    for (size_t i = 0; i < maxTurns; ++i)
    132137    {
    133138        // Check that we've reached the i'th next turn
    134139        if (m_DeltaSimTime < 0)
    135140            break;
    136141