BattleActionsController.cpp 33 KB

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