Skip to content

Commit

Permalink
review: dudantas part 1/2
Browse files Browse the repository at this point in the history
  • Loading branch information
kokekanon committed Jan 11, 2025
1 parent d27ae12 commit c1f349e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 51 deletions.
93 changes: 60 additions & 33 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7084,7 +7084,8 @@ uint8_t Player::getLastWing() const {
if (value > 0) {
return value;
}
return static_cast<uint8_t>(kv()->get("last-wing")->get<int>());
auto wingOpt = kv()->get("last-wing");
return static_cast<uint8_t>(wingOpt ? wingOpt->get<int>() : 0);
}

uint8_t Player::getCurrentWing() const {
Expand All @@ -7108,16 +7109,23 @@ bool Player::hasAnyWing() const {

uint8_t Player::getRandomWingId() const {
std::vector<uint8_t> 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<int32_t>(availableWings.size() - 1);
const auto randomIndex = uniform_random(0, std::max<int32_t>(0, availableWingsSize));
return availableWings.at(randomIndex);
if (availableWings.empty()) {
return 0;
}

const auto randomIndex = uniform_random(0, static_cast<int32_t>(availableWings.size() - 1));
if (randomIndex >= 0 && static_cast<size_t>(randomIndex) < availableWings.size()) {
return availableWings[randomIndex];
}

return 0;
}

bool Player::toggleWing(bool wing) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -7244,7 +7254,6 @@ bool Player::hasWing(const std::shared_ptr<Wing> &wing) const {
}

void Player::diswing() {
const auto &wing = g_game().attachedeffects->getWingByID(getCurrentWing());
defaultOutfit.lookWing = 0;
}

Expand All @@ -7255,7 +7264,8 @@ uint8_t Player::getLastAura() const {
if (value > 0) {
return value;
}
return static_cast<uint8_t>(kv()->get("last-aura")->get<int>());
auto auraOpt = kv()->get("last-aura");
return static_cast<uint8_t>(auraOpt ? auraOpt->get<int>() : 0);
}

uint8_t Player::getCurrentAura() const {
Expand All @@ -7279,16 +7289,23 @@ bool Player::hasAnyAura() const {

uint8_t Player::getRandomAuraId() const {
std::vector<uint8_t> 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<int32_t>(playerAuras.size() - 1);
const auto randomIndex = uniform_random(0, std::max<int32_t>(0, playerAurasSize));
return playerAuras.at(randomIndex);
if (playerAuras.empty()) {
return 0;
}

const auto randomIndex = uniform_random(0, static_cast<int32_t>(playerAuras.size() - 1));
if (randomIndex >= 0 && static_cast<size_t>(randomIndex) < playerAuras.size()) {
return playerAuras[randomIndex];
}

return 0;
}

bool Player::toggleAura(bool aura) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -7414,7 +7433,6 @@ bool Player::hasAura(const std::shared_ptr<Aura> &aura) const {
}

void Player::disaura() {
const auto &aura = g_game().attachedeffects->getAuraByID(getCurrentAura());
defaultOutfit.lookAura = 0;
}

Expand All @@ -7425,7 +7443,8 @@ uint8_t Player::getLastEffect() const {
if (value > 0) {
return value;
}
return static_cast<uint8_t>(kv()->get("last-effect")->get<int>());
auto effectOpt = kv()->get("last-effect");
return static_cast<uint8_t>(effectOpt ? effectOpt->get<int>() : 0);
}

uint8_t Player::getCurrentEffect() const {
Expand All @@ -7449,16 +7468,23 @@ bool Player::hasAnyEffect() const {

uint8_t Player::getRandomEffectId() const {
std::vector<uint8_t> 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<int32_t>(playerEffects.size() - 1);
const auto randomIndex = uniform_random(0, std::max<int32_t>(0, playerEffectsSize));
return playerEffects.at(randomIndex);
if (playerEffects.empty()) {
return 0;
}

const auto randomIndex = uniform_random(0, static_cast<int32_t>(playerEffects.size() - 1));
if (randomIndex >= 0 && static_cast<size_t>(randomIndex) < playerEffects.size()) {
return playerEffects[randomIndex];
}

return 0;
}

bool Player::toggleEffect(bool effect) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -7585,7 +7613,6 @@ bool Player::hasEffect(const std::shared_ptr<Effect> &effect) const {
}

void Player::diseffect() {
const auto &effect = g_game().attachedeffects->getEffectByID(getCurrentEffect());
defaultOutfit.lookEffect = 0;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -7748,25 +7775,25 @@ bool Player::addCustomOutfit(const std::string &type, const std::variant<uint16_
const std::string &name = std::get<std::string>(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;
}
Expand Down Expand Up @@ -7797,25 +7824,25 @@ bool Player::removeCustomOutfit(const std::string &type, const std::variant<uint
const std::string &name = std::get<std::string>(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;
}
Expand Down
3 changes: 0 additions & 3 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,9 +1455,6 @@ class Player final : public Creature, public Cylinder, public Bankable {
std::vector<uint16_t> quickLootListItemIds;

std::vector<OutfitEntry> outfits;
std::unordered_set<uint16_t> wings;
std::unordered_set<uint16_t> auras;
std::unordered_set<uint16_t> effects;
std::unordered_set<uint16_t> shaders;
std::vector<FamiliarEntry> familiars;

Expand Down
25 changes: 18 additions & 7 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -11107,10 +11107,13 @@ void Game::playerSetTyping(uint32_t playerId, uint8_t typing) {
}

void Game::refreshItem(const std::shared_ptr<Item> &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);
Expand All @@ -11126,7 +11129,11 @@ void Game::refreshItem(const std::shared_ptr<Item> &item) {
const auto spectators = Spectators().find<Player>(container->getPosition(), false, 2, 2, 2, 2);
// send to client
for (const auto &spectator : spectators) {
spectator->getPlayer()->sendUpdateContainerItem(container, static_cast<uint16_t>(index), item);
const auto &player = spectator->getPlayer();
if (!player) {
continue;
}
player->sendUpdateContainerItem(container, static_cast<uint16_t>(index), item);
}
}
return;
Expand All @@ -11135,7 +11142,11 @@ void Game::refreshItem(const std::shared_ptr<Item> &item) {
const auto spectators = Spectators().find<Player>(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;
}
Expand Down
5 changes: 0 additions & 5 deletions src/lua/functions/creatures/creature_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,6 @@ int CreatureFunctions::luaCreatureAttachEffectById(lua_State* L) {
const auto &creature = Lua::getUserdataShared<Creature>(L, 1);
if (!creature) {
Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
Lua::pushBoolean(L, false);
return 1;
}
uint16_t id = Lua::getNumber<uint16_t>(L, 2);
Expand All @@ -1215,7 +1214,6 @@ int CreatureFunctions::luaCreatureDetachEffectById(lua_State* L) {
const auto &creature = Lua::getUserdataShared<Creature>(L, 1);
if (!creature) {
Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
Lua::pushBoolean(L, false);
return 1;
}
uint16_t id = Lua::getNumber<uint16_t>(L, 2);
Expand All @@ -1228,7 +1226,6 @@ int CreatureFunctions::luaCreatureGetAttachedEffects(lua_State* L) {
const auto &creature = Lua::getUserdataShared<Creature>(L, 1);
if (!creature) {
Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
lua_pushnil(L);
return 1;
}

Expand All @@ -1246,7 +1243,6 @@ int CreatureFunctions::luaCreatureGetShader(lua_State* L) {
const auto &creature = Lua::getUserdataShared<Creature>(L, 1);
if (!creature) {
Lua::reportErrorFunc(Lua::getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
Lua::pushBoolean(L, false);
return 1;
}

Expand All @@ -1260,7 +1256,6 @@ int CreatureFunctions::luaCreatureSetShader(lua_State* L) {
const auto &creature = Lua::getUserdataShared<Creature>(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));
Expand Down
Loading

0 comments on commit c1f349e

Please sign in to comment.