BattleActionsController.cpp 33 KB

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