BonusCache.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * BonusCache.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 "BonusCache.h"
  12. #include "IBonusBearer.h"
  13. #include "BonusSelector.h"
  14. #include "BonusList.h"
  15. #include "../VCMI_Lib.h"
  16. #include "../IGameSettings.h"
  17. int BonusCacheBase::getBonusValueImpl(BonusCacheEntry & currentValue, const CSelector & selector, BonusCacheMode mode) const
  18. {
  19. if (target->getTreeVersion() == currentValue.version)
  20. {
  21. return currentValue.value;
  22. }
  23. else
  24. {
  25. // NOTE: following code theoretically can fail if bonus tree was changed by another thread between two following lines
  26. // However, this situation should not be possible - gamestate modification should only happen in single-treaded mode with locked gamestate mutex
  27. int newValue;
  28. if (mode == BonusCacheMode::VALUE)
  29. newValue = target->valOfBonuses(selector);
  30. else
  31. newValue = target->hasBonus(selector);
  32. currentValue.value = newValue;
  33. currentValue.version = target->getTreeVersion();
  34. return newValue;
  35. }
  36. }
  37. BonusValueCache::BonusValueCache(const IBonusBearer * target, const CSelector selector)
  38. :BonusCacheBase(target),selector(selector)
  39. {}
  40. int BonusValueCache::getValue() const
  41. {
  42. return getBonusValueImpl(value, selector, BonusCacheMode::VALUE);
  43. }
  44. bool BonusValueCache::hasBonus() const
  45. {
  46. return getBonusValueImpl(value, selector, BonusCacheMode::PRESENCE);
  47. }
  48. MagicSchoolMasteryCache::MagicSchoolMasteryCache(const IBonusBearer * target)
  49. :target(target)
  50. {}
  51. void MagicSchoolMasteryCache::update() const
  52. {
  53. static const CSelector allBonusesSelector = Selector::type()(BonusType::MAGIC_SCHOOL_SKILL);
  54. static const std::array schoolsSelector = {
  55. Selector::subtype()(SpellSchool::ANY),
  56. Selector::subtype()(SpellSchool::AIR),
  57. Selector::subtype()(SpellSchool::FIRE),
  58. Selector::subtype()(SpellSchool::WATER),
  59. Selector::subtype()(SpellSchool::EARTH),
  60. };
  61. auto list = target->getBonuses(allBonusesSelector);
  62. for (int i = 0; i < schoolsSelector.size(); ++i)
  63. schools[i] = list->valOfBonuses(schoolsSelector[i]);
  64. version = target->getTreeVersion();
  65. }
  66. int32_t MagicSchoolMasteryCache::getMastery(const SpellSchool & school) const
  67. {
  68. if (target->getTreeVersion() != version)
  69. update();
  70. return schools[school.num + 1];
  71. }
  72. PrimarySkillsCache::PrimarySkillsCache(const IBonusBearer * target)
  73. :target(target)
  74. {}
  75. void PrimarySkillsCache::update() const
  76. {
  77. static const CSelector primarySkillsSelector = Selector::type()(BonusType::PRIMARY_SKILL);
  78. static const CSelector attackSelector = Selector::subtype()(PrimarySkill::ATTACK);
  79. static const CSelector defenceSelector = Selector::subtype()(PrimarySkill::DEFENSE);
  80. static const CSelector spellPowerSelector = Selector::subtype()(PrimarySkill::SPELL_POWER);
  81. static const CSelector knowledgeSelector = Selector::subtype()(PrimarySkill::KNOWLEDGE);
  82. std::array<int, 4> minValues = {
  83. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::ATTACK),
  84. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::DEFENSE),
  85. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::SPELL_POWER),
  86. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::KNOWLEDGE)
  87. };
  88. auto list = target->getBonuses(primarySkillsSelector);
  89. skills[PrimarySkill::ATTACK] = std::max(minValues[PrimarySkill::ATTACK], list->valOfBonuses(attackSelector));
  90. skills[PrimarySkill::DEFENSE] = std::max(minValues[PrimarySkill::DEFENSE], list->valOfBonuses(defenceSelector));
  91. skills[PrimarySkill::SPELL_POWER] = std::max(minValues[PrimarySkill::SPELL_POWER], list->valOfBonuses(spellPowerSelector));
  92. skills[PrimarySkill::KNOWLEDGE] = std::max(minValues[PrimarySkill::KNOWLEDGE], list->valOfBonuses(knowledgeSelector));
  93. version = target->getTreeVersion();
  94. }
  95. const std::array<std::atomic<int32_t>, 4> & PrimarySkillsCache::getSkills() const
  96. {
  97. if (target->getTreeVersion() != version)
  98. update();
  99. return skills;
  100. }
  101. int BonusCachePerTurn::getValueUncached(int turns) const
  102. {
  103. std::lock_guard lock(bonusListMutex);
  104. int nodeTreeVersion = target->getTreeVersion();
  105. if (bonusListVersion != nodeTreeVersion)
  106. {
  107. bonusList = target->getBonuses(selector);
  108. bonusListVersion = nodeTreeVersion;
  109. }
  110. if (mode == BonusCacheMode::VALUE)
  111. {
  112. if (turns != 0)
  113. return bonusList->valOfBonuses(Selector::turns(turns));
  114. else
  115. return bonusList->totalValue();
  116. }
  117. else
  118. {
  119. if (turns != 0)
  120. return bonusList->getFirst(Selector::turns(turns)) != nullptr;
  121. else
  122. return !bonusList->empty();
  123. }
  124. }
  125. int BonusCachePerTurn::getValue(int turns) const
  126. {
  127. int nodeTreeVersion = target->getTreeVersion();
  128. if (turns < cachedTurns)
  129. {
  130. auto & entry = cache[turns];
  131. if (entry.version == nodeTreeVersion)
  132. {
  133. // best case: value is in cache and up-to-date
  134. return entry.value;
  135. }
  136. else
  137. {
  138. // else - compute value and update it in the cache
  139. int newValue = getValueUncached(turns);
  140. entry.value = newValue;
  141. entry.version = nodeTreeVersion;
  142. return newValue;
  143. }
  144. }
  145. else
  146. {
  147. // non-cacheable value - compute and return (should be 0 / close to 0 calls)
  148. return getValueUncached(turns);
  149. }
  150. }