BattleActionsController.cpp 34 KB

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