FIX: Proper fix for the tunnel bug.
[openttd-joker.git] / src / bridge_map.h
blobe044570553b5959aa31e9725620e61671249ddc6
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file bridge_map.h Map accessor functions for bridges. */
12 #ifndef BRIDGE_MAP_H
13 #define BRIDGE_MAP_H
15 #include "road_map.h"
16 #include "bridge.h"
18 /**
19 * Checks if this is a bridge, instead of a tunnel
20 * @param t The tile to analyze
21 * @pre IsTileType(t, MP_TUNNELBRIDGE)
22 * @return true if the structure is a bridge one
24 static inline bool IsBridge(TileIndex t)
26 assert(IsTileType(t, MP_TUNNELBRIDGE));
27 return HasBit(_m[t].m5, 7);
30 /**
31 * checks if there is a bridge on this tile
32 * @param t The tile to analyze
33 * @return true if a bridge is present
35 static inline bool IsBridgeTile(TileIndex t)
37 return IsTileType(t, MP_TUNNELBRIDGE) && IsBridge(t);
40 /**
41 * checks if a bridge is set above the ground of this tile
42 * @param t The tile to analyze
43 * @return true if a bridge is detected above
45 static inline bool IsBridgeAbove(TileIndex t)
47 return GB(_m[t].type, 2, 2) != 0;
50 /**
51 * Determines the type of bridge on a tile
52 * @param t The tile to analyze
53 * @pre IsBridgeTile(t)
54 * @return The bridge type
56 static inline BridgeType GetBridgeType(TileIndex t)
58 assert(IsBridgeTile(t));
59 return GB(_me[t].m6, 2, 4);
62 /**
63 * Get the axis of the bridge that goes over the tile. Not the axis or the ramp.
64 * @param t The tile to analyze
65 * @pre IsBridgeAbove(t)
66 * @return the above mentioned axis
68 static inline Axis GetBridgeAxis(TileIndex t)
70 assert(IsBridgeAbove(t));
71 return (Axis)(GB(_m[t].type, 2, 2) - 1);
74 TileIndex GetNorthernBridgeEnd(TileIndex t);
75 TileIndex GetSouthernBridgeEnd(TileIndex t);
76 TileIndex GetOtherBridgeEnd(TileIndex t);
78 int GetBridgeHeight(TileIndex tile);
79 /**
80 * Get the height ('z') of a bridge in pixels.
81 * @param tile the bridge ramp tile to get the bridge height from
82 * @return the height of the bridge in pixels
84 static inline int GetBridgePixelHeight(TileIndex tile)
86 return GetBridgeHeight(tile) * TILE_HEIGHT;
89 /**
90 * Remove the bridge over the given axis.
91 * @param t the tile to remove the bridge from
92 * @param a the axis of the bridge to remove
94 static inline void ClearSingleBridgeMiddle(TileIndex t, Axis a)
96 ClrBit(_m[t].type, 2 + a);
99 /**
100 * Removes bridges from the given, that is bridges along the X and Y axis.
101 * @param t the tile to remove the bridge from
103 static inline void ClearBridgeMiddle(TileIndex t)
105 ClearSingleBridgeMiddle(t, AXIS_X);
106 ClearSingleBridgeMiddle(t, AXIS_Y);
110 * Set that there is a bridge over the given axis.
111 * @param t the tile to add the bridge to
112 * @param a the axis of the bridge to add
114 static inline void SetBridgeMiddle(TileIndex t, Axis a)
116 SetBit(_m[t].type, 2 + a);
120 * Generic part to make a bridge ramp for both roads and rails.
121 * @param t the tile to make a bridge ramp
122 * @param o the new owner of the bridge ramp
123 * @param bridgetype the type of bridge this bridge ramp belongs to
124 * @param d the direction this ramp must be facing
125 * @param tt the transport type of the bridge
126 * @param rt the road or rail type
127 * @note this function should not be called directly.
129 static inline void MakeBridgeRamp(TileIndex t, Owner o, BridgeType bridgetype, DiagDirection d, TransportType tt, uint rt)
131 SetTileType(t, MP_TUNNELBRIDGE);
132 SetTileOwner(t, o);
133 _m[t].m2 = 0;
134 if (tt == TRANSPORT_RAIL) {
135 SB(_m[t].m1, 7, 1, GB(rt, 4, 1));
136 SB(_m[t].m3, 0, 4, GB(rt, 0, 4));
138 else {
139 _m[t].m3 = rt;
141 _m[t].m4 = 0;
142 _m[t].m5 = 1 << 7 | tt << 2 | d;
143 SB(_me[t].m6, 2, 4, bridgetype);
144 _me[t].m7 = 0;
148 * Make a bridge ramp for roads.
149 * @param t the tile to make a bridge ramp
150 * @param o the new owner of the bridge ramp
151 * @param owner_road the new owner of the road on the bridge
152 * @param owner_tram the new owner of the tram on the bridge
153 * @param bridgetype the type of bridge this bridge ramp belongs to
154 * @param d the direction this ramp must be facing
155 * @param r the road type of the bridge
157 static inline void MakeRoadBridgeRamp(TileIndex t, Owner o, Owner owner_road, Owner owner_tram, BridgeType bridgetype, DiagDirection d, RoadTypes r)
159 MakeBridgeRamp(t, o, bridgetype, d, TRANSPORT_ROAD, 0);
160 SetRoadOwner(t, ROADTYPE_ROAD, owner_road);
161 if (owner_tram != OWNER_TOWN) SetRoadOwner(t, ROADTYPE_TRAM, owner_tram);
162 SetRoadTypes(t, r);
166 * Make a bridge ramp for rails.
167 * @param t the tile to make a bridge ramp
168 * @param o the new owner of the bridge ramp
169 * @param bridgetype the type of bridge this bridge ramp belongs to
170 * @param d the direction this ramp must be facing
171 * @param r the rail type of the bridge
173 static inline void MakeRailBridgeRamp(TileIndex t, Owner o, BridgeType bridgetype, DiagDirection d, RailType r)
175 MakeBridgeRamp(t, o, bridgetype, d, TRANSPORT_RAIL, r);
179 * Make a bridge ramp for aqueducts.
180 * @param t the tile to make a bridge ramp
181 * @param o the new owner of the bridge ramp
182 * @param d the direction this ramp must be facing
184 static inline void MakeAqueductBridgeRamp(TileIndex t, Owner o, DiagDirection d)
186 MakeBridgeRamp(t, o, 0, d, TRANSPORT_WATER, 0);
190 * Checks if this road bridge head is a custom bridge head
191 * @param t The tile to analyze
192 * @pre IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_ROAD
193 * @return true if it is a custom bridge head
195 static inline bool IsRoadCustomBridgeHead(TileIndex t)
197 assert(IsBridgeTile(t) && (TransportType)GB(_m[t].m5, 2, 2) == TRANSPORT_ROAD);
198 return GB(_m[t].m2, 0, 8) != 0;
202 * Checks if this tile is a road bridge head with a custom bridge head
203 * @param t The tile to analyze
204 * @return true if it is a road bridge head with a custom bridge head
206 static inline bool IsRoadCustomBridgeHeadTile(TileIndex t)
208 return IsBridgeTile(t) && (TransportType)GB(_m[t].m5, 2, 2) == TRANSPORT_ROAD && IsRoadCustomBridgeHead(t);
212 * Returns the road bits for a (possibly custom) road bridge head
213 * @param t The tile to analyze
214 * @param rt Road type.
215 * @pre IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_ROAD
216 * @return road bits for the bridge head
218 static inline RoadBits GetCustomBridgeHeadRoadBits(TileIndex t, RoadType rt)
220 assert(IsBridgeTile(t));
221 if (!HasTileRoadType(t, rt)) return (RoadBits) 0;
222 RoadBits bits = (GB(_m[t].m5, 0, 1) ? ROAD_Y : ROAD_X) ^ (RoadBits) GB(_m[t].m2, rt == ROADTYPE_TRAM ? 4 : 0, 4);
223 return bits;
227 * Returns the road bits for a (possibly custom) road bridge head, for all road types
228 * @param t The tile to analyze
229 * @pre IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_ROAD
230 * @return road bits for the bridge head
232 static inline RoadBits GetCustomBridgeHeadAllRoadBits(TileIndex t)
234 return GetCustomBridgeHeadRoadBits(t, ROADTYPE_ROAD) | GetCustomBridgeHeadRoadBits(t, ROADTYPE_TRAM);
238 * Sets the road bits for a (possibly custom) road bridge head
239 * @param t The tile to modify
240 * @param rt Road type.
241 * @param bits The road bits.
242 * @pre IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_ROAD
243 * @pre HasTileRoadType() must be set correctly before calling this
245 static inline void SetCustomBridgeHeadRoadBits(TileIndex t, RoadType rt, RoadBits bits)
247 assert(IsBridgeTile(t));
248 if (HasTileRoadType(t, rt)) {
249 assert(bits != ROAD_NONE);
250 SB(_m[t].m2, rt == ROADTYPE_TRAM ? 4 : 0, 4, bits ^ (GB(_m[t].m5, 0, 1) ? ROAD_Y : ROAD_X));
251 } else {
252 assert(bits == ROAD_NONE);
253 SB(_m[t].m2, rt == ROADTYPE_TRAM ? 4 : 0, 4, 0);
257 #endif /* BRIDGE_MAP_H */