(svn r27707) -Fix: Make the result of NewGRF's CARGO_NAME string code reliably print...
[openttd.git] / src / tunnel_map.cpp
blob4e6d5a7e1391e8fbab03481d0a00620367f73270
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file tunnel_map.cpp Map accessors for tunnels. */
12 #include "stdafx.h"
13 #include "tunnelbridge_map.h"
15 #include "safeguards.h"
18 /**
19 * Gets the other end of the tunnel. Where a vehicle would reappear when it
20 * enters at the given tile.
21 * @param tile the tile to search from.
22 * @return the tile of the other end of the tunnel.
24 TileIndex GetOtherTunnelEnd(TileIndex tile)
26 DiagDirection dir = GetTunnelBridgeDirection(tile);
27 TileIndexDiff delta = TileOffsByDiagDir(dir);
28 int z = GetTileZ(tile);
30 dir = ReverseDiagDir(dir);
31 do {
32 tile += delta;
33 } while (
34 !IsTunnelTile(tile) ||
35 GetTunnelBridgeDirection(tile) != dir ||
36 GetTileZ(tile) != z
39 return tile;
43 /**
44 * Is there a tunnel in the way in the given direction?
45 * @param tile the tile to search from.
46 * @param z the 'z' to search on.
47 * @param dir the direction to start searching to.
48 * @return true if and only if there is a tunnel.
50 bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir)
52 TileIndexDiff delta = TileOffsByDiagDir(dir);
53 int height;
55 do {
56 tile -= delta;
57 if (!IsValidTile(tile)) return false;
58 height = GetTileZ(tile);
59 } while (z < height);
61 return z == height && IsTunnelTile(tile) && GetTunnelBridgeDirection(tile) == dir;
64 /**
65 * Is there a tunnel in the way in any direction?
66 * @param tile the tile to search from.
67 * @param z the 'z' to search on.
68 * @return true if and only if there is a tunnel.
70 bool IsTunnelInWay(TileIndex tile, int z)
72 return IsTunnelInWayDir(tile, z, (TileX(tile) > (MapMaxX() / 2)) ? DIAGDIR_NE : DIAGDIR_SW) ||
73 IsTunnelInWayDir(tile, z, (TileY(tile) > (MapMaxY() / 2)) ? DIAGDIR_NW : DIAGDIR_SE);