File: BonusCache.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (213 lines) | stat: -rw-r--r-- 7,725 bytes parent folder | download
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
203
204
205
206
207
208
209
210
211
212
213
/*
 * BonusCache.cpp, 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
 *
 */

#include "StdInc.h"
#include "BonusCache.h"
#include "IBonusBearer.h"

#include "BonusSelector.h"
#include "BonusList.h"

#include "../VCMI_Lib.h"
#include "../IGameSettings.h"

VCMI_LIB_NAMESPACE_BEGIN

int BonusCacheBase::getBonusValueImpl(BonusCacheEntry & currentValue, const CSelector & selector, BonusCacheMode mode) const
{
	if (target->getTreeVersion() == currentValue.version)
	{
		return currentValue.value;
	}
	else
	{
		// NOTE: following code theoretically can fail if bonus tree was changed by another thread between two following lines
		// However, this situation should not be possible - gamestate modification should only happen in single-treaded mode with locked gamestate mutex
		int newValue;

		if (mode == BonusCacheMode::VALUE)
			newValue = target->valOfBonuses(selector);
		else
			newValue = target->hasBonus(selector);
		currentValue.value = newValue;
		currentValue.version = target->getTreeVersion();

		return newValue;
	}
}

BonusValueCache::BonusValueCache(const IBonusBearer * target, const CSelector & selector)
	:BonusCacheBase(target),selector(selector)
{}

int BonusValueCache::getValue() const
{
	return getBonusValueImpl(value, selector, BonusCacheMode::VALUE);
}

bool BonusValueCache::hasBonus() const
{
	return getBonusValueImpl(value, selector, BonusCacheMode::PRESENCE);
}

MagicSchoolMasteryCache::MagicSchoolMasteryCache(const IBonusBearer * target)
	:target(target)
{}

void MagicSchoolMasteryCache::update() const
{
	static const CSelector allBonusesSelector = Selector::type()(BonusType::MAGIC_SCHOOL_SKILL);
	static const std::array schoolsSelector = {
		Selector::subtype()(SpellSchool::ANY),
		Selector::subtype()(SpellSchool::AIR),
		Selector::subtype()(SpellSchool::FIRE),
		Selector::subtype()(SpellSchool::WATER),
		Selector::subtype()(SpellSchool::EARTH),
	};

	auto list = target->getBonuses(allBonusesSelector);
	for (int i = 0; i < schoolsSelector.size(); ++i)
		schools[i] = list->valOfBonuses(schoolsSelector[i]);

	version = target->getTreeVersion();
}

int32_t MagicSchoolMasteryCache::getMastery(const SpellSchool & school) const
{
	if (target->getTreeVersion() != version)
		update();
	return schools[school.num + 1];
}

PrimarySkillsCache::PrimarySkillsCache(const IBonusBearer * target)
	:target(target)
{}

void PrimarySkillsCache::update() const
{
	static const CSelector primarySkillsSelector = Selector::type()(BonusType::PRIMARY_SKILL);
	static const CSelector attackSelector = Selector::subtype()(PrimarySkill::ATTACK);
	static const CSelector defenceSelector = Selector::subtype()(PrimarySkill::DEFENSE);
	static const CSelector spellPowerSelector = Selector::subtype()(PrimarySkill::SPELL_POWER);
	static const CSelector knowledgeSelector = Selector::subtype()(PrimarySkill::KNOWLEDGE);

	std::array<int, 4> minValues = {
		VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::ATTACK),
		VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::DEFENSE),
		VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::SPELL_POWER),
		VLC->engineSettings()->getVectorValue(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS, PrimarySkill::KNOWLEDGE)
	};

	auto list = target->getBonuses(primarySkillsSelector);
	skills[PrimarySkill::ATTACK] = std::max(minValues[PrimarySkill::ATTACK], list->valOfBonuses(attackSelector));
	skills[PrimarySkill::DEFENSE] = std::max(minValues[PrimarySkill::DEFENSE], list->valOfBonuses(defenceSelector));
	skills[PrimarySkill::SPELL_POWER] = std::max(minValues[PrimarySkill::SPELL_POWER], list->valOfBonuses(spellPowerSelector));
	skills[PrimarySkill::KNOWLEDGE] = std::max(minValues[PrimarySkill::KNOWLEDGE], list->valOfBonuses(knowledgeSelector));

	version = target->getTreeVersion();
}

const std::array<std::atomic<int32_t>, 4> & PrimarySkillsCache::getSkills() const
{
	if (target->getTreeVersion() != version)
		update();
	return skills;
}

int BonusCachePerTurn::getValueUncached(int turns) const
{
	std::lock_guard lock(bonusListMutex);

	int nodeTreeVersion = target->getTreeVersion();

	if (bonusListVersion != nodeTreeVersion)
	{
		bonusList = target->getBonuses(selector);
		bonusListVersion = nodeTreeVersion;
	}

	if (mode == BonusCacheMode::VALUE)
	{
		if (turns != 0)
			return bonusList->valOfBonuses(Selector::turns(turns));
		else
			return bonusList->totalValue();
	}
	else
	{
		if (turns != 0)
			return bonusList->getFirst(Selector::turns(turns)) != nullptr;
		else
			return !bonusList->empty();
	}
}

int BonusCachePerTurn::getValue(int turns) const
{
	int nodeTreeVersion = target->getTreeVersion();

	if (turns < cachedTurns)
	{
		auto & entry = cache[turns];
		if (entry.version == nodeTreeVersion)
		{
			// best case: value is in cache and up-to-date
			return entry.value;
		}
		else
		{
			// else - compute value and update it in the cache
			int newValue = getValueUncached(turns);
			entry.value = newValue;
			entry.version = nodeTreeVersion;
			return newValue;
		}
	}
	else
	{
		// non-cacheable value - compute and return (should be 0 / close to 0 calls)
		return getValueUncached(turns);
	}
}

const UnitBonusValuesProxy::SelectorsArray * UnitBonusValuesProxy::generateSelectors()
{
	static const CSelector additionalAttack = Selector::type()(BonusType::ADDITIONAL_ATTACK);
	static const CSelector selectorMelee = Selector::effectRange()(BonusLimitEffect::NO_LIMIT).Or(Selector::effectRange()(BonusLimitEffect::ONLY_MELEE_FIGHT));
	static const CSelector selectorRanged = Selector::effectRange()(BonusLimitEffect::NO_LIMIT).Or(Selector::effectRange()(BonusLimitEffect::ONLY_DISTANCE_FIGHT));
	static const CSelector minDamage = Selector::typeSubtype(BonusType::CREATURE_DAMAGE, BonusCustomSubtype::creatureDamageBoth).Or(Selector::typeSubtype(BonusType::CREATURE_DAMAGE, BonusCustomSubtype::creatureDamageMin));
	static const CSelector maxDamage = Selector::typeSubtype(BonusType::CREATURE_DAMAGE, BonusCustomSubtype::creatureDamageBoth).Or(Selector::typeSubtype(BonusType::CREATURE_DAMAGE, BonusCustomSubtype::creatureDamageMax));
	static const CSelector attack = Selector::typeSubtype(BonusType::PRIMARY_SKILL, BonusSubtypeID(PrimarySkill::ATTACK));
	static const CSelector defence = Selector::typeSubtype(BonusType::PRIMARY_SKILL, BonusSubtypeID(PrimarySkill::DEFENSE));

	static const UnitBonusValuesProxy::SelectorsArray selectors = {
		additionalAttack.And(selectorMelee), //TOTAL_ATTACKS_MELEE,
		additionalAttack.And(selectorRanged), //TOTAL_ATTACKS_RANGED,
		minDamage.And(selectorMelee), //MIN_DAMAGE_MELEE,
		minDamage.And(selectorRanged), //MIN_DAMAGE_RANGED,
		maxDamage.And(selectorMelee), //MAX_DAMAGE_MELEE,
		maxDamage.And(selectorRanged), //MAX_DAMAGE_RANGED,
		attack.And(selectorMelee),//ATTACK_MELEE,
		attack.And(selectorRanged),//ATTACK_RANGED,
		defence.And(selectorMelee),//DEFENCE_MELEE,
		defence.And(selectorRanged),//DEFENCE_RANGED,
		Selector::type()(BonusType::IN_FRENZY),//IN_FRENZY,
		Selector::type()(BonusType::HYPNOTIZED),//HYPNOTIZED,
		Selector::type()(BonusType::FORGETFULL),//FORGETFULL,
		Selector::type()(BonusType::FREE_SHOOTING).Or(Selector::type()(BonusType::SIEGE_WEAPON)),//HAS_FREE_SHOOTING,
		Selector::type()(BonusType::STACK_HEALTH),//STACK_HEALTH,
		Selector::type()(BonusType::INVINCIBLE),//INVINCIBLE,
		Selector::type()(BonusType::NONE).And(Selector::source(BonusSource::SPELL_EFFECT, BonusSourceID(SpellID(SpellID::CLONE))))
	};

	return &selectors;
}

VCMI_LIB_NAMESPACE_END