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