BattleActionsController.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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->getMovementRange() > 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. SpellID spellID = owner.getBattle()->getRandomBeneficialSpell(CRandomGenerator::getDefault(), owner.stacksController->getActiveStack(), targetStack);
  508. return spellID != SpellID::NONE;
  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. if (owner.stacksController->getActiveStack() == nullptr)
  717. return;
  718. auto action = selectAction(clickedHex);
  719. std::string newConsoleMsg;
  720. if (!actionIsLegal(action, clickedHex))
  721. return;
  722. actionRealize(action, clickedHex);
  723. GH.statusbar()->clear();
  724. }
  725. void BattleActionsController::tryActivateStackSpellcasting(const CStack *casterStack)
  726. {
  727. creatureSpells.clear();
  728. bool spellcaster = casterStack->hasBonusOfType(BonusType::SPELLCASTER);
  729. if(casterStack->canCast() && spellcaster)
  730. {
  731. // faerie dragon can cast only one, randomly selected spell until their next move
  732. //TODO: faerie dragon type spell should be selected by server
  733. const auto * spellToCast = owner.getBattle()->getRandomCastedSpell(CRandomGenerator::getDefault(), casterStack).toSpell();
  734. if (spellToCast)
  735. creatureSpells.push_back(spellToCast);
  736. }
  737. TConstBonusListPtr bl = casterStack->getBonuses(Selector::type()(BonusType::SPELLCASTER));
  738. for(const auto & bonus : *bl)
  739. {
  740. if (bonus->additionalInfo[0] <= 0)
  741. creatureSpells.push_back(bonus->subtype.as<SpellID>().toSpell());
  742. }
  743. }
  744. const spells::Caster * BattleActionsController::getCurrentSpellcaster() const
  745. {
  746. if (heroSpellToCast)
  747. return owner.currentHero();
  748. else
  749. return owner.stacksController->getActiveStack();
  750. }
  751. spells::Mode BattleActionsController::getCurrentCastMode() const
  752. {
  753. if (heroSpellToCast)
  754. return spells::Mode::HERO;
  755. else
  756. return spells::Mode::CREATURE_ACTIVE;
  757. }
  758. bool BattleActionsController::isCastingPossibleHere(const CSpell * currentSpell, const CStack *targetStack, BattleHex targetHex)
  759. {
  760. assert(currentSpell);
  761. if (!currentSpell)
  762. return false;
  763. auto caster = getCurrentSpellcaster();
  764. const spells::Mode mode = heroSpellToCast ? spells::Mode::HERO : spells::Mode::CREATURE_ACTIVE;
  765. spells::Target target;
  766. if(targetStack)
  767. target.emplace_back(targetStack);
  768. target.emplace_back(targetHex);
  769. spells::BattleCast cast(owner.getBattle().get(), caster, mode, currentSpell);
  770. auto m = currentSpell->battleMechanics(&cast);
  771. spells::detail::ProblemImpl problem; //todo: display problem in status bar
  772. return m->canBeCastAt(target, problem);
  773. }
  774. bool BattleActionsController::canStackMoveHere(const CStack * stackToMove, BattleHex myNumber) const
  775. {
  776. std::vector<BattleHex> acc = owner.getBattle()->battleGetAvailableHexes(stackToMove, false);
  777. BattleHex shiftedDest = myNumber.cloneInDirection(stackToMove->destShiftDir(), false);
  778. if (vstd::contains(acc, myNumber))
  779. return true;
  780. else if (stackToMove->doubleWide() && vstd::contains(acc, shiftedDest))
  781. return true;
  782. else
  783. return false;
  784. }
  785. void BattleActionsController::activateStack()
  786. {
  787. const CStack * s = owner.stacksController->getActiveStack();
  788. if(s)
  789. {
  790. tryActivateStackSpellcasting(s);
  791. possibleActions = getPossibleActionsForStack(s);
  792. std::list<PossiblePlayerBattleAction> actionsToSelect;
  793. if(!possibleActions.empty())
  794. {
  795. switch(possibleActions.front().get())
  796. {
  797. case PossiblePlayerBattleAction::SHOOT:
  798. actionsToSelect.push_back(possibleActions.front());
  799. actionsToSelect.push_back(PossiblePlayerBattleAction::ATTACK);
  800. break;
  801. case PossiblePlayerBattleAction::ATTACK_AND_RETURN:
  802. actionsToSelect.push_back(possibleActions.front());
  803. actionsToSelect.push_back(PossiblePlayerBattleAction::WALK_AND_ATTACK);
  804. break;
  805. case PossiblePlayerBattleAction::AIMED_SPELL_CREATURE:
  806. actionsToSelect.push_back(possibleActions.front());
  807. actionsToSelect.push_back(PossiblePlayerBattleAction::ATTACK);
  808. break;
  809. case PossiblePlayerBattleAction::ANY_LOCATION:
  810. actionsToSelect.push_back(possibleActions.front());
  811. actionsToSelect.push_back(PossiblePlayerBattleAction::ATTACK);
  812. break;
  813. }
  814. }
  815. owner.windowObject->setAlternativeActions(actionsToSelect);
  816. }
  817. }
  818. void BattleActionsController::onHexRightClicked(BattleHex clickedHex)
  819. {
  820. auto spellcastActionPredicate = [](PossiblePlayerBattleAction & action)
  821. {
  822. return action.spellcast();
  823. };
  824. bool isCurrentStackInSpellcastMode = !possibleActions.empty() && std::all_of(possibleActions.begin(), possibleActions.end(), spellcastActionPredicate);
  825. if (spellcastingModeActive() || isCurrentStackInSpellcastMode)
  826. {
  827. endCastingSpell();
  828. CRClickPopup::createAndPush(CGI->generaltexth->translate("core.genrltxt.731")); // spell cancelled
  829. return;
  830. }
  831. auto selectedStack = owner.getBattle()->battleGetStackByPos(clickedHex, true);
  832. if (selectedStack != nullptr)
  833. GH.windows().createAndPushWindow<CStackWindow>(selectedStack, true);
  834. if (clickedHex == BattleHex::HERO_ATTACKER && owner.attackingHero)
  835. owner.attackingHero->heroRightClicked();
  836. if (clickedHex == BattleHex::HERO_DEFENDER && owner.defendingHero)
  837. owner.defendingHero->heroRightClicked();
  838. }
  839. bool BattleActionsController::spellcastingModeActive() const
  840. {
  841. return heroSpellToCast != nullptr;
  842. }
  843. bool BattleActionsController::currentActionSpellcasting(BattleHex hoveredHex)
  844. {
  845. if (heroSpellToCast)
  846. return true;
  847. if (!owner.stacksController->getActiveStack())
  848. return false;
  849. auto action = selectAction(hoveredHex);
  850. return action.spellcast();
  851. }
  852. const std::vector<PossiblePlayerBattleAction> & BattleActionsController::getPossibleActions() const
  853. {
  854. return possibleActions;
  855. }
  856. void BattleActionsController::removePossibleAction(PossiblePlayerBattleAction action)
  857. {
  858. vstd::erase(possibleActions, action);
  859. }
  860. void BattleActionsController::pushFrontPossibleAction(PossiblePlayerBattleAction action)
  861. {
  862. possibleActions.insert(possibleActions.begin(), action);
  863. }