summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortruebrain <truebrain@openttd.org>2012-01-01 16:23:05 +0000
committertruebrain <truebrain@openttd.org>2012-01-01 16:23:05 +0000
commit3b2ecfab010426d2dac7918e6e32463d6b9e47f2 (patch)
tree1f82ee8dc03cf81f7ccb2f2fbb7e035a75460959
parent6f6035ce0ba88efe5a67da14f71cba54978ab471 (diff)
downloadopenttd-3b2ecfab010426d2dac7918e6e32463d6b9e47f2.tar.xz
(svn r23702) -Codechange: avoid using TileAddWrap() in FindStationsAroundTiles() by finding out where the border is in advance, speeding up the function with a factor 3 (you got to love random statistics which has no real meaning in the grand scheme of it all :D)
-rw-r--r--src/station_cmd.cpp30
1 files changed, 24 insertions, 6 deletions
diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp
index 57108c082..2086f3150 100644
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -3392,19 +3392,37 @@ CommandCost CmdRenameStation(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
void FindStationsAroundTiles(const TileArea &location, StationList *stations)
{
/* area to search = producer plus station catchment radius */
- int max_rad = (_settings_game.station.modified_catchment ? MAX_CATCHMENT : CA_UNMODIFIED);
+ uint max_rad = (_settings_game.station.modified_catchment ? MAX_CATCHMENT : CA_UNMODIFIED);
- for (int dy = -max_rad; dy < location.h + max_rad; dy++) {
- for (int dx = -max_rad; dx < location.w + max_rad; dx++) {
- TileIndex cur_tile = TileAddWrap(location.tile, dx, dy);
- if (cur_tile == INVALID_TILE || !IsTileType(cur_tile, MP_STATION)) continue;
+ uint x = TileX(location.tile);
+ uint y = TileY(location.tile);
+
+ uint min_x = (x > max_rad) ? x - max_rad : 0;
+ uint max_x = x + location.w + max_rad;
+ uint min_y = (y > max_rad) ? y - max_rad : 0;
+ uint max_y = y + location.h + max_rad;
+
+ if (min_x == 0 && _settings_game.construction.freeform_edges) min_x = 1;
+ if (min_y == 0 && _settings_game.construction.freeform_edges) min_y = 1;
+ if (max_x >= MapSizeX()) max_x = MapSizeX() - 1;
+ if (max_y >= MapSizeY()) max_y = MapSizeY() - 1;
+
+ for (uint cy = min_y; cy < max_y; cy++) {
+ for (uint cx = min_x; cx < max_x; cx++) {
+ TileIndex cur_tile = TileXY(cx, cy);
+ if (!IsTileType(cur_tile, MP_STATION)) continue;
Station *st = Station::GetByTile(cur_tile);
+ /* st can be NULL in case of waypoints */
if (st == NULL) continue;
if (_settings_game.station.modified_catchment) {
int rad = st->GetCatchmentRadius();
- if (dx < -rad || dx >= rad + location.w || dy < -rad || dy >= rad + location.h) continue;
+ int rad_x = cx - x;
+ int rad_y = cy - y;
+
+ if (rad_x < -rad || rad_x >= rad + location.w) continue;
+ if (rad_y < -rad || rad_y >= rad + location.h) continue;
}
/* Insert the station in the set. This will fail if it has