diff --git a/src/map.cpp b/src/map.cpp index 0308640bf0..eeddf8c5a2 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -108,6 +108,12 @@ TileIndex TileAddWrap(TileIndex tile, int addx, int addy) return TileXY(x, y); } +/** 'Lookup table' for tile offsets given an Axis */ +extern const TileIndexDiffC _tileoffs_by_axis[] = { + { 1, 0}, ///< AXIS_X + { 0, 1}, ///< AXIS_Y +}; + /** 'Lookup table' for tile offsets given a DiagDirection */ extern const TileIndexDiffC _tileoffs_by_diagdir[] = { {-1, 0}, ///< DIAGDIR_NE diff --git a/src/map_func.h b/src/map_func.h index 80deafa6c8..3d0331be63 100644 --- a/src/map_func.h +++ b/src/map_func.h @@ -543,6 +543,20 @@ uint DistanceMaxPlusManhattan(TileIndex, TileIndex); ///< Max + Manhattan uint DistanceFromEdge(TileIndex); ///< shortest distance from any edge of the map uint DistanceFromEdgeDir(TileIndex, DiagDirection); ///< distance from the map edge in given direction +/** + * Convert an Axis to a TileIndexDiff + * + * @param axis The Axis + * @return The resulting TileIndexDiff in southern direction (either SW or SE). + */ +inline TileIndexDiff TileOffsByAxis(Axis axis) +{ + extern const TileIndexDiffC _tileoffs_by_axis[]; + + assert(IsValidAxis(axis)); + return ToTileIndexDiff(_tileoffs_by_axis[axis]); +} + /** * Convert a DiagDirection to a TileIndexDiff * diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp index 8a307b2640..86dbe8bf32 100644 --- a/src/newgrf_station.cpp +++ b/src/newgrf_station.cpp @@ -70,7 +70,7 @@ struct ETileArea : TileArea { case TA_PLATFORM: { TileIndex start, end; Axis axis = GetRailStationAxis(tile); - TileIndexDiff delta = TileOffsByDiagDir(AxisToDiagDir(axis)); + TileIndexDiff delta = TileOffsByAxis(axis); for (end = tile; IsRailStationTile(end + delta) && IsCompatibleTrainStationTile(end + delta, tile); end += delta) { /* Nothing */ } for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(start - delta, tile); start -= delta) { /* Nothing */ } diff --git a/src/waypoint_cmd.cpp b/src/waypoint_cmd.cpp index 26d791e236..e5d7c40561 100644 --- a/src/waypoint_cmd.cpp +++ b/src/waypoint_cmd.cpp @@ -233,7 +233,7 @@ CommandCost CmdBuildRailWaypoint(DoCommandFlag flags, TileIndex start_tile, Axis StationID est = INVALID_STATION; /* Check whether the tiles we're building on are valid rail or not. */ - TileIndexDiff offset = TileOffsByDiagDir(AxisToDiagDir(OtherAxis(axis))); + TileIndexDiff offset = TileOffsByAxis(OtherAxis(axis)); for (int i = 0; i < count; i++) { TileIndex tile = start_tile + i * offset; CommandCost ret = IsValidTileForWaypoint(tile, axis, &est); @@ -373,7 +373,7 @@ CommandCost CmdBuildRoadWaypoint(DoCommandFlag flags, TileIndex start_tile, Axis if (ret.Failed()) return ret; /* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */ - TileIndex center_tile = start_tile + (count / 2) * TileOffsByDiagDir(AxisToDiagDir(OtherAxis(axis)));; + TileIndex center_tile = start_tile + (count / 2) * TileOffsByAxis(OtherAxis(axis)); if (wp == nullptr && reuse) wp = FindDeletedWaypointCloseTo(center_tile, STR_SV_STNAME_WAYPOINT, _current_company, true); if (wp != nullptr) {