road_func.h

Go to the documentation of this file.
00001 /* $Id: road_func.h 23735 2012-01-03 20:26:05Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef ROAD_FUNC_H
00013 #define ROAD_FUNC_H
00014 
00015 #include "core/bitmath_func.hpp"
00016 #include "road_type.h"
00017 #include "economy_func.h"
00018 
00028 #define FOR_EACH_SET_ROADTYPE(var, road_types) FOR_EACH_SET_BIT_EX(RoadType, var, RoadTypes, road_types)
00029 
00035 static inline bool IsValidRoadType(RoadType rt)
00036 {
00037   return rt == ROADTYPE_ROAD || rt == ROADTYPE_TRAM;
00038 }
00039 
00046 static inline RoadTypes RoadTypeToRoadTypes(RoadType rt)
00047 {
00048   return (RoadTypes)(1 << rt);
00049 }
00050 
00059 static inline RoadTypes ComplementRoadTypes(RoadTypes r)
00060 {
00061   return (RoadTypes)(ROADTYPES_ALL ^ r);
00062 }
00063 
00064 
00074 static inline RoadBits ComplementRoadBits(RoadBits r)
00075 {
00076   return (RoadBits)(ROAD_ALL ^ r);
00077 }
00078 
00087 static inline RoadBits MirrorRoadBits(RoadBits r)
00088 {
00089   return (RoadBits)(GB(r, 0, 2) << 2 | GB(r, 2, 2));
00090 }
00091 
00101 static inline RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
00102 {
00103   for (; rot > (DiagDirDiff)0; rot--) {
00104     r = (RoadBits)(GB(r, 0, 1) << 3 | GB(r, 1, 3));
00105   }
00106   return r;
00107 }
00108 
00115 static inline bool IsStraightRoad(RoadBits r)
00116 {
00117   return (r == ROAD_X || r == ROAD_Y);
00118 }
00119 
00129 static inline RoadBits DiagDirToRoadBits(DiagDirection d)
00130 {
00131   return (RoadBits)(ROAD_NW << (3 ^ d));
00132 }
00133 
00143 static inline RoadBits AxisToRoadBits(Axis a)
00144 {
00145   return a == AXIS_X ? ROAD_X : ROAD_Y;
00146 }
00147 
00148 
00155 static inline Money RoadMaintenanceCost(RoadType roadtype, uint32 num)
00156 {
00157   assert(roadtype < ROADTYPE_END);
00158   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.
00159 }
00160 
00161 bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts);
00162 bool ValParamRoadType(const RoadType rt);
00163 RoadTypes GetCompanyRoadtypes(const CompanyID company);
00164 
00165 void UpdateLevelCrossing(TileIndex tile, bool sound = true);
00166 
00167 #endif /* ROAD_FUNC_H */