# tile_map.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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, uint *h)
    00022 {
    00023   assert(tile < MapSize());
    00024
    00025   if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY() ||
    00026       (_settings_game.construction.freeform_edges && (TileX(tile) == 0 || TileY(tile) == 0))) {
    00027     if (h != NULL) *h = TileHeight(tile) * TILE_HEIGHT;
    00028     return SLOPE_FLAT;
    00029   }
    00030
    00031   uint a = TileHeight(tile); // Height of the N corner
    00032   uint min = a; // Minimal height of all corners examined so far
    00033   uint b = TileHeight(tile + TileDiffXY(1, 0)); // Height of the W corner
    00034   if (min > b) min = b;
    00035   uint c = TileHeight(tile + TileDiffXY(0, 1)); // Height of the E corner
    00036   if (min > c) min = c;
    00037   uint d = TileHeight(tile + TileDiffXY(1, 1)); // Height of the S corner
    00038   if (min > d) min = d;
    00039
    00040   /* Due to the fact that tiles must connect with each other without leaving gaps, the
    00041    * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
    00042    *
    00043    * Also, there is at most 1 corner with height difference of 2.
    00044    */
    00045
    00046   uint r = SLOPE_FLAT; // Computed slope of the tile
    00047
    00048   /* For each corner if not equal to minimum height:
    00049    *  - set the SLOPE_STEEP flag if the difference is 2
    00050    *  - add the corresponding SLOPE_X constant to the computed slope
    00051    */
    00052   if ((a -= min) != 0) r += (--a << 4) + SLOPE_N;
    00053   if ((c -= min) != 0) r += (--c << 4) + SLOPE_E;
    00054   if ((d -= min) != 0) r += (--d << 4) + SLOPE_S;
    00055   if ((b -= min) != 0) r += (--b << 4) + SLOPE_W;
    00056
    00057   if (h != NULL) *h = min * TILE_HEIGHT;
    00058
    00059   return (Slope)r;
    00060 }
    00061
    00067 uint GetTileZ(TileIndex tile)
    00068 {
    00069   if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY()) return 0;
    00070
    00071   uint h = TileHeight(tile); // N corner
    00072   h = min(h, TileHeight(tile + TileDiffXY(1, 0))); // W corner
    00073   h = min(h, TileHeight(tile + TileDiffXY(0, 1))); // E corner
    00074   h = min(h, TileHeight(tile + TileDiffXY(1, 1))); // S corner
    00075
    00076   return h * TILE_HEIGHT;
    00077 }
    00078
    00084 uint GetTileMaxZ(TileIndex t)
    00085 {
    00086   if (TileX(t) == MapMaxX() || TileY(t) == MapMaxY()) return 0;
    00087
    00088   uint h = TileHeight(t); // N corner
    00089   h = max(h, TileHeight(t + TileDiffXY(1, 0))); // W corner
    00090   h = max(h, TileHeight(t + TileDiffXY(0, 1))); // E corner
    00091   h = max(h, TileHeight(t + TileDiffXY(1, 1))); // S corner
    00092
    00093   return h * TILE_HEIGHT;
    00094 }