BattleActionsController.cpp 33 KB

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