TurnTimerHandler.cpp 5.9 KB

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