Fix #9521: Don't load at just removed docks that were part of a multi-dock station...
[openttd-github.git] / src / tunnel_map.cpp
blob9de52358013e85fa56f72789be558962c29b0974
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 tunnel_map.cpp Map accessors for tunnels. */
10 #include "stdafx.h"
11 #include "tunnelbridge_map.h"
13 #include "safeguards.h"
16 /**
17 * Gets the other end of the tunnel. Where a vehicle would reappear when it
18 * enters at the given tile.
19 * @param tile the tile to search from.
20 * @return the tile of the other end of the tunnel.
22 TileIndex GetOtherTunnelEnd(TileIndex tile)
24 DiagDirection dir = GetTunnelBridgeDirection(tile);
25 TileIndexDiff delta = TileOffsByDiagDir(dir);
26 int z = GetTileZ(tile);
28 dir = ReverseDiagDir(dir);
29 do {
30 tile += delta;
31 } while (
32 !IsTunnelTile(tile) ||
33 GetTunnelBridgeDirection(tile) != dir ||
34 GetTileZ(tile) != z
37 return tile;
41 /**
42 * Is there a tunnel in the way in the given direction?
43 * @param tile the tile to search from.
44 * @param z the 'z' to search on.
45 * @param dir the direction to start searching to.
46 * @return true if and only if there is a tunnel.
48 bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir)
50 TileIndexDiff delta = TileOffsByDiagDir(dir);
51 int height;
53 do {
54 tile -= delta;
55 if (!IsValidTile(tile)) return false;
56 height = GetTileZ(tile);
57 } while (z < height);
59 return z == height && IsTunnelTile(tile) && GetTunnelBridgeDirection(tile) == dir;
62 /**
63 * Is there a tunnel in the way in any direction?
64 * @param tile the tile to search from.
65 * @param z the 'z' to search on.
66 * @return true if and only if there is a tunnel.
68 bool IsTunnelInWay(TileIndex tile, int z)
70 return IsTunnelInWayDir(tile, z, (TileX(tile) > (MapMaxX() / 2)) ? DIAGDIR_NE : DIAGDIR_SW) ||
71 IsTunnelInWayDir(tile, z, (TileY(tile) > (MapMaxY() / 2)) ? DIAGDIR_NW : DIAGDIR_SE);