123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- /*
- * 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"
- #include "battles/BattleProcessor.h"
- #include "queries/QueriesProcessor.h"
- #include "processors/TurnOrderProcessor.h"
- #include "../lib/battle/BattleInfo.h"
- #include "../lib/gameState/CGameState.h"
- #include "../lib/CPlayerState.h"
- #include "../lib/CStack.h"
- #include "../lib/StartInfo.h"
- #include "../lib/NetPacks.h"
- TurnTimerHandler::TurnTimerHandler(CGameHandler & gh):
- gameHandler(gh)
- {
-
- }
- void TurnTimerHandler::onGameplayStart(PlayerColor player)
- {
- if(const auto * si = gameHandler.getStartInfo())
- {
- std::lock_guard<std::recursive_mutex> guard(mx);
- timers[player] = si->turnTimerInfo;
- timers[player].turnTimer = 0;
- }
- }
- void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
- {
- if(const auto * si = gameHandler.getStartInfo())
- {
- if(si->turnTimerInfo.isEnabled())
- {
- std::lock_guard<std::recursive_mutex> guard(mx);
- timers[player].baseTimer += timers[player].turnTimer;
- timers[player].turnTimer = si->turnTimerInfo.turnTimer;
-
- TurnTimeUpdate ttu;
- ttu.player = player;
- ttu.turnTimer = timers[player];
- gameHandler.sendAndApply(&ttu);
- }
- }
- }
- void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
- {
- const auto * gs = gameHandler.gameState();
- const auto * si = gameHandler.getStartInfo();
- if(!si || !gs)
- return;
-
- auto & state = gs->players.at(player);
-
- if(state.human && si->turnTimerInfo.isEnabled() && !gs->curB)
- {
- std::lock_guard<std::recursive_mutex> guard(mx);
- if(timers[player].turnTimer > 0)
- {
- 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 / 100 * 100 % frequency == 0)
- {
- TurnTimeUpdate ttu;
- ttu.player = state.color;
- ttu.turnTimer = timers[player];
- gameHandler.sendAndApply(&ttu);
- }
- }
- 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);
- }
- }
- void TurnTimerHandler::onBattleStart()
- {
- const auto * gs = gameHandler.gameState();
- const auto * si = gameHandler.getStartInfo();
- if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
- return;
-
- std::lock_guard<std::recursive_mutex> guard(mx);
- auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
- auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
-
- for(auto i : {attacker, defender})
- {
- if(i.isValidPlayer())
- {
- timers[i].battleTimer = si->turnTimerInfo.battleTimer;
- timers[i].creatureTimer = si->turnTimerInfo.creatureTimer;
-
- TurnTimeUpdate ttu;
- ttu.player = i;
- ttu.turnTimer = timers[i];
- gameHandler.sendAndApply(&ttu);
- }
- }
- }
- void TurnTimerHandler::onBattleNextStack(const CStack & stack)
- {
- const auto * gs = gameHandler.gameState();
- const auto * si = gameHandler.getStartInfo();
- if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
- return;
-
- std::lock_guard<std::recursive_mutex> guard(mx);
-
- auto player = stack.getOwner();
-
- if(!player.isValidPlayer())
- return;
-
- if(timers[player].battleTimer == 0)
- timers[player].battleTimer = timers[player].creatureTimer;
- timers[player].creatureTimer = si->turnTimerInfo.creatureTimer;
-
- TurnTimeUpdate ttu;
- ttu.player = player;
- ttu.turnTimer = timers[player];
- gameHandler.sendAndApply(&ttu);
- }
- void TurnTimerHandler::onBattleLoop(int waitTime)
- {
- const auto * gs = gameHandler.gameState();
- const auto * si = gameHandler.getStartInfo();
- if(!si || !gs || !gs->curB)
- return;
-
- std::lock_guard<std::recursive_mutex> guard(mx);
-
- ui8 side = 0;
- const CStack * stack = nullptr;
- bool isTactisPhase = gs->curB.get()->battleTacticDist() > 0;
-
- if(isTactisPhase)
- side = gs->curB.get()->battleGetTacticsSide();
- else
- {
- stack = gs->curB.get()->battleGetStackByID(gs->curB->getActiveStackID());
- if(!stack || !stack->getOwner().isValidPlayer())
- return;
- side = stack->unitSide();
- }
-
- auto & state = gs->players.at(gs->curB->getSidePlayer(side));
-
- auto turnTimerUpdateApplier = [&](TurnTimerInfo & tTimer, int waitTime)
- {
- if(tTimer.creatureTimer > 0)
- {
- 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 / 100 * 100 % frequency) == 0)
- {
- TurnTimeUpdate ttu;
- ttu.player = state.color;
- ttu.turnTimer = tTimer;
- gameHandler.sendAndApply(&ttu);
- }
- return true;
- }
- return false;
- };
-
- if(state.human && si->turnTimerInfo.isBattleEnabled())
- {
- if(!turnTimerUpdateApplier(timers[state.color], waitTime))
- {
- if(timers[state.color].battleTimer > 0)
- {
- timers[state.color].creatureTimer = timers[state.color].battleTimer;
- timers[state.color].battleTimer = 0;
- turnTimerUpdateApplier(timers[state.color], 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(state.color, doNothing);
- }
- }
- }
- }
|