BattleActionsController.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*
  2. * BattleActionsController.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 "BattleActionsController.h"
  12. #include "BattleWindow.h"
  13. #include "BattleStacksController.h"
  14. #include "BattleInterface.h"
  15. #include "BattleFieldController.h"
  16. #include "BattleSiegeController.h"
  17. #include "BattleInterfaceClasses.h"
  18. #include "../CGameInfo.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../gui/CursorHandler.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../gui/CIntObject.h"
  23. #include "../windows/CCreatureWindow.h"
  24. #include "../../CCallback.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/CGeneralTextHandler.h"
  27. #include "../../lib/CStack.h"
  28. #include "../../lib/battle/BattleAction.h"
  29. #include "../../lib/spells/CSpellHandler.h"
  30. #include "../../lib/spells/ISpellMechanics.h"
  31. #include "../../lib/spells/Problem.h"
  32. struct TextReplacement
  33. {
  34. std::string placeholder;
  35. std::string replacement;
  36. };
  37. using TextReplacementList = std::vector<TextReplacement>;
  38. static std::string replacePlaceholders(std::string input, const TextReplacementList & format )
  39. {
  40. for(const auto & entry : format)
  41. boost::replace_all(input, entry.placeholder, entry.replacement);
  42. return input;
  43. }
  44. static std::string translatePlural(int amount, const std::string& baseTextID)
  45. {
  46. if(amount == 1)
  47. return CGI->generaltexth->translate(baseTextID + ".1");
  48. return CGI->generaltexth->translate(baseTextID);
  49. }
  50. static std::string formatPluralImpl(int amount, const std::string & amountString, const std::string & baseTextID)
  51. {
  52. std::string baseString = translatePlural(amount, baseTextID);
  53. TextReplacementList replacements {
  54. { "%d", amountString }
  55. };
  56. return replacePlaceholders(baseString, replacements);
  57. }
  58. static std::string formatPlural(int amount, const std::string & baseTextID)
  59. {
  60. return formatPluralImpl(amount, std::to_string(amount), baseTextID);
  61. }
  62. static std::string formatPlural(DamageRange range, const std::string & baseTextID)
  63. {
  64. if (range.min == range.max)
  65. return formatPlural(range.min, baseTextID);
  66. std::string rangeString = std::to_string(range.min) + " - " + std::to_string(range.max);
  67. return formatPluralImpl(range.max, rangeString, baseTextID);
  68. }
  69. static std::string formatAttack(const DamageEstimation & estimation, const std::string & creatureName, const std::string & baseTextID, int shotsLeft)
  70. {
  71. TextReplacementList replacements = {
  72. { "%CREATURE", creatureName },
  73. { "%DAMAGE", formatPlural(estimation.damage, "vcmi.battleWindow.damageEstimation.damage") },
  74. { "%SHOTS", formatPlural(shotsLeft, "vcmi.battleWindow.damageEstimation.shots") },
  75. { "%KILLS", formatPlural(estimation.kills, "vcmi.battleWindow.damageEstimation.kills") },
  76. };
  77. return replacePlaceholders(CGI->generaltexth->translate(baseTextID), replacements);
  78. }
  79. static std::string formatMeleeAttack(const DamageEstimation & estimation, const std::string & creatureName)
  80. {
  81. std::string baseTextID = estimation.kills.max == 0 ?
  82. "vcmi.battleWindow.damageEstimation.melee" :
  83. "vcmi.battleWindow.damageEstimation.meleeKills";
  84. return formatAttack(estimation, creatureName, baseTextID, 0);
  85. }
  86. static std::string formatRangedAttack(const DamageEstimation & estimation, const std::string & creatureName, int shotsLeft)
  87. {
  88. std::string baseTextID = estimation.kills.max == 0 ?
  89. "vcmi.battleWindow.damageEstimation.ranged" :
  90. "vcmi.battleWindow.damageEstimation.rangedKills";
  91. return formatAttack(estimation, creatureName, baseTextID, shotsLeft);
  92. }
  93. BattleActionsController::BattleActionsController(BattleInterface & owner):
  94. owner(owner),
  95. heroSpellToCast(nullptr)
  96. {}
  97. void BattleActionsController::endCastingSpell()
  98. {
  99. if(heroSpellToCast)
  100. heroSpellToCast.reset();
  101. if(owner.stacksController->getActiveStack())
  102. possibleActions = getPossibleActionsForStack(owner.stacksController->getActiveStack()); //restore actions after they were cleared
  103. GH.fakeMouseMove();
  104. }
  105. bool BattleActionsController::isActiveStackSpellcaster() const
  106. {
  107. const CStack * casterStack = owner.stacksController->getActiveStack();
  108. if (!casterStack)
  109. return false;
  110. bool spellcaster = casterStack->hasBonusOfType(Bonus::SPELLCASTER);
  111. return (spellcaster && casterStack->canCast());
  112. }
  113. void BattleActionsController::enterCreatureCastingMode()
  114. {
  115. //silently check for possible errors
  116. if (owner.tacticsMode)
  117. return;
  118. //hero is casting a spell
  119. if (heroSpellToCast)
  120. return;
  121. if (!owner.stacksController->getActiveStack())
  122. return;
  123. if (!isActiveStackSpellcaster())
  124. return;
  125. for (auto const & action : possibleActions)
  126. {
  127. if (action.get() != PossiblePlayerBattleAction::NO_LOCATION)
  128. continue;
  129. const spells::Caster * caster = owner.stacksController->getActiveStack();
  130. const CSpell * spell = action.spell().toSpell();
  131. spells::Target target;
  132. target.emplace_back();
  133. spells::BattleCast cast(owner.curInt->cb.get(), caster, spells::Mode::CREATURE_ACTIVE, spell);
  134. auto m = spell->battleMechanics(&cast);
  135. spells::detail::ProblemImpl ignored;
  136. const bool isCastingPossible = m->canBeCastAt(target, ignored);
  137. if (isCastingPossible)
  138. {
  139. owner.giveCommand(EActionType::MONSTER_SPELL, BattleHex::INVALID, spell->getId());
  140. owner.stacksController->setSelectedStack(nullptr);
  141. CCS->curh->set(Cursor::Combat::POINTER);
  142. }
  143. return;
  144. }
  145. possibleActions = getPossibleActionsForStack(owner.stacksController->getActiveStack());
  146. auto actionFilterPredicate = [](const PossiblePlayerBattleAction x)
  147. {
  148. return !x.spellcast();
  149. };
  150. vstd::erase_if(possibleActions, actionFilterPredicate);
  151. GH.fakeMouseMove();
  152. }
  153. std::vector<PossiblePlayerBattleAction> BattleActionsController::getPossibleActionsForStack(const CStack *stack) const
  154. {
  155. BattleClientInterfaceData data; //hard to get rid of these things so for now they're required data to pass
  156. for (auto const & spell : creatureSpells)
  157. data.creatureSpellsToCast.push_back(spell->id);
  158. data.tacticsMode = owner.tacticsMode;
  159. auto allActions = owner.curInt->cb->getClientActionsForStack(stack, data);
  160. allActions.push_back(PossiblePlayerBattleAction::HERO_INFO);
  161. allActions.push_back(PossiblePlayerBattleAction::CREATURE_INFO);
  162. return std::vector<PossiblePlayerBattleAction>(allActions);
  163. }
  164. void BattleActionsController::reorderPossibleActionsPriority(const CStack * stack, MouseHoveredHexContext context)
  165. {
  166. if(owner.tacticsMode || possibleActions.empty()) return; //this function is not supposed to be called in tactics mode or before getPossibleActionsForStack
  167. auto assignPriority = [&](PossiblePlayerBattleAction const & item) -> uint8_t //large lambda assigning priority which would have to be part of possibleActions without it
  168. {
  169. switch(item.get())
  170. {
  171. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  172. case PossiblePlayerBattleAction::ANY_LOCATION:
  173. case PossiblePlayerBattleAction::NO_LOCATION:
  174. case PossiblePlayerBattleAction::FREE_LOCATION:
  175. case PossiblePlayerBattleAction::OBSTACLE:
  176. if(!stack->hasBonusOfType(Bonus::NO_SPELLCAST_BY_DEFAULT) && context == MouseHoveredHexContext::OCCUPIED_HEX)
  177. return 1;
  178. else
  179. return 100;//bottom priority
  180. break;
  181. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  182. return 2; break;
  183. case PossiblePlayerBattleAction::SHOOT:
  184. return 4; break;
  185. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  186. return 5; break;
  187. case PossiblePlayerBattleAction::ATTACK:
  188. return 6; break;
  189. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  190. return 7; break;
  191. case PossiblePlayerBattleAction::MOVE_STACK:
  192. return 8; break;
  193. case PossiblePlayerBattleAction::CATAPULT:
  194. return 9; break;
  195. case PossiblePlayerBattleAction::HEAL:
  196. return 10; break;
  197. case PossiblePlayerBattleAction::CREATURE_INFO:
  198. return 11; break;
  199. case PossiblePlayerBattleAction::HERO_INFO:
  200. return 12; break;
  201. case PossiblePlayerBattleAction::TELEPORT:
  202. return 13; break;
  203. default:
  204. assert(0);
  205. return 200; break;
  206. }
  207. };
  208. auto comparer = [&](PossiblePlayerBattleAction const & lhs, PossiblePlayerBattleAction const & rhs)
  209. {
  210. return assignPriority(lhs) < assignPriority(rhs);
  211. };
  212. std::sort(possibleActions.begin(), possibleActions.end(), comparer);
  213. }
  214. void BattleActionsController::castThisSpell(SpellID spellID)
  215. {
  216. heroSpellToCast = std::make_shared<BattleAction>();
  217. heroSpellToCast->actionType = EActionType::HERO_SPELL;
  218. heroSpellToCast->actionSubtype = spellID; //spell number
  219. heroSpellToCast->stackNumber = (owner.attackingHeroInstance->tempOwner == owner.curInt->playerID) ? -1 : -2;
  220. heroSpellToCast->side = owner.defendingHeroInstance ? (owner.curInt->playerID == owner.defendingHeroInstance->tempOwner) : false;
  221. //choosing possible targets
  222. const CGHeroInstance *castingHero = (owner.attackingHeroInstance->tempOwner == owner.curInt->playerID) ? owner.attackingHeroInstance : owner.defendingHeroInstance;
  223. assert(castingHero); // code below assumes non-null hero
  224. PossiblePlayerBattleAction spellSelMode = owner.curInt->cb->getCasterAction(spellID.toSpell(), castingHero, spells::Mode::HERO);
  225. if (spellSelMode.get() == PossiblePlayerBattleAction::NO_LOCATION) //user does not have to select location
  226. {
  227. heroSpellToCast->aimToHex(BattleHex::INVALID);
  228. owner.curInt->cb->battleMakeAction(heroSpellToCast.get());
  229. endCastingSpell();
  230. }
  231. else
  232. {
  233. possibleActions.clear();
  234. possibleActions.push_back (spellSelMode); //only this one action can be performed at the moment
  235. GH.fakeMouseMove();//update cursor
  236. }
  237. }
  238. const CSpell * BattleActionsController::getHeroSpellToCast( ) const
  239. {
  240. if (heroSpellToCast)
  241. return SpellID(heroSpellToCast->actionSubtype).toSpell();
  242. return nullptr;
  243. }
  244. const CSpell * BattleActionsController::getStackSpellToCast(BattleHex hoveredHex)
  245. {
  246. if (heroSpellToCast)
  247. return nullptr;
  248. if (!owner.stacksController->getActiveStack())
  249. return nullptr;
  250. if (!hoveredHex.isValid())
  251. return nullptr;
  252. auto action = selectAction(hoveredHex);
  253. if (action.spell() == SpellID::NONE)
  254. return nullptr;
  255. return action.spell().toSpell();
  256. }
  257. const CSpell * BattleActionsController::getCurrentSpell(BattleHex hoveredHex)
  258. {
  259. if (getHeroSpellToCast())
  260. return getHeroSpellToCast();
  261. return getStackSpellToCast(hoveredHex);
  262. }
  263. const CStack * BattleActionsController::getStackForHex(BattleHex hoveredHex)
  264. {
  265. const CStack * shere = owner.curInt->cb->battleGetStackByPos(hoveredHex, true);
  266. if(shere)
  267. return shere;
  268. return owner.curInt->cb->battleGetStackByPos(hoveredHex, false);
  269. }
  270. void BattleActionsController::actionSetCursor(PossiblePlayerBattleAction action, BattleHex targetHex)
  271. {
  272. switch (action.get())
  273. {
  274. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  275. CCS->curh->set(Cursor::Combat::POINTER);
  276. return;
  277. case PossiblePlayerBattleAction::MOVE_TACTICS:
  278. case PossiblePlayerBattleAction::MOVE_STACK:
  279. if (owner.stacksController->getActiveStack()->hasBonusOfType(Bonus::FLYING))
  280. CCS->curh->set(Cursor::Combat::FLY);
  281. else
  282. CCS->curh->set(Cursor::Combat::MOVE);
  283. return;
  284. case PossiblePlayerBattleAction::ATTACK:
  285. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  286. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  287. owner.fieldController->setBattleCursor(targetHex);
  288. return;
  289. case PossiblePlayerBattleAction::SHOOT:
  290. if (owner.curInt->cb->battleHasShootingPenalty(owner.stacksController->getActiveStack(), targetHex))
  291. CCS->curh->set(Cursor::Combat::SHOOT_PENALTY);
  292. else
  293. CCS->curh->set(Cursor::Combat::SHOOT);
  294. return;
  295. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  296. case PossiblePlayerBattleAction::ANY_LOCATION:
  297. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  298. case PossiblePlayerBattleAction::FREE_LOCATION:
  299. case PossiblePlayerBattleAction::OBSTACLE:
  300. CCS->curh->set(Cursor::Spellcast::SPELL);
  301. return;
  302. case PossiblePlayerBattleAction::TELEPORT:
  303. CCS->curh->set(Cursor::Combat::TELEPORT);
  304. return;
  305. case PossiblePlayerBattleAction::SACRIFICE:
  306. CCS->curh->set(Cursor::Combat::SACRIFICE);
  307. return;
  308. case PossiblePlayerBattleAction::HEAL:
  309. CCS->curh->set(Cursor::Combat::HEAL);
  310. return;
  311. case PossiblePlayerBattleAction::CATAPULT:
  312. CCS->curh->set(Cursor::Combat::SHOOT_CATAPULT);
  313. return;
  314. case PossiblePlayerBattleAction::CREATURE_INFO:
  315. CCS->curh->set(Cursor::Combat::QUERY);
  316. return;
  317. case PossiblePlayerBattleAction::HERO_INFO:
  318. CCS->curh->set(Cursor::Combat::HERO);
  319. return;
  320. }
  321. assert(0);
  322. }
  323. void BattleActionsController::actionSetCursorBlocked(PossiblePlayerBattleAction action, BattleHex targetHex)
  324. {
  325. switch (action.get())
  326. {
  327. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  328. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  329. case PossiblePlayerBattleAction::TELEPORT:
  330. case PossiblePlayerBattleAction::SACRIFICE:
  331. case PossiblePlayerBattleAction::FREE_LOCATION:
  332. CCS->curh->set(Cursor::Combat::BLOCKED);
  333. return;
  334. default:
  335. if (targetHex == -1)
  336. CCS->curh->set(Cursor::Combat::POINTER);
  337. else
  338. CCS->curh->set(Cursor::Combat::BLOCKED);
  339. return;
  340. }
  341. assert(0);
  342. }
  343. std::string BattleActionsController::actionGetStatusMessage(PossiblePlayerBattleAction action, BattleHex targetHex)
  344. {
  345. const CStack * targetStack = getStackForHex(targetHex);
  346. switch (action.get()) //display console message, realize selected action
  347. {
  348. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  349. return (boost::format(CGI->generaltexth->allTexts[481]) % targetStack->getName()).str(); //Select %s
  350. case PossiblePlayerBattleAction::MOVE_TACTICS:
  351. case PossiblePlayerBattleAction::MOVE_STACK:
  352. if (owner.stacksController->getActiveStack()->hasBonusOfType(Bonus::FLYING))
  353. return (boost::format(CGI->generaltexth->allTexts[295]) % owner.stacksController->getActiveStack()->getName()).str(); //Fly %s here
  354. else
  355. return (boost::format(CGI->generaltexth->allTexts[294]) % owner.stacksController->getActiveStack()->getName()).str(); //Move %s here
  356. case PossiblePlayerBattleAction::ATTACK:
  357. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  358. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  359. {
  360. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(targetHex);
  361. DamageEstimation estimation = owner.curInt->cb->battleEstimateDamage(owner.stacksController->getActiveStack(), targetStack, attackFromHex);
  362. return formatMeleeAttack(estimation, targetStack->getName());
  363. }
  364. case PossiblePlayerBattleAction::SHOOT:
  365. {
  366. const auto * shooter = owner.stacksController->getActiveStack();
  367. DamageEstimation estimation = owner.curInt->cb->battleEstimateDamage(shooter, targetStack, shooter->getPosition());
  368. return formatRangedAttack(estimation, targetStack->getName(), shooter->shots.available());
  369. }
  370. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  371. return boost::str(boost::format(CGI->generaltexth->allTexts[27]) % action.spell().toSpell()->getNameTranslated() % targetStack->getName()); //Cast %s on %s
  372. case PossiblePlayerBattleAction::ANY_LOCATION:
  373. return boost::str(boost::format(CGI->generaltexth->allTexts[26]) % action.spell().toSpell()->getNameTranslated()); //Cast %s
  374. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  375. return boost::str(boost::format(CGI->generaltexth->allTexts[301]) % targetStack->getName()); //Cast a spell on %
  376. case PossiblePlayerBattleAction::TELEPORT:
  377. return CGI->generaltexth->allTexts[25]; //Teleport Here
  378. case PossiblePlayerBattleAction::OBSTACLE:
  379. return CGI->generaltexth->allTexts[550];
  380. case PossiblePlayerBattleAction::SACRIFICE:
  381. return (boost::format(CGI->generaltexth->allTexts[549]) % targetStack->getName()).str(); //sacrifice the %s
  382. case PossiblePlayerBattleAction::FREE_LOCATION:
  383. return boost::str(boost::format(CGI->generaltexth->allTexts[26]) % action.spell().toSpell()->getNameTranslated()); //Cast %s
  384. case PossiblePlayerBattleAction::HEAL:
  385. return (boost::format(CGI->generaltexth->allTexts[419]) % targetStack->getName()).str(); //Apply first aid to the %s
  386. case PossiblePlayerBattleAction::CATAPULT:
  387. return ""; // TODO
  388. case PossiblePlayerBattleAction::CREATURE_INFO:
  389. return (boost::format(CGI->generaltexth->allTexts[297]) % targetStack->getName()).str();
  390. case PossiblePlayerBattleAction::HERO_INFO:
  391. return CGI->generaltexth->translate("core.genrltxt.417"); // "View Hero Stats"
  392. }
  393. assert(0);
  394. return "";
  395. }
  396. std::string BattleActionsController::actionGetStatusMessageBlocked(PossiblePlayerBattleAction action, BattleHex targetHex)
  397. {
  398. switch (action.get())
  399. {
  400. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  401. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  402. return CGI->generaltexth->allTexts[23];
  403. break;
  404. case PossiblePlayerBattleAction::TELEPORT:
  405. return CGI->generaltexth->allTexts[24]; //Invalid Teleport Destination
  406. break;
  407. case PossiblePlayerBattleAction::SACRIFICE:
  408. return CGI->generaltexth->allTexts[543]; //choose army to sacrifice
  409. break;
  410. case PossiblePlayerBattleAction::FREE_LOCATION:
  411. return boost::str(boost::format(CGI->generaltexth->allTexts[181]) % action.spell().toSpell()->getNameTranslated()); //No room to place %s here
  412. break;
  413. default:
  414. return "";
  415. }
  416. }
  417. bool BattleActionsController::actionIsLegal(PossiblePlayerBattleAction action, BattleHex targetHex)
  418. {
  419. const CStack * targetStack = getStackForHex(targetHex);
  420. bool targetStackOwned = targetStack && targetStack->owner == owner.curInt->playerID;
  421. switch (action.get())
  422. {
  423. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  424. case PossiblePlayerBattleAction::CREATURE_INFO:
  425. return (targetStack && targetStackOwned);
  426. case PossiblePlayerBattleAction::HERO_INFO:
  427. if (targetHex == BattleHex::HERO_ATTACKER)
  428. return owner.attackingHero != nullptr;
  429. if (targetHex == BattleHex::HERO_DEFENDER)
  430. return owner.defendingHero != nullptr;
  431. return false;
  432. case PossiblePlayerBattleAction::MOVE_TACTICS:
  433. case PossiblePlayerBattleAction::MOVE_STACK:
  434. if (!(targetStack && targetStack->alive())) //we can walk on dead stacks
  435. {
  436. if(canStackMoveHere(owner.stacksController->getActiveStack(), targetHex))
  437. return true;
  438. }
  439. return false;
  440. case PossiblePlayerBattleAction::ATTACK:
  441. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  442. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  443. if(owner.curInt->cb->battleCanAttack(owner.stacksController->getActiveStack(), targetStack, targetHex))
  444. {
  445. if (owner.fieldController->isTileAttackable(targetHex)) // move isTileAttackable to be part of battleCanAttack?
  446. return true;
  447. }
  448. return false;
  449. case PossiblePlayerBattleAction::SHOOT:
  450. return owner.curInt->cb->battleCanShoot(owner.stacksController->getActiveStack(), targetHex);
  451. case PossiblePlayerBattleAction::NO_LOCATION:
  452. return false;
  453. case PossiblePlayerBattleAction::ANY_LOCATION:
  454. return isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  455. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  456. return targetStack && isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  457. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  458. if(targetStack && targetStackOwned && targetStack != owner.stacksController->getActiveStack() && targetStack->alive()) //only positive spells for other allied creatures
  459. {
  460. int spellID = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), targetStack, CBattleInfoCallback::RANDOM_GENIE);
  461. return spellID > -1;
  462. }
  463. return false;
  464. case PossiblePlayerBattleAction::TELEPORT:
  465. {
  466. ui8 skill = getCurrentSpellcaster()->getEffectLevel(SpellID(SpellID::TELEPORT).toSpell());
  467. return owner.curInt->cb->battleCanTeleportTo(owner.stacksController->getSelectedStack(), targetHex, skill);
  468. }
  469. case PossiblePlayerBattleAction::SACRIFICE: //choose our living stack to sacrifice
  470. return targetStack && targetStack != owner.stacksController->getSelectedStack() && targetStackOwned && targetStack->alive();
  471. case PossiblePlayerBattleAction::OBSTACLE:
  472. case PossiblePlayerBattleAction::FREE_LOCATION:
  473. return isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  474. return isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  475. case PossiblePlayerBattleAction::CATAPULT:
  476. return owner.siegeController && owner.siegeController->isAttackableByCatapult(targetHex);
  477. case PossiblePlayerBattleAction::HEAL:
  478. return targetStack && targetStackOwned && targetStack->canBeHealed();
  479. }
  480. assert(0);
  481. return false;
  482. }
  483. void BattleActionsController::actionRealize(PossiblePlayerBattleAction action, BattleHex targetHex)
  484. {
  485. const CStack * targetStack = getStackForHex(targetHex);
  486. switch (action.get()) //display console message, realize selected action
  487. {
  488. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  489. {
  490. owner.stackActivated(targetStack);
  491. return;
  492. }
  493. case PossiblePlayerBattleAction::MOVE_TACTICS:
  494. case PossiblePlayerBattleAction::MOVE_STACK:
  495. {
  496. if(owner.stacksController->getActiveStack()->doubleWide())
  497. {
  498. std::vector<BattleHex> acc = owner.curInt->cb->battleGetAvailableHexes(owner.stacksController->getActiveStack());
  499. BattleHex shiftedDest = targetHex.cloneInDirection(owner.stacksController->getActiveStack()->destShiftDir(), false);
  500. if(vstd::contains(acc, targetHex))
  501. owner.giveCommand(EActionType::WALK, targetHex);
  502. else if(vstd::contains(acc, shiftedDest))
  503. owner.giveCommand(EActionType::WALK, shiftedDest);
  504. }
  505. else
  506. {
  507. owner.giveCommand(EActionType::WALK, targetHex);
  508. }
  509. return;
  510. }
  511. case PossiblePlayerBattleAction::ATTACK:
  512. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  513. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  514. {
  515. bool returnAfterAttack = action.get() == PossiblePlayerBattleAction::ATTACK_AND_RETURN;
  516. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(targetHex);
  517. if(attackFromHex.isValid()) //we can be in this line when unreachable creature is L - clicked (as of revision 1308)
  518. {
  519. auto command = new BattleAction(BattleAction::makeMeleeAttack(owner.stacksController->getActiveStack(), targetHex, attackFromHex, returnAfterAttack));
  520. owner.sendCommand(command, owner.stacksController->getActiveStack());
  521. }
  522. return;
  523. }
  524. case PossiblePlayerBattleAction::SHOOT:
  525. {
  526. owner.giveCommand(EActionType::SHOOT, targetHex);
  527. return;
  528. }
  529. case PossiblePlayerBattleAction::HEAL:
  530. {
  531. owner.giveCommand(EActionType::STACK_HEAL, targetHex);
  532. return;
  533. };
  534. case PossiblePlayerBattleAction::CATAPULT:
  535. {
  536. owner.giveCommand(EActionType::CATAPULT, targetHex);
  537. return;
  538. }
  539. case PossiblePlayerBattleAction::CREATURE_INFO:
  540. {
  541. GH.pushIntT<CStackWindow>(targetStack, false);
  542. return;
  543. }
  544. case PossiblePlayerBattleAction::HERO_INFO:
  545. {
  546. if (targetHex == BattleHex::HERO_ATTACKER)
  547. owner.attackingHero->heroLeftClicked();
  548. if (targetHex == BattleHex::HERO_DEFENDER)
  549. owner.defendingHero->heroLeftClicked();
  550. return;
  551. }
  552. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  553. case PossiblePlayerBattleAction::ANY_LOCATION:
  554. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  555. case PossiblePlayerBattleAction::TELEPORT:
  556. case PossiblePlayerBattleAction::OBSTACLE:
  557. case PossiblePlayerBattleAction::SACRIFICE:
  558. case PossiblePlayerBattleAction::FREE_LOCATION:
  559. {
  560. if (action.get() == PossiblePlayerBattleAction::AIMED_SPELL_CREATURE )
  561. {
  562. if (action.spell() == SpellID::SACRIFICE)
  563. {
  564. heroSpellToCast->aimToHex(targetHex);
  565. possibleActions.push_back(PossiblePlayerBattleAction::SACRIFICE);
  566. owner.stacksController->setSelectedStack(targetStack);
  567. return;
  568. }
  569. if (action.spell() == SpellID::TELEPORT)
  570. {
  571. heroSpellToCast->aimToUnit(targetStack);
  572. possibleActions.push_back(PossiblePlayerBattleAction::TELEPORT);
  573. owner.stacksController->setSelectedStack(targetStack);
  574. return;
  575. }
  576. }
  577. if (!spellcastingModeActive())
  578. {
  579. if (action.spell().toSpell())
  580. {
  581. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex, action.spell());
  582. }
  583. else //unknown random spell
  584. {
  585. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex);
  586. }
  587. }
  588. else
  589. {
  590. assert(getHeroSpellToCast());
  591. switch (getHeroSpellToCast()->id.toEnum())
  592. {
  593. case SpellID::SACRIFICE:
  594. heroSpellToCast->aimToUnit(targetStack);//victim
  595. break;
  596. default:
  597. heroSpellToCast->aimToHex(targetHex);
  598. break;
  599. }
  600. owner.curInt->cb->battleMakeAction(heroSpellToCast.get());
  601. endCastingSpell();
  602. }
  603. owner.stacksController->setSelectedStack(nullptr);
  604. return;
  605. }
  606. }
  607. assert(0);
  608. return;
  609. }
  610. PossiblePlayerBattleAction BattleActionsController::selectAction(BattleHex targetHex)
  611. {
  612. assert(owner.stacksController->getActiveStack() != nullptr);
  613. assert(!possibleActions.empty());
  614. assert(targetHex.isValid());
  615. if (owner.stacksController->getActiveStack() == nullptr)
  616. return PossiblePlayerBattleAction::INVALID;
  617. if (possibleActions.empty())
  618. return PossiblePlayerBattleAction::INVALID;
  619. const CStack * targetStack = getStackForHex(targetHex);
  620. reorderPossibleActionsPriority(owner.stacksController->getActiveStack(), targetStack ? MouseHoveredHexContext::OCCUPIED_HEX : MouseHoveredHexContext::UNOCCUPIED_HEX);
  621. for (PossiblePlayerBattleAction action : possibleActions)
  622. {
  623. if (actionIsLegal(action, targetHex))
  624. return action;
  625. }
  626. return possibleActions.front();
  627. }
  628. void BattleActionsController::onHexHovered(BattleHex hoveredHex)
  629. {
  630. if (owner.openingPlaying())
  631. {
  632. currentConsoleMsg = VLC->generaltexth->translate("vcmi.battleWindow.pressKeyToSkipIntro");
  633. GH.statusbar->write(currentConsoleMsg);
  634. return;
  635. }
  636. if (owner.stacksController->getActiveStack() == nullptr)
  637. return;
  638. if (hoveredHex == BattleHex::INVALID)
  639. {
  640. if (!currentConsoleMsg.empty())
  641. GH.statusbar->clearIfMatching(currentConsoleMsg);
  642. currentConsoleMsg.clear();
  643. CCS->curh->set(Cursor::Combat::BLOCKED);
  644. return;
  645. }
  646. auto action = selectAction(hoveredHex);
  647. std::string newConsoleMsg;
  648. if (actionIsLegal(action, hoveredHex))
  649. {
  650. actionSetCursor(action, hoveredHex);
  651. newConsoleMsg = actionGetStatusMessage(action, hoveredHex);
  652. }
  653. else
  654. {
  655. actionSetCursorBlocked(action, hoveredHex);
  656. newConsoleMsg = actionGetStatusMessageBlocked(action, hoveredHex);
  657. }
  658. if (!currentConsoleMsg.empty())
  659. GH.statusbar->clearIfMatching(currentConsoleMsg);
  660. if (!newConsoleMsg.empty())
  661. GH.statusbar->write(newConsoleMsg);
  662. currentConsoleMsg = newConsoleMsg;
  663. }
  664. void BattleActionsController::onHoverEnded()
  665. {
  666. CCS->curh->set(Cursor::Combat::POINTER);
  667. if (!currentConsoleMsg.empty())
  668. GH.statusbar->clearIfMatching(currentConsoleMsg);
  669. currentConsoleMsg.clear();
  670. }
  671. void BattleActionsController::onHexLeftClicked(BattleHex clickedHex)
  672. {
  673. if (owner.stacksController->getActiveStack() == nullptr)
  674. return;
  675. auto action = selectAction(clickedHex);
  676. std::string newConsoleMsg;
  677. if (!actionIsLegal(action, clickedHex))
  678. return;
  679. actionRealize(action, clickedHex);
  680. GH.statusbar->clear();
  681. }
  682. void BattleActionsController::tryActivateStackSpellcasting(const CStack *casterStack)
  683. {
  684. creatureSpells.clear();
  685. bool spellcaster = casterStack->hasBonusOfType(Bonus::SPELLCASTER);
  686. if(casterStack->canCast() && spellcaster)
  687. {
  688. // faerie dragon can cast only one, randomly selected spell until their next move
  689. //TODO: faerie dragon type spell should be selected by server
  690. const auto * spellToCast = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), casterStack, CBattleInfoCallback::RANDOM_AIMED).toSpell();
  691. if (spellToCast)
  692. creatureSpells.push_back(spellToCast);
  693. }
  694. TConstBonusListPtr bl = casterStack->getBonuses(Selector::type()(Bonus::SPELLCASTER));
  695. for (auto const & bonus : *bl)
  696. {
  697. if (bonus->additionalInfo[0] <= 0)
  698. creatureSpells.push_back(SpellID(bonus->subtype).toSpell());
  699. }
  700. }
  701. const spells::Caster * BattleActionsController::getCurrentSpellcaster() const
  702. {
  703. if (heroSpellToCast)
  704. return owner.getActiveHero();
  705. else
  706. return owner.stacksController->getActiveStack();
  707. }
  708. spells::Mode BattleActionsController::getCurrentCastMode() const
  709. {
  710. if (heroSpellToCast)
  711. return spells::Mode::HERO;
  712. else
  713. return spells::Mode::CREATURE_ACTIVE;
  714. }
  715. bool BattleActionsController::isCastingPossibleHere(const CSpell * currentSpell, const CStack *casterStack, const CStack *targetStack, BattleHex targetHex)
  716. {
  717. assert(currentSpell);
  718. if (!currentSpell)
  719. return false;
  720. auto caster = getCurrentSpellcaster();
  721. const spells::Mode mode = heroSpellToCast ? spells::Mode::HERO : spells::Mode::CREATURE_ACTIVE;
  722. spells::Target target;
  723. target.emplace_back(targetHex);
  724. spells::BattleCast cast(owner.curInt->cb.get(), caster, mode, currentSpell);
  725. auto m = currentSpell->battleMechanics(&cast);
  726. spells::detail::ProblemImpl problem; //todo: display problem in status bar
  727. return m->canBeCastAt(target, problem);
  728. }
  729. bool BattleActionsController::canStackMoveHere(const CStack * stackToMove, BattleHex myNumber) const
  730. {
  731. std::vector<BattleHex> acc = owner.curInt->cb->battleGetAvailableHexes(stackToMove);
  732. BattleHex shiftedDest = myNumber.cloneInDirection(stackToMove->destShiftDir(), false);
  733. if (vstd::contains(acc, myNumber))
  734. return true;
  735. else if (stackToMove->doubleWide() && vstd::contains(acc, shiftedDest))
  736. return true;
  737. else
  738. return false;
  739. }
  740. void BattleActionsController::activateStack()
  741. {
  742. const CStack * s = owner.stacksController->getActiveStack();
  743. if(s)
  744. {
  745. tryActivateStackSpellcasting(s);
  746. possibleActions = getPossibleActionsForStack(s);
  747. std::list<PossiblePlayerBattleAction> actionsToSelect;
  748. if(!possibleActions.empty())
  749. {
  750. switch(possibleActions.front().get())
  751. {
  752. case PossiblePlayerBattleAction::SHOOT:
  753. actionsToSelect.push_back(possibleActions.front());
  754. actionsToSelect.push_back(PossiblePlayerBattleAction::ATTACK);
  755. break;
  756. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  757. actionsToSelect.push_back(possibleActions.front());
  758. actionsToSelect.push_back(PossiblePlayerBattleAction::WALK_AND_ATTACK);
  759. break;
  760. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  761. actionsToSelect.push_back(possibleActions.front());
  762. break;
  763. }
  764. }
  765. owner.windowObject->setAlternativeActions(actionsToSelect);
  766. }
  767. }
  768. void BattleActionsController::onHexRightClicked(BattleHex clickedHex)
  769. {
  770. auto selectedStack = owner.curInt->cb->battleGetStackByPos(clickedHex, true);
  771. if (selectedStack != nullptr)
  772. GH.pushIntT<CStackWindow>(selectedStack, true);
  773. if (clickedHex == BattleHex::HERO_ATTACKER && owner.attackingHero)
  774. owner.attackingHero->heroRightClicked();
  775. if (clickedHex == BattleHex::HERO_DEFENDER && owner.defendingHero)
  776. owner.defendingHero->heroRightClicked();
  777. }
  778. bool BattleActionsController::spellcastingModeActive() const
  779. {
  780. return heroSpellToCast != nullptr;;
  781. }
  782. bool BattleActionsController::currentActionSpellcasting(BattleHex hoveredHex)
  783. {
  784. if (heroSpellToCast)
  785. return true;
  786. if (!owner.stacksController->getActiveStack())
  787. return false;
  788. auto action = selectAction(hoveredHex);
  789. return action.spellcast();
  790. }
  791. const std::vector<PossiblePlayerBattleAction> & BattleActionsController::getPossibleActions() const
  792. {
  793. return possibleActions;
  794. }
  795. void BattleActionsController::removePossibleAction(PossiblePlayerBattleAction action)
  796. {
  797. vstd::erase(possibleActions, action);
  798. }
  799. void BattleActionsController::pushFrontPossibleAction(PossiblePlayerBattleAction action)
  800. {
  801. possibleActions.insert(possibleActions.begin(), action);
  802. }