BattleProcessor.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * BattleProcessor.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 "BattleProcessor.h"
  12. #include "BattleActionProcessor.h"
  13. #include "BattleFlowProcessor.h"
  14. #include "BattleResultProcessor.h"
  15. #include "../CGameHandler.h"
  16. #include "../queries/QueriesProcessor.h"
  17. #include "../queries/BattleQueries.h"
  18. #include "../../lib/TerrainHandler.h"
  19. #include "../../lib/battle/CBattleInfoCallback.h"
  20. #include "../../lib/battle/BattleInfo.h"
  21. #include "../../lib/gameState/CGameState.h"
  22. #include "../../lib/mapping/CMap.h"
  23. #include "../../lib/modding/IdentifierStorage.h"
  24. BattleProcessor::BattleProcessor(CGameHandler * gameHandler)
  25. : gameHandler(gameHandler)
  26. , flowProcessor(std::make_unique<BattleFlowProcessor>(this))
  27. , actionsProcessor(std::make_unique<BattleActionProcessor>(this))
  28. , resultProcessor(std::make_unique<BattleResultProcessor>(this))
  29. {
  30. setGameHandler(gameHandler);
  31. }
  32. BattleProcessor::BattleProcessor():
  33. BattleProcessor(nullptr)
  34. {
  35. }
  36. BattleProcessor::~BattleProcessor() = default;
  37. void BattleProcessor::engageIntoBattle(PlayerColor player)
  38. {
  39. //notify interfaces
  40. PlayerBlocked pb;
  41. pb.player = player;
  42. pb.reason = PlayerBlocked::UPCOMING_BATTLE;
  43. pb.startOrEnd = PlayerBlocked::BLOCKADE_STARTED;
  44. gameHandler->sendAndApply(&pb);
  45. }
  46. void BattleProcessor::restartBattlePrimary(const BattleID & battleID, const CArmedInstance *army1, const CArmedInstance *army2, int3 tile,
  47. const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank,
  48. const CGTownInstance *town)
  49. {
  50. auto battle = gameHandler->gameState()->getBattle(battleID);
  51. auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(battle->sides[0].color));
  52. assert(lastBattleQuery);
  53. //existing battle query for retying auto-combat
  54. if(lastBattleQuery)
  55. {
  56. const CGHeroInstance*heroes[2];
  57. heroes[0] = hero1;
  58. heroes[1] = hero2;
  59. for(int i : {0, 1})
  60. {
  61. if(heroes[i])
  62. {
  63. SetMana restoreInitialMana;
  64. restoreInitialMana.val = lastBattleQuery->initialHeroMana[i];
  65. restoreInitialMana.hid = heroes[i]->id;
  66. gameHandler->sendAndApply(&restoreInitialMana);
  67. }
  68. }
  69. lastBattleQuery->result = std::nullopt;
  70. assert(lastBattleQuery->belligerents[0] == battle->sides[0].armyObject);
  71. assert(lastBattleQuery->belligerents[1] == battle->sides[1].armyObject);
  72. }
  73. BattleCancelled bc;
  74. bc.battleID = battleID;
  75. gameHandler->sendAndApply(&bc);
  76. startBattlePrimary(army1, army2, tile, hero1, hero2, creatureBank, town);
  77. }
  78. void BattleProcessor::startBattlePrimary(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile,
  79. const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank,
  80. const CGTownInstance *town)
  81. {
  82. assert(gameHandler->gameState()->getBattle(army1->getOwner()) == nullptr);
  83. assert(gameHandler->gameState()->getBattle(army2->getOwner()) == nullptr);
  84. engageIntoBattle(army1->tempOwner);
  85. engageIntoBattle(army2->tempOwner);
  86. const CArmedInstance *armies[2];
  87. armies[0] = army1;
  88. armies[1] = army2;
  89. const CGHeroInstance*heroes[2];
  90. heroes[0] = hero1;
  91. heroes[1] = hero2;
  92. auto battleID = setupBattle(tile, armies, heroes, creatureBank, town); //initializes stacks, places creatures on battlefield, blocks and informs player interfaces
  93. const auto * battle = gameHandler->gameState()->getBattle(battleID);
  94. assert(battle);
  95. auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(battle->sides[0].color));
  96. if (lastBattleQuery)
  97. {
  98. lastBattleQuery->battleID = battleID;
  99. }
  100. else
  101. {
  102. auto newBattleQuery = std::make_shared<CBattleQuery>(gameHandler, battle);
  103. // store initial mana to reset if battle has been restarted
  104. for(int i : {0, 1})
  105. if(heroes[i])
  106. newBattleQuery->initialHeroMana[i] = heroes[i]->mana;
  107. gameHandler->queries->addQuery(newBattleQuery);
  108. }
  109. flowProcessor->onBattleStarted(*battle);
  110. }
  111. void BattleProcessor::startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, bool creatureBank)
  112. {
  113. startBattlePrimary(army1, army2, tile,
  114. army1->ID == Obj::HERO ? static_cast<const CGHeroInstance*>(army1) : nullptr,
  115. army2->ID == Obj::HERO ? static_cast<const CGHeroInstance*>(army2) : nullptr,
  116. creatureBank);
  117. }
  118. void BattleProcessor::startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, bool creatureBank)
  119. {
  120. startBattleI(army1, army2, army2->visitablePos(), creatureBank);
  121. }
  122. BattleID BattleProcessor::setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance *heroes[2], bool creatureBank, const CGTownInstance *town)
  123. {
  124. const auto & t = *gameHandler->getTile(tile);
  125. TerrainId terrain = t.terType->getId();
  126. if (gameHandler->gameState()->map->isCoastalTile(tile)) //coastal tile is always ground
  127. terrain = ETerrainId::SAND;
  128. BattleField terType = gameHandler->gameState()->battleGetBattlefieldType(tile, gameHandler->getRandomGenerator());
  129. if (heroes[0] && heroes[0]->boat && heroes[1] && heroes[1]->boat)
  130. terType = BattleField(*VLC->identifiers()->getIdentifier("core", "battlefield.ship_to_ship"));
  131. //send info about battles
  132. BattleStart bs;
  133. bs.info = BattleInfo::setupBattle(tile, terrain, terType, armies, heroes, creatureBank, town);
  134. bs.battleID = gameHandler->gameState()->nextBattleID;
  135. engageIntoBattle(bs.info->sides[0].color);
  136. engageIntoBattle(bs.info->sides[1].color);
  137. auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(bs.info->sides[0].color));
  138. bs.info->replayAllowed = lastBattleQuery == nullptr && !bs.info->sides[1].color.isValidPlayer();
  139. gameHandler->sendAndApply(&bs);
  140. return bs.battleID;
  141. }
  142. bool BattleProcessor::checkBattleStateChanges(const CBattleInfoCallback & battle)
  143. {
  144. //check if drawbridge state need to be changes
  145. if (battle.battleGetSiegeLevel() > 0)
  146. updateGateState(battle);
  147. if (resultProcessor->battleIsEnding(battle))
  148. return true;
  149. //check if battle ended
  150. if (auto result = battle.battleIsFinished())
  151. {
  152. setBattleResult(battle, EBattleResult::NORMAL, *result);
  153. return true;
  154. }
  155. return false;
  156. }
  157. void BattleProcessor::updateGateState(const CBattleInfoCallback & battle)
  158. {
  159. // GATE_BRIDGE - leftmost tile, located over moat
  160. // GATE_OUTER - central tile, mostly covered by gate image
  161. // GATE_INNER - rightmost tile, inside the walls
  162. // GATE_OUTER or GATE_INNER:
  163. // - if defender moves unit on these tiles, bridge will open
  164. // - if there is a creature (dead or alive) on these tiles, bridge will always remain open
  165. // - blocked to attacker if bridge is closed
  166. // GATE_BRIDGE
  167. // - if there is a unit or corpse here, bridge can't open (and can't close in fortress)
  168. // - if Force Field is cast here, bridge can't open (but can close, in any town)
  169. // - deals moat damage to attacker if bridge is closed (fortress only)
  170. bool hasForceFieldOnBridge = !battle.battleGetAllObstaclesOnPos(BattleHex(BattleHex::GATE_BRIDGE), true).empty();
  171. bool hasStackAtGateInner = battle.battleGetUnitByPos(BattleHex(BattleHex::GATE_INNER), false) != nullptr;
  172. bool hasStackAtGateOuter = battle.battleGetUnitByPos(BattleHex(BattleHex::GATE_OUTER), false) != nullptr;
  173. bool hasStackAtGateBridge = battle.battleGetUnitByPos(BattleHex(BattleHex::GATE_BRIDGE), false) != nullptr;
  174. bool hasWideMoat = vstd::contains_if(battle.battleGetAllObstaclesOnPos(BattleHex(BattleHex::GATE_BRIDGE), false), [](const std::shared_ptr<const CObstacleInstance> & obst)
  175. {
  176. return obst->obstacleType == CObstacleInstance::MOAT;
  177. });
  178. BattleUpdateGateState db;
  179. db.state = battle.battleGetGateState();
  180. db.battleID = battle.getBattle()->getBattleID();
  181. if (battle.battleGetWallState(EWallPart::GATE) == EWallState::DESTROYED)
  182. {
  183. db.state = EGateState::DESTROYED;
  184. }
  185. else if (db.state == EGateState::OPENED)
  186. {
  187. bool hasStackOnLongBridge = hasStackAtGateBridge && hasWideMoat;
  188. bool gateCanClose = !hasStackAtGateInner && !hasStackAtGateOuter && !hasStackOnLongBridge;
  189. if (gateCanClose)
  190. db.state = EGateState::CLOSED;
  191. else
  192. db.state = EGateState::OPENED;
  193. }
  194. else // CLOSED or BLOCKED
  195. {
  196. bool gateBlocked = hasForceFieldOnBridge || hasStackAtGateBridge;
  197. if (gateBlocked)
  198. db.state = EGateState::BLOCKED;
  199. else
  200. db.state = EGateState::CLOSED;
  201. }
  202. if (db.state != battle.battleGetGateState())
  203. gameHandler->sendAndApply(&db);
  204. }
  205. bool BattleProcessor::makePlayerBattleAction(const BattleID & battleID, PlayerColor player, const BattleAction &ba)
  206. {
  207. const auto * battle = gameHandler->gameState()->getBattle(battleID);
  208. if (!battle)
  209. return false;
  210. bool result = actionsProcessor->makePlayerBattleAction(*battle, player, ba);
  211. if (gameHandler->gameState()->getBattle(battleID) != nullptr && !resultProcessor->battleIsEnding(*battle))
  212. flowProcessor->onActionMade(*battle, ba);
  213. return result;
  214. }
  215. void BattleProcessor::setBattleResult(const CBattleInfoCallback & battle, EBattleResult resultType, int victoriusSide)
  216. {
  217. resultProcessor->setBattleResult(battle, resultType, victoriusSide);
  218. resultProcessor->endBattle(battle);
  219. }
  220. bool BattleProcessor::makeAutomaticBattleAction(const CBattleInfoCallback & battle, const BattleAction &ba)
  221. {
  222. return actionsProcessor->makeAutomaticBattleAction(battle, ba);
  223. }
  224. void BattleProcessor::endBattleConfirm(const BattleID & battleID)
  225. {
  226. auto battle = gameHandler->gameState()->getBattle(battleID);
  227. assert(battle);
  228. if (!battle)
  229. return;
  230. resultProcessor->endBattleConfirm(*battle);
  231. }
  232. void BattleProcessor::battleAfterLevelUp(const BattleID & battleID, const BattleResult &result)
  233. {
  234. resultProcessor->battleAfterLevelUp(battleID, result);
  235. }
  236. void BattleProcessor::setGameHandler(CGameHandler * newGameHandler)
  237. {
  238. gameHandler = newGameHandler;
  239. actionsProcessor->setGameHandler(newGameHandler);
  240. flowProcessor->setGameHandler(newGameHandler);
  241. resultProcessor->setGameHandler(newGameHandler);
  242. }