OpenTTD Source  20240917-master-g9ab0a47812
road_func.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef ROAD_FUNC_H
11 #define ROAD_FUNC_H
12 
13 #include "core/bitmath_func.hpp"
14 #include "road.h"
15 #include "economy_func.h"
16 #include "transparency.h"
17 
23 inline bool IsValidRoadBits(RoadBits r)
24 {
25  return r < ROAD_END;
26 }
27 
38 {
39  assert(IsValidRoadBits(r));
40  return (RoadBits)(ROAD_ALL ^ r);
41 }
42 
52 {
53  assert(IsValidRoadBits(r));
54  return (RoadBits)(GB(r, 0, 2) << 2 | GB(r, 2, 2));
55 }
56 
67 {
68  assert(IsValidRoadBits(r));
69  for (; rot > (DiagDirDiff)0; rot--) {
70  r = (RoadBits)(GB(r, 0, 1) << 3 | GB(r, 1, 3));
71  }
72  return r;
73 }
74 
81 inline bool IsStraightRoad(RoadBits r)
82 {
83  assert(IsValidRoadBits(r));
84  return (r == ROAD_X || r == ROAD_Y);
85 }
86 
97 {
98  assert(IsValidDiagDirection(d));
99  return (RoadBits)(ROAD_NW << (3 ^ d));
100 }
101 
112 {
113  assert(IsValidAxis(a));
114  return a == AXIS_X ? ROAD_X : ROAD_Y;
115 }
116 
117 
125 inline Money RoadMaintenanceCost(RoadType roadtype, uint32_t num, uint32_t total_num)
126 {
127  assert(roadtype < ROADTYPE_END);
128  return (_price[PR_INFRASTRUCTURE_ROAD] * GetRoadTypeInfo(roadtype)->maintenance_multiplier * num * (1 + IntSqrt(total_num))) >> 12;
129 }
130 
135 inline bool HasRoadCatenary(RoadType roadtype)
136 {
137  assert(roadtype < ROADTYPE_END);
138  return HasBit(GetRoadTypeInfo(roadtype)->flags, ROTF_CATENARY);
139 }
140 
145 inline bool HasRoadCatenaryDrawn(RoadType roadtype)
146 {
147  return HasRoadCatenary(roadtype) && !IsInvisibilitySet(TO_CATENARY);
148 }
149 
150 bool HasRoadTypeAvail(CompanyID company, RoadType roadtype);
151 bool ValParamRoadType(RoadType roadtype);
152 RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces = true);
153 RoadTypes GetRoadTypes(bool introduces);
154 RoadTypes AddDateIntroducedRoadTypes(RoadTypes current, TimerGameCalendar::Date date);
155 
156 void UpdateLevelCrossing(TileIndex tile, bool sound = true, bool force_bar = false);
159 void UpdateCompanyRoadInfrastructure(RoadType rt, Owner o, int count);
160 
161 struct TileInfo;
162 enum Roadside : uint8_t;
163 void DrawRoadOverlays(const TileInfo *ti, PaletteID pal, const RoadTypeInfo *road_rti, const RoadTypeInfo *tram_rit, uint road_offset, uint tram_offset, bool draw_underlay = true);
164 void DrawRoadGroundSprites(const TileInfo *ti, RoadBits road, RoadBits tram, const RoadTypeInfo *road_rti, const RoadTypeInfo *tram_rti, Roadside roadside, bool snow_or_desert);
165 
166 #endif /* ROAD_FUNC_H */
RoadTypeInfo
Definition: road.h:78
TO_CATENARY
@ TO_CATENARY
catenary
Definition: transparency.h:30
AddDateIntroducedRoadTypes
RoadTypes AddDateIntroducedRoadTypes(RoadTypes current, TimerGameCalendar::Date date)
Add the road types that are to be introduced at the given date.
Definition: road.cpp:166
RoadBits
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:52
ROAD_Y
@ ROAD_Y
Full road along the y-axis (north-west + south-east)
Definition: road_type.h:59
TileInfo
Tile information, used while rendering the tile.
Definition: tile_cmd.h:43
DiagDirDiff
DiagDirDiff
Enumeration for the difference between to DiagDirection.
Definition: direction_type.h:95
IntSqrt
uint32_t IntSqrt(uint32_t num)
Compute the integer square root.
Definition: math_func.cpp:42
MarkDirtyAdjacentLevelCrossingTiles
void MarkDirtyAdjacentLevelCrossingTiles(TileIndex tile, Axis road_axis)
Find adjacent level crossing tiles in this multi-track crossing and mark them dirty.
Definition: train_cmd.cpp:1805
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
ROAD_ALL
@ ROAD_ALL
Full 4-way crossing.
Definition: road_type.h:66
GetCompanyRoadTypes
RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces=true)
Get the road types the given company can build.
Definition: road.cpp:199
RoadMaintenanceCost
Money RoadMaintenanceCost(RoadType roadtype, uint32_t num, uint32_t total_num)
Calculates the maintenance cost of a number of road bits.
Definition: road_func.h:125
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
economy_func.h
GetRoadTypes
RoadTypes GetRoadTypes(bool introduces)
Get list of road types, regardless of company availability.
Definition: road.cpp:227
PaletteID
uint32_t PaletteID
The number of the palette.
Definition: gfx_type.h:19
DrawRoadGroundSprites
void DrawRoadGroundSprites(const TileInfo *ti, RoadBits road, RoadBits tram, const RoadTypeInfo *road_rti, const RoadTypeInfo *tram_rti, Roadside roadside, bool snow_or_desert)
Draw road ground sprites.
Definition: road_cmd.cpp:1605
ROAD_NW
@ ROAD_NW
North-west part.
Definition: road_type.h:54
ROTF_CATENARY
@ ROTF_CATENARY
Bit number for adding catenary.
Definition: road.h:38
IsInvisibilitySet
bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:59
bitmath_func.hpp
HasRoadCatenaryDrawn
bool HasRoadCatenaryDrawn(RoadType roadtype)
Test if we should draw road catenary.
Definition: road_func.h:145
HasRoadTypeAvail
bool HasRoadTypeAvail(CompanyID company, RoadType roadtype)
Finds out, whether given company has a given RoadType available for construction.
Definition: road.cpp:111
RoadTypes
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:38
Roadside
Roadside
The possible road side decorations.
Definition: road_map.h:477
IsStraightRoad
bool IsStraightRoad(RoadBits r)
Check if we've got a straight road.
Definition: road_func.h:81
RotateRoadBits
RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
Calculate rotated RoadBits.
Definition: road_func.h:66
RoadType
RoadType
The different roadtypes we support.
Definition: road_type.h:25
UpdateAdjacentLevelCrossingTilesOnLevelCrossingRemoval
void UpdateAdjacentLevelCrossingTilesOnLevelCrossingRemoval(TileIndex tile, Axis road_axis)
Update adjacent level crossing tiles in this multi-track crossing, due to removal of a level crossing...
Definition: train_cmd.cpp:1822
road.h
DiagDirToRoadBits
RoadBits DiagDirToRoadBits(DiagDirection d)
Create the road-part which belongs to the given DiagDirection.
Definition: road_func.h:96
IsValidAxis
bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
Definition: direction_func.h:43
ValParamRoadType
bool ValParamRoadType(RoadType roadtype)
Validate functions for rail building.
Definition: road.cpp:153
AXIS_X
@ AXIS_X
The X axis.
Definition: direction_type.h:117
GetRoadTypeInfo
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:227
ROADTYPE_END
@ ROADTYPE_END
Used for iterations.
Definition: road_type.h:29
UpdateCompanyRoadInfrastructure
void UpdateCompanyRoadInfrastructure(RoadType rt, Owner o, int count)
Update road infrastructure counts for a company.
Definition: road_cmd.cpp:190
IsValidDiagDirection
bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
Definition: direction_func.h:21
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:116
ComplementRoadBits
RoadBits ComplementRoadBits(RoadBits r)
Calculate the complement of a RoadBits value.
Definition: road_func.h:37
transparency.h
IsValidRoadBits
bool IsValidRoadBits(RoadBits r)
Whether the given roadtype is valid.
Definition: road_func.h:23
UpdateLevelCrossing
void UpdateLevelCrossing(TileIndex tile, bool sound=true, bool force_bar=false)
Update a level crossing to barred or open (crossing may include multiple adjacent tiles).
Definition: train_cmd.cpp:1773
OverflowSafeInt< int64_t >
MirrorRoadBits
RoadBits MirrorRoadBits(RoadBits r)
Calculate the mirrored RoadBits.
Definition: road_func.h:51
DrawRoadOverlays
enum Roadside uint8_t void DrawRoadOverlays(const TileInfo *ti, PaletteID pal, const RoadTypeInfo *road_rti, const RoadTypeInfo *tram_rit, uint road_offset, uint tram_offset, bool draw_underlay=true)
Draw road underlay and overlay sprites.
Definition: road_cmd.cpp:1515
ROAD_X
@ ROAD_X
Full road along the x-axis (south-west + north-east)
Definition: road_type.h:58
AxisToRoadBits
RoadBits AxisToRoadBits(Axis a)
Create the road-part which belongs to the given Axis.
Definition: road_func.h:111
HasRoadCatenary
bool HasRoadCatenary(RoadType roadtype)
Test if a road type has catenary.
Definition: road_func.h:135
ROAD_END
@ ROAD_END
Out-of-range roadbits, used for iterations.
Definition: road_type.h:68
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103