BonusCache.cpp 4.3 KB

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