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