+/** + * Gets the maximum allowed height while generating a map based on + * mapsize, terraintype, and the maximum height level. + * @return The maximum height for the map generation. + */ +static height_t TGPGetMaxHeight() +{ + /** + * Desired maximum height - indexed by: + * - _settings_game.difficulty.terrain_type + * - min(MapLogX(), MapLogY()) - MIN_MAP_SIZE_BITS + * + * It is indexed by map size as well as terrain type since the map size limits the height of + * a usable mountain. For example, on a 64x64 map a 24 high single peak mountain (as if you + * raised land 24 times in the center of the map) will leave only a ring of about 10 tiles + * around the mountain to build on. On a 4096x4096 map it will just be a small annoyance that + * you can easily build around; it's not really a mountain. + */ + static const int max_height_divisor[4][MAX_MAP_SIZE_BITS - MIN_MAP_SIZE_BITS + 1] = { + {21, 40, 50, 64, 128, 380, 760 }, ///< Very flat + {16, 27, 38, 51, 102, 204, 408 }, ///< Flat + {10, 13, 17, 20, 33, 66, 132 }, ///< Hilly + {9, 10, 11, 12, 13, 24, 48 }, ///< Mountainous + }; + + int divisor = max_height_divisor[_settings_game.difficulty.terrain_type][min(MapLogX(), MapLogY()) - MIN_MAP_SIZE_BITS]; + int map_size_max_height = min(_height_map.size_x + 1, _height_map.size_y + 1) / divisor; + return I2H(min(map_size_max_height, _settings_game.construction.max_heightlevel)); +}