|
36 | 36 | #include <framework/core/graphicalapplication.h> |
37 | 37 | #include <framework/ui/uiwidget.h> |
38 | 38 | #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 | +} |
39 | 55 |
|
40 | 56 | #ifdef FRAMEWORK_EDITOR |
41 | 57 | #include "houses.h" |
@@ -649,26 +665,23 @@ std::vector<CreaturePtr> Map::getSpectatorsInRangeEx(const Position& centerPos, |
649 | 665 | const int startX = centerPos.x - minXRange; |
650 | 666 | const int endX = centerPos.x + maxXRange; |
651 | 667 |
|
652 | | - for (int z = startZ; z <= endZ; ++z) { |
| 668 | + const auto appendSpectatorsFromLayer = [&](const int z) { |
653 | 669 | for (int y = startY; y <= endY; ++y) { |
654 | 670 | 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; |
669 | 674 | } |
| 675 | + |
| 676 | + const auto sizeBeforeAppend = creatures.size(); |
| 677 | + tile->appendSpectators(creatures); |
| 678 | + cleanNewSpectators(creatures, seenIds, sizeBeforeAppend); |
670 | 679 | } |
671 | 680 | } |
| 681 | + }; |
| 682 | + |
| 683 | + for (int z = startZ; z <= endZ; ++z) { |
| 684 | + appendSpectatorsFromLayer(z); |
672 | 685 | } |
673 | 686 | return creatures; |
674 | 687 | } |
@@ -1490,26 +1503,20 @@ std::vector<CreaturePtr> Map::getSpectatorsByPattern(const Position& centerPos, |
1490 | 1503 | seenIds.reserve(m_knownCreatures.size()); |
1491 | 1504 | for (int y = centerPos.y - height / 2, endy = centerPos.y + height / 2; y <= endy; ++y) { |
1492 | 1505 | 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) { |
1494 | 1509 | continue; |
1495 | 1510 | } |
1496 | | - const TilePtr& tile = getTile(Position(x, y, centerPos.z)); |
| 1511 | + |
| 1512 | + const auto tile = getTile(Position(x, y, centerPos.z)); |
1497 | 1513 | if (!tile || !tile->hasCreatures()) { |
1498 | 1514 | continue; |
1499 | 1515 | } |
1500 | 1516 |
|
1501 | 1517 | const auto sizeBeforeAppend = creatures.size(); |
1502 | 1518 | 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); |
1513 | 1520 | } |
1514 | 1521 | } |
1515 | 1522 | return creatures; |
|
0 commit comments