2006-03-06 20:55:24 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file tunnel_map.cpp Map accessors for tunnels. */
|
2007-04-04 03:21:14 +00:00
|
|
|
|
2006-03-06 20:55:24 +00:00
|
|
|
#include "stdafx.h"
|
2007-12-16 15:38:51 +00:00
|
|
|
#include "tunnelbridge_map.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2006-03-06 20:55:24 +00:00
|
|
|
|
2007-04-18 18:20:31 +00:00
|
|
|
/**
|
|
|
|
* Gets the other end of the tunnel. Where a vehicle would reappear when it
|
|
|
|
* enters at the given tile.
|
|
|
|
* @param tile the tile to search from.
|
|
|
|
* @return the tile of the other end of the tunnel.
|
|
|
|
*/
|
2006-03-06 20:55:24 +00:00
|
|
|
TileIndex GetOtherTunnelEnd(TileIndex tile)
|
|
|
|
{
|
2007-12-16 15:38:51 +00:00
|
|
|
DiagDirection dir = GetTunnelBridgeDirection(tile);
|
2006-09-05 23:21:41 +00:00
|
|
|
TileIndexDiff delta = TileOffsByDiagDir(dir);
|
2011-11-04 11:30:37 +00:00
|
|
|
int z = GetTileZ(tile);
|
2006-03-06 20:55:24 +00:00
|
|
|
|
|
|
|
dir = ReverseDiagDir(dir);
|
|
|
|
do {
|
|
|
|
tile += delta;
|
|
|
|
} while (
|
2006-03-12 15:04:03 +00:00
|
|
|
!IsTunnelTile(tile) ||
|
2007-12-16 15:38:51 +00:00
|
|
|
GetTunnelBridgeDirection(tile) != dir ||
|
2011-11-04 10:28:31 +00:00
|
|
|
GetTileZ(tile) != z
|
2006-03-06 20:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return tile;
|
|
|
|
}
|
2006-03-07 07:51:05 +00:00
|
|
|
|
|
|
|
|
2007-04-18 18:20:31 +00:00
|
|
|
/**
|
|
|
|
* Is there a tunnel in the way in the given direction?
|
|
|
|
* @param tile the tile to search from.
|
|
|
|
* @param z the 'z' to search on.
|
|
|
|
* @param dir the direction to start searching to.
|
|
|
|
* @return true if and only if there is a tunnel.
|
|
|
|
*/
|
2011-11-08 19:44:41 +00:00
|
|
|
bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir)
|
2006-03-07 07:51:05 +00:00
|
|
|
{
|
2006-09-05 23:21:41 +00:00
|
|
|
TileIndexDiff delta = TileOffsByDiagDir(dir);
|
2011-11-08 19:44:41 +00:00
|
|
|
int height;
|
2006-03-07 07:51:05 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
tile -= delta;
|
2009-01-21 02:31:55 +00:00
|
|
|
if (!IsValidTile(tile)) return false;
|
2011-11-04 10:28:31 +00:00
|
|
|
height = GetTileZ(tile);
|
2006-03-07 07:51:05 +00:00
|
|
|
} while (z < height);
|
|
|
|
|
2010-11-20 15:44:24 +00:00
|
|
|
return z == height && IsTunnelTile(tile) && GetTunnelBridgeDirection(tile) == dir;
|
2006-03-07 07:51:05 +00:00
|
|
|
}
|
|
|
|
|
2007-04-18 18:20:31 +00:00
|
|
|
/**
|
|
|
|
* Is there a tunnel in the way in any direction?
|
|
|
|
* @param tile the tile to search from.
|
|
|
|
* @param z the 'z' to search on.
|
|
|
|
* @return true if and only if there is a tunnel.
|
|
|
|
*/
|
2011-11-08 19:44:41 +00:00
|
|
|
bool IsTunnelInWay(TileIndex tile, int z)
|
2006-03-07 07:51:05 +00:00
|
|
|
{
|
2010-11-20 15:44:24 +00:00
|
|
|
return IsTunnelInWayDir(tile, z, (TileX(tile) > (MapMaxX() / 2)) ? DIAGDIR_NE : DIAGDIR_SW) ||
|
|
|
|
IsTunnelInWayDir(tile, z, (TileY(tile) > (MapMaxY() / 2)) ? DIAGDIR_NW : DIAGDIR_SE);
|
2006-03-07 07:51:05 +00:00
|
|
|
}
|