TurnTimerHandler.cpp 5.4 KB

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