TurnTimerHandler.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. std::lock_guard<std::recursive_mutex> guard(mx);
  29. if(const auto * si = gameHandler.getStartInfo())
  30. {
  31. timerInfo[player].timer = si->turnTimerInfo;
  32. timerInfo[player].timer.turnTimer = 0;
  33. timerInfo[player].isEnabled = true;
  34. timerInfo[player].isBattle = false;
  35. timerInfo[player].lastUpdate = std::numeric_limits<int>::max();
  36. }
  37. }
  38. void TurnTimerHandler::setTimerEnabled(PlayerColor player, bool enabled)
  39. {
  40. std::lock_guard<std::recursive_mutex> guard(mx);
  41. assert(player.isValidPlayer());
  42. timerInfo[player].isEnabled = enabled;
  43. }
  44. void TurnTimerHandler::sendTimerUpdate(PlayerColor player)
  45. {
  46. auto & info = timerInfo[player];
  47. TurnTimeUpdate ttu;
  48. ttu.player = player;
  49. ttu.turnTimer = info.timer;
  50. gameHandler.sendAndApply(&ttu);
  51. info.lastUpdate = 0;
  52. }
  53. void TurnTimerHandler::onPlayerGetTurn(PlayerColor player)
  54. {
  55. std::lock_guard<std::recursive_mutex> guard(mx);
  56. if(const auto * si = gameHandler.getStartInfo())
  57. {
  58. if(si->turnTimerInfo.isEnabled())
  59. {
  60. auto & info = timerInfo[player];
  61. if(si->turnTimerInfo.baseTimer > 0)
  62. info.timer.baseTimer += info.timer.turnTimer;
  63. info.timer.turnTimer = si->turnTimerInfo.turnTimer;
  64. sendTimerUpdate(player);
  65. }
  66. }
  67. }
  68. void TurnTimerHandler::update(int waitTime)
  69. {
  70. std::lock_guard<std::recursive_mutex> guard(mx);
  71. if(const auto * gs = gameHandler.gameState())
  72. {
  73. for(PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
  74. if(gs->isPlayerMakingTurn(player))
  75. onPlayerMakingTurn(player, waitTime);
  76. if(gs->curB)
  77. onBattleLoop(waitTime);
  78. }
  79. }
  80. bool TurnTimerHandler::timerCountDown(int & timer, int initialTimer, PlayerColor player, int waitTime)
  81. {
  82. if(timer > 0)
  83. {
  84. auto & info = timerInfo[player];
  85. timer -= waitTime;
  86. info.lastUpdate += waitTime;
  87. int frequency = (timer > turnTimePropagateThreshold
  88. && initialTimer - timer > turnTimePropagateThreshold)
  89. ? turnTimePropagateFrequency : turnTimePropagateFrequencyCrit;
  90. if(info.lastUpdate >= frequency)
  91. sendTimerUpdate(player);
  92. return true;
  93. }
  94. return false;
  95. }
  96. void TurnTimerHandler::onPlayerMakingTurn(PlayerColor player, int waitTime)
  97. {
  98. std::lock_guard<std::recursive_mutex> guard(mx);
  99. const auto * gs = gameHandler.gameState();
  100. const auto * si = gameHandler.getStartInfo();
  101. if(!si || !gs || !si->turnTimerInfo.isEnabled())
  102. return;
  103. auto & info = timerInfo[player];
  104. const auto * state = gameHandler.getPlayerState(player);
  105. if(state && state->human && info.isEnabled && !info.isBattle && state->status == EPlayerStatus::INGAME)
  106. {
  107. if(!timerCountDown(info.timer.turnTimer, si->turnTimerInfo.turnTimer, player, waitTime))
  108. {
  109. if(info.timer.baseTimer > 0)
  110. {
  111. info.timer.turnTimer = info.timer.baseTimer;
  112. info.timer.baseTimer = 0;
  113. onPlayerMakingTurn(player, 0);
  114. }
  115. else if(!gameHandler.queries->topQuery(state->color)) //wait for replies to avoid pending queries
  116. gameHandler.turnOrder->onPlayerEndsTurn(state->color);
  117. }
  118. }
  119. }
  120. bool TurnTimerHandler::isPvpBattle() const
  121. {
  122. const auto * gs = gameHandler.gameState();
  123. auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
  124. auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
  125. if(attacker.isValidPlayer() && defender.isValidPlayer())
  126. {
  127. const auto * attackerState = gameHandler.getPlayerState(attacker);
  128. const auto * defenderState = gameHandler.getPlayerState(defender);
  129. if(attackerState && defenderState && attackerState->human && defenderState->human)
  130. return true;
  131. }
  132. return false;
  133. }
  134. void TurnTimerHandler::onBattleStart()
  135. {
  136. std::lock_guard<std::recursive_mutex> guard(mx);
  137. const auto * gs = gameHandler.gameState();
  138. const auto * si = gameHandler.getStartInfo();
  139. if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
  140. return;
  141. auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
  142. auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
  143. bool pvpBattle = isPvpBattle();
  144. for(auto i : {attacker, defender})
  145. {
  146. if(i.isValidPlayer())
  147. {
  148. auto & info = timerInfo[i];
  149. info.isBattle = true;
  150. info.timer.battleTimer = (pvpBattle ? si->turnTimerInfo.battleTimer : 0);
  151. info.timer.creatureTimer = (pvpBattle ? si->turnTimerInfo.creatureTimer : si->turnTimerInfo.battleTimer);
  152. sendTimerUpdate(i);
  153. }
  154. }
  155. }
  156. void TurnTimerHandler::onBattleEnd()
  157. {
  158. std::lock_guard<std::recursive_mutex> guard(mx);
  159. const auto * gs = gameHandler.gameState();
  160. const auto * si = gameHandler.getStartInfo();
  161. if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
  162. return;
  163. auto attacker = gs->curB->getSidePlayer(BattleSide::ATTACKER);
  164. auto defender = gs->curB->getSidePlayer(BattleSide::DEFENDER);
  165. bool pvpBattle = isPvpBattle();
  166. for(auto i : {attacker, defender})
  167. {
  168. if(i.isValidPlayer() && !pvpBattle)
  169. {
  170. auto & info = timerInfo[i];
  171. info.isBattle = false;
  172. if(si->turnTimerInfo.baseTimer && info.timer.baseTimer == 0)
  173. info.timer.baseTimer = info.timer.creatureTimer;
  174. else if(si->turnTimerInfo.turnTimer && info.timer.turnTimer == 0)
  175. info.timer.turnTimer = info.timer.creatureTimer;
  176. sendTimerUpdate(i);
  177. }
  178. }
  179. }
  180. void TurnTimerHandler::onBattleNextStack(const CStack & stack)
  181. {
  182. std::lock_guard<std::recursive_mutex> guard(mx);
  183. const auto * gs = gameHandler.gameState();
  184. const auto * si = gameHandler.getStartInfo();
  185. if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
  186. return;
  187. if(isPvpBattle())
  188. {
  189. auto player = stack.getOwner();
  190. auto & info = timerInfo[player];
  191. if(info.timer.battleTimer == 0)
  192. info.timer.battleTimer = info.timer.creatureTimer;
  193. info.timer.creatureTimer = si->turnTimerInfo.creatureTimer;
  194. sendTimerUpdate(player);
  195. }
  196. }
  197. void TurnTimerHandler::onBattleLoop(int waitTime)
  198. {
  199. std::lock_guard<std::recursive_mutex> guard(mx);
  200. const auto * gs = gameHandler.gameState();
  201. const auto * si = gameHandler.getStartInfo();
  202. if(!si || !gs || !gs->curB || !si->turnTimerInfo.isBattleEnabled())
  203. return;
  204. ui8 side = 0;
  205. const CStack * stack = nullptr;
  206. bool isTactisPhase = gs->curB->battleTacticDist() > 0;
  207. if(isTactisPhase)
  208. side = gs->curB->battleGetTacticsSide();
  209. else
  210. {
  211. stack = gs->curB->battleGetStackByID(gs->curB->getActiveStackID());
  212. if(!stack || !stack->getOwner().isValidPlayer())
  213. return;
  214. side = stack->unitSide();
  215. }
  216. auto player = gs->curB->getSidePlayer(side);
  217. if(!player.isValidPlayer())
  218. return;
  219. const auto * state = gameHandler.getPlayerState(player);
  220. if(!state || state->status != EPlayerStatus::INGAME || !state->human)
  221. return;
  222. auto & info = timerInfo[player];
  223. if(info.isEnabled && info.isBattle && !timerCountDown(info.timer.creatureTimer, si->turnTimerInfo.creatureTimer, player, waitTime))
  224. {
  225. if(isPvpBattle())
  226. {
  227. if(info.timer.battleTimer > 0)
  228. {
  229. info.timer.creatureTimer = info.timer.battleTimer;
  230. timerCountDown(info.timer.creatureTimer, info.timer.battleTimer, player, 0);
  231. info.timer.battleTimer = 0;
  232. }
  233. else
  234. {
  235. BattleAction doNothing;
  236. doNothing.side = side;
  237. if(isTactisPhase)
  238. doNothing.actionType = EActionType::END_TACTIC_PHASE;
  239. else
  240. {
  241. doNothing.actionType = EActionType::DEFEND;
  242. doNothing.stackNumber = stack->unitId();
  243. }
  244. gameHandler.battles->makePlayerBattleAction(player, doNothing);
  245. }
  246. }
  247. else
  248. {
  249. if(info.timer.turnTimer > 0)
  250. {
  251. info.timer.creatureTimer = info.timer.turnTimer;
  252. timerCountDown(info.timer.creatureTimer, info.timer.turnTimer, player, 0);
  253. info.timer.turnTimer = 0;
  254. }
  255. else if(info.timer.baseTimer > 0)
  256. {
  257. info.timer.creatureTimer = info.timer.baseTimer;
  258. timerCountDown(info.timer.creatureTimer, info.timer.baseTimer, player, 0);
  259. info.timer.baseTimer = 0;
  260. }
  261. else
  262. {
  263. BattleAction retreat;
  264. retreat.side = side;
  265. retreat.actionType = EActionType::RETREAT; //harsh punishment
  266. gameHandler.battles->makePlayerBattleAction(player, retreat);
  267. }
  268. }
  269. }
  270. }