BattleProcessor.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. //check if battle ended
  125. if (auto result = gameHandler->battleIsFinished())
  126. {
  127. setBattleResult(EBattleResult::NORMAL, *result);
  128. return true;
  129. }
  130. return false;
  131. }
  132. void BattleProcessor::updateGateState()
  133. {
  134. // GATE_BRIDGE - leftmost tile, located over moat
  135. // GATE_OUTER - central tile, mostly covered by gate image
  136. // GATE_INNER - rightmost tile, inside the walls
  137. // GATE_OUTER or GATE_INNER:
  138. // - if defender moves unit on these tiles, bridge will open
  139. // - if there is a creature (dead or alive) on these tiles, bridge will always remain open
  140. // - blocked to attacker if bridge is closed
  141. // GATE_BRIDGE
  142. // - if there is a unit or corpse here, bridge can't open (and can't close in fortress)
  143. // - if Force Field is cast here, bridge can't open (but can close, in any town)
  144. // - deals moat damage to attacker if bridge is closed (fortress only)
  145. bool hasForceFieldOnBridge = !gameHandler->battleGetAllObstaclesOnPos(BattleHex(ESiegeHex::GATE_BRIDGE), true).empty();
  146. bool hasStackAtGateInner = gameHandler->gameState()->curB->battleGetUnitByPos(BattleHex(ESiegeHex::GATE_INNER), false) != nullptr;
  147. bool hasStackAtGateOuter = gameHandler->gameState()->curB->battleGetUnitByPos(BattleHex(ESiegeHex::GATE_OUTER), false) != nullptr;
  148. bool hasStackAtGateBridge = gameHandler->gameState()->curB->battleGetUnitByPos(BattleHex(ESiegeHex::GATE_BRIDGE), false) != nullptr;
  149. bool hasWideMoat = vstd::contains_if(gameHandler->battleGetAllObstaclesOnPos(BattleHex(ESiegeHex::GATE_BRIDGE), false), [](const std::shared_ptr<const CObstacleInstance> & obst)
  150. {
  151. return obst->obstacleType == CObstacleInstance::MOAT;
  152. });
  153. BattleUpdateGateState db;
  154. db.state = gameHandler->gameState()->curB->si.gateState;
  155. if (gameHandler->gameState()->curB->si.wallState[EWallPart::GATE] == EWallState::DESTROYED)
  156. {
  157. db.state = EGateState::DESTROYED;
  158. }
  159. else if (db.state == EGateState::OPENED)
  160. {
  161. bool hasStackOnLongBridge = hasStackAtGateBridge && hasWideMoat;
  162. bool gateCanClose = !hasStackAtGateInner && !hasStackAtGateOuter && !hasStackOnLongBridge;
  163. if (gateCanClose)
  164. db.state = EGateState::CLOSED;
  165. else
  166. db.state = EGateState::OPENED;
  167. }
  168. else // CLOSED or BLOCKED
  169. {
  170. bool gateBlocked = hasForceFieldOnBridge || hasStackAtGateBridge;
  171. if (gateBlocked)
  172. db.state = EGateState::BLOCKED;
  173. else
  174. db.state = EGateState::CLOSED;
  175. }
  176. if (db.state != gameHandler->gameState()->curB->si.gateState)
  177. gameHandler->sendAndApply(&db);
  178. }
  179. bool BattleProcessor::makePlayerBattleAction(PlayerColor player, const BattleAction &ba)
  180. {
  181. bool result = actionsProcessor->makePlayerBattleAction(player, ba);
  182. flowProcessor->onActionMade(ba);
  183. return result;
  184. }
  185. void BattleProcessor::setBattleResult(EBattleResult resultType, int victoriusSide)
  186. {
  187. resultProcessor->setBattleResult(resultType, victoriusSide);
  188. resultProcessor->endBattle(gameHandler->gameState()->curB->tile, gameHandler->gameState()->curB->battleGetFightingHero(0), gameHandler->gameState()->curB->battleGetFightingHero(1));
  189. }
  190. bool BattleProcessor::makeAutomaticBattleAction(const BattleAction &ba)
  191. {
  192. return actionsProcessor->makeAutomaticBattleAction(ba);
  193. }
  194. void BattleProcessor::endBattleConfirm(const BattleInfo * battleInfo)
  195. {
  196. resultProcessor->endBattleConfirm(battleInfo);
  197. }
  198. void BattleProcessor::battleAfterLevelUp(const BattleResult &result)
  199. {
  200. resultProcessor->battleAfterLevelUp(result);
  201. }
  202. void BattleProcessor::setGameHandler(CGameHandler * newGameHandler)
  203. {
  204. gameHandler = newGameHandler;
  205. actionsProcessor->setGameHandler(newGameHandler);
  206. flowProcessor->setGameHandler(newGameHandler);
  207. resultProcessor->setGameHandler(newGameHandler);
  208. }