BattleActionsController.cpp 33 KB

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