BattleActionsController.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. estimation.kills.max = std::min<int64_t>(estimation.kills.max, targetStack->getCount());
  363. estimation.kills.min = std::min<int64_t>(estimation.kills.min, targetStack->getCount());
  364. return formatMeleeAttack(estimation, targetStack->getName());
  365. }
  366. case PossiblePlayerBattleAction::SHOOT:
  367. {
  368. const auto * shooter = owner.stacksController->getActiveStack();
  369. DamageEstimation estimation = owner.curInt->cb->battleEstimateDamage(shooter, targetStack, shooter->getPosition());
  370. estimation.kills.max = std::min<int64_t>(estimation.kills.max, targetStack->getCount());
  371. estimation.kills.min = std::min<int64_t>(estimation.kills.min, targetStack->getCount());
  372. return formatRangedAttack(estimation, targetStack->getName(), shooter->shots.available());
  373. }
  374. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  375. return boost::str(boost::format(CGI->generaltexth->allTexts[27]) % action.spell().toSpell()->getNameTranslated() % targetStack->getName()); //Cast %s on %s
  376. case PossiblePlayerBattleAction::ANY_LOCATION:
  377. return boost::str(boost::format(CGI->generaltexth->allTexts[26]) % action.spell().toSpell()->getNameTranslated()); //Cast %s
  378. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  379. return boost::str(boost::format(CGI->generaltexth->allTexts[301]) % targetStack->getName()); //Cast a spell on %
  380. case PossiblePlayerBattleAction::TELEPORT:
  381. return CGI->generaltexth->allTexts[25]; //Teleport Here
  382. case PossiblePlayerBattleAction::OBSTACLE:
  383. return CGI->generaltexth->allTexts[550];
  384. case PossiblePlayerBattleAction::SACRIFICE:
  385. return (boost::format(CGI->generaltexth->allTexts[549]) % targetStack->getName()).str(); //sacrifice the %s
  386. case PossiblePlayerBattleAction::FREE_LOCATION:
  387. return boost::str(boost::format(CGI->generaltexth->allTexts[26]) % action.spell().toSpell()->getNameTranslated()); //Cast %s
  388. case PossiblePlayerBattleAction::HEAL:
  389. return (boost::format(CGI->generaltexth->allTexts[419]) % targetStack->getName()).str(); //Apply first aid to the %s
  390. case PossiblePlayerBattleAction::CATAPULT:
  391. return ""; // TODO
  392. case PossiblePlayerBattleAction::CREATURE_INFO:
  393. return (boost::format(CGI->generaltexth->allTexts[297]) % targetStack->getName()).str();
  394. case PossiblePlayerBattleAction::HERO_INFO:
  395. return CGI->generaltexth->translate("core.genrltxt.417"); // "View Hero Stats"
  396. }
  397. assert(0);
  398. return "";
  399. }
  400. std::string BattleActionsController::actionGetStatusMessageBlocked(PossiblePlayerBattleAction action, BattleHex targetHex)
  401. {
  402. switch (action.get())
  403. {
  404. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  405. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  406. return CGI->generaltexth->allTexts[23];
  407. break;
  408. case PossiblePlayerBattleAction::TELEPORT:
  409. return CGI->generaltexth->allTexts[24]; //Invalid Teleport Destination
  410. break;
  411. case PossiblePlayerBattleAction::SACRIFICE:
  412. return CGI->generaltexth->allTexts[543]; //choose army to sacrifice
  413. break;
  414. case PossiblePlayerBattleAction::FREE_LOCATION:
  415. return boost::str(boost::format(CGI->generaltexth->allTexts[181]) % action.spell().toSpell()->getNameTranslated()); //No room to place %s here
  416. break;
  417. default:
  418. return "";
  419. }
  420. }
  421. bool BattleActionsController::actionIsLegal(PossiblePlayerBattleAction action, BattleHex targetHex)
  422. {
  423. const CStack * targetStack = getStackForHex(targetHex);
  424. bool targetStackOwned = targetStack && targetStack->owner == owner.curInt->playerID;
  425. switch (action.get())
  426. {
  427. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  428. case PossiblePlayerBattleAction::CREATURE_INFO:
  429. return (targetStack && targetStackOwned);
  430. case PossiblePlayerBattleAction::HERO_INFO:
  431. if (targetHex == BattleHex::HERO_ATTACKER)
  432. return owner.attackingHero != nullptr;
  433. if (targetHex == BattleHex::HERO_DEFENDER)
  434. return owner.defendingHero != nullptr;
  435. return false;
  436. case PossiblePlayerBattleAction::MOVE_TACTICS:
  437. case PossiblePlayerBattleAction::MOVE_STACK:
  438. if (!(targetStack && targetStack->alive())) //we can walk on dead stacks
  439. {
  440. if(canStackMoveHere(owner.stacksController->getActiveStack(), targetHex))
  441. return true;
  442. }
  443. return false;
  444. case PossiblePlayerBattleAction::ATTACK:
  445. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  446. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  447. if(owner.curInt->cb->battleCanAttack(owner.stacksController->getActiveStack(), targetStack, targetHex))
  448. {
  449. if (owner.fieldController->isTileAttackable(targetHex)) // move isTileAttackable to be part of battleCanAttack?
  450. return true;
  451. }
  452. return false;
  453. case PossiblePlayerBattleAction::SHOOT:
  454. return owner.curInt->cb->battleCanShoot(owner.stacksController->getActiveStack(), targetHex);
  455. case PossiblePlayerBattleAction::NO_LOCATION:
  456. return false;
  457. case PossiblePlayerBattleAction::ANY_LOCATION:
  458. return isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  459. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  460. return targetStack && isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  461. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  462. if(targetStack && targetStackOwned && targetStack != owner.stacksController->getActiveStack() && targetStack->alive()) //only positive spells for other allied creatures
  463. {
  464. int spellID = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), targetStack, CBattleInfoCallback::RANDOM_GENIE);
  465. return spellID > -1;
  466. }
  467. return false;
  468. case PossiblePlayerBattleAction::TELEPORT:
  469. {
  470. ui8 skill = getCurrentSpellcaster()->getEffectLevel(SpellID(SpellID::TELEPORT).toSpell());
  471. return owner.curInt->cb->battleCanTeleportTo(owner.stacksController->getSelectedStack(), targetHex, skill);
  472. }
  473. case PossiblePlayerBattleAction::SACRIFICE: //choose our living stack to sacrifice
  474. return targetStack && targetStack != owner.stacksController->getSelectedStack() && targetStackOwned && targetStack->alive();
  475. case PossiblePlayerBattleAction::OBSTACLE:
  476. case PossiblePlayerBattleAction::FREE_LOCATION:
  477. return isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  478. return isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  479. case PossiblePlayerBattleAction::CATAPULT:
  480. return owner.siegeController && owner.siegeController->isAttackableByCatapult(targetHex);
  481. case PossiblePlayerBattleAction::HEAL:
  482. return targetStack && targetStackOwned && targetStack->canBeHealed();
  483. }
  484. assert(0);
  485. return false;
  486. }
  487. void BattleActionsController::actionRealize(PossiblePlayerBattleAction action, BattleHex targetHex)
  488. {
  489. const CStack * targetStack = getStackForHex(targetHex);
  490. switch (action.get()) //display console message, realize selected action
  491. {
  492. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  493. {
  494. owner.stackActivated(targetStack);
  495. return;
  496. }
  497. case PossiblePlayerBattleAction::MOVE_TACTICS:
  498. case PossiblePlayerBattleAction::MOVE_STACK:
  499. {
  500. if(owner.stacksController->getActiveStack()->doubleWide())
  501. {
  502. std::vector<BattleHex> acc = owner.curInt->cb->battleGetAvailableHexes(owner.stacksController->getActiveStack());
  503. BattleHex shiftedDest = targetHex.cloneInDirection(owner.stacksController->getActiveStack()->destShiftDir(), false);
  504. if(vstd::contains(acc, targetHex))
  505. owner.giveCommand(EActionType::WALK, targetHex);
  506. else if(vstd::contains(acc, shiftedDest))
  507. owner.giveCommand(EActionType::WALK, shiftedDest);
  508. }
  509. else
  510. {
  511. owner.giveCommand(EActionType::WALK, targetHex);
  512. }
  513. return;
  514. }
  515. case PossiblePlayerBattleAction::ATTACK:
  516. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  517. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  518. {
  519. bool returnAfterAttack = action.get() == PossiblePlayerBattleAction::ATTACK_AND_RETURN;
  520. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(targetHex);
  521. if(attackFromHex.isValid()) //we can be in this line when unreachable creature is L - clicked (as of revision 1308)
  522. {
  523. auto command = new BattleAction(BattleAction::makeMeleeAttack(owner.stacksController->getActiveStack(), targetHex, attackFromHex, returnAfterAttack));
  524. owner.sendCommand(command, owner.stacksController->getActiveStack());
  525. }
  526. return;
  527. }
  528. case PossiblePlayerBattleAction::SHOOT:
  529. {
  530. owner.giveCommand(EActionType::SHOOT, targetHex);
  531. return;
  532. }
  533. case PossiblePlayerBattleAction::HEAL:
  534. {
  535. owner.giveCommand(EActionType::STACK_HEAL, targetHex);
  536. return;
  537. };
  538. case PossiblePlayerBattleAction::CATAPULT:
  539. {
  540. owner.giveCommand(EActionType::CATAPULT, targetHex);
  541. return;
  542. }
  543. case PossiblePlayerBattleAction::CREATURE_INFO:
  544. {
  545. GH.pushIntT<CStackWindow>(targetStack, false);
  546. return;
  547. }
  548. case PossiblePlayerBattleAction::HERO_INFO:
  549. {
  550. if (targetHex == BattleHex::HERO_ATTACKER)
  551. owner.attackingHero->heroLeftClicked();
  552. if (targetHex == BattleHex::HERO_DEFENDER)
  553. owner.defendingHero->heroLeftClicked();
  554. return;
  555. }
  556. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  557. case PossiblePlayerBattleAction::ANY_LOCATION:
  558. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  559. case PossiblePlayerBattleAction::TELEPORT:
  560. case PossiblePlayerBattleAction::OBSTACLE:
  561. case PossiblePlayerBattleAction::SACRIFICE:
  562. case PossiblePlayerBattleAction::FREE_LOCATION:
  563. {
  564. if (action.get() == PossiblePlayerBattleAction::AIMED_SPELL_CREATURE )
  565. {
  566. if (action.spell() == SpellID::SACRIFICE)
  567. {
  568. heroSpellToCast->aimToHex(targetHex);
  569. possibleActions.push_back({PossiblePlayerBattleAction::SACRIFICE, SpellID::SACRIFICE});
  570. owner.stacksController->setSelectedStack(targetStack);
  571. return;
  572. }
  573. if (action.spell() == SpellID::TELEPORT)
  574. {
  575. heroSpellToCast->aimToUnit(targetStack);
  576. possibleActions.push_back({PossiblePlayerBattleAction::TELEPORT, SpellID::TELEPORT});
  577. owner.stacksController->setSelectedStack(targetStack);
  578. return;
  579. }
  580. }
  581. if (!spellcastingModeActive())
  582. {
  583. if (action.spell().toSpell())
  584. {
  585. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex, action.spell());
  586. }
  587. else //unknown random spell
  588. {
  589. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex);
  590. }
  591. }
  592. else
  593. {
  594. assert(getHeroSpellToCast());
  595. switch (getHeroSpellToCast()->id.toEnum())
  596. {
  597. case SpellID::SACRIFICE:
  598. heroSpellToCast->aimToUnit(targetStack);//victim
  599. break;
  600. default:
  601. heroSpellToCast->aimToHex(targetHex);
  602. break;
  603. }
  604. owner.curInt->cb->battleMakeAction(heroSpellToCast.get());
  605. endCastingSpell();
  606. }
  607. owner.stacksController->setSelectedStack(nullptr);
  608. return;
  609. }
  610. }
  611. assert(0);
  612. return;
  613. }
  614. PossiblePlayerBattleAction BattleActionsController::selectAction(BattleHex targetHex)
  615. {
  616. assert(owner.stacksController->getActiveStack() != nullptr);
  617. assert(!possibleActions.empty());
  618. assert(targetHex.isValid());
  619. if (owner.stacksController->getActiveStack() == nullptr)
  620. return PossiblePlayerBattleAction::INVALID;
  621. if (possibleActions.empty())
  622. return PossiblePlayerBattleAction::INVALID;
  623. const CStack * targetStack = getStackForHex(targetHex);
  624. reorderPossibleActionsPriority(owner.stacksController->getActiveStack(), targetStack ? MouseHoveredHexContext::OCCUPIED_HEX : MouseHoveredHexContext::UNOCCUPIED_HEX);
  625. for (PossiblePlayerBattleAction action : possibleActions)
  626. {
  627. if (actionIsLegal(action, targetHex))
  628. return action;
  629. }
  630. return possibleActions.front();
  631. }
  632. void BattleActionsController::onHexHovered(BattleHex hoveredHex)
  633. {
  634. if (owner.openingPlaying())
  635. {
  636. currentConsoleMsg = VLC->generaltexth->translate("vcmi.battleWindow.pressKeyToSkipIntro");
  637. GH.statusbar->write(currentConsoleMsg);
  638. return;
  639. }
  640. if (owner.stacksController->getActiveStack() == nullptr)
  641. return;
  642. if (hoveredHex == BattleHex::INVALID)
  643. {
  644. if (!currentConsoleMsg.empty())
  645. GH.statusbar->clearIfMatching(currentConsoleMsg);
  646. currentConsoleMsg.clear();
  647. CCS->curh->set(Cursor::Combat::BLOCKED);
  648. return;
  649. }
  650. auto action = selectAction(hoveredHex);
  651. std::string newConsoleMsg;
  652. if (actionIsLegal(action, hoveredHex))
  653. {
  654. actionSetCursor(action, hoveredHex);
  655. newConsoleMsg = actionGetStatusMessage(action, hoveredHex);
  656. }
  657. else
  658. {
  659. actionSetCursorBlocked(action, hoveredHex);
  660. newConsoleMsg = actionGetStatusMessageBlocked(action, hoveredHex);
  661. }
  662. if (!currentConsoleMsg.empty())
  663. GH.statusbar->clearIfMatching(currentConsoleMsg);
  664. if (!newConsoleMsg.empty())
  665. GH.statusbar->write(newConsoleMsg);
  666. currentConsoleMsg = newConsoleMsg;
  667. }
  668. void BattleActionsController::onHoverEnded()
  669. {
  670. CCS->curh->set(Cursor::Combat::POINTER);
  671. if (!currentConsoleMsg.empty())
  672. GH.statusbar->clearIfMatching(currentConsoleMsg);
  673. currentConsoleMsg.clear();
  674. }
  675. void BattleActionsController::onHexLeftClicked(BattleHex clickedHex)
  676. {
  677. if (owner.stacksController->getActiveStack() == nullptr)
  678. return;
  679. auto action = selectAction(clickedHex);
  680. std::string newConsoleMsg;
  681. if (!actionIsLegal(action, clickedHex))
  682. return;
  683. actionRealize(action, clickedHex);
  684. GH.statusbar->clear();
  685. }
  686. void BattleActionsController::tryActivateStackSpellcasting(const CStack *casterStack)
  687. {
  688. creatureSpells.clear();
  689. bool spellcaster = casterStack->hasBonusOfType(Bonus::SPELLCASTER);
  690. if(casterStack->canCast() && spellcaster)
  691. {
  692. // faerie dragon can cast only one, randomly selected spell until their next move
  693. //TODO: faerie dragon type spell should be selected by server
  694. const auto * spellToCast = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), casterStack, CBattleInfoCallback::RANDOM_AIMED).toSpell();
  695. if (spellToCast)
  696. creatureSpells.push_back(spellToCast);
  697. }
  698. TConstBonusListPtr bl = casterStack->getBonuses(Selector::type()(Bonus::SPELLCASTER));
  699. for (auto const & bonus : *bl)
  700. {
  701. if (bonus->additionalInfo[0] <= 0)
  702. creatureSpells.push_back(SpellID(bonus->subtype).toSpell());
  703. }
  704. }
  705. const spells::Caster * BattleActionsController::getCurrentSpellcaster() const
  706. {
  707. if (heroSpellToCast)
  708. return owner.getActiveHero();
  709. else
  710. return owner.stacksController->getActiveStack();
  711. }
  712. spells::Mode BattleActionsController::getCurrentCastMode() const
  713. {
  714. if (heroSpellToCast)
  715. return spells::Mode::HERO;
  716. else
  717. return spells::Mode::CREATURE_ACTIVE;
  718. }
  719. bool BattleActionsController::isCastingPossibleHere(const CSpell * currentSpell, const CStack *casterStack, const CStack *targetStack, BattleHex targetHex)
  720. {
  721. assert(currentSpell);
  722. if (!currentSpell)
  723. return false;
  724. auto caster = getCurrentSpellcaster();
  725. const spells::Mode mode = heroSpellToCast ? spells::Mode::HERO : spells::Mode::CREATURE_ACTIVE;
  726. spells::Target target;
  727. target.emplace_back(targetHex);
  728. spells::BattleCast cast(owner.curInt->cb.get(), caster, mode, currentSpell);
  729. auto m = currentSpell->battleMechanics(&cast);
  730. spells::detail::ProblemImpl problem; //todo: display problem in status bar
  731. return m->canBeCastAt(target, problem);
  732. }
  733. bool BattleActionsController::canStackMoveHere(const CStack * stackToMove, BattleHex myNumber) const
  734. {
  735. std::vector<BattleHex> acc = owner.curInt->cb->battleGetAvailableHexes(stackToMove);
  736. BattleHex shiftedDest = myNumber.cloneInDirection(stackToMove->destShiftDir(), false);
  737. if (vstd::contains(acc, myNumber))
  738. return true;
  739. else if (stackToMove->doubleWide() && vstd::contains(acc, shiftedDest))
  740. return true;
  741. else
  742. return false;
  743. }
  744. void BattleActionsController::activateStack()
  745. {
  746. const CStack * s = owner.stacksController->getActiveStack();
  747. if(s)
  748. {
  749. tryActivateStackSpellcasting(s);
  750. possibleActions = getPossibleActionsForStack(s);
  751. std::list<PossiblePlayerBattleAction> actionsToSelect;
  752. if(!possibleActions.empty())
  753. {
  754. switch(possibleActions.front().get())
  755. {
  756. case PossiblePlayerBattleAction::SHOOT:
  757. actionsToSelect.push_back(possibleActions.front());
  758. actionsToSelect.push_back(PossiblePlayerBattleAction::ATTACK);
  759. break;
  760. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  761. actionsToSelect.push_back(possibleActions.front());
  762. actionsToSelect.push_back(PossiblePlayerBattleAction::WALK_AND_ATTACK);
  763. break;
  764. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  765. actionsToSelect.push_back(possibleActions.front());
  766. break;
  767. }
  768. }
  769. owner.windowObject->setAlternativeActions(actionsToSelect);
  770. }
  771. }
  772. void BattleActionsController::onHexRightClicked(BattleHex clickedHex)
  773. {
  774. auto selectedStack = owner.curInt->cb->battleGetStackByPos(clickedHex, true);
  775. if (selectedStack != nullptr)
  776. GH.pushIntT<CStackWindow>(selectedStack, true);
  777. if (clickedHex == BattleHex::HERO_ATTACKER && owner.attackingHero)
  778. owner.attackingHero->heroRightClicked();
  779. if (clickedHex == BattleHex::HERO_DEFENDER && owner.defendingHero)
  780. owner.defendingHero->heroRightClicked();
  781. }
  782. bool BattleActionsController::spellcastingModeActive() const
  783. {
  784. return heroSpellToCast != nullptr;;
  785. }
  786. bool BattleActionsController::currentActionSpellcasting(BattleHex hoveredHex)
  787. {
  788. if (heroSpellToCast)
  789. return true;
  790. if (!owner.stacksController->getActiveStack())
  791. return false;
  792. auto action = selectAction(hoveredHex);
  793. return action.spellcast();
  794. }
  795. const std::vector<PossiblePlayerBattleAction> & BattleActionsController::getPossibleActions() const
  796. {
  797. return possibleActions;
  798. }
  799. void BattleActionsController::removePossibleAction(PossiblePlayerBattleAction action)
  800. {
  801. vstd::erase(possibleActions, action);
  802. }
  803. void BattleActionsController::pushFrontPossibleAction(PossiblePlayerBattleAction action)
  804. {
  805. possibleActions.insert(possibleActions.begin(), action);
  806. }