summaryrefslogtreecommitdiff
path: root/src/station_base.h
diff options
context:
space:
mode:
authordP <dp@dpointer.org>2020-05-12 01:36:28 +0300
committerCharles Pigott <charlespigott@googlemail.com>2020-05-13 08:43:01 +0100
commit7bd52970a1e83cb88069e73e26479eb71bd17726 (patch)
tree88e6fca4b38b16333dd6f37c7fde50fd4464596d /src/station_base.h
parent2d5869fc79a1f1f347ab31665148c806d54afcd9 (diff)
downloadopenttd-7bd52970a1e83cb88069e73e26479eb71bd17726.tar.xz
Codechange: Refactor FindStationsAroundTiles to avoid code duplication
Diffstat (limited to 'src/station_base.h')
-rw-r--r--src/station_base.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/station_base.h b/src/station_base.h
index 0c33a58ed..3be7d4437 100644
--- a/src/station_base.h
+++ b/src/station_base.h
@@ -503,6 +503,7 @@ public:
uint GetCatchmentRadius() const;
Rect GetCatchmentRect() const;
bool CatchmentCoversTown(TownID t) const;
+ void AddIndustryToDeliver(Industry *ind);
void RemoveFromAllNearbyLists();
inline bool TileIsInCatchment(TileIndex tile) const
@@ -557,4 +558,39 @@ public:
void RebuildStationKdtree();
+/**
+ * Call a function on all stations that have any part of the requested area within their catchment.
+ * @param area The tile area to check
+ */
+template<typename Func>
+void ForAllStationsAroundTiles(const TileArea &ta, Func func)
+{
+ /* Not using, or don't have a nearby stations list, so we need to scan. */
+ std::set<StationID> seen_stations;
+
+ /* Scan an area around the building covering the maximum possible station
+ * to find the possible nearby stations. */
+ uint max_c = _settings_game.station.modified_catchment ? MAX_CATCHMENT : CA_UNMODIFIED;
+ TileArea ta_ext = TileArea(ta).Expand(max_c);
+ TILE_AREA_LOOP(tile, ta_ext) {
+ if (IsTileType(tile, MP_STATION)) seen_stations.insert(GetStationIndex(tile));
+ }
+
+ for (StationID stationid : seen_stations) {
+ Station *st = Station::GetIfValid(stationid);
+ if (st == nullptr) continue; /* Waypoint */
+
+ /* Check if station is attached to an industry */
+ if (!_settings_game.station.serve_neutral_industries && st->industry != nullptr) continue;
+
+ /* Test if the tile is within the station's catchment */
+ TILE_AREA_LOOP(tile, ta) {
+ if (st->TileIsInCatchment(tile)) {
+ func(st);
+ break;
+ }
+ }
+ }
+}
+
#endif /* STATION_BASE_H */