BattleProcessor.cpp 8.5 KB

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