TurnTimerHandler.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. timers[player] = si->turnTimerInfo;
  33. timers[player].turnTimer = 0;
  34. }
  35. }
  36. }
  37. void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
  38. {
  39. if(const auto * si = gameHandler.getStartInfo())
  40. {
  41. if(si->turnTimerInfo.isEnabled())
  42. {
  43. timers[player].baseTimer += timers[player].turnTimer;
  44. timers[player].turnTimer = si->turnTimerInfo.turnTimer;
  45. TurnTimeUpdate ttu;
  46. ttu.player = player;
  47. ttu.turnTimer = timers[player];
  48. gameHandler.sendAndApply(&ttu);
  49. }
  50. }
  51. }
  52. void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
  53. {
  54. const auto * gs = gameHandler.gameState();
  55. const auto * si = gameHandler.getStartInfo();
  56. if(!si || !gs)
  57. return;
  58. auto & state = gs->players.at(player);
  59. if(state.human && si->turnTimerInfo.isEnabled() && !gs->curB)
  60. {
  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 % 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. auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
  91. auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
  92. for(auto i : {attacker, defender})
  93. {
  94. if(i.isValidPlayer())
  95. {
  96. timers[i].battleTimer = si->turnTimerInfo.battleTimer;
  97. timers[i].creatureTimer = si->turnTimerInfo.creatureTimer;
  98. TurnTimeUpdate ttu;
  99. ttu.player = i;
  100. ttu.turnTimer = timers[i];
  101. gameHandler.sendAndApply(&ttu);
  102. }
  103. }
  104. }
  105. void TurnTimerHandler::onBattleNextStack(const CStack & stack)
  106. {
  107. const auto * gs = gameHandler.gameState();
  108. const auto * si = gameHandler.getStartInfo();
  109. if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
  110. return;
  111. auto player = stack.getOwner();
  112. if(!player.isValidPlayer())
  113. return;
  114. if(timers[player].battleTimer < si->turnTimerInfo.battleTimer)
  115. timers[player].battleTimer = timers[player].creatureTimer;
  116. timers[player].creatureTimer = si->turnTimerInfo.creatureTimer;
  117. TurnTimeUpdate ttu;
  118. ttu.player = player;
  119. ttu.turnTimer = timers[player];
  120. gameHandler.sendAndApply(&ttu);
  121. }
  122. void TurnTimerHandler::onBattleLoop(int waitTime)
  123. {
  124. const auto * gs = gameHandler.gameState();
  125. const auto * si = gameHandler.getStartInfo();
  126. if(!si || !gs || !gs->curB)
  127. return;
  128. const auto * stack = gs->curB.get()->battleGetStackByID(gs->curB->getActiveStackID());
  129. if(!stack || !stack->getOwner().isValidPlayer())
  130. return;
  131. auto & state = gs->players.at(gs->curB->getSidePlayer(stack->unitSide()));
  132. auto turnTimerUpdateApplier = [&](TurnTimerInfo & tTimer, int waitTime)
  133. {
  134. if(tTimer.creatureTimer > 0)
  135. {
  136. tTimer.creatureTimer -= waitTime;
  137. int frequency = (tTimer.creatureTimer > turnTimePropagateThreshold
  138. && si->turnTimerInfo.creatureTimer - tTimer.creatureTimer > turnTimePropagateThreshold)
  139. ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
  140. if(state.status == EPlayerStatus::INGAME //do not send message if player is not active already
  141. && tTimer.creatureTimer % frequency == 0)
  142. {
  143. TurnTimeUpdate ttu;
  144. ttu.player = state.color;
  145. ttu.turnTimer = tTimer;
  146. gameHandler.sendAndApply(&ttu);
  147. }
  148. return true;
  149. }
  150. return false;
  151. };
  152. if(state.human && si->turnTimerInfo.isBattleEnabled())
  153. {
  154. if(!turnTimerUpdateApplier(timers[state.color], waitTime))
  155. {
  156. if(timers[state.color].battleTimer > 0)
  157. {
  158. timers[state.color].creatureTimer = timers[state.color].battleTimer;
  159. timers[state.color].battleTimer = 0;
  160. turnTimerUpdateApplier(timers[state.color], 0);
  161. }
  162. else
  163. {
  164. BattleAction doNothing;
  165. doNothing.actionType = EActionType::DEFEND;
  166. doNothing.side = stack->unitSide();
  167. doNothing.stackNumber = stack->unitId();
  168. gameHandler.battles->makePlayerBattleAction(state.color, doNothing);
  169. }
  170. }
  171. }
  172. }