BonusCache.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. MagicSchoolMasteryCache::MagicSchoolMasteryCache(const IBonusBearer * target)
  45. :target(target)
  46. {}
  47. void MagicSchoolMasteryCache::update() const
  48. {
  49. static const CSelector allBonusesSelector = Selector::type()(BonusType::MAGIC_SCHOOL_SKILL);
  50. static const std::array schoolsSelector = {
  51. Selector::subtype()(SpellSchool::ANY),
  52. Selector::subtype()(SpellSchool::AIR),
  53. Selector::subtype()(SpellSchool::FIRE),
  54. Selector::subtype()(SpellSchool::WATER),
  55. Selector::subtype()(SpellSchool::EARTH),
  56. };
  57. auto list = target->getBonuses(allBonusesSelector);
  58. for (int i = 0; i < schoolsSelector.size(); ++i)
  59. schools[i] = list->valOfBonuses(schoolsSelector[i]);
  60. version = target->getTreeVersion();
  61. }
  62. int32_t MagicSchoolMasteryCache::getMastery(const SpellSchool & school) const
  63. {
  64. if (target->getTreeVersion() != version)
  65. update();
  66. return schools[school.num + 1];
  67. }
  68. PrimarySkillsCache::PrimarySkillsCache(const IBonusBearer * target)
  69. :target(target)
  70. {}
  71. void PrimarySkillsCache::update() const
  72. {
  73. static const CSelector primarySkillsSelector = Selector::type()(BonusType::PRIMARY_SKILL);
  74. static const CSelector attackSelector = Selector::subtype()(PrimarySkill::ATTACK);
  75. static const CSelector defenceSelector = Selector::subtype()(PrimarySkill::DEFENSE);
  76. static const CSelector spellPowerSelector = Selector::subtype()(PrimarySkill::SPELL_POWER);
  77. static const CSelector knowledgeSelector = Selector::subtype()(PrimarySkill::KNOWLEDGE);
  78. std::array<int, 4> minValues = {
  79. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::ATTACK),
  80. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::DEFENSE),
  81. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::SPELL_POWER),
  82. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::KNOWLEDGE)
  83. };
  84. auto list = target->getBonuses(primarySkillsSelector);
  85. skills[PrimarySkill::ATTACK] = std::max(minValues[PrimarySkill::ATTACK], list->valOfBonuses(attackSelector));
  86. skills[PrimarySkill::DEFENSE] = std::max(minValues[PrimarySkill::DEFENSE], list->valOfBonuses(defenceSelector));
  87. skills[PrimarySkill::SPELL_POWER] = std::max(minValues[PrimarySkill::SPELL_POWER], list->valOfBonuses(spellPowerSelector));
  88. skills[PrimarySkill::KNOWLEDGE] = std::max(minValues[PrimarySkill::KNOWLEDGE], list->valOfBonuses(knowledgeSelector));
  89. version = target->getTreeVersion();
  90. }
  91. const std::array<std::atomic<int32_t>, 4> & PrimarySkillsCache::getSkills() const
  92. {
  93. if (target->getTreeVersion() != version)
  94. update();
  95. return skills;
  96. }
  97. int BonusCachePerTurn::getValueUncached(int turns) const
  98. {
  99. std::lock_guard lock(bonusListMutex);
  100. int nodeTreeVersion = target->getTreeVersion();
  101. if (bonusListVersion != nodeTreeVersion)
  102. {
  103. bonusList = target->getBonuses(selector);
  104. bonusListVersion = nodeTreeVersion;
  105. }
  106. if (mode == BonusCacheMode::VALUE)
  107. {
  108. if (turns != 0)
  109. return bonusList->valOfBonuses(Selector::turns(turns));
  110. else
  111. return bonusList->totalValue();
  112. }
  113. else
  114. {
  115. if (turns != 0)
  116. return bonusList->getFirst(Selector::turns(turns)) != nullptr;
  117. else
  118. return !bonusList->empty();
  119. }
  120. }
  121. int BonusCachePerTurn::getValue(int turns) const
  122. {
  123. int nodeTreeVersion = target->getTreeVersion();
  124. if (turns < cachedTurns)
  125. {
  126. auto & entry = cache[turns];
  127. if (entry.version == nodeTreeVersion)
  128. {
  129. // best case: value is in cache and up-to-date
  130. return entry.value;
  131. }
  132. else
  133. {
  134. // else - compute value and update it in the cache
  135. int newValue = getValueUncached(turns);
  136. entry.value = newValue;
  137. entry.version = nodeTreeVersion;
  138. return newValue;
  139. }
  140. }
  141. else
  142. {
  143. // non-cacheable value - compute and return (should be 0 / close to 0 calls)
  144. return getValueUncached(turns);
  145. }
  146. }