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

321 lines
8.6 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"
#include "../lib/networkPacks/PacksForClient.h"
#include "../lib/networkPacks/PacksForClientBattle.h"
2023-08-14 21:31:44 +02:00
#include "../lib/CPlayerState.h"
#include "../lib/CStack.h"
#include "../lib/StartInfo.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].battleTimer = 0;
timers[player].unitTimer = 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-11-23 19:48:17 +02:00
if(si->turnTimerInfo.accumulatingTurnTimer)
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
if(lastUpdate[player] >= turnTimePropagateFrequency)
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)
{
// turn timers are only used if turn timer is non-zero
if (si->turnTimerInfo.turnTimer == 0)
return;
if(timerCountDown(timer.turnTimer, si->turnTimerInfo.turnTimer, player, waitTime))
return;
if(timerCountDown(timer.baseTimer, si->turnTimerInfo.baseTimer, player, waitTime))
return;
if(endTurnAllowed[state->color] && !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
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 = si->turnTimerInfo.battleTimer;
timer.unitTimer = (pvpBattle ? si->turnTimerInfo.unitTimer : 0);
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);
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 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.accumulatingUnitTimer)
timer.battleTimer += timer.unitTimer;
timer.unitTimer = si->turnTimerInfo.unitTimer;
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);
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)
2023-08-14 21:31:44 +02:00
{
// in pvp battles, timers are only used if unit timer is non-zero
if(isPvpBattle(battleID) && si->turnTimerInfo.unitTimer == 0)
return;
if (timerCountDown(timer.unitTimer, si->turnTimerInfo.unitTimer, player, waitTime))
return;
if (timerCountDown(timer.battleTimer, si->turnTimerInfo.battleTimer, player, waitTime))
return;
if (timerCountDown(timer.turnTimer, si->turnTimerInfo.turnTimer, player, waitTime))
return;
if (timerCountDown(timer.baseTimer, si->turnTimerInfo.baseTimer, player, waitTime))
return;
if(isPvpBattle(battleID))
2023-08-14 21:31:44 +02:00
{
BattleAction doNothing;
doNothing.side = side;
if(isTactisPhase)
doNothing.actionType = EActionType::END_TACTIC_PHASE;
2023-08-30 00:17:29 +02:00
else
{
doNothing.actionType = EActionType::DEFEND;
doNothing.stackNumber = stack->unitId();
2023-08-14 21:31:44 +02:00
}
gameHandler.battles->makePlayerBattleAction(battleID, player, doNothing);
2023-08-28 02:40:35 +02:00
}
else
{
BattleAction retreat;
retreat.side = side;
retreat.actionType = EActionType::RETREAT; //harsh punishment
gameHandler.battles->makePlayerBattleAction(battleID, player, retreat);
2023-08-14 21:31:44 +02:00
}
}
}