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