TurnTimerHandler.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. if(si->turnTimerInfo.isEnabled())
  31. {
  32. std::lock_guard<std::recursive_mutex> guard(mx);
  33. timers[player] = si->turnTimerInfo;
  34. timers[player].turnTimer = 0;
  35. }
  36. }
  37. }
  38. void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
  39. {
  40. if(const auto * si = gameHandler.getStartInfo())
  41. {
  42. if(si->turnTimerInfo.isEnabled())
  43. {
  44. std::lock_guard<std::recursive_mutex> guard(mx);
  45. timers[player].baseTimer += timers[player].turnTimer;
  46. timers[player].turnTimer = si->turnTimerInfo.turnTimer;
  47. TurnTimeUpdate ttu;
  48. ttu.player = player;
  49. ttu.turnTimer = timers[player];
  50. gameHandler.sendAndApply(&ttu);
  51. }
  52. }
  53. }
  54. void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
  55. {
  56. const auto * gs = gameHandler.gameState();
  57. const auto * si = gameHandler.getStartInfo();
  58. if(!si || !gs)
  59. return;
  60. auto & state = gs->players.at(player);
  61. if(state.human && si->turnTimerInfo.isEnabled() && !gs->curB)
  62. {
  63. std::lock_guard<std::recursive_mutex> guard(mx);
  64. if(timers[player].turnTimer > 0)
  65. {
  66. timers[player].turnTimer -= waitTime;
  67. int frequency = (timers[player].turnTimer > turnTimePropagateThreshold ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit);
  68. if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
  69. && timers[player].turnTimer % frequency == 0)
  70. {
  71. TurnTimeUpdate ttu;
  72. ttu.player = state.color;
  73. ttu.turnTimer = timers[player];
  74. gameHandler.sendAndApply(&ttu);
  75. }
  76. }
  77. else if(timers[player].baseTimer > 0)
  78. {
  79. timers[player].turnTimer = timers[player].baseTimer;
  80. timers[player].baseTimer = 0;
  81. onPlayerMakingTurn(player, 0);
  82. }
  83. else if(!gameHandler.queries->topQuery(state.color)) //wait for replies to avoid pending queries
  84. gameHandler.turnOrder->onPlayerEndsTurn(state.color);
  85. }
  86. }
  87. void TurnTimerHandler::onBattleStart()
  88. {
  89. const auto * gs = gameHandler.gameState();
  90. const auto * si = gameHandler.getStartInfo();
  91. if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
  92. return;
  93. std::lock_guard<std::recursive_mutex> guard(mx);
  94. auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
  95. auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
  96. for(auto i : {attacker, defender})
  97. {
  98. if(i.isValidPlayer())
  99. {
  100. timers[i].battleTimer = si->turnTimerInfo.battleTimer;
  101. timers[i].creatureTimer = si->turnTimerInfo.creatureTimer;
  102. TurnTimeUpdate ttu;
  103. ttu.player = i;
  104. ttu.turnTimer = timers[i];
  105. gameHandler.sendAndApply(&ttu);
  106. }
  107. }
  108. }
  109. void TurnTimerHandler::onBattleNextStack(const CStack & stack)
  110. {
  111. const auto * gs = gameHandler.gameState();
  112. const auto * si = gameHandler.getStartInfo();
  113. if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
  114. return;
  115. std::lock_guard<std::recursive_mutex> guard(mx);
  116. auto player = stack.getOwner();
  117. if(!player.isValidPlayer())
  118. return;
  119. if(timers[player].battleTimer < si->turnTimerInfo.battleTimer)
  120. timers[player].battleTimer = timers[player].creatureTimer;
  121. timers[player].creatureTimer = si->turnTimerInfo.creatureTimer;
  122. TurnTimeUpdate ttu;
  123. ttu.player = player;
  124. ttu.turnTimer = timers[player];
  125. gameHandler.sendAndApply(&ttu);
  126. }
  127. void TurnTimerHandler::onBattleLoop(int waitTime)
  128. {
  129. const auto * gs = gameHandler.gameState();
  130. const auto * si = gameHandler.getStartInfo();
  131. if(!si || !gs || !gs->curB)
  132. return;
  133. std::lock_guard<std::recursive_mutex> guard(mx);
  134. const auto * stack = gs->curB.get()->battleGetStackByID(gs->curB->getActiveStackID());
  135. if(!stack || !stack->getOwner().isValidPlayer())
  136. return;
  137. auto & state = gs->players.at(gs->curB->getSidePlayer(stack->unitSide()));
  138. auto turnTimerUpdateApplier = [&](TurnTimerInfo & tTimer, int waitTime)
  139. {
  140. if(tTimer.creatureTimer > 0)
  141. {
  142. tTimer.creatureTimer -= waitTime;
  143. int frequency = (tTimer.creatureTimer > turnTimePropagateThreshold
  144. && si->turnTimerInfo.creatureTimer - tTimer.creatureTimer > turnTimePropagateThreshold)
  145. ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
  146. if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
  147. && tTimer.creatureTimer % frequency == 0)
  148. {
  149. TurnTimeUpdate ttu;
  150. ttu.player = state.color;
  151. ttu.turnTimer = tTimer;
  152. gameHandler.sendAndApply(&ttu);
  153. }
  154. return true;
  155. }
  156. return false;
  157. };
  158. if(state.human && si->turnTimerInfo.isBattleEnabled())
  159. {
  160. if(!turnTimerUpdateApplier(timers[state.color], waitTime))
  161. {
  162. if(timers[state.color].battleTimer > 0)
  163. {
  164. timers[state.color].creatureTimer = timers[state.color].battleTimer;
  165. timers[state.color].battleTimer = 0;
  166. turnTimerUpdateApplier(timers[state.color], 0);
  167. }
  168. else
  169. {
  170. BattleAction doNothing;
  171. doNothing.actionType = EActionType::DEFEND;
  172. doNothing.side = stack->unitSide();
  173. doNothing.stackNumber = stack->unitId();
  174. gameHandler.battles->makePlayerBattleAction(state.color, doNothing);
  175. }
  176. }
  177. }
  178. }