22#include "table/strings.h"
72static inline uint8_t
RGBToGreyscale(uint8_t red, uint8_t green, uint8_t blue)
76 return ((red * 19595) + (green * 38470) + (blue * 7471)) / 65536;
91 uint8_t gray_palette[256];
92 png_bytep *row_pointers =
nullptr;
93 bool has_palette = png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE;
94 uint channels = png_get_channels(png_ptr, info_ptr);
101 bool all_gray =
true;
103 png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size);
104 for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) {
105 all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue;
106 gray_palette[i] =
RGBToGreyscale(palette[i].red, palette[i].green, palette[i].blue);
115 if (palette_size == 16 && !all_gray) {
116 for (i = 0; i < palette_size; i++) {
117 gray_palette[i] = 256 * i / palette_size;
122 row_pointers = png_get_rows(png_ptr, info_ptr);
125 for (x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
126 for (y = 0; y < png_get_image_height(png_ptr, info_ptr); y++) {
127 uint8_t *pixel = &map[y * png_get_image_width(png_ptr, info_ptr) + x];
128 uint x_offset = x * channels;
131 *pixel = gray_palette[row_pointers[y][x_offset]];
132 }
else if (channels == 3) {
134 row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]);
136 *pixel = row_pointers[y][x_offset];
152static bool ReadHeightmapPNG(std::string_view filename, uint *x, uint *y, std::vector<uint8_t> *map)
154 png_structp png_ptr =
nullptr;
155 png_infop info_ptr =
nullptr;
158 if (!fp.has_value()) {
163 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
164 if (png_ptr ==
nullptr) {
169 info_ptr = png_create_info_struct(png_ptr);
170 if (info_ptr ==
nullptr || setjmp(png_jmpbuf(png_ptr))) {
172 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
176 png_init_io(png_ptr, *fp);
180 png_set_packing(png_ptr);
181 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_STRIP_16,
nullptr);
185 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)) {
187 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
191 uint width = png_get_image_width(png_ptr, info_ptr);
192 uint height = png_get_image_height(png_ptr, info_ptr);
196 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
200 if (map !=
nullptr) {
201 map->resize(
static_cast<size_t>(width) * height);
208 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
223 uint8_t gray_palette[256];
225 if (!data.palette.empty()) {
226 bool all_gray =
true;
230 all_gray &= data.palette[i].r == data.palette[i].g && data.palette[i].r == data.palette[i].b;
231 gray_palette[i] =
RGBToGreyscale(data.palette[i].r, data.palette[i].g, data.palette[i].b);
251 gray_palette[1] = 16;
256 for (uint y = 0; y < info.
height; y++) {
257 uint8_t *pixel = &map[y *
static_cast<size_t>(info.
width)];
258 const uint8_t *bitmap = &data.bitmap[y *
static_cast<size_t>(info.
width) * (info.
bpp == 24 ? 3 : 1)];
260 for (uint x = 0; x < info.
width; x++) {
261 if (info.
bpp != 24) {
262 *pixel++ = gray_palette[*bitmap++];
281static bool ReadHeightmapBMP(std::string_view filename, uint *x, uint *y, std::vector<uint8_t> *map)
284 if (!f.has_value()) {
303 if (map !=
nullptr) {
309 map->resize(
static_cast<size_t>(info.
width) * info.
height);
329 const uint num_div = 16384;
335 uint row_pad = 0, col_pad = 0;
337 uint img_row, img_col;
342 default: NOT_REACHED();
353 if ((img_width * num_div) / img_height > ((width * num_div) / height)) {
355 img_scale = (width * num_div) / img_width;
356 row_pad = (1 + height - ((img_height * img_scale) / num_div)) / 2;
359 img_scale = (height * num_div) / img_height;
360 col_pad = (1 + width - ((img_width * img_scale) / num_div)) / 2;
369 for (row = 0; row < height; row++) {
370 for (col = 0; col < width; col++) {
372 default: NOT_REACHED();
379 (row < row_pad) || (row >= (height - row_pad - (
_settings_game.construction.freeform_edges ? 0 : 1))) ||
380 (col < col_pad) || (col >= (width - col_pad - (
_settings_game.construction.freeform_edges ? 0 : 1)))) {
385 img_row = (((row - row_pad) * num_div) / img_scale);
387 default: NOT_REACHED();
389 img_col = (((width - 1 - col - col_pad) * num_div) / img_scale);
392 img_col = (((col - col_pad) * num_div) / img_scale);
396 assert(img_row < img_height);
397 assert(img_col < img_width);
399 uint heightmap_height = map[img_row * img_width + img_col];
401 if (heightmap_height > 0) {
405 heightmap_height = 1 + (heightmap_height - 1) *
_settings_game.game_creation.heightmap_height / 255;
426 uint8_t current_height;
427 uint8_t max_height =
_settings_game.construction.map_height_limit;
434 for (row = 0; (uint)row < height; row++) {
435 for (col = 0; (uint)col < width; col++) {
449 if (
TileHeight(tile) >= (uint)current_height + 2) {
462 for (row = height - 1; row >= 0; row--) {
463 for (col = width - 1; col >= 0; col--) {
465 if ((uint)col != width - 1) {
470 if ((uint)row != height - 1) {
478 if (
TileHeight(tile) >= (uint)current_height + 2) {
540 std::vector<uint8_t> map;
564 int edge_distance =
_settings_game.construction.freeform_edges ? 0 : 2;
565 for (uint row = edge_distance; row <
Map::SizeY() - edge_distance; row++) {
566 for (uint col = edge_distance; col <
Map::SizeX() - edge_distance; col++) {
bool BmpReadBitmap(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads the bitmap 1 bpp and 4 bpp bitmaps are converted to 8 bpp bitmaps.
bool BmpReadHeader(RandomAccessFile &file, BmpInfo &info, BmpData &data)
Reads bitmap headers, and palette (if any).
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.
@ Rocks
Rocks with snow transition (0-3).
@ Grass
Plain grass with dirt transition (0-3).
void MakeClear(Tile t, ClearGround g, uint density)
Make a clear tile.
Functions related to errors.
@ Error
Errors (eg. saving/loading failed).
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Functions for standard in/out file operations.
DetailedFileType
Kinds of files in each AbstractFileType.
@ Heightmap
Subdirectory of scenario for heightmaps.
Functions related to the gfx engine.
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
static const uint MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS
Maximum number of pixels for one dimension of a heightmap image.
static bool ReadHeightmapBMP(std::string_view filename, uint *x, uint *y, std::vector< uint8_t > *map)
Reads the heightmap and/or size of the heightmap from a BMP file.
bool GetHeightmapDimensions(DetailedFileType dft, std::string_view filename, uint *x, uint *y)
Get the dimensions of a heightmap.
static bool ReadHeightmapPNG(std::string_view filename, uint *x, uint *y, std::vector< uint8_t > *map)
Reads the heightmap and/or size of the heightmap from a PNG file.
void FlatEmptyWorld(uint8_t tile_height)
Make an empty world where all tiles are of height 'tile_height'.
static void GreyscaleToMapHeights(uint img_width, uint img_height, std::span< const uint8_t > map)
Converts a given greyscale map to something that fits in OTTD map system and create a map of that dat...
static const uint MAX_HEIGHTMAP_SIZE_PIXELS
Maximum size in pixels of the heightmap image.
static void ReadHeightmapPNGImageData(std::span< uint8_t > map, png_structp png_ptr, png_infop info_ptr)
The PNG Heightmap loader.
static bool ReadHeightMap(DetailedFileType dft, std::string_view filename, uint *x, uint *y, std::vector< uint8_t > *map)
Reads the heightmap with the correct file reader.
bool LoadHeightmap(DetailedFileType dft, std::string_view filename)
Load a heightmap from file and change the map in its current dimensions to a landscape representing t...
static uint8_t RGBToGreyscale(uint8_t red, uint8_t green, uint8_t blue)
Convert RGB colours to Greyscale using 29.9% Red, 58.7% Green, 11.4% Blue (average luminosity formula...
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...
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.
Functions related to creating heightmaps from files.
@ HM_CLOCKWISE
Rotate the map clockwise 45 degrees.
@ HM_COUNTER_CLOCKWISE
Rotate the map counter clockwise 45 degrees.
bool IsMapSurroundedByWater()
Check if all tiles on the map edge should be considered water borders.
Functions related to OTTD's landscape.
uint DistanceFromEdge(TileIndex tile)
Param the minimum distance to an edge.
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
static const uint MAX_MAP_SIZE
Maximal map size = 4096.
Pseudo random number generator.
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.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
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.
Functions related to OTTD's strings.
uint32_t height
bitmap height
uint32_t width
bitmap width
uint32_t palette_size
number of colours in palette
uint16_t bpp
bits per pixel
static uint SizeX()
Get the size of the map along the X.
static uint SizeY()
Get the size of the map along the Y.
static uint TileHeight(Tile tile)
Returns the height of a tile.
bool IsInnerTile(Tile tile)
Check if a tile is within the map (not a border).
void SetTileHeight(Tile tile, uint height)
Sets the height of a tile.
static constexpr uint MAX_TILE_HEIGHT
Maximum allowed tile height.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Map accessors for void tiles.
void MakeVoid(Tile t)
Make a nice void tile ;).