BattleProcessor.cpp 8.6 KB

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