-
-
Notifications
You must be signed in to change notification settings - Fork 661
/
Copy pathio_bosstiary.cpp
314 lines (264 loc) · 9.67 KB
/
io_bosstiary.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
309
310
311
312
313
314
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <[email protected]>
* 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 "io/io_bosstiary.hpp"
#include "creatures/monsters/monsters.hpp"
#include "creatures/players/player.hpp"
#include "game/game.hpp"
#include "utils/tools.hpp"
#include "items/item.hpp"
void IOBosstiary::loadBoostedBoss() {
Database &database = Database::getInstance();
std::ostringstream query;
query << "SELECT * FROM `boosted_boss`";
DBResult_ptr result = database.storeQuery(query.str());
if (!result) {
g_logger().error("[{}] Failed to detect boosted boss database. (CODE 01)", __FUNCTION__);
return;
}
uint16_t date = result->getNumber<uint16_t>("date");
auto timeNow = getTimeNow();
auto time = localtime(&timeNow);
auto today = time->tm_mday;
auto bossMap = getBosstiaryMap();
if (bossMap.size() <= 1) {
g_logger().error("[{}] It is not possible to create a boosted boss with only one registered boss. (CODE 02)", __FUNCTION__);
return;
}
std::string bossName;
uint16_t bossId = 0;
if (date == today) {
bossName = result->getString("boostname");
bossId = result->getNumber<uint16_t>("raceid");
setBossBoostedName(bossName);
setBossBoostedId(bossId);
g_logger().info("Boosted boss: {}", bossName);
return;
}
// Filter only archfoe bosses
std::map<uint16_t, std::string> bossInfo;
for (auto [infoBossRaceId, infoBossName] : bossMap) {
const auto mType = getMonsterTypeByBossRaceId(infoBossRaceId);
if (!mType || mType->info.bosstiaryRace != BosstiaryRarity_t::RARITY_ARCHFOE) {
continue;
}
bossInfo.try_emplace(infoBossRaceId, infoBossName);
}
// Check if not have archfoe registered boss
if (bossInfo.size() == 0) {
g_logger().error("Failed to boost a boss. There is no boss registered with the Archfoe Rarity.");
return;
}
uint16_t oldBossRace = result->getNumber<uint16_t>("raceid");
while (true) {
uint32_t randomIndex = uniform_random(0, static_cast<int32_t>(bossInfo.size()));
auto it = std::next(bossInfo.begin(), randomIndex);
if (it == bossInfo.end()) {
break;
}
const auto &[randomBossId, randomBossName] = *it;
if (randomBossId == oldBossRace) {
continue;
}
bossName = randomBossName;
bossId = randomBossId;
break;
}
query.str(std::string());
query << "UPDATE `boosted_boss` SET ";
query << "`date` = '" << today << "',";
query << "`boostname` = " << database.escapeString(bossName) << ",";
if (const auto bossType = getMonsterTypeByBossRaceId(bossId);
bossType) {
query << "`looktypeEx` = " << static_cast<int>(bossType->info.outfit.lookTypeEx) << ",";
query << "`looktype` = " << static_cast<int>(bossType->info.outfit.lookType) << ",";
query << "`lookfeet` = " << static_cast<int>(bossType->info.outfit.lookFeet) << ",";
query << "`looklegs` = " << static_cast<int>(bossType->info.outfit.lookLegs) << ",";
query << "`lookhead` = " << static_cast<int>(bossType->info.outfit.lookHead) << ",";
query << "`lookbody` = " << static_cast<int>(bossType->info.outfit.lookBody) << ",";
query << "`lookaddons` = " << static_cast<int>(bossType->info.outfit.lookAddons) << ",";
query << "`lookmount` = " << static_cast<int>(bossType->info.outfit.lookMount) << ",";
}
query << "`raceid` = '" << bossId << "'";
if (!database.executeQuery(query.str())) {
g_logger().error("[{}] Failed to detect boosted boss database. (CODE 03)", __FUNCTION__);
return;
}
query.str(std::string());
query << "UPDATE `player_bosstiary` SET `bossIdSlotOne` = 0 WHERE `bossIdSlotOne` = " << bossId;
if (!database.executeQuery(query.str())) {
g_logger().error("[{}] Failed to reset players selected boss slot 1. (CODE 03)", __FUNCTION__);
}
query.str(std::string());
query << "UPDATE `player_bosstiary` SET `bossIdSlotTwo` = 0 WHERE `bossIdSlotTwo` = " << bossId;
if (!database.executeQuery(query.str())) {
g_logger().error("[{}] Failed to reset players selected boss slot 1. (CODE 03)", __FUNCTION__);
}
setBossBoostedName(bossName);
setBossBoostedId(bossId);
g_logger().info("Boosted boss: {}", bossName);
}
void IOBosstiary::addBosstiaryMonster(uint16_t raceId, const std::string &name) {
if (auto it = bosstiaryMap.find(raceId);
it != bosstiaryMap.end()) {
return;
}
bosstiaryMap.try_emplace(raceId, name);
auto boss = std::pair<uint16_t, std::string>(raceId, name);
bosstiaryMap.insert(boss);
}
const std::map<uint16_t, std::string> &IOBosstiary::getBosstiaryMap() const {
return bosstiaryMap;
}
void IOBosstiary::setBossBoostedName(const std::string_view &name) {
boostedBoss = name;
}
std::string IOBosstiary::getBoostedBossName() const {
return boostedBoss;
}
void IOBosstiary::setBossBoostedId(uint16_t raceId) {
boostedBossId = raceId;
}
uint16_t IOBosstiary::getBoostedBossId() const {
return boostedBossId;
}
std::shared_ptr<MonsterType> IOBosstiary::getMonsterTypeByBossRaceId(uint16_t raceId) const {
for ([[maybe_unused]] const auto &[bossRaceId, bossName] : getBosstiaryMap()) {
if (bossRaceId == raceId) {
const auto monsterType = g_monsters().getMonsterType(bossName);
if (!monsterType) {
g_logger().error("[{}] Boss with id {} not found in boss map", __FUNCTION__, raceId);
continue;
}
return monsterType;
}
}
return nullptr;
}
void IOBosstiary::addBosstiaryKill(std::shared_ptr<Player> player, const std::shared_ptr<MonsterType> mtype, uint32_t amount /*= 1*/) const {
if (!player || !mtype) {
return;
}
uint16_t bossId = mtype->info.raceid;
if (bossId == 0) {
return;
}
auto oldBossLevel = getBossCurrentLevel(player, bossId);
player->addBestiaryKillCount(bossId, amount);
player->refreshCyclopediaMonsterTracker(true);
auto newBossLevel = getBossCurrentLevel(player, bossId);
if (oldBossLevel == newBossLevel) {
return;
}
player->sendBosstiaryEntryChanged(bossId);
auto bossRace = mtype->info.bosstiaryRace;
const std::vector<LevelInfo> &infoForCurrentRace = levelInfos.at(bossRace);
auto pointsForCurrentLevel = infoForCurrentRace[newBossLevel - 1].points;
player->addBossPoints(pointsForCurrentLevel);
int32_t value = player->getStorageValue(STORAGEVALUE_PODIUM);
if (value != 1 && newBossLevel == 2) {
auto returnValue = g_game().processHouseOffer(player, ITEM_PODIUM_OF_VIGOUR);
if (!returnValue) {
return;
}
std::string podiumMessage = "Congratulations! You have reached the Expertise level for your first boss. "
"As a reward, a free Podium of Vigour has been sent to your Store inbox. "
"You can place this podium in any house you own with this character. "
"Use it to display bosses for which you have reached at least the Expertise level.";
player->sendTextMessage(MESSAGE_GAME_HIGHLIGHT, podiumMessage);
player->addStorageValue(STORAGEVALUE_PODIUM, 1);
}
}
uint16_t IOBosstiary::calculateLootBonus(uint32_t bossPoints) const {
// Calculate Bonus based on Boss Points
if (bossPoints <= 250) {
return static_cast<uint16_t>(25 + bossPoints / 10);
} else if (bossPoints < 1250) {
return static_cast<uint16_t>(37.5 + bossPoints / 20);
}
return static_cast<uint16_t>(100 + 0.5 * (sqrt(8 * ((bossPoints - 1250) / 5) + 81) - 9));
}
uint32_t IOBosstiary::calculateBossPoints(uint16_t lootBonus) const {
// Calculate Boss Points based on Bonus
if (lootBonus <= 25) {
return 0;
}
if (lootBonus <= 50) {
return 10 * lootBonus - 250;
} else if (lootBonus <= 100) {
return 20 * lootBonus - 750;
}
return static_cast<uint32_t>((2.5 * lootBonus * lootBonus) - (477.5 * lootBonus) + 24000);
}
std::vector<uint16_t> IOBosstiary::getBosstiaryFinished(const std::shared_ptr<Player> &player, uint8_t level /* = 1*/) const {
if (!player) {
return {};
}
stdext::vector_set<uint16_t> unlockedMonsters;
for (const auto &[bossId, bossName] : getBosstiaryMap()) {
uint32_t bossKills = player->getBestiaryKillCount(bossId);
if (bossKills == 0) {
continue;
}
const auto mType = g_monsters().getMonsterType(bossName);
if (!mType) {
continue;
}
auto bossRace = mType->info.bosstiaryRace;
auto it = levelInfos.find(bossRace);
if (it != levelInfos.end()) {
const std::vector<LevelInfo> &infoForCurrentRace = it->second;
auto levelKills = infoForCurrentRace.at(level - 1).kills;
if (bossKills >= levelKills) {
unlockedMonsters.emplace(bossId);
}
} else {
g_logger().warn("[{}] boss with id {} and name {} not found in bossRace", __FUNCTION__, bossId, bossName);
}
}
return unlockedMonsters.data();
}
uint8_t IOBosstiary::getBossCurrentLevel(std::shared_ptr<Player> player, uint16_t bossId) const {
if (bossId == 0 || !player) {
return 0;
}
auto mType = getMonsterTypeByBossRaceId(bossId);
if (!mType) {
return 0;
}
uint32_t currentKills = player->getBestiaryKillCount(bossId);
auto bossRace = mType->info.bosstiaryRace;
uint8_t level = 0;
if (auto it = levelInfos.find(bossRace);
it != levelInfos.end()) {
const std::vector<LevelInfo> &infoForCurrentRace = it->second;
for (const auto &raceInfo : infoForCurrentRace) {
if (currentKills >= raceInfo.kills) {
++level;
}
}
} else {
g_logger().warn("[{}] boss with id {} and name {} not found in bossRace", __FUNCTION__, bossId, mType->name);
}
return level;
}
uint32_t IOBosstiary::calculteRemoveBoss(uint8_t removeTimes) const {
if (removeTimes < 2) {
return 0;
}
return 300000 * removeTimes - 500000;
}
const std::vector<LevelInfo> &IOBosstiary::getBossRaceKillStages(BosstiaryRarity_t race) const {
auto it = levelInfos.find(race);
if (it != levelInfos.end()) {
return it->second;
}
static std::vector<LevelInfo> emptyVector;
return emptyVector;
}