Skip to content

Commit

Permalink
Replace hardcoded values with MAX_STACKPOS (#4565)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramon-bernardo authored Nov 21, 2023
1 parent a51f8cc commit 2946e9e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static constexpr int32_t RANGE_WRAP_ITEM_INTERVAL = 400;
static constexpr int32_t RANGE_REQUEST_TRADE_INTERVAL = 400;

static constexpr uint8_t ITEM_STACK_SIZE = 100;
static constexpr uint32_t MAX_STACKPOS = 10;

/**
* Main Game class.
Expand Down
27 changes: 14 additions & 13 deletions src/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ void ProtocolGame::GetTileDescription(const Tile* tile, NetworkMessage& msg)
for (auto it = items->getBeginTopItem(), end = items->getEndTopItem(); it != end; ++it) {
msg.addItem(*it);

if (++count == 10) {
if (++count == MAX_STACKPOS) {
break;
}
}
Expand All @@ -864,11 +864,11 @@ void ProtocolGame::GetTileDescription(const Tile* tile, NetworkMessage& msg)
}
}

if (items && count < 10) {
if (items && count < MAX_STACKPOS) {

Check failure on line 867 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-Debug-luajit=on

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]

Check failure on line 867 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / test

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]

Check failure on line 867 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-Debug-luajit=off

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]
for (auto it = items->getBeginDownItem(), end = items->getEndDownItem(); it != end; ++it) {
msg.addItem(*it);

if (++count == 10) {
if (++count == MAX_STACKPOS) {
return;
}
}
Expand Down Expand Up @@ -2442,20 +2442,20 @@ void ProtocolGame::sendCloseContainer(uint8_t cid)
writeToOutputBuffer(msg);
}

void ProtocolGame::sendCreatureTurn(const Creature* creature, uint32_t stackPos)
void ProtocolGame::sendCreatureTurn(const Creature* creature, uint32_t stackpos)
{
if (!canSee(creature)) {
return;
}

NetworkMessage msg;
msg.addByte(0x6B);
if (stackPos >= 10) {
if (stackpos >= MAX_STACKPOS) {
msg.add<uint16_t>(0xFFFF);
msg.add<uint32_t>(creature->getID());
} else {
msg.addPosition(creature->getPosition());
msg.addByte(stackPos);
msg.addByte(stackpos);
}

msg.add<uint16_t>(0x63);
Expand Down Expand Up @@ -2712,7 +2712,7 @@ void ProtocolGame::sendUpdateTileCreature(const Position& pos, uint32_t stackpos

void ProtocolGame::sendRemoveTileCreature(const Creature* creature, const Position& pos, uint32_t stackpos)
{
if (stackpos < 10) {
if (stackpos < MAX_STACKPOS) {
if (!canSee(pos)) {
return;
}
Expand Down Expand Up @@ -2789,8 +2789,9 @@ void ProtocolGame::sendAddCreature(const Creature* creature, const Position& pos
// refresh the tile instead
// 1. this is a rare case, and is only triggered by forcing summon in a position
// 2. since no stackpos will be send to the client about that creature, removing it must be done with its id if
// its stackpos remains >= 10. this is done to add creatures to battle list instead of rendering on screen
if (stackpos >= 10) {
// its stackpos remains >= MAX_STACKPOS. this is done to add creatures to battle list instead of rendering on
// screen
if (stackpos >= MAX_STACKPOS) {

Check failure on line 2794 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-Debug-luajit=on

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]

Check failure on line 2794 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / test

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]

Check failure on line 2794 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-Debug-luajit=off

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]
// @todo: should we avoid this check?
if (const Tile* tile = creature->getTile()) {
sendUpdateTile(tile, pos);
Expand Down Expand Up @@ -2873,7 +2874,7 @@ void ProtocolGame::sendMoveCreature(const Creature* creature, const Position& ne
RemoveTileCreature(msg, creature, oldPos, oldStackPos);
} else {
msg.addByte(0x6D);
if (oldStackPos < 10) {
if (oldStackPos < MAX_STACKPOS) {

Check failure on line 2877 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-Debug-luajit=on

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]

Check failure on line 2877 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / test

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]

Check failure on line 2877 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-Debug-luajit=off

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]
msg.addPosition(oldPos);
msg.addByte(oldStackPos);
} else {
Expand Down Expand Up @@ -2917,7 +2918,7 @@ void ProtocolGame::sendMoveCreature(const Creature* creature, const Position& ne
} else {
NetworkMessage msg;
msg.addByte(0x6D);
if (oldStackPos < 10) {
if (oldStackPos < MAX_STACKPOS) {

Check failure on line 2921 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-Debug-luajit=on

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]

Check failure on line 2921 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / test

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]

Check failure on line 2921 in src/protocolgame.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-Debug-luajit=off

comparison of integer expressions of different signedness: ‘int32_t’ {aka ‘int’} and ‘const uint32_t’ {aka ‘const unsigned int’} [-Werror=sign-compare]
msg.addPosition(oldPos);
msg.addByte(oldStackPos);
} else {
Expand Down Expand Up @@ -3675,7 +3676,7 @@ void ProtocolGame::AddCreatureLight(NetworkMessage& msg, const Creature* creatur
// tile
void ProtocolGame::RemoveTileThing(NetworkMessage& msg, const Position& pos, uint32_t stackpos)
{
if (stackpos >= 10) {
if (stackpos >= MAX_STACKPOS) {
return;
}

Expand All @@ -3687,7 +3688,7 @@ void ProtocolGame::RemoveTileThing(NetworkMessage& msg, const Position& pos, uin
void ProtocolGame::RemoveTileCreature(NetworkMessage& msg, const Creature* creature, const Position& pos,
uint32_t stackpos)
{
if (stackpos < 10) {
if (stackpos < MAX_STACKPOS) {
RemoveTileThing(msg, pos, stackpos);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/protocolgame.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class ProtocolGame final : public Protocol
void sendSkills();
void sendPing();
void sendPingBack();
void sendCreatureTurn(const Creature* creature, uint32_t stackPos);
void sendCreatureTurn(const Creature* creature, uint32_t stackpos);
void sendCreatureSay(const Creature* creature, SpeakClasses type, const std::string& text,
const Position* pos = nullptr);

Expand Down
8 changes: 4 additions & 4 deletions src/tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,13 +1244,13 @@ int32_t Tile::getStackposOfItem(const Player* player, const Item* item) const
for (auto it = items->getBeginTopItem(), end = items->getEndTopItem(); it != end; ++it) {
if (*it == item) {
return n;
} else if (++n == 10) {
} else if (++n == MAX_STACKPOS) {
return -1;
}
}
} else {
n += items->getTopItemCount();
if (n >= 10) {
if (n >= MAX_STACKPOS) {
return -1;
}
}
Expand All @@ -1259,7 +1259,7 @@ int32_t Tile::getStackposOfItem(const Player* player, const Item* item) const
if (const CreatureVector* creatures = getCreatures()) {
for (const Creature* creature : *creatures) {
if (player->canSeeCreature(creature)) {
if (++n >= 10) {
if (++n >= MAX_STACKPOS) {
return -1;
}
}
Expand All @@ -1270,7 +1270,7 @@ int32_t Tile::getStackposOfItem(const Player* player, const Item* item) const
for (auto it = items->getBeginDownItem(), end = items->getEndDownItem(); it != end; ++it) {
if (*it == item) {
return n;
} else if (++n >= 10) {
} else if (++n >= MAX_STACKPOS) {
return -1;
}
}
Expand Down

0 comments on commit 2946e9e

Please sign in to comment.