diff --git a/client/adventureMap/TurnTimerWidget.cpp b/client/adventureMap/TurnTimerWidget.cpp index bd9606d62..b47d111da 100644 --- a/client/adventureMap/TurnTimerWidget.cpp +++ b/client/adventureMap/TurnTimerWidget.cpp @@ -97,60 +97,63 @@ void TurnTimerWidget::setTime(PlayerColor player, int time) } } +void TurnTimerWidget::updateTimer(PlayerColor player, uint32_t msPassed) +{ + const auto & time = LOCPLINT->cb->getPlayerTurnTime(player); + if(time.isActive) + cachedTurnTime -= msPassed; + + if(cachedTurnTime < 0) + cachedTurnTime = 0; //do not go below zero + + if(lastPlayer != player) + { + lastPlayer = player; + lastTurnTime = 0; + } + + auto timeCheckAndUpdate = [&](int time) + { + if(time / 1000 != lastTurnTime / 1000) + { + //do not update timer on this tick + lastTurnTime = time; + cachedTurnTime = time; + } + else + setTime(player, cachedTurnTime); + }; + + auto * playerInfo = LOCPLINT->cb->getPlayer(player); + if(player.isValidPlayer() || (playerInfo && playerInfo->isHuman())) + { + if(time.isBattle) + timeCheckAndUpdate(time.creatureTimer); + else + timeCheckAndUpdate(time.turnTimer); + } + else + timeCheckAndUpdate(0); +} + void TurnTimerWidget::tick(uint32_t msPassed) { if(!LOCPLINT || !LOCPLINT->cb) return; - for(PlayerColor p(0); p < PlayerColor::PLAYER_LIMIT; ++p) + if(LOCPLINT->battleInt) { - auto player = p; - if(LOCPLINT->battleInt) - { - if(auto * stack = LOCPLINT->battleInt->stacksController->getActiveStack()) - player = stack->getOwner(); - else - continue; - if(p != player) - continue; - } - else if(!LOCPLINT->cb->isPlayerMakingTurn(player)) - continue; - - auto time = LOCPLINT->cb->getPlayerTurnTime(player); - if(time.isActive) - cachedTurnTime -= msPassed; - - if(cachedTurnTime < 0) - cachedTurnTime = 0; //do not go below zero - - if(lastPlayer != player) - { - lastPlayer = player; - lastTurnTime = 0; - } - - auto timeCheckAndUpdate = [&](int time) - { - if(time / 1000 != lastTurnTime / 1000) - { - //do not update timer on this tick - lastTurnTime = time; - cachedTurnTime = time; - } - else - setTime(player, cachedTurnTime); - }; - - auto * playerInfo = LOCPLINT->cb->getPlayer(player); - if(player.isValidPlayer() || (playerInfo && playerInfo->isHuman())) - { - if(time.isBattle) - timeCheckAndUpdate(time.creatureTimer); - else - timeCheckAndUpdate(time.turnTimer); - } + if(auto * stack = LOCPLINT->battleInt->stacksController->getActiveStack()) + updateTimer(stack->getOwner(), msPassed); else - timeCheckAndUpdate(0); + updateTimer(PlayerColor::NEUTRAL, msPassed); + } + else + { + for(PlayerColor p(0); p < PlayerColor::PLAYER_LIMIT; ++p) + { + if(LOCPLINT->cb->isPlayerMakingTurn(p)) + updateTimer(p, msPassed); + } } } diff --git a/client/adventureMap/TurnTimerWidget.h b/client/adventureMap/TurnTimerWidget.h index ccc801eb4..f8b4b97fc 100644 --- a/client/adventureMap/TurnTimerWidget.h +++ b/client/adventureMap/TurnTimerWidget.h @@ -46,6 +46,8 @@ private: std::set notifications; std::shared_ptr buildDrawRect(const JsonNode & config) const; + + void updateTimer(PlayerColor player, uint32_t msPassed); public: diff --git a/lib/TurnTimerInfo.h b/lib/TurnTimerInfo.h index 843e0c0de..c708345b4 100644 --- a/lib/TurnTimerInfo.h +++ b/lib/TurnTimerInfo.h @@ -19,7 +19,7 @@ struct DLL_LINKAGE TurnTimerInfo int battleTimer = 0; //in ms, counts down during battles when creature timer runs out int creatureTimer = 0; //in ms, counts down when player is choosing action in battle - bool isActive = true; //should be paused if set to false + bool isActive = false; //is being counting down bool isBattle = false; //indicator for current timer mode bool isEnabled() const; diff --git a/server/TurnTimerHandler.cpp b/server/TurnTimerHandler.cpp index 7473b2f8c..9dae655af 100644 --- a/server/TurnTimerHandler.cpp +++ b/server/TurnTimerHandler.cpp @@ -187,14 +187,18 @@ void TurnTimerHandler::onBattleEnd() for(auto i : {attacker, defender}) { - if(i.isValidPlayer() && !pvpBattle) + if(i.isValidPlayer()) { auto & timer = timers[i]; timer.isBattle = false; - if(si->turnTimerInfo.baseTimer && timer.baseTimer == 0) - timer.baseTimer = timer.creatureTimer; - else if(si->turnTimerInfo.turnTimer && timer.turnTimer == 0) - timer.turnTimer = timer.creatureTimer; + + if(!pvpBattle) + { + if(si->turnTimerInfo.baseTimer && timer.baseTimer == 0) + timer.baseTimer = timer.creatureTimer; + else if(si->turnTimerInfo.turnTimer && timer.turnTimer == 0) + timer.turnTimer = timer.creatureTimer; + } sendTimerUpdate(i); } diff --git a/server/battles/BattleResultProcessor.cpp b/server/battles/BattleResultProcessor.cpp index f983b9797..bf0827949 100644 --- a/server/battles/BattleResultProcessor.cpp +++ b/server/battles/BattleResultProcessor.cpp @@ -263,9 +263,8 @@ void BattleResultProcessor::endBattle(int3 tile, const CGHeroInstance * heroAtta otherBattleQuery->result = battleQuery->result; } - gameHandler->sendAndApply(battleResult.get()); //after this point casualties objects are destroyed - gameHandler->turnTimerHandler.onBattleEnd(); + gameHandler->sendAndApply(battleResult.get()); //after this point casualties objects are destroyed if (battleResult->queryID == QueryID::NONE) endBattleConfirm(gameHandler->gameState()->curB);