# 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
    00015 static Slope GetTileSlopeGivenHeight(uint hnorth, uint hwest, uint heast, uint hsouth, uint *h) {
    00016
    00017   /* Due to the fact that tiles must connect with each other without leaving gaps, the
    00018    * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
    00019    *
    00020    * Also, there is at most 1 corner with height difference of 2. */
    00021
    00022   uint hminnw = min(hnorth, hwest);
    00023   uint hmines = min(heast, hsouth);
    00024   uint hmin = min(hminnw, hmines);
    00025
    00026   uint hmaxnw = max(hnorth, hwest);
    00027   uint hmaxes = max(heast, hsouth);
    00028   uint hmax = max(hmaxnw, hmaxes);
    00029
    00030   uint r = SLOPE_FLAT;
    00031
    00032   if (hnorth != hmin) {
    00033     r += SLOPE_N;
    00034   }
    00035   if (hwest != hmin) {
    00036     r += SLOPE_W;
    00037   }
    00038   if (heast != hmin) {
    00039     r += SLOPE_E;
    00040   }
    00041   if (hsouth != hmin) {
    00042     r += SLOPE_S;
    00043   }
    00044   if (hmax - hmin == 2) {
    00045     r += SLOPE_STEEP;
    00046   }
    00047
    00048   if (h != NULL) *h = hmin * TILE_HEIGHT;
    00049
    00050   return (Slope)r;
    00051 }
    00052
    00059 Slope GetTileSlope(TileIndex tile, uint *h)
    00060 {
    00061   assert(tile < MapSize());
    00062
    00063   if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY() ||
    00064       (_settings_game.construction.freeform_edges && (TileX(tile) == 0 || TileY(tile) == 0))) {
    00065     if (h != NULL) *h = TileHeight(tile) * TILE_HEIGHT;
    00066     return SLOPE_FLAT;
    00067   }
    00068
    00069   uint hnorth = TileHeight(tile);                    // Height of the North corner.
    00070   uint hwest = TileHeight(tile + TileDiffXY(1, 0));  // Height of the West corner.
    00071   uint heast = TileHeight(tile + TileDiffXY(0, 1));  // Height of the East corner.
    00072   uint hsouth = TileHeight(tile + TileDiffXY(1, 1)); // Height of the South corner.
    00073
    00074   return GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
    00075 }
    00076
    00083 Slope GetTileSlopeOutsideMap(int x, int y, uint *h)
    00084 {
    00085   uint hnorth = TileHeightOutsideMap(x, y);         // N corner.
    00086   uint hwest = TileHeightOutsideMap(x + 1, y);      // W corner.
    00087   uint heast = TileHeightOutsideMap(x, y + 1);      // E corner.
    00088   uint hsouth = TileHeightOutsideMap(x + 1, y + 1); // S corner.
    00089
    00090   return GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
    00091 }
    00092
    00098 uint GetTileZ(TileIndex tile)
    00099 {
    00100   if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY()) return 0;
    00101
    00102   uint h = TileHeight(tile);                       // N corner.
    00103   h = min(h, TileHeight(tile + TileDiffXY(1, 0))); // W corner.
    00104   h = min(h, TileHeight(tile + TileDiffXY(0, 1))); // E corner.
    00105   h = min(h, TileHeight(tile + TileDiffXY(1, 1))); // S corner.
    00106
    00107   return h * TILE_HEIGHT;
    00108 }
    00109
    00115 uint GetTileZOutsideMap(int x, int y)
    00116 {
    00117   uint h = TileHeightOutsideMap(x, y);            // N corner.
    00118   h = min(h, TileHeightOutsideMap(x + 1, y));     // W corner.
    00119   h = min(h, TileHeightOutsideMap(x, y + 1));     // E corner.
    00120   h = min(h, TileHeightOutsideMap(x + 1, y + 1)); // S corner.
    00121
    00122   return h * TILE_HEIGHT;
    00123 }
    00124
    00130 uint GetTileMaxZ(TileIndex t)
    00131 {
    00132   if (TileX(t) == MapMaxX() || TileY(t) == MapMaxY()) return TileHeightOutsideMap(TileX(t), TileY(t));
    00133
    00134   uint h = TileHeight(t); // N corner
    00135   h = max(h, TileHeight(t + TileDiffXY(1, 0))); // W corner
    00136   h = max(h, TileHeight(t + TileDiffXY(0, 1))); // E corner
    00137   h = max(h, TileHeight(t + TileDiffXY(1, 1))); // S corner
    00138
    00139   return h * TILE_HEIGHT;
    00140 }
    00141
    00150 uint GetTileMaxZOutsideMap(int x, int y)
    00151 {
    00152   uint h = TileHeightOutsideMap(x, y);
    00153   h = max(h, TileHeightOutsideMap(x + 1, y));
    00154   h = max(h, TileHeightOutsideMap(x, y + 1));
    00155   h = max(h, TileHeightOutsideMap(x + 1, y + 1));
    00156
    00157   return h * TILE_HEIGHT;
    00158 }