diff --git a/src/landscape.cpp b/src/landscape.cpp index 9f92912c91..f3dcebccf6 100644 --- a/src/landscape.cpp +++ b/src/landscape.cpp @@ -93,7 +93,7 @@ static const uint TILE_UPDATE_FREQUENCY = 1 << TILE_UPDATE_FREQUENCY_LOG; ///< * @ingroup SnowLineGroup * @see GetSnowLine() GameCreationSettings */ -static SnowLine *_snow_line = nullptr; +static std::unique_ptr _snow_line; /** * Map 2D viewport or smallmap coordinate to 3D world or tile coordinate. @@ -584,21 +584,12 @@ bool IsSnowLineSet() /** * Set a variable snow line, as loaded from a newgrf file. - * @param table the 12 * 32 byte table containing the snowline for each day + * @param snow_line The new snow line configuration. * @ingroup SnowLineGroup */ -void SetSnowLine(uint8_t table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]) +void SetSnowLine(std::unique_ptr &&snow_line) { - _snow_line = CallocT(1); - _snow_line->lowest_value = 0xFF; - memcpy(_snow_line->table, table, sizeof(_snow_line->table)); - - for (uint i = 0; i < SNOW_LINE_MONTHS; i++) { - for (uint j = 0; j < SNOW_LINE_DAYS; j++) { - _snow_line->highest_value = std::max(_snow_line->highest_value, table[i][j]); - _snow_line->lowest_value = std::min(_snow_line->lowest_value, table[i][j]); - } - } + _snow_line = std::move(snow_line); } /** @@ -640,7 +631,6 @@ uint8_t LowestSnowLine() */ void ClearSnowLine() { - free(_snow_line); _snow_line = nullptr; } diff --git a/src/landscape.h b/src/landscape.h index 98221c5019..db2a9781e4 100644 --- a/src/landscape.h +++ b/src/landscape.h @@ -22,12 +22,12 @@ static const uint SNOW_LINE_DAYS = 32; ///< Number of days in each month in th */ struct SnowLine { uint8_t table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]; ///< Height of the snow line each day of the year - uint8_t highest_value; ///< Highest snow line of the year - uint8_t lowest_value; ///< Lowest snow line of the year + uint8_t highest_value = 0; ///< Highest snow line of the year + uint8_t lowest_value = UINT8_MAX; ///< Lowest snow line of the year }; bool IsSnowLineSet(); -void SetSnowLine(uint8_t table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]); +void SetSnowLine(std::unique_ptr &&snow_line); uint8_t GetSnowLine(); uint8_t HighestSnowLine(); uint8_t LowestSnowLine(); diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 3e76a943bb..7d69676b19 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -2873,24 +2873,28 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By } else if (buf.Remaining() < SNOW_LINE_MONTHS * SNOW_LINE_DAYS) { GrfMsg(1, "GlobalVarChangeInfo: Not enough entries set in the snowline table ({})", buf.Remaining()); } else { - uint8_t table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]; + auto snow_line = std::make_unique(); for (uint i = 0; i < SNOW_LINE_MONTHS; i++) { for (uint j = 0; j < SNOW_LINE_DAYS; j++) { - table[i][j] = buf.ReadByte(); + uint8_t &level = snow_line->table[i][j]; + level = buf.ReadByte(); if (_cur.grffile->grf_version >= 8) { - if (table[i][j] != 0xFF) table[i][j] = table[i][j] * (1 + _settings_game.construction.map_height_limit) / 256; + if (level != 0xFF) level = level * (1 + _settings_game.construction.map_height_limit) / 256; } else { - if (table[i][j] >= 128) { + if (level >= 128) { /* no snow */ - table[i][j] = 0xFF; + level = 0xFF; } else { - table[i][j] = table[i][j] * (1 + _settings_game.construction.map_height_limit) / 128; + level = level * (1 + _settings_game.construction.map_height_limit) / 128; } } + + snow_line->highest_value = std::max(snow_line->highest_value, level); + snow_line->lowest_value = std::min(snow_line->lowest_value, level); } } - SetSnowLine(table); + SetSnowLine(std::move(snow_line)); } break;