forked from opentibiabr/canary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocation.cpp
308 lines (259 loc) · 9.84 KB
/
vocation.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <opentibiabr@outlook.com>
* Repository: https://github.com/opentibiabr/canary
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.com/
*/
#include "pch.hpp"
#include "creatures/players/vocations/vocation.hpp"
#include "utils/pugicast.hpp"
#include "utils/tools.hpp"
bool Vocations::reload() {
vocationsMap.clear();
return loadFromXml();
}
bool Vocations::loadFromXml() {
pugi::xml_document doc;
auto folder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__) + "/XML/vocations.xml";
pugi::xml_parse_result result = doc.load_file(folder.c_str());
if (!result) {
printXMLError(__FUNCTION__, folder, result);
return false;
}
for (auto vocationNode : doc.child("vocations").children()) {
pugi::xml_attribute attr;
if (!(attr = vocationNode.attribute("id"))) {
g_logger().warn("[{}] - Missing vocation id", __FUNCTION__);
continue;
}
uint16_t id = pugi::cast<uint16_t>(attr.value());
auto res = vocationsMap.emplace(std::piecewise_construct, std::forward_as_tuple(id), std::forward_as_tuple(std::make_shared<Vocation>(id)));
auto voc = res.first->second;
if ((attr = vocationNode.attribute("name"))) {
voc->name = attr.as_string();
}
if ((attr = vocationNode.attribute("clientid"))) {
voc->clientId = pugi::cast<uint16_t>(attr.value());
}
if ((attr = vocationNode.attribute("baseid"))) {
voc->baseId = pugi::cast<uint16_t>(attr.value());
}
if ((attr = vocationNode.attribute("description"))) {
voc->description = attr.as_string();
}
if ((attr = vocationNode.attribute("magicshield"))) {
voc->magicShield = attr.as_bool();
}
if ((attr = vocationNode.attribute("gaincap"))) {
voc->gainCap = pugi::cast<uint32_t>(attr.value()) * 100;
}
if ((attr = vocationNode.attribute("gainhp"))) {
voc->gainHP = pugi::cast<uint32_t>(attr.value());
}
if ((attr = vocationNode.attribute("gainmana"))) {
voc->gainMana = pugi::cast<uint32_t>(attr.value());
}
if ((attr = vocationNode.attribute("gainhpticks"))) {
voc->gainHealthTicks = pugi::cast<uint32_t>(attr.value());
}
if ((attr = vocationNode.attribute("gainhpamount"))) {
voc->gainHealthAmount = pugi::cast<uint32_t>(attr.value());
}
if ((attr = vocationNode.attribute("gainmanaticks"))) {
voc->gainManaTicks = pugi::cast<uint32_t>(attr.value());
}
if ((attr = vocationNode.attribute("gainmanaamount"))) {
voc->gainManaAmount = pugi::cast<uint32_t>(attr.value());
}
if ((attr = vocationNode.attribute("manamultiplier"))) {
voc->manaMultiplier = pugi::cast<float>(attr.value());
}
if ((attr = vocationNode.attribute("attackspeed"))) {
voc->attackSpeed = pugi::cast<uint32_t>(attr.value());
}
if ((attr = vocationNode.attribute("basespeed"))) {
voc->baseSpeed = pugi::cast<uint32_t>(attr.value());
}
if ((attr = vocationNode.attribute("soulmax"))) {
voc->soulMax = pugi::cast<uint16_t>(attr.value());
}
if ((attr = vocationNode.attribute("gainsoulticks"))) {
voc->gainSoulTicks = pugi::cast<uint32_t>(attr.value());
}
if ((attr = vocationNode.attribute("fromvoc"))) {
voc->fromVocation = pugi::cast<uint32_t>(attr.value());
}
if ((attr = vocationNode.attribute("canCombat"))) {
voc->combat = attr.as_bool();
}
if ((attr = vocationNode.attribute("avatarlooktype"))) {
voc->avatarLookType = pugi::cast<uint16_t>(attr.value());
}
for (auto childNode : vocationNode.children()) {
if (strcasecmp(childNode.name(), "skill") == 0) {
pugi::xml_attribute skillIdAttribute = childNode.attribute("id");
if (skillIdAttribute) {
uint16_t skill_id = pugi::cast<uint16_t>(skillIdAttribute.value());
if (skill_id <= SKILL_LAST) {
voc->skillMultipliers[skill_id] = pugi::cast<float>(childNode.attribute("multiplier").value());
} else {
g_logger().warn("[Vocations::loadFromXml] - "
"No valid skill id: {} for vocation: {}",
skill_id, voc->id);
}
} else {
g_logger().warn("[Vocations::loadFromXml] - "
"Missing skill id for vocation: {}",
voc->id);
}
} else if (strcasecmp(childNode.name(), "mitigation") == 0) {
pugi::xml_attribute factorAttribute = childNode.attribute("multiplier");
if (factorAttribute) {
voc->mitigationFactor = pugi::cast<float>(factorAttribute.value());
}
pugi::xml_attribute primaryShieldAttribute = childNode.attribute("primaryShield");
if (primaryShieldAttribute) {
voc->mitigationPrimaryShield = pugi::cast<float>(primaryShieldAttribute.value());
}
pugi::xml_attribute secondaryShieldAttribute = childNode.attribute("secondaryShield");
if (secondaryShieldAttribute) {
voc->mitigationSecondaryShield = pugi::cast<float>(secondaryShieldAttribute.value());
}
} else if (strcasecmp(childNode.name(), "formula") == 0) {
pugi::xml_attribute meleeDamageAttribute = childNode.attribute("meleeDamage");
if (meleeDamageAttribute) {
voc->meleeDamageMultiplier = pugi::cast<float>(meleeDamageAttribute.value());
}
pugi::xml_attribute distDamageAttribute = childNode.attribute("distDamage");
if (distDamageAttribute) {
voc->distDamageMultiplier = pugi::cast<float>(distDamageAttribute.value());
}
pugi::xml_attribute defenseAttribute = childNode.attribute("defense");
if (defenseAttribute) {
voc->defenseMultiplier = pugi::cast<float>(defenseAttribute.value());
}
pugi::xml_attribute armorAttribute = childNode.attribute("armor");
if (armorAttribute) {
voc->armorMultiplier = pugi::cast<float>(armorAttribute.value());
}
} else if (strcasecmp(childNode.name(), "pvp") == 0) {
pugi::xml_attribute pvpDamageReceivedMultiplier = childNode.attribute("damageReceivedMultiplier");
if (pvpDamageReceivedMultiplier) {
voc->pvpDamageReceivedMultiplier = pugi::cast<float>(pvpDamageReceivedMultiplier.value());
}
pugi::xml_attribute pvpDamageDealtMultiplier = childNode.attribute("damageDealtMultiplier");
if (pvpDamageDealtMultiplier) {
voc->pvpDamageDealtMultiplier = pugi::cast<float>(pvpDamageDealtMultiplier.value());
}
} else if (strcasecmp(childNode.name(), "gem") == 0) {
pugi::xml_attribute qualityAttr = childNode.attribute("quality");
pugi::xml_attribute nameAttr = childNode.attribute("name");
auto quality = pugi::cast<uint8_t>(qualityAttr.value());
auto name = nameAttr.as_string();
voc->wheelGems[static_cast<WheelGemQuality_t>(quality)] = name;
}
}
}
return true;
}
std::shared_ptr<Vocation> Vocations::getVocation(uint16_t id) {
auto it = vocationsMap.find(id);
if (it == vocationsMap.end()) {
g_logger().warn("[Vocations::getVocation] - "
"Vocation {} not found",
id);
return nullptr;
}
return it->second;
}
uint16_t Vocations::getVocationId(const std::string &name) const {
for (const auto &it : vocationsMap) {
if (strcasecmp(it.second->name.c_str(), name.c_str()) == 0) {
return it.first;
}
}
return -1;
}
uint16_t Vocations::getPromotedVocation(uint16_t vocationId) const {
for (const auto &it : vocationsMap) {
if (it.second->fromVocation == vocationId && it.first != vocationId) {
return it.first;
}
}
return VOCATION_NONE;
}
uint32_t Vocation::skillBase[SKILL_LAST + 1] = { 50, 50, 50, 50, 30, 100, 20 };
const uint16_t minSkillLevel = 10;
absl::uint128 Vocation::getTotalSkillTries(uint8_t skill, uint16_t level) {
if (skill > SKILL_LAST) {
return 0;
}
auto it = cacheSkillTotal[skill].find(level);
if (it != cacheSkillTotal[skill].end()) {
return it->second;
}
absl::uint128 totalTries = 0;
for (uint16_t i = minSkillLevel; i <= level; ++i) {
totalTries += getReqSkillTries(skill, i);
}
cacheSkillTotal[skill][level] = totalTries;
return totalTries;
}
uint64_t Vocation::getReqSkillTries(uint8_t skill, uint16_t level) {
if (skill > SKILL_LAST || level <= minSkillLevel) {
return 0;
}
auto it = cacheSkill[skill].find(level);
if (it != cacheSkill[skill].end()) {
return it->second;
}
uint64_t tries = static_cast<uint64_t>(skillBase[skill] * std::pow(static_cast<double>(skillMultipliers[skill]), level - (minSkillLevel + 1)));
cacheSkill[skill][level] = tries;
return tries;
}
absl::uint128 Vocation::getTotalMana(uint32_t magLevel) {
if (magLevel == 0) {
return 0;
}
auto it = cacheManaTotal.find(magLevel);
if (it != cacheManaTotal.end()) {
return it->second;
}
absl::uint128 totalMana = 0;
for (uint32_t i = 1; i <= magLevel; ++i) {
totalMana += getReqMana(i);
}
return totalMana;
}
uint64_t Vocation::getReqMana(uint32_t magLevel) {
if (magLevel == 0) {
return 0;
}
auto it = cacheMana.find(magLevel);
if (it != cacheMana.end()) {
return it->second;
}
uint64_t reqMana = std::floor<uint64_t>(1600 * std::pow<double>(manaMultiplier, static_cast<int32_t>(magLevel) - 1));
cacheMana[magLevel] = reqMana;
return reqMana;
}
std::vector<WheelGemSupremeModifier_t> Vocation::getSupremeGemModifiers() {
if (!m_supremeGemModifiers.empty()) {
return m_supremeGemModifiers;
}
auto baseVocation = g_vocations().getVocation(getBaseId());
auto vocationName = asLowerCaseString(baseVocation->getVocName());
auto allModifiers = magic_enum::enum_entries<WheelGemSupremeModifier_t>();
g_logger().debug("Loading supreme gem modifiers for vocation: {}", vocationName);
for (const auto &[value, modifierName] : allModifiers) {
std::string targetVocation(modifierName.substr(0, modifierName.find('_')));
toLowerCaseString(targetVocation);
g_logger().debug("Checking supreme gem modifier: {}, targetVocation: {}", modifierName, targetVocation);
if (targetVocation == "general" || targetVocation.find(vocationName) != std::string::npos) {
m_supremeGemModifiers.push_back(value);
}
}
return m_supremeGemModifiers;
}