# tile_map.cpp

Go to the documentation of this file.

    00001 /* $Id: tile_map.cpp 25849 2013-10-12 22:07:58Z zuu $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "tile_map.h"
    00014
    00021 Slope GetTileSlope(TileIndex tile, int *h)
    00022 {
    00023   assert(tile < MapSize());
    00024
    00025   if (!IsInnerTile(tile)) {
    00026     if (h != NULL) *h = TileHeight(tile);
    00027     return SLOPE_FLAT;
    00028   }
    00029
    00030   int a = TileHeight(tile); // Height of the N corner
    00031   int min = a; // Minimal height of all corners examined so far
    00032   int b = TileHeight(tile + TileDiffXY(1, 0)); // Height of the W corner
    00033   if (min > b) min = b;
    00034   int c = TileHeight(tile + TileDiffXY(0, 1)); // Height of the E corner
    00035   if (min > c) min = c;
    00036   int d = TileHeight(tile + TileDiffXY(1, 1)); // Height of the S corner
    00037   if (min > d) min = d;
    00038
    00039   /* Due to the fact that tiles must connect with each other without leaving gaps, the
    00040    * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
    00041    *
    00042    * Also, there is at most 1 corner with height difference of 2.
    00043    */
    00044
    00045   uint r = SLOPE_FLAT; // Computed slope of the tile
    00046
    00047   /* For each corner if not equal to minimum height:
    00048    *  - set the SLOPE_STEEP flag if the difference is 2
    00049    *  - add the corresponding SLOPE_X constant to the computed slope
    00050    */
    00051   if ((a -= min) != 0) r += (--a << 4) + SLOPE_N;
    00052   if ((c -= min) != 0) r += (--c << 4) + SLOPE_E;
    00053   if ((d -= min) != 0) r += (--d << 4) + SLOPE_S;
    00054   if ((b -= min) != 0) r += (--b << 4) + SLOPE_W;
    00055
    00056   if (h != NULL) *h = min;
    00057
    00058   return (Slope)r;
    00059 }
    00060
    00067 bool IsTileFlat(TileIndex tile, int *h)
    00068 {
    00069   assert(tile < MapSize());
    00070
    00071   if (!IsInnerTile(tile)) {
    00072     if (h != NULL) *h = TileHeight(tile);
    00073     return true;
    00074   }
    00075
    00076   uint z = TileHeight(tile);
    00077   if (TileHeight(tile + TileDiffXY(1, 0)) != z) return false;
    00078   if (TileHeight(tile + TileDiffXY(0, 1)) != z) return false;
    00079   if (TileHeight(tile + TileDiffXY(1, 1)) != z) return false;
    00080
    00081   if (h != NULL) *h = z;
    00082   return true;
    00083 }
    00084
    00090 int GetTileZ(TileIndex tile)
    00091 {
    00092   if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY()) return 0;
    00093
    00094   int h = TileHeight(tile); // N corner
    00095   h = min(h, TileHeight(tile + TileDiffXY(1, 0))); // W corner
    00096   h = min(h, TileHeight(tile + TileDiffXY(0, 1))); // E corner
    00097   h = min(h, TileHeight(tile + TileDiffXY(1, 1))); // S corner
    00098
    00099   return h;
    00100 }
    00101
    00107 int GetTileMaxZ(TileIndex t)
    00108 {
    00109   if (TileX(t) == MapMaxX() || TileY(t) == MapMaxY()) return 0;
    00110
    00111   int h = TileHeight(t); // N corner
    00112   h = max<int>(h, TileHeight(t + TileDiffXY(1, 0))); // W corner
    00113   h = max<int>(h, TileHeight(t + TileDiffXY(0, 1))); // E corner
    00114   h = max<int>(h, TileHeight(t + TileDiffXY(1, 1))); // S corner
    00115
    00116   return h;
    00117 }