BattleSpellMechanics.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * BattleSpellMechanics.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 "BattleSpellMechanics.h"
  12. #include "Problem.h"
  13. #include "CSpellHandler.h"
  14. #include "../battle/IBattleState.h"
  15. #include "../battle/CBattleInfoCallback.h"
  16. #include "../CStack.h"
  17. #include "../NetPacks.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. namespace spells
  20. {
  21. namespace SRSLPraserHelpers
  22. {
  23. static int XYToHex(int x, int y)
  24. {
  25. return x + GameConstants::BFIELD_WIDTH * y;
  26. }
  27. static int XYToHex(std::pair<int, int> xy)
  28. {
  29. return XYToHex(xy.first, xy.second);
  30. }
  31. static int hexToY(int battleFieldPosition)
  32. {
  33. return battleFieldPosition/GameConstants::BFIELD_WIDTH;
  34. }
  35. static int hexToX(int battleFieldPosition)
  36. {
  37. int pos = battleFieldPosition - hexToY(battleFieldPosition) * GameConstants::BFIELD_WIDTH;
  38. return pos;
  39. }
  40. static std::pair<int, int> hexToPair(int battleFieldPosition)
  41. {
  42. return std::make_pair(hexToX(battleFieldPosition), hexToY(battleFieldPosition));
  43. }
  44. //moves hex by one hex in given direction
  45. //0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left
  46. static std::pair<int, int> gotoDir(int x, int y, int direction)
  47. {
  48. switch(direction)
  49. {
  50. case 0: //top left
  51. return std::make_pair((y%2) ? x-1 : x, y-1);
  52. case 1: //top right
  53. return std::make_pair((y%2) ? x : x+1, y-1);
  54. case 2: //right
  55. return std::make_pair(x+1, y);
  56. case 3: //right bottom
  57. return std::make_pair((y%2) ? x : x+1, y+1);
  58. case 4: //left bottom
  59. return std::make_pair((y%2) ? x-1 : x, y+1);
  60. case 5: //left
  61. return std::make_pair(x-1, y);
  62. default:
  63. throw std::runtime_error("Disaster: wrong direction in SRSLPraserHelpers::gotoDir!\n");
  64. }
  65. }
  66. static std::pair<int, int> gotoDir(std::pair<int, int> xy, int direction)
  67. {
  68. return gotoDir(xy.first, xy.second, direction);
  69. }
  70. static bool isGoodHex(std::pair<int, int> xy)
  71. {
  72. return xy.first >=0 && xy.first < GameConstants::BFIELD_WIDTH && xy.second >= 0 && xy.second < GameConstants::BFIELD_HEIGHT;
  73. }
  74. //helper function for rangeInHexes
  75. static std::set<ui16> getInRange(unsigned int center, int low, int high)
  76. {
  77. std::set<ui16> ret;
  78. if(low == 0)
  79. {
  80. ret.insert(center);
  81. }
  82. std::pair<int, int> mainPointForLayer[6]; //A, B, C, D, E, F points
  83. for(auto & elem : mainPointForLayer)
  84. elem = hexToPair(center);
  85. for(int it=1; it<=high; ++it) //it - distance to the center
  86. {
  87. for(int b=0; b<6; ++b)
  88. mainPointForLayer[b] = gotoDir(mainPointForLayer[b], b);
  89. if(it>=low)
  90. {
  91. std::pair<int, int> curHex;
  92. //adding lines (A-b, B-c, C-d, etc)
  93. for(int v=0; v<6; ++v)
  94. {
  95. curHex = mainPointForLayer[v];
  96. for(int h=0; h<it; ++h)
  97. {
  98. if(isGoodHex(curHex))
  99. ret.insert(XYToHex(curHex));
  100. curHex = gotoDir(curHex, (v+2)%6);
  101. }
  102. }
  103. } //if(it>=low)
  104. }
  105. return ret;
  106. }
  107. }
  108. BattleSpellMechanics::BattleSpellMechanics(const IBattleCast * event,
  109. std::shared_ptr<effects::Effects> effects_,
  110. std::shared_ptr<IReceptiveCheck> targetCondition_):
  111. BaseMechanics(event),
  112. effects(std::move(effects_)),
  113. targetCondition(std::move(targetCondition_))
  114. {}
  115. BattleSpellMechanics::~BattleSpellMechanics() = default;
  116. void BattleSpellMechanics::applyEffects(ServerCallback * server, const Target & targets, bool indirect, bool ignoreImmunity) const
  117. {
  118. auto callback = [&](const effects::Effect * effect, bool & stop)
  119. {
  120. if(indirect == effect->indirect)
  121. {
  122. if(ignoreImmunity)
  123. {
  124. effect->apply(server, this, targets);
  125. }
  126. else
  127. {
  128. EffectTarget filtered = effect->filterTarget(this, targets);
  129. effect->apply(server, this, filtered);
  130. }
  131. }
  132. };
  133. effects->forEachEffect(getEffectLevel(), callback);
  134. }
  135. bool BattleSpellMechanics::canBeCast(Problem & problem) const
  136. {
  137. auto genProblem = battle()->battleCanCastSpell(caster, mode);
  138. if(genProblem != ESpellCastProblem::OK)
  139. return adaptProblem(genProblem, problem);
  140. switch(mode)
  141. {
  142. case Mode::HERO:
  143. {
  144. const auto * castingHero = dynamic_cast<const CGHeroInstance *>(caster); //todo: unify hero|creature spell cost
  145. if(!castingHero)
  146. {
  147. logGlobal->debug("CSpell::canBeCast: invalid caster");
  148. genProblem = ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
  149. }
  150. else if(!castingHero->getArt(ArtifactPosition::SPELLBOOK))
  151. genProblem = ESpellCastProblem::NO_SPELLBOOK;
  152. else if(!castingHero->canCastThisSpell(owner))
  153. genProblem = ESpellCastProblem::HERO_DOESNT_KNOW_SPELL;
  154. else if(castingHero->mana < battle()->battleGetSpellCost(owner, castingHero)) //not enough mana
  155. genProblem = ESpellCastProblem::NOT_ENOUGH_MANA;
  156. }
  157. break;
  158. }
  159. if(genProblem != ESpellCastProblem::OK)
  160. return adaptProblem(genProblem, problem);
  161. if(!owner->isCombat())
  162. return adaptProblem(ESpellCastProblem::ADVMAP_SPELL_INSTEAD_OF_BATTLE_SPELL, problem);
  163. const PlayerColor player = caster->getCasterOwner();
  164. const auto side = battle()->playerToSide(player);
  165. if(!side)
  166. return adaptProblem(ESpellCastProblem::INVALID, problem);
  167. //effect like Recanter's Cloak. Blocks also passive casting.
  168. //TODO: check creature abilities to block
  169. //TODO: check any possible caster
  170. if(battle()->battleMaxSpellLevel(side.value()) < getSpellLevel() || battle()->battleMinSpellLevel(side.value()) > getSpellLevel())
  171. return adaptProblem(ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED, problem);
  172. return effects->applicable(problem, this);
  173. }
  174. bool BattleSpellMechanics::canBeCastAt(const Target & target, Problem & problem) const
  175. {
  176. if(!canBeCast(problem))
  177. return false;
  178. Target spellTarget = transformSpellTarget(target);
  179. return effects->applicable(problem, this, target, spellTarget);
  180. }
  181. std::vector<const CStack *> BattleSpellMechanics::getAffectedStacks(const Target & target) const
  182. {
  183. Target spellTarget = transformSpellTarget(target);
  184. EffectTarget all;
  185. effects->forEachEffect(getEffectLevel(), [&all, &target, &spellTarget, this](const effects::Effect * e, bool & stop)
  186. {
  187. EffectTarget one = e->transformTarget(this, target, spellTarget);
  188. vstd::concatenate(all, one);
  189. });
  190. std::set<const CStack *> stacks;
  191. for(const Destination & dest : all)
  192. {
  193. if(dest.unitValue)
  194. {
  195. //FIXME: remove and return battle::Unit
  196. stacks.insert(battle()->battleGetStackByID(dest.unitValue->unitId(), false));
  197. }
  198. }
  199. std::vector<const CStack *> res;
  200. std::copy(stacks.begin(), stacks.end(), std::back_inserter(res));
  201. return res;
  202. }
  203. void BattleSpellMechanics::cast(ServerCallback * server, const Target & target)
  204. {
  205. BattleSpellCast sc;
  206. int spellCost = 0;
  207. sc.side = casterSide;
  208. sc.spellID = getSpellId();
  209. sc.battleID = battle()->getBattle()->getBattleID();
  210. sc.tile = target.at(0).hexValue;
  211. sc.castByHero = mode == Mode::HERO;
  212. sc.casterStack = caster->getCasterUnitId();
  213. sc.manaGained = 0;
  214. sc.activeCast = false;
  215. affectedUnits.clear();
  216. const CGHeroInstance * otherHero = nullptr;
  217. {
  218. //check it there is opponent hero
  219. const ui8 otherSide = battle()->otherSide(casterSide);
  220. if(battle()->battleHasHero(otherSide))
  221. otherHero = battle()->battleGetFightingHero(otherSide);
  222. }
  223. //calculate spell cost
  224. if(mode == Mode::HERO)
  225. {
  226. const auto * casterHero = dynamic_cast<const CGHeroInstance *>(caster);
  227. spellCost = battle()->battleGetSpellCost(owner, casterHero);
  228. if(nullptr != otherHero) //handle mana channel
  229. {
  230. int manaChannel = 0;
  231. for(const auto * stack : battle()->battleGetAllStacks(true)) //TODO: shouldn't bonus system handle it somehow?
  232. {
  233. if(stack->unitOwner() == otherHero->tempOwner)
  234. {
  235. vstd::amax(manaChannel, stack->valOfBonuses(BonusType::MANA_CHANNELING));
  236. }
  237. }
  238. sc.manaGained = (manaChannel * spellCost) / 100;
  239. }
  240. sc.activeCast = true;
  241. }
  242. else if(mode == Mode::CREATURE_ACTIVE || mode == Mode::ENCHANTER)
  243. {
  244. spellCost = 1;
  245. sc.activeCast = true;
  246. }
  247. beforeCast(sc, *server->getRNG(), target);
  248. BattleLogMessage castDescription;
  249. castDescription.battleID = battle()->getBattle()->getBattleID();
  250. switch (mode)
  251. {
  252. case Mode::CREATURE_ACTIVE:
  253. case Mode::ENCHANTER:
  254. case Mode::HERO:
  255. case Mode::PASSIVE:
  256. {
  257. MetaString line;
  258. caster->getCastDescription(owner, affectedUnits, line);
  259. if(!line.empty())
  260. castDescription.lines.push_back(line);
  261. }
  262. break;
  263. default:
  264. break;
  265. }
  266. doRemoveEffects(server, affectedUnits, std::bind(&BattleSpellMechanics::counteringSelector, this, _1));
  267. for(auto & unit : affectedUnits)
  268. sc.affectedCres.insert(unit->unitId());
  269. if(!castDescription.lines.empty())
  270. server->apply(&castDescription);
  271. server->apply(&sc);
  272. for(auto & p : effectsToApply)
  273. p.first->apply(server, this, p.second);
  274. if(sc.activeCast)
  275. {
  276. caster->spendMana(server, spellCost);
  277. if(sc.manaGained > 0)
  278. {
  279. assert(otherHero);
  280. otherHero->spendMana(server, -sc.manaGained);
  281. }
  282. }
  283. // send empty event to client
  284. // temporary(?) workaround to force animations to trigger
  285. StacksInjured fakeEvent;
  286. fakeEvent.battleID = battle()->getBattle()->getBattleID();
  287. server->apply(&fakeEvent);
  288. }
  289. void BattleSpellMechanics::beforeCast(BattleSpellCast & sc, vstd::RNG & rng, const Target & target)
  290. {
  291. affectedUnits.clear();
  292. Target spellTarget = transformSpellTarget(target);
  293. std::vector <const battle::Unit *> resisted;
  294. auto rangeGen = rng.getInt64Range(0, 99);
  295. auto filterResisted = [&, this](const battle::Unit * unit) -> bool
  296. {
  297. if(isNegativeSpell() && isMagicalEffect())
  298. {
  299. //magic resistance
  300. const int prob = std::min(unit->magicResistance(), 100); //probability of resistance in %
  301. if(rangeGen() < prob)
  302. return true;
  303. }
  304. return false;
  305. };
  306. auto filterUnit = [&](const battle::Unit * unit)
  307. {
  308. if(filterResisted(unit))
  309. resisted.push_back(unit);
  310. else
  311. affectedUnits.push_back(unit);
  312. };
  313. //prepare targets
  314. effectsToApply = effects->prepare(this, target, spellTarget);
  315. std::set<const battle::Unit *> unitTargets = collectTargets();
  316. //process them
  317. for(const auto * unit : unitTargets)
  318. filterUnit(unit);
  319. //and update targets
  320. for(auto & p : effectsToApply)
  321. {
  322. vstd::erase_if(p.second, [&](const Destination & d)
  323. {
  324. if(!d.unitValue)
  325. return false;
  326. return vstd::contains(resisted, d.unitValue);
  327. });
  328. }
  329. if(mode == Mode::MAGIC_MIRROR)
  330. {
  331. if(caster->getHeroCaster() == nullptr)
  332. {
  333. sc.reflectedCres.insert(caster->getCasterUnitId());
  334. }
  335. }
  336. for(const auto * unit : resisted)
  337. sc.resistedCres.insert(unit->unitId());
  338. }
  339. void BattleSpellMechanics::castEval(ServerCallback * server, const Target & target)
  340. {
  341. affectedUnits.clear();
  342. //TODO: evaluate caster updates (mana usage etc.)
  343. //TODO: evaluate random values
  344. Target spellTarget = transformSpellTarget(target);
  345. effectsToApply = effects->prepare(this, target, spellTarget);
  346. std::set<const battle::Unit *> unitTargets = collectTargets();
  347. auto selector = std::bind(&BattleSpellMechanics::counteringSelector, this, _1);
  348. std::copy(std::begin(unitTargets), std::end(unitTargets), std::back_inserter(affectedUnits));
  349. doRemoveEffects(server, affectedUnits, selector);
  350. for(auto & p : effectsToApply)
  351. p.first->apply(server, this, p.second);
  352. }
  353. std::set<const battle::Unit *> BattleSpellMechanics::collectTargets() const
  354. {
  355. std::set<const battle::Unit *> result;
  356. for(const auto & p : effectsToApply)
  357. {
  358. for(const Destination & d : p.second)
  359. if(d.unitValue)
  360. result.insert(d.unitValue);
  361. }
  362. return result;
  363. }
  364. void BattleSpellMechanics::doRemoveEffects(ServerCallback * server, const std::vector<const battle::Unit *> & targets, const CSelector & selector)
  365. {
  366. SetStackEffect sse;
  367. sse.battleID = battle()->getBattle()->getBattleID();
  368. for(const auto * unit : targets)
  369. {
  370. std::vector<Bonus> buffer;
  371. auto bl = unit->getBonuses(selector);
  372. for(const auto & item : *bl)
  373. buffer.emplace_back(*item);
  374. if(!buffer.empty())
  375. sse.toRemove.emplace_back(unit->unitId(), buffer);
  376. }
  377. if(!sse.toRemove.empty())
  378. server->apply(&sse);
  379. }
  380. bool BattleSpellMechanics::counteringSelector(const Bonus * bonus) const
  381. {
  382. if(bonus->source != BonusSource::SPELL_EFFECT)
  383. return false;
  384. for(const SpellID & id : owner->counteredSpells)
  385. {
  386. if(bonus->sid == id.toEnum())
  387. return true;
  388. }
  389. return false;
  390. }
  391. std::set<BattleHex> BattleSpellMechanics::spellRangeInHexes(BattleHex centralHex) const
  392. {
  393. using namespace SRSLPraserHelpers;
  394. std::set<BattleHex> ret;
  395. std::string rng = owner->getLevelInfo(getRangeLevel()).range + ','; //copy + artificial comma for easier handling
  396. if(rng.size() >= 2 && rng[0] != 'X') //there is at least one hex in range (+artificial comma)
  397. {
  398. std::string number1;
  399. std::string number2;
  400. int beg = 0;
  401. int end = 0;
  402. bool readingFirst = true;
  403. for(auto & elem : rng)
  404. {
  405. if(std::isdigit(elem) ) //reading number
  406. {
  407. if(readingFirst)
  408. number1 += elem;
  409. else
  410. number2 += elem;
  411. }
  412. else if(elem == ',') //comma
  413. {
  414. //calculating variables
  415. if(readingFirst)
  416. {
  417. beg = std::stoi(number1);
  418. number1 = "";
  419. }
  420. else
  421. {
  422. end = std::stoi(number2);
  423. number2 = "";
  424. }
  425. //obtaining new hexes
  426. std::set<ui16> curLayer;
  427. if(readingFirst)
  428. {
  429. curLayer = getInRange(centralHex, beg, beg);
  430. }
  431. else
  432. {
  433. curLayer = getInRange(centralHex, beg, end);
  434. readingFirst = true;
  435. }
  436. //adding obtained hexes
  437. for(const auto & curLayer_it : curLayer)
  438. {
  439. ret.insert(curLayer_it);
  440. }
  441. }
  442. else if(elem == '-') //dash
  443. {
  444. beg = std::stoi(number1);
  445. number1 = "";
  446. readingFirst = false;
  447. }
  448. }
  449. }
  450. return ret;
  451. }
  452. Target BattleSpellMechanics::transformSpellTarget(const Target & aimPoint) const
  453. {
  454. Target spellTarget;
  455. if(aimPoint.empty())
  456. {
  457. logGlobal->error("Aimed spell cast with no destination.");
  458. }
  459. else
  460. {
  461. const Destination & primary = aimPoint.at(0);
  462. BattleHex aimPointHex = primary.hexValue;
  463. //transform primary spell target with spell range (if it`s valid), leave anything else to effects
  464. if(aimPointHex.isValid())
  465. {
  466. auto spellRange = spellRangeInHexes(aimPointHex);
  467. for(const auto & hex : spellRange)
  468. spellTarget.push_back(Destination(hex));
  469. }
  470. }
  471. if(spellTarget.empty())
  472. spellTarget.push_back(Destination(BattleHex::INVALID));
  473. return spellTarget;
  474. }
  475. std::vector<AimType> BattleSpellMechanics::getTargetTypes() const
  476. {
  477. auto ret = BaseMechanics::getTargetTypes();
  478. if(!ret.empty())
  479. {
  480. effects->forEachEffect(getEffectLevel(), [&](const effects::Effect * e, bool & stop)
  481. {
  482. e->adjustTargetTypes(ret);
  483. stop = ret.empty();
  484. });
  485. }
  486. return ret;
  487. }
  488. std::vector<Destination> BattleSpellMechanics::getPossibleDestinations(size_t index, AimType aimType, const Target & current, bool fast) const
  489. {
  490. //TODO: BattleSpellMechanics::getPossibleDestinations
  491. if(index != 0)
  492. return std::vector<Destination>();
  493. std::vector<Destination> ret;
  494. switch(aimType)
  495. {
  496. case AimType::CREATURE:
  497. {
  498. auto stacks = battle()->battleGetAllStacks();
  499. for(auto stack : stacks)
  500. {
  501. Target tmp = current;
  502. tmp.emplace_back(stack->getPosition());
  503. detail::ProblemImpl ignored;
  504. if(canBeCastAt(tmp, ignored))
  505. ret.emplace_back(stack->getPosition());
  506. }
  507. break;
  508. }
  509. case AimType::LOCATION:
  510. if(fast)
  511. {
  512. auto stacks = battle()->battleGetAllStacks();
  513. std::set<BattleHex> hexesToCheck;
  514. for(auto stack : stacks)
  515. {
  516. hexesToCheck.insert(stack->getPosition());
  517. for(auto adjacent : stack->getPosition().neighbouringTiles())
  518. hexesToCheck.insert(adjacent);
  519. }
  520. for(auto hex : hexesToCheck)
  521. {
  522. if(hex.isAvailable())
  523. {
  524. Target tmp = current;
  525. tmp.emplace_back(hex);
  526. detail::ProblemImpl ignored;
  527. if(canBeCastAt(tmp, ignored))
  528. ret.emplace_back(hex);
  529. }
  530. }
  531. }
  532. else
  533. {
  534. for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
  535. {
  536. BattleHex dest(i);
  537. if(dest.isAvailable())
  538. {
  539. Target tmp = current;
  540. tmp.emplace_back(dest);
  541. detail::ProblemImpl ignored;
  542. if(canBeCastAt(tmp, ignored))
  543. ret.emplace_back(dest);
  544. }
  545. }
  546. }
  547. break;
  548. case AimType::NO_TARGET:
  549. ret.emplace_back();
  550. break;
  551. default:
  552. break;
  553. }
  554. return ret;
  555. }
  556. bool BattleSpellMechanics::isReceptive(const battle::Unit * target) const
  557. {
  558. return targetCondition->isReceptive(this, target);
  559. }
  560. std::vector<BattleHex> BattleSpellMechanics::rangeInHexes(BattleHex centralHex) const
  561. {
  562. if(isMassive() || !centralHex.isValid())
  563. return std::vector<BattleHex>(1, BattleHex::INVALID);
  564. Target aimPoint;
  565. aimPoint.push_back(Destination(centralHex));
  566. Target spellTarget = transformSpellTarget(aimPoint);
  567. std::set<BattleHex> effectRange;
  568. effects->forEachEffect(getEffectLevel(), [&](const effects::Effect * effect, bool & stop)
  569. {
  570. if(!effect->indirect)
  571. {
  572. effect->adjustAffectedHexes(effectRange, this, spellTarget);
  573. }
  574. });
  575. std::vector<BattleHex> ret;
  576. ret.reserve(effectRange.size());
  577. std::copy(effectRange.begin(), effectRange.end(), std::back_inserter(ret));
  578. return ret;
  579. }
  580. const Spell * BattleSpellMechanics::getSpell() const
  581. {
  582. return owner;
  583. }
  584. }
  585. VCMI_LIB_NAMESPACE_END