OpenTTD
tile_map.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "tile_map.h"
14 
15 #include "safeguards.h"
16 
26 static Slope GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth, int *h)
27 {
28  /* Due to the fact that tiles must connect with each other without leaving gaps, the
29  * biggest difference in height between any corner and 'min' is between 0, 1, or 2.
30  *
31  * Also, there is at most 1 corner with height difference of 2.
32  */
33  int hminnw = min(hnorth, hwest);
34  int hmines = min(heast, hsouth);
35  int hmin = min(hminnw, hmines);
36 
37  if (h != NULL) *h = hmin;
38 
39  int hmaxnw = max(hnorth, hwest);
40  int hmaxes = max(heast, hsouth);
41  int hmax = max(hmaxnw, hmaxes);
42 
43  Slope r = SLOPE_FLAT;
44 
45  if (hnorth != hmin) r |= SLOPE_N;
46  if (hwest != hmin) r |= SLOPE_W;
47  if (heast != hmin) r |= SLOPE_E;
48  if (hsouth != hmin) r |= SLOPE_S;
49 
50  if (hmax - hmin == 2) r |= SLOPE_STEEP;
51 
52  return r;
53 }
54 
62 {
63  uint x1 = TileX(tile);
64  uint y1 = TileY(tile);
65  uint x2 = min(x1 + 1, MapMaxX());
66  uint y2 = min(y1 + 1, MapMaxY());
67 
68  int hnorth = TileHeight(tile); // Height of the North corner.
69  int hwest = TileHeight(TileXY(x2, y1)); // Height of the West corner.
70  int heast = TileHeight(TileXY(x1, y2)); // Height of the East corner.
71  int hsouth = TileHeight(TileXY(x2, y2)); // Height of the South corner.
72 
73  return GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
74 }
75 
84 Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
85 {
86  int hnorth = TileHeightOutsideMap(x, y); // N corner.
87  int hwest = TileHeightOutsideMap(x + 1, y); // W corner.
88  int heast = TileHeightOutsideMap(x, y + 1); // E corner.
89  int hsouth = TileHeightOutsideMap(x + 1, y + 1); // S corner.
90 
91  Slope s = GetTileSlopeGivenHeight(hnorth, hwest, heast, hsouth, h);
92  if (h != NULL) *h *= TILE_HEIGHT;
93  return s;
94 }
95 
102 bool IsTileFlat(TileIndex tile, int *h)
103 {
104  uint x1 = TileX(tile);
105  uint y1 = TileY(tile);
106  uint x2 = min(x1 + 1, MapMaxX());
107  uint y2 = min(y1 + 1, MapMaxY());
108 
109  uint z = TileHeight(tile);
110  if (TileHeight(TileXY(x2, y1)) != z) return false;
111  if (TileHeight(TileXY(x1, y2)) != z) return false;
112  if (TileHeight(TileXY(x2, y2)) != z) return false;
113 
114  if (h != NULL) *h = z;
115  return true;
116 }
117 
124 {
125  uint x1 = TileX(tile);
126  uint y1 = TileY(tile);
127  uint x2 = min(x1 + 1, MapMaxX());
128  uint y2 = min(y1 + 1, MapMaxY());
129 
130  int h = TileHeight(tile); // N corner
131  h = min(h, TileHeight(TileXY(x2, y1))); // W corner
132  h = min(h, TileHeight(TileXY(x1, y2))); // E corner
133  h = min(h, TileHeight(TileXY(x2, y2))); // S corner
134 
135  return h;
136 }
137 
144 {
145  uint x1 = TileX(t);
146  uint y1 = TileY(t);
147  uint x2 = min(x1 + 1, MapMaxX());
148  uint y2 = min(y1 + 1, MapMaxY());
149 
150  int h = TileHeight(t); // N corner
151  h = max<int>(h, TileHeight(TileXY(x2, y1))); // W corner
152  h = max<int>(h, TileHeight(TileXY(x1, y2))); // E corner
153  h = max<int>(h, TileHeight(TileXY(x2, y2))); // S corner
154 
155  return h;
156 }
the north corner of the tile is raised
Definition: slope_type.h:55
the west corner of the tile is raised
Definition: slope_type.h:52
Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
Return the slope of a given tile, also for tiles outside the map (virtual "black" tiles)...
Definition: tile_map.cpp:84
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
the east corner of the tile is raised
Definition: slope_type.h:54
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
bool IsTileFlat(TileIndex tile, int *h)
Check if a given tile is flat.
Definition: tile_map.cpp:102
a flat tile
Definition: slope_type.h:51
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:123
static Slope GetTileSlopeGivenHeight(int hnorth, int hwest, int heast, int hsouth, int *h)
Get a tile&#39;s slope given the heigh of its four corners.
Definition: tile_map.cpp:26
Slope GetTileSlope(TileIndex tile, int *h)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:61
indicates the slope is steep
Definition: slope_type.h:56
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
Definition: tile_type.h:18
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:143
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
Slope
Enumeration for the slope-type.
Definition: slope_type.h:50
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:113
static uint TileHeight(TileIndex tile)
Returns the height of a tile.
Definition: tile_map.h:31
static uint TileHeightOutsideMap(int x, int y)
Returns the height of a tile, also for tiles outside the map (virtual "black" tiles).
Definition: tile_map.h:44
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:104
Map writing/reading functions for tiles.
the south corner of the tile is raised
Definition: slope_type.h:53
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:165