1
0
Fork 0

Codechange: use unique_ptr over CallocT and avoid copying table

pull/12878/head
Rubidium 2025-01-13 20:34:24 +01:00 committed by rubidium42
parent 41c9f2d82c
commit 3541ba4d0c
3 changed files with 18 additions and 24 deletions

View File

@ -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<SnowLine> _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<SnowLine> &&snow_line)
{
_snow_line = CallocT<SnowLine>(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;
}

View File

@ -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<SnowLine> &&snow_line);
uint8_t GetSnowLine();
uint8_t HighestSnowLine();
uint8_t LowestSnowLine();

View File

@ -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<SnowLine>();
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;