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

309 lines
8.2 KiB
C++
Raw Normal View History

/*
* 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"
#include "processors/TurnOrderProcessor.h"
2023-08-14 21:31:44 +02:00
#include "../lib/battle/BattleInfo.h"
#include "../lib/gameState/CGameState.h"
2023-08-14 21:31:44 +02:00
#include "../lib/CPlayerState.h"
#include "../lib/CStack.h"
#include "../lib/StartInfo.h"
2023-08-21 23:49:50 +02:00
#include "../lib/NetPacks.h"
TurnTimerHandler::TurnTimerHandler(CGameHandler & gh):
gameHandler(gh)
{
}
2023-08-26 03:39:29 +02:00
void TurnTimerHandler::onGameplayStart(PlayerColor player)
{
2023-08-29 13:48:42 +02:00
std::lock_guard<std::recursive_mutex> guard(mx);
if(const auto * si = gameHandler.getStartInfo())
{
timers[player] = si->turnTimerInfo;
timers[player].turnTimer = 0;
timers[player].isActive = true;
timers[player].isBattle = false;
lastUpdate[player] = std::numeric_limits<int>::max();
}
}
2023-08-28 02:40:35 +02:00
void TurnTimerHandler::setTimerEnabled(PlayerColor player, bool enabled)
{
std::lock_guard<std::recursive_mutex> guard(mx);
assert(player.isValidPlayer());
timers[player].isActive = enabled;
2023-08-30 01:44:09 +02:00
sendTimerUpdate(player);
2023-08-30 00:17:29 +02:00
}
void TurnTimerHandler::sendTimerUpdate(PlayerColor player)
{
TurnTimeUpdate ttu;
ttu.player = player;
ttu.turnTimer = timers[player];
2023-08-30 00:17:29 +02:00
gameHandler.sendAndApply(&ttu);
lastUpdate[player] = 0;
2023-08-28 02:40:35 +02:00
}
2023-08-26 03:39:29 +02:00
void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
{
2023-08-29 13:48:42 +02:00
std::lock_guard<std::recursive_mutex> guard(mx);
if(const auto * si = gameHandler.getStartInfo())
{
if(si->turnTimerInfo.isEnabled())
{
auto & timer = timers[player];
2023-08-28 02:45:35 +02:00
if(si->turnTimerInfo.baseTimer > 0)
timer.baseTimer += timer.turnTimer;
timer.turnTimer = si->turnTimerInfo.turnTimer;
2023-08-30 00:17:29 +02:00
sendTimerUpdate(player);
}
}
}
2023-08-29 13:48:42 +02:00
void TurnTimerHandler::update(int waitTime)
{
std::lock_guard<std::recursive_mutex> guard(mx);
if(const auto * gs = gameHandler.gameState())
{
for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
if(gs->isPlayerMakingTurn(player))
onPlayerMakingTurn(player, waitTime);
if(gs->curB)
onBattleLoop(waitTime);
}
}
2023-08-30 00:17:29 +02:00
bool TurnTimerHandler::timerCountDown(int & timer, int initialTimer, PlayerColor player, int waitTime)
{
if(timer > 0)
{
timer -= waitTime;
lastUpdate[player] += waitTime;
2023-08-30 00:17:29 +02:00
int frequency = (timer > turnTimePropagateThreshold
&& initialTimer - timer > turnTimePropagateThreshold)
? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
if(lastUpdate[player] >= frequency)
2023-08-30 00:17:29 +02:00
sendTimerUpdate(player);
return true;
}
return false;
}
2023-08-26 03:39:29 +02:00
void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
{
2023-08-29 13:48:42 +02:00
std::lock_guard<std::recursive_mutex> guard(mx);
const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo();
2023-08-30 00:17:29 +02:00
if(!si || !gs || !si->turnTimerInfo.isEnabled())
return;
auto & timer = timers[player];
2023-08-30 00:17:29 +02:00
const auto * state = gameHandler.getPlayerState(player);
if(state && state->human && timer.isActive && !timer.isBattle && state->status == EPlayerStatus::INGAME)
{
if(!timerCountDown(timer.turnTimer, si->turnTimerInfo.turnTimer, player, waitTime))
{
if(timer.baseTimer > 0)
{
timer.turnTimer = timer.baseTimer;
timer.baseTimer = 0;
2023-08-30 00:17:29 +02:00
onPlayerMakingTurn(player, 0);
}
2023-08-30 00:17:29 +02:00
else if(!gameHandler.queries->topQuery(state->color)) //wait for replies to avoid pending queries
gameHandler.turnOrder->onPlayerEndsTurn(state->color);
}
}
}
2023-08-14 21:31:44 +02:00
2023-08-30 00:17:29 +02:00
bool TurnTimerHandler::isPvpBattle() const
{
const auto * gs = gameHandler.gameState();
auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
if(attacker.isValidPlayer() && defender.isValidPlayer())
{
const auto * attackerState = gameHandler.getPlayerState(attacker);
const auto * defenderState = gameHandler.getPlayerState(defender);
if(attackerState && defenderState && attackerState->human && defenderState->human)
return true;
}
return false;
}
void TurnTimerHandler::onBattleStart()
2023-08-14 21:31:44 +02:00
{
2023-08-29 13:48:42 +02:00
std::lock_guard<std::recursive_mutex> guard(mx);
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
auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
2023-08-30 00:17:29 +02:00
bool pvpBattle = isPvpBattle();
for(auto i : {attacker, defender})
2023-08-14 21:31:44 +02:00
{
if(i.isValidPlayer())
2023-08-14 21:31:44 +02:00
{
auto & timer = timers[i];
timer.isBattle = true;
timer.battleTimer = (pvpBattle ? si->turnTimerInfo.battleTimer : 0);
timer.creatureTimer = (pvpBattle ? si->turnTimerInfo.creatureTimer : si->turnTimerInfo.battleTimer);
2023-08-26 03:39:29 +02:00
2023-08-30 00:17:29 +02:00
sendTimerUpdate(i);
2023-08-14 21:31:44 +02:00
}
}
}
2023-08-30 00:17:29 +02:00
void TurnTimerHandler::onBattleEnd()
2023-08-14 21:31:44 +02:00
{
2023-08-29 13:48:42 +02:00
std::lock_guard<std::recursive_mutex> guard(mx);
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-30 00:17:29 +02:00
auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
2023-08-21 23:49:50 +02:00
2023-08-30 00:17:29 +02:00
bool pvpBattle = isPvpBattle();
2023-08-21 23:49:50 +02:00
2023-08-30 00:17:29 +02:00
for(auto i : {attacker, defender})
{
2023-08-30 01:33:59 +02:00
if(i.isValidPlayer())
2023-08-30 00:17:29 +02:00
{
auto & timer = timers[i];
timer.isBattle = false;
2023-08-30 01:33:59 +02:00
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;
}
2023-08-30 00:17:29 +02:00
sendTimerUpdate(i);
}
}
}
void TurnTimerHandler::onBattleNextStack(const CStack & stack)
{
std::lock_guard<std::recursive_mutex> guard(mx);
const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo();
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
2023-08-26 03:39:29 +02:00
return;
2023-08-30 00:17:29 +02:00
if(isPvpBattle())
{
auto player = stack.getOwner();
2023-08-26 03:39:29 +02:00
auto & timer = timers[player];
if(timer.battleTimer == 0)
timer.battleTimer = timer.creatureTimer;
timer.creatureTimer = si->turnTimerInfo.creatureTimer;
2023-08-26 03:39:29 +02:00
2023-08-30 00:17:29 +02:00
sendTimerUpdate(player);
}
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
{
2023-08-29 13:48:42 +02:00
std::lock_guard<std::recursive_mutex> guard(mx);
2023-08-14 21:31:44 +02:00
const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo();
2023-08-28 02:40:35 +02:00
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
2023-08-14 21:31:44 +02:00
return;
2023-08-28 00:00:02 +02:00
ui8 side = 0;
const CStack * stack = nullptr;
2023-08-30 00:17:29 +02:00
bool isTactisPhase = gs->curB->battleTacticDist() > 0;
2023-08-28 00:00:02 +02:00
if(isTactisPhase)
2023-08-30 00:17:29 +02:00
side = gs->curB->battleGetTacticsSide();
2023-08-28 00:00:02 +02:00
else
{
2023-08-30 00:17:29 +02:00
stack = gs->curB->battleGetStackByID(gs->curB->getActiveStackID());
2023-08-28 00:00:02 +02:00
if(!stack || !stack->getOwner().isValidPlayer())
return;
side = stack->unitSide();
}
2023-08-21 23:49:50 +02:00
2023-08-30 00:17:29 +02:00
auto player = gs->curB->getSidePlayer(side);
if(!player.isValidPlayer())
return;
2023-08-21 23:49:50 +02:00
2023-08-30 00:17:29 +02:00
const auto * state = gameHandler.getPlayerState(player);
if(!state || state->status != EPlayerStatus::INGAME || !state->human)
return;
auto & timer = timers[player];
if(timer.isActive && timer.isBattle && !timerCountDown(timer.creatureTimer, si->turnTimerInfo.creatureTimer, player, waitTime))
2023-08-14 21:31:44 +02:00
{
2023-08-30 00:17:29 +02:00
if(isPvpBattle())
2023-08-14 21:31:44 +02:00
{
if(timer.battleTimer > 0)
2023-08-14 21:31:44 +02:00
{
timer.creatureTimer = timer.battleTimer;
timerCountDown(timer.creatureTimer, timer.battleTimer, player, 0);
timer.battleTimer = 0;
2023-08-30 00:17:29 +02:00
}
else
{
BattleAction doNothing;
doNothing.side = side;
if(isTactisPhase)
doNothing.actionType = EActionType::END_TACTIC_PHASE;
else
{
doNothing.actionType = EActionType::DEFEND;
doNothing.stackNumber = stack->unitId();
}
gameHandler.battles->makePlayerBattleAction(player, doNothing);
2023-08-14 21:31:44 +02:00
}
2023-08-28 02:40:35 +02:00
}
else
{
if(timer.turnTimer > 0)
2023-08-30 00:17:29 +02:00
{
timer.creatureTimer = timer.turnTimer;
timerCountDown(timer.creatureTimer, timer.turnTimer, player, 0);
timer.turnTimer = 0;
2023-08-30 00:17:29 +02:00
}
else if(timer.baseTimer > 0)
2023-08-30 00:17:29 +02:00
{
timer.creatureTimer = timer.baseTimer;
timerCountDown(timer.creatureTimer, timer.baseTimer, player, 0);
timer.baseTimer = 0;
2023-08-30 00:17:29 +02:00
}
2023-08-21 23:49:50 +02:00
else
{
2023-08-30 00:17:29 +02:00
BattleAction retreat;
retreat.side = side;
retreat.actionType = EActionType::RETREAT; //harsh punishment
gameHandler.battles->makePlayerBattleAction(player, retreat);
2023-08-21 23:49:50 +02:00
}
2023-08-14 21:31:44 +02:00
}
}
}