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. GH.fakeMouseMove();
  110. }
  111. bool BattleActionsController::isActiveStackSpellcaster() const
  112. {
  113. const CStack * casterStack = owner.stacksController->getActiveStack();
  114. if (!casterStack)
  115. return false;
  116. bool spellcaster = casterStack->hasBonusOfType(Bonus::SPELLCASTER);
  117. return (spellcaster && casterStack->canCast());
  118. }
  119. void BattleActionsController::enterCreatureCastingMode()
  120. {
  121. //silently check for possible errors
  122. if (owner.tacticsMode)
  123. return;
  124. //hero is casting a spell
  125. if (heroSpellToCast)
  126. return;
  127. if (!owner.stacksController->getActiveStack())
  128. return;
  129. if (!isActiveStackSpellcaster())
  130. return;
  131. for (auto const & action : possibleActions)
  132. {
  133. if (action.get() != PossiblePlayerBattleAction::NO_LOCATION)
  134. continue;
  135. const spells::Caster * caster = owner.stacksController->getActiveStack();
  136. const CSpell * spell = action.spell().toSpell();
  137. spells::Target target;
  138. target.emplace_back();
  139. spells::BattleCast cast(owner.curInt->cb.get(), caster, spells::Mode::CREATURE_ACTIVE, spell);
  140. auto m = spell->battleMechanics(&cast);
  141. spells::detail::ProblemImpl ignored;
  142. const bool isCastingPossible = m->canBeCastAt(target, ignored);
  143. if (isCastingPossible)
  144. {
  145. owner.giveCommand(EActionType::MONSTER_SPELL, BattleHex::INVALID, spell->getId());
  146. selectedStack = nullptr;
  147. CCS->curh->set(Cursor::Combat::POINTER);
  148. }
  149. return;
  150. }
  151. possibleActions = getPossibleActionsForStack(owner.stacksController->getActiveStack());
  152. auto actionFilterPredicate = [](const PossiblePlayerBattleAction x)
  153. {
  154. return !x.spellcast();
  155. };
  156. vstd::erase_if(possibleActions, actionFilterPredicate);
  157. GH.fakeMouseMove();
  158. }
  159. std::vector<PossiblePlayerBattleAction> BattleActionsController::getPossibleActionsForStack(const CStack *stack) const
  160. {
  161. BattleClientInterfaceData data; //hard to get rid of these things so for now they're required data to pass
  162. for (auto const & spell : creatureSpells)
  163. data.creatureSpellsToCast.push_back(spell->id);
  164. data.tacticsMode = owner.tacticsMode;
  165. auto allActions = owner.curInt->cb->getClientActionsForStack(stack, data);
  166. allActions.push_back(PossiblePlayerBattleAction::HERO_INFO);
  167. allActions.push_back(PossiblePlayerBattleAction::CREATURE_INFO);
  168. return std::vector<PossiblePlayerBattleAction>(allActions);
  169. }
  170. void BattleActionsController::reorderPossibleActionsPriority(const CStack * stack, MouseHoveredHexContext context)
  171. {
  172. if(owner.tacticsMode || possibleActions.empty()) return; //this function is not supposed to be called in tactics mode or before getPossibleActionsForStack
  173. auto assignPriority = [&](PossiblePlayerBattleAction const & item) -> uint8_t //large lambda assigning priority which would have to be part of possibleActions without it
  174. {
  175. switch(item.get())
  176. {
  177. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  178. case PossiblePlayerBattleAction::ANY_LOCATION:
  179. case PossiblePlayerBattleAction::NO_LOCATION:
  180. case PossiblePlayerBattleAction::FREE_LOCATION:
  181. case PossiblePlayerBattleAction::OBSTACLE:
  182. if(!stack->hasBonusOfType(Bonus::NO_SPELLCAST_BY_DEFAULT) && context == MouseHoveredHexContext::OCCUPIED_HEX)
  183. return 1;
  184. else
  185. return 100;//bottom priority
  186. break;
  187. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  188. return 2; break;
  189. case PossiblePlayerBattleAction::SHOOT:
  190. return 4; break;
  191. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  192. return 5; break;
  193. case PossiblePlayerBattleAction::ATTACK:
  194. return 6; break;
  195. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  196. return 7; break;
  197. case PossiblePlayerBattleAction::MOVE_STACK:
  198. return 8; break;
  199. case PossiblePlayerBattleAction::CATAPULT:
  200. return 9; break;
  201. case PossiblePlayerBattleAction::HEAL:
  202. return 10; break;
  203. case PossiblePlayerBattleAction::CREATURE_INFO:
  204. return 11; break;
  205. case PossiblePlayerBattleAction::HERO_INFO:
  206. return 12; break;
  207. case PossiblePlayerBattleAction::TELEPORT:
  208. return 13; break;
  209. default:
  210. assert(0);
  211. return 200; break;
  212. }
  213. };
  214. auto comparer = [&](PossiblePlayerBattleAction const & lhs, PossiblePlayerBattleAction const & rhs)
  215. {
  216. return assignPriority(lhs) < assignPriority(rhs);
  217. };
  218. std::sort(possibleActions.begin(), possibleActions.end(), comparer);
  219. }
  220. void BattleActionsController::castThisSpell(SpellID spellID)
  221. {
  222. heroSpellToCast = std::make_shared<BattleAction>();
  223. heroSpellToCast->actionType = EActionType::HERO_SPELL;
  224. heroSpellToCast->actionSubtype = spellID; //spell number
  225. heroSpellToCast->stackNumber = (owner.attackingHeroInstance->tempOwner == owner.curInt->playerID) ? -1 : -2;
  226. heroSpellToCast->side = owner.defendingHeroInstance ? (owner.curInt->playerID == owner.defendingHeroInstance->tempOwner) : false;
  227. //choosing possible targets
  228. const CGHeroInstance *castingHero = (owner.attackingHeroInstance->tempOwner == owner.curInt->playerID) ? owner.attackingHeroInstance : owner.defendingHeroInstance;
  229. assert(castingHero); // code below assumes non-null hero
  230. PossiblePlayerBattleAction spellSelMode = owner.curInt->cb->getCasterAction(spellID.toSpell(), castingHero, spells::Mode::HERO);
  231. if (spellSelMode.get() == PossiblePlayerBattleAction::NO_LOCATION) //user does not have to select location
  232. {
  233. heroSpellToCast->aimToHex(BattleHex::INVALID);
  234. owner.curInt->cb->battleMakeAction(heroSpellToCast.get());
  235. endCastingSpell();
  236. }
  237. else
  238. {
  239. possibleActions.clear();
  240. possibleActions.push_back (spellSelMode); //only this one action can be performed at the moment
  241. GH.fakeMouseMove();//update cursor
  242. }
  243. owner.windowObject->blockUI(true);
  244. }
  245. const CSpell * BattleActionsController::getHeroSpellToCast( ) const
  246. {
  247. if (heroSpellToCast)
  248. return SpellID(heroSpellToCast->actionSubtype).toSpell();
  249. return nullptr;
  250. }
  251. const CSpell * BattleActionsController::getStackSpellToCast(BattleHex hoveredHex)
  252. {
  253. if (heroSpellToCast)
  254. return nullptr;
  255. if (!owner.stacksController->getActiveStack())
  256. return nullptr;
  257. if (!hoveredHex.isValid())
  258. return nullptr;
  259. auto action = selectAction(hoveredHex);
  260. if (action.spell() == SpellID::NONE)
  261. return nullptr;
  262. return action.spell().toSpell();
  263. }
  264. const CSpell * BattleActionsController::getCurrentSpell(BattleHex hoveredHex)
  265. {
  266. if (getHeroSpellToCast())
  267. return getHeroSpellToCast();
  268. return getStackSpellToCast(hoveredHex);
  269. }
  270. const CStack * BattleActionsController::getStackForHex(BattleHex hoveredHex)
  271. {
  272. const CStack * shere = owner.curInt->cb->battleGetStackByPos(hoveredHex, true);
  273. if(shere)
  274. return shere;
  275. return owner.curInt->cb->battleGetStackByPos(hoveredHex, false);
  276. }
  277. void BattleActionsController::actionSetCursor(PossiblePlayerBattleAction action, BattleHex targetHex)
  278. {
  279. switch (action.get())
  280. {
  281. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  282. CCS->curh->set(Cursor::Combat::POINTER);
  283. return;
  284. case PossiblePlayerBattleAction::MOVE_TACTICS:
  285. case PossiblePlayerBattleAction::MOVE_STACK:
  286. if (owner.stacksController->getActiveStack()->hasBonusOfType(Bonus::FLYING))
  287. CCS->curh->set(Cursor::Combat::FLY);
  288. else
  289. CCS->curh->set(Cursor::Combat::MOVE);
  290. return;
  291. case PossiblePlayerBattleAction::ATTACK:
  292. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  293. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  294. owner.fieldController->setBattleCursor(targetHex);
  295. return;
  296. case PossiblePlayerBattleAction::SHOOT:
  297. if (owner.curInt->cb->battleHasShootingPenalty(owner.stacksController->getActiveStack(), targetHex))
  298. CCS->curh->set(Cursor::Combat::SHOOT_PENALTY);
  299. else
  300. CCS->curh->set(Cursor::Combat::SHOOT);
  301. return;
  302. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  303. case PossiblePlayerBattleAction::ANY_LOCATION:
  304. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  305. case PossiblePlayerBattleAction::FREE_LOCATION:
  306. case PossiblePlayerBattleAction::OBSTACLE:
  307. CCS->curh->set(Cursor::Spellcast::SPELL);
  308. return;
  309. case PossiblePlayerBattleAction::TELEPORT:
  310. CCS->curh->set(Cursor::Combat::TELEPORT);
  311. return;
  312. case PossiblePlayerBattleAction::SACRIFICE:
  313. CCS->curh->set(Cursor::Combat::SACRIFICE);
  314. return;
  315. case PossiblePlayerBattleAction::HEAL:
  316. CCS->curh->set(Cursor::Combat::HEAL);
  317. return;
  318. case PossiblePlayerBattleAction::CATAPULT:
  319. CCS->curh->set(Cursor::Combat::SHOOT_CATAPULT);
  320. return;
  321. case PossiblePlayerBattleAction::CREATURE_INFO:
  322. CCS->curh->set(Cursor::Combat::QUERY);
  323. return;
  324. case PossiblePlayerBattleAction::HERO_INFO:
  325. CCS->curh->set(Cursor::Combat::HERO);
  326. return;
  327. }
  328. assert(0);
  329. }
  330. void BattleActionsController::actionSetCursorBlocked(PossiblePlayerBattleAction action, BattleHex targetHex)
  331. {
  332. switch (action.get())
  333. {
  334. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  335. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  336. case PossiblePlayerBattleAction::TELEPORT:
  337. case PossiblePlayerBattleAction::SACRIFICE:
  338. case PossiblePlayerBattleAction::FREE_LOCATION:
  339. CCS->curh->set(Cursor::Combat::BLOCKED);
  340. return;
  341. default:
  342. if (targetHex == -1)
  343. CCS->curh->set(Cursor::Combat::POINTER);
  344. else
  345. CCS->curh->set(Cursor::Combat::BLOCKED);
  346. return;
  347. }
  348. assert(0);
  349. }
  350. std::string BattleActionsController::actionGetStatusMessage(PossiblePlayerBattleAction action, BattleHex targetHex)
  351. {
  352. const CStack * targetStack = getStackForHex(targetHex);
  353. switch (action.get()) //display console message, realize selected action
  354. {
  355. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  356. return (boost::format(CGI->generaltexth->allTexts[481]) % targetStack->getName()).str(); //Select %s
  357. case PossiblePlayerBattleAction::MOVE_TACTICS:
  358. case PossiblePlayerBattleAction::MOVE_STACK:
  359. if (owner.stacksController->getActiveStack()->hasBonusOfType(Bonus::FLYING))
  360. return (boost::format(CGI->generaltexth->allTexts[295]) % owner.stacksController->getActiveStack()->getName()).str(); //Fly %s here
  361. else
  362. return (boost::format(CGI->generaltexth->allTexts[294]) % owner.stacksController->getActiveStack()->getName()).str(); //Move %s here
  363. case PossiblePlayerBattleAction::ATTACK:
  364. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  365. case PossiblePlayerBattleAction::ATTACK_AND_RETURN: //TODO: allow to disable return
  366. {
  367. BattleHex attackFromHex = owner.fieldController->fromWhichHexAttack(targetHex);
  368. DamageEstimation estimation = owner.curInt->cb->battleEstimateDamage(owner.stacksController->getActiveStack(), targetStack, attackFromHex);
  369. estimation.kills.max = std::min<int64_t>(estimation.kills.max, targetStack->getCount());
  370. estimation.kills.min = std::min<int64_t>(estimation.kills.min, targetStack->getCount());
  371. return formatMeleeAttack(estimation, targetStack->getName());
  372. }
  373. case PossiblePlayerBattleAction::SHOOT:
  374. {
  375. const auto * shooter = owner.stacksController->getActiveStack();
  376. DamageEstimation estimation = owner.curInt->cb->battleEstimateDamage(shooter, targetStack, shooter->getPosition());
  377. estimation.kills.max = std::min<int64_t>(estimation.kills.max, targetStack->getCount());
  378. estimation.kills.min = std::min<int64_t>(estimation.kills.min, targetStack->getCount());
  379. return formatRangedAttack(estimation, targetStack->getName(), shooter->shots.available());
  380. }
  381. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  382. return boost::str(boost::format(CGI->generaltexth->allTexts[27]) % action.spell().toSpell()->getNameTranslated() % targetStack->getName()); //Cast %s on %s
  383. case PossiblePlayerBattleAction::ANY_LOCATION:
  384. return boost::str(boost::format(CGI->generaltexth->allTexts[26]) % action.spell().toSpell()->getNameTranslated()); //Cast %s
  385. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL: //we assume that teleport / sacrifice will never be available as random spell
  386. return boost::str(boost::format(CGI->generaltexth->allTexts[301]) % targetStack->getName()); //Cast a spell on %
  387. case PossiblePlayerBattleAction::TELEPORT:
  388. return CGI->generaltexth->allTexts[25]; //Teleport Here
  389. case PossiblePlayerBattleAction::OBSTACLE:
  390. return CGI->generaltexth->allTexts[550];
  391. case PossiblePlayerBattleAction::SACRIFICE:
  392. return (boost::format(CGI->generaltexth->allTexts[549]) % targetStack->getName()).str(); //sacrifice the %s
  393. case PossiblePlayerBattleAction::FREE_LOCATION:
  394. return boost::str(boost::format(CGI->generaltexth->allTexts[26]) % action.spell().toSpell()->getNameTranslated()); //Cast %s
  395. case PossiblePlayerBattleAction::HEAL:
  396. return (boost::format(CGI->generaltexth->allTexts[419]) % targetStack->getName()).str(); //Apply first aid to the %s
  397. case PossiblePlayerBattleAction::CATAPULT:
  398. return ""; // TODO
  399. case PossiblePlayerBattleAction::CREATURE_INFO:
  400. return (boost::format(CGI->generaltexth->allTexts[297]) % targetStack->getName()).str();
  401. case PossiblePlayerBattleAction::HERO_INFO:
  402. return CGI->generaltexth->translate("core.genrltxt.417"); // "View Hero Stats"
  403. }
  404. assert(0);
  405. return "";
  406. }
  407. std::string BattleActionsController::actionGetStatusMessageBlocked(PossiblePlayerBattleAction action, BattleHex targetHex)
  408. {
  409. switch (action.get())
  410. {
  411. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  412. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  413. return CGI->generaltexth->allTexts[23];
  414. break;
  415. case PossiblePlayerBattleAction::TELEPORT:
  416. return CGI->generaltexth->allTexts[24]; //Invalid Teleport Destination
  417. break;
  418. case PossiblePlayerBattleAction::SACRIFICE:
  419. return CGI->generaltexth->allTexts[543]; //choose army to sacrifice
  420. break;
  421. case PossiblePlayerBattleAction::FREE_LOCATION:
  422. return boost::str(boost::format(CGI->generaltexth->allTexts[181]) % action.spell().toSpell()->getNameTranslated()); //No room to place %s here
  423. break;
  424. default:
  425. return "";
  426. }
  427. }
  428. bool BattleActionsController::actionIsLegal(PossiblePlayerBattleAction action, BattleHex targetHex)
  429. {
  430. const CStack * targetStack = getStackForHex(targetHex);
  431. bool targetStackOwned = targetStack && targetStack->owner == owner.curInt->playerID;
  432. switch (action.get())
  433. {
  434. case PossiblePlayerBattleAction::CHOOSE_TACTICS_STACK:
  435. return (targetStack && targetStackOwned && targetStack->Speed() > 0);
  436. case PossiblePlayerBattleAction::CREATURE_INFO:
  437. return (targetStack && targetStackOwned);
  438. case PossiblePlayerBattleAction::HERO_INFO:
  439. if (targetHex == BattleHex::HERO_ATTACKER)
  440. return owner.attackingHero != nullptr;
  441. if (targetHex == BattleHex::HERO_DEFENDER)
  442. return owner.defendingHero != nullptr;
  443. return false;
  444. case PossiblePlayerBattleAction::MOVE_TACTICS:
  445. case PossiblePlayerBattleAction::MOVE_STACK:
  446. if (!(targetStack && targetStack->alive())) //we can walk on dead stacks
  447. {
  448. if(canStackMoveHere(owner.stacksController->getActiveStack(), targetHex))
  449. return true;
  450. }
  451. return false;
  452. case PossiblePlayerBattleAction::ATTACK:
  453. case PossiblePlayerBattleAction::WALK_AND_ATTACK:
  454. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  455. if(owner.curInt->cb->battleCanAttack(owner.stacksController->getActiveStack(), targetStack, targetHex))
  456. {
  457. if (owner.fieldController->isTileAttackable(targetHex)) // move isTileAttackable to be part of battleCanAttack?
  458. return true;
  459. }
  460. return false;
  461. case PossiblePlayerBattleAction::SHOOT:
  462. return owner.curInt->cb->battleCanShoot(owner.stacksController->getActiveStack(), targetHex);
  463. case PossiblePlayerBattleAction::NO_LOCATION:
  464. return false;
  465. case PossiblePlayerBattleAction::ANY_LOCATION:
  466. return isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  467. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  468. return !selectedStack && targetStack && isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  469. case PossiblePlayerBattleAction::RANDOM_GENIE_SPELL:
  470. if(targetStack && targetStackOwned && targetStack != owner.stacksController->getActiveStack() && targetStack->alive()) //only positive spells for other allied creatures
  471. {
  472. int spellID = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), targetStack, CBattleInfoCallback::RANDOM_GENIE);
  473. return spellID > -1;
  474. }
  475. return false;
  476. case PossiblePlayerBattleAction::TELEPORT:
  477. {
  478. ui8 skill = getCurrentSpellcaster()->getEffectLevel(SpellID(SpellID::TELEPORT).toSpell());
  479. return owner.curInt->cb->battleCanTeleportTo(selectedStack, targetHex, skill);
  480. }
  481. case PossiblePlayerBattleAction::SACRIFICE: //choose our living stack to sacrifice
  482. return targetStack && targetStack != selectedStack && targetStackOwned && targetStack->alive();
  483. case PossiblePlayerBattleAction::OBSTACLE:
  484. case PossiblePlayerBattleAction::FREE_LOCATION:
  485. return isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), targetStack, targetHex);
  486. return isCastingPossibleHere(action.spell().toSpell(), owner.stacksController->getActiveStack(), 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 *casterStack, 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. }