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 "../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->owner == 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);
  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. {
  479. ui8 skill = getCurrentSpellcaster()->getEffectLevel(SpellID(SpellID::TELEPORT).toSpell());
  480. return owner.curInt->cb->battleCanTeleportTo(selectedStack, targetHex, skill);
  481. }
  482. case PossiblePlayerBattleAction::SACRIFICE: //choose our living stack to sacrifice
  483. return targetStack && targetStack != selectedStack && targetStackOwned && targetStack->alive();
  484. case PossiblePlayerBattleAction::OBSTACLE:
  485. case PossiblePlayerBattleAction::FREE_LOCATION:
  486. return isCastingPossibleHere(action.spell().toSpell(), targetStack, targetHex);
  487. case PossiblePlayerBattleAction::CATAPULT:
  488. return owner.siegeController && owner.siegeController->isAttackableByCatapult(targetHex);
  489. case PossiblePlayerBattleAction::HEAL:
  490. return targetStack && targetStackOwned && targetStack->canBeHealed();
  491. }
  492. assert(0);
  493. return false;
  494. }
  495. void BattleActionsController::actionRealize(PossiblePlayerBattleAction action, BattleHex targetHex)
  496. {
  497. const CStack * targetStack = getStackForHex(targetHex);
  498. switch (action.get()) //display console message, realize selected action
  499. {
  500. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  501. {
  502. owner.stackActivated(targetStack);
  503. return;
  504. }
  505. case PossiblePlayerBattleAction::MOVE_TACTICS:
  506. case PossiblePlayerBattleAction::MOVE_STACK:
  507. {
  508. if(owner.stacksController->getActiveStack()->doubleWide())
  509. {
  510. std::vector<BattleHex> acc = owner.curInt->cb->battleGetAvailableHexes(owner.stacksController->getActiveStack(), true);
  511. BattleHex shiftedDest = targetHex.cloneInDirection(owner.stacksController->getActiveStack()->destShiftDir(), false);
  512. if(vstd::contains(acc, targetHex))
  513. owner.giveCommand(EActionType::WALK, targetHex);
  514. else if(vstd::contains(acc, shiftedDest))
  515. owner.giveCommand(EActionType::WALK, shiftedDest);
  516. }
  517. else
  518. {
  519. owner.giveCommand(EActionType::WALK, targetHex);
  520. }
  521. return;
  522. }
  523. case PossiblePlayerBattleAction::ATTACK:
  524. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  525. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  526. {
  527. bool returnAfterAttack = action.get() == PossiblePlayerBattleAction::ATTACK_AND_RETURN;
  528. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(targetHex);
  529. if(attackFromHex.isValid()) //we can be in this line when unreachable creature is L - clicked (as of revision 1308)
  530. {
  531. auto command = new BattleAction(BattleAction::makeMeleeAttack(owner.stacksController->getActiveStack(), targetHex, attackFromHex, returnAfterAttack));
  532. owner.sendCommand(command, owner.stacksController->getActiveStack());
  533. }
  534. return;
  535. }
  536. case PossiblePlayerBattleAction::SHOOT:
  537. {
  538. owner.giveCommand(EActionType::SHOOT, targetHex);
  539. return;
  540. }
  541. case PossiblePlayerBattleAction::HEAL:
  542. {
  543. owner.giveCommand(EActionType::STACK_HEAL, targetHex);
  544. return;
  545. };
  546. case PossiblePlayerBattleAction::CATAPULT:
  547. {
  548. owner.giveCommand(EActionType::CATAPULT, targetHex);
  549. return;
  550. }
  551. case PossiblePlayerBattleAction::CREATURE_INFO:
  552. {
  553. GH.pushIntT<CStackWindow>(targetStack, false);
  554. return;
  555. }
  556. case PossiblePlayerBattleAction::HERO_INFO:
  557. {
  558. if (targetHex == BattleHex::HERO_ATTACKER)
  559. owner.attackingHero->heroLeftClicked();
  560. if (targetHex == BattleHex::HERO_DEFENDER)
  561. owner.defendingHero->heroLeftClicked();
  562. return;
  563. }
  564. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  565. case PossiblePlayerBattleAction::ANY_LOCATION:
  566. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  567. case PossiblePlayerBattleAction::TELEPORT:
  568. case PossiblePlayerBattleAction::OBSTACLE:
  569. case PossiblePlayerBattleAction::SACRIFICE:
  570. case PossiblePlayerBattleAction::FREE_LOCATION:
  571. {
  572. if (action.get() == PossiblePlayerBattleAction::AIMED_SPELL_CREATURE )
  573. {
  574. if (action.spell() == SpellID::SACRIFICE)
  575. {
  576. heroSpellToCast->aimToHex(targetHex);
  577. possibleActions.push_back({PossiblePlayerBattleAction::SACRIFICE, action.spell()});
  578. selectedStack = targetStack;
  579. return;
  580. }
  581. if (action.spell() == SpellID::TELEPORT)
  582. {
  583. heroSpellToCast->aimToUnit(targetStack);
  584. possibleActions.push_back({PossiblePlayerBattleAction::TELEPORT, action.spell()});
  585. selectedStack = targetStack;
  586. return;
  587. }
  588. }
  589. if (!spellcastingModeActive())
  590. {
  591. if (action.spell().toSpell())
  592. {
  593. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex, action.spell());
  594. }
  595. else //unknown random spell
  596. {
  597. owner.giveCommand(EActionType::MONSTER_SPELL, targetHex);
  598. }
  599. }
  600. else
  601. {
  602. assert(getHeroSpellToCast());
  603. switch (getHeroSpellToCast()->id.toEnum())
  604. {
  605. case SpellID::SACRIFICE:
  606. heroSpellToCast->aimToUnit(targetStack);//victim
  607. break;
  608. default:
  609. heroSpellToCast->aimToHex(targetHex);
  610. break;
  611. }
  612. owner.curInt->cb->battleMakeAction(heroSpellToCast.get());
  613. endCastingSpell();
  614. }
  615. selectedStack = nullptr;
  616. return;
  617. }
  618. }
  619. assert(0);
  620. return;
  621. }
  622. PossiblePlayerBattleAction BattleActionsController::selectAction(BattleHex targetHex)
  623. {
  624. assert(owner.stacksController->getActiveStack() != nullptr);
  625. assert(!possibleActions.empty());
  626. assert(targetHex.isValid());
  627. if (owner.stacksController->getActiveStack() == nullptr)
  628. return PossiblePlayerBattleAction::INVALID;
  629. if (possibleActions.empty())
  630. return PossiblePlayerBattleAction::INVALID;
  631. const CStack * targetStack = getStackForHex(targetHex);
  632. reorderPossibleActionsPriority(owner.stacksController->getActiveStack(), targetStack ? MouseHoveredHexContext::OCCUPIED_HEX : MouseHoveredHexContext::UNOCCUPIED_HEX);
  633. for (PossiblePlayerBattleAction action : possibleActions)
  634. {
  635. if (actionIsLegal(action, targetHex))
  636. return action;
  637. }
  638. return possibleActions.front();
  639. }
  640. void BattleActionsController::onHexHovered(BattleHex hoveredHex)
  641. {
  642. if (owner.openingPlaying())
  643. {
  644. currentConsoleMsg = VLC->generaltexth->translate("vcmi.battleWindow.pressKeyToSkipIntro");
  645. GH.statusbar->write(currentConsoleMsg);
  646. return;
  647. }
  648. if (owner.stacksController->getActiveStack() == nullptr)
  649. return;
  650. if (hoveredHex == BattleHex::INVALID)
  651. {
  652. if (!currentConsoleMsg.empty())
  653. GH.statusbar->clearIfMatching(currentConsoleMsg);
  654. currentConsoleMsg.clear();
  655. CCS->curh->set(Cursor::Combat::BLOCKED);
  656. return;
  657. }
  658. auto action = selectAction(hoveredHex);
  659. std::string newConsoleMsg;
  660. if (actionIsLegal(action, hoveredHex))
  661. {
  662. actionSetCursor(action, hoveredHex);
  663. newConsoleMsg = actionGetStatusMessage(action, hoveredHex);
  664. }
  665. else
  666. {
  667. actionSetCursorBlocked(action, hoveredHex);
  668. newConsoleMsg = actionGetStatusMessageBlocked(action, hoveredHex);
  669. }
  670. if (!currentConsoleMsg.empty())
  671. GH.statusbar->clearIfMatching(currentConsoleMsg);
  672. if (!newConsoleMsg.empty())
  673. GH.statusbar->write(newConsoleMsg);
  674. currentConsoleMsg = newConsoleMsg;
  675. }
  676. void BattleActionsController::onHoverEnded()
  677. {
  678. CCS->curh->set(Cursor::Combat::POINTER);
  679. if (!currentConsoleMsg.empty())
  680. GH.statusbar->clearIfMatching(currentConsoleMsg);
  681. currentConsoleMsg.clear();
  682. }
  683. void BattleActionsController::onHexLeftClicked(BattleHex clickedHex)
  684. {
  685. static BattleHex lastSelectedHex;
  686. static BattleHex lastDirectionalHex;
  687. static PossiblePlayerBattleAction::Actions lastSelectedAction;
  688. if (owner.stacksController->getActiveStack() == nullptr)
  689. return;
  690. auto action = selectAction(clickedHex);
  691. std::string newConsoleMsg;
  692. if (!actionIsLegal(action, clickedHex))
  693. return;
  694. auto directionalHex = lastDirectionalHex;
  695. if(action.get() == PossiblePlayerBattleAction::ATTACK
  696. || action.get() == PossiblePlayerBattleAction::WALK_AND_ATTACK
  697. || action.get() == PossiblePlayerBattleAction::ATTACK_AND_RETURN)
  698. directionalHex = owner.fieldController->fromWhichHexAttack(clickedHex);
  699. if(!touchscreenMode || (lastSelectedAction == action.get() && lastSelectedHex == clickedHex && lastDirectionalHex == directionalHex))
  700. {
  701. actionRealize(action, clickedHex);
  702. GH.statusbar->clear();
  703. }
  704. else
  705. {
  706. lastSelectedAction = action.get();
  707. lastSelectedHex = clickedHex;
  708. lastDirectionalHex = directionalHex;
  709. }
  710. }
  711. void BattleActionsController::tryActivateStackSpellcasting(const CStack *casterStack)
  712. {
  713. creatureSpells.clear();
  714. bool spellcaster = casterStack->hasBonusOfType(Bonus::SPELLCASTER);
  715. if(casterStack->canCast() && spellcaster)
  716. {
  717. // faerie dragon can cast only one, randomly selected spell until their next move
  718. //TODO: faerie dragon type spell should be selected by server
  719. const auto * spellToCast = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), casterStack, CBattleInfoCallback::RANDOM_AIMED).toSpell();
  720. if (spellToCast)
  721. creatureSpells.push_back(spellToCast);
  722. }
  723. TConstBonusListPtr bl = casterStack->getBonuses(Selector::type()(Bonus::SPELLCASTER));
  724. for (auto const & bonus : *bl)
  725. {
  726. if (bonus->additionalInfo[0] <= 0)
  727. creatureSpells.push_back(SpellID(bonus->subtype).toSpell());
  728. }
  729. }
  730. const spells::Caster * BattleActionsController::getCurrentSpellcaster() const
  731. {
  732. if (heroSpellToCast)
  733. return owner.getActiveHero();
  734. else
  735. return owner.stacksController->getActiveStack();
  736. }
  737. spells::Mode BattleActionsController::getCurrentCastMode() const
  738. {
  739. if (heroSpellToCast)
  740. return spells::Mode::HERO;
  741. else
  742. return spells::Mode::CREATURE_ACTIVE;
  743. }
  744. bool BattleActionsController::isCastingPossibleHere(const CSpell * currentSpell, const CStack *targetStack, BattleHex targetHex)
  745. {
  746. assert(currentSpell);
  747. if (!currentSpell)
  748. return false;
  749. auto caster = getCurrentSpellcaster();
  750. const spells::Mode mode = heroSpellToCast ? spells::Mode::HERO : spells::Mode::CREATURE_ACTIVE;
  751. spells::Target target;
  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.pushIntT<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. }