BattleProcessor.cpp 12 KB

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