/*
 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
 */

/** @file track_func.h Different conversion functions from one kind of track to another. */

#ifndef TRACK_FUNC_H
#define TRACK_FUNC_H

#include "track_type.h"
#include "direction_func.h"
#include "slope_func.h"

/**
 * Checks if a Track is valid.
 *
 * @param track The value to check
 * @return true if the given value is a valid track.
 * @note Use this in an assert()
 */
inline bool IsValidTrack(Track track)
{
	return track < Track::End;
}

/**
 * Checks if a Trackdir is valid for road vehicles.
 *
 * @param trackdir The value to check
 * @return true if the given value is a valid Trackdir
 * @note Use this in an assert()
 */
inline bool IsValidTrackdirForRoadVehicle(Trackdir trackdir)
{
	return trackdir < Trackdir::End;
}

/**
 * Checks if a Trackdir is valid for non-road vehicles.
 *
 * @param trackdir The value to check
 * @return true if the given value is a valid Trackdir
 * @note Use this in an assert()
 */
inline bool IsValidTrackdir(Trackdir trackdir)
{
	return trackdir < Trackdir::End && TRACKDIR_BIT_MASK.Test(trackdir);
}

/**
 * Convert an Axis to the corresponding Track
 * Axis::X -> Track::X
 * Axis::Y -> Track::Y
 * Uses the fact that they share the same internal encoding
 *
 * @param a the axis to convert
 * @return the track corresponding to the axis
 */
inline Track AxisToTrack(Axis a)
{
	assert(IsValidAxis(a));
	return static_cast<Track>(to_underlying(a));
}

/**
 * Returns a single horizontal/vertical trackbit that is in a specific tile corner.
 *
 * @param corner The corner of a tile.
 * @return The TrackBits of the track in the corner.
 */
inline TrackBits CornerToTrackBits(Corner corner)
{
	extern const CornerIndexArray<TrackBits> _corner_to_trackbits;
	assert(IsValidCorner(corner));
	return _corner_to_trackbits[corner];
}

/**
 * Maps a Trackdir to the corresponding TrackdirBits value
 * @param trackdir the track direction to convert
 * @return the converted TrackdirBits value
 */
inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
{
	assert(IsValidTrackdir(trackdir));
	return trackdir;
}

/**
 * Removes first Track from TrackBits and returns it
 *
 * This function searches for the first bit in the TrackBits,
 * remove this bit from the parameter and returns the found
 * bit as Track value. It returns Track::Invalid if the
 * parameter was empty. This is basically used in while-loops
 * to get up to 6 possible tracks on a tile until the parameter
 * becomes empty.
 *
 * @param tracks The value with the TrackBits
 * @return The first Track from the TrackBits value
 * @see FindFirstTrack
 */
inline Track RemoveFirstTrack(TrackBits &tracks)
{
	if (tracks.None()) return Track::Invalid;

	assert(!tracks.Any({Track::Wormhole, Track::Depot}));
	Track first = tracks.GetNthSetBit(0).value();
	tracks.Reset(first);
	return first;
}

/**
 * Removes first Trackdir from TrackdirBits and returns it
 *
 * This function searches for the first bit in the TrackdirBits parameter,
 * remove this bit from the parameter and returns the fnound bit as
 * Trackdir value. It returns Trackdir::Invalid if the trackdirs is
 * empty or INVALID_TRACKDIR_BIT. This is basically used in a
 * while-loop to get all track-directions step by step until the value
 * reaches TRACKDIR_BIT_NONE.
 *
 * @param trackdirs The value with the TrackdirBits
 * @return The first Trackdir from the TrackdirBits value
 * @see FindFirstTrackdir
 */
inline Trackdir RemoveFirstTrackdir(TrackdirBits &trackdirs)
{
	if (trackdirs.None() || trackdirs == INVALID_TRACKDIR_BIT) return Trackdir::Invalid;

	assert(!trackdirs.Any({Trackdir::RvRev_NE, Trackdir::RvRev_SE, Trackdir::RvRev_SW, Trackdir::RvRev_NW}));
	Trackdir first = trackdirs.GetNthSetBit(0).value();
	trackdirs.Reset(first);
	return first;
}

/**
 * Returns first Track from TrackBits or Track::Invalid
 *
 * This function returns the first Track found in the TrackBits value as Track-value.
 * It returns Track::Invalid if the parameter is empty.
 *
 * @param tracks The TrackBits value
 * @return The first Track found or Track::Invalid
 * @see RemoveFirstTrack
 */
inline Track FindFirstTrack(TrackBits tracks)
{
	return tracks.GetNthSetBit(0).value_or(Track::Invalid);
}

/**
 * Converts TrackBits to Track.
 *
 * This function converts a TrackBits value to a Track value. As it
 * is not possible to convert two or more tracks to one track the
 * parameter must contain only one track.
 *
 * @param tracks The TrackBits value to convert
 * @return The Track from the value
 * @pre tracks must contains only one Track
 */
inline Track TrackBitsToTrack(TrackBits tracks)
{
	assert(tracks.Count() == 1 && !tracks.Any({Track::Wormhole, Track::Depot}));
	return FindFirstTrack(tracks);
}

/**
 * Returns first Trackdir from TrackdirBits or Trackdir::Invalid
 *
 * This function returns the first Trackdir in the given TrackdirBits value or
 * Trackdir::Invalid if the value is empty. The TrackdirBits must
 * not be INVALID_TRACKDIR_BIT.
 *
 * @param trackdirs The TrackdirBits value
 * @return The first Trackdir from the TrackdirBits or Trackdir::Invalid on empty.
 * @pre trackdirs must not be INVALID_TRACKDIR_BIT
 * @see RemoveFirstTrackdir
 */
inline Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
{
	assert(!trackdirs.Any({Trackdir::RvRev_NE, Trackdir::RvRev_SE, Trackdir::RvRev_SW, Trackdir::RvRev_NW}));
	return trackdirs.GetNthSetBit(0).value_or(Trackdir::Invalid);
}

/*
 * Functions describing logical relations between Tracks, TrackBits, Trackdirs
 * TrackdirBits, Direction and DiagDirections.
 */

/**
 * Find the opposite track to a given track.
 *
 * Track::Lower -> Track::Upper and vice versa, likewise for left/right.
 * Track::X is mapped to Track::Y and reversed.
 *
 * @param t the track to convert
 * @return the opposite track
 */
inline Track TrackToOppositeTrack(Track t)
{
	assert(IsValidTrack(t));
	return static_cast<Track>(to_underlying(t) ^ 1);
}

/**
 * Maps a trackdir to the reverse trackdir.
 *
 * Returns the reverse trackdir of a Trackdir value. The reverse trackdir
 * is the same track with the other direction on it.
 *
 * @param trackdir The Trackdir value
 * @return The reverse trackdir
 * @pre trackdir must not be Trackdir::Invalid
 */
inline Trackdir ReverseTrackdir(Trackdir trackdir)
{
	assert(IsValidTrackdirForRoadVehicle(trackdir));
	return static_cast<Trackdir>(to_underlying(trackdir) ^ 8);
}

/**
 * Returns the Track that a given Trackdir represents
 *
 * This function filters the Track which is used in the Trackdir value and
 * returns it as a Track value.
 *
 * @param trackdir The trackdir value
 * @return The Track which is used in the value
 */
inline Track TrackdirToTrack(Trackdir trackdir)
{
	assert(IsValidTrackdir(trackdir));
	return static_cast<Track>(to_underlying(trackdir) & 0x7);
}

/**
 * Returns a Trackdir for the given Track
 *
 * Since every Track corresponds to two Trackdirs, we choose the
 * one which points between NE and S. Note that the actual
 * implementation is quite futile, but this might change
 * in the future.
 *
 * @param track The given Track
 * @return The Trackdir from the given Track
 */
inline Trackdir TrackToTrackdir(Track track)
{
	assert(IsValidTrack(track));
	return static_cast<Trackdir>(track);
}

/**
 * Returns a TrackdirBit mask from a given Track
 *
 * The TrackdirBit mask contains the two TrackdirBits that
 * correspond with the given Track (one for each direction).
 *
 * @param track The track to get the TrackdirBits from
 * @return The TrackdirBits which the selected tracks
 */
inline TrackdirBits TrackToTrackdirBits(Track track)
{
	Trackdir td = TrackToTrackdir(track);
	return TrackdirToTrackdirBits(td) | TrackdirToTrackdirBits(ReverseTrackdir(td));
}

/**
 * Discards all directional information from a TrackdirBits value
 *
 * Any Track which is present in either direction will be present in the result.
 *
 * @param bits The TrackdirBits to get the TrackBits from
 * @return The TrackBits
 */
inline TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
{
	return static_cast<TrackBits>((bits.base() | (bits.base() >> 8)) & TRACK_BIT_ALL.base());
}

/**
 * Converts TrackBits to TrackdirBits while allowing both directions.
 *
 * @param bits The TrackBits
 * @return The TrackdirBits containing of bits in both directions.
 */
inline TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
{
	return static_cast<TrackdirBits>(bits.base() * 0x101);
}

/**
 * Maps a trackdir to the trackdir that you will end up on if you go straight
 * ahead.
 *
 * This will be the same trackdir for diagonal trackdirs, but a
 * different (alternating) one for straight trackdirs
 *
 * @param trackdir The given trackdir
 * @return The next Trackdir value of the next tile.
 */
inline Trackdir NextTrackdir(Trackdir trackdir)
{
	assert(IsValidTrackdir(trackdir));
	extern const TrackdirIndexArray<Trackdir> _next_trackdir;
	return _next_trackdir[trackdir];
}

/**
 * Maps a track to all tracks that make 90 deg turns with it.
 *
 * For the diagonal directions these are the complement of the
 * direction, for the straight directions these are the
 * two vertical or horizontal tracks, depend on the given direction
 *
 * @param track The given track
 * @return The TrackBits with the tracks marked which cross the given track by 90 deg.
 */
inline TrackBits TrackCrossesTracks(Track track)
{
	assert(IsValidTrack(track));
	extern const TrackIndexArray<TrackBits> _track_crosses_tracks;
	return _track_crosses_tracks[track];
}

/**
 * Maps a trackdir to the (4-way) direction the tile is exited when following
 * that trackdir.
 *
 * For the diagonal directions these are the same directions. For
 * the straight directions these are the directions from the imagined
 * base-tile to the bordering tile which will be joined if the given
 * straight direction is leaved from the base-tile.
 *
 * @param trackdir The given track direction
 * @return The direction which points to the resulting tile if following the Trackdir
 */
inline DiagDirection TrackdirToExitdir(Trackdir trackdir)
{
	assert(IsValidTrackdirForRoadVehicle(trackdir));
	extern const TrackdirIndexArray<DiagDirection> _trackdir_to_exitdir;
	return _trackdir_to_exitdir[trackdir];
}

/**
 * Maps a track and an (4-way) dir to the trackdir that represents the track
 * with the exit in the given direction.
 *
 * For the diagonal tracks the resulting track direction are clear for a given
 * DiagDirection. It either matches the direction or it returns Trackdir::Invalid,
 * as a Track::X cannot be applied with DIAG_SE.
 * For the straight tracks the resulting track direction will be the
 * direction which the DiagDirection is pointing. But this will be Trackdir::Invalid
 * if the DiagDirection is pointing 'away' the track.
 *
 * @param track The track to apply an direction on
 * @param diagdir The DiagDirection to apply on
 * @return The resulting track direction or Trackdir::Invalid if not possible.
 */
inline Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir)
{
	assert(IsValidTrack(track));
	assert(IsValidDiagDirection(diagdir));
	extern const TrackIndexArray<DiagDirectionIndexArray<Trackdir>> _track_exitdir_to_trackdir;
	return _track_exitdir_to_trackdir[track][diagdir];
}

/**
 * Maps a track and an (4-way) dir to the trackdir that represents the track
 * with the entry in the given direction.
 *
 * For the diagonal tracks the return value is clear, its either the matching
 * track direction or Trackdir::Invalid.
 * For the straight tracks this returns the track direction which results if
 * you follow the DiagDirection and then turn by 45 deg left or right on the
 * next tile. The new direction on the new track will be the returning Trackdir
 * value. If the parameters makes no sense like the track Track::Upper and the
 * direction DiagDirection::NE (target track cannot be reached) this function returns
 * Trackdir::Invalid.
 *
 * @param track The target track
 * @param diagdir The direction to "come from"
 * @return the resulting Trackdir or Trackdir::Invalid if not possible.
 */
inline Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdir)
{
	assert(IsValidTrack(track));
	assert(IsValidDiagDirection(diagdir));
	extern const TrackIndexArray<DiagDirectionIndexArray<Trackdir>> _track_enterdir_to_trackdir;
	return _track_enterdir_to_trackdir[track][diagdir];
}

/**
 * Maps a track and a full (8-way) direction to the trackdir that represents
 * the track running in the given direction.
 * @param track The track to get the trackdir for.
 * @param dir The direction along the track.
 * @return The resulting trackdir.
 */
inline Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
{
	assert(IsValidTrack(track));
	assert(IsValidDirection(dir));
	extern const TrackIndexArray<DirectionIndexArray<Trackdir>> _track_direction_to_trackdir;
	return _track_direction_to_trackdir[track][dir];
}

/**
 * Maps a DiagDirection to the associated diagonal Track.
 *
 * @param diagdir The direction
 * @return The resulting Track
 */
inline Track DiagDirToDiagTrack(DiagDirection diagdir)
{
	assert(IsValidDiagDirection(diagdir));
	return static_cast<Track>(to_underlying(diagdir) & 1);
}

/**
 * Maps a (4-way) direction to the diagonal trackdir that runs in that
 * direction.
 *
 * @param diagdir The direction
 * @return The resulting Trackdir direction
 */
inline Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
{
	assert(IsValidDiagDirection(diagdir));
	extern const DiagDirectionIndexArray<Trackdir> _dir_to_diag_trackdir;
	return _dir_to_diag_trackdir[diagdir];
}

/**
 * Returns all trackdirs that can be reached when entering a tile from a given
 * (diagonal) direction.
 *
 * This will obviously include 90 degree turns, since no information is available
 * about the exact angle of entering
 *
 * @param diagdir The joining direction
 * @return The TrackdirBits which can be used from the given direction
 * @see DiagdirReachesTracks
 */
inline TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
{
	assert(IsValidDiagDirection(diagdir));
	extern const DiagDirectionIndexArray<TrackdirBits> _exitdir_reaches_trackdirs;
	return _exitdir_reaches_trackdirs[diagdir];
}

/**
 * Returns all tracks that can be reached when entering a tile from a given
 * (diagonal) direction.
 *
 * This will obviously include 90 degree turns, since no
 * information is available about the exact angle of entering
 *
 * @param diagdir The joining direction
 * @return The tracks which can be used
 * @see DiagdirReachesTrackdirs
 */
inline TrackBits DiagdirReachesTracks(DiagDirection diagdir) { return TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(diagdir)); }

/**
 * Maps a trackdir to the trackdirs that can be reached from it (ie, when
 * entering the next tile.
 *
 * This will include 90 degree turns!
 *
 * @param trackdir The track direction which will be leaved
 * @return The track directions which can be used from this direction (in the next tile)
 */
inline TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
{
	assert(IsValidTrackdir(trackdir));
	extern const DiagDirectionIndexArray<TrackdirBits> _exitdir_reaches_trackdirs;
	return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)];
}
/* Note that there is no direct table for this function (there used to be),
 * but it uses two simpler tables to achieve the result */

/**
 * Maps a trackdir to all trackdirs that make 90 deg turns with it.
 *
 * For the diagonal tracks this returns the track direction bits
 * of the other axis in both directions, which cannot be joined by
 * the given track direction.
 * For the straight tracks this returns all possible 90 deg turns
 * either on the current tile (which no train can joined) or on the
 * bordering tiles.
 *
 * @param trackdir The track direction
 * @return The TrackdirBits which are (more or less) 90 deg turns.
 */
inline TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
{
	assert(IsValidTrackdir(trackdir));
	extern const TrackIndexArray<TrackdirBits> _track_crosses_trackdirs;
	return _track_crosses_trackdirs[TrackdirToTrack(trackdir)];
}

/**
 * Checks if a given Track is diagonal
 *
 * @param track The given track to check
 * @return true if diagonal, else false
 */
inline bool IsDiagonalTrack(Track track)
{
	assert(IsValidTrack(track));
	return (track == Track::X) || (track == Track::Y);
}

/**
 * Checks if a given Trackdir is diagonal.
 *
 * @param trackdir The given trackdir
 * @return true if the trackdir use a diagonal track
 */
inline bool IsDiagonalTrackdir(Trackdir trackdir)
{
	assert(IsValidTrackdir(trackdir));
	return IsDiagonalTrack(TrackdirToTrack(trackdir));
}


/**
 * Checks if the given tracks overlap, ie form a crossing. Basically this
 * means when there is more than one track on the tile, except when there are
 * two parallel tracks.
 * @param  bits The tracks present.
 * @return Whether the tracks present overlap in any way.
 */
inline bool TracksOverlap(TrackBits bits)
{
	/* With no, or only one track, there is no overlap */
	if (bits.Count() <= 1) return false;
	/* We know that there are at least two tracks present. When there are more
	 * than 2 tracks, they will surely overlap. When there are two, they will
	 * always overlap unless they are lower & upper or right & left. */
	return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
}

/**
 * Check if a given track is contained within or overlaps some other tracks.
 *
 * @param tracks Tracks to be tested against
 * @param track The track to test
 * @return true if the track is already in the tracks or overlaps the tracks.
 */
inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
{
	if (tracks.Test(track)) return true;
	return TracksOverlap(tracks | track);
}

/**
 * Checks whether the trackdir means that we are reversing.
 * @param dir the trackdir to check
 * @return true if it is a reversing road trackdir
 */
inline bool IsReversingRoadTrackdir(Trackdir dir)
{
	assert(IsValidTrackdirForRoadVehicle(dir));
	return (to_underlying(dir) & 0x07) >= 6;
}

/**
 * Checks whether the given trackdir is a straight road
 * @param dir the trackdir to check
 * @return true if it is a straight road trackdir
 */
inline bool IsStraightRoadTrackdir(Trackdir dir)
{
	assert(IsValidTrackdirForRoadVehicle(dir));
	return (to_underlying(dir) & 0x06) == 0;
}

/**
 * Checks whether a trackdir on a specific slope is going uphill.
 *
 * Valid for rail and road tracks.
 * Valid for tile-slopes (under foundation) and foundation-slopes (on foundation).
 *
 * @param slope The slope of the tile.
 * @param dir The trackdir of interest.
 * @return true iff the track goes upwards.
 */
inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
{
	assert(IsValidTrackdirForRoadVehicle(dir));
	extern const TrackdirBits _uphill_trackdirs[];
	return _uphill_trackdirs[RemoveHalftileSlope(slope)].Test(dir);
}

/**
 * Determine the side in which the vehicle will leave the tile
 *
 * @param direction vehicle direction
 * @param track vehicle track bits
 * @return side of tile the vehicle will leave
 */
inline DiagDirection VehicleExitDir(Direction direction, TrackBits track)
{
	static const DiagDirectionIndexArray<TrackBits> state_dir_table{Track::Right, Track::Lower, Track::Left, Track::Upper};

	DiagDirection diagdir = DirToDiagDir(direction);

	/* Determine the diagonal direction in which we will exit this tile */
	if (!IsDiagonalDirection(direction) && track != state_dir_table[diagdir]) {
		diagdir = ChangeDiagDir(diagdir, DiagDirDiff::Left90);
	}

	return diagdir;
}

#endif /* TRACK_FUNC_H */
