BattleSpellMechanics.cpp 14 KB

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