tile_map.cpp

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

Generated on Fri Jan 9 19:01:52 2009 for openttd by  doxygen 1.5.6