TargetCondition.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * TargetCondition.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 "TargetCondition.h"
  12. #include "../GameConstants.h"
  13. #include "../CBonusTypeHandler.h"
  14. #include "../battle/CBattleInfoCallback.h"
  15. #include "../battle/Unit.h"
  16. #include "../serializer/JsonSerializeFormat.h"
  17. #include "../VCMI_Lib.h"
  18. #include "../CModHandler.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. namespace spells
  21. {
  22. class TargetConditionItemBase : public TargetConditionItem
  23. {
  24. public:
  25. bool inverted = false;
  26. bool exclusive = false;
  27. void setInverted(bool value) override
  28. {
  29. inverted = value;
  30. }
  31. void setExclusive(bool value) override
  32. {
  33. exclusive = value;
  34. }
  35. bool isExclusive() const override
  36. {
  37. return exclusive;
  38. }
  39. bool isReceptive(const Mechanics * m, const battle::Unit * target) const override
  40. {
  41. bool result = check(m, target);
  42. return inverted != result;
  43. }
  44. protected:
  45. virtual bool check(const Mechanics * m, const battle::Unit * target) const = 0;
  46. };
  47. class BonusCondition : public TargetConditionItemBase
  48. {
  49. public:
  50. BonusCondition(BonusTypeID type_)
  51. : type(type_)
  52. {
  53. }
  54. protected:
  55. bool check(const Mechanics * m, const battle::Unit * target) const override
  56. {
  57. return target->hasBonus(Selector::type()(type));
  58. }
  59. private:
  60. BonusTypeID type;
  61. };
  62. class CreatureCondition : public TargetConditionItemBase
  63. {
  64. public:
  65. CreatureCondition(const CreatureID & type_): type(type_) {}
  66. protected:
  67. bool check(const Mechanics * m, const battle::Unit * target) const override
  68. {
  69. return target->creatureId() == type;
  70. }
  71. private:
  72. CreatureID type;
  73. };
  74. class AbsoluteLevelCondition : public TargetConditionItemBase
  75. {
  76. public:
  77. AbsoluteLevelCondition()
  78. {
  79. inverted = false;
  80. exclusive = true;
  81. }
  82. protected:
  83. bool check(const Mechanics * m, const battle::Unit * target) const override
  84. {
  85. std::stringstream cachingStr;
  86. cachingStr << "type_" << Bonus::LEVEL_SPELL_IMMUNITY << "addInfo_1";
  87. TConstBonusListPtr levelImmunities = target->getBonuses(Selector::type()(Bonus::LEVEL_SPELL_IMMUNITY).And(Selector::info()(1)), cachingStr.str());
  88. return levelImmunities->size() == 0 ||
  89. levelImmunities->totalValue() < m->getSpellLevel() ||
  90. m->getSpellLevel() <= 0;
  91. }
  92. };
  93. class AbsoluteSpellCondition : public TargetConditionItemBase
  94. {
  95. public:
  96. AbsoluteSpellCondition()
  97. {
  98. inverted = false;
  99. exclusive = true;
  100. }
  101. protected:
  102. bool check(const Mechanics * m, const battle::Unit * target) const override
  103. {
  104. std::stringstream cachingStr;
  105. cachingStr << "type_" << Bonus::SPELL_IMMUNITY << "subtype_" << m->getSpellIndex() << "addInfo_1";
  106. return !target->hasBonus(Selector::typeSubtypeInfo(Bonus::SPELL_IMMUNITY, m->getSpellIndex(), 1), cachingStr.str());
  107. }
  108. };
  109. class ElementalCondition : public TargetConditionItemBase
  110. {
  111. public:
  112. ElementalCondition()
  113. {
  114. inverted = true;
  115. exclusive = true;
  116. }
  117. protected:
  118. bool check(const Mechanics * m, const battle::Unit * target) const override
  119. {
  120. bool elementalImmune = false;
  121. auto filter = m->getElementalImmunity();
  122. for(auto element : filter)
  123. {
  124. if(target->hasBonusOfType(element, 0)) //always resist if immune to all spells altogether
  125. {
  126. elementalImmune = true;
  127. break;
  128. }
  129. else if(!m->isPositiveSpell()) //negative or indifferent
  130. {
  131. if(target->hasBonusOfType(element, 1))
  132. {
  133. elementalImmune = true;
  134. break;
  135. }
  136. }
  137. }
  138. return elementalImmune;
  139. }
  140. };
  141. class NormalLevelCondition : public TargetConditionItemBase
  142. {
  143. public:
  144. NormalLevelCondition()
  145. {
  146. inverted = false;
  147. exclusive = true;
  148. }
  149. protected:
  150. bool check(const Mechanics * m, const battle::Unit * target) const override
  151. {
  152. TConstBonusListPtr levelImmunities = target->getBonuses(Selector::type()(Bonus::LEVEL_SPELL_IMMUNITY));
  153. return levelImmunities->size() == 0 ||
  154. levelImmunities->totalValue() < m->getSpellLevel() ||
  155. m->getSpellLevel() <= 0;
  156. }
  157. };
  158. class NormalSpellCondition : public TargetConditionItemBase
  159. {
  160. public:
  161. NormalSpellCondition()
  162. {
  163. inverted = false;
  164. exclusive = true;
  165. }
  166. protected:
  167. bool check(const Mechanics * m, const battle::Unit * target) const override
  168. {
  169. return !target->hasBonusOfType(Bonus::SPELL_IMMUNITY, m->getSpellIndex());
  170. }
  171. };
  172. //for Hypnotize
  173. class HealthValueCondition : public TargetConditionItemBase
  174. {
  175. protected:
  176. bool check(const Mechanics * m, const battle::Unit * target) const override
  177. {
  178. //todo: maybe do not resist on passive cast
  179. //TODO: what with other creatures casting hypnotize, Faerie Dragons style?
  180. int64_t subjectHealth = target->getAvailableHealth();
  181. //apply 'damage' bonus for hypnotize, including hero specialty
  182. auto maxHealth = m->applySpellBonus(m->getEffectValue(), target);
  183. return subjectHealth <= maxHealth;
  184. }
  185. };
  186. class SpellEffectCondition : public TargetConditionItemBase
  187. {
  188. public:
  189. SpellEffectCondition(const SpellID & spellID_): spellID(spellID_)
  190. {
  191. std::stringstream builder;
  192. builder << "source_" << Bonus::SPELL_EFFECT << "id_" << spellID.num;
  193. cachingString = builder.str();
  194. selector = Selector::source(Bonus::SPELL_EFFECT, spellID.num);
  195. }
  196. protected:
  197. bool check(const Mechanics * m, const battle::Unit * target) const override
  198. {
  199. return target->hasBonus(selector, cachingString);
  200. }
  201. private:
  202. CSelector selector;
  203. std::string cachingString;
  204. SpellID spellID;
  205. };
  206. class ReceptiveFeatureCondition : public TargetConditionItemBase
  207. {
  208. protected:
  209. bool check(const Mechanics * m, const battle::Unit * target) const override
  210. {
  211. return m->isPositiveSpell() && target->hasBonus(selector, cachingString);
  212. }
  213. private:
  214. CSelector selector = Selector::type()(Bonus::RECEPTIVE);
  215. std::string cachingString = "type_RECEPTIVE";
  216. };
  217. class ImmunityNegationCondition : public TargetConditionItemBase
  218. {
  219. protected:
  220. bool check(const Mechanics * m, const battle::Unit * target) const override
  221. {
  222. const bool battleWideNegation = target->hasBonusOfType(Bonus::NEGATE_ALL_NATURAL_IMMUNITIES, 0);
  223. const bool heroNegation = target->hasBonusOfType(Bonus::NEGATE_ALL_NATURAL_IMMUNITIES, 1);
  224. //anyone can cast on artifact holder`s stacks
  225. if(heroNegation)
  226. {
  227. return true;
  228. }
  229. //this stack is from other player
  230. else if(battleWideNegation)
  231. {
  232. if(m->ownerMatches(target, false))
  233. return true;
  234. }
  235. return false;
  236. }
  237. };
  238. class DefaultTargetConditionItemFactory : public TargetConditionItemFactory
  239. {
  240. public:
  241. Object createAbsoluteLevel() const override
  242. {
  243. static std::shared_ptr<TargetConditionItem> antimagicCondition = std::make_shared<AbsoluteLevelCondition>();
  244. return antimagicCondition;
  245. }
  246. Object createAbsoluteSpell() const override
  247. {
  248. static std::shared_ptr<TargetConditionItem> alCondition = std::make_shared<AbsoluteSpellCondition>();
  249. return alCondition;
  250. }
  251. Object createElemental() const override
  252. {
  253. static std::shared_ptr<TargetConditionItem> elementalCondition = std::make_shared<ElementalCondition>();
  254. return elementalCondition;
  255. }
  256. Object createNormalLevel() const override
  257. {
  258. static std::shared_ptr<TargetConditionItem> nlCondition = std::make_shared<NormalLevelCondition>();
  259. return nlCondition;
  260. }
  261. Object createNormalSpell() const override
  262. {
  263. static std::shared_ptr<TargetConditionItem> nsCondition = std::make_shared<NormalSpellCondition>();
  264. return nsCondition;
  265. }
  266. Object createConfigurable(std::string scope, std::string type, std::string identifier) const override
  267. {
  268. if(type == "bonus")
  269. {
  270. //TODO: support custom bonus types
  271. auto it = bonusNameMap.find(identifier);
  272. if(it != bonusNameMap.end())
  273. return std::make_shared<BonusCondition>(it->second);
  274. else
  275. logMod->error("Invalid bonus type %s in spell target condition.", identifier);
  276. }
  277. else if(type == "creature")
  278. {
  279. auto rawId = VLC->modh->identifiers.getIdentifier(scope, type, identifier, true);
  280. if(rawId)
  281. return std::make_shared<CreatureCondition>(CreatureID(rawId.get()));
  282. else
  283. logMod->error("Invalid creature %s type in spell target condition.", identifier);
  284. }
  285. else if(type == "spell")
  286. {
  287. auto rawId = VLC->modh->identifiers.getIdentifier(scope, type, identifier, true);
  288. if(rawId)
  289. return std::make_shared<SpellEffectCondition>(SpellID(rawId.get()));
  290. else
  291. logMod->error("Invalid spell %s in spell target condition.", identifier);
  292. }
  293. else if(type == "healthValueSpecial")
  294. {
  295. return std::make_shared<HealthValueCondition>();
  296. }
  297. else
  298. {
  299. logMod->error("Invalid type %s in spell target condition.", type);
  300. }
  301. return Object();
  302. }
  303. Object createReceptiveFeature() const override
  304. {
  305. static std::shared_ptr<TargetConditionItem> condition = std::make_shared<ReceptiveFeatureCondition>();
  306. return condition;
  307. }
  308. Object createImmunityNegation() const override
  309. {
  310. static std::shared_ptr<TargetConditionItem> condition = std::make_shared<ImmunityNegationCondition>();
  311. return condition;
  312. }
  313. };
  314. const TargetConditionItemFactory * TargetConditionItemFactory::getDefault()
  315. {
  316. static std::unique_ptr<TargetConditionItemFactory> singleton;
  317. if(!singleton)
  318. singleton = std::make_unique<DefaultTargetConditionItemFactory>();
  319. return singleton.get();
  320. }
  321. bool TargetCondition::isReceptive(const Mechanics * m, const battle::Unit * target) const
  322. {
  323. if(!check(absolute, m, target))
  324. return false;
  325. for(const auto & item : negation)
  326. {
  327. if(item->isReceptive(m, target))
  328. return true;
  329. }
  330. return check(normal, m, target);
  331. }
  332. void TargetCondition::serializeJson(JsonSerializeFormat & handler, const ItemFactory * itemFactory)
  333. {
  334. bool isNonMagical = false;
  335. if(handler.saving)
  336. {
  337. logGlobal->error("Spell target condition saving is not supported");
  338. return;
  339. }
  340. absolute.clear();
  341. normal.clear();
  342. negation.clear();
  343. absolute.push_back(itemFactory->createAbsoluteSpell());
  344. handler.serializeBool("nonMagical", isNonMagical);
  345. if(!isNonMagical)
  346. {
  347. absolute.push_back(itemFactory->createAbsoluteLevel());
  348. normal.push_back(itemFactory->createElemental());
  349. normal.push_back(itemFactory->createNormalLevel());
  350. normal.push_back(itemFactory->createNormalSpell());
  351. negation.push_back(itemFactory->createReceptiveFeature());
  352. negation.push_back(itemFactory->createImmunityNegation());
  353. }
  354. {
  355. auto anyOf = handler.enterStruct("anyOf");
  356. loadConditions(anyOf->getCurrent(), false, false, itemFactory);
  357. }
  358. {
  359. auto allOf = handler.enterStruct("allOf");
  360. loadConditions(allOf->getCurrent(), true, false, itemFactory);
  361. }
  362. {
  363. auto noneOf = handler.enterStruct("noneOf");
  364. loadConditions(noneOf->getCurrent(), true, true, itemFactory);
  365. }
  366. }
  367. bool TargetCondition::check(const ItemVector & condition, const Mechanics * m, const battle::Unit * target) const
  368. {
  369. bool nonExclusiveCheck = false;
  370. bool nonExclusiveExits = false;
  371. for(const auto & item : condition)
  372. {
  373. if(item->isExclusive())
  374. {
  375. if(!item->isReceptive(m, target))
  376. return false;
  377. }
  378. else
  379. {
  380. if(item->isReceptive(m, target))
  381. nonExclusiveCheck = true;
  382. nonExclusiveExits = true;
  383. }
  384. }
  385. return !nonExclusiveExits || nonExclusiveCheck;
  386. }
  387. void TargetCondition::loadConditions(const JsonNode & source, bool exclusive, bool inverted, const ItemFactory * itemFactory)
  388. {
  389. for(const auto & keyValue : source.Struct())
  390. {
  391. bool isAbsolute;
  392. const JsonNode & value = keyValue.second;
  393. if(value.String() == "absolute")
  394. isAbsolute = true;
  395. else if(value.String() == "normal")
  396. isAbsolute = false;
  397. else
  398. continue;
  399. std::string scope;
  400. std::string type;
  401. std::string identifier;
  402. CModHandler::parseIdentifier(keyValue.first, scope, type, identifier);
  403. std::shared_ptr<TargetConditionItem> item = itemFactory->createConfigurable(scope, type, identifier);
  404. if(item)
  405. {
  406. item->setExclusive(exclusive);
  407. item->setInverted(inverted);
  408. if(isAbsolute)
  409. absolute.push_back(item);
  410. else
  411. normal.push_back(item);
  412. }
  413. }
  414. }
  415. }
  416. VCMI_LIB_NAMESPACE_END