2007-12-18 20:38:16 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-12-18 20:38:16 +00:00
|
|
|
/** @file road_func.h Functions related to roads. */
|
|
|
|
|
|
|
|
#ifndef ROAD_FUNC_H
|
|
|
|
#define ROAD_FUNC_H
|
|
|
|
|
2007-12-21 19:21:21 +00:00
|
|
|
#include "core/bitmath_func.hpp"
|
2007-12-18 20:38:16 +00:00
|
|
|
#include "road_type.h"
|
2011-12-03 23:40:46 +00:00
|
|
|
#include "economy_func.h"
|
2007-12-18 20:38:16 +00:00
|
|
|
|
2010-05-11 21:01:57 +00:00
|
|
|
/**
|
|
|
|
* Iterate through each set RoadType in a RoadTypes value.
|
|
|
|
* For more informations see FOR_EACH_SET_BIT_EX.
|
|
|
|
*
|
|
|
|
* @param var Loop index variable that stores fallowing set road type. Must be of type RoadType.
|
|
|
|
* @param road_types The value to iterate through (any expression).
|
|
|
|
*
|
|
|
|
* @see FOR_EACH_SET_BIT_EX
|
|
|
|
*/
|
|
|
|
#define FOR_EACH_SET_ROADTYPE(var, road_types) FOR_EACH_SET_BIT_EX(RoadType, var, RoadTypes, road_types)
|
|
|
|
|
2007-12-18 20:38:16 +00:00
|
|
|
/**
|
|
|
|
* Whether the given roadtype is valid.
|
|
|
|
* @param rt the roadtype to check for validness
|
|
|
|
* @return true if and only if valid
|
|
|
|
*/
|
|
|
|
static inline bool IsValidRoadType(RoadType rt)
|
|
|
|
{
|
|
|
|
return rt == ROADTYPE_ROAD || rt == ROADTYPE_TRAM;
|
|
|
|
}
|
|
|
|
|
2013-11-25 13:16:06 +00:00
|
|
|
/**
|
|
|
|
* Whether the given roadtype is valid.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param r the roadtype to check for validness
|
2013-11-25 13:16:06 +00:00
|
|
|
* @return true if and only if valid
|
|
|
|
*/
|
|
|
|
static inline bool IsValidRoadBits(RoadBits r)
|
|
|
|
{
|
|
|
|
return r < ROAD_END;
|
|
|
|
}
|
|
|
|
|
2007-12-18 20:38:16 +00:00
|
|
|
/**
|
|
|
|
* Maps a RoadType to the corresponding RoadTypes value
|
|
|
|
*
|
|
|
|
* @param rt the roadtype to get the roadtypes from
|
|
|
|
* @return the roadtypes with the given roadtype
|
|
|
|
*/
|
|
|
|
static inline RoadTypes RoadTypeToRoadTypes(RoadType rt)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidRoadType(rt));
|
2007-12-18 20:38:16 +00:00
|
|
|
return (RoadTypes)(1 << rt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the RoadTypes which are not present in the given RoadTypes
|
|
|
|
*
|
|
|
|
* This function returns the complement of a given RoadTypes.
|
|
|
|
*
|
|
|
|
* @param r The given RoadTypes
|
|
|
|
* @return The complement of the given RoadTypes
|
|
|
|
*/
|
|
|
|
static inline RoadTypes ComplementRoadTypes(RoadTypes r)
|
|
|
|
{
|
|
|
|
return (RoadTypes)(ROADTYPES_ALL ^ r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the complement of a RoadBits value
|
|
|
|
*
|
|
|
|
* Simply flips all bits in the RoadBits value to get the complement
|
|
|
|
* of the RoadBits.
|
|
|
|
*
|
|
|
|
* @param r The given RoadBits value
|
|
|
|
* @return the complement
|
|
|
|
*/
|
|
|
|
static inline RoadBits ComplementRoadBits(RoadBits r)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidRoadBits(r));
|
2007-12-18 20:38:16 +00:00
|
|
|
return (RoadBits)(ROAD_ALL ^ r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the mirrored RoadBits
|
|
|
|
*
|
|
|
|
* Simply move the bits to their new position.
|
|
|
|
*
|
|
|
|
* @param r The given RoadBits value
|
|
|
|
* @return the mirrored
|
|
|
|
*/
|
|
|
|
static inline RoadBits MirrorRoadBits(RoadBits r)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidRoadBits(r));
|
2007-12-18 20:38:16 +00:00
|
|
|
return (RoadBits)(GB(r, 0, 2) << 2 | GB(r, 2, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate rotated RoadBits
|
|
|
|
*
|
2013-01-08 22:46:42 +00:00
|
|
|
* Move the Roadbits clockwise until they are in their final position.
|
2007-12-18 20:38:16 +00:00
|
|
|
*
|
|
|
|
* @param r The given RoadBits value
|
|
|
|
* @param rot The given Rotation angle
|
|
|
|
* @return the rotated
|
|
|
|
*/
|
|
|
|
static inline RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidRoadBits(r));
|
2009-08-14 17:11:59 +00:00
|
|
|
for (; rot > (DiagDirDiff)0; rot--) {
|
2007-12-18 20:38:16 +00:00
|
|
|
r = (RoadBits)(GB(r, 0, 1) << 3 | GB(r, 1, 3));
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-03-25 18:59:25 +00:00
|
|
|
/**
|
|
|
|
* Check if we've got a straight road
|
|
|
|
*
|
|
|
|
* @param r The given RoadBits
|
|
|
|
* @return true if we've got a straight road
|
|
|
|
*/
|
|
|
|
static inline bool IsStraightRoad(RoadBits r)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidRoadBits(r));
|
2008-03-25 18:59:25 +00:00
|
|
|
return (r == ROAD_X || r == ROAD_Y);
|
|
|
|
}
|
|
|
|
|
2007-12-18 20:38:16 +00:00
|
|
|
/**
|
|
|
|
* Create the road-part which belongs to the given DiagDirection
|
|
|
|
*
|
|
|
|
* This function returns a RoadBits value which belongs to
|
|
|
|
* the given DiagDirection.
|
|
|
|
*
|
|
|
|
* @param d The DiagDirection
|
|
|
|
* @return The result RoadBits which the selected road-part set
|
|
|
|
*/
|
|
|
|
static inline RoadBits DiagDirToRoadBits(DiagDirection d)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidDiagDirection(d));
|
2007-12-18 20:38:16 +00:00
|
|
|
return (RoadBits)(ROAD_NW << (3 ^ d));
|
|
|
|
}
|
|
|
|
|
2008-03-22 10:56:08 +00:00
|
|
|
/**
|
|
|
|
* Create the road-part which belongs to the given Axis
|
|
|
|
*
|
|
|
|
* This function returns a RoadBits value which belongs to
|
|
|
|
* the given Axis.
|
|
|
|
*
|
|
|
|
* @param a The Axis
|
|
|
|
* @return The result RoadBits which the selected road-part set
|
|
|
|
*/
|
|
|
|
static inline RoadBits AxisToRoadBits(Axis a)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidAxis(a));
|
2008-03-22 10:56:08 +00:00
|
|
|
return a == AXIS_X ? ROAD_X : ROAD_Y;
|
|
|
|
}
|
|
|
|
|
2011-12-03 23:40:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the maintenance cost of a number of road bits.
|
|
|
|
* @param roadtype Road type to get the cost for.
|
|
|
|
* @param num Number of road bits.
|
|
|
|
* @return Total cost.
|
|
|
|
*/
|
|
|
|
static inline Money RoadMaintenanceCost(RoadType roadtype, uint32 num)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidRoadType(roadtype));
|
2011-12-03 23:40:46 +00:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts);
|
2008-01-09 21:05:03 +00:00
|
|
|
bool ValParamRoadType(const RoadType rt);
|
2008-09-30 20:39:50 +00:00
|
|
|
RoadTypes GetCompanyRoadtypes(const CompanyID company);
|
2008-01-09 21:05:03 +00:00
|
|
|
|
2017-07-20 00:07:02 +00:00
|
|
|
void UpdateLevelCrossing(TileIndex tile, bool sound = true, bool force_close = false);
|
2017-05-07 18:33:59 +00:00
|
|
|
bool IsCrossingOccupiedByRoadVehicle(TileIndex t);
|
2008-01-17 19:49:06 +00:00
|
|
|
|
2007-12-18 20:38:16 +00:00
|
|
|
#endif /* ROAD_FUNC_H */
|