2023-08-14 01:20:27 +02:00
|
|
|
/*
|
|
|
|
* TurnTimerHandler.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "TurnTimerHandler.h"
|
|
|
|
#include "CGameHandler.h"
|
2023-08-21 23:49:50 +02:00
|
|
|
#include "battles/BattleProcessor.h"
|
|
|
|
#include "queries/QueriesProcessor.h"
|
2023-08-22 17:45:13 +02:00
|
|
|
#include "processors/TurnOrderProcessor.h"
|
2023-08-14 21:31:44 +02:00
|
|
|
#include "../lib/battle/BattleInfo.h"
|
2023-08-14 01:20:27 +02:00
|
|
|
#include "../lib/gameState/CGameState.h"
|
2023-08-14 21:31:44 +02:00
|
|
|
#include "../lib/CPlayerState.h"
|
|
|
|
#include "../lib/CStack.h"
|
2023-08-14 01:20:27 +02:00
|
|
|
#include "../lib/StartInfo.h"
|
2023-08-21 23:49:50 +02:00
|
|
|
#include "../lib/NetPacks.h"
|
2023-08-14 01:20:27 +02:00
|
|
|
|
|
|
|
TurnTimerHandler::TurnTimerHandler(CGameHandler & gh):
|
|
|
|
gameHandler(gh)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-26 03:39:29 +02:00
|
|
|
void TurnTimerHandler::onGameplayStart(PlayerColor player)
|
2023-08-14 01:20:27 +02:00
|
|
|
{
|
|
|
|
if(const auto * si = gameHandler.getStartInfo())
|
|
|
|
{
|
2023-08-27 23:59:47 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(mx);
|
|
|
|
timers[player] = si->turnTimerInfo;
|
|
|
|
timers[player].turnTimer = 0;
|
2023-08-14 01:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 03:39:29 +02:00
|
|
|
void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
|
2023-08-14 01:20:27 +02:00
|
|
|
{
|
|
|
|
if(const auto * si = gameHandler.getStartInfo())
|
|
|
|
{
|
|
|
|
if(si->turnTimerInfo.isEnabled())
|
|
|
|
{
|
2023-08-27 23:10:04 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(mx);
|
2023-08-26 03:39:29 +02:00
|
|
|
timers[player].baseTimer += timers[player].turnTimer;
|
|
|
|
timers[player].turnTimer = si->turnTimerInfo.turnTimer;
|
2023-08-14 01:20:27 +02:00
|
|
|
|
|
|
|
TurnTimeUpdate ttu;
|
2023-08-26 03:39:29 +02:00
|
|
|
ttu.player = player;
|
|
|
|
ttu.turnTimer = timers[player];
|
2023-08-14 21:31:44 +02:00
|
|
|
gameHandler.sendAndApply(&ttu);
|
2023-08-14 01:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 03:39:29 +02:00
|
|
|
void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
|
2023-08-14 01:20:27 +02:00
|
|
|
{
|
|
|
|
const auto * gs = gameHandler.gameState();
|
|
|
|
const auto * si = gameHandler.getStartInfo();
|
|
|
|
if(!si || !gs)
|
|
|
|
return;
|
|
|
|
|
2023-08-26 03:39:29 +02:00
|
|
|
auto & state = gs->players.at(player);
|
|
|
|
|
2023-08-19 17:00:08 +02:00
|
|
|
if(state.human && si->turnTimerInfo.isEnabled() && !gs->curB)
|
2023-08-14 01:20:27 +02:00
|
|
|
{
|
2023-08-27 23:10:04 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(mx);
|
2023-08-26 03:39:29 +02:00
|
|
|
if(timers[player].turnTimer > 0)
|
2023-08-14 01:20:27 +02:00
|
|
|
{
|
2023-08-26 03:39:29 +02:00
|
|
|
timers[player].turnTimer -= waitTime;
|
|
|
|
int frequency = (timers[player].turnTimer > turnTimePropagateThreshold ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit);
|
2023-08-14 01:20:27 +02:00
|
|
|
|
|
|
|
if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
|
2023-08-26 03:39:29 +02:00
|
|
|
&& timers[player].turnTimer % frequency == 0)
|
2023-08-14 01:20:27 +02:00
|
|
|
{
|
|
|
|
TurnTimeUpdate ttu;
|
|
|
|
ttu.player = state.color;
|
2023-08-26 03:39:29 +02:00
|
|
|
ttu.turnTimer = timers[player];
|
2023-08-14 21:31:44 +02:00
|
|
|
gameHandler.sendAndApply(&ttu);
|
2023-08-14 01:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-26 03:39:29 +02:00
|
|
|
else if(timers[player].baseTimer > 0)
|
2023-08-14 01:20:27 +02:00
|
|
|
{
|
2023-08-26 03:39:29 +02:00
|
|
|
timers[player].turnTimer = timers[player].baseTimer;
|
|
|
|
timers[player].baseTimer = 0;
|
|
|
|
onPlayerMakingTurn(player, 0);
|
2023-08-14 01:20:27 +02:00
|
|
|
}
|
2023-08-24 20:35:01 +02:00
|
|
|
else if(!gameHandler.queries->topQuery(state.color)) //wait for replies to avoid pending queries
|
2023-08-24 12:36:35 +02:00
|
|
|
gameHandler.turnOrder->onPlayerEndsTurn(state.color);
|
2023-08-14 01:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-14 21:31:44 +02:00
|
|
|
|
2023-08-22 12:14:50 +02:00
|
|
|
void TurnTimerHandler::onBattleStart()
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-22 12:14:50 +02:00
|
|
|
const auto * gs = gameHandler.gameState();
|
|
|
|
const auto * si = gameHandler.getStartInfo();
|
|
|
|
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
|
|
|
|
return;
|
|
|
|
|
2023-08-27 23:10:04 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(mx);
|
|
|
|
|
2023-08-22 12:14:50 +02:00
|
|
|
auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
|
|
|
|
auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
|
|
|
|
|
|
|
|
for(auto i : {attacker, defender})
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-22 12:14:50 +02:00
|
|
|
if(i.isValidPlayer())
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-26 03:39:29 +02:00
|
|
|
timers[i].battleTimer = si->turnTimerInfo.battleTimer;
|
|
|
|
timers[i].creatureTimer = si->turnTimerInfo.creatureTimer;
|
|
|
|
|
2023-08-14 21:31:44 +02:00
|
|
|
TurnTimeUpdate ttu;
|
2023-08-26 03:39:29 +02:00
|
|
|
ttu.player = i;
|
|
|
|
ttu.turnTimer = timers[i];
|
2023-08-14 21:31:44 +02:00
|
|
|
gameHandler.sendAndApply(&ttu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 23:49:50 +02:00
|
|
|
void TurnTimerHandler::onBattleNextStack(const CStack & stack)
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-21 23:49:50 +02:00
|
|
|
const auto * gs = gameHandler.gameState();
|
|
|
|
const auto * si = gameHandler.getStartInfo();
|
2023-08-26 03:39:29 +02:00
|
|
|
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
|
2023-08-21 23:49:50 +02:00
|
|
|
return;
|
|
|
|
|
2023-08-27 23:10:04 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(mx);
|
|
|
|
|
2023-08-26 03:39:29 +02:00
|
|
|
auto player = stack.getOwner();
|
2023-08-21 23:49:50 +02:00
|
|
|
|
2023-08-26 03:39:29 +02:00
|
|
|
if(!player.isValidPlayer())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(timers[player].battleTimer < si->turnTimerInfo.battleTimer)
|
|
|
|
timers[player].battleTimer = timers[player].creatureTimer;
|
|
|
|
timers[player].creatureTimer = si->turnTimerInfo.creatureTimer;
|
|
|
|
|
|
|
|
TurnTimeUpdate ttu;
|
|
|
|
ttu.player = player;
|
|
|
|
ttu.turnTimer = timers[player];
|
|
|
|
gameHandler.sendAndApply(&ttu);
|
2023-08-14 21:31:44 +02:00
|
|
|
}
|
|
|
|
|
2023-08-21 23:49:50 +02:00
|
|
|
void TurnTimerHandler::onBattleLoop(int waitTime)
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
|
|
|
const auto * gs = gameHandler.gameState();
|
|
|
|
const auto * si = gameHandler.getStartInfo();
|
|
|
|
if(!si || !gs || !gs->curB)
|
|
|
|
return;
|
|
|
|
|
2023-08-27 23:10:04 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(mx);
|
|
|
|
|
2023-08-21 23:49:50 +02:00
|
|
|
const auto * stack = gs->curB.get()->battleGetStackByID(gs->curB->getActiveStackID());
|
2023-08-22 00:08:42 +02:00
|
|
|
if(!stack || !stack->getOwner().isValidPlayer())
|
2023-08-21 23:49:50 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto & state = gs->players.at(gs->curB->getSidePlayer(stack->unitSide()));
|
|
|
|
|
2023-08-26 03:39:29 +02:00
|
|
|
auto turnTimerUpdateApplier = [&](TurnTimerInfo & tTimer, int waitTime)
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-21 23:49:50 +02:00
|
|
|
if(tTimer.creatureTimer > 0)
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-26 03:39:29 +02:00
|
|
|
tTimer.creatureTimer -= waitTime;
|
2023-08-26 03:45:45 +02:00
|
|
|
int frequency = (tTimer.creatureTimer > turnTimePropagateThreshold
|
|
|
|
&& si->turnTimerInfo.creatureTimer - tTimer.creatureTimer > turnTimePropagateThreshold)
|
|
|
|
? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
|
2023-08-14 21:31:44 +02:00
|
|
|
|
|
|
|
if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
|
2023-08-26 03:39:29 +02:00
|
|
|
&& tTimer.creatureTimer % frequency == 0)
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
|
|
|
TurnTimeUpdate ttu;
|
|
|
|
ttu.player = state.color;
|
2023-08-26 03:39:29 +02:00
|
|
|
ttu.turnTimer = tTimer;
|
2023-08-14 21:31:44 +02:00
|
|
|
gameHandler.sendAndApply(&ttu);
|
|
|
|
}
|
2023-08-21 23:49:50 +02:00
|
|
|
return true;
|
2023-08-14 21:31:44 +02:00
|
|
|
}
|
2023-08-21 23:49:50 +02:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if(state.human && si->turnTimerInfo.isBattleEnabled())
|
|
|
|
{
|
2023-08-26 03:39:29 +02:00
|
|
|
if(!turnTimerUpdateApplier(timers[state.color], waitTime))
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-26 03:39:29 +02:00
|
|
|
if(timers[state.color].battleTimer > 0)
|
2023-08-21 23:49:50 +02:00
|
|
|
{
|
2023-08-26 03:39:29 +02:00
|
|
|
timers[state.color].creatureTimer = timers[state.color].battleTimer;
|
|
|
|
timers[state.color].battleTimer = 0;
|
|
|
|
turnTimerUpdateApplier(timers[state.color], 0);
|
2023-08-21 23:49:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BattleAction doNothing;
|
|
|
|
doNothing.actionType = EActionType::DEFEND;
|
|
|
|
doNothing.side = stack->unitSide();
|
|
|
|
doNothing.stackNumber = stack->unitId();
|
|
|
|
gameHandler.battles->makePlayerBattleAction(state.color, doNothing);
|
|
|
|
}
|
2023-08-14 21:31:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|