OpenTTD

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 */
road_type.h
Enums and other types related to roads.
ROADTYPES_ALL
Road + trams.
Definition: road_type.h:40
ComplementRoadBits
static RoadBits ComplementRoadBits(RoadBits r)
Calculate the complement of a RoadBits value.
Definition: road_func.h:85
ValParamRoadType
bool ValParamRoadType(const RoadType rt)
Validate functions for rail building.
Definition: road.cpp:129
HasRoadTypesAvail
bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts)
Finds out, whether given company has all given RoadTypes available.
Definition: road.cpp:110
ROADTYPE_ROAD
Basic road type.
Definition: road_type.h:24
ROAD_X
Full road along the x-axis (south-west + north-east)
Definition: road_type.h:61
RoadTypeToRoadTypes
static RoadTypes RoadTypeToRoadTypes(RoadType rt)
Maps a RoadType to the corresponding RoadTypes value.
Definition: road_func.h:56
RotateRoadBits
static RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
Calculate rotated RoadBits.
Definition: road_func.h:114
bitmath_func.hpp
Functions related to bit mathematics.
RoadType
RoadType
The different roadtypes we support.
Definition: road_type.h:22
IsValidRoadType
static bool IsValidRoadType(RoadType rt)
Whether the given roadtype is valid.
Definition: road_func.h:35
ROAD_END
Out-of-range roadbits, used for iterations.
Definition: road_type.h:71
DiagDirToRoadBits
static RoadBits DiagDirToRoadBits(DiagDirection d)
Create the road-part which belongs to the given DiagDirection.
Definition: road_func.h:144
ROAD_ALL
Full 4-way crossing.
Definition: road_type.h:69
ROADTYPE_TRAM
Trams.
Definition: road_type.h:25
RoadBits
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:55
IsValidDiagDirection
static bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
Definition: direction_func.h:23
DiagDirDiff
DiagDirDiff
Enumeration for the difference between to DiagDirection.
Definition: direction_type.h:108
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:80
UpdateLevelCrossing
void UpdateLevelCrossing(TileIndex tile, bool sound=true)
Sets correct crossing state.
Definition: train_cmd.cpp:1699
RoadTypes
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:36
ROAD_NW
North-west part.
Definition: road_type.h:57
AXIS_X
The X axis.
Definition: direction_type.h:128
MirrorRoadBits
static RoadBits MirrorRoadBits(RoadBits r)
Calculate the mirrored RoadBits.
Definition: road_func.h:99
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
economy_func.h
Functions related to the economy.
IsStraightRoad
static bool IsStraightRoad(RoadBits r)
Check if we&#39;ve got a straight road.
Definition: road_func.h:129
IsValidAxis
static bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
Definition: direction_func.h:45
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:34
ComplementRoadTypes
static RoadTypes ComplementRoadTypes(RoadTypes r)
Returns the RoadTypes which are not present in the given RoadTypes.
Definition: road_func.h:70
IntSqrt
uint32 IntSqrt(uint32 num)
Compute the integer square root.
Definition: math_func.cpp:79
IsValidRoadBits
static bool IsValidRoadBits(RoadBits r)
Whether the given roadtype is valid.
Definition: road_func.h:45
ROAD_Y
Full road along the y-axis (north-west + south-east)
Definition: road_type.h:62
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:20
GetCompanyRoadtypes
RoadTypes GetCompanyRoadtypes(const CompanyID company)
Get the road types the given company can build.
Definition: road.cpp:139
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:127
RoadMaintenanceCost
static Money RoadMaintenanceCost(RoadType roadtype, uint32 num)
Calculates the maintenance cost of a number of road bits.
Definition: road_func.h:172
AxisToRoadBits
static RoadBits AxisToRoadBits(Axis a)
Create the road-part which belongs to the given Axis.
Definition: road_func.h:159
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >