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