Skip to content

Commit a50f965

Browse files
dudantaslibergod
authored andcommitted
Make type-checking methods const and refactor spectators logic
Changed type-checking methods (e.g., isItem, isCreature) to be const across Thing and derived classes for better const-correctness. Refactored Map's getSpectatorsInRangeEx and getSpectatorsByPattern to use a helper for cleaning duplicate/new spectators. Updated Thing to make most property accessors const and improved code clarity. Replaced std::reverse with std::ranges::reverse in Tile::getCreatures.
1 parent 5b346ae commit a50f965

File tree

10 files changed

+490
-166
lines changed

10 files changed

+490
-166
lines changed

src/client/creature.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class Creature : public Thing
159159
bool isDead() { return m_healthPercent <= 0; }
160160
bool isFullHealth() { return m_healthPercent == 100; }
161161
bool canBeSeen() { return !isInvisible() || isPlayer(); }
162-
bool isCreature() override { return true; }
162+
bool isCreature() const override { return true; }
163163
bool isCovered() { return m_isCovered; }
164164

165165
void setCovered(bool covered);
@@ -367,12 +367,12 @@ minHeight,
367367
class Npc final : public Creature
368368
{
369369
public:
370-
bool isNpc() override { return true; }
370+
bool isNpc() const override { return true; }
371371
};
372372

373373
// @bindclass
374374
class Monster final : public Creature
375375
{
376376
public:
377-
bool isMonster() override { return true; }
377+
bool isMonster() const override { return true; }
378378
};

src/client/effect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Effect final : public Thing
3333
void setId(uint32_t id) override;
3434
void setPosition(const Position& position, uint8_t stackPos = 0) override;
3535

36-
bool isEffect() override { return true; }
36+
bool isEffect() const override { return true; }
3737
bool waitFor(const EffectPtr&);
3838

3939
EffectPtr asEffect() { return static_self_cast<Effect>(); }

src/client/item.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Item final : public Thing
104104

105105
ItemPtr clone();
106106
ItemPtr asItem() { return static_self_cast<Item>(); }
107-
bool isItem() override { return true; }
107+
bool isItem() const override { return true; }
108108

109109
void updatePatterns();
110110
int calculateAnimationPhase();
@@ -143,7 +143,7 @@ class Item final : public Thing
143143

144144
bool isHouseDoor() { return m_attribs.has(ATTR_HOUSEDOORID); }
145145
bool isDepot() { return m_attribs.has(ATTR_DEPOT_ID); }
146-
bool isContainer() override { return m_attribs.has(ATTR_CONTAINER_ITEMS) || Thing::isContainer(); }
146+
bool isContainer() const override { return m_attribs.has(ATTR_CONTAINER_ITEMS) || Thing::isContainer(); }
147147
bool isDoor() { return m_attribs.has(ATTR_HOUSEDOORID); }
148148
bool isTeleport() { return m_attribs.has(ATTR_TELE_DEST); }
149149

src/client/localplayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class LocalPlayer final : public Player
124124
bool isParalyzed() const { return (m_states & Otc::IconParalyze) == Otc::IconParalyze; }
125125

126126
LocalPlayerPtr asLocalPlayer() { return static_self_cast<LocalPlayer>(); }
127-
bool isLocalPlayer() override { return true; }
127+
bool isLocalPlayer() const override { return true; }
128128

129129
void onPositionChange(const Position& newPos, const Position& oldPos) override;
130130

src/client/map.cpp

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@
3636
#include <framework/core/graphicalapplication.h>
3737
#include <framework/ui/uiwidget.h>
3838
#include <queue>
39+
#include <unordered_set>
40+
41+
namespace
42+
{
43+
void cleanNewSpectators(std::vector<CreaturePtr>& creatures, std::unordered_set<uint32_t>& seenIds, const std::size_t startIndex)
44+
{
45+
auto it = creatures.begin() + startIndex;
46+
while (it != creatures.end()) {
47+
if (const auto& creature = *it; !creature || !seenIds.insert(creature->getId()).second) {
48+
it = creatures.erase(it);
49+
continue;
50+
}
51+
++it;
52+
}
53+
}
54+
}
3955

4056
#ifdef FRAMEWORK_EDITOR
4157
#include "houses.h"
@@ -649,26 +665,23 @@ std::vector<CreaturePtr> Map::getSpectatorsInRangeEx(const Position& centerPos,
649665
const int startX = centerPos.x - minXRange;
650666
const int endX = centerPos.x + maxXRange;
651667

652-
for (int z = startZ; z <= endZ; ++z) {
668+
const auto appendSpectatorsFromLayer = [&](const int z) {
653669
for (int y = startY; y <= endY; ++y) {
654670
for (int x = startX; x <= endX; ++x) {
655-
if (const auto& tile = getTile(Position(x, y, z)); tile && tile->hasCreatures()) {
656-
const auto sizeBeforeAppend = creatures.size();
657-
658-
tile->appendSpectators(creatures);
659-
660-
auto it = creatures.begin() + sizeBeforeAppend;
661-
while (it != creatures.end()) {
662-
const auto& creature = *it;
663-
if (!creature || !seenIds.insert(creature->getId()).second) {
664-
it = creatures.erase(it);
665-
continue;
666-
}
667-
++it;
668-
}
671+
const auto tile = getTile(Position(x, y, z));
672+
if (!tile || !tile->hasCreatures()) {
673+
continue;
669674
}
675+
676+
const auto sizeBeforeAppend = creatures.size();
677+
tile->appendSpectators(creatures);
678+
cleanNewSpectators(creatures, seenIds, sizeBeforeAppend);
670679
}
671680
}
681+
};
682+
683+
for (int z = startZ; z <= endZ; ++z) {
684+
appendSpectatorsFromLayer(z);
672685
}
673686
return creatures;
674687
}
@@ -1490,26 +1503,20 @@ std::vector<CreaturePtr> Map::getSpectatorsByPattern(const Position& centerPos,
14901503
seenIds.reserve(m_knownCreatures.size());
14911504
for (int y = centerPos.y - height / 2, endy = centerPos.y + height / 2; y <= endy; ++y) {
14921505
for (int x = centerPos.x - width / 2, endx = centerPos.x + width / 2; x <= endx; ++x) {
1493-
if (!finalPattern[p++]) {
1506+
const auto enabled = finalPattern[p];
1507+
++p;
1508+
if (!enabled) {
14941509
continue;
14951510
}
1496-
const TilePtr& tile = getTile(Position(x, y, centerPos.z));
1511+
1512+
const auto tile = getTile(Position(x, y, centerPos.z));
14971513
if (!tile || !tile->hasCreatures()) {
14981514
continue;
14991515
}
15001516

15011517
const auto sizeBeforeAppend = creatures.size();
15021518
tile->appendSpectators(creatures);
1503-
1504-
auto it = creatures.begin() + sizeBeforeAppend;
1505-
while (it != creatures.end()) {
1506-
const auto& creature = *it;
1507-
if (!creature || !seenIds.insert(creature->getId()).second) {
1508-
it = creatures.erase(it);
1509-
continue;
1510-
}
1511-
++it;
1512-
}
1519+
cleanNewSpectators(creatures, seenIds, sizeBeforeAppend);
15131520
}
15141521
}
15151522
return creatures;

src/client/missile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Missile final : public Thing
3434
void setId(uint32_t id) override;
3535
void setPath(const Position& fromPosition, const Position& toPosition);
3636

37-
bool isMissile() override { return true; }
37+
bool isMissile() const override { return true; }
3838

3939
MissilePtr asMissile() { return static_self_cast<Missile>(); }
4040

src/client/player.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ class Player : public Creature
3232
~Player() override = default;
3333

3434
PlayerPtr asPlayer() { return static_self_cast<Player>(); }
35-
bool isPlayer() override { return true; }
35+
bool isPlayer() const override { return true; }
3636
};

0 commit comments

Comments
 (0)