ISpellMechanics.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /*
  2. * ISpellMechanics.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 "ISpellMechanics.h"
  12. #include "../CRandomGenerator.h"
  13. #include "../VCMI_Lib.h"
  14. #include "../HeroBonus.h"
  15. #include "../battle/CBattleInfoCallback.h"
  16. #include "../battle/IBattleState.h"
  17. #include "../battle/Unit.h"
  18. #include "../mapObjects/CGHeroInstance.h"
  19. #include "../serializer/JsonDeserializer.h"
  20. #include "../serializer/JsonSerializer.h"
  21. #include "TargetCondition.h"
  22. #include "Problem.h"
  23. #include "AdventureSpellMechanics.h"
  24. #include "BattleSpellMechanics.h"
  25. #include "effects/Effects.h"
  26. #include "effects/Registry.h"
  27. #include "effects/Damage.h"
  28. #include "effects/Timed.h"
  29. #include "CSpellHandler.h"
  30. #include "../NetPacks.h"
  31. #include "../CHeroHandler.h"//todo: remove
  32. #include "../IGameCallback.h"//todo: remove
  33. #include "../BattleFieldHandler.h"
  34. VCMI_LIB_NAMESPACE_BEGIN
  35. namespace spells
  36. {
  37. static std::shared_ptr<TargetCondition> makeCondition(const CSpell * s)
  38. {
  39. std::shared_ptr<TargetCondition> res = std::make_shared<TargetCondition>();
  40. JsonDeserializer deser(nullptr, s->targetCondition);
  41. res->serializeJson(deser, TargetConditionItemFactory::getDefault());
  42. return res;
  43. }
  44. class CustomMechanicsFactory : public ISpellMechanicsFactory
  45. {
  46. public:
  47. std::unique_ptr<Mechanics> create(const IBattleCast * event) const override
  48. {
  49. BattleSpellMechanics * ret = new BattleSpellMechanics(event, effects, targetCondition);
  50. return std::unique_ptr<Mechanics>(ret);
  51. }
  52. protected:
  53. std::shared_ptr<effects::Effects> effects;
  54. CustomMechanicsFactory(const CSpell * s)
  55. : ISpellMechanicsFactory(s), effects(new effects::Effects)
  56. {
  57. targetCondition = makeCondition(s);
  58. }
  59. void loadEffects(const JsonNode & config, const int level)
  60. {
  61. JsonDeserializer deser(nullptr, config);
  62. effects->serializeJson(VLC->spellEffects(), deser, level);
  63. }
  64. private:
  65. std::shared_ptr<IReceptiveCheck> targetCondition;
  66. };
  67. class ConfigurableMechanicsFactory : public CustomMechanicsFactory
  68. {
  69. public:
  70. ConfigurableMechanicsFactory(const CSpell * s)
  71. : CustomMechanicsFactory(s)
  72. {
  73. for(int level = 0; level < GameConstants::SPELL_SCHOOL_LEVELS; level++)
  74. loadEffects(s->getLevelInfo(level).battleEffects, level);
  75. }
  76. };
  77. //to be used for spells configured with old format
  78. class FallbackMechanicsFactory : public CustomMechanicsFactory
  79. {
  80. public:
  81. FallbackMechanicsFactory(const CSpell * s)
  82. : CustomMechanicsFactory(s)
  83. {
  84. for(int level = 0; level < GameConstants::SPELL_SCHOOL_LEVELS; level++)
  85. {
  86. const CSpell::LevelInfo & levelInfo = s->getLevelInfo(level);
  87. assert(levelInfo.battleEffects.isNull());
  88. if(s->isOffensive())
  89. {
  90. //default constructed object should be enough
  91. effects->add("directDamage", std::make_shared<effects::Damage>(), level);
  92. }
  93. std::shared_ptr<effects::Effect> effect;
  94. if(!levelInfo.effects.empty())
  95. {
  96. auto timed = new effects::Timed();
  97. timed->cumulative = false;
  98. timed->bonus = levelInfo.effects;
  99. effect.reset(timed);
  100. }
  101. if(!levelInfo.cumulativeEffects.empty())
  102. {
  103. auto timed = new effects::Timed();
  104. timed->cumulative = true;
  105. timed->bonus = levelInfo.cumulativeEffects;
  106. effect.reset(timed);
  107. }
  108. if(effect)
  109. effects->add("timed", effect, level);
  110. }
  111. }
  112. };
  113. BattleCast::BattleCast(const CBattleInfoCallback * cb_, const Caster * caster_, const Mode mode_, const CSpell * spell_)
  114. : spell(spell_),
  115. cb(cb_),
  116. caster(caster_),
  117. mode(mode_),
  118. magicSkillLevel(),
  119. effectPower(),
  120. effectDuration(),
  121. effectValue(),
  122. smart(boost::logic::indeterminate),
  123. massive(boost::logic::indeterminate)
  124. {
  125. gameCb = IObjectInterface::cb; //FIXME: pass player callback (problem is that BattleAI do not have one)
  126. }
  127. BattleCast::BattleCast(const BattleCast & orig, const Caster * caster_)
  128. : spell(orig.spell),
  129. cb(orig.cb),
  130. gameCb(orig.gameCb),
  131. caster(caster_),
  132. mode(Mode::MAGIC_MIRROR),
  133. magicSkillLevel(orig.magicSkillLevel),
  134. effectPower(orig.effectPower),
  135. effectDuration(orig.effectDuration),
  136. effectValue(orig.effectValue),
  137. smart(true),
  138. massive(false)
  139. {
  140. }
  141. BattleCast::~BattleCast() = default;
  142. const CSpell * BattleCast::getSpell() const
  143. {
  144. return spell;
  145. }
  146. Mode BattleCast::getMode() const
  147. {
  148. return mode;
  149. }
  150. const Caster * BattleCast::getCaster() const
  151. {
  152. return caster;
  153. }
  154. const CBattleInfoCallback * BattleCast::getBattle() const
  155. {
  156. return cb;
  157. }
  158. const IGameInfoCallback * BattleCast::getGame() const
  159. {
  160. return gameCb;
  161. }
  162. BattleCast::OptionalValue BattleCast::getSpellLevel() const
  163. {
  164. return magicSkillLevel;
  165. }
  166. BattleCast::OptionalValue BattleCast::getEffectPower() const
  167. {
  168. return effectPower;
  169. }
  170. BattleCast::OptionalValue BattleCast::getEffectDuration() const
  171. {
  172. return effectDuration;
  173. }
  174. BattleCast::OptionalValue64 BattleCast::getEffectValue() const
  175. {
  176. return effectValue;
  177. }
  178. boost::logic::tribool BattleCast::isSmart() const
  179. {
  180. return smart;
  181. }
  182. boost::logic::tribool BattleCast::isMassive() const
  183. {
  184. return massive;
  185. }
  186. void BattleCast::setSpellLevel(BattleCast::Value value)
  187. {
  188. magicSkillLevel = boost::make_optional(value);
  189. }
  190. void BattleCast::setEffectPower(BattleCast::Value value)
  191. {
  192. effectPower = boost::make_optional(value);
  193. }
  194. void BattleCast::setEffectDuration(BattleCast::Value value)
  195. {
  196. effectDuration = boost::make_optional(value);
  197. }
  198. void BattleCast::setEffectValue(BattleCast::Value64 value)
  199. {
  200. effectValue = boost::make_optional(value);
  201. }
  202. void BattleCast::applyEffects(ServerCallback * server, Target target, bool indirect, bool ignoreImmunity) const
  203. {
  204. auto m = spell->battleMechanics(this);
  205. m->applyEffects(server, target, indirect, ignoreImmunity);
  206. }
  207. void BattleCast::cast(ServerCallback * server, Target target)
  208. {
  209. if(target.empty())
  210. target.emplace_back();
  211. auto m = spell->battleMechanics(this);
  212. const battle::Unit * mainTarget = nullptr;
  213. if(target.front().unitValue)
  214. {
  215. mainTarget = target.front().unitValue;
  216. }
  217. else if(target.front().hexValue.isValid())
  218. {
  219. mainTarget = cb->battleGetUnitByPos(target.front().hexValue, true);
  220. }
  221. bool tryMagicMirror = (mainTarget != nullptr) && (mode == Mode::HERO || mode == Mode::CREATURE_ACTIVE);//TODO: recheck
  222. tryMagicMirror = tryMagicMirror && (mainTarget->unitOwner() != caster->getCasterOwner()) && !spell->isPositive();//TODO: recheck
  223. m->cast(server, target);
  224. //Magic Mirror effect
  225. if(tryMagicMirror)
  226. {
  227. const std::string magicMirrorCacheStr = "type_MAGIC_MIRROR";
  228. static const auto magicMirrorSelector = Selector::type()(Bonus::MAGIC_MIRROR);
  229. auto rangeGen = server->getRNG()->getInt64Range(0, 99);
  230. const int mirrorChance = mainTarget->valOfBonuses(magicMirrorSelector, magicMirrorCacheStr);
  231. if(rangeGen() < mirrorChance)
  232. {
  233. auto mirrorTargets = cb->battleGetUnitsIf([this](const battle::Unit * unit)
  234. {
  235. //Get all caster stacks. Magic mirror can reflect to immune creature (with no effect)
  236. return unit->unitOwner() == caster->getCasterOwner() && unit->isValidTarget(true);
  237. });
  238. if(!mirrorTargets.empty())
  239. {
  240. auto mirrorDestination = (*RandomGeneratorUtil::nextItem(mirrorTargets, *server->getRNG()));
  241. Target mirrorTarget;
  242. mirrorTarget.emplace_back(mirrorDestination);
  243. BattleCast mirror(*this, mainTarget);
  244. mirror.cast(server, mirrorTarget);
  245. }
  246. }
  247. }
  248. }
  249. void BattleCast::castEval(ServerCallback * server, Target target)
  250. {
  251. //TODO: make equivalent to normal cast
  252. if(target.empty())
  253. target.emplace_back();
  254. auto m = spell->battleMechanics(this);
  255. //TODO: reflection
  256. //TODO: random effects evaluation
  257. m->castEval(server, target);
  258. }
  259. bool BattleCast::castIfPossible(ServerCallback * server, Target target)
  260. {
  261. if(spell->canBeCast(cb, mode, caster))
  262. {
  263. cast(server, target);
  264. return true;
  265. }
  266. return false;
  267. }
  268. std::vector<Target> BattleCast::findPotentialTargets() const
  269. {
  270. //TODO: for more than 2 destinations per target much more efficient algorithm is required
  271. auto m = spell->battleMechanics(this);
  272. auto targetTypes = m->getTargetTypes();
  273. if(targetTypes.empty() || targetTypes.size() > 2)
  274. {
  275. return std::vector<Target>();
  276. }
  277. else
  278. {
  279. std::vector<Target> previous;
  280. std::vector<Target> next;
  281. for(size_t index = 0; index < targetTypes.size(); index++)
  282. {
  283. std::swap(previous, next);
  284. next.clear();
  285. std::vector<Destination> destinations;
  286. if(previous.empty())
  287. {
  288. Target empty;
  289. destinations = m->getPossibleDestinations(index, targetTypes.at(index), empty);
  290. for(auto & destination : destinations)
  291. {
  292. Target target;
  293. target.emplace_back(destination);
  294. next.push_back(target);
  295. }
  296. }
  297. else
  298. {
  299. for(const Target & current : previous)
  300. {
  301. destinations = m->getPossibleDestinations(index, targetTypes.at(index), current);
  302. for(auto & destination : destinations)
  303. {
  304. Target target = current;
  305. target.emplace_back(destination);
  306. next.push_back(target);
  307. }
  308. }
  309. }
  310. if(next.empty())
  311. break;
  312. }
  313. return next;
  314. }
  315. }
  316. ///ISpellMechanicsFactory
  317. ISpellMechanicsFactory::ISpellMechanicsFactory(const CSpell * s)
  318. : spell(s)
  319. {
  320. }
  321. ISpellMechanicsFactory::~ISpellMechanicsFactory()
  322. {
  323. }
  324. std::unique_ptr<ISpellMechanicsFactory> ISpellMechanicsFactory::get(const CSpell * s)
  325. {
  326. if(s->hasBattleEffects())
  327. return make_unique<ConfigurableMechanicsFactory>(s);
  328. else
  329. return make_unique<FallbackMechanicsFactory>(s);
  330. }
  331. ///Mechanics
  332. Mechanics::Mechanics()
  333. : caster(nullptr),
  334. casterSide(0)
  335. {
  336. }
  337. Mechanics::~Mechanics() = default;
  338. BaseMechanics::BaseMechanics(const IBattleCast * event)
  339. : Mechanics(),
  340. owner(event->getSpell()),
  341. mode(event->getMode()),
  342. smart(event->isSmart()),
  343. massive(event->isMassive())
  344. {
  345. cb = event->getBattle();
  346. gameCb = event->getGame();
  347. caster = event->getCaster();
  348. //FIXME: do not crash on invalid side
  349. casterSide = cb->playerToSide(caster->getCasterOwner()).get();
  350. {
  351. auto value = event->getSpellLevel();
  352. if(value)
  353. rangeLevel = value.get();
  354. else
  355. rangeLevel = caster->getSpellSchoolLevel(owner);
  356. vstd::abetween(rangeLevel, 0, 3);
  357. }
  358. {
  359. auto value = event->getSpellLevel();
  360. if(value)
  361. effectLevel = value.get();
  362. else
  363. effectLevel = caster->getEffectLevel(owner);
  364. vstd::abetween(effectLevel, 0, 3);
  365. }
  366. {
  367. auto value = event->getEffectPower();
  368. if(value)
  369. effectPower = value.get();
  370. else
  371. effectPower = caster->getEffectPower(owner);
  372. vstd::amax(effectPower, 0);
  373. }
  374. {
  375. auto value = event->getEffectDuration();
  376. if(value)
  377. effectDuration = value.get();
  378. else
  379. effectDuration = caster->getEnchantPower(owner);
  380. vstd::amax(effectDuration, 0); //???
  381. }
  382. {
  383. auto value = event->getEffectValue();
  384. if(value)
  385. {
  386. effectValue = value.get();
  387. }
  388. else
  389. {
  390. auto casterValue = caster->getEffectValue(owner);
  391. if(casterValue == 0)
  392. effectValue = owner->calculateRawEffectValue(effectLevel, effectPower, 1);
  393. else
  394. effectValue = casterValue;
  395. }
  396. vstd::amax(effectValue, 0);
  397. }
  398. }
  399. BaseMechanics::~BaseMechanics() = default;
  400. bool BaseMechanics::adaptGenericProblem(Problem & target) const
  401. {
  402. MetaString text;
  403. // %s recites the incantations but they seem to have no effect.
  404. text.addTxt(MetaString::GENERAL_TXT, 541);
  405. assert(caster);
  406. caster->getCasterName(text);
  407. target.add(std::move(text), spells::Problem::NORMAL);
  408. return false;
  409. }
  410. bool BaseMechanics::adaptProblem(ESpellCastProblem::ESpellCastProblem source, Problem & target) const
  411. {
  412. if(source == ESpellCastProblem::OK)
  413. return true;
  414. switch(source)
  415. {
  416. case ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED:
  417. {
  418. MetaString text;
  419. //TODO: refactor
  420. auto hero = dynamic_cast<const CGHeroInstance *>(caster);
  421. if(!hero)
  422. return adaptGenericProblem(target);
  423. //Recanter's Cloak or similar effect. Try to retrieve bonus
  424. const auto b = hero->getBonusLocalFirst(Selector::type()(Bonus::BLOCK_MAGIC_ABOVE));
  425. //TODO what about other values and non-artifact sources?
  426. if(b && b->val == 2 && b->source == Bonus::ARTIFACT)
  427. {
  428. //The %s prevents %s from casting 3rd level or higher spells.
  429. text.addTxt(MetaString::GENERAL_TXT, 536);
  430. text.addReplacement(MetaString::ART_NAMES, b->sid);
  431. caster->getCasterName(text);
  432. target.add(std::move(text), spells::Problem::NORMAL);
  433. }
  434. else if(b && b->source == Bonus::TERRAIN_OVERLAY && VLC->battlefields()->getByIndex(b->sid)->identifier == "cursed_ground")
  435. {
  436. text.addTxt(MetaString::GENERAL_TXT, 537);
  437. target.add(std::move(text), spells::Problem::NORMAL);
  438. }
  439. else
  440. {
  441. return adaptGenericProblem(target);
  442. }
  443. }
  444. break;
  445. case ESpellCastProblem::WRONG_SPELL_TARGET:
  446. case ESpellCastProblem::STACK_IMMUNE_TO_SPELL:
  447. case ESpellCastProblem::NO_APPROPRIATE_TARGET:
  448. {
  449. MetaString text;
  450. text.addTxt(MetaString::GENERAL_TXT, 185);
  451. target.add(std::move(text), spells::Problem::NORMAL);
  452. }
  453. break;
  454. case ESpellCastProblem::INVALID:
  455. {
  456. MetaString text;
  457. text.addReplacement("Internal error during check of spell cast.");
  458. target.add(std::move(text), spells::Problem::CRITICAL);
  459. }
  460. break;
  461. default:
  462. return adaptGenericProblem(target);
  463. }
  464. return false;
  465. }
  466. int32_t BaseMechanics::getSpellIndex() const
  467. {
  468. return getSpellId().toEnum();
  469. }
  470. SpellID BaseMechanics::getSpellId() const
  471. {
  472. return owner->getId();
  473. }
  474. std::string BaseMechanics::getSpellName() const
  475. {
  476. return owner->getName();
  477. }
  478. int32_t BaseMechanics::getSpellLevel() const
  479. {
  480. return owner->getLevel();
  481. }
  482. bool BaseMechanics::isSmart() const
  483. {
  484. if(boost::logic::indeterminate(smart))
  485. {
  486. const CSpell::TargetInfo targetInfo(owner, getRangeLevel(), mode);
  487. return targetInfo.smart;
  488. }
  489. else
  490. {
  491. return (bool)smart;
  492. }
  493. }
  494. bool BaseMechanics::isMassive() const
  495. {
  496. if(boost::logic::indeterminate(massive))
  497. {
  498. const CSpell::TargetInfo targetInfo(owner, getRangeLevel(), mode);
  499. return targetInfo.massive;
  500. }
  501. else
  502. {
  503. return (bool)massive;
  504. }
  505. }
  506. bool BaseMechanics::requiresClearTiles() const
  507. {
  508. const CSpell::TargetInfo targetInfo(owner, getRangeLevel(), mode);
  509. return targetInfo.clearAffected;
  510. }
  511. bool BaseMechanics::alwaysHitFirstTarget() const
  512. {
  513. return mode == Mode::SPELL_LIKE_ATTACK;
  514. }
  515. bool BaseMechanics::isNegativeSpell() const
  516. {
  517. return owner->isNegative();
  518. }
  519. bool BaseMechanics::isPositiveSpell() const
  520. {
  521. return owner->isPositive();
  522. }
  523. int64_t BaseMechanics::adjustEffectValue(const battle::Unit * target) const
  524. {
  525. return owner->adjustRawDamage(caster, target, getEffectValue());
  526. }
  527. int64_t BaseMechanics::applySpellBonus(int64_t value, const battle::Unit * target) const
  528. {
  529. return caster->getSpellBonus(owner, value, target);
  530. }
  531. int64_t BaseMechanics::applySpecificSpellBonus(int64_t value) const
  532. {
  533. return caster->getSpecificSpellBonus(owner, value);
  534. }
  535. int64_t BaseMechanics::calculateRawEffectValue(int32_t basePowerMultiplier, int32_t levelPowerMultiplier) const
  536. {
  537. return owner->calculateRawEffectValue(getEffectLevel(), basePowerMultiplier, levelPowerMultiplier);
  538. }
  539. std::vector<Bonus::BonusType> BaseMechanics::getElementalImmunity() const
  540. {
  541. std::vector<Bonus::BonusType> ret;
  542. owner->forEachSchool([&](const SchoolInfo & cnf, bool & stop)
  543. {
  544. ret.push_back(cnf.immunityBonus);
  545. });
  546. return ret;
  547. }
  548. bool BaseMechanics::ownerMatches(const battle::Unit * unit) const
  549. {
  550. return ownerMatches(unit, owner->getPositiveness());
  551. }
  552. bool BaseMechanics::ownerMatches(const battle::Unit * unit, const boost::logic::tribool positivness) const
  553. {
  554. return cb->battleMatchOwner(caster->getCasterOwner(), unit, positivness);
  555. }
  556. IBattleCast::Value BaseMechanics::getEffectLevel() const
  557. {
  558. return effectLevel;
  559. }
  560. IBattleCast::Value BaseMechanics::getRangeLevel() const
  561. {
  562. return rangeLevel;
  563. }
  564. IBattleCast::Value BaseMechanics::getEffectPower() const
  565. {
  566. return effectPower;
  567. }
  568. IBattleCast::Value BaseMechanics::getEffectDuration() const
  569. {
  570. return effectDuration;
  571. }
  572. IBattleCast::Value64 BaseMechanics::getEffectValue() const
  573. {
  574. return effectValue;
  575. }
  576. PlayerColor BaseMechanics::getCasterColor() const
  577. {
  578. return caster->getCasterOwner();
  579. }
  580. std::vector<AimType> BaseMechanics::getTargetTypes() const
  581. {
  582. std::vector<AimType> ret;
  583. detail::ProblemImpl ingored;
  584. if(canBeCast(ingored))
  585. {
  586. auto spellTargetType = owner->getTargetType();
  587. if(isMassive())
  588. spellTargetType = AimType::NO_TARGET;
  589. else if(spellTargetType == AimType::OBSTACLE)
  590. spellTargetType = AimType::LOCATION;
  591. ret.push_back(spellTargetType);
  592. }
  593. return ret;
  594. }
  595. const CreatureService * BaseMechanics::creatures() const
  596. {
  597. return VLC->creatures(); //todo: redirect
  598. }
  599. #if SCRIPTING_ENABLED
  600. const scripting::Service * BaseMechanics::scripts() const
  601. {
  602. return VLC->scripts(); //todo: redirect
  603. }
  604. #endif
  605. const Service * BaseMechanics::spells() const
  606. {
  607. return VLC->spells(); //todo: redirect
  608. }
  609. const IGameInfoCallback * BaseMechanics::game() const
  610. {
  611. return gameCb;
  612. }
  613. const CBattleInfoCallback * BaseMechanics::battle() const
  614. {
  615. return cb;
  616. }
  617. } //namespace spells
  618. ///IAdventureSpellMechanics
  619. IAdventureSpellMechanics::IAdventureSpellMechanics(const CSpell * s)
  620. : owner(s)
  621. {
  622. }
  623. std::unique_ptr<IAdventureSpellMechanics> IAdventureSpellMechanics::createMechanics(const CSpell * s)
  624. {
  625. switch (s->id)
  626. {
  627. case SpellID::SUMMON_BOAT:
  628. return make_unique<SummonBoatMechanics>(s);
  629. case SpellID::SCUTTLE_BOAT:
  630. return make_unique<ScuttleBoatMechanics>(s);
  631. case SpellID::DIMENSION_DOOR:
  632. return make_unique<DimensionDoorMechanics>(s);
  633. case SpellID::FLY:
  634. case SpellID::WATER_WALK:
  635. case SpellID::VISIONS:
  636. case SpellID::DISGUISE:
  637. return make_unique<AdventureSpellMechanics>(s); //implemented using bonus system
  638. case SpellID::TOWN_PORTAL:
  639. return make_unique<TownPortalMechanics>(s);
  640. case SpellID::VIEW_EARTH:
  641. return make_unique<ViewEarthMechanics>(s);
  642. case SpellID::VIEW_AIR:
  643. return make_unique<ViewAirMechanics>(s);
  644. default:
  645. return s->combat ? std::unique_ptr<IAdventureSpellMechanics>() : make_unique<AdventureSpellMechanics>(s);
  646. }
  647. }
  648. VCMI_LIB_NAMESPACE_END