77 | 77 | m_CurrentTurn = newCurrentTurn; |
78 | 78 | m_ReadyTurn = newReadyTurn; |
79 | 79 | m_DeltaSimTime = 0; |
80 | 80 | size_t queuedCommandsSize = m_QueuedCommands.size(); |
81 | 81 | m_QueuedCommands.clear(); |
82 | 82 | m_QueuedCommands.resize(queuedCommandsSize); |
83 | 83 | } |
84 | 84 | |
85 | 85 | void CNetTurnManager::SetPlayerID(int playerId) |
86 | 86 | { |
87 | 87 | m_PlayerId = playerId; |
88 | 88 | } |
89 | 89 | |
90 | 90 | bool CNetTurnManager::WillUpdate(float simFrameLength) |
91 | 91 | { |
92 | 92 | // Keep this in sync with the return value of Update() |
93 | 93 | |
94 | 94 | if (m_DeltaSimTime + simFrameLength < 0) |
95 | 95 | return false; |
96 | 96 | |
97 | 97 | if (m_ReadyTurn <= m_CurrentTurn) |
98 | 98 | return false; |
99 | 99 | |
100 | 100 | return true; |
101 | 101 | } |
102 | 102 | |
103 | 103 | bool CNetTurnManager::Update(float simFrameLength, size_t maxTurns) |
104 | 104 | { |
105 | 105 | m_DeltaSimTime += simFrameLength; |
106 | 106 | |
107 | 112 | // If we haven't reached the next turn yet, do nothing |
108 | 113 | if (m_DeltaSimTime < 0) |
109 | 114 | return false; |
110 | 115 | |
111 | 116 | NETTURN_LOG((L"Update current=%d ready=%d\n", m_CurrentTurn, m_ReadyTurn)); |
112 | 117 | |
113 | 118 | // Check that the next turn is ready for execution |
114 | 119 | if (m_ReadyTurn <= m_CurrentTurn) |
115 | 120 | { |
116 | 121 | // Oops, we wanted to start the next turn but it's not ready yet - |
117 | 122 | // there must be too much network lag. |
118 | 123 | // TODO: complain to the user. |
119 | 124 | // TODO: send feedback to the server to increase the turn length. |
120 | 125 | |
121 | 126 | // Reset the next-turn timer to 0 so we try again next update but |
122 | 127 | // so we don't rush to catch up in subsequent turns. |
123 | 128 | // TODO: we should do clever rate adjustment instead of just pausing like this. |
124 | 129 | m_DeltaSimTime = 0; |
125 | 130 | |
126 | 131 | return false; |
127 | 132 | } |
128 | 133 | |
129 | 134 | maxTurns = std::max((size_t)1, maxTurns); // always do at least one turn |
130 | 135 | |
131 | 136 | for (size_t i = 0; i < maxTurns; ++i) |
132 | 137 | { |
133 | 138 | // Check that we've reached the i'th next turn |
134 | 139 | if (m_DeltaSimTime < 0) |
135 | 140 | break; |
136 | 141 | |