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

Timers fixes

This commit is contained in:
nordsoft 2023-08-30 03:33:59 +04:00
parent 9a42abe2a7
commit cc3ca9a41e
5 changed files with 64 additions and 56 deletions

View File

@ -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);
}
}
}

View File

@ -46,6 +46,8 @@ private:
std::set<int> notifications;
std::shared_ptr<DrawRect> buildDrawRect(const JsonNode & config) const;
void updateTimer(PlayerColor player, uint32_t msPassed);
public:

View File

@ -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;

View File

@ -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);
}

View File

@ -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);