OpenTTD: src/road_func.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

road_func.h
Go to the documentation of this file.
1 /* $Id: road_func.h 26105 2013-11-25 13:16:06Z rubidium $ */
2
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 */
9
12 #ifndef ROAD_FUNC_H
13 #define ROAD_FUNC_H
14
15 #include "core/bitmath_func.hpp"
16 #include "road_type.h"
17 #include "economy_func.h"
18
28 #define FOR_EACH_SET_ROADTYPE(var, road_types) FOR_EACH_SET_BIT_EX(RoadType, var, RoadTypes, road_types)
29
35 static inline bool IsValidRoadType(RoadType rt)
36 {
37 return rt == ROADTYPE_ROAD || rt == ROADTYPE_TRAM;
38 }
39
45 static inline bool IsValidRoadBits(RoadBits r)
46 {
47 return r < ROAD_END;
48 }
49
56 static inline RoadTypes RoadTypeToRoadTypes(RoadType rt)
57 {
58 assert(IsValidRoadType(rt));
59 return (RoadTypes)(1 << rt);
60 }
61
70 static inline RoadTypes ComplementRoadTypes(RoadTypes r)
71 {
72 return (RoadTypes)(ROADTYPES_ALL ^ r);
73 }
74
75
85 static inline RoadBits ComplementRoadBits(RoadBits r)
86 {
87 assert(IsValidRoadBits(r));
88 return (RoadBits)(ROAD_ALL ^ r);
89 }
90
99 static inline RoadBits MirrorRoadBits(RoadBits r)
100 {
101 assert(IsValidRoadBits(r));
102 return (RoadBits)(GB(r, 0, 2) << 2 | GB(r, 2, 2));
103 }
104
114 static inline RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
115 {
116 assert(IsValidRoadBits(r));
117 for (; rot > (DiagDirDiff)0; rot--) {
118 r = (RoadBits)(GB(r, 0, 1) << 3 | GB(r, 1, 3));
119 }
120 return r;
121 }
122
129 static inline bool IsStraightRoad(RoadBits r)
130 {
131 assert(IsValidRoadBits(r));
132 return (r == ROAD_X || r == ROAD_Y);
133 }
134
144 static inline RoadBits DiagDirToRoadBits(DiagDirection d)
145 {
146 assert(IsValidDiagDirection(d));
147 return (RoadBits)(ROAD_NW << (3 ^ d));
148 }
149
159 static inline RoadBits AxisToRoadBits(Axis a)
160 {
161 assert(IsValidAxis(a));
162 return a == AXIS_X ? ROAD_X : ROAD_Y;
163 }
164
165
172 static inline Money RoadMaintenanceCost(RoadType roadtype, uint32 num)
173 {
174 assert(IsValidRoadType(roadtype));
175 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.
176 }
177
178 bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts);
179 bool ValParamRoadType(const RoadType rt);
180 RoadTypes GetCompanyRoadtypes(const CompanyID company);
181
182 void UpdateLevelCrossing(TileIndex tile, bool sound = true);
183
184 #endif /* ROAD_FUNC_H */