1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/*
* BonusCache.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
#include "BonusSelector.h"
VCMI_LIB_NAMESPACE_BEGIN
enum class BonusCacheMode : int8_t
{
VALUE, // total value of bonus will be cached
PRESENCE, // presence of bonus will be cached
};
/// Internal base class with no own cache
class BonusCacheBase
{
protected:
const IBonusBearer * target;
explicit BonusCacheBase(const IBonusBearer * target):
target(target)
{}
struct BonusCacheEntry
{
std::atomic<int32_t> version = 0;
std::atomic<int32_t> value = 0;
BonusCacheEntry() = default;
BonusCacheEntry(const BonusCacheEntry & other)
: version(other.version.load())
, value(other.value.load())
{
}
BonusCacheEntry & operator =(const BonusCacheEntry & other)
{
version = other.version.load();
value = other.value.load();
return *this;
}
};
int getBonusValueImpl(BonusCacheEntry & currentValue, const CSelector & selector, BonusCacheMode) const;
};
/// Cache that tracks a single query to bonus system
class BonusValueCache : public BonusCacheBase
{
CSelector selector;
mutable BonusCacheEntry value;
public:
BonusValueCache(const IBonusBearer * target, const CSelector & selector);
int getValue() const;
bool hasBonus() const;
};
/// Cache that can track a list of queries to bonus system
template<size_t SIZE>
class BonusValuesArrayCache : public BonusCacheBase
{
public:
using SelectorsArray = std::array<const CSelector, SIZE>;
BonusValuesArrayCache(const IBonusBearer * target, const SelectorsArray * selectors)
: BonusCacheBase(target)
, selectors(selectors)
{}
int getBonusValue(int index) const
{
return getBonusValueImpl(cache[index], (*selectors)[index], BonusCacheMode::VALUE);
}
int hasBonus(int index) const
{
return getBonusValueImpl(cache[index], (*selectors)[index], BonusCacheMode::PRESENCE);
}
private:
using CacheArray = std::array<BonusCacheEntry, SIZE>;
const SelectorsArray * selectors;
mutable CacheArray cache;
};
class UnitBonusValuesProxy
{
public:
enum ECacheKeys : int8_t
{
TOTAL_ATTACKS_MELEE,
TOTAL_ATTACKS_RANGED,
MIN_DAMAGE_MELEE,
MIN_DAMAGE_RANGED,
MAX_DAMAGE_MELEE,
MAX_DAMAGE_RANGED,
ATTACK_MELEE,
ATTACK_RANGED,
DEFENCE_MELEE,
DEFENCE_RANGED,
IN_FRENZY,
HYPNOTIZED,
FORGETFULL,
HAS_FREE_SHOOTING,
STACK_HEALTH,
INVINCIBLE,
CLONE_MARKER,
TOTAL_KEYS,
};
static constexpr size_t KEYS_COUNT = static_cast<size_t>(ECacheKeys::TOTAL_KEYS);
using SelectorsArray = BonusValuesArrayCache<KEYS_COUNT>::SelectorsArray;
UnitBonusValuesProxy(const IBonusBearer * Target):
cache(Target, generateSelectors())
{}
int getBonusValue(ECacheKeys which) const
{
auto index = static_cast<size_t>(which);
return cache.getBonusValue(index);
}
int hasBonus(ECacheKeys which) const
{
auto index = static_cast<size_t>(which);
return cache.hasBonus(index);
}
private:
const SelectorsArray * generateSelectors();
BonusValuesArrayCache<KEYS_COUNT> cache;
};
/// Cache that tracks values of primary skill values in bonus system
class PrimarySkillsCache
{
const IBonusBearer * target;
mutable std::atomic<int32_t> version = 0;
mutable std::array<std::atomic<int32_t>, 4> skills;
void update() const;
public:
PrimarySkillsCache(const IBonusBearer * target);
const std::array<std::atomic<int32_t>, 4> & getSkills() const;
};
/// Cache that tracks values of spell school mastery in bonus system
class MagicSchoolMasteryCache
{
const IBonusBearer * target;
mutable std::atomic<int32_t> version = 0;
mutable std::array<std::atomic<int32_t>, 4+1> schools;
void update() const;
public:
MagicSchoolMasteryCache(const IBonusBearer * target);
int32_t getMastery(const SpellSchool & school) const;
};
/// Cache that tracks values for different values of bonus duration
class BonusCachePerTurn : public BonusCacheBase
{
static constexpr int cachedTurns = 8;
const CSelector selector;
mutable TConstBonusListPtr bonusList;
mutable std::mutex bonusListMutex;
mutable std::atomic<int32_t> bonusListVersion = 0;
mutable std::array<BonusCacheEntry, cachedTurns> cache;
const BonusCacheMode mode;
int getValueUncached(int turns) const;
public:
BonusCachePerTurn(const IBonusBearer * target, const CSelector & selector, BonusCacheMode mode)
: BonusCacheBase(target)
, selector(selector)
, mode(mode)
{}
int getValue(int turns) const;
};
VCMI_LIB_NAMESPACE_END
|