From 932b57720c3e032a3804c3b0215b01607c33c510 Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Mon, 3 Aug 2026 21:02:06 +0200 Subject: [PATCH] dedupe tunnel lookup and key tunnel tiles by QPoint --- src/ui/GameWorldView.cpp | 46 +++++++++++++++++++--------------------- src/ui/GameWorldView.h | 8 ++++++- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/ui/GameWorldView.cpp b/src/ui/GameWorldView.cpp index 5de92ff..63b8d18 100644 --- a/src/ui/GameWorldView.cpp +++ b/src/ui/GameWorldView.cpp @@ -960,29 +960,39 @@ BuildingType GameWorldView::effectiveBuilderType() const return inTunnelMode() ? m_tunnelGhostType : *m_builderType; } -std::map, TunnelTileInfo> GameWorldView::collectTunnelTiles() const +TunnelTileMap GameWorldView::collectTunnelTiles() const { // Index every tunnel entry/exit — built or still a construction site — by its // single-cell tile, so a just-placed tunnel (not yet constructed) is matchable // (REQ-BLD-TUNNEL-MODE, REQ-BLD-TUNNEL-SELECT-HIGHLIGHT). - std::map, TunnelTileInfo> tunnels; + TunnelTileMap tunnels; for (const Building& b : m_sim->getBuildings().getAllBuildings()) { if (b.type == BuildingType::TunnelEntry || b.type == BuildingType::TunnelExit) { - tunnels[{b.anchor.x(), b.anchor.y()}] = TunnelTileInfo{b.type, b.rotation}; + tunnels[b.anchor] = TunnelTileInfo{b.type, b.rotation}; } } for (const ConstructionSite& s : m_sim->getBuildings().getAllSites()) { if (s.type == BuildingType::TunnelEntry || s.type == BuildingType::TunnelExit) { - tunnels[{s.anchor.x(), s.anchor.y()}] = TunnelTileInfo{s.type, s.rotation}; + tunnels[s.anchor] = TunnelTileInfo{s.type, s.rotation}; } } return tunnels; } +TunnelLookup GameWorldView::makeTunnelLookup(const TunnelTileMap& tunnels) +{ + return [&tunnels](QPoint tile) -> std::optional + { + const TunnelTileMap::const_iterator it = tunnels.find(tile); + if (it == tunnels.end()) { return std::nullopt; } + return it->second; + }; +} + void GameWorldView::updateTunnelGhost() { m_tunnelGhostType = BuildingType::TunnelEntry; @@ -995,14 +1005,8 @@ void GameWorldView::updateTunnelGhost() return; } - const std::map, TunnelTileInfo> tunnels = collectTunnelTiles(); - const TunnelLookup lookup = [&tunnels](QPoint tile) -> std::optional - { - const std::map, TunnelTileInfo>::const_iterator it = - tunnels.find({tile.x(), tile.y()}); - if (it == tunnels.end()) { return std::nullopt; } - return it->second; - }; + const TunnelTileMap tunnels = collectTunnelTiles(); + const TunnelLookup lookup = makeTunnelLookup(tunnels); const TunnelCompletion completion = resolveTunnelCompletion(lookup, m_ghostTile, m_ghostRotation, @@ -1979,21 +1983,15 @@ void GameWorldView::drawSelectedTunnelConnections(QPainter& painter) { if (m_selectedBuildingIds.empty()) { return; } - const std::map, TunnelTileInfo> tunnels = collectTunnelTiles(); + const TunnelTileMap tunnels = collectTunnelTiles(); if (tunnels.empty()) { return; } - const TunnelLookup lookup = [&tunnels](QPoint tile) -> std::optional - { - const std::map, TunnelTileInfo>::const_iterator it = - tunnels.find({tile.x(), tile.y()}); - if (it == tunnels.end()) { return std::nullopt; } - return it->second; - }; + const TunnelLookup lookup = makeTunnelLookup(tunnels); // Collect the tiles to highlight in a set so a connection selected from both ends // (or overlapping runs) is filled exactly once — filling a semi-transparent green // twice would darken it (REQ-BLD-TUNNEL-SELECT-HIGHLIGHT). - std::set> highlightTiles; + std::set highlightTiles; for (const BuildingId id : m_selectedBuildingIds) { std::optional anchor; @@ -2023,15 +2021,15 @@ void GameWorldView::drawSelectedTunnelConnections(QPainter& painter) (delta.y() > 0) - (delta.y() < 0)); for (QPoint t = *anchor; ; t += stepDir) { - highlightTiles.insert({t.x(), t.y()}); + highlightTiles.insert(t); if (t == *partner) { break; } } } const QColor green = m_visuals->overlays.tunnelPreview; - for (const std::pair& tile : highlightTiles) + for (const QPoint& tile : highlightTiles) { - painter.fillRect(tileRect(QPoint(tile.first, tile.second)), green); + painter.fillRect(tileRect(tile), green); } } diff --git a/src/ui/GameWorldView.h b/src/ui/GameWorldView.h index 63c5f51..93de251 100644 --- a/src/ui/GameWorldView.h +++ b/src/ui/GameWorldView.h @@ -67,6 +67,9 @@ struct QPointCompare } }; +// Tunnel entries/exits indexed by their single-cell tile (REQ-BLD-TUNNEL-MODE). +using TunnelTileMap = std::map; + class GameWorldView : public QOpenGLWidget, public CombinedEventHandler, TunnelTileInfo> collectTunnelTiles() const; + TunnelTileMap collectTunnelTiles() const; + // Wraps a tunnel tile index in the lookup functor the TunnelCompletion helpers + // take. The returned functor references `tunnels`, which must outlive it. + static TunnelLookup makeTunnelLookup(const TunnelTileMap& tunnels); // Draws the green connection highlight for every selected tunnel end that has a // matching end (REQ-BLD-TUNNEL-SELECT-HIGHLIGHT). void drawSelectedTunnelConnections(QPainter& painter);