BattleProcessor.cpp 11 KB

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