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 road_func.h Functions related to roads. */
9
10 #ifndef ROAD_FUNC_H
11 #define ROAD_FUNC_H
12
13 #include "core/bitmath_func.hpp"
14 #include "road.h"
15 #include "economy_func.h"
16 #include "transparency.h"
17
18 /**
19 * Whether the given roadtype is valid.
20 * @param r the roadtype to check for validness
21 * @return true if and only if valid
22 */
IsValidRoadBits(RoadBits r)23 static inline bool IsValidRoadBits(RoadBits r)
24 {
25 return r < ROAD_END;
26 }
27
28 /**
29 * Calculate the complement of a RoadBits value
30 *
31 * Simply flips all bits in the RoadBits value to get the complement
32 * of the RoadBits.
33 *
34 * @param r The given RoadBits value
35 * @return the complement
36 */
ComplementRoadBits(RoadBits r)37 static inline RoadBits ComplementRoadBits(RoadBits r)
38 {
39 assert(IsValidRoadBits(r));
40 return (RoadBits)(ROAD_ALL ^ r);
41 }
42
43 /**
44 * Calculate the mirrored RoadBits
45 *
46 * Simply move the bits to their new position.
47 *
48 * @param r The given RoadBits value
49 * @return the mirrored
50 */
MirrorRoadBits(RoadBits r)51 static inline RoadBits MirrorRoadBits(RoadBits r)
52 {
53 assert(IsValidRoadBits(r));
54 return (RoadBits)(GB(r, 0, 2) << 2 | GB(r, 2, 2));
55 }
56
57 /**
58 * Calculate rotated RoadBits
59 *
60 * Move the Roadbits clockwise until they are in their final position.
61 *
62 * @param r The given RoadBits value
63 * @param rot The given Rotation angle
64 * @return the rotated
65 */
RotateRoadBits(RoadBits r,DiagDirDiff rot)66 static inline RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
67 {
68 assert(IsValidRoadBits(r));
69 for (; rot > (DiagDirDiff)0; rot--) {
70 r = (RoadBits)(GB(r, 0, 1) << 3 | GB(r, 1, 3));
71 }
72 return r;
73 }
74
75 /**
76 * Check if we've got a straight road
77 *
78 * @param r The given RoadBits
79 * @return true if we've got a straight road
80 */
IsStraightRoad(RoadBits r)81 static inline bool IsStraightRoad(RoadBits r)
82 {
83 assert(IsValidRoadBits(r));
84 return (r == ROAD_X || r == ROAD_Y);
85 }
86
87 /**
88 * Create the road-part which belongs to the given DiagDirection
89 *
90 * This function returns a RoadBits value which belongs to
91 * the given DiagDirection.
92 *
93 * @param d The DiagDirection
94 * @return The result RoadBits which the selected road-part set
95 */
DiagDirToRoadBits(DiagDirection d)96 static inline RoadBits DiagDirToRoadBits(DiagDirection d)
97 {
98 assert(IsValidDiagDirection(d));
99 return (RoadBits)(ROAD_NW << (3 ^ d));
100 }
101
102 /**
103 * Create the road-part which belongs to the given Axis
104 *
105 * This function returns a RoadBits value which belongs to
106 * the given Axis.
107 *
108 * @param a The Axis
109 * @return The result RoadBits which the selected road-part set
110 */
AxisToRoadBits(Axis a)111 static inline RoadBits AxisToRoadBits(Axis a)
112 {
113 assert(IsValidAxis(a));
114 return a == AXIS_X ? ROAD_X : ROAD_Y;
115 }
116
117
118 /**
119 * Calculates the maintenance cost of a number of road bits.
120 * @param roadtype Road type to get the cost for.
121 * @param num Number of road bits.
122 * @param total_num Total number of road bits of all road/tram-types.
123 * @return Total cost.
124 */
RoadMaintenanceCost(RoadType roadtype,uint32 num,uint32 total_num)125 static inline Money RoadMaintenanceCost(RoadType roadtype, uint32 num, uint32 total_num)
126 {
127 assert(roadtype < ROADTYPE_END);
128 return (_price[PR_INFRASTRUCTURE_ROAD] * GetRoadTypeInfo(roadtype)->maintenance_multiplier * num * (1 + IntSqrt(total_num))) >> 12;
129 }
130
131 /**
132 * Test if a road type has catenary
133 * @param roadtype Road type to test
134 */
HasRoadCatenary(RoadType roadtype)135 static inline bool HasRoadCatenary(RoadType roadtype)
136 {
137 assert(roadtype < ROADTYPE_END);
138 return HasBit(GetRoadTypeInfo(roadtype)->flags, ROTF_CATENARY);
139 }
140
141 /**
142 * Test if we should draw road catenary
143 * @param roadtype Road type to test
144 */
HasRoadCatenaryDrawn(RoadType roadtype)145 static inline bool HasRoadCatenaryDrawn(RoadType roadtype)
146 {
147 return HasRoadCatenary(roadtype) && !IsInvisibilitySet(TO_CATENARY);
148 }
149
150 bool HasRoadTypeAvail(CompanyID company, RoadType roadtype);
151 bool ValParamRoadType(RoadType roadtype);
152 RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces = true);
153 RoadTypes GetRoadTypes(bool introduces);
154 RoadTypes AddDateIntroducedRoadTypes(RoadTypes current, Date date);
155
156 void UpdateLevelCrossing(TileIndex tile, bool sound = true);
157 void UpdateCompanyRoadInfrastructure(RoadType rt, Owner o, int count);
158
159 struct TileInfo;
160 void DrawRoadOverlays(const TileInfo *ti, PaletteID pal, const RoadTypeInfo *road_rti, const RoadTypeInfo *tram_rit, uint road_offset, uint tram_offset);
161
162 #endif /* ROAD_FUNC_H */
163