src/tunnel_map.cpp
author skidd13
Sun, 02 Dec 2007 21:43:16 +0000
changeset 8004 1c54bc6f4bdf
parent 7323 20a18eb8efaf
child 8083 ad22eade501f
permissions -rw-r--r--
(svn r11563) -Codechange: Align the preprocessor code in stdafx.h with tabs
3154
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     1
/* $Id$ */
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     2
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 5584
diff changeset
     3
/** @file tunnel_map.cpp */
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 5584
diff changeset
     4
3154
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     5
#include "stdafx.h"
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     6
#include "openttd.h"
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     7
#include "tile.h"
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     8
#include "tunnel_map.h"
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     9
6489
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    10
/**
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    11
 * Gets the other end of the tunnel. Where a vehicle would reappear when it
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    12
 * enters at the given tile.
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    13
 * @param tile the tile to search from.
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    14
 * @return the tile of the other end of the tunnel.
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    15
 */
3154
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    16
TileIndex GetOtherTunnelEnd(TileIndex tile)
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    17
{
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    18
	DiagDirection dir = GetTunnelDirection(tile);
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4291
diff changeset
    19
	TileIndexDiff delta = TileOffsByDiagDir(dir);
3154
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    20
	uint z = GetTileZ(tile);
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    21
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    22
	dir = ReverseDiagDir(dir);
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    23
	do {
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    24
		tile += delta;
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    25
	} while (
3184
118a520164e4 (svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents: 3156
diff changeset
    26
		!IsTunnelTile(tile) ||
3154
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    27
		GetTunnelDirection(tile) != dir ||
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    28
		GetTileZ(tile) != z
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    29
	);
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    30
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    31
	return tile;
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    32
}
3156
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    33
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    34
6489
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    35
/**
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    36
 * Is there a tunnel in the way in the given direction?
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    37
 * @param tile the tile to search from.
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    38
 * @param z    the 'z' to search on.
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    39
 * @param dir  the direction to start searching to.
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    40
 * @return true if and only if there is a tunnel.
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    41
 */
7002
1bf6a62b0fcb (svn r10258) -Codechange: as we are now using int64 all over the place, it's better to use int64 variables in the string generating too instead of packing them into two int32s.
rubidium
parents: 6489
diff changeset
    42
bool IsTunnelInWayDir(TileIndex tile, uint z, DiagDirection dir)
3156
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    43
{
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4291
diff changeset
    44
	TileIndexDiff delta = TileOffsByDiagDir(dir);
3156
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    45
	uint height;
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    46
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    47
	do {
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    48
		tile -= delta;
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    49
		height = GetTileZ(tile);
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    50
	} while (z < height);
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    51
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    52
	return
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    53
		z == height &&
3184
118a520164e4 (svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents: 3156
diff changeset
    54
		IsTunnelTile(tile) &&
3156
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    55
		GetTunnelDirection(tile) == dir;
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    56
}
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    57
6489
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    58
/**
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    59
 * Is there a tunnel in the way in any direction?
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    60
 * @param tile the tile to search from.
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    61
 * @param z the 'z' to search on.
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    62
 * @return true if and only if there is a tunnel.
511474c82dd3 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6422
diff changeset
    63
 */
3156
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    64
bool IsTunnelInWay(TileIndex tile, uint z)
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    65
{
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    66
	return
7323
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 7002
diff changeset
    67
		IsTunnelInWayDir(tile, z, (TileX(tile) > (MapMaxX() / 2)) ? DIAGDIR_NE : DIAGDIR_SW) ||
20a18eb8efaf (svn r10686) -Fix [FS#1058]: determining whether there is a tunnel going under the lowered area is only needed in two directions instead of all four, so take the directions (one for each axis) to the nearest border (along the given axis). Furthermore GetTileZ did much more than absolutely necessary.
rubidium
parents: 7002
diff changeset
    68
		IsTunnelInWayDir(tile, z, (TileY(tile) > (MapMaxY() / 2)) ? DIAGDIR_NW : DIAGDIR_SE);
3156
f4caf4197189 (svn r3779) Move CheckTunnelInWay() to a more appropriate place, invert its result and give it a less ambiguous name (IsTunnelInWay)
tron
parents: 3154
diff changeset
    69
}