BattleActionsController.cpp 33 KB

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