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