TurnTimerHandler.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * TurnTimerHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "TurnTimerHandler.h"
  12. #include "CGameHandler.h"
  13. #include "battles/BattleProcessor.h"
  14. #include "queries/QueriesProcessor.h"
  15. #include "processors/TurnOrderProcessor.h"
  16. #include "../lib/battle/BattleInfo.h"
  17. #include "../lib/gameState/CGameState.h"
  18. #include "../lib/CPlayerState.h"
  19. #include "../lib/CStack.h"
  20. #include "../lib/StartInfo.h"
  21. #include "../lib/NetPacks.h"
  22. TurnTimerHandler::TurnTimerHandler(CGameHandler & gh):
  23. gameHandler(gh)
  24. {
  25. }
  26. void TurnTimerHandler::onGameplayStart(PlayerColor player)
  27. {
  28. if(const auto * si = gameHandler.getStartInfo())
  29. {
  30. std::lock_guard<std::recursive_mutex> guard(mx);
  31. timers[player] = si->turnTimerInfo;
  32. timers[player].turnTimer = 0;
  33. timerEnabled[player] = true;
  34. }
  35. }
  36. void TurnTimerHandler::setTimerEnabled(PlayerColor player, bool enabled)
  37. {
  38. std::lock_guard<std::recursive_mutex> guard(mx);
  39. assert(player.isValidPlayer());
  40. timerEnabled[player] = enabled;
  41. }
  42. void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
  43. {
  44. if(const auto * si = gameHandler.getStartInfo())
  45. {
  46. if(si->turnTimerInfo.isEnabled())
  47. {
  48. std::lock_guard<std::recursive_mutex> guard(mx);
  49. if(si->turnTimerInfo.baseTimer > 0)
  50. timers[player].baseTimer += timers[player].turnTimer;
  51. timers[player].turnTimer = si->turnTimerInfo.turnTimer;
  52. TurnTimeUpdate ttu;
  53. ttu.player = player;
  54. ttu.turnTimer = timers[player];
  55. gameHandler.sendAndApply(&ttu);
  56. }
  57. }
  58. }
  59. void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
  60. {
  61. const auto * gs = gameHandler.gameState();
  62. const auto * si = gameHandler.getStartInfo();
  63. if(!si || !gs || gs->curB || !si->turnTimerInfo.isEnabled())
  64. return;
  65. std::lock_guard<std::recursive_mutex> guard(mx);
  66. auto & state = gs->players.at(player);
  67. if(state.human && timerEnabled[player])
  68. {
  69. if(timers[player].turnTimer > 0)
  70. {
  71. timers[player].turnTimer -= waitTime;
  72. int frequency = (timers[player].turnTimer > turnTimePropagateThreshold ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit);
  73. if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
  74. && timers[player].turnTimer / 100 * 100 % frequency == 0)
  75. {
  76. TurnTimeUpdate ttu;
  77. ttu.player = state.color;
  78. ttu.turnTimer = timers[player];
  79. gameHandler.sendAndApply(&ttu);
  80. }
  81. }
  82. else if(timers[player].baseTimer > 0)
  83. {
  84. timers[player].turnTimer = timers[player].baseTimer;
  85. timers[player].baseTimer = 0;
  86. onPlayerMakingTurn(player, 0);
  87. }
  88. else if(!gameHandler.queries->topQuery(state.color)) //wait for replies to avoid pending queries
  89. gameHandler.turnOrder->onPlayerEndsTurn(state.color);
  90. }
  91. }
  92. void TurnTimerHandler::onBattleStart()
  93. {
  94. const auto * gs = gameHandler.gameState();
  95. const auto * si = gameHandler.getStartInfo();
  96. if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
  97. return;
  98. std::lock_guard<std::recursive_mutex> guard(mx);
  99. auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
  100. auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
  101. for(auto i : {attacker, defender})
  102. {
  103. if(i.isValidPlayer())
  104. {
  105. timers[i].battleTimer = si->turnTimerInfo.battleTimer;
  106. timers[i].creatureTimer = si->turnTimerInfo.creatureTimer;
  107. TurnTimeUpdate ttu;
  108. ttu.player = i;
  109. ttu.turnTimer = timers[i];
  110. gameHandler.sendAndApply(&ttu);
  111. }
  112. }
  113. }
  114. void TurnTimerHandler::onBattleNextStack(const CStack & stack)
  115. {
  116. const auto * gs = gameHandler.gameState();
  117. const auto * si = gameHandler.getStartInfo();
  118. if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
  119. return;
  120. std::lock_guard<std::recursive_mutex> guard(mx);
  121. auto player = stack.getOwner();
  122. if(!player.isValidPlayer())
  123. return;
  124. if(timers[player].battleTimer == 0)
  125. timers[player].battleTimer = timers[player].creatureTimer;
  126. timers[player].creatureTimer = si->turnTimerInfo.creatureTimer;
  127. TurnTimeUpdate ttu;
  128. ttu.player = player;
  129. ttu.turnTimer = timers[player];
  130. gameHandler.sendAndApply(&ttu);
  131. }
  132. void TurnTimerHandler::onBattleLoop(int waitTime)
  133. {
  134. const auto * gs = gameHandler.gameState();
  135. const auto * si = gameHandler.getStartInfo();
  136. if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
  137. return;
  138. std::lock_guard<std::recursive_mutex> guard(mx);
  139. ui8 side = 0;
  140. const CStack * stack = nullptr;
  141. bool isTactisPhase = gs->curB.get()->battleTacticDist() > 0;
  142. if(isTactisPhase)
  143. side = gs->curB.get()->battleGetTacticsSide();
  144. else
  145. {
  146. stack = gs->curB.get()->battleGetStackByID(gs->curB->getActiveStackID());
  147. if(!stack || !stack->getOwner().isValidPlayer())
  148. return;
  149. side = stack->unitSide();
  150. }
  151. auto & state = gs->players.at(gs->curB->getSidePlayer(side));
  152. auto turnTimerUpdateApplier = [&](TurnTimerInfo & tTimer, int waitTime)
  153. {
  154. if(tTimer.creatureTimer > 0)
  155. {
  156. tTimer.creatureTimer -= waitTime;
  157. int frequency = (tTimer.creatureTimer > turnTimePropagateThreshold
  158. && si->turnTimerInfo.creatureTimer - tTimer.creatureTimer > turnTimePropagateThreshold)
  159. ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
  160. if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
  161. && (tTimer.creatureTimer / 100 * 100 % frequency) == 0)
  162. {
  163. TurnTimeUpdate ttu;
  164. ttu.player = state.color;
  165. ttu.turnTimer = tTimer;
  166. gameHandler.sendAndApply(&ttu);
  167. }
  168. return true;
  169. }
  170. return false;
  171. };
  172. if(state.human && timerEnabled[state.color]
  173. && !turnTimerUpdateApplier(timers[state.color], waitTime))
  174. {
  175. if(timers[state.color].battleTimer > 0)
  176. {
  177. timers[state.color].creatureTimer = timers[state.color].battleTimer;
  178. timers[state.color].battleTimer = 0;
  179. turnTimerUpdateApplier(timers[state.color], 0);
  180. }
  181. else
  182. {
  183. BattleAction doNothing;
  184. doNothing.side = side;
  185. if(isTactisPhase)
  186. doNothing.actionType = EActionType::END_TACTIC_PHASE;
  187. else
  188. {
  189. doNothing.actionType = EActionType::DEFEND;
  190. doNothing.stackNumber = stack->unitId();
  191. }
  192. gameHandler.battles->makePlayerBattleAction(state.color, doNothing);
  193. }
  194. }
  195. }