Skip to content

Commit

Permalink
improve: change hardcoded blessings to magic enum (opentibiabr#2804)
Browse files Browse the repository at this point in the history
• Removed an unnecessary map in the game, moved to magic enum (extracts
directly from the enum name)
• Removed the amount of hardcoded bless for the enum count

Resolves opentibiabr#2745
Close opentibiabr#2787
  • Loading branch information
dudantas authored Aug 9, 2024
1 parent 1d6b6d1 commit 193939c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 65 deletions.
9 changes: 6 additions & 3 deletions src/creatures/players/cyclopedia/player_cyclopedia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@

#include "pch.hpp"

#include "player_cyclopedia.hpp"

#include "database/databasetasks.hpp"
#include "creatures/players/player.hpp"
#include "player_cyclopedia.hpp"
#include "game/game.hpp"
#include "kv/kv.hpp"

#include "enums/player_blessings.hpp"

PlayerCyclopedia::PlayerCyclopedia(Player &player) :
m_player(player) { }

Expand Down Expand Up @@ -139,8 +142,8 @@ void PlayerCyclopedia::updateStoreSummary(uint8_t type, uint16_t amount, const s
insertValue(type, amount, id);
break;
case Summary_t::ALL_BLESSINGS:
for (int i = 1; i < 8; ++i) {
insertValue(static_cast<uint8_t>(Summary_t::BLESSINGS), amount, fmt::format("{}", i));
for (auto blessIt : magic_enum::enum_values<Blessings>()) {
insertValue(static_cast<uint8_t>(Summary_t::BLESSINGS), amount, fmt::format("{}", blessIt));
}
break;
default:
Expand Down
38 changes: 15 additions & 23 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "enums/account_errors.hpp"
#include "enums/account_type.hpp"
#include "enums/account_group_type.hpp"
#include "enums/player_blessings.hpp"

MuteCountMap Player::muteCountMap;

Expand Down Expand Up @@ -6649,33 +6650,24 @@ void Player::initializeTaskHunting() {
}

std::string Player::getBlessingsName() const {
uint8_t count = 0;
std::for_each(blessings.begin(), blessings.end(), [&count](uint8_t amount) {
if (amount != 0) {
count++;
std::vector<std::string> blessingNames;
for (auto bless : magic_enum::enum_values<Blessings>()) {
if (hasBlessing(enumToValue(bless))) {
std::string name = toStartCaseWithSpace(magic_enum::enum_name(bless).data());
blessingNames.emplace_back(name);
}
});
}

auto BlessingNames = g_game().getBlessingNames();
std::ostringstream os;
for (uint8_t i = 1; i <= 8; i++) {
if (hasBlessing(i)) {
if (auto blessName = BlessingNames.find(static_cast<Blessings_t>(i));
blessName != BlessingNames.end()) {
os << (*blessName).second;
} else {
continue;
}

--count;
if (count > 1) {
os << ", ";
} else if (count == 1) {
os << " and ";
} else {
os << ".";
}
if (!blessingNames.empty()) {
// Join all elements but the last with ", " and add the last one with " and "
for (size_t i = 0; i < blessingNames.size() - 1; ++i) {
os << blessingNames[i] << ", ";
}
if (blessingNames.size() > 1) {
os << "and ";
}
os << blessingNames.back() << ".";
}

return os.str();
Expand Down
25 changes: 25 additions & 0 deletions src/enums/player_blessings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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/
*/

#pragma once

#ifndef USE_PRECOMPILED_HEADERS
#include <cstdint>
#endif

enum class Blessings : uint8_t {
TwistOfFate = 1,
TheWisdomOfSolitude = 2,
TheSparkOfThePhoenix = 3,
TheFireOfTheSuns = 4,
TheSpiritualShielding = 5,
TheEmbraceOfTibia = 6,
BloodOfTheMountain = 7,
HearthOfTheMountain = 8
};
16 changes: 1 addition & 15 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "enums/account_group_type.hpp"
#include "enums/account_errors.hpp"
#include "enums/account_coins.hpp"
#include "enums/player_blessings.hpp"

#include <appearances.pb.h>

Expand Down Expand Up @@ -364,17 +365,6 @@ Game::Game() {
HighscoreCategory("Magic Level", static_cast<uint8_t>(HighscoreCategories_t::MAGIC_LEVEL))
};

m_blessingNames = {
{ static_cast<uint8_t>(TWIST_OF_FATE), "Twist of Fate" },
{ static_cast<uint8_t>(WISDOM_OF_SOLITUDE), "The Wisdom of Solitude" },
{ static_cast<uint8_t>(SPARK_OF_THE_PHOENIX), "The Spark of the Phoenix" },
{ static_cast<uint8_t>(FIRE_OF_THE_SUNS), "The Fire of the Suns" },
{ static_cast<uint8_t>(SPIRITUAL_SHIELDING), "The Spiritual Shielding" },
{ static_cast<uint8_t>(EMBRACE_OF_TIBIA), "The Embrace of Tibia" },
{ static_cast<uint8_t>(BLOOD_OF_THE_MOUNTAIN), "Blood of the Mountain" },
{ static_cast<uint8_t>(HEARTH_OF_THE_MOUNTAIN), "Heart of the Mountain" },
};

m_summaryCategories = {
{ static_cast<uint8_t>(Summary_t::HOUSE_ITEMS), "house-items" },
{ static_cast<uint8_t>(Summary_t::BOOSTS), "xp-boosts" },
Expand Down Expand Up @@ -10668,10 +10658,6 @@ const std::string &Game::getSummaryKeyByType(uint8_t type) {
return m_summaryCategories[type];
}

const std::map<uint8_t, std::string> &Game::getBlessingNames() {
return m_blessingNames;
}

const std::unordered_map<uint16_t, std::string> &Game::getHirelingSkills() {
return m_hirelingSkills;
}
Expand Down
2 changes: 0 additions & 2 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,6 @@ class Game {
std::vector<HighscoreCategory> m_highscoreCategories;
std::unordered_map<uint8_t, std::string> m_highscoreCategoriesNames;

std::map<uint8_t, std::string> m_blessingNames;

std::unordered_map<uint8_t, std::string> m_summaryCategories;
std::unordered_map<uint16_t, std::string> m_hirelingSkills;
std::unordered_map<uint16_t, std::string> m_hirelingOutfits;
Expand Down
11 changes: 0 additions & 11 deletions src/game/game_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,6 @@ enum class HighscoreCategories_t : uint8_t {
BOSS_POINTS = 14,
};

enum Blessings_t : uint8_t {
TWIST_OF_FATE = 1,
WISDOM_OF_SOLITUDE = 2,
SPARK_OF_THE_PHOENIX = 3,
FIRE_OF_THE_SUNS = 4,
SPIRITUAL_SHIELDING = 5,
EMBRACE_OF_TIBIA = 6,
BLOOD_OF_THE_MOUNTAIN = 7,
HEARTH_OF_THE_MOUNTAIN = 8,
};

enum HighscoreType_t : uint8_t {
HIGHSCORE_GETENTRIES = 0,
HIGHSCORE_OURRANK = 1
Expand Down
25 changes: 14 additions & 11 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "enums/account_type.hpp"
#include "enums/account_group_type.hpp"
#include "enums/account_coins.hpp"
#include "enums/player_blessings.hpp"

#include "creatures/players/highscore_category.hpp"

Expand Down Expand Up @@ -3576,15 +3577,14 @@ void ProtocolGame::sendCyclopediaCharacterCombatStats() {
msg.add<uint16_t>(static_cast<uint16_t>(player->getReflectFlat(COMBAT_PHYSICALDAMAGE)));

uint8_t haveBlesses = 0;
const uint8_t blessings = 8;
for (uint8_t i = 1; i < blessings; ++i) {
if (player->hasBlessing(i)) {
for (auto bless : magic_enum::enum_values<Blessings>()) {
if (player->hasBlessing(enumToValue(bless))) {
++haveBlesses;
}
}

msg.addByte(haveBlesses);
msg.addByte(blessings);
msg.addByte(magic_enum::enum_count<Blessings>());

std::shared_ptr<Item> weapon = player->getWeapon();
if (weapon) {
Expand Down Expand Up @@ -3997,13 +3997,16 @@ void ProtocolGame::sendCyclopediaCharacterStoreSummary() {

auto cyclopediaSummary = player->cyclopedia()->getSummary();

// getBlessingsObtained
auto blessingNames = g_game().getBlessingNames();
msg.addByte(static_cast<uint8_t>(blessingNames.size()));
for (const auto &bless : blessingNames) {
msg.addString(bless.second, "ProtocolGame::sendCyclopediaCharacterStoreSummary - blessing.name");
uint8_t blessingIndex = bless.first - 1;
msg.addByte((blessingIndex < player->blessings.size()) ? static_cast<uint16_t>(player->blessings[blessingIndex]) : 0);
msg.addByte(static_cast<uint8_t>(magic_enum::enum_count<Blessings>()));
for (auto bless : magic_enum::enum_values<Blessings>()) {
std::string name = toStartCaseWithSpace(magic_enum::enum_name(bless).data());
msg.addString(name, "ProtocolGame::sendCyclopediaCharacterStoreSummary - blessing.name");
auto blessValue = enumToValue(bless);
if (player->hasBlessing(blessValue)) {
msg.addByte(static_cast<uint16_t>(player->blessings[blessValue - 1]));
} else {
msg.addByte(0x00);
}
}

uint8_t preySlotsUnlocked = 0;
Expand Down

0 comments on commit 193939c

Please sign in to comment.