Update readme.md
[openttd-joker.git] / src / bridge_map.h
blob34187ed92dd5e31a1303ce45bc55a65b1da90169
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 = INVALID_ROADTYPES;
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
156 * @param upgrade whether the bridge is an upgrade instead of a totally new bridge
158 static inline void MakeRoadBridgeRamp(TileIndex t, Owner o, Owner owner_road, Owner owner_tram, BridgeType bridgetype, DiagDirection d, RoadTypeIdentifiers r, bool upgrade)
160 // Backup custom bridgehead data.
161 auto m2_backup = _m[t].m2;
163 MakeBridgeRamp(t, o, bridgetype, d, TRANSPORT_ROAD, 0);
164 SetRoadOwner(t, ROADTYPE_ROAD, owner_road);
165 if (owner_tram != OWNER_TOWN) SetRoadOwner(t, ROADTYPE_TRAM, owner_tram);
166 SetRoadTypes(t, r);
168 // Restore custom bridgehead data if we're upgrading an existing bridge.
169 if (upgrade) {
170 for (int i = 0; i < 8; ++i) {
171 if (HasBit(m2_backup, i)) SetBit(_m[t].m2, i);
177 * Make a bridge ramp for rails.
178 * @param t the tile to make a bridge ramp
179 * @param o the new owner of the bridge ramp
180 * @param bridgetype the type of bridge this bridge ramp belongs to
181 * @param d the direction this ramp must be facing
182 * @param r the rail type of the bridge
183 * @param upgrade whether the bridge is an upgrade instead of a totally new bridge
185 static inline void MakeRailBridgeRamp(TileIndex t, Owner o, BridgeType bridgetype, DiagDirection d, RailType r, bool upgrade)
187 // Backup bridge signal data.
188 auto m2_backup = _m[t].m2;
189 auto m5_backup = _m[t].m5;
190 auto m6_backup = _me[t].m6;
192 MakeBridgeRamp(t, o, bridgetype, d, TRANSPORT_RAIL, r);
194 // Restore bridge signal data if we're upgrading an existing bridge.
195 if (upgrade) {
196 _m[t].m2 = m2_backup;
198 if (HasBit(m5_backup, 5)) SetBit(_m[t].m5, 5);
199 if (HasBit(m5_backup, 6)) SetBit(_m[t].m5, 6);
201 if (HasBit(m6_backup, 0)) SetBit(_me[t].m6, 0);
202 if (HasBit(m6_backup, 1)) SetBit(_me[t].m6, 1);
203 if (HasBit(m6_backup, 6)) SetBit(_me[t].m6, 6);
208 * Make a bridge ramp for aqueducts.
209 * @param t the tile to make a bridge ramp
210 * @param o the new owner of the bridge ramp
211 * @param d the direction this ramp must be facing
213 static inline void MakeAqueductBridgeRamp(TileIndex t, Owner o, DiagDirection d)
215 MakeBridgeRamp(t, o, 0, d, TRANSPORT_WATER, 0);
219 * Checks if this road bridge head is a custom bridge head
220 * @param t The tile to analyze
221 * @pre IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_ROAD
222 * @return true if it is a custom bridge head
224 static inline bool IsRoadCustomBridgeHead(TileIndex t)
226 assert(IsBridgeTile(t) && (TransportType)GB(_m[t].m5, 2, 2) == TRANSPORT_ROAD);
227 return GB(_m[t].m2, 0, 8) != 0;
231 * Checks if this tile is a road bridge head with a custom bridge head
232 * @param t The tile to analyze
233 * @return true if it is a road bridge head with a custom bridge head
235 static inline bool IsRoadCustomBridgeHeadTile(TileIndex t)
237 return IsBridgeTile(t) && (TransportType)GB(_m[t].m5, 2, 2) == TRANSPORT_ROAD && IsRoadCustomBridgeHead(t);
241 * Returns the road bits for a (possibly custom) road bridge head
242 * @param t The tile to analyze
243 * @param rt Road type.
244 * @pre IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_ROAD
245 * @return road bits for the bridge head
247 static inline RoadBits GetCustomBridgeHeadRoadBits(TileIndex t, RoadType rt)
249 assert(IsBridgeTile(t));
250 if (!HasTileRoadType(t, rt)) return (RoadBits) 0;
251 RoadBits bits = (GB(_m[t].m5, 0, 1) ? ROAD_Y : ROAD_X) ^ (RoadBits) GB(_m[t].m2, rt == ROADTYPE_TRAM ? 4 : 0, 4);
252 return bits;
256 * Returns the road bits for a (possibly custom) road bridge head, for all road types
257 * @param t The tile to analyze
258 * @pre IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_ROAD
259 * @return road bits for the bridge head
261 static inline RoadBits GetCustomBridgeHeadAllRoadBits(TileIndex t)
263 return GetCustomBridgeHeadRoadBits(t, ROADTYPE_ROAD) | GetCustomBridgeHeadRoadBits(t, ROADTYPE_TRAM);
267 * Sets the road bits for a (possibly custom) road bridge head
268 * @param t The tile to modify
269 * @param rt Road type.
270 * @param bits The road bits.
271 * @pre IsBridgeTile(t) && GetTunnelBridgeTransportType(t) == TRANSPORT_ROAD
272 * @pre HasTileRoadType() must be set correctly before calling this
274 static inline void SetCustomBridgeHeadRoadBits(TileIndex t, RoadType rt, RoadBits bits)
276 assert(IsBridgeTile(t));
277 if (HasTileRoadType(t, rt)) {
278 assert(bits != ROAD_NONE);
279 SB(_m[t].m2, rt == ROADTYPE_TRAM ? 4 : 0, 4, bits ^ (GB(_m[t].m5, 0, 1) ? ROAD_Y : ROAD_X));
280 } else {
281 assert(bits == ROAD_NONE);
282 SB(_m[t].m2, rt == ROADTYPE_TRAM ? 4 : 0, 4, 0);
286 #endif /* BRIDGE_MAP_H */