|
@@ -28,168 +28,280 @@ TurnTimerHandler::TurnTimerHandler(CGameHandler & gh):
|
|
|
|
|
|
void TurnTimerHandler::onGameplayStart(PlayerColor player)
|
|
void TurnTimerHandler::onGameplayStart(PlayerColor player)
|
|
{
|
|
{
|
|
|
|
+ std::lock_guard<std::recursive_mutex> guard(mx);
|
|
if(const auto * si = gameHandler.getStartInfo())
|
|
if(const auto * si = gameHandler.getStartInfo())
|
|
{
|
|
{
|
|
- if(si->turnTimerInfo.isEnabled())
|
|
|
|
- {
|
|
|
|
- timers[player] = si->turnTimerInfo;
|
|
|
|
- timers[player].turnTimer = 0;
|
|
|
|
- }
|
|
|
|
|
|
+ timers[player] = si->turnTimerInfo;
|
|
|
|
+ timers[player].turnTimer = 0;
|
|
|
|
+ timers[player].isActive = true;
|
|
|
|
+ timers[player].isBattle = false;
|
|
|
|
+ lastUpdate[player] = std::numeric_limits<int>::max();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void TurnTimerHandler::setTimerEnabled(PlayerColor player, bool enabled)
|
|
|
|
+{
|
|
|
|
+ std::lock_guard<std::recursive_mutex> guard(mx);
|
|
|
|
+ assert(player.isValidPlayer());
|
|
|
|
+ timers[player].isActive = enabled;
|
|
|
|
+ sendTimerUpdate(player);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void TurnTimerHandler::sendTimerUpdate(PlayerColor player)
|
|
|
|
+{
|
|
|
|
+ TurnTimeUpdate ttu;
|
|
|
|
+ ttu.player = player;
|
|
|
|
+ ttu.turnTimer = timers[player];
|
|
|
|
+ gameHandler.sendAndApply(&ttu);
|
|
|
|
+ lastUpdate[player] = 0;
|
|
|
|
+}
|
|
|
|
+
|
|
void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
|
|
void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
|
|
{
|
|
{
|
|
|
|
+ std::lock_guard<std::recursive_mutex> guard(mx);
|
|
if(const auto * si = gameHandler.getStartInfo())
|
|
if(const auto * si = gameHandler.getStartInfo())
|
|
{
|
|
{
|
|
if(si->turnTimerInfo.isEnabled())
|
|
if(si->turnTimerInfo.isEnabled())
|
|
{
|
|
{
|
|
- timers[player].baseTimer += timers[player].turnTimer;
|
|
|
|
- timers[player].turnTimer = si->turnTimerInfo.turnTimer;
|
|
|
|
|
|
+ auto & timer = timers[player];
|
|
|
|
+ if(si->turnTimerInfo.baseTimer > 0)
|
|
|
|
+ timer.baseTimer += timer.turnTimer;
|
|
|
|
+ timer.turnTimer = si->turnTimerInfo.turnTimer;
|
|
|
|
|
|
- TurnTimeUpdate ttu;
|
|
|
|
- ttu.player = player;
|
|
|
|
- ttu.turnTimer = timers[player];
|
|
|
|
- gameHandler.sendAndApply(&ttu);
|
|
|
|
|
|
+ sendTimerUpdate(player);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+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);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+bool TurnTimerHandler::timerCountDown(int & timer, int initialTimer, PlayerColor player, int waitTime)
|
|
|
|
+{
|
|
|
|
+ if(timer > 0)
|
|
|
|
+ {
|
|
|
|
+ timer -= waitTime;
|
|
|
|
+ lastUpdate[player] += waitTime;
|
|
|
|
+ int frequency = (timer > turnTimePropagateThreshold
|
|
|
|
+ && initialTimer - timer > turnTimePropagateThreshold)
|
|
|
|
+ ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
|
|
|
|
+
|
|
|
|
+ if(lastUpdate[player] >= frequency)
|
|
|
|
+ sendTimerUpdate(player);
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+
|
|
void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
|
|
void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
|
|
{
|
|
{
|
|
|
|
+ std::lock_guard<std::recursive_mutex> guard(mx);
|
|
const auto * gs = gameHandler.gameState();
|
|
const auto * gs = gameHandler.gameState();
|
|
const auto * si = gameHandler.getStartInfo();
|
|
const auto * si = gameHandler.getStartInfo();
|
|
- if(!si || !gs)
|
|
|
|
|
|
+ if(!si || !gs || !si->turnTimerInfo.isEnabled())
|
|
return;
|
|
return;
|
|
|
|
|
|
- auto & state = gs->players.at(player);
|
|
|
|
-
|
|
|
|
- if(state.human && si->turnTimerInfo.isEnabled() && !gs->curB)
|
|
|
|
|
|
+ auto & timer = timers[player];
|
|
|
|
+ const auto * state = gameHandler.getPlayerState(player);
|
|
|
|
+ if(state && state->human && timer.isActive && !timer.isBattle && state->status == EPlayerStatus::INGAME)
|
|
{
|
|
{
|
|
- if(timers[player].turnTimer > 0)
|
|
|
|
|
|
+ if(!timerCountDown(timer.turnTimer, si->turnTimerInfo.turnTimer, player, waitTime))
|
|
{
|
|
{
|
|
- timers[player].turnTimer -= waitTime;
|
|
|
|
- int frequency = (timers[player].turnTimer > turnTimePropagateThreshold ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit);
|
|
|
|
-
|
|
|
|
- if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
|
|
|
|
- && timers[player].turnTimer % frequency == 0)
|
|
|
|
|
|
+ if(timer.baseTimer > 0)
|
|
{
|
|
{
|
|
- TurnTimeUpdate ttu;
|
|
|
|
- ttu.player = state.color;
|
|
|
|
- ttu.turnTimer = timers[player];
|
|
|
|
- gameHandler.sendAndApply(&ttu);
|
|
|
|
|
|
+ timer.turnTimer = timer.baseTimer;
|
|
|
|
+ timer.baseTimer = 0;
|
|
|
|
+ onPlayerMakingTurn(player, 0);
|
|
}
|
|
}
|
|
|
|
+ else if(!gameHandler.queries->topQuery(state->color)) //wait for replies to avoid pending queries
|
|
|
|
+ gameHandler.turnOrder->onPlayerEndsTurn(state->color);
|
|
}
|
|
}
|
|
- else if(timers[player].baseTimer > 0)
|
|
|
|
- {
|
|
|
|
- timers[player].turnTimer = timers[player].baseTimer;
|
|
|
|
- timers[player].baseTimer = 0;
|
|
|
|
- onPlayerMakingTurn(player, 0);
|
|
|
|
- }
|
|
|
|
- else if(!gameHandler.queries->topQuery(state.color)) //wait for replies to avoid pending queries
|
|
|
|
- gameHandler.turnOrder->onPlayerEndsTurn(state.color);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+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()
|
|
void TurnTimerHandler::onBattleStart()
|
|
{
|
|
{
|
|
|
|
+ std::lock_guard<std::recursive_mutex> guard(mx);
|
|
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 || !si->turnTimerInfo.isBattleEnabled())
|
|
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
|
|
return;
|
|
return;
|
|
-
|
|
|
|
|
|
+
|
|
auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
|
|
auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
|
|
auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
|
|
auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
|
|
|
|
|
|
|
|
+ bool pvpBattle = isPvpBattle();
|
|
|
|
+
|
|
for(auto i : {attacker, defender})
|
|
for(auto i : {attacker, defender})
|
|
{
|
|
{
|
|
if(i.isValidPlayer())
|
|
if(i.isValidPlayer())
|
|
{
|
|
{
|
|
- timers[i].battleTimer = si->turnTimerInfo.battleTimer;
|
|
|
|
- timers[i].creatureTimer = si->turnTimerInfo.creatureTimer;
|
|
|
|
|
|
+ auto & timer = timers[i];
|
|
|
|
+ timer.isBattle = true;
|
|
|
|
+ timer.battleTimer = (pvpBattle ? si->turnTimerInfo.battleTimer : 0);
|
|
|
|
+ timer.creatureTimer = (pvpBattle ? si->turnTimerInfo.creatureTimer : si->turnTimerInfo.battleTimer);
|
|
|
|
|
|
- TurnTimeUpdate ttu;
|
|
|
|
- ttu.player = i;
|
|
|
|
- ttu.turnTimer = timers[i];
|
|
|
|
- gameHandler.sendAndApply(&ttu);
|
|
|
|
|
|
+ sendTimerUpdate(i);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void TurnTimerHandler::onBattleNextStack(const CStack & stack)
|
|
|
|
|
|
+void TurnTimerHandler::onBattleEnd()
|
|
{
|
|
{
|
|
|
|
+ std::lock_guard<std::recursive_mutex> guard(mx);
|
|
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 || !si->turnTimerInfo.isBattleEnabled())
|
|
if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
|
|
return;
|
|
return;
|
|
|
|
+
|
|
|
|
+ auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
|
|
|
|
+ auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
|
|
|
|
|
|
- auto player = stack.getOwner();
|
|
|
|
|
|
+ bool pvpBattle = isPvpBattle();
|
|
|
|
|
|
- if(!player.isValidPlayer())
|
|
|
|
|
|
+ for(auto i : {attacker, defender})
|
|
|
|
+ {
|
|
|
|
+ if(i.isValidPlayer())
|
|
|
|
+ {
|
|
|
|
+ auto & timer = timers[i];
|
|
|
|
+ timer.isBattle = false;
|
|
|
|
+
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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())
|
|
return;
|
|
return;
|
|
|
|
+
|
|
|
|
+ if(isPvpBattle())
|
|
|
|
+ {
|
|
|
|
+ auto player = stack.getOwner();
|
|
|
|
|
|
- if(timers[player].battleTimer < si->turnTimerInfo.battleTimer)
|
|
|
|
- timers[player].battleTimer = timers[player].creatureTimer;
|
|
|
|
- timers[player].creatureTimer = si->turnTimerInfo.creatureTimer;
|
|
|
|
|
|
+ auto & timer = timers[player];
|
|
|
|
+ if(timer.battleTimer == 0)
|
|
|
|
+ timer.battleTimer = timer.creatureTimer;
|
|
|
|
+ timer.creatureTimer = si->turnTimerInfo.creatureTimer;
|
|
|
|
|
|
- TurnTimeUpdate ttu;
|
|
|
|
- ttu.player = player;
|
|
|
|
- ttu.turnTimer = timers[player];
|
|
|
|
- gameHandler.sendAndApply(&ttu);
|
|
|
|
|
|
+ sendTimerUpdate(player);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
void TurnTimerHandler::onBattleLoop(int waitTime)
|
|
void TurnTimerHandler::onBattleLoop(int waitTime)
|
|
{
|
|
{
|
|
|
|
+ std::lock_guard<std::recursive_mutex> guard(mx);
|
|
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 || !si->turnTimerInfo.isBattleEnabled())
|
|
return;
|
|
return;
|
|
|
|
|
|
- const auto * stack = gs->curB.get()->battleGetStackByID(gs->curB->getActiveStackID());
|
|
|
|
- if(!stack || !stack->getOwner().isValidPlayer())
|
|
|
|
|
|
+ ui8 side = 0;
|
|
|
|
+ const CStack * stack = nullptr;
|
|
|
|
+ bool isTactisPhase = gs->curB->battleTacticDist() > 0;
|
|
|
|
+
|
|
|
|
+ if(isTactisPhase)
|
|
|
|
+ side = gs->curB->battleGetTacticsSide();
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ stack = gs->curB->battleGetStackByID(gs->curB->getActiveStackID());
|
|
|
|
+ if(!stack || !stack->getOwner().isValidPlayer())
|
|
|
|
+ return;
|
|
|
|
+ side = stack->unitSide();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ auto player = gs->curB->getSidePlayer(side);
|
|
|
|
+ if(!player.isValidPlayer())
|
|
return;
|
|
return;
|
|
|
|
|
|
- auto & state = gs->players.at(gs->curB->getSidePlayer(stack->unitSide()));
|
|
|
|
|
|
+ const auto * state = gameHandler.getPlayerState(player);
|
|
|
|
+ if(!state || state->status != EPlayerStatus::INGAME || !state->human)
|
|
|
|
+ return;
|
|
|
|
|
|
- auto turnTimerUpdateApplier = [&](TurnTimerInfo & tTimer, int waitTime)
|
|
|
|
|
|
+ auto & timer = timers[player];
|
|
|
|
+ if(timer.isActive && timer.isBattle && !timerCountDown(timer.creatureTimer, si->turnTimerInfo.creatureTimer, player, waitTime))
|
|
{
|
|
{
|
|
- if(tTimer.creatureTimer > 0)
|
|
|
|
|
|
+ if(isPvpBattle())
|
|
{
|
|
{
|
|
- tTimer.creatureTimer -= waitTime;
|
|
|
|
- int frequency = (tTimer.creatureTimer > turnTimePropagateThreshold
|
|
|
|
- && si->turnTimerInfo.creatureTimer - tTimer.creatureTimer > turnTimePropagateThreshold)
|
|
|
|
- ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
|
|
|
|
-
|
|
|
|
- if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
|
|
|
|
- && tTimer.creatureTimer % frequency == 0)
|
|
|
|
|
|
+ if(timer.battleTimer > 0)
|
|
{
|
|
{
|
|
- TurnTimeUpdate ttu;
|
|
|
|
- ttu.player = state.color;
|
|
|
|
- ttu.turnTimer = tTimer;
|
|
|
|
- gameHandler.sendAndApply(&ttu);
|
|
|
|
|
|
+ timer.creatureTimer = timer.battleTimer;
|
|
|
|
+ timerCountDown(timer.creatureTimer, timer.battleTimer, player, 0);
|
|
|
|
+ timer.battleTimer = 0;
|
|
|
|
+ }
|
|
|
|
+ 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);
|
|
}
|
|
}
|
|
- return true;
|
|
|
|
}
|
|
}
|
|
- return false;
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- if(state.human && si->turnTimerInfo.isBattleEnabled())
|
|
|
|
- {
|
|
|
|
- if(!turnTimerUpdateApplier(timers[state.color], waitTime))
|
|
|
|
|
|
+ else
|
|
{
|
|
{
|
|
- if(timers[state.color].battleTimer > 0)
|
|
|
|
|
|
+ if(timer.turnTimer > 0)
|
|
{
|
|
{
|
|
- timers[state.color].creatureTimer = timers[state.color].battleTimer;
|
|
|
|
- timers[state.color].battleTimer = 0;
|
|
|
|
- turnTimerUpdateApplier(timers[state.color], 0);
|
|
|
|
|
|
+ timer.creatureTimer = timer.turnTimer;
|
|
|
|
+ timerCountDown(timer.creatureTimer, timer.turnTimer, player, 0);
|
|
|
|
+ timer.turnTimer = 0;
|
|
|
|
+ }
|
|
|
|
+ else if(timer.baseTimer > 0)
|
|
|
|
+ {
|
|
|
|
+ timer.creatureTimer = timer.baseTimer;
|
|
|
|
+ timerCountDown(timer.creatureTimer, timer.baseTimer, player, 0);
|
|
|
|
+ timer.baseTimer = 0;
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- BattleAction doNothing;
|
|
|
|
- doNothing.actionType = EActionType::DEFEND;
|
|
|
|
- doNothing.side = stack->unitSide();
|
|
|
|
- doNothing.stackNumber = stack->unitId();
|
|
|
|
- gameHandler.battles->makePlayerBattleAction(state.color, doNothing);
|
|
|
|
|
|
+ BattleAction retreat;
|
|
|
|
+ retreat.side = side;
|
|
|
|
+ retreat.actionType = EActionType::RETREAT; //harsh punishment
|
|
|
|
+ gameHandler.battles->makePlayerBattleAction(player, retreat);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|