1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file tunnelbridge_map.h Functions that have tunnels and bridges in common */
9 
10 #ifndef TUNNELBRIDGE_MAP_H
11 #define TUNNELBRIDGE_MAP_H
12 
13 #include "bridge_map.h"
14 #include "tunnel_map.h"
15 
16 
17 /**
18  * Get the direction pointing to the other end.
19  *
20  * Tunnel: Get the direction facing into the tunnel
21  * Bridge: Get the direction pointing onto the bridge
22  * @param t The tile to analyze
23  * @pre IsTileType(t, MP_TUNNELBRIDGE)
24  * @return the above mentioned direction
25  */
GetTunnelBridgeDirection(TileIndex t)26 static inline DiagDirection GetTunnelBridgeDirection(TileIndex t)
27 {
28 	assert(IsTileType(t, MP_TUNNELBRIDGE));
29 	return (DiagDirection)GB(_m[t].m5, 0, 2);
30 }
31 
32 /**
33  * Tunnel: Get the transport type of the tunnel (road or rail)
34  * Bridge: Get the transport type of the bridge's ramp
35  * @param t The tile to analyze
36  * @pre IsTileType(t, MP_TUNNELBRIDGE)
37  * @return the transport type in the tunnel/bridge
38  */
GetTunnelBridgeTransportType(TileIndex t)39 static inline TransportType GetTunnelBridgeTransportType(TileIndex t)
40 {
41 	assert(IsTileType(t, MP_TUNNELBRIDGE));
42 	return (TransportType)GB(_m[t].m5, 2, 2);
43 }
44 
45 /**
46  * Tunnel: Is this tunnel entrance in a snowy or desert area?
47  * Bridge: Does the bridge ramp lie in a snow or desert area?
48  * @param t The tile to analyze
49  * @pre IsTileType(t, MP_TUNNELBRIDGE)
50  * @return true if and only if the tile is in a snowy/desert area
51  */
HasTunnelBridgeSnowOrDesert(TileIndex t)52 static inline bool HasTunnelBridgeSnowOrDesert(TileIndex t)
53 {
54 	assert(IsTileType(t, MP_TUNNELBRIDGE));
55 	return HasBit(_me[t].m7, 5);
56 }
57 
58 /**
59  * Tunnel: Places this tunnel entrance in a snowy or desert area, or takes it out of there.
60  * Bridge: Sets whether the bridge ramp lies in a snow or desert area.
61  * @param t the tunnel entrance / bridge ramp tile
62  * @param snow_or_desert is the entrance/ramp in snow or desert (true), when
63  *                       not in snow and not in desert false
64  * @pre IsTileType(t, MP_TUNNELBRIDGE)
65  */
SetTunnelBridgeSnowOrDesert(TileIndex t,bool snow_or_desert)66 static inline void SetTunnelBridgeSnowOrDesert(TileIndex t, bool snow_or_desert)
67 {
68 	assert(IsTileType(t, MP_TUNNELBRIDGE));
69 	SB(_me[t].m7, 5, 1, snow_or_desert);
70 }
71 
72 /**
73  * Determines type of the wormhole and returns its other end
74  * @param t one end
75  * @pre IsTileType(t, MP_TUNNELBRIDGE)
76  * @return other end
77  */
GetOtherTunnelBridgeEnd(TileIndex t)78 static inline TileIndex GetOtherTunnelBridgeEnd(TileIndex t)
79 {
80 	assert(IsTileType(t, MP_TUNNELBRIDGE));
81 	return IsTunnel(t) ? GetOtherTunnelEnd(t) : GetOtherBridgeEnd(t);
82 }
83 
84 
85 /**
86  * Get the reservation state of the rail tunnel/bridge
87  * @pre IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL
88  * @param t the tile
89  * @return reservation state
90  */
HasTunnelBridgeReservation(TileIndex t)91 static inline bool HasTunnelBridgeReservation(TileIndex t)
92 {
93 	assert(IsTileType(t, MP_TUNNELBRIDGE));
94 	assert(GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL);
95 	return HasBit(_m[t].m5, 4);
96 }
97 
98 /**
99  * Set the reservation state of the rail tunnel/bridge
100  * @pre IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL
101  * @param t the tile
102  * @param b the reservation state
103  */
SetTunnelBridgeReservation(TileIndex t,bool b)104 static inline void SetTunnelBridgeReservation(TileIndex t, bool b)
105 {
106 	assert(IsTileType(t, MP_TUNNELBRIDGE));
107 	assert(GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL);
108 	SB(_m[t].m5, 4, 1, b ? 1 : 0);
109 }
110 
111 /**
112  * Get the reserved track bits for a rail tunnel/bridge
113  * @pre IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL
114  * @param t the tile
115  * @return reserved track bits
116  */
GetTunnelBridgeReservationTrackBits(TileIndex t)117 static inline TrackBits GetTunnelBridgeReservationTrackBits(TileIndex t)
118 {
119 	return HasTunnelBridgeReservation(t) ? DiagDirToDiagTrackBits(GetTunnelBridgeDirection(t)) : TRACK_BIT_NONE;
120 }
121 
122 #endif /* TUNNELBRIDGE_MAP_H */
123