Skip to content

Commit

Permalink
feat: shared_ptr: familiar, groups and reload: familiars, outfits (#2556
Browse files Browse the repository at this point in the history
)
  • Loading branch information
beats-dh authored Jun 12, 2024
1 parent 5d0d1c9 commit 201aa8c
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 142 deletions.
1 change: 1 addition & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"DEBUG_LOG": "ON",
"ASAN_ENABLED": "OFF",
"BUILD_STATIC_LIBRARY": "OFF",
"SPEED_UP_BUILD_UNITY": "OFF",
"VCPKG_TARGET_TRIPLET": "x64-windows"
}
},
Expand Down
3 changes: 3 additions & 0 deletions data/scripts/talkactions/god/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local reloadTypes = {
["configuration"] = RELOAD_TYPE_CONFIG,
["core"] = RELOAD_TYPE_CORE,
["events"] = RELOAD_TYPE_EVENTS,
["familiar"] = RELOAD_TYPE_FAMILIARS,
["global"] = RELOAD_TYPE_CORE,
["group"] = RELOAD_TYPE_GROUPS,
["groups"] = RELOAD_TYPE_GROUPS,
Expand All @@ -20,6 +21,8 @@ local reloadTypes = {
["monsters"] = RELOAD_TYPE_MONSTERS,
["mount"] = RELOAD_TYPE_MOUNTS,
["mounts"] = RELOAD_TYPE_MOUNTS,
["outfit"] = RELOAD_TYPE_OUTFITS,
["outfits"] = RELOAD_TYPE_OUTFITS,
["npc"] = RELOAD_TYPE_NPCS,
["npcs"] = RELOAD_TYPE_NPCS,
["raid"] = RELOAD_TYPE_RAIDS,
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/appearance/mounts/mounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool Mounts::loadFromXml() {
}

for (auto mountNode : doc.child("mounts").children()) {
uint16_t lookType = pugi::cast<uint16_t>(mountNode.attribute("clientid").value());
auto lookType = pugi::cast<uint16_t>(mountNode.attribute("clientid").value());
if (g_configManager().getBoolean(WARN_UNSAFE_SCRIPTS, __FUNCTION__) && lookType != 0 && !g_game().isLookTypeRegistered(lookType)) {
g_logger().warn("{} - An unregistered creature mount with id '{}' was blocked to prevent client crash.", __FUNCTION__, lookType);
continue;
Expand Down
37 changes: 22 additions & 15 deletions src/creatures/appearance/outfit/outfit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include "utils/tools.hpp"
#include "game/game.hpp"

Outfits &Outfits::getInstance() {
return inject<Outfits>();
}

bool Outfits::reload() {
for (auto &outfitsVector : outfits) {
outfitsVector.clear();
Expand Down Expand Up @@ -41,7 +45,7 @@ bool Outfits::loadFromXml() {
continue;
}

uint16_t type = pugi::cast<uint16_t>(attr.value());
auto type = pugi::cast<uint16_t>(attr.value());
if (type > PLAYERSEX_LAST) {
g_logger().warn("[Outfits::loadFromXml] - Invalid outfit type {}", type);
continue;
Expand All @@ -53,7 +57,7 @@ bool Outfits::loadFromXml() {
continue;
}

if (uint16_t lookType = pugi::cast<uint16_t>(lookTypeAttribute.value());
if (auto lookType = pugi::cast<uint16_t>(lookTypeAttribute.value());
g_configManager().getBoolean(WARN_UNSAFE_SCRIPTS, __FUNCTION__) && lookType != 0
&& !g_game().isLookTypeRegistered(lookType)) {
g_logger().warn("[Outfits::loadFromXml] An unregistered creature looktype type with id '{}' was ignored to prevent client crash.", lookType);
Expand All @@ -74,7 +78,22 @@ bool Outfits::loadFromXml() {
return true;
}

std::shared_ptr<Outfit> Outfits::getOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const {
std::shared_ptr<Outfit> Outfits::getOutfitByLookType(const std::shared_ptr<const Player> &player, uint16_t lookType, bool isOppositeOutfit) const {
if (!player) {
g_logger().error("[{}] - Player not found", __FUNCTION__);
return nullptr;
}

auto sex = player->getSex();
if (sex != PLAYERSEX_FEMALE && sex != PLAYERSEX_MALE) {
g_logger().error("[{}] - Sex invalid or player: {}", __FUNCTION__, player->getName());
return nullptr;
}

if (isOppositeOutfit) {
sex = (sex == PLAYERSEX_MALE) ? PLAYERSEX_FEMALE : PLAYERSEX_MALE;
}

auto it = std::ranges::find_if(outfits[sex], [&lookType](const auto &outfit) {
return outfit->lookType == lookType;
});
Expand All @@ -84,15 +103,3 @@ std::shared_ptr<Outfit> Outfits::getOutfitByLookType(PlayerSex_t sex, uint16_t l
}
return nullptr;
}

/**
* Get the oposite sex equivalent outfit
* @param sex current sex
* @param lookType current looktype
* @return <b>const</b> pointer to the outfit or <b>nullptr</b> if it could not be found.
*/

std::shared_ptr<Outfit> Outfits::getOpositeSexOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const {
PlayerSex_t searchSex = (sex == PLAYERSEX_MALE) ? PLAYERSEX_FEMALE : PLAYERSEX_MALE;
return getOutfitByLookType(searchSex, lookType);
}
14 changes: 6 additions & 8 deletions src/creatures/appearance/outfit/outfit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
#include "declarations.hpp"
#include "lib/di/container.hpp"

class Player;

struct OutfitEntry {
constexpr OutfitEntry(uint16_t initLookType, uint8_t initAddons) :
constexpr explicit OutfitEntry(uint16_t initLookType, uint8_t initAddons) :
lookType(initLookType), addons(initAddons) { }

uint16_t lookType;
Expand Down Expand Up @@ -42,16 +44,12 @@ struct ProtocolOutfit {

class Outfits {
public:
static Outfits &getInstance() {
return inject<Outfits>();
}

std::shared_ptr<Outfit> getOpositeSexOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const;
static Outfits &getInstance();

bool loadFromXml();
bool reload();
bool loadFromXml();

[[nodiscard]] std::shared_ptr<Outfit> getOutfitByLookType(PlayerSex_t sex, uint16_t lookType) const;
[[nodiscard]] std::shared_ptr<Outfit> getOutfitByLookType(const std::shared_ptr<const Player> &player, uint16_t lookType, bool isOppositeOutfit = false) const;
[[nodiscard]] const std::vector<std::shared_ptr<Outfit>> &getOutfits(PlayerSex_t sex) const {
return outfits[sex];
}
Expand Down
7 changes: 6 additions & 1 deletion src/creatures/players/grouping/familiars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
#include "pch.hpp"

#include "creatures/players/grouping/familiars.hpp"
#include "lib/di/container.hpp"
#include "config/configmanager.hpp"
#include "utils/pugicast.hpp"
#include "utils/tools.hpp"

Familiars &Familiars::getInstance() {
return inject<Familiars>();
}

bool Familiars::reload() {
for (auto &familiarsVector : familiars) {
familiarsVector.clear();
Expand Down Expand Up @@ -42,7 +47,7 @@ bool Familiars::loadFromXml() {
continue;
}

uint16_t vocation = pugi::cast<uint16_t>(attr.value());
auto vocation = pugi::cast<uint16_t>(attr.value());
if (vocation > VOCATION_LAST) {
g_logger().warn("[Familiars::loadFromXml] - Invalid familiar vocation {}", vocation);
continue;
Expand Down
5 changes: 2 additions & 3 deletions src/creatures/players/grouping/familiars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#pragma once

#include "declarations.hpp"
#include "lib/di/container.hpp"

struct FamiliarEntry {
Expand All @@ -32,9 +33,7 @@ struct Familiar {

class Familiars {
public:
static Familiars &getInstance() {
return inject<Familiars>();
}
static Familiars &getInstance();

bool loadFromXml();
bool reload();
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/players/grouping/groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ PlayerFlags_t Groups::getFlagFromNumber(uint8_t value) {
return magic_enum::enum_value<PlayerFlags_t>(value);
}

bool Groups::reload() const {
bool Groups::reload() {
// Clear groups
g_game().groups.getGroups().clear();
return g_game().groups.load();
Expand Down Expand Up @@ -99,7 +99,7 @@ bool Groups::load() {
return true;
}

std::shared_ptr<Group> Groups::getGroup(uint16_t id) {
std::shared_ptr<Group> Groups::getGroup(uint16_t id) const {
if (auto it = std::find_if(groups_vector.begin(), groups_vector.end(), [id](auto group_it) {
return group_it->id == id;
});
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/players/grouping/groups.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class Groups {
public:
static uint8_t getFlagNumber(PlayerFlags_t playerFlags);
static PlayerFlags_t getFlagFromNumber(uint8_t value);
bool reload() const;
static bool reload();
bool load();
std::shared_ptr<Group> getGroup(uint16_t id);
[[nodiscard]] std::shared_ptr<Group> getGroup(uint16_t id) const;
std::vector<std::shared_ptr<Group>> &getGroups() {
return groups_vector;
}
Expand Down
6 changes: 3 additions & 3 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4914,7 +4914,7 @@ bool Player::canWear(uint16_t lookType, uint8_t addons) const {
return true;
}

const auto &outfit = Outfits::getInstance().getOutfitByLookType(sex, lookType);
const auto &outfit = Outfits::getInstance().getOutfitByLookType(getPlayer(), lookType);
if (!outfit) {
return false;
}
Expand Down Expand Up @@ -5001,7 +5001,7 @@ bool Player::removeOutfitAddon(uint16_t lookType, uint8_t addons) {
return false;
}

bool Player::getOutfitAddons(const std::shared_ptr<Outfit> outfit, uint8_t &addons) const {
bool Player::getOutfitAddons(const std::shared_ptr<Outfit> &outfit, uint8_t &addons) const {
if (group->access) {
addons = 3;
return true;
Expand Down Expand Up @@ -5826,7 +5826,7 @@ bool Player::toggleMount(bool mount) {
return false;
}

const auto &playerOutfit = Outfits::getInstance().getOutfitByLookType(getSex(), defaultOutfit.lookType);
const auto &playerOutfit = Outfits::getInstance().getOutfitByLookType(getPlayer(), defaultOutfit.lookType);
if (!playerOutfit) {
return false;
}
Expand Down
11 changes: 8 additions & 3 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,12 @@ class Player final : public Creature, public Cylinder, public Bankable {
return blessings[index - 1] != 0;
}
uint8_t getBlessingCount(uint8_t index) const {
return blessings[index - 1];
if (index > 0 && index <= blessings.size()) {
return blessings[index - 1];
} else {
g_logger().error("[{}] - index outside range 0-10.", __FUNCTION__);
return 0;
}
}
std::string getBlessingsName() const;

Expand Down Expand Up @@ -1031,7 +1036,7 @@ class Player final : public Creature, public Cylinder, public Bankable {
void addOutfit(uint16_t lookType, uint8_t addons);
bool removeOutfit(uint16_t lookType);
bool removeOutfitAddon(uint16_t lookType, uint8_t addons);
bool getOutfitAddons(const std::shared_ptr<Outfit> outfit, uint8_t &addons) const;
bool getOutfitAddons(const std::shared_ptr<Outfit> &outfit, uint8_t &addons) const;

bool canFamiliar(uint16_t lookType) const;
void addFamiliar(uint16_t lookType);
Expand Down Expand Up @@ -2853,7 +2858,7 @@ class Player final : public Creature, public Cylinder, public Bankable {

uint16_t lastStatsTrainingTime = 0;
uint16_t staminaMinutes = 2520;
std::vector<uint8_t> blessings = { 0, 0, 0, 0, 0, 0, 0, 0 };
std::vector<uint8_t> blessings = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
uint16_t maxWriteLen = 0;
uint16_t baseXpGain = 100;
uint16_t voucherXpBoost = 0;
Expand Down
Loading

0 comments on commit 201aa8c

Please sign in to comment.