Remove second template parameter from class GUIList
[openttd/fttd.git] / src / road_func.h
blobbfe261980c8c9a0871fb063a8b77cc856606115d
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 road_func.h Functions related to roads. */
12 #ifndef ROAD_FUNC_H
13 #define ROAD_FUNC_H
15 #include "core/bitmath_func.hpp"
16 #include "road_type.h"
17 #include "economy_func.h"
19 /**
20 * Iterate through each set RoadType in a RoadTypes value.
21 * For more informations see FOR_EACH_SET_BIT_EX.
23 * @param var Loop index variable that stores fallowing set road type. Must be of type RoadType.
24 * @param road_types The value to iterate through (any expression).
26 * @see FOR_EACH_SET_BIT_EX
28 #define FOR_EACH_SET_ROADTYPE(var, road_types) FOR_EACH_SET_BIT_EX(RoadType, var, RoadTypes, road_types)
30 /**
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 IsValidRoadType(RoadType rt)
37 return rt == ROADTYPE_ROAD || rt == ROADTYPE_TRAM;
40 /**
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 return (RoadTypes)(1 << rt);
51 /**
52 * Returns the RoadTypes which are not present in the given RoadTypes
54 * This function returns the complement of a given RoadTypes.
56 * @param r The given RoadTypes
57 * @return The complement of the given RoadTypes
59 static inline RoadTypes ComplementRoadTypes(RoadTypes r)
61 return (RoadTypes)(ROADTYPES_ALL ^ r);
65 /**
66 * Calculate the complement of a RoadBits value
68 * Simply flips all bits in the RoadBits value to get the complement
69 * of the RoadBits.
71 * @param r The given RoadBits value
72 * @return the complement
74 static inline RoadBits ComplementRoadBits(RoadBits r)
76 return (RoadBits)(ROAD_ALL ^ r);
79 /**
80 * Calculate the mirrored RoadBits
82 * Simply move the bits to their new position.
84 * @param r The given RoadBits value
85 * @return the mirrored
87 static inline RoadBits MirrorRoadBits(RoadBits r)
89 return (RoadBits)(GB(r, 0, 2) << 2 | GB(r, 2, 2));
92 /**
93 * Calculate rotated RoadBits
95 * Move the Roadbits clockwise until they are in their final position.
97 * @param r The given RoadBits value
98 * @param rot The given Rotation angle
99 * @return the rotated
101 static inline RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
103 for (; rot > (DiagDirDiff)0; rot--) {
104 r = (RoadBits)(GB(r, 0, 1) << 3 | GB(r, 1, 3));
106 return r;
110 * Check if we've got a straight road
112 * @param r The given RoadBits
113 * @return true if we've got a straight road
115 static inline bool IsStraightRoad(RoadBits r)
117 return (r == ROAD_X || r == ROAD_Y);
121 * Create the road-part which belongs to the given DiagDirection
123 * This function returns a RoadBits value which belongs to
124 * the given DiagDirection.
126 * @param d The DiagDirection
127 * @return The result RoadBits which the selected road-part set
129 static inline RoadBits DiagDirToRoadBits(DiagDirection d)
131 return (RoadBits)(ROAD_NW << (3 ^ d));
135 * Create the road-part which belongs to the given Axis
137 * This function returns a RoadBits value which belongs to
138 * the given Axis.
140 * @param a The Axis
141 * @return The result RoadBits which the selected road-part set
143 static inline RoadBits AxisToRoadBits(Axis a)
145 return a == AXIS_X ? ROAD_X : ROAD_Y;
150 * Calculates the maintenance cost of a number of road bits.
151 * @param roadtype Road type to get the cost for.
152 * @param num Number of road bits.
153 * @return Total cost.
155 static inline Money RoadMaintenanceCost(RoadType roadtype, uint32 num)
157 assert(roadtype < ROADTYPE_END);
158 return (_price[PR_INFRASTRUCTURE_ROAD] * (roadtype == ROADTYPE_TRAM ? 3 : 2) * num * (1 + IntSqrt(num))) >> 9; // 2 bits fraction for the multiplier and 7 bits scaling.
161 bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts);
162 bool ValParamRoadType(const RoadType rt);
163 RoadTypes GetCompanyRoadtypes(const CompanyID company);
165 void UpdateLevelCrossing(TileIndex tile, bool sound = true);
167 #endif /* ROAD_FUNC_H */