BattleProcessor.cpp 11 KB

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