1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file landscape.h Functions related to OTTD's landscape. */
9 
10 #ifndef LANDSCAPE_H
11 #define LANDSCAPE_H
12 
13 #include "core/geometry_type.hpp"
14 #include "tile_cmd.h"
15 
16 static const uint SNOW_LINE_MONTHS = 12; ///< Number of months in the snow line table.
17 static const uint SNOW_LINE_DAYS   = 32; ///< Number of days in each month in the snow line table.
18 
19 /**
20  * Structure describing the height of the snow line each day of the year
21  * @ingroup SnowLineGroup
22  */
23 struct SnowLine {
24 	byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]; ///< Height of the snow line each day of the year
25 	byte highest_value; ///< Highest snow line of the year
26 	byte lowest_value;  ///< Lowest snow line of the year
27 };
28 
29 bool IsSnowLineSet();
30 void SetSnowLine(byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]);
31 byte GetSnowLine();
32 byte HighestSnowLine();
33 byte LowestSnowLine();
34 void ClearSnowLine();
35 
36 int GetSlopeZInCorner(Slope tileh, Corner corner);
37 Slope GetFoundationSlope(TileIndex tile, int *z = nullptr);
38 
39 uint GetPartialPixelZ(int x, int y, Slope corners);
40 int GetSlopePixelZ(int x, int y);
41 int GetSlopePixelZOutsideMap(int x, int y);
42 void GetSlopePixelZOnEdge(Slope tileh, DiagDirection edge, int *z1, int *z2);
43 
44 /**
45  * Determine the Z height of a corner relative to TileZ.
46  *
47  * @pre The slope must not be a halftile slope.
48  *
49  * @param tileh The slope.
50  * @param corner The corner.
51  * @return Z position of corner relative to TileZ.
52  */
GetSlopePixelZInCorner(Slope tileh,Corner corner)53 static inline int GetSlopePixelZInCorner(Slope tileh, Corner corner)
54 {
55 	return GetSlopeZInCorner(tileh, corner) * TILE_HEIGHT;
56 }
57 
58 /**
59  * Get slope of a tile on top of a (possible) foundation
60  * If a tile does not have a foundation, the function returns the same as GetTilePixelSlope.
61  *
62  * @param tile The tile of interest.
63  * @param z returns the z of the foundation slope. (Can be nullptr, if not needed)
64  * @return The slope on top of the foundation.
65  */
GetFoundationPixelSlope(TileIndex tile,int * z)66 static inline Slope GetFoundationPixelSlope(TileIndex tile, int *z)
67 {
68 	assert(z != nullptr);
69 	Slope s = GetFoundationSlope(tile, z);
70 	*z *= TILE_HEIGHT;
71 	return s;
72 }
73 
74 /**
75  * Map 3D world or tile coordinate to equivalent 2D coordinate as used in the viewports and smallmap.
76  * @param x X world or tile coordinate (runs in SW direction in the 2D view).
77  * @param y Y world or tile coordinate (runs in SE direction in the 2D view).
78  * @param z Z world or tile coordinate (runs in N direction in the 2D view).
79  * @return Equivalent coordinate in the 2D view.
80  * @see RemapCoords2
81  */
RemapCoords(int x,int y,int z)82 static inline Point RemapCoords(int x, int y, int z)
83 {
84 	Point pt;
85 	pt.x = (y - x) * 2 * ZOOM_LVL_BASE;
86 	pt.y = (y + x - z) * ZOOM_LVL_BASE;
87 	return pt;
88 }
89 
90 /**
91  * Map 3D world or tile coordinate to equivalent 2D coordinate as used in the viewports and smallmap.
92  * Same as #RemapCoords, except the Z coordinate is read from the map.
93  * @param x X world or tile coordinate (runs in SW direction in the 2D view).
94  * @param y Y world or tile coordinate (runs in SE direction in the 2D view).
95  * @return Equivalent coordinate in the 2D view.
96  * @see RemapCoords
97  */
RemapCoords2(int x,int y)98 static inline Point RemapCoords2(int x, int y)
99 {
100 	return RemapCoords(x, y, GetSlopePixelZ(x, y));
101 }
102 
103 /**
104  * Map 2D viewport or smallmap coordinate to 3D world or tile coordinate.
105  * Function assumes <tt>z == 0</tt>. For other values of \p z, add \p z to \a y before the call.
106  * @param x X coordinate of the 2D coordinate.
107  * @param y Y coordinate of the 2D coordinate.
108  * @return X and Y components of equivalent world or tile coordinate.
109  * @note Inverse of #RemapCoords function. Smaller values may get rounded.
110  * @see InverseRemapCoords2
111  */
InverseRemapCoords(int x,int y)112 static inline Point InverseRemapCoords(int x, int y)
113 {
114 	Point pt = {(y * 2 - x) >> (2 + ZOOM_LVL_SHIFT), (y * 2 + x) >> (2 + ZOOM_LVL_SHIFT)};
115 	return pt;
116 }
117 
118 Point InverseRemapCoords2(int x, int y, bool clamp_to_map = false, bool *clamped = nullptr);
119 
120 uint ApplyFoundationToSlope(Foundation f, Slope *s);
121 /**
122  * Applies a foundation to a slope.
123  *
124  * @pre      Foundation and slope must be valid combined.
125  * @param f  The #Foundation.
126  * @param s  The #Slope to modify.
127  * @return   Increment to the tile Z coordinate.
128  */
ApplyPixelFoundationToSlope(Foundation f,Slope * s)129 static inline uint ApplyPixelFoundationToSlope(Foundation f, Slope *s)
130 {
131 	return ApplyFoundationToSlope(f, s) * TILE_HEIGHT;
132 }
133 
134 void DrawFoundation(TileInfo *ti, Foundation f);
135 bool HasFoundationNW(TileIndex tile, Slope slope_here, uint z_here);
136 bool HasFoundationNE(TileIndex tile, Slope slope_here, uint z_here);
137 
138 void DoClearSquare(TileIndex tile);
139 void RunTileLoop();
140 
141 void InitializeLandscape();
142 void GenerateLandscape(byte mode);
143 
144 #endif /* LANDSCAPE_H */
145