BattleProcessor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 "../CVCMIServer.h"
  17. #include "../processors/HeroPoolProcessor.h"
  18. #include "../queries/QueriesProcessor.h"
  19. #include "../queries/BattleQueries.h"
  20. #include "../../lib/ArtifactUtils.h"
  21. #include "../../lib/CGeneralTextHandler.h"
  22. #include "../../lib/CStack.h"
  23. #include "../../lib/CondSh.h"
  24. #include "../../lib/GameSettings.h"
  25. #include "../../lib/ScopeGuard.h"
  26. #include "../../lib/TerrainHandler.h"
  27. #include "../../lib/UnlockGuard.h"
  28. #include "../../lib/battle/BattleInfo.h"
  29. #include "../../lib/battle/CUnitState.h"
  30. #include "../../lib/gameState/CGameState.h"
  31. #include "../../lib/mapObjects/CGTownInstance.h"
  32. #include "../../lib/mapping/CMap.h"
  33. #include "../../lib/modding/IdentifierStorage.h"
  34. #include "../../lib/serializer/Cast.h"
  35. #include "../../lib/spells/AbilityCaster.h"
  36. #include "../../lib/spells/BonusCaster.h"
  37. #include "../../lib/spells/CSpellHandler.h"
  38. #include "../../lib/spells/ISpellMechanics.h"
  39. #include "../../lib/spells/ObstacleCasterProxy.h"
  40. #include "../../lib/spells/Problem.h"
  41. #define COMPLAIN_RET_IF(cond, txt) do {if (cond){gameHandler->complain(txt); return;}} while(0)
  42. #define COMPLAIN_RET_FALSE_IF(cond, txt) do {if (cond){gameHandler->complain(txt); return false;}} while(0)
  43. #define COMPLAIN_RET(txt) {gameHandler->complain(txt); return false;}
  44. #define COMPLAIN_RETF(txt, FORMAT) {gameHandler->complain(boost::str(boost::format(txt) % FORMAT)); return false;}
  45. BattleProcessor::BattleProcessor(CGameHandler * gameHandler)
  46. : gameHandler(gameHandler)
  47. , flowProcessor(std::make_unique<BattleFlowProcessor>(this))
  48. , actionsProcessor(std::make_unique<BattleActionProcessor>(this))
  49. , resultProcessor(std::make_unique<BattleResultProcessor>(this))
  50. {
  51. setGameHandler(gameHandler);
  52. }
  53. BattleProcessor::BattleProcessor():
  54. BattleProcessor(nullptr)
  55. {
  56. }
  57. BattleProcessor::~BattleProcessor() = default;
  58. void BattleProcessor::engageIntoBattle(PlayerColor player)
  59. {
  60. //notify interfaces
  61. PlayerBlocked pb;
  62. pb.player = player;
  63. pb.reason = PlayerBlocked::UPCOMING_BATTLE;
  64. pb.startOrEnd = PlayerBlocked::BLOCKADE_STARTED;
  65. gameHandler->sendAndApply(&pb);
  66. }
  67. void BattleProcessor::startBattlePrimary(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile,
  68. const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank,
  69. const CGTownInstance *town) //use hero=nullptr for no hero
  70. {
  71. if(gameHandler->gameState()->curB)
  72. gameHandler->gameState()->curB.dellNull();
  73. engageIntoBattle(army1->tempOwner);
  74. engageIntoBattle(army2->tempOwner);
  75. static const CArmedInstance *armies[2];
  76. armies[0] = army1;
  77. armies[1] = army2;
  78. static const CGHeroInstance*heroes[2];
  79. heroes[0] = hero1;
  80. heroes[1] = hero2;
  81. setupBattle(tile, armies, heroes, creatureBank, town); //initializes stacks, places creatures on battlefield, blocks and informs player interfaces
  82. auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(gameHandler->gameState()->curB->sides[0].color));
  83. //existing battle query for retying auto-combat
  84. if(lastBattleQuery)
  85. {
  86. for(int i : {0, 1})
  87. {
  88. if(heroes[i])
  89. {
  90. SetMana restoreInitialMana;
  91. restoreInitialMana.val = lastBattleQuery->initialHeroMana[i];
  92. restoreInitialMana.hid = heroes[i]->id;
  93. gameHandler->sendAndApply(&restoreInitialMana);
  94. }
  95. }
  96. lastBattleQuery->bi = gameHandler->gameState()->curB;
  97. lastBattleQuery->result = std::nullopt;
  98. lastBattleQuery->belligerents[0] = gameHandler->gameState()->curB->sides[0].armyObject;
  99. lastBattleQuery->belligerents[1] = gameHandler->gameState()->curB->sides[1].armyObject;
  100. }
  101. auto nextBattleQuery = std::make_shared<CBattleQuery>(gameHandler, gameHandler->gameState()->curB);
  102. for(int i : {0, 1})
  103. {
  104. if(heroes[i])
  105. {
  106. nextBattleQuery->initialHeroMana[i] = heroes[i]->mana;
  107. }
  108. }
  109. gameHandler->queries->addQuery(nextBattleQuery);
  110. flowProcessor->onBattleStarted();
  111. }
  112. void BattleProcessor::startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, bool creatureBank)
  113. {
  114. startBattlePrimary(army1, army2, tile,
  115. army1->ID == Obj::HERO ? static_cast<const CGHeroInstance*>(army1) : nullptr,
  116. army2->ID == Obj::HERO ? static_cast<const CGHeroInstance*>(army2) : nullptr,
  117. creatureBank);
  118. }
  119. void BattleProcessor::startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, bool creatureBank)
  120. {
  121. startBattleI(army1, army2, army2->visitablePos(), creatureBank);
  122. }
  123. void BattleProcessor::setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance *heroes[2], bool creatureBank, const CGTownInstance *town)
  124. {
  125. const auto & t = *gameHandler->getTile(tile);
  126. TerrainId terrain = t.terType->getId();
  127. if (gameHandler->gameState()->map->isCoastalTile(tile)) //coastal tile is always ground
  128. terrain = ETerrainId::SAND;
  129. BattleField terType = gameHandler->gameState()->battleGetBattlefieldType(tile, gameHandler->getRandomGenerator());
  130. if (heroes[0] && heroes[0]->boat && heroes[1] && heroes[1]->boat)
  131. terType = BattleField(*VLC->identifiers()->getIdentifier("core", "battlefield.ship_to_ship"));
  132. //send info about battles
  133. BattleStart bs;
  134. bs.info = BattleInfo::setupBattle(tile, terrain, terType, armies, heroes, creatureBank, town);
  135. engageIntoBattle(bs.info->sides[0].color);
  136. engageIntoBattle(bs.info->sides[1].color);
  137. auto lastBattleQuery = std::dynamic_pointer_cast<CBattleQuery>(gameHandler->queries->topQuery(bs.info->sides[0].color));
  138. bs.info->replayAllowed = lastBattleQuery == nullptr && !bs.info->sides[1].color.isValidPlayer();
  139. gameHandler->sendAndApply(&bs);
  140. }
  141. void BattleProcessor::checkBattleStateChanges()
  142. {
  143. //check if drawbridge state need to be changes
  144. if (gameHandler->battleGetSiegeLevel() > 0)
  145. updateGateState();
  146. //check if battle ended
  147. if (auto result = gameHandler->battleIsFinished())
  148. {
  149. setBattleResult(EBattleResult::NORMAL, *result);
  150. }
  151. }
  152. void BattleProcessor::updateGateState()
  153. {
  154. // GATE_BRIDGE - leftmost tile, located over moat
  155. // GATE_OUTER - central tile, mostly covered by gate image
  156. // GATE_INNER - rightmost tile, inside the walls
  157. // GATE_OUTER or GATE_INNER:
  158. // - if defender moves unit on these tiles, bridge will open
  159. // - if there is a creature (dead or alive) on these tiles, bridge will always remain open
  160. // - blocked to attacker if bridge is closed
  161. // GATE_BRIDGE
  162. // - if there is a unit or corpse here, bridge can't open (and can't close in fortress)
  163. // - if Force Field is cast here, bridge can't open (but can close, in any town)
  164. // - deals moat damage to attacker if bridge is closed (fortress only)
  165. bool hasForceFieldOnBridge = !gameHandler->battleGetAllObstaclesOnPos(BattleHex(ESiegeHex::GATE_BRIDGE), true).empty();
  166. bool hasStackAtGateInner = gameHandler->gameState()->curB->battleGetUnitByPos(BattleHex(ESiegeHex::GATE_INNER), false) != nullptr;
  167. bool hasStackAtGateOuter = gameHandler->gameState()->curB->battleGetUnitByPos(BattleHex(ESiegeHex::GATE_OUTER), false) != nullptr;
  168. bool hasStackAtGateBridge = gameHandler->gameState()->curB->battleGetUnitByPos(BattleHex(ESiegeHex::GATE_BRIDGE), false) != nullptr;
  169. bool hasWideMoat = vstd::contains_if(gameHandler->battleGetAllObstaclesOnPos(BattleHex(ESiegeHex::GATE_BRIDGE), false), [](const std::shared_ptr<const CObstacleInstance> & obst)
  170. {
  171. return obst->obstacleType == CObstacleInstance::MOAT;
  172. });
  173. BattleUpdateGateState db;
  174. db.state = gameHandler->gameState()->curB->si.gateState;
  175. if (gameHandler->gameState()->curB->si.wallState[EWallPart::GATE] == EWallState::DESTROYED)
  176. {
  177. db.state = EGateState::DESTROYED;
  178. }
  179. else if (db.state == EGateState::OPENED)
  180. {
  181. bool hasStackOnLongBridge = hasStackAtGateBridge && hasWideMoat;
  182. bool gateCanClose = !hasStackAtGateInner && !hasStackAtGateOuter && !hasStackOnLongBridge;
  183. if (gateCanClose)
  184. db.state = EGateState::CLOSED;
  185. else
  186. db.state = EGateState::OPENED;
  187. }
  188. else // CLOSED or BLOCKED
  189. {
  190. bool gateBlocked = hasForceFieldOnBridge || hasStackAtGateBridge;
  191. if (gateBlocked)
  192. db.state = EGateState::BLOCKED;
  193. else
  194. db.state = EGateState::CLOSED;
  195. }
  196. if (db.state != gameHandler->gameState()->curB->si.gateState)
  197. gameHandler->sendAndApply(&db);
  198. }
  199. bool BattleProcessor::makeBattleAction(PlayerColor player, BattleAction &ba)
  200. {
  201. const BattleInfo * b = gameHandler->gameState()->curB;
  202. if(!b && gameHandler->complain("Can not make action - there is no battle ongoing!"))
  203. return false;
  204. if (ba.side != 0 && ba.side != 1 && gameHandler->complain("Can not make action - invalid battle side!"))
  205. return false;
  206. if(b->tacticDistance)
  207. {
  208. if(ba.actionType != EActionType::WALK && ba.actionType != EActionType::END_TACTIC_PHASE
  209. && ba.actionType != EActionType::RETREAT && ba.actionType != EActionType::SURRENDER)
  210. {
  211. gameHandler->complain("Can not make actions while in tactics mode!");
  212. return false;
  213. }
  214. if(player != b->sides[ba.side].color)
  215. {
  216. gameHandler->complain("Can not make actions in battles you are not part of!");
  217. return false;
  218. }
  219. }
  220. else
  221. {
  222. auto active = b->battleActiveUnit();
  223. if(!active && gameHandler->complain("No active unit in battle!"))
  224. return false;
  225. auto unitOwner = b->battleGetOwner(active);
  226. if(player != unitOwner && gameHandler->complain("Can not make actions in battles you are not part of!"))
  227. return false;
  228. }
  229. return makeBattleAction(ba);
  230. }
  231. void BattleProcessor::setBattleResult(EBattleResult resultType, int victoriusSide)
  232. {
  233. resultProcessor->setBattleResult(resultType, victoriusSide);
  234. resultProcessor->endBattle(gameHandler->gameState()->curB->tile, gameHandler->gameState()->curB->battleGetFightingHero(0), gameHandler->gameState()->curB->battleGetFightingHero(1));
  235. }
  236. bool BattleProcessor::makeBattleAction(const BattleAction &ba)
  237. {
  238. bool result = actionsProcessor->makeBattleAction(ba);
  239. flowProcessor->onActionMade(ba);
  240. return result;
  241. }
  242. void BattleProcessor::endBattleConfirm(const BattleInfo * battleInfo)
  243. {
  244. resultProcessor->endBattleConfirm(battleInfo);
  245. }
  246. void BattleProcessor::battleAfterLevelUp(const BattleResult &result)
  247. {
  248. resultProcessor->battleAfterLevelUp(result);
  249. }
  250. void BattleProcessor::setGameHandler(CGameHandler * newGameHandler)
  251. {
  252. gameHandler = newGameHandler;
  253. actionsProcessor->setGameHandler(newGameHandler);
  254. flowProcessor->setGameHandler(newGameHandler);
  255. resultProcessor->setGameHandler(newGameHandler);
  256. }