BattleProcessor.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/battle/BattleLayout.h"
  24. #include "../../lib/entities/building/TownFortifications.h"
  25. #include "../../lib/gameState/CGameState.h"
  26. #include "../../lib/mapping/CMap.h"
  27. #include "../../lib/mapObjects/CGHeroInstance.h"
  28. #include "../../lib/modding/IdentifierStorage.h"
  29. #include "../../lib/networkPacks/PacksForClient.h"
  30. #include "../../lib/networkPacks/PacksForClientBattle.h"
  31. #include "../../lib/CPlayerState.h"
  32. BattleProcessor::BattleProcessor(CGameHandler * gameHandler)
  33. : gameHandler(gameHandler)
  34. , flowProcessor(std::make_unique<BattleFlowProcessor>(this, gameHandler))
  35. , actionsProcessor(std::make_unique<BattleActionProcessor>(this, gameHandler))
  36. , resultProcessor(std::make_unique<BattleResultProcessor>(this, gameHandler))
  37. {
  38. }
  39. BattleProcessor::~BattleProcessor() = default;
  40. void BattleProcessor::engageIntoBattle(PlayerColor player)
  41. {
  42. //notify interfaces
  43. PlayerBlocked pb;
  44. pb.player = player;
  45. pb.reason = PlayerBlocked::UPCOMING_BATTLE;
  46. pb.startOrEnd = PlayerBlocked::BLOCKADE_STARTED;
  47. gameHandler->sendAndApply(pb);
  48. }
  49. void BattleProcessor::restartBattle(const BattleID & battleID, const CArmedInstance *army1, const CArmedInstance *army2, int3 tile,
  50. const CGHeroInstance *hero1, const CGHeroInstance *hero2, const BattleLayout & layout, 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. startBattle(army1, army2, tile, hero1, hero2, layout, town);
  79. }
  80. void BattleProcessor::startBattle(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile,
  81. const CGHeroInstance *hero1, const CGHeroInstance *hero2, const BattleLayout & layout, 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, layout, 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::startBattle(const CArmedInstance *army1, const CArmedInstance *army2)
  121. {
  122. startBattle(army1, army2, army2->visitablePos(),
  123. army1->ID == Obj::HERO ? dynamic_cast<const CGHeroInstance*>(army1) : nullptr,
  124. army2->ID == Obj::HERO ? dynamic_cast<const CGHeroInstance*>(army2) : nullptr,
  125. BattleLayout::createDefaultLayout(gameHandler, army1, army2),
  126. nullptr);
  127. }
  128. BattleID BattleProcessor::setupBattle(int3 tile, BattleSideArray<const CArmedInstance *> armies, BattleSideArray<const CGHeroInstance *> heroes, const BattleLayout & layout, const CGTownInstance *town)
  129. {
  130. const auto & t = *gameHandler->getTile(tile);
  131. TerrainId terrain = t.terType->getId();
  132. if (gameHandler->gameState()->map->isCoastalTile(tile)) //coastal tile is always ground
  133. terrain = ETerrainId::SAND;
  134. BattleField terType = gameHandler->gameState()->battleGetBattlefieldType(tile, gameHandler->getRandomGenerator());
  135. if (heroes[BattleSide::ATTACKER] && heroes[BattleSide::ATTACKER]->boat && heroes[BattleSide::DEFENDER] && heroes[BattleSide::DEFENDER]->boat)
  136. terType = BattleField(*VLC->identifiers()->getIdentifier("core", "battlefield.ship_to_ship"));
  137. //send info about battles
  138. BattleStart bs;
  139. bs.info = BattleInfo::setupBattle(tile, terrain, terType, armies, heroes, layout, town);
  140. bs.battleID = gameHandler->gameState()->nextBattleID;
  141. engageIntoBattle(bs.info->getSide(BattleSide::ATTACKER).color);
  142. engageIntoBattle(bs.info->getSide(BattleSide::DEFENDER).color);
  143. auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(bs.info->getSide(BattleSide::ATTACKER).color));
  144. if(!lastBattleQuery)
  145. lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(bs.info->getSide(BattleSide::DEFENDER).color));
  146. bool isDefenderHuman = bs.info->getSide(BattleSide::DEFENDER).color.isValidPlayer() && gameHandler->getPlayerState(bs.info->getSide(BattleSide::DEFENDER).color)->isHuman();
  147. bool isAttackerHuman = gameHandler->getPlayerState(bs.info->getSide(BattleSide::ATTACKER).color)->isHuman();
  148. bool onlyOnePlayerHuman = isDefenderHuman != isAttackerHuman;
  149. bs.info->replayAllowed = lastBattleQuery == nullptr && onlyOnePlayerHuman;
  150. gameHandler->sendAndApply(bs);
  151. return bs.battleID;
  152. }
  153. bool BattleProcessor::checkBattleStateChanges(const CBattleInfoCallback & battle)
  154. {
  155. //check if drawbridge state need to be changes
  156. if (battle.battleGetFortifications().wallsHealth > 0)
  157. updateGateState(battle);
  158. if (resultProcessor->battleIsEnding(battle))
  159. return true;
  160. //check if battle ended
  161. if (auto result = battle.battleIsFinished())
  162. {
  163. setBattleResult(battle, EBattleResult::NORMAL, *result);
  164. return true;
  165. }
  166. return false;
  167. }
  168. void BattleProcessor::updateGateState(const CBattleInfoCallback & battle)
  169. {
  170. // GATE_BRIDGE - leftmost tile, located over moat
  171. // GATE_OUTER - central tile, mostly covered by gate image
  172. // GATE_INNER - rightmost tile, inside the walls
  173. // GATE_OUTER or GATE_INNER:
  174. // - if defender moves unit on these tiles, bridge will open
  175. // - if there is a creature (dead or alive) on these tiles, bridge will always remain open
  176. // - blocked to attacker if bridge is closed
  177. // GATE_BRIDGE
  178. // - if there is a unit or corpse here, bridge can't open (and can't close in fortress)
  179. // - if Force Field is cast here, bridge can't open (but can close, in any town)
  180. // - deals moat damage to attacker if bridge is closed (fortress only)
  181. bool hasForceFieldOnBridge = !battle.battleGetAllObstaclesOnPos(BattleHex(BattleHex::GATE_BRIDGE), true).empty();
  182. bool hasStackAtGateInner = battle.battleGetUnitByPos(BattleHex(BattleHex::GATE_INNER), false) != nullptr;
  183. bool hasStackAtGateOuter = battle.battleGetUnitByPos(BattleHex(BattleHex::GATE_OUTER), false) != nullptr;
  184. bool hasStackAtGateBridge = battle.battleGetUnitByPos(BattleHex(BattleHex::GATE_BRIDGE), false) != nullptr;
  185. bool hasWideMoat = vstd::contains_if(battle.battleGetAllObstaclesOnPos(BattleHex(BattleHex::GATE_BRIDGE), false), [](const std::shared_ptr<const CObstacleInstance> & obst)
  186. {
  187. return obst->obstacleType == CObstacleInstance::MOAT;
  188. });
  189. BattleUpdateGateState db;
  190. db.state = battle.battleGetGateState();
  191. db.battleID = battle.getBattle()->getBattleID();
  192. if (battle.battleGetWallState(EWallPart::GATE) == EWallState::DESTROYED)
  193. {
  194. db.state = EGateState::DESTROYED;
  195. }
  196. else if (db.state == EGateState::OPENED)
  197. {
  198. bool hasStackOnLongBridge = hasStackAtGateBridge && hasWideMoat;
  199. bool gateCanClose = !hasStackAtGateInner && !hasStackAtGateOuter && !hasStackOnLongBridge;
  200. if (gateCanClose)
  201. db.state = EGateState::CLOSED;
  202. else
  203. db.state = EGateState::OPENED;
  204. }
  205. else // CLOSED or BLOCKED
  206. {
  207. bool gateBlocked = hasForceFieldOnBridge || hasStackAtGateBridge;
  208. if (gateBlocked)
  209. db.state = EGateState::BLOCKED;
  210. else
  211. db.state = EGateState::CLOSED;
  212. }
  213. if (db.state != battle.battleGetGateState())
  214. gameHandler->sendAndApply(db);
  215. }
  216. bool BattleProcessor::makePlayerBattleAction(const BattleID & battleID, PlayerColor player, const BattleAction &ba)
  217. {
  218. const auto * battle = gameHandler->gameState()->getBattle(battleID);
  219. if (!battle)
  220. return false;
  221. bool result = actionsProcessor->makePlayerBattleAction(*battle, player, ba);
  222. if (gameHandler->gameState()->getBattle(battleID) != nullptr && !resultProcessor->battleIsEnding(*battle))
  223. flowProcessor->onActionMade(*battle, ba);
  224. return result;
  225. }
  226. void BattleProcessor::setBattleResult(const CBattleInfoCallback & battle, EBattleResult resultType, BattleSide victoriusSide)
  227. {
  228. resultProcessor->setBattleResult(battle, resultType, victoriusSide);
  229. resultProcessor->endBattle(battle);
  230. }
  231. bool BattleProcessor::makeAutomaticBattleAction(const CBattleInfoCallback & battle, const BattleAction &ba)
  232. {
  233. return actionsProcessor->makeAutomaticBattleAction(battle, ba);
  234. }
  235. void BattleProcessor::endBattleConfirm(const BattleID & battleID)
  236. {
  237. auto battle = gameHandler->gameState()->getBattle(battleID);
  238. assert(battle);
  239. if (!battle)
  240. return;
  241. resultProcessor->endBattleConfirm(*battle);
  242. }
  243. void BattleProcessor::battleAfterLevelUp(const BattleID & battleID, const BattleResult &result)
  244. {
  245. resultProcessor->battleAfterLevelUp(battleID, result);
  246. }