From ce10d9be3f3ae1422e4e1f79fc5c867ab38526e7 Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Sat, 23 Mar 2019 12:39:13 +0100 Subject: Fix #7374: Ensure k-d trees are always updated when station sign moves --- src/base_station_base.h | 6 ++++++ src/station.cpp | 2 +- src/station_base.h | 2 ++ src/station_cmd.cpp | 26 ++++++++++++++++++-------- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/base_station_base.h b/src/base_station_base.h index cd512c517..8af01f8a7 100644 --- a/src/base_station_base.h +++ b/src/base_station_base.h @@ -110,6 +110,12 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> { */ virtual void UpdateVirtCoord() = 0; + virtual void MoveSign(TileIndex new_xy) + { + this->xy = new_xy; + this->UpdateVirtCoord(); + } + /** * Get the tile area for a given station type. * @param ta tile area to fill. diff --git a/src/station.cpp b/src/station.cpp index 3c50ef94f..6f10806a4 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -207,7 +207,7 @@ RoadStop *Station::GetPrimaryRoadStop(const RoadVehicle *v) const void Station::AddFacility(StationFacility new_facility_bit, TileIndex facil_xy) { if (this->facilities == FACIL_NONE) { - this->xy = facil_xy; + this->MoveSign(facil_xy); this->random_bits = Random(); } this->facilities |= new_facility_bit; diff --git a/src/station_base.h b/src/station_base.h index 243a2f0ce..794fbdfdf 100644 --- a/src/station_base.h +++ b/src/station_base.h @@ -492,6 +492,8 @@ public: void UpdateVirtCoord() override; + void MoveSign(TileIndex new_xy) override; + void AfterStationTileSetChange(bool adding, StationType type); uint GetPlatformLength(TileIndex tile, DiagDirection dir) const override; diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 72c70456f..6121424d5 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -429,6 +429,23 @@ void Station::UpdateVirtCoord() SetWindowDirty(WC_STATION_VIEW, this->index); } +/** + * Move the station main coordinate somewhere else. + * @param new_xy new tile location of the sign + */ +void Station::MoveSign(TileIndex new_xy) +{ + if (this->xy == new_xy) return; + + _viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeStation(this->index)); + _station_kdtree.Remove(this->index); + + this->BaseStation::MoveSign(new_xy); + + _station_kdtree.Insert(this->index); + _viewport_sign_kdtree.Insert(ViewportSignKdtreeItem::MakeStation(this->index)); +} + /** Update the virtual coords needed to draw the station sign for all stations. */ void UpdateAllStationVirtCoords() { @@ -672,14 +689,7 @@ static void UpdateStationSignCoord(BaseStation *st) /* clamp sign coord to be inside the station rect */ TileIndex new_xy = TileXY(ClampU(TileX(st->xy), r->left, r->right), ClampU(TileY(st->xy), r->top, r->bottom)); - if (new_xy != st->xy) { - _viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeStation(st->index)); - _station_kdtree.Remove(st->index); - st->xy = new_xy; - _station_kdtree.Insert(st->index); - _viewport_sign_kdtree.Insert(ViewportSignKdtreeItem::MakeStation(st->index)); - st->UpdateVirtCoord(); - } + st->MoveSign(new_xy); if (!Station::IsExpected(st)) return; Station *full_station = Station::From(st); -- cgit v1.2.3-70-g09d2