BattleActionsController.cpp 33 KB

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