BonusCache.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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) 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 = target->valOfBonuses(selector);
  28. currentValue.value = newValue;
  29. currentValue.version = target->getTreeVersion();
  30. return newValue;
  31. }
  32. }
  33. BonusValueCache::BonusValueCache(const IBonusBearer * target, const CSelector selector)
  34. :BonusCacheBase(target),selector(selector)
  35. {}
  36. int BonusValueCache::getValue() const
  37. {
  38. return getBonusValueImpl(value, selector);
  39. }
  40. PrimarySkillsCache::PrimarySkillsCache(const IBonusBearer * target)
  41. :target(target)
  42. {}
  43. void PrimarySkillsCache::update() const
  44. {
  45. static const CSelector primarySkillsSelector = Selector::type()(BonusType::PRIMARY_SKILL);
  46. static const CSelector attackSelector = Selector::subtype()(PrimarySkill::ATTACK);
  47. static const CSelector defenceSelector = Selector::subtype()(PrimarySkill::DEFENSE);
  48. static const CSelector spellPowerSelector = Selector::subtype()(PrimarySkill::SPELL_POWER);
  49. static const CSelector knowledgeSelector = Selector::subtype()(PrimarySkill::KNOWLEDGE);
  50. std::array<int, 4> minValues = {
  51. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::ATTACK),
  52. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::DEFENSE),
  53. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::SPELL_POWER),
  54. VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::KNOWLEDGE)
  55. };
  56. auto list = target->getBonuses(primarySkillsSelector);
  57. skills[PrimarySkill::ATTACK] = std::max(minValues[PrimarySkill::ATTACK], list->valOfBonuses(attackSelector));
  58. skills[PrimarySkill::DEFENSE] = std::max(minValues[PrimarySkill::DEFENSE], list->valOfBonuses(defenceSelector));
  59. skills[PrimarySkill::SPELL_POWER] = std::max(minValues[PrimarySkill::SPELL_POWER], list->valOfBonuses(spellPowerSelector));
  60. skills[PrimarySkill::KNOWLEDGE] = std::max(minValues[PrimarySkill::KNOWLEDGE], list->valOfBonuses(knowledgeSelector));
  61. version = target->getTreeVersion();
  62. }
  63. const std::array<std::atomic<int32_t>, 4> & PrimarySkillsCache::getSkills() const
  64. {
  65. if (target->getTreeVersion() != version)
  66. update();
  67. return skills;
  68. }