TargetCondition.cpp 12 KB

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