1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-27 22:49:25 +02:00

Adopt turn timer to battle refactoring

This commit is contained in:
nordsoft
2023-08-22 01:49:50 +04:00
parent d50edc28ab
commit 7b4b01a280
5 changed files with 66 additions and 42 deletions

View File

@@ -1066,10 +1066,8 @@ void CGameHandler::run(bool resume)
{ {
turnTimerHandler.onPlayerMakingTurn(gs->players[playerColor], waitTime); turnTimerHandler.onPlayerMakingTurn(gs->players[playerColor], waitTime);
if(gs->curB) if(gs->curB)
{ turnTimerHandler.onBattleLoop(waitTime);
turnTimerHandler.onBattleLoop(gs->players[gs->curB->getSidePlayer(BattleSide::ATTACKER)], waitTime);
turnTimerHandler.onBattleLoop(gs->players[gs->curB->getSidePlayer(BattleSide::DEFENDER)], waitTime);
}
static time_duration p = milliseconds(waitTime); static time_duration p = milliseconds(waitTime);
states.cv.timed_wait(lock, p); states.cv.timed_wait(lock, p);
} }
@@ -1272,11 +1270,11 @@ bool CGameHandler::moveHero(ObjectInstanceID hid, int3 dst, ui8 teleporting, boo
if(!transit) if(!transit)
{ {
for(auto topQuery = queries.topQuery(h->tempOwner); true; topQuery = queries.topQuery(h->tempOwner)) for(auto topQuery = queries->topQuery(h->tempOwner); true; topQuery = queries->topQuery(h->tempOwner))
{ {
moveQuery = std::dynamic_pointer_cast<CHeroMovementQuery>(topQuery); moveQuery = std::dynamic_pointer_cast<CHeroMovementQuery>(topQuery);
if(moveQuery) if(moveQuery)
queries.popIfTop(moveQuery); queries->popIfTop(moveQuery);
else else
break; break;
} }

View File

@@ -83,8 +83,6 @@ class CGameHandler : public IGameCallback, public CBattleInfoCallback, public En
CVCMIServer * lobby; CVCMIServer * lobby;
std::shared_ptr<CApplier<CBaseForGHApply>> applier; std::shared_ptr<CApplier<CBaseForGHApply>> applier;
TurnTimerHandler turnTimerHandler;
public: public:
using CCallbackBase::setBattle; using CCallbackBase::setBattle;
@@ -109,6 +107,8 @@ public:
SpellCastEnvironment * spellEnv; SpellCastEnvironment * spellEnv;
TurnTimerHandler turnTimerHandler;
const Services * services() const override; const Services * services() const override;
const BattleCb * battle() const override; const BattleCb * battle() const override;
const GameCb * game() const override; const GameCb * game() const override;

View File

@@ -10,11 +10,14 @@
#include "StdInc.h" #include "StdInc.h"
#include "TurnTimerHandler.h" #include "TurnTimerHandler.h"
#include "CGameHandler.h" #include "CGameHandler.h"
#include "battles/BattleProcessor.h"
#include "queries/QueriesProcessor.h"
#include "../lib/battle/BattleInfo.h" #include "../lib/battle/BattleInfo.h"
#include "../lib/gameState/CGameState.h" #include "../lib/gameState/CGameState.h"
#include "../lib/CPlayerState.h" #include "../lib/CPlayerState.h"
#include "../lib/CStack.h" #include "../lib/CStack.h"
#include "../lib/StartInfo.h" #include "../lib/StartInfo.h"
#include "../lib/NetPacks.h"
TurnTimerHandler::TurnTimerHandler(CGameHandler & gh): TurnTimerHandler::TurnTimerHandler(CGameHandler & gh):
gameHandler(gh) gameHandler(gh)
@@ -80,7 +83,7 @@ void TurnTimerHandler::onPlayerMakingTurn(PlayerState & state, int waitTime)
state.turnTimer.baseTimer = 0; state.turnTimer.baseTimer = 0;
onPlayerMakingTurn(state, waitTime); onPlayerMakingTurn(state, waitTime);
} }
else if(!gameHandler.queries.topQuery(state.color)) //wait for replies to avoid pending queries else if(!gameHandler.queries->topQuery(state.color)) //wait for replies to avoid pending queries
gameHandler.states.players.at(state.color).makingTurn = false; //force end turn gameHandler.states.players.at(state.color).makingTurn = false; //force end turn
} }
} }
@@ -101,10 +104,15 @@ void TurnTimerHandler::onBattleStart(PlayerState & state)
} }
} }
void TurnTimerHandler::onBattleNextStack(PlayerState & state) void TurnTimerHandler::onBattleNextStack(const CStack & stack)
{
if(const auto * si = gameHandler.getStartInfo())
{ {
const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo();
if(!si || !gs)
return;
const auto & state = gs->players.at(stack.getOwner());
if(si->turnTimerInfo.isBattleEnabled()) if(si->turnTimerInfo.isBattleEnabled())
{ {
TurnTimeUpdate ttu; TurnTimeUpdate ttu;
@@ -116,44 +124,60 @@ void TurnTimerHandler::onBattleNextStack(PlayerState & state)
gameHandler.sendAndApply(&ttu); gameHandler.sendAndApply(&ttu);
} }
} }
}
void TurnTimerHandler::onBattleLoop(PlayerState & state, int waitTime) void TurnTimerHandler::onBattleLoop(int waitTime)
{ {
const auto * gs = gameHandler.gameState(); const auto * gs = gameHandler.gameState();
const auto * si = gameHandler.getStartInfo(); const auto * si = gameHandler.getStartInfo();
if(!si || !gs || !gs->curB) if(!si || !gs || !gs->curB)
return; return;
if(state.human && si->turnTimerInfo.isBattleEnabled()) const auto * stack = gs->curB.get()->battleGetStackByID(gs->curB->getActiveStackID());
if(!stack)
return;
auto & state = gs->players.at(gs->curB->getSidePlayer(stack->unitSide()));
auto turnTimerUpdateApplier = [&](const TurnTimerInfo & tTimer)
{ {
if(state.turnTimer.creatureTimer > 0) TurnTimerInfo turnTimerUpdate = tTimer;
if(tTimer.creatureTimer > 0)
{ {
state.turnTimer.creatureTimer -= waitTime; turnTimerUpdate.creatureTimer -= waitTime;
int frequency = (state.turnTimer.creatureTimer > turnTimePropagateThreshold ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit); int frequency = (turnTimerUpdate.creatureTimer > turnTimePropagateThreshold ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit);
if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
&& state.turnTimer.creatureTimer % frequency == 0) && turnTimerUpdate.creatureTimer % frequency == 0)
{ {
TurnTimeUpdate ttu; TurnTimeUpdate ttu;
ttu.player = state.color; ttu.player = state.color;
ttu.turnTimer = state.turnTimer; ttu.turnTimer = turnTimerUpdate;
gameHandler.sendAndApply(&ttu); gameHandler.sendAndApply(&ttu);
} }
return true;
} }
else if(state.turnTimer.battleTimer > 0) return false;
};
if(state.human && si->turnTimerInfo.isBattleEnabled())
{ {
state.turnTimer.creatureTimer = state.turnTimer.battleTimer; TurnTimerInfo turnTimer = state.turnTimer;
state.turnTimer.battleTimer = 0; if(!turnTimerUpdateApplier(turnTimer))
onBattleLoop(state, waitTime); {
if(turnTimer.battleTimer > 0)
{
turnTimer.creatureTimer = turnTimer.battleTimer;
turnTimer.battleTimer = 0;
turnTimerUpdateApplier(turnTimer);
} }
else if(auto * stack = const_cast<BattleInfo *>(gs->curB.get())->getStack(gs->curB->getActiveStackID())) else
{ {
BattleAction doNothing; BattleAction doNothing;
doNothing.actionType = EActionType::DEFEND; doNothing.actionType = EActionType::DEFEND;
doNothing.side = stack->unitSide(); doNothing.side = stack->unitSide();
doNothing.stackNumber = stack->unitId(); doNothing.stackNumber = stack->unitId();
gameHandler.makeAutomaticAction(stack, doNothing); gameHandler.battles->makePlayerBattleAction(state.color, doNothing);
}
} }
} }
} }

View File

@@ -34,6 +34,6 @@ public:
void onPlayerGetTurn(PlayerState & state); void onPlayerGetTurn(PlayerState & state);
void onPlayerMakingTurn(PlayerState & state, int waitTime); void onPlayerMakingTurn(PlayerState & state, int waitTime);
void onBattleStart(PlayerState & state); void onBattleStart(PlayerState & state);
void onBattleNextStack(PlayerState & state); void onBattleNextStack(const CStack & stack);
void onBattleLoop(PlayerState & state, int waitTime); void onBattleLoop(int waitTime);
}; };

View File

@@ -316,6 +316,8 @@ void BattleFlowProcessor::activateNextStack()
if(!removeGhosts.changedStacks.empty()) if(!removeGhosts.changedStacks.empty())
gameHandler->sendAndApply(&removeGhosts); gameHandler->sendAndApply(&removeGhosts);
gameHandler->turnTimerHandler.onBattleNextStack(*next);
if (!tryMakeAutomaticAction(next)) if (!tryMakeAutomaticAction(next))
{ {
setActiveStack(next); setActiveStack(next);