TargetCondition.cpp 12 KB

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