OpenTTD

tunnel_map.cpp
Go to the documentation of this file.
1 /* $Id: tunnel_map.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
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 */
9
12 #include "stdafx.h"
13 #include "tunnelbridge_map.h"
14
15 #include "safeguards.h"
16
17
24 TileIndex GetOtherTunnelEnd(TileIndex tile)
25 {
26 DiagDirection dir = GetTunnelBridgeDirection(tile);
27 TileIndexDiff delta = TileOffsByDiagDir(dir);
28 int z = GetTileZ(tile);
29
30 dir = ReverseDiagDir(dir);
31 do {
32 tile += delta;
33 } while (
34 !IsTunnelTile(tile) ||
35 GetTunnelBridgeDirection(tile) != dir ||
36 GetTileZ(tile) != z
37 );
38
39 return tile;
40 }
41
42
50 bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir)
51 {
52 TileIndexDiff delta = TileOffsByDiagDir(dir);
53 int height;
54
55 do {
56 tile -= delta;
57 if (!IsValidTile(tile)) return false;
58 height = GetTileZ(tile);
59 } while (z < height);
60
61 return z == height && IsTunnelTile(tile) && GetTunnelBridgeDirection(tile) == dir;
62 }
63
70 bool IsTunnelInWay(TileIndex tile, int z)
71 {
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);
74 }
TileIndexDiff
int32 TileIndexDiff
An offset value between to tiles.
Definition: map_func.h:156
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
DIAGDIR_NE
Northeast, upper right on your monitor.
Definition: direction_type.h:82
IsTunnelInWayDir
bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir)
Is there a tunnel in the way in the given direction?
Definition: tunnel_map.cpp:50
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:182
IsValidTile
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:154
TileOffsByDiagDir
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:343
ReverseDiagDir
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Definition: direction_func.h:120
DIAGDIR_SE
Southeast.
Definition: direction_type.h:83
DIAGDIR_SW
Southwest.
Definition: direction_type.h:84
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
safeguards.h
A number of safeguards to prevent using unsafe methods.
DIAGDIR_NW
Northwest.
Definition: direction_type.h:85
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:80
GetTunnelBridgeDirection
static DiagDirection GetTunnelBridgeDirection(TileIndex t)
Get the direction pointing to the other end.
Definition: tunnelbridge_map.h:28
IsTunnelInWay
bool IsTunnelInWay(TileIndex tile, int z)
Is there a tunnel in the way in any direction?
Definition: tunnel_map.cpp:70
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
MapMaxY
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:113
GetOtherTunnelEnd
TileIndex GetOtherTunnelEnd(TileIndex tile)
Gets the other end of the tunnel.
Definition: tunnel_map.cpp:24
tunnelbridge_map.h
Functions that have tunnels and bridges in common.
IsTunnelTile
static bool IsTunnelTile(TileIndex t)
Is this a tunnel (entrance)?
Definition: tunnel_map.h:35
MapMaxX
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:104