Rework return statement in FlowsDown
[openttd/fttd.git] / src / bridge.h
blobe1a13b5713319b18b25a873e94fe6fb5d58c0203
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.h Header file for bridges */
12 #ifndef BRIDGE_H
13 #define BRIDGE_H
15 #include "gfx_type.h"
16 #include "tile_cmd.h"
17 #include "slope_func.h"
18 #include "viewport_func.h"
20 /**
21 * This enum is related to the definition of bridge pieces,
22 * which is used to determine the proper sprite table to use
23 * while drawing a given bridge part.
25 enum BridgePieces {
26 BRIDGE_PIECE_NORTH = 0,
27 BRIDGE_PIECE_SOUTH,
28 BRIDGE_PIECE_INNER_NORTH,
29 BRIDGE_PIECE_INNER_SOUTH,
30 BRIDGE_PIECE_MIDDLE_ODD,
31 BRIDGE_PIECE_MIDDLE_EVEN,
32 BRIDGE_PIECE_HEAD,
33 BRIDGE_PIECE_INVALID,
36 DECLARE_POSTFIX_INCREMENT(BridgePieces)
38 static const uint MAX_BRIDGES = 13; ///< Maximal number of available bridge specs.
40 typedef uint BridgeType; ///< Bridge spec number.
42 /**
43 * Struct containing information about a single bridge type
45 struct BridgeSpec {
46 Year avail_year; ///< the year where it becomes available
47 byte min_length; ///< the minimum length (not counting start and end tile)
48 uint16 max_length; ///< the maximum length (not counting start and end tile)
49 uint16 price; ///< the price multiplier
50 uint16 speed; ///< maximum travel speed (1 unit = 1/1.6 mph = 1 km-ish/h)
51 SpriteID sprite; ///< the sprite which is used in the GUI
52 PaletteID pal; ///< the palette which is used in the GUI
53 StringID material; ///< the string that contains the bridge description
54 StringID transport_name[2]; ///< description of the bridge, when built for road or rail
55 PalSpriteID **sprite_table; ///< table of sprites for drawing the bridge
56 byte flags; ///< bit 0 set: disable drawing of far pillars.
59 extern BridgeSpec _bridge[MAX_BRIDGES];
61 Foundation GetBridgeFoundation(Slope tileh, Axis axis);
62 bool HasBridgeFlatRamp(Slope tileh, Axis axis);
64 /**
65 * Get the height increase for a bridge ramp
66 * @param dir Bridge direction
67 * @param x x within the tile
68 * @param y y within the tile
70 static inline int GetBridgePartialPixelZ(DiagDirection dir, uint x, uint y)
72 assert(x < TILE_SIZE);
73 assert(y < TILE_SIZE);
75 return DistanceFromTileEdge(ReverseDiagDir(dir), x, y) / 2 + 1;
78 /**
79 * Get the direction(s) to which rail/road can be connected in an extended bridgehead.
80 * @param tileh Slope of the tile
81 * @param dir Direction of the bridge
82 * @return DIAGDIRDIFF_SAME for all directions allowed; DIAGDIRDIFF_REVERSE for none (invalid slope); else invalid direction as a difference from dir
84 static inline DiagDirDiff CheckExtendedBridgeHead(Slope tileh, DiagDirection dir)
86 extern const Slope bridgehead_valid_slopes[DIAGDIR_END][2];
88 if (tileh == InclinedSlope(ReverseDiagDir(dir))) {
89 return DIAGDIRDIFF_SAME;
90 } else if (tileh == bridgehead_valid_slopes[dir][0]) {
91 return DIAGDIRDIFF_90RIGHT;
92 } else if (tileh == bridgehead_valid_slopes[dir][1]) {
93 return DIAGDIRDIFF_90LEFT;
94 } else {
95 return DIAGDIRDIFF_REVERSE;
99 /**
100 * Get the specification of a bridge type.
101 * @param i The type of bridge to get the specification for.
102 * @return The specification.
104 static inline const BridgeSpec *GetBridgeSpec(BridgeType i)
106 assert(i < lengthof(_bridge));
107 return &_bridge[i];
110 int GetBridgeHeight(TileIndex tile);
112 * Get the height ('z') of a bridge in pixels.
113 * @param tile the bridge ramp tile to get the bridge height from
114 * @return the height of the bridge in pixels
116 static inline int GetBridgePixelHeight(TileIndex tile)
118 return GetBridgeHeight(tile) * TILE_HEIGHT;
121 void DrawBridgeTramBits(int x, int y, int z, int offset, bool overlay, bool head);
122 void DrawBridgeMiddle(const TileInfo *ti);
123 void DrawBridgeGround(TileInfo *ti);
124 const PalSpriteID *GetBridgeRampSprite(int index, int offset, Slope slope, DiagDirection dir);
125 void DrawAqueductRamp(TileInfo *ti);
127 CommandCost CheckBridgeAvailability(BridgeType bridge_type, uint bridge_len, DoCommandFlag flags = DC_NONE);
128 int CalcBridgeLenCostFactor(int x);
130 void ResetBridges();
132 CommandCost CheckBridgeTiles(TileIndex tile1, TileIndex tile2, Axis *axis);
133 CommandCost CheckBridgeBuildable(TileIndex tile1, TileIndex tile2, DoCommandFlag flags, bool clear1, bool clear2, bool restricted = false);
134 CommandCost CheckBridgeSlope(DiagDirection dir, Slope *tileh, int *z);
136 static inline void MarkBridgeTilesDirty(TileIndex start, TileIndex end, DiagDirection dir, bool first = true)
138 assert(DiagdirBetweenTiles(start, end) == dir);
140 TileIndexDiff delta = TileOffsByDiagDir(dir);
141 for (TileIndex tile = (first ? start : start + delta); tile != end; tile += delta) {
142 MarkTileDirtyByTile(tile);
144 MarkTileDirtyByTile(end);
147 void SetBridgeMiddleTiles(TileIndex tile1, TileIndex tile2, Axis direction);
148 void RemoveBridgeMiddleTiles(TileIndex tile1, TileIndex tile2);
150 #endif /* BRIDGE_H */