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-19 23:26:02 +00:00
|
|
|
/** @file slope_func.h Functions related to slopes. */
|
2007-10-20 16:50:48 +00:00
|
|
|
|
2007-12-19 23:26:02 +00:00
|
|
|
#ifndef SLOPE_FUNC_H
|
|
|
|
#define SLOPE_FUNC_H
|
2006-04-23 13:48:16 +00:00
|
|
|
|
2007-12-21 19:21:21 +00:00
|
|
|
#include "core/math_func.hpp"
|
2007-12-19 23:26:02 +00:00
|
|
|
#include "slope_type.h"
|
|
|
|
#include "direction_type.h"
|
2007-12-25 23:42:52 +00:00
|
|
|
#include "tile_type.h"
|
2007-10-05 19:36:13 +00:00
|
|
|
|
2007-10-09 21:11:23 +00:00
|
|
|
/**
|
|
|
|
* Rangecheck for Corner enumeration.
|
|
|
|
*
|
|
|
|
* @param corner A #Corner.
|
|
|
|
* @return true iff corner is in a valid range.
|
|
|
|
*/
|
|
|
|
static inline bool IsValidCorner(Corner corner)
|
|
|
|
{
|
2007-11-24 10:38:43 +00:00
|
|
|
return IsInsideMM(corner, 0, CORNER_END);
|
2007-10-09 21:11:23 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 23:26:02 +00:00
|
|
|
|
2007-07-23 16:30:59 +00:00
|
|
|
/**
|
|
|
|
* Checks if a slope is steep.
|
|
|
|
*
|
|
|
|
* @param s The given #Slope.
|
|
|
|
* @return True if the slope is steep, else false.
|
|
|
|
*/
|
2006-04-23 13:48:16 +00:00
|
|
|
static inline bool IsSteepSlope(Slope s)
|
|
|
|
{
|
|
|
|
return (s & SLOPE_STEEP) != 0;
|
|
|
|
}
|
|
|
|
|
2007-10-20 16:50:48 +00:00
|
|
|
/**
|
|
|
|
* Checks for non-continuous slope on halftile foundations.
|
|
|
|
*
|
|
|
|
* @param s The given #Slope.
|
|
|
|
* @return True if the slope is non-continuous, else false.
|
|
|
|
*/
|
|
|
|
static inline bool IsHalftileSlope(Slope s)
|
|
|
|
{
|
|
|
|
return (s & SLOPE_HALFTILE) != 0;
|
|
|
|
}
|
|
|
|
|
2008-01-25 15:47:58 +00:00
|
|
|
/**
|
|
|
|
* Removes a halftile slope from a slope
|
|
|
|
*
|
|
|
|
* Non-halftile slopes remain unmodified.
|
|
|
|
*
|
|
|
|
* @param s A #Slope.
|
2010-04-12 14:12:47 +00:00
|
|
|
* @return The slope s without its halftile slope.
|
2008-01-25 15:47:58 +00:00
|
|
|
*/
|
|
|
|
static inline Slope RemoveHalftileSlope(Slope s)
|
|
|
|
{
|
2008-04-02 14:13:15 +00:00
|
|
|
return s & ~SLOPE_HALFTILE_MASK;
|
2008-01-25 15:47:58 +00:00
|
|
|
}
|
|
|
|
|
2007-07-23 16:30:59 +00:00
|
|
|
/**
|
|
|
|
* Return the complement of a slope.
|
|
|
|
*
|
|
|
|
* This method returns the complement of a slope. The complement of a
|
|
|
|
* slope is a slope with raised corner which aren't raised in the given
|
|
|
|
* slope.
|
|
|
|
*
|
2007-10-20 16:50:48 +00:00
|
|
|
* @pre The slope must neither be steep nor a halftile slope.
|
2007-07-23 16:30:59 +00:00
|
|
|
* @param s The #Slope to get the complement.
|
|
|
|
* @return a complement Slope of the given slope.
|
|
|
|
*/
|
2006-04-23 13:48:16 +00:00
|
|
|
static inline Slope ComplementSlope(Slope s)
|
|
|
|
{
|
2007-10-20 16:50:48 +00:00
|
|
|
assert(!IsSteepSlope(s) && !IsHalftileSlope(s));
|
2008-04-02 14:13:15 +00:00
|
|
|
return s ^ SLOPE_ELEVATED;
|
2006-04-23 13:48:16 +00:00
|
|
|
}
|
|
|
|
|
2008-01-25 15:47:58 +00:00
|
|
|
/**
|
|
|
|
* Tests if a specific slope has exactly one corner raised.
|
|
|
|
*
|
|
|
|
* @param s The #Slope
|
|
|
|
* @return true iff exactly one corner is raised
|
|
|
|
*/
|
|
|
|
static inline bool IsSlopeWithOneCornerRaised(Slope s)
|
|
|
|
{
|
|
|
|
return (s == SLOPE_W) || (s == SLOPE_S) || (s == SLOPE_E) || (s == SLOPE_N);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the slope with a specific corner raised.
|
|
|
|
*
|
|
|
|
* @param corner The #Corner.
|
|
|
|
* @return The #Slope with corner "corner" raised.
|
|
|
|
*/
|
|
|
|
static inline Slope SlopeWithOneCornerRaised(Corner corner)
|
|
|
|
{
|
|
|
|
assert(IsValidCorner(corner));
|
|
|
|
return (Slope)(1 << corner);
|
|
|
|
}
|
|
|
|
|
2007-10-09 21:11:23 +00:00
|
|
|
/**
|
|
|
|
* Tests if a slope has a highest corner (i.e. one corner raised or a steep slope).
|
|
|
|
*
|
2007-10-20 16:50:48 +00:00
|
|
|
* Note: A halftile slope is ignored.
|
|
|
|
*
|
2007-10-09 21:11:23 +00:00
|
|
|
* @param s The #Slope.
|
|
|
|
* @return true iff the slope has a highest corner.
|
|
|
|
*/
|
|
|
|
static inline bool HasSlopeHighestCorner(Slope s)
|
|
|
|
{
|
2008-01-25 15:47:58 +00:00
|
|
|
s = RemoveHalftileSlope(s);
|
|
|
|
return IsSteepSlope(s) || IsSlopeWithOneCornerRaised(s);
|
2007-10-09 21:11:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-26 16:51:10 +00:00
|
|
|
/**
|
|
|
|
* Returns the highest corner of a slope (one corner raised or a steep slope).
|
|
|
|
*
|
2007-10-20 16:50:48 +00:00
|
|
|
* @pre The slope must be a slope with one corner raised or a steep slope. A halftile slope is ignored.
|
2007-07-26 16:51:10 +00:00
|
|
|
* @param s The #Slope.
|
2007-10-05 19:36:13 +00:00
|
|
|
* @return Highest corner.
|
2007-07-26 16:51:10 +00:00
|
|
|
*/
|
2007-10-05 19:36:13 +00:00
|
|
|
static inline Corner GetHighestSlopeCorner(Slope s)
|
2007-07-26 16:51:10 +00:00
|
|
|
{
|
2008-01-25 15:47:58 +00:00
|
|
|
switch (RemoveHalftileSlope(s)) {
|
2007-07-26 16:51:10 +00:00
|
|
|
case SLOPE_W:
|
2007-10-05 19:36:13 +00:00
|
|
|
case SLOPE_STEEP_W: return CORNER_W;
|
2007-07-26 16:51:10 +00:00
|
|
|
case SLOPE_S:
|
2007-10-05 19:36:13 +00:00
|
|
|
case SLOPE_STEEP_S: return CORNER_S;
|
2007-07-26 16:51:10 +00:00
|
|
|
case SLOPE_E:
|
2007-10-05 19:36:13 +00:00
|
|
|
case SLOPE_STEEP_E: return CORNER_E;
|
2007-07-26 16:51:10 +00:00
|
|
|
case SLOPE_N:
|
2007-10-05 19:36:13 +00:00
|
|
|
case SLOPE_STEEP_N: return CORNER_N;
|
2007-07-26 16:51:10 +00:00
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-20 16:50:48 +00:00
|
|
|
/**
|
|
|
|
* Returns the leveled halftile of a halftile slope.
|
|
|
|
*
|
|
|
|
* @pre The slope must be a halftile slope.
|
|
|
|
* @param s The #Slope.
|
|
|
|
* @return The corner of the leveled halftile.
|
|
|
|
*/
|
|
|
|
static inline Corner GetHalftileSlopeCorner(Slope s)
|
|
|
|
{
|
|
|
|
assert(IsHalftileSlope(s));
|
|
|
|
return (Corner)((s >> 6) & 3);
|
|
|
|
}
|
|
|
|
|
2007-09-14 22:27:40 +00:00
|
|
|
/**
|
|
|
|
* Returns the height of the highest corner of a slope relative to TileZ (= minimal height)
|
|
|
|
*
|
|
|
|
* @param s The #Slope.
|
|
|
|
* @return Relative height of highest corner.
|
|
|
|
*/
|
2011-11-04 11:30:37 +00:00
|
|
|
static inline int GetSlopeMaxZ(Slope s)
|
2007-09-14 22:27:40 +00:00
|
|
|
{
|
|
|
|
if (s == SLOPE_FLAT) return 0;
|
2011-11-04 10:20:24 +00:00
|
|
|
if (IsSteepSlope(s)) return 2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the height of the highest corner of a slope relative to TileZ (= minimal height)
|
|
|
|
*
|
|
|
|
* @param s The #Slope.
|
|
|
|
* @return Relative height of highest corner.
|
|
|
|
*/
|
2011-11-04 11:30:37 +00:00
|
|
|
static inline int GetSlopeMaxPixelZ(Slope s)
|
2011-11-04 10:20:24 +00:00
|
|
|
{
|
|
|
|
return GetSlopeMaxZ(s) * TILE_HEIGHT;
|
2007-09-14 22:27:40 +00:00
|
|
|
}
|
|
|
|
|
2007-10-05 19:36:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the opposite corner.
|
|
|
|
*
|
|
|
|
* @param corner A #Corner.
|
|
|
|
* @return The opposite corner to "corner".
|
|
|
|
*/
|
|
|
|
static inline Corner OppositeCorner(Corner corner)
|
|
|
|
{
|
|
|
|
return (Corner)(corner ^ 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-25 15:47:58 +00:00
|
|
|
* Tests if a specific slope has exactly three corners raised.
|
2007-10-05 19:36:13 +00:00
|
|
|
*
|
2008-01-25 15:47:58 +00:00
|
|
|
* @param s The #Slope
|
|
|
|
* @return true iff exactly three corners are raised
|
2007-10-05 19:36:13 +00:00
|
|
|
*/
|
2008-01-25 15:47:58 +00:00
|
|
|
static inline bool IsSlopeWithThreeCornersRaised(Slope s)
|
2007-10-05 19:36:13 +00:00
|
|
|
{
|
2008-01-25 15:47:58 +00:00
|
|
|
return !IsHalftileSlope(s) && !IsSteepSlope(s) && IsSlopeWithOneCornerRaised(ComplementSlope(s));
|
2007-10-05 19:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the slope with all except one corner raised.
|
|
|
|
*
|
|
|
|
* @param corner The #Corner.
|
|
|
|
* @return The #Slope with all corners but "corner" raised.
|
|
|
|
*/
|
|
|
|
static inline Slope SlopeWithThreeCornersRaised(Corner corner)
|
|
|
|
{
|
|
|
|
return ComplementSlope(SlopeWithOneCornerRaised(corner));
|
|
|
|
}
|
|
|
|
|
2008-01-25 15:47:58 +00:00
|
|
|
/**
|
|
|
|
* Returns a specific steep slope
|
|
|
|
*
|
|
|
|
* @param corner A #Corner.
|
|
|
|
* @return The steep #Slope with "corner" as highest corner.
|
|
|
|
*/
|
|
|
|
static inline Slope SteepSlope(Corner corner)
|
|
|
|
{
|
2008-04-02 14:13:15 +00:00
|
|
|
return SLOPE_STEEP | SlopeWithThreeCornersRaised(OppositeCorner(corner));
|
2008-01-25 15:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if a specific slope is an inclined slope.
|
|
|
|
*
|
|
|
|
* @param s The #Slope
|
|
|
|
* @return true iff the slope is inclined.
|
|
|
|
*/
|
|
|
|
static inline bool IsInclinedSlope(Slope s)
|
|
|
|
{
|
|
|
|
return (s == SLOPE_NW) || (s == SLOPE_SW) || (s == SLOPE_SE) || (s == SLOPE_NE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the direction of an inclined slope.
|
|
|
|
*
|
|
|
|
* @param s A #Slope
|
|
|
|
* @return The direction the slope goes up in. Or INVALID_DIAGDIR if the slope is not an inclined slope.
|
|
|
|
*/
|
|
|
|
static inline DiagDirection GetInclinedSlopeDirection(Slope s)
|
|
|
|
{
|
|
|
|
switch (s) {
|
|
|
|
case SLOPE_NE: return DIAGDIR_NE;
|
|
|
|
case SLOPE_SE: return DIAGDIR_SE;
|
|
|
|
case SLOPE_SW: return DIAGDIR_SW;
|
|
|
|
case SLOPE_NW: return DIAGDIR_NW;
|
|
|
|
default: return INVALID_DIAGDIR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-30 17:51:07 +00:00
|
|
|
* Returns the slope that is inclined in a specific direction.
|
2008-01-25 15:47:58 +00:00
|
|
|
*
|
|
|
|
* @param dir A #DiagDirection
|
|
|
|
* @return The #Slope that goes up in direction dir.
|
|
|
|
*/
|
|
|
|
static inline Slope InclinedSlope(DiagDirection dir)
|
|
|
|
{
|
|
|
|
switch (dir) {
|
|
|
|
case DIAGDIR_NE: return SLOPE_NE;
|
|
|
|
case DIAGDIR_SE: return SLOPE_SE;
|
|
|
|
case DIAGDIR_SW: return SLOPE_SW;
|
|
|
|
case DIAGDIR_NW: return SLOPE_NW;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-20 16:50:48 +00:00
|
|
|
/**
|
|
|
|
* Adds a halftile slope to a slope.
|
|
|
|
*
|
|
|
|
* @param s #Slope without a halftile slope.
|
|
|
|
* @param corner The #Corner of the halftile.
|
|
|
|
* @return The #Slope s with the halftile slope added.
|
|
|
|
*/
|
|
|
|
static inline Slope HalftileSlope(Slope s, Corner corner)
|
|
|
|
{
|
|
|
|
assert(IsValidCorner(corner));
|
|
|
|
return (Slope)(s | SLOPE_HALFTILE | (corner << 6));
|
|
|
|
}
|
|
|
|
|
2007-07-26 16:51:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for FOUNDATION_NONE.
|
|
|
|
*
|
|
|
|
* @param f Maybe a #Foundation.
|
|
|
|
* @return true iff f is a foundation.
|
|
|
|
*/
|
|
|
|
static inline bool IsFoundation(Foundation f)
|
|
|
|
{
|
|
|
|
return f != FOUNDATION_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if the foundation is a leveled foundation.
|
|
|
|
*
|
|
|
|
* @param f The #Foundation.
|
|
|
|
* @return true iff f is a leveled foundation.
|
|
|
|
*/
|
|
|
|
static inline bool IsLeveledFoundation(Foundation f)
|
|
|
|
{
|
|
|
|
return f == FOUNDATION_LEVELED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if the foundation is an inclined foundation.
|
|
|
|
*
|
|
|
|
* @param f The #Foundation.
|
|
|
|
* @return true iff f is an inclined foundation.
|
|
|
|
*/
|
|
|
|
static inline bool IsInclinedFoundation(Foundation f)
|
|
|
|
{
|
|
|
|
return (f == FOUNDATION_INCLINED_X) || (f == FOUNDATION_INCLINED_Y);
|
|
|
|
}
|
|
|
|
|
2007-10-20 21:04:14 +00:00
|
|
|
/**
|
|
|
|
* Tests if a foundation is a non-continuous foundation, i.e. halftile-foundation or FOUNDATION_STEEP_BOTH.
|
|
|
|
*
|
|
|
|
* @param f The #Foundation.
|
|
|
|
* @return true iff f is a non-continuous foundation
|
|
|
|
*/
|
|
|
|
static inline bool IsNonContinuousFoundation(Foundation f)
|
|
|
|
{
|
2007-11-24 10:38:43 +00:00
|
|
|
return IsInsideMM(f, FOUNDATION_STEEP_BOTH, FOUNDATION_HALFTILE_N + 1);
|
2007-10-20 21:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the halftile corner of a halftile-foundation
|
|
|
|
*
|
|
|
|
* @pre f != FOUNDATION_STEEP_BOTH
|
|
|
|
*
|
|
|
|
* @param f The #Foundation.
|
|
|
|
* @return The #Corner with track.
|
|
|
|
*/
|
|
|
|
static inline Corner GetHalftileFoundationCorner(Foundation f)
|
|
|
|
{
|
2007-11-24 10:38:43 +00:00
|
|
|
assert(IsInsideMM(f, FOUNDATION_HALFTILE_W, FOUNDATION_HALFTILE_N + 1));
|
2007-10-20 21:04:14 +00:00
|
|
|
return (Corner)(f - FOUNDATION_HALFTILE_W);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if a foundation is a special rail foundation for single horizontal/vertical track.
|
|
|
|
*
|
|
|
|
* @param f The #Foundation.
|
|
|
|
* @return true iff f is a special rail foundation for single horizontal/vertical track.
|
|
|
|
*/
|
|
|
|
static inline bool IsSpecialRailFoundation(Foundation f)
|
|
|
|
{
|
2007-11-24 10:38:43 +00:00
|
|
|
return IsInsideMM(f, FOUNDATION_RAIL_W, FOUNDATION_RAIL_N + 1);
|
2007-10-20 21:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the track corner of a special rail foundation
|
|
|
|
*
|
|
|
|
* @param f The #Foundation.
|
|
|
|
* @return The #Corner with track.
|
|
|
|
*/
|
|
|
|
static inline Corner GetRailFoundationCorner(Foundation f)
|
|
|
|
{
|
|
|
|
assert(IsSpecialRailFoundation(f));
|
|
|
|
return (Corner)(f - FOUNDATION_RAIL_W);
|
|
|
|
}
|
|
|
|
|
2007-07-26 16:51:10 +00:00
|
|
|
/**
|
|
|
|
* Returns the foundation needed to flatten a slope.
|
|
|
|
* The returned foundation is either FOUNDATION_NONE if the tile was already flat, or FOUNDATION_LEVELED.
|
|
|
|
*
|
|
|
|
* @param s The current #Slope.
|
|
|
|
* @return The needed #Foundation.
|
|
|
|
*/
|
|
|
|
static inline Foundation FlatteningFoundation(Slope s)
|
|
|
|
{
|
|
|
|
return (s == SLOPE_FLAT ? FOUNDATION_NONE : FOUNDATION_LEVELED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the along a specific axis inclined foundation.
|
|
|
|
*
|
|
|
|
* @param axis The #Axis.
|
|
|
|
* @return The needed #Foundation.
|
|
|
|
*/
|
|
|
|
static inline Foundation InclinedFoundation(Axis axis)
|
|
|
|
{
|
|
|
|
return (axis == AXIS_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
|
|
|
|
}
|
|
|
|
|
2007-10-20 21:04:14 +00:00
|
|
|
/**
|
|
|
|
* Returns the halftile foundation for single horizontal/vertical track.
|
|
|
|
*
|
|
|
|
* @param corner The #Corner with the track.
|
|
|
|
* @return The wanted #Foundation.
|
|
|
|
*/
|
|
|
|
static inline Foundation HalftileFoundation(Corner corner)
|
|
|
|
{
|
|
|
|
assert(IsValidCorner(corner));
|
|
|
|
return (Foundation)(FOUNDATION_HALFTILE_W + corner);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the special rail foundation for single horizontal/vertical track.
|
|
|
|
*
|
|
|
|
* @param corner The #Corner with the track.
|
|
|
|
* @return The wanted #Foundation.
|
|
|
|
*/
|
|
|
|
static inline Foundation SpecialRailFoundation(Corner corner)
|
|
|
|
{
|
|
|
|
assert(IsValidCorner(corner));
|
|
|
|
return (Foundation)(FOUNDATION_RAIL_W + corner);
|
|
|
|
}
|
|
|
|
|
2010-07-19 17:00:54 +00:00
|
|
|
/**
|
|
|
|
* Returns the #Sprite offset for a given #Slope.
|
|
|
|
*
|
|
|
|
* @param s The #Slope to get the offset for.
|
|
|
|
* @return The sprite offset for this #Slope.
|
|
|
|
*/
|
|
|
|
static inline uint SlopeToSpriteOffset(Slope s)
|
|
|
|
{
|
|
|
|
extern const byte _slope_to_sprite_offset[32];
|
|
|
|
return _slope_to_sprite_offset[s];
|
|
|
|
}
|
|
|
|
|
2007-12-19 23:26:02 +00:00
|
|
|
#endif /* SLOPE_FUNC_H */
|