1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/server/TurnTimerHandler.cpp

338 lines
9.0 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-09-02 01:17:32 +02:00
endTurnAllowed[player] = true;
}
}
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
}
2023-09-02 01:17:32 +02:00
void TurnTimerHandler::setEndTurnAllowed(PlayerColor player, bool enabled)
{
std::lock_guard<std::recursive_mutex> guard(mx);
assert(player.isValidPlayer());
endTurnAllowed[player] = enabled;
}
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())
{
2023-09-02 01:17:32 +02:00
endTurnAllowed[player] = true;
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);
for (auto & battle : gs->currentBattles)
onBattleLoop(battle->battleID, waitTime);
2023-08-29 13:48:42 +02:00
}
}
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-09-02 01:17:32 +02:00
else if(endTurnAllowed[state->color] && !gameHandler.queries->topQuery(state->color)) //wait for replies to avoid pending queries
2023-08-30 00:17:29 +02:00
gameHandler.turnOrder->onPlayerEndsTurn(state->color);
}
}
}
2023-08-14 21:31:44 +02:00
bool TurnTimerHandler::isPvpBattle(const BattleID & battleID) const
2023-08-30 00:17:29 +02:00
{
const auto * gs = gameHandler.gameState();
auto attacker = gs->getBattle(battleID)->getSidePlayer(BattleSide::ATTACKER);
auto defender = gs->getBattle(battleID)->getSidePlayer(BattleSide::DEFENDER);
2023-08-30 00:17:29 +02:00
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(const BattleID & battleID)
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)
return;
2023-08-27 23:10:04 +02:00
auto attacker = gs->getBattle(battleID)->getSidePlayer(BattleSide::ATTACKER);
auto defender = gs->getBattle(battleID)->getSidePlayer(BattleSide::DEFENDER);
bool pvpBattle = isPvpBattle(battleID);
2023-08-30 00:17:29 +02:00
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;
2023-09-02 11:14:24 +02:00
timer.isActive = si->turnTimerInfo.isBattleEnabled();
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
}
}
}
void TurnTimerHandler::onBattleEnd(const BattleID & battleID)
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();
if(!si || !gs)
{
assert(0);
2023-08-21 23:49:50 +02:00
return;
}
2023-08-30 00:17:29 +02:00
if (!si->turnTimerInfo.isBattleEnabled())
return;
2023-08-21 23:49:50 +02:00
auto attacker = gs->getBattle(battleID)->getSidePlayer(BattleSide::ATTACKER);
auto defender = gs->getBattle(battleID)->getSidePlayer(BattleSide::DEFENDER);
bool pvpBattle = isPvpBattle(battleID);
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-09-02 11:14:24 +02:00
timer.isActive = true;
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 BattleID & battleID, const CStack & stack)
2023-08-30 00:17:29 +02:00
{
std::lock_guard<std::recursive_mutex> guard(mx);
const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo();
if(!si || !gs || !gs->getBattle(battleID))
{
assert(0);
return;
}
if (!si->turnTimerInfo.isBattleEnabled())
2023-08-26 03:39:29 +02:00
return;
2023-08-30 00:17:29 +02:00
if(isPvpBattle(battleID))
2023-08-30 00:17:29 +02:00
{
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
}
void TurnTimerHandler::onBattleLoop(const BattleID & battleID, 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-09-05 16:22:11 +02:00
if(!si || !gs)
{
assert(0);
2023-08-14 21:31:44 +02:00
return;
}
2023-08-14 21:31:44 +02:00
2023-09-05 16:22:11 +02:00
if (!si->turnTimerInfo.isBattleEnabled())
return;
2023-08-28 00:00:02 +02:00
ui8 side = 0;
const CStack * stack = nullptr;
bool isTactisPhase = gs->getBattle(battleID)->battleTacticDist() > 0;
2023-08-28 00:00:02 +02:00
if(isTactisPhase)
side = gs->getBattle(battleID)->battleGetTacticsSide();
2023-08-28 00:00:02 +02:00
else
{
stack = gs->getBattle(battleID)->battleGetStackByID(gs->getBattle(battleID)->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
auto player = gs->getBattle(battleID)->getSidePlayer(side);
2023-08-30 00:17:29 +02:00
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);
2023-09-05 16:22:11 +02:00
assert(state && state->status != EPlayerStatus::INGAME);
2023-08-30 00:17:29 +02:00
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
{
if(isPvpBattle(battleID))
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(battleID, 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(battleID, player, retreat);
2023-08-21 23:49:50 +02:00
}
2023-08-14 21:31:44 +02:00
}
}
}