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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file tile_map.cpp Global tile accessors. */
|
2007-04-04 03:21:14 +00:00
|
|
|
|
2005-01-29 14:56:25 +00:00
|
|
|
#include "stdafx.h"
|
2007-12-19 23:26:02 +00:00
|
|
|
#include "tile_map.h"
|
2005-01-29 13:33:48 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2014-09-21 07:48:18 +00:00
|
|
|
/**
|
|
|
|
* Get a tile's slope given the heigh of its four corners.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param hnorth The height at the northern corner in the same unit as TileHeight.
|
|
|
|
* @param hwest The height at the western corner in the same unit as TileHeight.
|
|
|
|
* @param heast The height at the eastern corner in the same unit as TileHeight.
|
|
|
|
* @param hsouth The height at the southern corner in the same unit as TileHeight.
|
|
|
|
* @param[out] h The lowest height of the four corners.
|
2014-09-21 07:48:18 +00:00
|
|
|
* @return The slope.
|
|
|
|
*/
|
|
|
|
static Slope GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth, int *h)
|
|
|
|
{
|
|
|
|
/* Due to the fact that tiles must connect with each other without leaving gaps, the
|
|
|
|
* biggest difference in height between any corner and 'min' is between 0, 1, or 2.
|
|
|
|
*
|
|
|
|
* Also, there is at most 1 corner with height difference of 2.
|
|
|
|
*/
|
2021-01-08 10:16:18 +00:00
|
|
|
int hminnw = std::min(hnorth, hwest);
|
|
|
|
int hmines = std::min(heast, hsouth);
|
|
|
|
int hmin = std::min(hminnw, hmines);
|
2014-09-21 07:48:18 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (h != nullptr) *h = hmin;
|
2014-09-21 07:48:18 +00:00
|
|
|
|
2021-01-08 10:16:18 +00:00
|
|
|
int hmaxnw = std::max(hnorth, hwest);
|
|
|
|
int hmaxes = std::max(heast, hsouth);
|
|
|
|
int hmax = std::max(hmaxnw, hmaxes);
|
2014-09-21 07:48:18 +00:00
|
|
|
|
|
|
|
Slope r = SLOPE_FLAT;
|
|
|
|
|
|
|
|
if (hnorth != hmin) r |= SLOPE_N;
|
|
|
|
if (hwest != hmin) r |= SLOPE_W;
|
|
|
|
if (heast != hmin) r |= SLOPE_E;
|
|
|
|
if (hsouth != hmin) r |= SLOPE_S;
|
|
|
|
|
|
|
|
if (hmax - hmin == 2) r |= SLOPE_STEEP;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the slope of a given tile inside the map.
|
2008-10-19 15:39:12 +00:00
|
|
|
* @param tile Tile to compute slope of
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param h If not \c nullptr, pointer to storage of z height
|
2010-08-01 19:44:49 +00:00
|
|
|
* @return Slope of the tile, except for the HALFTILE part
|
|
|
|
*/
|
2011-11-04 11:30:37 +00:00
|
|
|
Slope GetTileSlope(TileIndex tile, int *h)
|
2005-02-07 10:41:45 +00:00
|
|
|
{
|
2019-01-13 19:58:48 +00:00
|
|
|
uint x1 = TileX(tile);
|
|
|
|
uint y1 = TileY(tile);
|
2021-01-08 10:16:18 +00:00
|
|
|
uint x2 = std::min(x1 + 1, MapMaxX());
|
|
|
|
uint y2 = std::min(y1 + 1, MapMaxY());
|
2005-02-07 10:41:45 +00:00
|
|
|
|
2019-01-13 19:58:48 +00:00
|
|
|
int hnorth = TileHeight(tile); // Height of the North corner.
|
|
|
|
int hwest = TileHeight(TileXY(x2, y1)); // Height of the West corner.
|
|
|
|
int heast = TileHeight(TileXY(x1, y2)); // Height of the East corner.
|
|
|
|
int hsouth = TileHeight(TileXY(x2, y2)); // Height of the South corner.
|
2006-06-25 17:39:19 +00:00
|
|
|
|
2014-09-21 07:48:18 +00:00
|
|
|
return GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
|
|
|
|
}
|
2006-06-25 17:39:19 +00:00
|
|
|
|
2014-09-21 07:48:18 +00:00
|
|
|
/**
|
2019-01-13 19:58:48 +00:00
|
|
|
* Return the slope of a given tile, also for tiles outside the map (virtual "black" tiles).
|
2014-09-21 07:48:18 +00:00
|
|
|
*
|
2019-09-29 20:27:32 +00:00
|
|
|
* @param x X coordinate of the tile to compute slope of, may be outside the map.
|
|
|
|
* @param y Y coordinate of the tile to compute slope of, may be outside the map.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param h If not \c nullptr, pointer to storage of z height.
|
2019-01-13 19:58:48 +00:00
|
|
|
* @return Slope of the tile, except for the HALFTILE part.
|
2014-09-21 07:48:18 +00:00
|
|
|
*/
|
|
|
|
Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
|
|
|
|
{
|
|
|
|
int hnorth = TileHeightOutsideMap(x, y); // N corner.
|
|
|
|
int hwest = TileHeightOutsideMap(x + 1, y); // W corner.
|
|
|
|
int heast = TileHeightOutsideMap(x, y + 1); // E corner.
|
|
|
|
int hsouth = TileHeightOutsideMap(x + 1, y + 1); // S corner.
|
|
|
|
|
|
|
|
Slope s = GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (h != nullptr) *h *= TILE_HEIGHT;
|
2014-09-21 07:48:18 +00:00
|
|
|
return s;
|
2005-02-07 10:41:45 +00:00
|
|
|
}
|
|
|
|
|
2013-10-12 22:07:58 +00:00
|
|
|
/**
|
|
|
|
* Check if a given tile is flat
|
|
|
|
* @param tile Tile to check
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param h If not \c nullptr, pointer to storage of z height (only if tile is flat)
|
2013-10-12 22:07:58 +00:00
|
|
|
* @return Whether the tile is flat
|
|
|
|
*/
|
|
|
|
bool IsTileFlat(TileIndex tile, int *h)
|
|
|
|
{
|
2019-01-13 19:58:48 +00:00
|
|
|
uint x1 = TileX(tile);
|
|
|
|
uint y1 = TileY(tile);
|
2021-01-08 10:16:18 +00:00
|
|
|
uint x2 = std::min(x1 + 1, MapMaxX());
|
|
|
|
uint y2 = std::min(y1 + 1, MapMaxY());
|
2013-10-12 22:07:58 +00:00
|
|
|
|
|
|
|
uint z = TileHeight(tile);
|
2019-01-13 19:58:48 +00:00
|
|
|
if (TileHeight(TileXY(x2, y1)) != z) return false;
|
|
|
|
if (TileHeight(TileXY(x1, y2)) != z) return false;
|
|
|
|
if (TileHeight(TileXY(x2, y2)) != z) return false;
|
2013-10-12 22:07:58 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (h != nullptr) *h = z;
|
2013-10-12 22:07:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-19 15:39:12 +00:00
|
|
|
/**
|
|
|
|
* Get bottom height of the tile
|
|
|
|
* @param tile Tile to compute height of
|
2010-08-01 19:44:49 +00:00
|
|
|
* @return Minimum height of the tile
|
|
|
|
*/
|
2011-11-04 11:30:37 +00:00
|
|
|
int GetTileZ(TileIndex tile)
|
2005-02-07 10:41:45 +00:00
|
|
|
{
|
2019-01-13 19:58:48 +00:00
|
|
|
uint x1 = TileX(tile);
|
|
|
|
uint y1 = TileY(tile);
|
2021-01-08 10:16:18 +00:00
|
|
|
uint x2 = std::min(x1 + 1, MapMaxX());
|
|
|
|
uint y2 = std::min(y1 + 1, MapMaxY());
|
|
|
|
|
|
|
|
return std::min({
|
|
|
|
TileHeight(tile), // N corner
|
|
|
|
TileHeight(TileXY(x2, y1)), // W corner
|
|
|
|
TileHeight(TileXY(x1, y2)), // E corner
|
|
|
|
TileHeight(TileXY(x2, y2)), // S corner
|
|
|
|
});
|
2005-02-07 10:41:45 +00:00
|
|
|
}
|
2006-05-07 07:55:05 +00:00
|
|
|
|
2014-09-21 07:48:18 +00:00
|
|
|
/**
|
|
|
|
* Get top height of the tile inside the map.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param t Tile to compute height of
|
2010-08-01 19:44:49 +00:00
|
|
|
* @return Maximum height of the tile
|
|
|
|
*/
|
2011-11-04 11:30:37 +00:00
|
|
|
int GetTileMaxZ(TileIndex t)
|
2006-05-07 07:55:05 +00:00
|
|
|
{
|
2019-01-13 19:58:48 +00:00
|
|
|
uint x1 = TileX(t);
|
|
|
|
uint y1 = TileY(t);
|
2021-01-08 10:16:18 +00:00
|
|
|
uint x2 = std::min(x1 + 1, MapMaxX());
|
|
|
|
uint y2 = std::min(y1 + 1, MapMaxY());
|
|
|
|
|
|
|
|
return std::max({
|
|
|
|
TileHeight(t), // N corner
|
|
|
|
TileHeight(TileXY(x2, y1)), // W corner
|
|
|
|
TileHeight(TileXY(x1, y2)), // E corner
|
|
|
|
TileHeight(TileXY(x2, y2)), // S corner
|
|
|
|
});
|
2006-05-07 07:55:05 +00:00
|
|
|
}
|