diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index d70d3455aab..abe845640e1 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -7084,7 +7084,8 @@ uint8_t Player::getLastWing() const { if (value > 0) { return value; } - return static_cast(kv()->get("last-wing")->get()); + auto wingOpt = kv()->get("last-wing"); + return static_cast(wingOpt ? wingOpt->get() : 0); } uint8_t Player::getCurrentWing() const { @@ -7108,16 +7109,23 @@ bool Player::hasAnyWing() const { uint8_t Player::getRandomWingId() const { std::vector availableWings; - const auto wings = g_game().attachedeffects->getWings(); + const auto &wings = g_game().attachedeffects->getWings(); for (const auto &wing : wings) { if (hasWing(wing)) { availableWings.emplace_back(wing->id); } } - const auto availableWingsSize = static_cast(availableWings.size() - 1); - const auto randomIndex = uniform_random(0, std::max(0, availableWingsSize)); - return availableWings.at(randomIndex); + if (availableWings.empty()) { + return 0; + } + + const auto randomIndex = uniform_random(0, static_cast(availableWings.size() - 1)); + if (randomIndex >= 0 && static_cast(randomIndex) < availableWings.size()) { + return availableWings[randomIndex]; + } + + return 0; } bool Player::toggleWing(bool wing) { @@ -7181,7 +7189,8 @@ bool Player::toggleWing(bool wing) { } bool Player::tameWing(uint8_t wingId) { - if (!g_game().attachedeffects->getWingByID(wingId)) { + const auto &wingPtr = g_game().attachedeffects->getWingByID(wingId); + if (!wingPtr) { return false; } @@ -7200,7 +7209,8 @@ bool Player::tameWing(uint8_t wingId) { } bool Player::untameWing(uint8_t wingId) { - if (!g_game().attachedeffects->getWingByID(wingId)) { + const auto &wingPtr = g_game().attachedeffects->getWingByID(wingId); + if (!wingPtr) { return false; } @@ -7244,7 +7254,6 @@ bool Player::hasWing(const std::shared_ptr &wing) const { } void Player::diswing() { - const auto &wing = g_game().attachedeffects->getWingByID(getCurrentWing()); defaultOutfit.lookWing = 0; } @@ -7255,7 +7264,8 @@ uint8_t Player::getLastAura() const { if (value > 0) { return value; } - return static_cast(kv()->get("last-aura")->get()); + auto auraOpt = kv()->get("last-aura"); + return static_cast(auraOpt ? auraOpt->get() : 0); } uint8_t Player::getCurrentAura() const { @@ -7279,16 +7289,23 @@ bool Player::hasAnyAura() const { uint8_t Player::getRandomAuraId() const { std::vector playerAuras; - const auto auras = g_game().attachedeffects->getAuras(); + const auto &auras = g_game().attachedeffects->getAuras(); for (const auto &aura : auras) { if (hasAura(aura)) { playerAuras.emplace_back(aura->id); } } - const auto playerAurasSize = static_cast(playerAuras.size() - 1); - const auto randomIndex = uniform_random(0, std::max(0, playerAurasSize)); - return playerAuras.at(randomIndex); + if (playerAuras.empty()) { + return 0; + } + + const auto randomIndex = uniform_random(0, static_cast(playerAuras.size() - 1)); + if (randomIndex >= 0 && static_cast(randomIndex) < playerAuras.size()) { + return playerAuras[randomIndex]; + } + + return 0; } bool Player::toggleAura(bool aura) { @@ -7352,7 +7369,8 @@ bool Player::toggleAura(bool aura) { } bool Player::tameAura(uint8_t auraId) { - if (!g_game().attachedeffects->getAuraByID(auraId)) { + const auto &auraPtr = g_game().attachedeffects->getAuraByID(auraId); + if (!auraPtr) { return false; } @@ -7371,7 +7389,8 @@ bool Player::tameAura(uint8_t auraId) { } bool Player::untameAura(uint8_t auraId) { - if (!g_game().attachedeffects->getAuraByID(auraId)) { + const auto &auraPtr = g_game().attachedeffects->getAuraByID(auraId); + if (!auraPtr) { return false; } @@ -7414,7 +7433,6 @@ bool Player::hasAura(const std::shared_ptr &aura) const { } void Player::disaura() { - const auto &aura = g_game().attachedeffects->getAuraByID(getCurrentAura()); defaultOutfit.lookAura = 0; } @@ -7425,7 +7443,8 @@ uint8_t Player::getLastEffect() const { if (value > 0) { return value; } - return static_cast(kv()->get("last-effect")->get()); + auto effectOpt = kv()->get("last-effect"); + return static_cast(effectOpt ? effectOpt->get() : 0); } uint8_t Player::getCurrentEffect() const { @@ -7449,16 +7468,23 @@ bool Player::hasAnyEffect() const { uint8_t Player::getRandomEffectId() const { std::vector playerEffects; - const auto effects = g_game().attachedeffects->getEffects(); + const auto &effects = g_game().attachedeffects->getEffects(); for (const auto &effect : effects) { if (hasEffect(effect)) { playerEffects.emplace_back(effect->id); } } - const auto playerEffectsSize = static_cast(playerEffects.size() - 1); - const auto randomIndex = uniform_random(0, std::max(0, playerEffectsSize)); - return playerEffects.at(randomIndex); + if (playerEffects.empty()) { + return 0; + } + + const auto randomIndex = uniform_random(0, static_cast(playerEffects.size() - 1)); + if (randomIndex >= 0 && static_cast(randomIndex) < playerEffects.size()) { + return playerEffects[randomIndex]; + } + + return 0; } bool Player::toggleEffect(bool effect) { @@ -7522,7 +7548,8 @@ bool Player::toggleEffect(bool effect) { } bool Player::tameEffect(uint8_t effectId) { - if (!g_game().attachedeffects->getEffectByID(effectId)) { + const auto &effectPtr = g_game().attachedeffects->getEffectByID(effectId); + if (!effectPtr) { return false; } @@ -7541,7 +7568,8 @@ bool Player::tameEffect(uint8_t effectId) { } bool Player::untameEffect(uint8_t effectId) { - if (!g_game().attachedeffects->getEffectByID(effectId)) { + const auto &effectPtr = g_game().attachedeffects->getEffectByID(effectId); + if (!effectPtr) { return false; } @@ -7585,7 +7613,6 @@ bool Player::hasEffect(const std::shared_ptr &effect) const { } void Player::diseffect() { - const auto &effect = g_game().attachedeffects->getEffectByID(getCurrentEffect()); defaultOutfit.lookEffect = 0; } @@ -7683,7 +7710,7 @@ bool Player::tameShader(uint16_t shaderId) { } bool Player::untameShader(uint16_t shaderId) { - auto shaderPtr = g_game().attachedeffects->getShaderByID(shaderId); + const auto &shaderPtr = g_game().attachedeffects->getShaderByID(shaderId); if (!shaderPtr) { return false; } @@ -7748,25 +7775,25 @@ bool Player::addCustomOutfit(const std::string &type, const std::variant(idOrName); if (type == "wing") { - auto element = g_game().attachedeffects->getWingByName(name); + const auto &element = g_game().attachedeffects->getWingByName(name); if (!element) { return false; } elementId = element->id; } else if (type == "aura") { - auto element = g_game().attachedeffects->getAuraByName(name); + const auto &element = g_game().attachedeffects->getAuraByName(name); if (!element) { return false; } elementId = element->id; } else if (type == "effect") { - auto element = g_game().attachedeffects->getEffectByName(name); + const auto &element = g_game().attachedeffects->getEffectByName(name); if (!element) { return false; } elementId = element->id; } else if (type == "shader") { - auto element = g_game().attachedeffects->getShaderByName(name); + const auto &element = g_game().attachedeffects->getShaderByName(name); if (!element) { return false; } @@ -7797,25 +7824,25 @@ bool Player::removeCustomOutfit(const std::string &type, const std::variant(idOrName); if (type == "wings") { - auto element = g_game().attachedeffects->getWingByName(name); + const auto &element = g_game().attachedeffects->getWingByName(name); if (!element) { return false; } elementId = element->id; } else if (type == "aura") { - auto element = g_game().attachedeffects->getAuraByName(name); + const auto &element = g_game().attachedeffects->getAuraByName(name); if (!element) { return false; } elementId = element->id; } else if (type == "effect") { - auto element = g_game().attachedeffects->getEffectByName(name); + const auto &element = g_game().attachedeffects->getEffectByName(name); if (!element) { return false; } elementId = element->id; } else if (type == "shader") { - auto element = g_game().attachedeffects->getShaderByName(name); + const auto &element = g_game().attachedeffects->getShaderByName(name); if (!element) { return false; } diff --git a/src/creatures/players/player.hpp b/src/creatures/players/player.hpp index 7862960c9e3..465751fbec3 100644 --- a/src/creatures/players/player.hpp +++ b/src/creatures/players/player.hpp @@ -1455,9 +1455,6 @@ class Player final : public Creature, public Cylinder, public Bankable { std::vector quickLootListItemIds; std::vector outfits; - std::unordered_set wings; - std::unordered_set auras; - std::unordered_set effects; std::unordered_set shaders; std::vector familiars; diff --git a/src/game/game.cpp b/src/game/game.cpp index d139544b32c..70bab810f74 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -6195,7 +6195,7 @@ void Game::playerChangeOutfit(uint32_t playerId, Outfit_t outfit, uint8_t isMoun // @ wings if (outfit.lookWing != 0) { - const auto wing = attachedeffects->getWingByID(outfit.lookWing); + const auto &wing = attachedeffects->getWingByID(outfit.lookWing); if (!wing) { return; } @@ -6213,7 +6213,7 @@ void Game::playerChangeOutfit(uint32_t playerId, Outfit_t outfit, uint8_t isMoun // @ // @ Effect if (outfit.lookEffect != 0) { - const auto effect = attachedeffects->getEffectByID(outfit.lookEffect); + const auto &effect = attachedeffects->getEffectByID(outfit.lookEffect); if (!effect) { return; } @@ -6231,7 +6231,7 @@ void Game::playerChangeOutfit(uint32_t playerId, Outfit_t outfit, uint8_t isMoun // @ // @ Aura if (outfit.lookAura != 0) { - const auto aura = attachedeffects->getAuraByID(outfit.lookAura); + const auto &aura = attachedeffects->getAuraByID(outfit.lookAura); if (!aura) { return; } @@ -6248,7 +6248,7 @@ void Game::playerChangeOutfit(uint32_t playerId, Outfit_t outfit, uint8_t isMoun // @ /// shaders if (outfit.lookShader != 0) { - const auto shaderPtr = attachedeffects->getShaderByID(outfit.lookShader); + const auto &shaderPtr = attachedeffects->getShaderByID(outfit.lookShader); if (!shaderPtr) { return; } @@ -11107,10 +11107,13 @@ void Game::playerSetTyping(uint32_t playerId, uint8_t typing) { } void Game::refreshItem(const std::shared_ptr &item) { - if (!item || !item->getParent()) { + if (!item) { return; } const auto &parent = item->getParent(); + if (!parent) { + return; + } if (const auto &creature = parent->getCreature()) { if (const auto &player = creature->getPlayer()) { int32_t index = player->getThingIndex(item); @@ -11126,7 +11129,11 @@ void Game::refreshItem(const std::shared_ptr &item) { const auto spectators = Spectators().find(container->getPosition(), false, 2, 2, 2, 2); // send to client for (const auto &spectator : spectators) { - spectator->getPlayer()->sendUpdateContainerItem(container, static_cast(index), item); + const auto &player = spectator->getPlayer(); + if (!player) { + continue; + } + player->sendUpdateContainerItem(container, static_cast(index), item); } } return; @@ -11135,7 +11142,11 @@ void Game::refreshItem(const std::shared_ptr &item) { const auto spectators = Spectators().find(tile->getPosition(), true); // send to client for (const auto &spectator : spectators) { - spectator->getPlayer()->sendUpdateTileItem(tile, tile->getPosition(), item); + const auto &player = spectator->getPlayer(); + if (!player) { + continue; + } + player->sendUpdateTileItem(tile, tile->getPosition(), item); } return; } diff --git a/src/lua/functions/creatures/creature_functions.cpp b/src/lua/functions/creatures/creature_functions.cpp index 60862f3f1a3..f152df6918c 100644 --- a/src/lua/functions/creatures/creature_functions.cpp +++ b/src/lua/functions/creatures/creature_functions.cpp @@ -1197,7 +1197,6 @@ int CreatureFunctions::luaCreatureAttachEffectById(lua_State* L) { const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - Lua::pushBoolean(L, false); return 1; } uint16_t id = Lua::getNumber(L, 2); @@ -1215,7 +1214,6 @@ int CreatureFunctions::luaCreatureDetachEffectById(lua_State* L) { const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - Lua::pushBoolean(L, false); return 1; } uint16_t id = Lua::getNumber(L, 2); @@ -1228,7 +1226,6 @@ int CreatureFunctions::luaCreatureGetAttachedEffects(lua_State* L) { const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - lua_pushnil(L); return 1; } @@ -1246,7 +1243,6 @@ int CreatureFunctions::luaCreatureGetShader(lua_State* L) { const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - Lua::pushBoolean(L, false); return 1; } @@ -1260,7 +1256,6 @@ int CreatureFunctions::luaCreatureSetShader(lua_State* L) { const auto &creature = Lua::getUserdataShared(L, 1); if (!creature) { Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); - Lua::pushBoolean(L, false); return 1; } creature->setShader(Lua::getString(L, 2)); diff --git a/src/server/network/protocol/protocolgame.cpp b/src/server/network/protocol/protocolgame.cpp index ddd47191139..c1849d0f937 100644 --- a/src/server/network/protocol/protocolgame.cpp +++ b/src/server/network/protocol/protocolgame.cpp @@ -9527,7 +9527,7 @@ void ProtocolGame::sendOutfitWindowCustomOTCR(NetworkMessage &msg) { uint16_t limitWings = std::numeric_limits::max(); uint16_t wingSize = 0; msg.skipBytes(1); - const auto wings = g_game().attachedeffects->getWings(); + const auto &wings = g_game().attachedeffects->getWings(); for (const auto &wing : wings) { if (player->hasWing(wing)) { msg.add(wing->id); @@ -9547,7 +9547,7 @@ void ProtocolGame::sendOutfitWindowCustomOTCR(NetworkMessage &msg) { uint16_t limitAuras = std::numeric_limits::max(); uint16_t auraSize = 0; msg.skipBytes(1); - const auto auras = g_game().attachedeffects->getAuras(); + const auto &auras = g_game().attachedeffects->getAuras(); for (const auto &aura : auras) { if (player->hasAura(aura)) { msg.add(aura->id); @@ -9567,7 +9567,7 @@ void ProtocolGame::sendOutfitWindowCustomOTCR(NetworkMessage &msg) { uint16_t limitEffects = std::numeric_limits::max(); uint16_t effectSize = 0; msg.skipBytes(1); - const auto effects = g_game().attachedeffects->getEffects(); + const auto &effects = g_game().attachedeffects->getEffects(); for (const auto &effect : effects) { if (player->hasEffect(effect)) { msg.add(effect->id);