# tunnel_map.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "tunnelbridge_map.h"
    00014 #include "station_map.h"
    00015
    00016
    00023 TileIndex GetOtherTunnelEnd(TileIndex tile)
    00024 {
    00025   DiagDirection dir = GetTunnelBridgeDirection(tile);
    00026   TileIndexDiff delta = TileOffsByDiagDir(dir);
    00027   uint z = GetTileZ(tile);
    00028
    00029   dir = ReverseDiagDir(dir);
    00030   do {
    00031     tile += delta;
    00032   } while (
    00033     !IsTunnelTile(tile) ||
    00034     GetTunnelBridgeDirection(tile) != dir ||
    00035     GetTileZ(tile) != z
    00036   );
    00037
    00038   return tile;
    00039 }
    00040
    00048 bool IsWaterCrossingTunnel(TileIndex tile, DiagDirection dir)
    00049 {
    00050   TileIndexDiff delta = TileOffsByDiagDir(dir);
    00051   int height;
    00052   /* Initialize amount of tiles allowed to be passed backwards to find crossing water tunnel entrance.
    00053    * and stops iteration to save time. */
    00054   int tiles = 5;
    00055
    00056   do {
    00057     tile -= delta;
    00058     if (!IsValidTile(tile)) return false;
    00059     height = GetTileZ(tile);
    00060     tiles--;
    00061   } while (height > 0 && tiles > 0);
    00062
    00063   return height == 0 && IsTunnelTile(tile) && GetTunnelBridgeDirection(tile) == dir;
    00064 }
    00065
    00073 bool IsTunnelInWayDir(TileIndex tile, uint z, DiagDirection dir)
    00074 {
    00075   TileIndexDiff delta = TileOffsByDiagDir(dir);
    00076   uint height;
    00077
    00078   do {
    00079     tile -= delta;
    00080     if (!IsValidTile(tile)) return false;
    00081     height = GetTileZ(tile);
    00082   } while (z < height ||
    00083       (height == 0 &&
    00084       GetTileMaxZ(tile) == 0 && // Flat tile.
    00085       (IsTileType(tile, MP_WATER) || IsBuoyTile(tile))));
    00086
    00087   if (z == height) {
    00088     if (IsTunnelTile(tile)) return GetTunnelBridgeDirection(tile) == dir;
    00089
    00090     if (height == 0) {
    00091       DiagDirection direction_slope = GetInclinedSlopeDirection(GetTileSlope(tile, NULL));
    00092       if (direction_slope == dir || direction_slope == ReverseDiagDir(dir)) {
    00093         return IsWaterCrossingTunnel(tile, ReverseDiagDir(direction_slope));
    00094       }
    00095     }
    00096   }
    00097   return false;
    00098 }
    00099
    00106 bool IsTunnelInWay(TileIndex tile, uint z)
    00107 {
    00108   return IsTunnelInWayDir(tile, z, (TileX(tile) > (MapMaxX() / 2)) ? DIAGDIR_NE : DIAGDIR_SW) ||
    00109       IsTunnelInWayDir(tile, z, (TileY(tile) > (MapMaxY() / 2)) ? DIAGDIR_NW : DIAGDIR_SE);
    00110 }
    00111
    00112 bool IsTunnelBelow(TileIndex tile)
    00113 {
    00114   uint32 z = GetTileZ(tile);
    00115   while (z > 0) {
    00116     z -= TILE_HEIGHT;
    00117     if (IsTunnelInWay(tile, z)) return true;
    00118   }
    00119   return false;
    00120 }