BattleActionsController.cpp 33 KB

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