BattleQueries.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * BattleQueries.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 "BattleQueries.h"
  12. #include "MapQueries.h"
  13. #include "QueriesProcessor.h"
  14. #include "../CGameHandler.h"
  15. #include "../battles/BattleProcessor.h"
  16. #include "../../lib/battle/IBattleState.h"
  17. #include "../../lib/battle/SideInBattle.h"
  18. #include "../../lib/CPlayerState.h"
  19. #include "../../lib/mapObjects/CGObjectInstance.h"
  20. #include "../../lib/mapObjects/CGTownInstance.h"
  21. #include "../../lib/networkPacks/PacksForServer.h"
  22. #include "../../lib/serializer/Cast.h"
  23. void CBattleQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
  24. {
  25. assert(result);
  26. if(result && !isAiVsHuman)
  27. objectVisit.visitedObject->battleFinished(objectVisit.visitingHero, *result);
  28. }
  29. CBattleQuery::CBattleQuery(CGameHandler * owner, const IBattleInfo * bi):
  30. CQuery(owner),
  31. battleID(bi->getBattleID())
  32. {
  33. belligerents[0] = bi->getSideArmy(0);
  34. belligerents[1] = bi->getSideArmy(1);
  35. isAiVsHuman = bi->getSidePlayer(1).isValidPlayer() && gh->getPlayerState(bi->getSidePlayer(0))->isHuman() != gh->getPlayerState(bi->getSidePlayer(1))->isHuman();
  36. addPlayer(bi->getSidePlayer(0));
  37. addPlayer(bi->getSidePlayer(1));
  38. }
  39. CBattleQuery::CBattleQuery(CGameHandler * owner):
  40. CQuery(owner)
  41. {
  42. belligerents[0] = belligerents[1] = nullptr;
  43. }
  44. bool CBattleQuery::blocksPack(const CPack * pack) const
  45. {
  46. if(dynamic_ptr_cast<MakeAction>(pack) != nullptr)
  47. return false;
  48. if(dynamic_ptr_cast<GamePause>(pack) != nullptr)
  49. return false;
  50. return true;
  51. }
  52. void CBattleQuery::onRemoval(PlayerColor color)
  53. {
  54. assert(result);
  55. if(result)
  56. gh->battles->battleAfterLevelUp(battleID, *result);
  57. }
  58. void CBattleQuery::onExposure(QueryPtr topQuery)
  59. {
  60. // this method may be called in two cases:
  61. // 1) when requesting battle replay (but before replay starts -> no valid result)
  62. // 2) when aswering on levelup queries after accepting battle result -> valid result
  63. if(result)
  64. owner->popQuery(*this);
  65. }
  66. CBattleDialogQuery::CBattleDialogQuery(CGameHandler * owner, const IBattleInfo * bi, std::optional<BattleResult> Br):
  67. CDialogQuery(owner),
  68. bi(bi),
  69. result(Br)
  70. {
  71. addPlayer(bi->getSidePlayer(0));
  72. addPlayer(bi->getSidePlayer(1));
  73. }
  74. void CBattleDialogQuery::onRemoval(PlayerColor color)
  75. {
  76. if (!gh->getPlayerState(color)->isHuman())
  77. return;
  78. assert(answer);
  79. if(*answer == 1)
  80. {
  81. gh->battles->restartBattlePrimary(
  82. bi->getBattleID(),
  83. bi->getSideArmy(0),
  84. bi->getSideArmy(1),
  85. bi->getLocation(),
  86. bi->getSideHero(0),
  87. bi->getSideHero(1),
  88. bi->isCreatureBank(),
  89. bi->getDefendedTown()
  90. );
  91. }
  92. else
  93. {
  94. auto hero = bi->getSideHero(BattleSide::ATTACKER);
  95. auto visitingObj = bi->getDefendedTown() ? bi->getDefendedTown() : gh->getVisitingObject(hero);
  96. bool isAiVsHuman = bi->getSidePlayer(1).isValidPlayer() && gh->getPlayerState(bi->getSidePlayer(0))->isHuman() != gh->getPlayerState(bi->getSidePlayer(1))->isHuman();
  97. gh->battles->endBattleConfirm(bi->getBattleID());
  98. if(visitingObj && result && isAiVsHuman)
  99. visitingObj->battleFinished(hero, *result);
  100. }
  101. }