2006-03-05 12:34:55 +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 19:52:14 +00:00
|
|
|
/** @file direction_func.h Different functions related to conversions between directions. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2007-12-18 19:52:14 +00:00
|
|
|
#ifndef DIRECTION_FUNC_H
|
|
|
|
#define DIRECTION_FUNC_H
|
2006-03-05 12:34:55 +00:00
|
|
|
|
2007-12-18 19:52:14 +00:00
|
|
|
#include "direction_type.h"
|
2007-01-10 18:56:51 +00:00
|
|
|
|
2013-11-25 13:16:06 +00:00
|
|
|
/**
|
|
|
|
* Checks if an integer value is a valid DiagDirection
|
|
|
|
*
|
|
|
|
* @param d The value to check
|
|
|
|
* @return True if the value belongs to a DiagDirection, else false
|
|
|
|
*/
|
|
|
|
static inline bool IsValidDiagDirection(DiagDirection d)
|
|
|
|
{
|
|
|
|
return d < DIAGDIR_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if an integer value is a valid Direction
|
|
|
|
*
|
|
|
|
* @param d The value to check
|
|
|
|
* @return True if the value belongs to a Direction, else false
|
|
|
|
*/
|
|
|
|
static inline bool IsValidDirection(Direction d)
|
|
|
|
{
|
|
|
|
return d < DIR_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if an integer value is a valid Axis
|
|
|
|
*
|
|
|
|
* @param d The value to check
|
|
|
|
* @return True if the value belongs to an Axis, else false
|
|
|
|
*/
|
|
|
|
static inline bool IsValidAxis(Axis d)
|
|
|
|
{
|
|
|
|
return d < AXIS_END;
|
|
|
|
}
|
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
|
|
|
* Return the reverse of a direction
|
|
|
|
*
|
|
|
|
* @param d The direction to get the reverse from
|
|
|
|
* @return The reverse Direction
|
|
|
|
*/
|
2006-03-08 06:55:33 +00:00
|
|
|
static inline Direction ReverseDir(Direction d)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidDirection(d));
|
2006-03-08 06:55:33 +00:00
|
|
|
return (Direction)(4 ^ d);
|
|
|
|
}
|
|
|
|
|
2006-03-05 12:34:55 +00:00
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
2015-10-30 16:20:00 +00:00
|
|
|
* Calculate the difference between two directions
|
2007-08-03 12:10:07 +00:00
|
|
|
*
|
|
|
|
* @param d0 The first direction as the base
|
|
|
|
* @param d1 The second direction as the offset from the base
|
2015-10-30 16:20:00 +00:00
|
|
|
* @return The difference how the second direction drifts of the first one.
|
2007-08-03 12:10:07 +00:00
|
|
|
*/
|
2006-03-08 07:48:56 +00:00
|
|
|
static inline DirDiff DirDifference(Direction d0, Direction d1)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidDirection(d0));
|
|
|
|
assert(IsValidDirection(d1));
|
2011-04-01 12:17:23 +00:00
|
|
|
/* Cast to uint so compiler can use bitmask. If the difference is negative
|
|
|
|
* and we used int instead of uint, further "+ 8" would have to be added. */
|
|
|
|
return (DirDiff)((uint)(d0 - d1) % 8);
|
2006-03-08 07:48:56 +00:00
|
|
|
}
|
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
|
|
|
* Applies two differences together
|
|
|
|
*
|
2015-10-30 16:20:00 +00:00
|
|
|
* This function adds two differences together and returns the resulting
|
2007-08-03 12:10:07 +00:00
|
|
|
* difference. So adding two DIRDIFF_REVERSE together results in the
|
|
|
|
* DIRDIFF_SAME difference.
|
|
|
|
*
|
|
|
|
* @param d The first difference
|
|
|
|
* @param delta The second difference to add on
|
|
|
|
* @return The resulting difference
|
|
|
|
*/
|
2006-03-08 07:48:56 +00:00
|
|
|
static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
|
|
|
|
{
|
2011-04-01 12:17:23 +00:00
|
|
|
/* Cast to uint so compiler can use bitmask. Result can never be negative. */
|
|
|
|
return (DirDiff)((uint)(d + delta) % 8);
|
2006-03-08 07:48:56 +00:00
|
|
|
}
|
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
|
|
|
* Change a direction by a given difference
|
|
|
|
*
|
|
|
|
* This functions returns a new direction of the given direction
|
|
|
|
* which is rotated by the given difference.
|
|
|
|
*
|
|
|
|
* @param d The direction to get a new direction from
|
|
|
|
* @param delta The offset/drift applied to the direction
|
|
|
|
* @return The new direction
|
|
|
|
*/
|
2006-03-08 07:48:56 +00:00
|
|
|
static inline Direction ChangeDir(Direction d, DirDiff delta)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidDirection(d));
|
2011-04-01 12:17:23 +00:00
|
|
|
/* Cast to uint so compiler can use bitmask. Result can never be negative. */
|
|
|
|
return (Direction)((uint)(d + delta) % 8);
|
2006-03-08 07:48:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
|
|
|
* Returns the reverse direction of the given DiagDirection
|
|
|
|
*
|
|
|
|
* @param d The DiagDirection to get the reverse from
|
|
|
|
* @return The reverse direction
|
|
|
|
*/
|
2006-03-05 12:34:55 +00:00
|
|
|
static inline DiagDirection ReverseDiagDir(DiagDirection d)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidDiagDirection(d));
|
2006-03-05 12:54:22 +00:00
|
|
|
return (DiagDirection)(2 ^ d);
|
2006-03-05 12:34:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 16:20:00 +00:00
|
|
|
/**
|
|
|
|
* Calculate the difference between two DiagDirection values
|
|
|
|
*
|
|
|
|
* @param d0 The first direction as the base
|
|
|
|
* @param d1 The second direction as the offset from the base
|
|
|
|
* @return The difference how the second direction drifts of the first one.
|
|
|
|
*/
|
|
|
|
static inline DiagDirDiff DiagDirDifference(DiagDirection d0, DiagDirection d1)
|
|
|
|
{
|
|
|
|
assert(IsValidDiagDirection(d0));
|
|
|
|
assert(IsValidDiagDirection(d1));
|
|
|
|
/* Cast to uint so compiler can use bitmask. Result can never be negative. */
|
|
|
|
return (DiagDirDiff)((uint)(d0 - d1) % 4);
|
|
|
|
}
|
2007-09-26 16:12:43 +00:00
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
|
|
|
* Applies a difference on a DiagDirection
|
|
|
|
*
|
|
|
|
* This function applies a difference on a DiagDirection and returns
|
|
|
|
* the new DiagDirection.
|
|
|
|
*
|
|
|
|
* @param d The DiagDirection
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param delta The difference to apply on
|
2007-08-03 12:10:07 +00:00
|
|
|
* @return The new direction which was calculated
|
|
|
|
*/
|
2006-03-08 08:51:26 +00:00
|
|
|
static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidDiagDirection(d));
|
2011-04-01 12:17:23 +00:00
|
|
|
/* Cast to uint so compiler can use bitmask. Result can never be negative. */
|
|
|
|
return (DiagDirection)((uint)(d + delta) % 4);
|
2006-03-08 08:51:26 +00:00
|
|
|
}
|
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
|
|
|
* Convert a Direction to a DiagDirection.
|
|
|
|
*
|
|
|
|
* This function can be used to convert the 8-way Direction to
|
|
|
|
* the 4-way DiagDirection. If the direction cannot be mapped its
|
|
|
|
* "rounded clockwise". So DIR_N becomes DIAGDIR_NE.
|
|
|
|
*
|
|
|
|
* @param dir The direction to convert
|
|
|
|
* @return The resulting DiagDirection, maybe "rounded clockwise".
|
|
|
|
*/
|
2006-03-06 20:28:28 +00:00
|
|
|
static inline DiagDirection DirToDiagDir(Direction dir)
|
2006-03-05 12:34:55 +00:00
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidDirection(dir));
|
2006-03-05 12:34:55 +00:00
|
|
|
return (DiagDirection)(dir >> 1);
|
|
|
|
}
|
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
|
|
|
* Convert a DiagDirection to a Direction.
|
|
|
|
*
|
|
|
|
* This function can be used to convert the 4-way DiagDirection
|
|
|
|
* to the 8-way Direction. As 4-way are less than 8-way not all
|
|
|
|
* possible directions can be calculated.
|
|
|
|
*
|
|
|
|
* @param dir The direction to convert
|
|
|
|
* @return The resulting Direction
|
|
|
|
*/
|
2006-03-06 20:28:28 +00:00
|
|
|
static inline Direction DiagDirToDir(DiagDirection dir)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidDiagDirection(dir));
|
2006-03-06 20:28:28 +00:00
|
|
|
return (Direction)(dir * 2 + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
|
|
|
* Select the other axis as provided.
|
|
|
|
*
|
|
|
|
* This is basically the not-operator for the axis.
|
|
|
|
*
|
|
|
|
* @param a The given axis
|
|
|
|
* @return The other axis
|
|
|
|
*/
|
2006-07-22 08:59:52 +00:00
|
|
|
static inline Axis OtherAxis(Axis a)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidAxis(a));
|
2006-07-22 08:59:52 +00:00
|
|
|
return (Axis)(a ^ 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
|
|
|
* Convert a DiagDirection to the axis.
|
|
|
|
*
|
|
|
|
* This function returns the axis which belongs to the given
|
|
|
|
* DiagDirection. The axis X belongs to the DiagDirection
|
|
|
|
* north-east and south-west.
|
|
|
|
*
|
|
|
|
* @param d The DiagDirection
|
|
|
|
* @return The axis which belongs to the direction
|
|
|
|
*/
|
2006-03-06 20:55:24 +00:00
|
|
|
static inline Axis DiagDirToAxis(DiagDirection d)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidDiagDirection(d));
|
2006-03-06 20:55:24 +00:00
|
|
|
return (Axis)(d & 1);
|
|
|
|
}
|
|
|
|
|
2006-03-15 07:10:41 +00:00
|
|
|
|
2007-08-03 12:10:07 +00:00
|
|
|
/**
|
2006-03-15 07:10:41 +00:00
|
|
|
* Converts an Axis to a DiagDirection
|
2007-08-03 12:10:07 +00:00
|
|
|
*
|
|
|
|
* This function returns the DiagDirection which
|
|
|
|
* belongs to the axis. As 2 directions are mapped to an axis
|
|
|
|
* this function returns the one which points to south,
|
|
|
|
* either south-west (on X axis) or south-east (on Y axis)
|
|
|
|
*
|
|
|
|
* @param a The axis
|
|
|
|
* @return The direction pointed to south
|
2006-03-15 07:10:41 +00:00
|
|
|
*/
|
|
|
|
static inline DiagDirection AxisToDiagDir(Axis a)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidAxis(a));
|
2006-03-15 07:10:41 +00:00
|
|
|
return (DiagDirection)(2 - a);
|
|
|
|
}
|
|
|
|
|
2008-04-24 13:05:51 +00:00
|
|
|
/**
|
|
|
|
* Converts an Axis to a Direction
|
|
|
|
*
|
|
|
|
* This function returns the Direction which
|
|
|
|
* belongs to the axis. As 2 directions are mapped to an axis
|
|
|
|
* this function returns the one which points to south,
|
|
|
|
* either south-west (on X axis) or south-east (on Y axis)
|
|
|
|
*
|
|
|
|
* @param a The axis
|
|
|
|
* @return The direction pointed to south
|
|
|
|
*/
|
|
|
|
static inline Direction AxisToDirection(Axis a)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidAxis(a));
|
2008-04-24 13:05:51 +00:00
|
|
|
return (Direction)(5 - 2 * a);
|
|
|
|
}
|
|
|
|
|
2006-06-04 16:04:15 +00:00
|
|
|
/**
|
|
|
|
* Convert an axis and a flag for north/south into a DiagDirection
|
2007-04-06 04:10:19 +00:00
|
|
|
* @param xy axis to convert
|
2006-06-04 16:04:15 +00:00
|
|
|
* @param ns north -> 0, south -> 1
|
2007-04-06 04:10:19 +00:00
|
|
|
* @return the desired DiagDirection
|
2006-06-04 16:04:15 +00:00
|
|
|
*/
|
|
|
|
static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidAxis(xy));
|
2006-06-04 16:04:15 +00:00
|
|
|
return (DiagDirection)(xy * 3 ^ ns * 2);
|
|
|
|
}
|
|
|
|
|
2011-11-21 20:51:43 +00:00
|
|
|
/**
|
|
|
|
* Checks if a given Direction is diagonal.
|
|
|
|
*
|
|
|
|
* @param dir The given direction.
|
|
|
|
* @return True if the direction is diagonal.
|
|
|
|
*/
|
|
|
|
static inline bool IsDiagonalDirection(Direction dir)
|
|
|
|
{
|
2013-11-25 13:16:06 +00:00
|
|
|
assert(IsValidDirection(dir));
|
2011-11-21 20:51:43 +00:00
|
|
|
return (dir & 1) != 0;
|
|
|
|
}
|
|
|
|
|
2010-12-22 10:50:32 +00:00
|
|
|
#endif /* DIRECTION_FUNC_H */
|