tile_map.cpp

Go to the documentation of this file.
00001 /* $Id: tile_map.cpp 15299 2009-01-31 20:16:06Z smatz $ */
00002 
00005 #include "stdafx.h"
00006 #include "tile_map.h"
00007 #include "core/math_func.hpp"
00008 #include "settings_type.h"
00009 
00015 Slope GetTileSlope(TileIndex tile, uint *h)
00016 {
00017   assert(tile < MapSize());
00018 
00019   if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY() ||
00020       (_settings_game.construction.freeform_edges && (TileX(tile) == 0 || TileY(tile) == 0))) {
00021     if (h != NULL) *h = TileHeight(tile) * TILE_HEIGHT;
00022     return SLOPE_FLAT;
00023   }
00024 
00025   uint a = TileHeight(tile); // Height of the N corner
00026   uint min = a; // Minimal height of all corners examined so far
00027   uint b = TileHeight(tile + TileDiffXY(1, 0)); // Height of the W corner
00028   if (min > b) min = b;
00029   uint c = TileHeight(tile + TileDiffXY(0, 1)); // Height of the E corner
00030   if (min > c) min = c;
00031   uint d = TileHeight(tile + TileDiffXY(1, 1)); // Height of the S corner
00032   if (min > d) min = d;
00033 
00034   /* Due to the fact that tiles must connect with each other without leaving gaps, the
00035    * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
00036    *
00037    * Also, there is at most 1 corner with height difference of 2.
00038    */
00039 
00040   uint r = SLOPE_FLAT; // Computed slope of the tile
00041 
00042   /* For each corner if not equal to minimum height:
00043    *  - set the SLOPE_STEEP flag if the difference is 2
00044    *  - add the corresponding SLOPE_X constant to the computed slope
00045    */
00046   if ((a -= min) != 0) r += (--a << 4) + SLOPE_N;
00047   if ((c -= min) != 0) r += (--c << 4) + SLOPE_E;
00048   if ((d -= min) != 0) r += (--d << 4) + SLOPE_S;
00049   if ((b -= min) != 0) r += (--b << 4) + SLOPE_W;
00050 
00051   if (h != NULL) *h = min * TILE_HEIGHT;
00052 
00053   return (Slope)r;
00054 }
00055 
00060 uint GetTileZ(TileIndex tile)
00061 {
00062   if (TileX(tile) == MapMaxX() || TileY(tile) == MapMaxY()) return 0;
00063 
00064   uint h = TileHeight(tile); // N corner
00065   h = min(h, TileHeight(tile + TileDiffXY(1, 0))); // W corner
00066   h = min(h, TileHeight(tile + TileDiffXY(0, 1))); // E corner
00067   h = min(h, TileHeight(tile + TileDiffXY(1, 1))); // S corner
00068 
00069   return h * TILE_HEIGHT;
00070 }
00071 
00076 uint GetTileMaxZ(TileIndex t)
00077 {
00078   if (TileX(t) == MapMaxX() || TileY(t) == MapMaxY()) return 0;
00079 
00080   uint h = TileHeight(t); // N corner
00081   h = max(h, TileHeight(t + TileDiffXY(1, 0))); // W corner
00082   h = max(h, TileHeight(t + TileDiffXY(0, 1))); // E corner
00083   h = max(h, TileHeight(t + TileDiffXY(1, 1))); // S corner
00084 
00085   return h * TILE_HEIGHT;
00086 }

Generated on Sun Nov 15 15:40:16 2009 for OpenTTD by  doxygen 1.5.6