Skip to content

Commit

Permalink
improve: rework on Player::getStorageValue for return storage value (o…
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Jan 12, 2023
1 parent ccb2474 commit 6df2a92
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/creatures/combat/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ bool ConditionRegeneration::executeCondition(Creature* creature, int32_t interva
Player* player = creature->getPlayer();
int32_t PlayerdailyStreak = 0;
if (player) {
player->getStorageValue(STORAGEVALUE_DAILYREWARD, PlayerdailyStreak);
PlayerdailyStreak = player->getStorageValue(STORAGEVALUE_DAILYREWARD);
}
if (creature->getZone() != ZONE_PROTECTION || PlayerdailyStreak >= DAILY_REWARD_HP_REGENERATION) {
if (internalHealthTicks >= getHealthTicks(creature)) {
Expand Down
3 changes: 1 addition & 2 deletions src/creatures/players/imbuements/imbuements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,9 @@ std::vector<Imbuement*> Imbuements::getImbuements(const Player* player, Item* it
}

// Parse the storages for each imbuement in imbuements.xml and config.lua (enable/disable storage)
int32_t storageValue;
if (g_configManager().getBoolean(TOGGLE_IMBUEMENT_SHRINE_STORAGE)
&& imbuement->getStorage() != 0
&& !player->getStorageValue(imbuement->getStorage(), storageValue)
&& player->getStorageValue(imbuement->getStorage() == -1)
&& imbuement->getBaseID() >= 1 && imbuement->getBaseID() <= 3) {
continue;
}
Expand Down
33 changes: 15 additions & 18 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,30 +732,27 @@ void Player::addStorageValue(const uint32_t key, const int32_t value, const bool
}

if (value != -1) {
int32_t oldValue;
getStorageValue(key, oldValue);

storageMap[key] = value;

if (!isLogin) {
auto currentFrameTime = g_dispatcher().getDispatcherCycle();
g_events().eventOnStorageUpdate(this, key, value, oldValue, currentFrameTime);
g_events().eventOnStorageUpdate(this, key, value, getStorageValue(key), currentFrameTime);
}
} else {
storageMap.erase(key);
}
}

bool Player::getStorageValue(const uint32_t key, int32_t& value) const
int32_t Player::getStorageValue(const uint32_t key) const
{
int32_t value = -1;
auto it = storageMap.find(key);
if (it == storageMap.end()) {
value = -1;
return false;
return value;
}

value = it->second;
return true;
return value;
}

bool Player::canSee(const Position& pos) const
Expand Down Expand Up @@ -5154,8 +5151,8 @@ void Player::sendUnjustifiedPoints()

uint8_t Player::getCurrentMount() const
{
int32_t value;
if (getStorageValue(PSTRG_MOUNTS_CURRENTMOUNT, value)) {
int32_t value = getStorageValue(PSTRG_MOUNTS_CURRENTMOUNT);
if (value > 0) {
return value;
}
return 0;
Expand Down Expand Up @@ -5242,8 +5239,8 @@ bool Player::tameMount(uint8_t mountId)
const uint8_t tmpMountId = mountId - 1;
const uint32_t key = PSTRG_MOUNTS_RANGE_START + (tmpMountId / 31);

int32_t value;
if (getStorageValue(key, value)) {
int32_t value = getStorageValue(key);
if (value != -1) {
value |= (1 << (tmpMountId % 31));
} else {
value = (1 << (tmpMountId % 31));
Expand All @@ -5262,8 +5259,8 @@ bool Player::untameMount(uint8_t mountId)
const uint8_t tmpMountId = mountId - 1;
const uint32_t key = PSTRG_MOUNTS_RANGE_START + (tmpMountId / 31);

int32_t value;
if (!getStorageValue(key, value)) {
int32_t value = getStorageValue(key);
if (value == -1) {
return true;
}

Expand Down Expand Up @@ -5294,8 +5291,8 @@ bool Player::hasMount(const Mount* mount) const

const uint8_t tmpMountId = mount->id - 1;

int32_t value;
if (!getStorageValue(PSTRG_MOUNTS_RANGE_START + (tmpMountId / 31), value)) {
int32_t value = getStorageValue(PSTRG_MOUNTS_RANGE_START + (tmpMountId / 31));
if (value == -1) {
return false;
}

Expand Down Expand Up @@ -6350,10 +6347,10 @@ bool Player::saySpell(
}

int32_t valueEmote = 0;
// Send to client
// Send to client
for (Creature* spectator : spectators) {
if (Player* tmpPlayer = spectator->getPlayer()) {
tmpPlayer->getStorageValue(STORAGEVALUE_EMOTE, valueEmote);
valueEmote = tmpPlayer->getStorageValue(STORAGEVALUE_EMOTE);
if (!ghostMode || tmpPlayer->canSeeCreature(this)) {
if (valueEmote == 1) {
tmpPlayer->sendCreatureSay(this, TALKTYPE_MONSTER_SAY, text, pos);
Expand Down
7 changes: 3 additions & 4 deletions src/creatures/players/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,9 @@ class Player final : public Creature, public Cylinder
}
uint32_t getBestiaryKillCount(uint16_t raceid) const
{
int32_t cp = 0;
uint32_t key = STORAGEVALUE_BESTIARYKILLCOUNT + raceid;
getStorageValue(key, cp);
return cp > 0 ? static_cast<uint32_t>(cp) : 0;
auto value = getStorageValue(key);
return value > 0 ? static_cast<uint32_t>(value) : 0;
}

void setGUID(uint32_t newGuid) {
Expand Down Expand Up @@ -406,7 +405,7 @@ class Player final : public Creature, public Cylinder
bool canOpenCorpse(uint32_t ownerId) const;

void addStorageValue(const uint32_t key, const int32_t value, const bool isLogin = false);
bool getStorageValue(const uint32_t key, int32_t& value) const;
int32_t getStorageValue(const uint32_t key) const;
void genReservedStorageRange();

void setGroup(Group* newGroup) {
Expand Down
7 changes: 1 addition & 6 deletions src/lua/functions/creatures/player/player_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1578,12 +1578,7 @@ int PlayerFunctions::luaPlayerGetStorageValue(lua_State* L) {
}

uint32_t key = getNumber<uint32_t>(L, 2);
int32_t value;
if (player->getStorageValue(key, value)) {
lua_pushnumber(L, value);
} else {
lua_pushnumber(L, -1);
}
lua_pushnumber(L, player->getStorageValue(key));
return 1;
}

Expand Down
7 changes: 2 additions & 5 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5228,8 +5228,7 @@ void ProtocolGame::sendRestingStatus(uint8_t protection)
NetworkMessage msg;
msg.addByte(0xA9);
msg.addByte(protection); // 1 / 0
int32_t PlayerdailyStreak = 0;
player->getStorageValue(STORAGEVALUE_DAILYREWARD, PlayerdailyStreak);
int32_t PlayerdailyStreak = player->getStorageValue(STORAGEVALUE_DAILYREWARD);
msg.addByte(PlayerdailyStreak < 2 ? 0 : 1);
if (PlayerdailyStreak < 2)
{
Expand Down Expand Up @@ -7105,9 +7104,7 @@ void ProtocolGame::AddHiddenShopItem(NetworkMessage &msg)
void ProtocolGame::AddShopItem(NetworkMessage &msg, const ShopBlock &shopBlock)
{
// Sends the item information empty if the player doesn't have the storage to buy/sell a certain item
int32_t storageValue;
player->getStorageValue(shopBlock.itemStorageKey, storageValue);
if (shopBlock.itemStorageKey != 0 && storageValue < shopBlock.itemStorageValue)
if (shopBlock.itemStorageKey != 0 && player->getStorageValue(shopBlock.itemStorageKey) < shopBlock.itemStorageValue)
{
AddHiddenShopItem(msg);
return;
Expand Down

0 comments on commit 6df2a92

Please sign in to comment.