Cache current value of snow line height

This commit is contained in:
Jonathan G Rennison
2021-02-21 22:02:58 +00:00
parent f595696e97
commit 2e20da40ed
6 changed files with 29 additions and 2 deletions

View File

@@ -21,6 +21,7 @@
#include "newgrf_profiling.h"
#include "console_func.h"
#include "debug.h"
#include "landscape.h"
#include "widgets/statusbar_widget.h"
#include "safeguards.h"
@@ -54,6 +55,7 @@ void SetDate(Date date, DateFract fract)
ConvertDateToYMD(date, &ymd);
_cur_date_ymd = ymd;
SetScaledTickVariables();
UpdateCachedSnowLine();
}
void SetScaledTickVariables()
@@ -322,6 +324,8 @@ void IncreaseDate()
/* update internal variables before calling the daily/monthly/yearly loops */
_cur_date_ymd = ymd;
UpdateCachedSnowLine();
/* yes, call various daily loops */
OnNewDay();

View File

@@ -92,6 +92,8 @@ extern const byte _slope_to_sprite_offset[32] = {
*/
static SnowLine *_snow_line = nullptr;
byte _cached_snowline = 0;
/**
* Map 2D viewport or smallmap coordinate to 3D world or tile coordinate.
* Function takes into account height of tiles and foundations.
@@ -643,6 +645,8 @@ void SetSnowLine(byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS])
_snow_line->lowest_value = std::min(_snow_line->lowest_value, table[i][j]);
}
}
UpdateCachedSnowLine();
}
/**
@@ -650,13 +654,18 @@ void SetSnowLine(byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS])
* @return the snow line height.
* @ingroup SnowLineGroup
*/
byte GetSnowLine()
byte GetSnowLineUncached()
{
if (_snow_line == nullptr) return _settings_game.game_creation.snow_line_height;
return _snow_line->table[_cur_date_ymd.month][_cur_date_ymd.day];
}
void UpdateCachedSnowLine()
{
_cached_snowline = GetSnowLineUncached();
}
/**
* Get the highest possible snow line height, either variable or static.
* @return the highest snow line height.
@@ -685,6 +694,7 @@ void ClearSnowLine()
{
free(_snow_line);
_snow_line = nullptr;
UpdateCachedSnowLine();
}
/**

View File

@@ -28,11 +28,18 @@ struct SnowLine {
bool IsSnowLineSet();
void SetSnowLine(byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]);
byte GetSnowLine();
byte GetSnowLineUncached();
void UpdateCachedSnowLine();
byte HighestSnowLine();
byte LowestSnowLine();
void ClearSnowLine();
inline byte GetSnowLine()
{
extern byte _cached_snowline;
return _cached_snowline;
}
int GetSlopeZInCorner(Slope tileh, Corner corner);
Slope GetFoundationSlope(TileIndex tile, int *z = nullptr);

View File

@@ -100,6 +100,7 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settin
} else {
SetScaledTickVariables();
}
UpdateCachedSnowLine();
LinkGraphSchedule::Clear();
ClearTraceRestrictMapping();

View File

@@ -10387,6 +10387,7 @@ void LoadNewGRF(uint load_index, uint file_index, uint num_baseset)
_tick_counter = 0;
_tick_skip_counter = 0;
_display_opt = 0;
UpdateCachedSnowLine();
SetScaledTickVariables();
}
@@ -10483,6 +10484,7 @@ void LoadNewGRF(uint load_index, uint file_index, uint num_baseset)
_tick_counter = tick_counter;
_tick_skip_counter = tick_skip_counter;
_display_opt = display_opt;
UpdateCachedSnowLine();
SetScaledTickVariables();
}

View File

@@ -627,6 +627,7 @@ bool AfterLoadGame()
RebuildTownKdtree();
RebuildStationKdtree();
UpdateCachedSnowLine();
_viewport_sign_kdtree_valid = false;
@@ -1671,6 +1672,7 @@ bool AfterLoadGame()
_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
SetScaledTickVariables();
ConvertDateToYMD(_date, &_cur_date_ymd);
UpdateCachedSnowLine();
for (Station *st : Station::Iterate()) st->build_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
for (Waypoint *wp : Waypoint::Iterate()) wp->build_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
@@ -3061,6 +3063,7 @@ bool AfterLoadGame()
/* This triggers only when old snow_lines were copied into the snow_line_height. */
if (IsSavegameVersionBefore(SLV_164) && _settings_game.game_creation.snow_line_height >= MIN_SNOWLINE_HEIGHT * TILE_HEIGHT && SlXvIsFeatureMissing(XSLFI_CHILLPP)) {
_settings_game.game_creation.snow_line_height /= TILE_HEIGHT;
UpdateCachedSnowLine();
}
if (IsSavegameVersionBefore(SLV_164) && !IsSavegameVersionBefore(SLV_32)) {