Skip to content

Commit

Permalink
Merge branch 'main' into refactor-sendSaleItemList
Browse files Browse the repository at this point in the history
  • Loading branch information
Luan Luciano authored Jun 13, 2024
2 parents 2829d8f + 7c3393a commit 9917293
Show file tree
Hide file tree
Showing 25 changed files with 319 additions and 166 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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ local createMonster = TalkAction("/m")
-- @param param: String containing the command parameters.
-- Format: "/m monstername, monstercount, [fiendish/influenced level], spawnRadius, [forceCreate]"
-- Example: "/m rat, 10, fiendish, 5, true"
-- @param: the last param is by default "false", if add "," or any value i'ts set to true
-- @param: the last param is by default "false", if add "," or any value it's set to true
-- @return true if the command is executed successfully, false otherwise.
function createMonster.onSay(player, words, param)
-- create log
Expand Down Expand Up @@ -127,3 +127,33 @@ end
createMonster:separator(" ")
createMonster:groupType("god")
createMonster:register()

----------------- Rename monster name -----------------
local setMonsterName = TalkAction("/setmonstername")

-- @function setMonsterName.onSay
-- @desc TalkAction to rename nearby monsters within a radius of 4 sqm.
-- Format: "/setmonstername newName"
function setMonsterName.onSay(player, words, param)
if param == "" then
player:sendCancelMessage("Command param required.")
return true
end

local split = param:split(",")
local monsterNewName = split[1]

local spectators, spectator = Game.getSpectators(player:getPosition(), false, false, 4, 4, 4, 4)
for i = 1, #spectators do
spectator = spectators[i]
if spectator:isMonster() then
spectator:setName(monsterNewName)
end
end

return true
end

setMonsterName:separator(" ")
setMonsterName:groupType("god")
setMonsterName:register()
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
33 changes: 32 additions & 1 deletion src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ std::shared_ptr<Monster> Monster::createMonster(const std::string &name) {

Monster::Monster(const std::shared_ptr<MonsterType> mType) :
Creature(),
strDescription(asLowerCaseString(mType->nameDescription)),
nameDescription(asLowerCaseString(mType->nameDescription)),
mType(mType) {
defaultOutfit = mType->info.outfit;
currentOutfit = mType->info.outfit;
Expand Down Expand Up @@ -64,6 +64,37 @@ void Monster::removeList() {
g_game().removeMonster(static_self_cast<Monster>());
}

const std::string &Monster::getName() const {
if (name.empty()) {
return mType->name;
}
return name;
}

void Monster::setName(const std::string &name) {
if (getName() == name) {
return;
}

this->name = name;

// NOTE: Due to how client caches known creatures,
// it is not feasible to send creature update to everyone that has ever met it
auto spectators = Spectators().find<Player>(position, true);
for (const auto &spectator : spectators) {
if (const auto &tmpPlayer = spectator->getPlayer()) {
tmpPlayer->sendUpdateTileCreature(static_self_cast<Monster>());
}
}
}

const std::string &Monster::getNameDescription() const {
if (nameDescription.empty()) {
return mType->nameDescription;
}
return nameDescription;
}

bool Monster::canWalkOnFieldType(CombatType_t combatType) const {
switch (combatType) {
case COMBAT_ENERGYDAMAGE:
Expand Down
20 changes: 11 additions & 9 deletions src/creatures/monsters/monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,22 @@ class Monster final : public Creature {
}
}

void removeList() override;
void addList() override;
void removeList() override;

const std::string &getName() const override;
void setName(const std::string &name);

const std::string &getName() const override {
return mType->name;
}
// Real monster name, set on monster creation "createMonsterType(typeName)"
const std::string &getTypeName() const override {
return mType->typeName;
}
const std::string &getNameDescription() const override {
return mType->nameDescription;
}
const std::string &getNameDescription() const override;
void setNameDescription(const std::string &nameDescription) {
this->nameDescription = nameDescription;
};
std::string getDescription(int32_t) override {
return strDescription + '.';
return nameDescription + '.';
}

CreatureType_t getType() const override {
Expand Down Expand Up @@ -363,7 +364,8 @@ class Monster final : public Creature {
uint16_t forgeStack = 0;
ForgeClassifications_t monsterForgeClassification = ForgeClassifications_t::FORGE_NORMAL_MONSTER;

std::string strDescription;
std::string name;
std::string nameDescription;

std::shared_ptr<MonsterType> mType;
SpawnMonster* spawnMonster = nullptr;
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
Loading

0 comments on commit 9917293

Please sign in to comment.