diff --git a/src/map.cpp b/src/map.cpp index 392ab8ab6c..a8023c9226 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -32,17 +32,14 @@ uint _map_tile_mask; ///< _map_size - 1 (to mask the mapsize) Tile *_m = NULL; ///< Tiles of the map TileExtended *_me = NULL; ///< Extended Tiles of the map - /** - * (Re)allocates a map with the given dimension + * Validates whether a map with the given dimension is valid * @param size_x the width of the map along the NE/SW edge * @param size_y the 'height' of the map along the SE/NW edge + * @return true if valid, or false if not valid */ -void AllocateMap(uint size_x, uint size_y) +bool ValidateMapSize(uint size_x, uint size_y) { - DEBUG(map, 2, "Min/max map size %d/%d, max map tiles %d", MIN_MAP_SIZE, MAX_MAP_SIZE, MAX_MAP_TILES); - DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y); - /* Make sure that the map size is within the limits and that * size of both axes is a power of 2. */ if (size_x * size_y > MAX_MAP_TILES || @@ -50,6 +47,22 @@ void AllocateMap(uint size_x, uint size_y) size_y < MIN_MAP_SIZE || (size_x & (size_x - 1)) != 0 || (size_y & (size_y - 1)) != 0) { + return false; + } + return true; +} + +/** + * (Re)allocates a map with the given dimension + * @param size_x the width of the map along the NE/SW edge + * @param size_y the 'height' of the map along the SE/NW edge + */ +void AllocateMap(uint size_x, uint size_y) +{ + DEBUG(map, 2, "Min/max map size %d/%d, max map tiles %d", MIN_MAP_SIZE, MAX_MAP_SIZE, MAX_MAP_TILES); + DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y); + + if (!ValidateMapSize(size_x, size_y)) { error("Invalid map size"); } diff --git a/src/map_func.h b/src/map_func.h index 9198c2cd1f..b390f0eaef 100644 --- a/src/map_func.h +++ b/src/map_func.h @@ -43,6 +43,7 @@ extern Tile *_m; */ extern TileExtended *_me; +bool ValidateMapSize(uint size_x, uint size_y); void AllocateMap(uint size_x, uint size_y); /** diff --git a/src/saveload/extended_ver_sl.cpp b/src/saveload/extended_ver_sl.cpp index 3f6b693f56..40aa560ce0 100644 --- a/src/saveload/extended_ver_sl.cpp +++ b/src/saveload/extended_ver_sl.cpp @@ -33,6 +33,7 @@ #include "saveload.h" #include "extended_ver_sl.h" #include "../timetable.h" +#include "../map_func.h" #include @@ -62,6 +63,7 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = { { XSLFI_VARIABLE_DAY_LENGTH, XSCF_NULL, 1, 1, "variable_day_length", NULL, NULL, NULL }, { XSLFI_ORDER_OCCUPANCY, XSCF_NULL, 1, 1, "order_occupancy", NULL, NULL, NULL }, { XSLFI_MORE_COND_ORDERS, XSCF_NULL, 1, 1, "more_cond_orders", NULL, NULL, NULL }, + { XSLFI_EXTRA_LARGE_MAP, XSCF_NULL, 0, 1, "extra_large_map", NULL, NULL, NULL }, { XSLFI_NULL, XSCF_NULL, 0, 0, NULL, NULL, NULL, NULL },// This is the end marker }; @@ -125,6 +127,9 @@ void SlXvSetCurrentState() for (; info->index != XSLFI_NULL; ++info) { _sl_xv_feature_versions[info->index] = info->save_version; } + if (MapSizeX() > 8192 || MapSizeY() > 8192) { + _sl_xv_feature_versions[XSLFI_EXTRA_LARGE_MAP] = 1; + } } /** @@ -220,7 +225,6 @@ static void Save_SLXI() SlXvSetCurrentState(); static const SaveLoad _xlsi_sub_chunk_desc[] = { - SLE_VAR(SlxiSubChunkInfo, save_version, SLE_UINT16), SLE_STR(SlxiSubChunkInfo, name, SLE_STR, 0), SLE_END() }; @@ -234,9 +238,9 @@ static void Save_SLXI() chunk_counts.resize(XSLFI_SIZE); const SlxiSubChunkInfo *info = _sl_xv_sub_chunk_infos; for (; info->index != XSLFI_NULL; ++info) { - if (info->save_version > 0) { + if (_sl_xv_feature_versions[info->index] > 0) { item_count++; - length += 4; + length += 6; length += SlCalcObjLength(info, _xlsi_sub_chunk_desc); if (info->save_proc) { uint32 extra_data_length = info->save_proc(info, true); @@ -264,7 +268,8 @@ static void Save_SLXI() // write data info = _sl_xv_sub_chunk_infos; for (; info->index != XSLFI_NULL; ++info) { - if (info->save_version > 0) { + uint16 save_version = _sl_xv_feature_versions[info->index]; + if (save_version > 0) { SlxiSubChunkFlags flags = info->flags; assert(!(flags & (XSCF_EXTRA_DATA_PRESENT | XSCF_CHUNK_ID_LIST_PRESENT))); uint32 extra_data_length = extra_data_lengths[info->index]; @@ -272,6 +277,7 @@ static void Save_SLXI() if (extra_data_length > 0) flags |= XSCF_EXTRA_DATA_PRESENT; if (chunk_count > 0) flags |= XSCF_CHUNK_ID_LIST_PRESENT; SlWriteUint32(flags); + SlWriteUint16(save_version); SlObject(const_cast(info), _xlsi_sub_chunk_desc); if (extra_data_length > 0) { diff --git a/src/saveload/extended_ver_sl.h b/src/saveload/extended_ver_sl.h index 7f010b59ec..86469e4a29 100644 --- a/src/saveload/extended_ver_sl.h +++ b/src/saveload/extended_ver_sl.h @@ -44,6 +44,7 @@ enum SlXvFeatureIndex { XSLFI_TRAFFIC_LIGHTS, ///< This save game uses road traffic lights XSLFI_RAIL_AGEING, ///< This save game uses the rail aging patch XSLFI_SPRINGPP, ///< This is a SpringPP game, use this for loading some settings + XSLFI_EXTRA_LARGE_MAP, ///< Extra large map XSLFI_SIZE, ///< Total count of features, including null feature }; diff --git a/src/saveload/map_sl.cpp b/src/saveload/map_sl.cpp index 86a185ca42..ee157ec17c 100644 --- a/src/saveload/map_sl.cpp +++ b/src/saveload/map_sl.cpp @@ -37,6 +37,9 @@ static void Save_MAPS() static void Load_MAPS() { SlGlobList(_map_dimensions); + if (!ValidateMapSize(_map_dim_x, _map_dim_y)) { + SlErrorCorruptFmt("Invalid map size: %u x %u", _map_dim_x, _map_dim_y); + } AllocateMap(_map_dim_x, _map_dim_y); }