1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Use precision clocks for timer

This commit is contained in:
nordsoft 2023-08-28 03:14:58 +04:00
parent 7dc1717ec6
commit 2c61d1b23f
2 changed files with 11 additions and 6 deletions

View File

@ -999,18 +999,23 @@ void CGameHandler::run(bool resume)
turnOrder->onGameStarted();
//wait till game is done
auto clockLast = std::chrono::high_resolution_clock::now();
while(lobby->getState() == EServerState::GAMEPLAY)
{
const auto clockDuration = std::chrono::high_resolution_clock::now() - clockLast;
const int timePassed = std::chrono::duration_cast<std::chrono::milliseconds>(clockDuration).count();
clockLast += clockDuration;
const int waitTime = 100; //ms
for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
if(gs->isPlayerMakingTurn(player))
turnTimerHandler.onPlayerMakingTurn(player, waitTime);
turnTimerHandler.onPlayerMakingTurn(player, timePassed);
if(gs->curB)
turnTimerHandler.onBattleLoop(waitTime);
turnTimerHandler.onBattleLoop(timePassed);
boost::this_thread::sleep_for(boost::chrono::milliseconds(waitTime));
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
}
}

View File

@ -72,7 +72,7 @@ void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
int frequency = (timers[player].turnTimer > turnTimePropagateThreshold ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit);
if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
&& timers[player].turnTimer % frequency == 0)
&& timers[player].turnTimer / 100 * 100 % frequency == 0)
{
TurnTimeUpdate ttu;
ttu.player = state.color;
@ -132,7 +132,7 @@ void TurnTimerHandler::onBattleNextStack(const CStack & stack)
if(!player.isValidPlayer())
return;
if(timers[player].battleTimer < si->turnTimerInfo.battleTimer)
if(timers[player].battleTimer == 0)
timers[player].battleTimer = timers[player].creatureTimer;
timers[player].creatureTimer = si->turnTimerInfo.creatureTimer;
@ -177,7 +177,7 @@ void TurnTimerHandler::onBattleLoop(int waitTime)
? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
&& tTimer.creatureTimer % frequency == 0)
&& (tTimer.creatureTimer / 100 * 100 % frequency) == 0)
{
TurnTimeUpdate ttu;
ttu.player = state.color;