BattleActionsController.cpp 33 KB

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