2
0

BattleProcessor.cpp 10 KB

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