From 804557721c20f6351194fe6b99353fcb64a13afd Mon Sep 17 00:00:00 2001 From: Anthony Lazar Date: Fri, 23 Feb 2024 23:55:34 -0500 Subject: [PATCH] Codechange: Keep track of all open infrastructure windows in a vector highlight the infrastructure of the most recently opened company. Changed code style base on comments. --- src/company_gui.cpp | 13 ++++++++++--- src/company_gui.h | 2 +- src/viewport.cpp | 25 +++++++++++-------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/company_gui.cpp b/src/company_gui.cpp index 3c7aa4a67f..f4e81740fb 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -53,7 +53,7 @@ /** Company GUI constants. */ static void DoSelectCompanyManagerFace(Window *parent); static void ShowCompanyInfrastructure(CompanyID company); -bool _infrastructure_window_open = false; +std::vector _viewport_infrastructure_window_order; /** List of revenues. */ static const std::initializer_list _expenses_list_revenue = { @@ -1831,13 +1831,20 @@ struct CompanyInfrastructureWindow : Window RoadTypes roadtypes; ///< Valid roadtypes. uint total_width; ///< String width of the total cost line. + /** * Hide the window and all its child windows, and mark them for a later deletion. * Stop white highlight of company owned infrastructure */ void Close([[maybe_unused]] int data) override { - _infrastructure_window_open = false; + auto to_remove_from_order = std::find(_viewport_infrastructure_window_order.begin(), _viewport_infrastructure_window_order.end(), (CompanyID)this->window_number); + + /* Cautious error checking */ + if (to_remove_from_order != _viewport_infrastructure_window_order.end()) { + _viewport_infrastructure_window_order.erase(to_remove_from_order); + } + MarkWholeScreenDirty(); this->Window::Close(); } @@ -2168,7 +2175,7 @@ static void ShowCompanyInfrastructure(CompanyID company) { if (!Company::IsValidID(company)) return; AllocateWindowDescFront(&_company_infrastructure_desc, company); - _infrastructure_window_open = true; + _viewport_infrastructure_window_order.push_back(company); MarkWholeScreenDirty(); } diff --git a/src/company_gui.h b/src/company_gui.h index d2633d74d3..c49e75607a 100644 --- a/src/company_gui.h +++ b/src/company_gui.h @@ -26,6 +26,6 @@ void InvalidateCompanyWindows(const Company *c); void CloseCompanyWindows(CompanyID company); void DirtyCompanyInfrastructureWindows(CompanyID company); -extern bool _infrastructure_window_open; +extern std::vector _viewport_infrastructure_window_order; #endif /* COMPANY_GUI_H */ diff --git a/src/viewport.cpp b/src/viewport.cpp index 9cb465756e..31cf35647d 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -1038,31 +1038,28 @@ static TileHighlightType GetTileHighlightType(TileIndex t) } } - if (_current_company < MAX_COMPANIES && _infrastructure_window_open) { - /* Edge case of company owned tramway on non-company owned tile */ - if (IsTileType(t, MP_ROAD) && !IsRoadDepot(t) && HasTileRoadType(t, RTT_TRAM)) { - if (IsRoadOwner(t, RTT_TRAM, _current_company)) { - return THT_WHITE; - } - } - + /* Highlight infrastructure owned by the company with the most recently currently opened infrastructure window */ + if (!_viewport_infrastructure_window_order.empty()) { switch (GetTileType(t)) { case MP_ROAD: + /* Edge case of company owned tramway on non-company owned tile */ + if (IsTileType(t, MP_ROAD) && !IsRoadDepot(t) && HasTileRoadType(t, RTT_TRAM)) { + if (IsRoadOwner(t, RTT_TRAM, _viewport_infrastructure_window_order.back())) { + return THT_WHITE; + } + } + [[fallthrough]]; case MP_RAILWAY: case MP_TUNNELBRIDGE: case MP_WATER: - { - CommandCost ret = CheckTileOwnership(t); - if (!ret.Failed()) { + if (GetTileOwner(t) == _viewport_infrastructure_window_order.back()) { return THT_WHITE; } break; - } default: - { return THT_NONE; break; - } + } } return THT_NONE;