OpenTTD Source  20241108-master-g80f628063a
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 */
Functions related to bit mathematics.
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
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.
Owner
Enum for all companies/owners.
Definition: company_type.h:18
uint32_t IntSqrt(uint32_t num)
Compute the integer square root.
Definition: math_func.cpp:42
bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
Axis
Allow incrementing of DiagDirDiff variables.
@ AXIS_X
The X axis.
DiagDirection
Enumeration for diagonal directions.
DiagDirDiff
Enumeration for the difference between to DiagDirection.
Functions related to the economy.
uint32_t PaletteID
The number of the palette.
Definition: gfx_type.h:19
Road specific functions.
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:227
@ ROTF_CATENARY
Bit number for adding catenary.
Definition: road.h:38
bool HasRoadTypeAvail(CompanyID company, RoadType roadtype)
Finds out, whether given company has a given RoadType available for construction.
Definition: road.cpp:111
RoadBits AxisToRoadBits(Axis a)
Create the road-part which belongs to the given Axis.
Definition: road_func.h:111
bool IsValidRoadBits(RoadBits r)
Whether the given roadtype is valid.
Definition: road_func.h:23
bool HasRoadCatenaryDrawn(RoadType roadtype)
Test if we should draw road catenary.
Definition: road_func.h:145
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
RoadTypes GetRoadTypes(bool introduces)
Get list of road types, regardless of company availability.
Definition: road.cpp:227
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
bool ValParamRoadType(RoadType roadtype)
Validate functions for rail building.
Definition: road.cpp:153
bool HasRoadCatenary(RoadType roadtype)
Test if a road type has catenary.
Definition: road_func.h:135
void UpdateCompanyRoadInfrastructure(RoadType rt, Owner o, int count)
Update road infrastructure counts for a company.
Definition: road_cmd.cpp:190
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
bool IsStraightRoad(RoadBits r)
Check if we've got a straight road.
Definition: road_func.h:81
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 MirrorRoadBits(RoadBits r)
Calculate the mirrored RoadBits.
Definition: road_func.h:51
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
RoadBits DiagDirToRoadBits(DiagDirection d)
Create the road-part which belongs to the given DiagDirection.
Definition: road_func.h:96
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
RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
Calculate rotated RoadBits.
Definition: road_func.h:66
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
RoadBits ComplementRoadBits(RoadBits r)
Calculate the complement of a RoadBits value.
Definition: road_func.h:37
RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces=true)
Get the road types the given company can build.
Definition: road.cpp:199
Roadside
The possible road side decorations.
Definition: road_map.h:477
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:52
@ ROAD_ALL
Full 4-way crossing.
Definition: road_type.h:66
@ ROAD_Y
Full road along the y-axis (north-west + south-east)
Definition: road_type.h:59
@ ROAD_NW
North-west part.
Definition: road_type.h:54
@ ROAD_X
Full road along the x-axis (south-west + north-east)
Definition: road_type.h:58
@ ROAD_END
Out-of-range roadbits, used for iterations.
Definition: road_type.h:68
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:38
RoadType
The different roadtypes we support.
Definition: road_type.h:25
@ ROADTYPE_END
Used for iterations.
Definition: road_type.h:29
Tile information, used while rendering the tile.
Definition: tile_cmd.h:43
Functions related to transparency.
@ TO_CATENARY
catenary
Definition: transparency.h:30
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