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 
25 {
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 
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 }
int32 TileIndexDiff
An offset value between to tiles.
Definition: map_func.h:156
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
Northeast, upper right on your monitor.
bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir)
Is there a tunnel in the way in the given direction?
Definition: tunnel_map.cpp:50
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:182
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:154
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:343
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Southeast.
Southwest.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
Northwest.
DiagDirection
Enumeration for diagonal directions.
static DiagDirection GetTunnelBridgeDirection(TileIndex t)
Get the direction pointing to the other end.
bool IsTunnelInWay(TileIndex tile, int z)
Is there a tunnel in the way in any direction?
Definition: tunnel_map.cpp:70
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:113
TileIndex GetOtherTunnelEnd(TileIndex tile)
Gets the other end of the tunnel.
Definition: tunnel_map.cpp:24
Functions that have tunnels and bridges in common.
static bool IsTunnelTile(TileIndex t)
Is this a tunnel (entrance)?
Definition: tunnel_map.h:35
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:104