1 /* $Id: road_func.h 26105 2013-11-25 13:16:06Z rubidium $ */
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/>.
10 /** @file road_func.h Functions related to roads. */
15 #include "core/bitmath_func.hpp"
17 #include "economy_func.h"
18 #include "transparency.h"
21 * Whether the given roadtype is valid.
22 * @param rt the roadtype to check for validness
23 * @return true if and only if valid
25 static inline bool IsValidRoadType(RoadType rt
)
27 return rt
== ROADTYPE_ROAD
|| rt
== ROADTYPE_TRAM
;
31 * Whether the given roadtype is valid.
32 * @param rt the roadtype to check for validness
33 * @return true if and only if valid
35 static inline bool IsValidRoadBits(RoadBits r
)
41 * Maps a RoadType to the corresponding RoadTypes value
43 * @param rt the roadtype to get the roadtypes from
44 * @return the roadtypes with the given roadtype
46 static inline RoadTypes
RoadTypeToRoadTypes(RoadType rt
)
48 assert(IsValidRoadType(rt
));
49 return (RoadTypes
)(1 << rt
);
53 * Returns the RoadTypes which are not present in the given RoadTypes
55 * This function returns the complement of a given RoadTypes.
57 * @param r The given RoadTypes
58 * @return The complement of the given RoadTypes
60 static inline RoadTypes
ComplementRoadTypes(RoadTypes r
)
62 return (RoadTypes
)(ROADTYPES_ALL
^ r
);
67 * Calculate the complement of a RoadBits value
69 * Simply flips all bits in the RoadBits value to get the complement
72 * @param r The given RoadBits value
73 * @return the complement
75 static inline RoadBits
ComplementRoadBits(RoadBits r
)
77 assert(IsValidRoadBits(r
));
78 return (RoadBits
)(ROAD_ALL
^ r
);
82 * Calculate the mirrored RoadBits
84 * Simply move the bits to their new position.
86 * @param r The given RoadBits value
87 * @return the mirrored
89 static inline RoadBits
MirrorRoadBits(RoadBits r
)
91 assert(IsValidRoadBits(r
));
92 return (RoadBits
)(GB(r
, 0, 2) << 2 | GB(r
, 2, 2));
96 * Calculate rotated RoadBits
98 * Move the Roadbits clockwise until they are in their final position.
100 * @param r The given RoadBits value
101 * @param rot The given Rotation angle
102 * @return the rotated
104 static inline RoadBits
RotateRoadBits(RoadBits r
, DiagDirDiff rot
)
106 assert(IsValidRoadBits(r
));
107 for (; rot
> (DiagDirDiff
)0; rot
--) {
108 r
= (RoadBits
)(GB(r
, 0, 1) << 3 | GB(r
, 1, 3));
114 * Check if we've got a straight road
116 * @param r The given RoadBits
117 * @return true if we've got a straight road
119 static inline bool IsStraightRoad(RoadBits r
)
121 assert(IsValidRoadBits(r
));
122 return (r
== ROAD_X
|| r
== ROAD_Y
);
126 * Create the road-part which belongs to the given DiagDirection
128 * This function returns a RoadBits value which belongs to
129 * the given DiagDirection.
131 * @param d The DiagDirection
132 * @return The result RoadBits which the selected road-part set
134 static inline RoadBits
DiagDirToRoadBits(DiagDirection d
)
136 assert(IsValidDiagDirection(d
));
137 return (RoadBits
)(ROAD_NW
<< (3 ^ d
));
141 * Create the road-part which belongs to the given Axis
143 * This function returns a RoadBits value which belongs to
147 * @return The result RoadBits which the selected road-part set
149 static inline RoadBits
AxisToRoadBits(Axis a
)
151 assert(IsValidAxis(a
));
152 return a
== AXIS_X
? ROAD_X
: ROAD_Y
;
157 * Calculates the maintenance cost of a number of road bits.
158 * @param rtid Road type to get the cost for.
159 * @param num Number of road bits.
160 * @param total_num Total number of road bits of all road/tram-types.
161 * @return Total cost.
163 static inline Money
RoadMaintenanceCost(RoadTypeIdentifier rtid
, uint32 num
, uint32 total_num
)
165 assert(rtid
.IsValid());
166 return (_price
[PR_INFRASTRUCTURE_ROAD
] * GetRoadTypeInfo(rtid
)->maintenance_multiplier
* num
* (1 + IntSqrt(total_num
))) >> 12;
170 * Test if a road type has catenary
171 * @param rti Road type to test
173 static inline bool HasRoadCatenary(RoadTypeIdentifier rti
)
175 assert(IsValidRoadType(rti
.basetype
));
176 return HasBit(GetRoadTypeInfo(rti
)->flags
, ROTF_CATENARY
);
180 * Test if we should draw road catenary
181 * @param rti Road type to test
183 static inline bool HasRoadCatenaryDrawn(RoadTypeIdentifier rti
)
185 return HasRoadCatenary(rti
) && !IsInvisibilitySet(TO_CATENARY
);
188 bool HasRoadTypeAvail(CompanyID company
, RoadTypeIdentifier rtid
);
189 bool ValParamRoadType(RoadTypeIdentifier rtid
);
190 RoadSubTypes
GetCompanyRoadtypes(CompanyID company
, RoadType rt
);
191 RoadSubTypes
AddDateIntroducedRoadTypes(RoadType rt
, RoadSubTypes current
, Date date
);
193 void UpdateLevelCrossing(TileIndex tile
, bool sound
= true, bool force_close
= false);
195 #endif /* ROAD_FUNC_H */