BattleProcessor.cpp 11 KB

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