BattleActionsController.cpp 33 KB

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