Codechange: Use functor for Kdtree's XYFunc. (#13074)
[openttd-github.git] / src / station_kdtree.h
blob084876a6fd134a41dd65b1478a4fb95638ce4898
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file station_kdtree.h Declarations for accessing the k-d tree of stations */
10 #ifndef STATION_KDTREE_H
11 #define STATION_KDTREE_H
13 #include "core/kdtree.hpp"
14 #include "core/math_func.hpp"
15 #include "station_base.h"
16 #include "map_func.h"
18 struct Kdtree_StationXYFunc {
19 inline uint16_t operator()(StationID stid, int dim)
21 return (dim == 0) ? TileX(BaseStation::Get(stid)->xy) : TileY(BaseStation::Get(stid)->xy);
25 using StationKdtree = Kdtree<StationID, Kdtree_StationXYFunc, uint16_t, int>;
26 extern StationKdtree _station_kdtree;
28 /**
29 * Call a function on all stations whose sign is within a radius of a center tile.
30 * @param center Central tile to search around.
31 * @param radius Distance in both X and Y to search within.
32 * @param func The function to call, must take a single parameter which is Station*.
34 template <typename Func>
35 void ForAllStationsRadius(TileIndex center, uint radius, Func func)
37 uint16_t x1, y1, x2, y2;
38 x1 = (uint16_t)std::max<int>(0, TileX(center) - radius);
39 x2 = (uint16_t)std::min<int>(TileX(center) + radius + 1, Map::SizeX());
40 y1 = (uint16_t)std::max<int>(0, TileY(center) - radius);
41 y2 = (uint16_t)std::min<int>(TileY(center) + radius + 1, Map::SizeY());
43 _station_kdtree.FindContained(x1, y1, x2, y2, [&](StationID id) {
44 func(Station::Get(id));
45 });
48 #endif