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

View File

@@ -92,6 +92,8 @@ extern const byte _slope_to_sprite_offset[32] = {
*/ */
static SnowLine *_snow_line = nullptr; static SnowLine *_snow_line = nullptr;
byte _cached_snowline = 0;
/** /**
* Map 2D viewport or smallmap coordinate to 3D world or tile coordinate. * Map 2D viewport or smallmap coordinate to 3D world or tile coordinate.
* Function takes into account height of tiles and foundations. * 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]); _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. * @return the snow line height.
* @ingroup SnowLineGroup * @ingroup SnowLineGroup
*/ */
byte GetSnowLine() byte GetSnowLineUncached()
{ {
if (_snow_line == nullptr) return _settings_game.game_creation.snow_line_height; if (_snow_line == nullptr) return _settings_game.game_creation.snow_line_height;
return _snow_line->table[_cur_date_ymd.month][_cur_date_ymd.day]; 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. * Get the highest possible snow line height, either variable or static.
* @return the highest snow line height. * @return the highest snow line height.
@@ -685,6 +694,7 @@ void ClearSnowLine()
{ {
free(_snow_line); free(_snow_line);
_snow_line = nullptr; _snow_line = nullptr;
UpdateCachedSnowLine();
} }
/** /**

View File

@@ -28,11 +28,18 @@ struct SnowLine {
bool IsSnowLineSet(); bool IsSnowLineSet();
void SetSnowLine(byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]); void SetSnowLine(byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS]);
byte GetSnowLine(); byte GetSnowLineUncached();
void UpdateCachedSnowLine();
byte HighestSnowLine(); byte HighestSnowLine();
byte LowestSnowLine(); byte LowestSnowLine();
void ClearSnowLine(); void ClearSnowLine();
inline byte GetSnowLine()
{
extern byte _cached_snowline;
return _cached_snowline;
}
int GetSlopeZInCorner(Slope tileh, Corner corner); int GetSlopeZInCorner(Slope tileh, Corner corner);
Slope GetFoundationSlope(TileIndex tile, int *z = nullptr); 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 { } else {
SetScaledTickVariables(); SetScaledTickVariables();
} }
UpdateCachedSnowLine();
LinkGraphSchedule::Clear(); LinkGraphSchedule::Clear();
ClearTraceRestrictMapping(); ClearTraceRestrictMapping();

View File

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

View File

@@ -627,6 +627,7 @@ bool AfterLoadGame()
RebuildTownKdtree(); RebuildTownKdtree();
RebuildStationKdtree(); RebuildStationKdtree();
UpdateCachedSnowLine();
_viewport_sign_kdtree_valid = false; _viewport_sign_kdtree_valid = false;
@@ -1671,6 +1672,7 @@ bool AfterLoadGame()
_date += DAYS_TILL_ORIGINAL_BASE_YEAR; _date += DAYS_TILL_ORIGINAL_BASE_YEAR;
SetScaledTickVariables(); SetScaledTickVariables();
ConvertDateToYMD(_date, &_cur_date_ymd); ConvertDateToYMD(_date, &_cur_date_ymd);
UpdateCachedSnowLine();
for (Station *st : Station::Iterate()) st->build_date += DAYS_TILL_ORIGINAL_BASE_YEAR; 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; 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. */ /* 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)) { 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; _settings_game.game_creation.snow_line_height /= TILE_HEIGHT;
UpdateCachedSnowLine();
} }
if (IsSavegameVersionBefore(SLV_164) && !IsSavegameVersionBefore(SLV_32)) { if (IsSavegameVersionBefore(SLV_164) && !IsSavegameVersionBefore(SLV_32)) {