OpenTTD Source 20250218-master-g53dd1258a7
heightmap.cpp
Go to the documentation of this file.
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
10#include "stdafx.h"
11#include "heightmap.h"
12#include "clear_map.h"
13#include "strings_func.h"
14#include "void_map.h"
15#include "error.h"
16#include "saveload/saveload.h"
17#include "bmp.h"
18#include "gfx_func.h"
19#include "fios.h"
20#include "fileio_func.h"
21
22#include "table/strings.h"
23
24#include "safeguards.h"
25
32
33/*
34 * Maximum size in pixels of the heightmap image.
35 */
36static const uint MAX_HEIGHTMAP_SIZE_PIXELS = 256 << 20; // ~256 million
37/*
38 * When loading a PNG or BMP the 24 bpp variant requires at least 4 bytes per pixel
39 * of memory to load the data. Make sure the "reasonable" limit is well within the
40 * maximum amount of memory allocatable on 32 bit platforms.
41 */
42static_assert(MAX_HEIGHTMAP_SIZE_PIXELS < UINT32_MAX / 8);
43
53static inline bool IsValidHeightmapDimension(size_t width, size_t height)
54{
55 return (uint64_t)width * height <= MAX_HEIGHTMAP_SIZE_PIXELS &&
56 width > 0 && width <= MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS &&
57 height > 0 && height <= MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS;
58}
59
64static inline uint8_t RGBToGrayscale(uint8_t red, uint8_t green, uint8_t blue)
65{
66 /* To avoid doubles and stuff, multiply it with a total of 65536 (16bits), then
67 * divide by it to normalize the value to a byte again. */
68 return ((red * 19595) + (green * 38470) + (blue * 7471)) / 65536;
69}
70
71
72#ifdef WITH_PNG
73
74#include <png.h>
75
79static void ReadHeightmapPNGImageData(std::span<uint8_t> map, png_structp png_ptr, png_infop info_ptr)
80{
81 uint x, y;
82 uint8_t gray_palette[256];
83 png_bytep *row_pointers = nullptr;
84 bool has_palette = png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE;
85 uint channels = png_get_channels(png_ptr, info_ptr);
86
87 /* Get palette and convert it to grayscale */
88 if (has_palette) {
89 int i;
90 int palette_size;
91 png_color *palette;
92 bool all_gray = true;
93
94 png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size);
95 for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) {
96 all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue;
97 gray_palette[i] = RGBToGrayscale(palette[i].red, palette[i].green, palette[i].blue);
98 }
99
106 if (palette_size == 16 && !all_gray) {
107 for (i = 0; i < palette_size; i++) {
108 gray_palette[i] = 256 * i / palette_size;
109 }
110 }
111 }
112
113 row_pointers = png_get_rows(png_ptr, info_ptr);
114
115 /* Read the raw image data and convert in 8-bit grayscale */
116 for (x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
117 for (y = 0; y < png_get_image_height(png_ptr, info_ptr); y++) {
118 uint8_t *pixel = &map[y * png_get_image_width(png_ptr, info_ptr) + x];
119 uint x_offset = x * channels;
120
121 if (has_palette) {
122 *pixel = gray_palette[row_pointers[y][x_offset]];
123 } else if (channels == 3) {
124 *pixel = RGBToGrayscale(row_pointers[y][x_offset + 0],
125 row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]);
126 } else {
127 *pixel = row_pointers[y][x_offset];
128 }
129 }
130 }
131}
132
138static bool ReadHeightmapPNG(const char *filename, uint *x, uint *y, std::vector<uint8_t> *map)
139{
140 png_structp png_ptr = nullptr;
141 png_infop info_ptr = nullptr;
142
143 auto fp = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
144 if (!fp.has_value()) {
145 ShowErrorMessage(GetEncodedString(STR_ERROR_PNGMAP), GetEncodedString(STR_ERROR_PNGMAP_FILE_NOT_FOUND), WL_ERROR);
146 return false;
147 }
148
149 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
150 if (png_ptr == nullptr) {
151 ShowErrorMessage(GetEncodedString(STR_ERROR_PNGMAP), GetEncodedString(STR_ERROR_PNGMAP_MISC), WL_ERROR);
152 return false;
153 }
154
155 info_ptr = png_create_info_struct(png_ptr);
156 if (info_ptr == nullptr || setjmp(png_jmpbuf(png_ptr))) {
157 ShowErrorMessage(GetEncodedString(STR_ERROR_PNGMAP), GetEncodedString(STR_ERROR_PNGMAP_MISC), WL_ERROR);
158 png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
159 return false;
160 }
161
162 png_init_io(png_ptr, *fp);
163
164 /* Allocate memory and read image, without alpha or 16-bit samples
165 * (result is either 8-bit indexed/grayscale or 24-bit RGB) */
166 png_set_packing(png_ptr);
167 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_STRIP_16, nullptr);
168
169 /* Maps of wrong colour-depth are not used.
170 * (this should have been taken care of by stripping alpha and 16-bit samples on load) */
171 if ((png_get_channels(png_ptr, info_ptr) != 1) && (png_get_channels(png_ptr, info_ptr) != 3) && (png_get_bit_depth(png_ptr, info_ptr) != 8)) {
172 ShowErrorMessage(GetEncodedString(STR_ERROR_PNGMAP), GetEncodedString(STR_ERROR_PNGMAP_IMAGE_TYPE), WL_ERROR);
173 png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
174 return false;
175 }
176
177 uint width = png_get_image_width(png_ptr, info_ptr);
178 uint height = png_get_image_height(png_ptr, info_ptr);
179
180 if (!IsValidHeightmapDimension(width, height)) {
181 ShowErrorMessage(GetEncodedString(STR_ERROR_PNGMAP), GetEncodedString(STR_ERROR_HEIGHTMAP_TOO_LARGE), WL_ERROR);
182 png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
183 return false;
184 }
185
186 if (map != nullptr) {
187 map->resize(static_cast<size_t>(width) * height);
188 ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
189 }
190
191 *x = width;
192 *y = height;
193
194 png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
195 return true;
196}
197
198#endif /* WITH_PNG */
199
200
204static void ReadHeightmapBMPImageData(std::span<uint8_t> map, const BmpInfo &info, const BmpData &data)
205{
206 uint8_t gray_palette[256];
207
208 if (!data.palette.empty()) {
209 bool all_gray = true;
210
211 if (info.palette_size != 2) {
212 for (uint i = 0; i < info.palette_size && (info.palette_size != 16 || all_gray); i++) {
213 all_gray &= data.palette[i].r == data.palette[i].g && data.palette[i].r == data.palette[i].b;
214 gray_palette[i] = RGBToGrayscale(data.palette[i].r, data.palette[i].g, data.palette[i].b);
215 }
216
223 if (info.palette_size == 16 && !all_gray) {
224 for (uint i = 0; i < info.palette_size; i++) {
225 gray_palette[i] = 256 * i / info.palette_size;
226 }
227 }
228 } else {
233 gray_palette[0] = 0;
234 gray_palette[1] = 16;
235 }
236 }
237
238 /* Read the raw image data and convert in 8-bit grayscale */
239 for (uint y = 0; y < info.height; y++) {
240 uint8_t *pixel = &map[y * static_cast<size_t>(info.width)];
241 const uint8_t *bitmap = &data.bitmap[y * static_cast<size_t>(info.width) * (info.bpp == 24 ? 3 : 1)];
242
243 for (uint x = 0; x < info.width; x++) {
244 if (info.bpp != 24) {
245 *pixel++ = gray_palette[*bitmap++];
246 } else {
247 *pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2));
248 bitmap += 3;
249 }
250 }
251 }
252}
253
259static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, std::vector<uint8_t> *map)
260{
261 auto f = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
262 if (!f.has_value()) {
263 ShowErrorMessage(GetEncodedString(STR_ERROR_BMPMAP), GetEncodedString(STR_ERROR_PNGMAP_FILE_NOT_FOUND), WL_ERROR);
264 return false;
265 }
266
267 RandomAccessFile file(filename, HEIGHTMAP_DIR);
268 BmpInfo info{};
269 BmpData data{};
270
271 if (!BmpReadHeader(file, info, data)) {
272 ShowErrorMessage(GetEncodedString(STR_ERROR_BMPMAP), GetEncodedString(STR_ERROR_BMPMAP_IMAGE_TYPE), WL_ERROR);
273 return false;
274 }
275
276 if (!IsValidHeightmapDimension(info.width, info.height)) {
277 ShowErrorMessage(GetEncodedString(STR_ERROR_BMPMAP), GetEncodedString(STR_ERROR_HEIGHTMAP_TOO_LARGE), WL_ERROR);
278 return false;
279 }
280
281 if (map != nullptr) {
282 if (!BmpReadBitmap(file, info, data)) {
283 ShowErrorMessage(GetEncodedString(STR_ERROR_BMPMAP), GetEncodedString(STR_ERROR_BMPMAP_IMAGE_TYPE), WL_ERROR);
284 return false;
285 }
286
287 map->resize(static_cast<size_t>(info.width) * info.height);
288 ReadHeightmapBMPImageData(*map, info, data);
289 }
290
291 *x = info.width;
292 *y = info.height;
293
294 return true;
295}
296
304static void GrayscaleToMapHeights(uint img_width, uint img_height, std::span<const uint8_t> map)
305{
306 /* Defines the detail of the aspect ratio (to avoid doubles) */
307 const uint num_div = 16384;
308 /* Ensure multiplication with num_div does not cause overflows. */
309 static_assert(num_div <= std::numeric_limits<uint>::max() / MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS);
310
311 uint width, height;
312 uint row, col;
313 uint row_pad = 0, col_pad = 0;
314 uint img_scale;
315 uint img_row, img_col;
316 TileIndex tile;
317
318 /* Get map size and calculate scale and padding values */
320 default: NOT_REACHED();
322 width = Map::SizeX();
323 height = Map::SizeY();
324 break;
325 case HM_CLOCKWISE:
326 width = Map::SizeY();
327 height = Map::SizeX();
328 break;
329 }
330
331 if ((img_width * num_div) / img_height > ((width * num_div) / height)) {
332 /* Image is wider than map - center vertically */
333 img_scale = (width * num_div) / img_width;
334 row_pad = (1 + height - ((img_height * img_scale) / num_div)) / 2;
335 } else {
336 /* Image is taller than map - center horizontally */
337 img_scale = (height * num_div) / img_height;
338 col_pad = (1 + width - ((img_width * img_scale) / num_div)) / 2;
339 }
340
342 for (uint x = 0; x < Map::SizeX(); x++) MakeVoid(TileXY(x, 0));
343 for (uint y = 0; y < Map::SizeY(); y++) MakeVoid(TileXY(0, y));
344 }
345
346 /* Form the landscape */
347 for (row = 0; row < height; row++) {
348 for (col = 0; col < width; col++) {
350 default: NOT_REACHED();
351 case HM_COUNTER_CLOCKWISE: tile = TileXY(col, row); break;
352 case HM_CLOCKWISE: tile = TileXY(row, col); break;
353 }
354
355 /* Check if current tile is within the 1-pixel map edge or padding regions */
357 (row < row_pad) || (row >= (height - row_pad - (_settings_game.construction.freeform_edges ? 0 : 1))) ||
358 (col < col_pad) || (col >= (width - col_pad - (_settings_game.construction.freeform_edges ? 0 : 1)))) {
359 SetTileHeight(tile, 0);
360 } else {
361 /* Use nearest neighbour resizing to scale map data.
362 * We rotate the map 45 degrees (counter)clockwise */
363 img_row = (((row - row_pad) * num_div) / img_scale);
365 default: NOT_REACHED();
367 img_col = (((width - 1 - col - col_pad) * num_div) / img_scale);
368 break;
369 case HM_CLOCKWISE:
370 img_col = (((col - col_pad) * num_div) / img_scale);
371 break;
372 }
373
374 assert(img_row < img_height);
375 assert(img_col < img_width);
376
377 uint heightmap_height = map[img_row * img_width + img_col];
378
379 if (heightmap_height > 0) {
380 /* 0 is sea level.
381 * Other grey scales are scaled evenly to the available height levels > 0.
382 * (The coastline is independent from the number of height levels) */
383 heightmap_height = 1 + (heightmap_height - 1) * _settings_game.game_creation.heightmap_height / 255;
384 }
385
386 SetTileHeight(tile, heightmap_height);
387 }
388 /* Only clear the tiles within the map area. */
389 if (IsInnerTile(tile)) {
390 MakeClear(tile, CLEAR_GRASS, 3);
391 }
392 }
393 }
394}
395
401{
402 uint width, height;
403 int row, col;
404 uint8_t current_height;
405 uint8_t max_height = _settings_game.construction.map_height_limit;
406
407 /* Adjust height difference to maximum one horizontal/vertical change. */
408 width = Map::SizeX();
409 height = Map::SizeY();
410
411 /* Top and left edge */
412 for (row = 0; (uint)row < height; row++) {
413 for (col = 0; (uint)col < width; col++) {
414 current_height = MAX_TILE_HEIGHT;
415 if (col != 0) {
416 /* Find lowest tile; either the top or left one */
417 current_height = TileHeight(TileXY(col - 1, row)); // top edge
418 }
419 if (row != 0) {
420 if (TileHeight(TileXY(col, row - 1)) < current_height) {
421 current_height = TileHeight(TileXY(col, row - 1)); // left edge
422 }
423 }
424
425 /* Does the height differ more than one? */
426 TileIndex tile = TileXY(col, row);
427 if (TileHeight(tile) >= (uint)current_height + 2) {
428 /* Then change the height to be no more than one */
429 SetTileHeight(tile, current_height + 1);
430 /* Height was changed so now there's a chance, more likely at higher altitude, of the
431 * tile turning into rock. */
432 if (IsInnerTile(tile) && RandomRange(max_height) <= current_height) {
433 MakeClear(tile, CLEAR_ROCKS, 3);
434 }
435 }
436 }
437 }
438
439 /* Bottom and right edge */
440 for (row = height - 1; row >= 0; row--) {
441 for (col = width - 1; col >= 0; col--) {
442 current_height = MAX_TILE_HEIGHT;
443 if ((uint)col != width - 1) {
444 /* Find lowest tile; either the bottom and right one */
445 current_height = TileHeight(TileXY(col + 1, row)); // bottom edge
446 }
447
448 if ((uint)row != height - 1) {
449 if (TileHeight(TileXY(col, row + 1)) < current_height) {
450 current_height = TileHeight(TileXY(col, row + 1)); // right edge
451 }
452 }
453
454 /* Does the height differ more than one? */
455 TileIndex tile = TileXY(col, row);
456 if (TileHeight(tile) >= (uint)current_height + 2) {
457 /* Then change the height to be no more than one */
458 SetTileHeight(tile, current_height + 1);
459 /* Height was changed so now there's a chance, more likely at higher altitude, of the
460 * tile turning into rock. */
461 if (IsInnerTile(tile) && RandomRange(max_height) <= current_height) {
462 MakeClear(tile, CLEAR_ROCKS, 3);
463 }
464 }
465 }
466 }
467}
468
478static bool ReadHeightMap(DetailedFileType dft, const char *filename, uint *x, uint *y, std::vector<uint8_t> *map)
479{
480 switch (dft) {
481 default:
482 NOT_REACHED();
483
484#ifdef WITH_PNG
486 return ReadHeightmapPNG(filename, x, y, map);
487#endif /* WITH_PNG */
488
490 return ReadHeightmapBMP(filename, x, y, map);
491 }
492}
493
502bool GetHeightmapDimensions(DetailedFileType dft, const char *filename, uint *x, uint *y)
503{
504 return ReadHeightMap(dft, filename, x, y, nullptr);
505}
506
514bool LoadHeightmap(DetailedFileType dft, const char *filename)
515{
516 uint x, y;
517 std::vector<uint8_t> map;
518
519 if (!ReadHeightMap(dft, filename, &x, &y, &map)) {
520 return false;
521 }
522
523 GrayscaleToMapHeights(x, y, map);
524
525 FixSlopes();
527
528 return true;
529}
530
535void FlatEmptyWorld(uint8_t tile_height)
536{
537 int edge_distance = _settings_game.construction.freeform_edges ? 0 : 2;
538 for (uint row = edge_distance; row < Map::SizeY() - edge_distance; row++) {
539 for (uint col = edge_distance; col < Map::SizeX() - edge_distance; col++) {
540 SetTileHeight(TileXY(col, row), tile_height);
541 }
542 }
543
544 FixSlopes();
546}
Read and write support for bmps.
A file from which bytes, words and double words are read in (potentially) a random order.
Map accessors for 'clear' tiles.
@ CLEAR_GRASS
0-3
Definition clear_map.h:20
@ CLEAR_ROCKS
3
Definition clear_map.h:22
void MakeClear(Tile t, ClearGround g, uint density)
Make a clear tile.
Definition clear_map.h:259
Functions related to errors.
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition error.h:26
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, const CommandCost &cc)
Display an error message in a window.
std::optional< FileHandle > FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition fileio.cpp:243
Functions for Standard In/Out file operations.
DetailedFileType
Kinds of files in each AbstractFileType.
Definition fileio_type.h:29
@ DFT_HEIGHTMAP_BMP
BMP file.
Definition fileio_type.h:35
@ DFT_HEIGHTMAP_PNG
PNG file.
Definition fileio_type.h:36
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Declarations for savegames operations.
Functions related to the gfx engine.
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1529
static const uint MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS
Maximum number of pixels for one dimension of a heightmap image.
Definition heightmap.cpp:31
static void GrayscaleToMapHeights(uint img_width, uint img_height, std::span< const uint8_t > map)
Converts a given grayscale map to something that fits in OTTD map system and create a map of that dat...
bool LoadHeightmap(DetailedFileType dft, const char *filename)
Load a heightmap from file and change the map in its current dimensions to a landscape representing t...
static uint8_t RGBToGrayscale(uint8_t red, uint8_t green, uint8_t blue)
Convert RGB colours to Grayscale using 29.9% Red, 58.7% Green, 11.4% Blue (average luminosity formula...
Definition heightmap.cpp:64
bool GetHeightmapDimensions(DetailedFileType dft, const char *filename, uint *x, uint *y)
Get the dimensions of a heightmap.
void FlatEmptyWorld(uint8_t tile_height)
Make an empty world where all tiles are of height 'tile_height'.
static bool ReadHeightmapPNG(const char *filename, uint *x, uint *y, std::vector< uint8_t > *map)
Reads the heightmap and/or size of the heightmap from a PNG file.
static void ReadHeightmapPNGImageData(std::span< uint8_t > map, png_structp png_ptr, png_infop info_ptr)
The PNG Heightmap loader.
Definition heightmap.cpp:79
static bool IsValidHeightmapDimension(size_t width, size_t height)
Check whether the loaded dimension of the heightmap image are considered valid enough to attempt to l...
Definition heightmap.cpp:53
void FixSlopes()
This function takes care of the fact that land in OpenTTD can never differ more than 1 in height.
static void ReadHeightmapBMPImageData(std::span< uint8_t > map, const BmpInfo &info, const BmpData &data)
The BMP Heightmap loader.
static bool ReadHeightMap(DetailedFileType dft, const char *filename, uint *x, uint *y, std::vector< uint8_t > *map)
Reads the heightmap with the correct file reader.
static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, std::vector< uint8_t > *map)
Reads the heightmap and/or size of the heightmap from a BMP file.
Functions related to creating heightmaps from files.
@ HM_CLOCKWISE
Rotate the map clockwise 45 degrees.
Definition heightmap.h:21
@ HM_COUNTER_CLOCKWISE
Rotate the map counter clockwise 45 degrees.
Definition heightmap.h:20
uint DistanceFromEdge(TileIndex tile)
Param the minimum distance to an edge.
Definition map.cpp:203
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:372
static const uint MAX_MAP_SIZE
Maximal map size = 4096.
Definition map_type.h:40
uint32_t RandomRange(uint32_t limit, const std::source_location location=std::source_location::current())
Pick a random number between 0 and limit - 1, inclusive.
A number of safeguards to prevent using unsafe methods.
Functions/types related to saving and loading games.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:58
Definition of base types and functions in a cross-platform compatible way.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:103
Functions related to OTTD's strings.
Definition bmp.h:26
Definition bmp.h:16
uint32_t height
bitmap height
Definition bmp.h:19
uint32_t width
bitmap width
Definition bmp.h:18
uint32_t palette_size
number of colours in palette
Definition bmp.h:23
uint16_t bpp
bits per pixel
Definition bmp.h:21
bool freeform_edges
allow terraforming the tiles at the map edges
uint8_t map_height_limit
the maximum allowed heightlevel
uint8_t heightmap_rotation
rotation director for the heightmap
uint8_t heightmap_height
highest mountain for heightmap (towards what it scales)
ConstructionSettings construction
construction of things in-game
GameCreationSettings game_creation
settings used during the creation of a game (map)
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:278
static debug_inline uint SizeX()
Get the size of the map along the X.
Definition map_func.h:269
bool IsInnerTile(Tile tile)
Check if a tile is within the map (not a border)
Definition tile_map.h:109
void SetTileHeight(Tile tile, uint height)
Sets the height of a tile.
Definition tile_map.h:57
static debug_inline uint TileHeight(Tile tile)
Returns the height of a tile.
Definition tile_map.h:29
static const uint MAX_TILE_HEIGHT
Maximum allowed tile height.
Definition tile_type.h:24
Map accessors for void tiles.
void MakeVoid(Tile t)
Make a nice void tile ;)
Definition void_map.h:19