src/tunnel_map.cpp
author rubidium
Wed, 19 Dec 2007 19:44:29 +0000
changeset 8602 9e75e594a068
parent 8579 3efbb430092e
child 8604 8afdd9877afd
permissions -rw-r--r--
(svn r11667) -Codechange: split window.h into a header that defines some 'global' window related types, on that defined 'global' window functions and one that defines functions and types only used by *_gui.cpps.
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     1
/* $Id$ */
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     2
6918
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 5835
diff changeset
     3
/** @file tunnel_map.cpp */
5589c415e28f (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 5835
diff changeset
     4
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     5
#include "stdafx.h"
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     6
#include "openttd.h"
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     7
#include "tile.h"
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
     8
#include "tunnel_map.h"
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 7819
diff changeset
     9
#include "tunnelbridge_map.h"
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 7819
diff changeset
    10
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    11
6985
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    12
/**
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    13
 * Gets the other end of the tunnel. Where a vehicle would reappear when it
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    14
 * enters at the given tile.
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    15
 * @param tile the tile to search from.
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    16
 * @return the tile of the other end of the tunnel.
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    17
 */
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    18
TileIndex GetOtherTunnelEnd(TileIndex tile)
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    19
{
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 7819
diff changeset
    20
	DiagDirection dir = GetTunnelBridgeDirection(tile);
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4291
diff changeset
    21
	TileIndexDiff delta = TileOffsByDiagDir(dir);
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    22
	uint z = GetTileZ(tile);
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    23
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    24
	dir = ReverseDiagDir(dir);
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    25
	do {
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    26
		tile += delta;
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    27
	} while (
3184
7405329343ce (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
    28
		!IsTunnelTile(tile) ||
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 7819
diff changeset
    29
		GetTunnelBridgeDirection(tile) != dir ||
3154
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    30
		GetTileZ(tile) != z
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    31
	);
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    32
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    33
	return tile;
a8fffb204d0e (svn r3777) Add some functions to handle tunnels
tron
parents:
diff changeset
    34
}
3156
028b6756b279 (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
    35
028b6756b279 (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
    36
6985
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    37
/**
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    38
 * Is there a tunnel in the way in the given direction?
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    39
 * @param tile the tile to search from.
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    40
 * @param z    the 'z' to search on.
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    41
 * @param dir  the direction to start searching to.
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    42
 * @return true if and only if there is a tunnel.
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    43
 */
7498
ce6588257e8d (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: 6985
diff changeset
    44
bool IsTunnelInWayDir(TileIndex tile, uint z, DiagDirection dir)
3156
028b6756b279 (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
{
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4291
diff changeset
    46
	TileIndexDiff delta = TileOffsByDiagDir(dir);
3156
028b6756b279 (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
	uint height;
028b6756b279 (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
028b6756b279 (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
	do {
028b6756b279 (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
		tile -= delta;
028b6756b279 (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
		height = GetTileZ(tile);
028b6756b279 (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
	} while (z < height);
028b6756b279 (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
028b6756b279 (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
    54
	return
028b6756b279 (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
		z == height &&
3184
7405329343ce (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
    56
		IsTunnelTile(tile) &&
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 7819
diff changeset
    57
		GetTunnelBridgeDirection(tile) == dir;
3156
028b6756b279 (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
    58
}
028b6756b279 (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
    59
6985
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    60
/**
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    61
 * Is there a tunnel in the way in any direction?
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    62
 * @param tile the tile to search from.
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    63
 * @param z the 'z' to search on.
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    64
 * @return true if and only if there is a tunnel.
6fd300b20503 (svn r9670) -Documentation: add documentation to some the _map files.
rubidium
parents: 6918
diff changeset
    65
 */
3156
028b6756b279 (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
bool IsTunnelInWay(TileIndex tile, uint z)
028b6756b279 (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
    67
{
028b6756b279 (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
    68
	return
7819
694a7dacd30a (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: 7498
diff changeset
    69
		IsTunnelInWayDir(tile, z, (TileX(tile) > (MapMaxX() / 2)) ? DIAGDIR_NE : DIAGDIR_SW) ||
694a7dacd30a (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: 7498
diff changeset
    70
		IsTunnelInWayDir(tile, z, (TileY(tile) > (MapMaxY() / 2)) ? DIAGDIR_NW : DIAGDIR_SE);
3156
028b6756b279 (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
    71
}