OpenTTD Source: src/tunnel_map.cpp Source File

OpenTTD Source 15.3

tunnel_map.cpp
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "tunnelbridge_map.h"
12
13#include "safeguards.h"
14
15
22 TileIndex GetOtherTunnelEnd(TileIndex tile)
23{
24 DiagDirection dir = GetTunnelBridgeDirection(tile);
25 TileIndexDiff delta = TileOffsByDiagDir(dir);
26 int z = GetTileZ(tile);
27
28 dir = ReverseDiagDir(dir);
29 do {
30 tile += delta;
31 } while (
32 !IsTunnelTile(tile) ||
33 GetTunnelBridgeDirection(tile) != dir ||
34 GetTileZ(tile) != z
35 );
36
37 return tile;
38}
39
40
48 bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir)
49{
50 TileIndexDiff delta = TileOffsByDiagDir(dir);
51 int height;
52
53 do {
54 tile -= delta;
55 if (!IsValidTile(tile)) return false;
56 height = GetTileZ(tile);
57 } while (z < height);
58
59 return z == height && IsTunnelTile(tile) && GetTunnelBridgeDirection(tile) == dir;
60}
61
68 bool IsTunnelInWay(TileIndex tile, int z)
69{
70 return IsTunnelInWayDir(tile, z, (TileX(tile) > (Map::MaxX() / 2)) ? DIAGDIR_NE : DIAGDIR_SW) ||
71 IsTunnelInWayDir(tile, z, (TileY(tile) > (Map::MaxY() / 2)) ? DIAGDIR_NW : DIAGDIR_SE);
72}
ReverseDiagDir
DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Definition direction_func.h:118
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
DIAGDIR_NE
@ DIAGDIR_NE
Northeast, upper right on your monitor.
Definition direction_type.h:80
DIAGDIR_NW
@ DIAGDIR_NW
Northwest.
Definition direction_type.h:83
DIAGDIR_SE
@ DIAGDIR_SE
Southeast.
Definition direction_type.h:81
DIAGDIR_SW
@ DIAGDIR_SW
Southwest.
Definition direction_type.h:82
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:438
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:428
TileOffsByDiagDir
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:583
TileIndexDiff
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
Map::MaxY
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition map_func.h:308
Map::MaxX
static uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition map_func.h:299
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.cpp:116
IsValidTile
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition tile_map.h:161
IsTunnelInWay
bool IsTunnelInWay(TileIndex tile, int z)
Is there a tunnel in the way in any direction?
Definition tunnel_map.cpp:68
IsTunnelInWayDir
bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir)
Is there a tunnel in the way in the given direction?
Definition tunnel_map.cpp:48
GetOtherTunnelEnd
TileIndex GetOtherTunnelEnd(TileIndex tile)
Gets the other end of the tunnel.
Definition tunnel_map.cpp:22
IsTunnelTile
bool IsTunnelTile(Tile t)
Is this a tunnel (entrance)?
Definition tunnel_map.h:34
tunnelbridge_map.h
Functions that have tunnels and bridges in common.
GetTunnelBridgeDirection
DiagDirection GetTunnelBridgeDirection(Tile t)
Get the direction pointing to the other end.
Definition tunnelbridge_map.h:26