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-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)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TurnTimerHandler::onGameplayStart(PlayerState & state)
|
|
|
|
{
|
|
|
|
if(const auto * si = gameHandler.getStartInfo())
|
|
|
|
{
|
|
|
|
if(si->turnTimerInfo.isEnabled())
|
|
|
|
{
|
2023-08-20 02:06:52 +02:00
|
|
|
state.turnTimer = si->turnTimerInfo;
|
|
|
|
state.turnTimer.turnTimer = 0;
|
2023-08-14 01:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TurnTimerHandler::onPlayerGetTurn(PlayerState & state)
|
|
|
|
{
|
|
|
|
if(const auto * si = gameHandler.getStartInfo())
|
|
|
|
{
|
|
|
|
if(si->turnTimerInfo.isEnabled())
|
|
|
|
{
|
|
|
|
state.turnTimer.baseTimer += state.turnTimer.turnTimer;
|
|
|
|
state.turnTimer.turnTimer = si->turnTimerInfo.turnTimer;
|
|
|
|
|
|
|
|
TurnTimeUpdate ttu;
|
|
|
|
ttu.player = state.color;
|
|
|
|
ttu.turnTimer = state.turnTimer;
|
2023-08-14 21:31:44 +02:00
|
|
|
gameHandler.sendAndApply(&ttu);
|
2023-08-14 01:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TurnTimerHandler::onPlayerMakingTurn(PlayerState & state, int waitTime)
|
|
|
|
{
|
|
|
|
const auto * gs = gameHandler.gameState();
|
|
|
|
const auto * si = gameHandler.getStartInfo();
|
|
|
|
if(!si || !gs)
|
|
|
|
return;
|
|
|
|
|
2023-08-19 17:00:08 +02:00
|
|
|
if(state.human && si->turnTimerInfo.isEnabled() && !gs->curB)
|
2023-08-14 01:20:27 +02:00
|
|
|
{
|
|
|
|
if(state.turnTimer.turnTimer > 0)
|
|
|
|
{
|
|
|
|
state.turnTimer.turnTimer -= waitTime;
|
2023-08-14 21:31:44 +02:00
|
|
|
int frequency = (state.turnTimer.creatureTimer > 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-14 21:31:44 +02:00
|
|
|
&& state.turnTimer.turnTimer % frequency == 0)
|
2023-08-14 01:20:27 +02:00
|
|
|
{
|
|
|
|
TurnTimeUpdate ttu;
|
|
|
|
ttu.player = state.color;
|
|
|
|
ttu.turnTimer = state.turnTimer;
|
2023-08-14 21:31:44 +02:00
|
|
|
gameHandler.sendAndApply(&ttu);
|
2023-08-14 01:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(state.turnTimer.baseTimer > 0)
|
|
|
|
{
|
|
|
|
state.turnTimer.turnTimer = state.turnTimer.baseTimer;
|
|
|
|
state.turnTimer.baseTimer = 0;
|
|
|
|
onPlayerMakingTurn(state, waitTime);
|
|
|
|
}
|
2023-08-21 23:49:50 +02:00
|
|
|
else if(!gameHandler.queries->topQuery(state.color)) //wait for replies to avoid pending queries
|
2023-08-14 01:20:27 +02:00
|
|
|
gameHandler.states.players.at(state.color).makingTurn = false; //force end turn
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
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-22 12:14:50 +02:00
|
|
|
const auto & state = gs->players.at(i);
|
2023-08-14 21:31:44 +02:00
|
|
|
TurnTimeUpdate ttu;
|
|
|
|
ttu.player = state.color;
|
|
|
|
ttu.turnTimer = state.turnTimer;
|
|
|
|
ttu.turnTimer.battleTimer = si->turnTimerInfo.battleTimer;
|
|
|
|
ttu.turnTimer.creatureTimer = si->turnTimerInfo.creatureTimer;
|
|
|
|
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-22 00:08:42 +02:00
|
|
|
if(!si || !gs || !gs->curB)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!stack.getOwner().isValidPlayer())
|
2023-08-21 23:49:50 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
const auto & state = gs->players.at(stack.getOwner());
|
|
|
|
|
|
|
|
if(si->turnTimerInfo.isBattleEnabled())
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-21 23:49:50 +02:00
|
|
|
TurnTimeUpdate ttu;
|
|
|
|
ttu.player = state.color;
|
|
|
|
ttu.turnTimer = state.turnTimer;
|
|
|
|
if(state.turnTimer.battleTimer < si->turnTimerInfo.battleTimer)
|
|
|
|
ttu.turnTimer.battleTimer = ttu.turnTimer.creatureTimer;
|
|
|
|
ttu.turnTimer.creatureTimer = si->turnTimerInfo.creatureTimer;
|
|
|
|
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-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()));
|
|
|
|
|
|
|
|
auto turnTimerUpdateApplier = [&](const TurnTimerInfo & tTimer)
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-21 23:49:50 +02:00
|
|
|
TurnTimerInfo turnTimerUpdate = tTimer;
|
|
|
|
if(tTimer.creatureTimer > 0)
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-21 23:49:50 +02:00
|
|
|
turnTimerUpdate.creatureTimer -= waitTime;
|
|
|
|
int frequency = (turnTimerUpdate.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-21 23:49:50 +02:00
|
|
|
&& turnTimerUpdate.creatureTimer % frequency == 0)
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
|
|
|
TurnTimeUpdate ttu;
|
|
|
|
ttu.player = state.color;
|
2023-08-21 23:49:50 +02:00
|
|
|
ttu.turnTimer = turnTimerUpdate;
|
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())
|
|
|
|
{
|
|
|
|
TurnTimerInfo turnTimer = state.turnTimer;
|
|
|
|
if(!turnTimerUpdateApplier(turnTimer))
|
2023-08-14 21:31:44 +02:00
|
|
|
{
|
2023-08-21 23:49:50 +02:00
|
|
|
if(turnTimer.battleTimer > 0)
|
|
|
|
{
|
|
|
|
turnTimer.creatureTimer = turnTimer.battleTimer;
|
|
|
|
turnTimer.battleTimer = 0;
|
|
|
|
turnTimerUpdateApplier(turnTimer);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|