2
0

TargetCondition.cpp 12 KB

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