BattleProcessor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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/CBattleInfoCallback.h"
  20. #include "../../lib/battle/CObstacleInstance.h"
  21. #include "../../lib/battle/BattleInfo.h"
  22. #include "../../lib/gameState/CGameState.h"
  23. #include "../../lib/mapping/CMap.h"
  24. #include "../../lib/mapObjects/CGHeroInstance.h"
  25. #include "../../lib/modding/IdentifierStorage.h"
  26. #include "../../lib/networkPacks/PacksForClient.h"
  27. #include "../../lib/networkPacks/PacksForClientBattle.h"
  28. #include "../../lib/CPlayerState.h"
  29. BattleProcessor::BattleProcessor(CGameHandler * gameHandler)
  30. : gameHandler(gameHandler)
  31. , flowProcessor(std::make_unique<BattleFlowProcessor>(this))
  32. , actionsProcessor(std::make_unique<BattleActionProcessor>(this))
  33. , resultProcessor(std::make_unique<BattleResultProcessor>(this))
  34. {
  35. setGameHandler(gameHandler);
  36. }
  37. BattleProcessor::BattleProcessor():
  38. BattleProcessor(nullptr)
  39. {
  40. }
  41. BattleProcessor::~BattleProcessor() = default;
  42. void BattleProcessor::engageIntoBattle(PlayerColor player)
  43. {
  44. //notify interfaces
  45. PlayerBlocked pb;
  46. pb.player = player;
  47. pb.reason = PlayerBlocked::UPCOMING_BATTLE;
  48. pb.startOrEnd = PlayerBlocked::BLOCKADE_STARTED;
  49. gameHandler->sendAndApply(&pb);
  50. }
  51. void BattleProcessor::restartBattlePrimary(const BattleID & battleID, const CArmedInstance *army1, const CArmedInstance *army2, int3 tile,
  52. const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank,
  53. const CGTownInstance *town)
  54. {
  55. auto battle = gameHandler->gameState()->getBattle(battleID);
  56. auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(battle->sides[0].color));
  57. assert(lastBattleQuery);
  58. //existing battle query for retying auto-combat
  59. if(lastBattleQuery)
  60. {
  61. const CGHeroInstance*heroes[2];
  62. heroes[0] = hero1;
  63. heroes[1] = hero2;
  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->result = std::nullopt;
  75. assert(lastBattleQuery->belligerents[0] == battle->sides[0].armyObject);
  76. assert(lastBattleQuery->belligerents[1] == battle->sides[1].armyObject);
  77. }
  78. BattleCancelled bc;
  79. bc.battleID = battleID;
  80. gameHandler->sendAndApply(&bc);
  81. startBattlePrimary(army1, army2, tile, hero1, hero2, creatureBank, town);
  82. }
  83. void BattleProcessor::startBattlePrimary(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile,
  84. const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank,
  85. const CGTownInstance *town)
  86. {
  87. assert(gameHandler->gameState()->getBattle(army1->getOwner()) == nullptr);
  88. assert(gameHandler->gameState()->getBattle(army2->getOwner()) == nullptr);
  89. const CArmedInstance *armies[2];
  90. armies[0] = army1;
  91. armies[1] = army2;
  92. const CGHeroInstance*heroes[2];
  93. heroes[0] = hero1;
  94. heroes[1] = hero2;
  95. auto battleID = setupBattle(tile, armies, heroes, creatureBank, town); //initializes stacks, places creatures on battlefield, blocks and informs player interfaces
  96. const auto * battle = gameHandler->gameState()->getBattle(battleID);
  97. assert(battle);
  98. //add battle bonuses based from player state only when attacks neutral creatures
  99. const auto * attackerInfo = gameHandler->getPlayerState(army1->getOwner(), false);
  100. if(attackerInfo && !army2->getOwner().isValidPlayer())
  101. {
  102. for(auto bonus : attackerInfo->battleBonuses)
  103. {
  104. GiveBonus giveBonus(GiveBonus::ETarget::OBJECT);
  105. giveBonus.id = hero1->id;
  106. giveBonus.bonus = bonus;
  107. gameHandler->sendAndApply(&giveBonus);
  108. }
  109. }
  110. auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(battle->sides[0].color));
  111. if (lastBattleQuery)
  112. {
  113. lastBattleQuery->battleID = battleID;
  114. }
  115. else
  116. {
  117. auto newBattleQuery = std::make_shared<CBattleQuery>(gameHandler, battle);
  118. // store initial mana to reset if battle has been restarted
  119. for(int i : {0, 1})
  120. if(heroes[i])
  121. newBattleQuery->initialHeroMana[i] = heroes[i]->mana;
  122. gameHandler->queries->addQuery(newBattleQuery);
  123. }
  124. flowProcessor->onBattleStarted(*battle);
  125. }
  126. void BattleProcessor::startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, bool creatureBank)
  127. {
  128. startBattlePrimary(army1, army2, tile,
  129. army1->ID == Obj::HERO ? static_cast<const CGHeroInstance*>(army1) : nullptr,
  130. army2->ID == Obj::HERO ? static_cast<const CGHeroInstance*>(army2) : nullptr,
  131. creatureBank);
  132. }
  133. void BattleProcessor::startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, bool creatureBank)
  134. {
  135. startBattleI(army1, army2, army2->visitablePos(), creatureBank);
  136. }
  137. BattleID BattleProcessor::setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance *heroes[2], bool creatureBank, const CGTownInstance *town)
  138. {
  139. const auto & t = *gameHandler->getTile(tile);
  140. TerrainId terrain = t.terType->getId();
  141. if (gameHandler->gameState()->map->isCoastalTile(tile)) //coastal tile is always ground
  142. terrain = ETerrainId::SAND;
  143. BattleField terType = gameHandler->gameState()->battleGetBattlefieldType(tile, gameHandler->getRandomGenerator());
  144. if (heroes[0] && heroes[0]->boat && heroes[1] && heroes[1]->boat)
  145. terType = BattleField(*VLC->identifiers()->getIdentifier("core", "battlefield.ship_to_ship"));
  146. //send info about battles
  147. BattleStart bs;
  148. bs.info = BattleInfo::setupBattle(tile, terrain, terType, armies, heroes, creatureBank, town);
  149. bs.battleID = gameHandler->gameState()->nextBattleID;
  150. engageIntoBattle(bs.info->sides[0].color);
  151. engageIntoBattle(bs.info->sides[1].color);
  152. auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(bs.info->sides[0].color));
  153. bs.info->replayAllowed = lastBattleQuery == nullptr && !bs.info->sides[1].color.isValidPlayer();
  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.battleGetSiegeLevel() > 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, int 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. }
  251. void BattleProcessor::setGameHandler(CGameHandler * newGameHandler)
  252. {
  253. gameHandler = newGameHandler;
  254. actionsProcessor->setGameHandler(newGameHandler);
  255. flowProcessor->setGameHandler(newGameHandler);
  256. resultProcessor->setGameHandler(newGameHandler);
  257. }