Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / road_func.h
blob8da47a1f11ac614a704d4cbd40a5616eb76e92e7
1 /* $Id: road_func.h 26105 2013-11-25 13:16:06Z rubidium $ */
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 */
10 /** @file road_func.h Functions related to roads. */
12 #ifndef ROAD_FUNC_H
13 #define ROAD_FUNC_H
15 #include "core/bitmath_func.hpp"
16 #include "road_type.h"
17 #include "economy_func.h"
19 /**
20 * Iterate through each set RoadType in a RoadTypes value.
21 * For more informations see FOR_EACH_SET_BIT_EX.
23 * @param var Loop index variable that stores fallowing set road type. Must be of type RoadType.
24 * @param road_types The value to iterate through (any expression).
26 * @see FOR_EACH_SET_BIT_EX
28 #define FOR_EACH_SET_ROADTYPE(var, road_types) FOR_EACH_SET_BIT_EX(RoadType, var, RoadTypes, road_types)
30 /**
31 * Whether the given roadtype is valid.
32 * @param rt the roadtype to check for validness
33 * @return true if and only if valid
35 static inline bool IsValidRoadType(RoadType rt)
37 return rt == ROADTYPE_ROAD || rt == ROADTYPE_TRAM;
40 /**
41 * Whether the given roadtype is valid.
42 * @param rt the roadtype to check for validness
43 * @return true if and only if valid
45 static inline bool IsValidRoadBits(RoadBits r)
47 return r < ROAD_END;
50 /**
51 * Maps a RoadType to the corresponding RoadTypes value
53 * @param rt the roadtype to get the roadtypes from
54 * @return the roadtypes with the given roadtype
56 static inline RoadTypes RoadTypeToRoadTypes(RoadType rt)
58 assert(IsValidRoadType(rt));
59 return (RoadTypes)(1 << rt);
62 /**
63 * Returns the RoadTypes which are not present in the given RoadTypes
65 * This function returns the complement of a given RoadTypes.
67 * @param r The given RoadTypes
68 * @return The complement of the given RoadTypes
70 static inline RoadTypes ComplementRoadTypes(RoadTypes r)
72 return (RoadTypes)(ROADTYPES_ALL ^ r);
76 /**
77 * Calculate the complement of a RoadBits value
79 * Simply flips all bits in the RoadBits value to get the complement
80 * of the RoadBits.
82 * @param r The given RoadBits value
83 * @return the complement
85 static inline RoadBits ComplementRoadBits(RoadBits r)
87 assert(IsValidRoadBits(r));
88 return (RoadBits)(ROAD_ALL ^ r);
91 /**
92 * Calculate the mirrored RoadBits
94 * Simply move the bits to their new position.
96 * @param r The given RoadBits value
97 * @return the mirrored
99 static inline RoadBits MirrorRoadBits(RoadBits r)
101 assert(IsValidRoadBits(r));
102 return (RoadBits)(GB(r, 0, 2) << 2 | GB(r, 2, 2));
106 * Calculate rotated RoadBits
108 * Move the Roadbits clockwise until they are in their final position.
110 * @param r The given RoadBits value
111 * @param rot The given Rotation angle
112 * @return the rotated
114 static inline RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
116 assert(IsValidRoadBits(r));
117 for (; rot > (DiagDirDiff)0; rot--) {
118 r = (RoadBits)(GB(r, 0, 1) << 3 | GB(r, 1, 3));
120 return r;
124 * Check if we've got a straight road
126 * @param r The given RoadBits
127 * @return true if we've got a straight road
129 static inline bool IsStraightRoad(RoadBits r)
131 assert(IsValidRoadBits(r));
132 return (r == ROAD_X || r == ROAD_Y);
136 * Create the road-part which belongs to the given DiagDirection
138 * This function returns a RoadBits value which belongs to
139 * the given DiagDirection.
141 * @param d The DiagDirection
142 * @return The result RoadBits which the selected road-part set
144 static inline RoadBits DiagDirToRoadBits(DiagDirection d)
146 assert(IsValidDiagDirection(d));
147 return (RoadBits)(ROAD_NW << (3 ^ d));
151 * Create the road-part which belongs to the given Axis
153 * This function returns a RoadBits value which belongs to
154 * the given Axis.
156 * @param a The Axis
157 * @return The result RoadBits which the selected road-part set
159 static inline RoadBits AxisToRoadBits(Axis a)
161 assert(IsValidAxis(a));
162 return a == AXIS_X ? ROAD_X : ROAD_Y;
167 * Calculates the maintenance cost of a number of road bits.
168 * @param roadtype Road type to get the cost for.
169 * @param num Number of road bits.
170 * @return Total cost.
172 static inline Money RoadMaintenanceCost(RoadType roadtype, uint32 num)
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.
178 bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts);
179 bool ValParamRoadType(const RoadType rt);
180 RoadTypes GetCompanyRoadtypes(const CompanyID company);
182 void UpdateLevelCrossing(TileIndex tile, bool sound = true, bool force_close = false);
184 #endif /* ROAD_FUNC_H */