From 646a7e625b17c32a36678810781075142f4df5d6 Mon Sep 17 00:00:00 2001 From: Tyler Trahan Date: Fri, 14 Apr 2023 16:49:12 -0400 Subject: [PATCH] Change: Use seconds for Linkgraph update settings (#10610) --- src/date_type.h | 2 ++ src/lang/english.txt | 8 ++++---- src/linkgraph/linkgraphjob.cpp | 2 +- src/linkgraph/linkgraphschedule.cpp | 6 +++--- src/saveload/afterload.cpp | 6 ++++++ src/saveload/saveload.h | 1 + src/settings.cpp | 14 ++++++++++---- src/table/settings/linkgraph_settings.ini | 20 ++++++++++---------- 8 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/date_type.h b/src/date_type.h index 818a124265..c26844d589 100644 --- a/src/date_type.h +++ b/src/date_type.h @@ -30,6 +30,8 @@ static const int DAYS_IN_YEAR = 365; ///< days per year static const int DAYS_IN_LEAP_YEAR = 366; ///< sometimes, you need one day more... static const int MONTHS_IN_YEAR = 12; ///< months per year +static const int SECONDS_PER_DAY = 2; ///< approximate seconds per day, not for precise calculations + static const int STATION_RATING_TICKS = 185; ///< cycle duration for updating station rating static const int STATION_ACCEPTANCE_TICKS = 250; ///< cycle duration for updating station acceptance static const int STATION_LINKGRAPH_TICKS = 504; ///< cycle duration for cleaning dead links diff --git a/src/lang/english.txt b/src/lang/english.txt index 09dad7f035..0ea5d374b0 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -1926,10 +1926,10 @@ STR_CONFIG_SETTING_LARGER_TOWNS_DISABLED :None STR_CONFIG_SETTING_CITY_SIZE_MULTIPLIER :Initial city size multiplier: {STRING2} STR_CONFIG_SETTING_CITY_SIZE_MULTIPLIER_HELPTEXT :Average size of cities relative to normal towns at start of the game -STR_CONFIG_SETTING_LINKGRAPH_INTERVAL :Update distribution graph every {STRING2}{NBSP}day{P 0:2 "" s} -STR_CONFIG_SETTING_LINKGRAPH_INTERVAL_HELPTEXT :Time between subsequent recalculations of the link graph. Each recalculation calculates the plans for one component of the graph. That means that a value X for this setting does not mean the whole graph will be updated every X days. Only some component will. The shorter you set it the more CPU time will be necessary to calculate it. The longer you set it the longer it will take until the cargo distribution starts on new routes. -STR_CONFIG_SETTING_LINKGRAPH_TIME :Take {STRING2}{NBSP}day{P 0:2 "" s} for recalculation of distribution graph -STR_CONFIG_SETTING_LINKGRAPH_TIME_HELPTEXT :Time taken for each recalculation of a link graph component. When a recalculation is started, a thread is spawned which is allowed to run for this number of days. The shorter you set this the more likely it is that the thread is not finished when it's supposed to. Then the game stops until it is ("lag"). The longer you set it the longer it takes for the distribution to be updated when routes change. +STR_CONFIG_SETTING_LINKGRAPH_RECALC_INTERVAL :Update distribution graph every {STRING2}{NBSP}second{P 0:2 "" s} +STR_CONFIG_SETTING_LINKGRAPH_RECALC_INTERVAL_HELPTEXT :Time between subsequent recalculations of the link graph. Each recalculation calculates the plans for one component of the graph. That means that a value X for this setting does not mean the whole graph will be updated every X seconds. Only some component will. The shorter you set it the more CPU time will be necessary to calculate it. The longer you set it the longer it will take until the cargo distribution starts on new routes. +STR_CONFIG_SETTING_LINKGRAPH_RECALC_TIME :Take {STRING2}{NBSP}second{P 0:2 "" s} for recalculation of distribution graph +STR_CONFIG_SETTING_LINKGRAPH_RECALC_TIME_HELPTEXT :Time taken for each recalculation of a link graph component. When a recalculation is started, a thread is spawned which is allowed to run for this number of seconds. The shorter you set this the more likely it is that the thread is not finished when it's supposed to. Then the game stops until it is ("lag"). The longer you set it the longer it takes for the distribution to be updated when routes change. STR_CONFIG_SETTING_DISTRIBUTION_PAX :Distribution mode for passengers: {STRING2} STR_CONFIG_SETTING_DISTRIBUTION_PAX_HELPTEXT :"Symmetric" means that roughly the same number of passengers will go from a station A to a station B as from B to A. "Asymmetric" means that arbitrary numbers of passengers can go in either direction. "Manual" means that no automatic distribution will take place for passengers. diff --git a/src/linkgraph/linkgraphjob.cpp b/src/linkgraph/linkgraphjob.cpp index 5902e01e73..6c5e889bc6 100644 --- a/src/linkgraph/linkgraphjob.cpp +++ b/src/linkgraph/linkgraphjob.cpp @@ -37,7 +37,7 @@ LinkGraphJob::LinkGraphJob(const LinkGraph &orig) : * This is on purpose. */ link_graph(orig), settings(_settings_game.linkgraph), - join_date(_date + _settings_game.linkgraph.recalc_time), + join_date(_date + (_settings_game.linkgraph.recalc_time / SECONDS_PER_DAY)), job_completed(false), job_aborted(false) { diff --git a/src/linkgraph/linkgraphschedule.cpp b/src/linkgraph/linkgraphschedule.cpp index 3581c72ac9..cdd057718b 100644 --- a/src/linkgraph/linkgraphschedule.cpp +++ b/src/linkgraph/linkgraphschedule.cpp @@ -178,7 +178,7 @@ void StateGameLoop_LinkGraphPauseControl() } } else if (_pause_mode == PM_UNPAUSED && _date_fract == LinkGraphSchedule::SPAWN_JOIN_TICK - 2 && - _date % _settings_game.linkgraph.recalc_interval == _settings_game.linkgraph.recalc_interval / 2 && + _date % (_settings_game.linkgraph.recalc_interval / SECONDS_PER_DAY) == (_settings_game.linkgraph.recalc_interval / SECONDS_PER_DAY) / 2 && LinkGraphSchedule::instance.IsJoinWithUnfinishedJobDue()) { /* Perform check two _date_fract ticks before we would join, to make * sure it also works in multiplayer. */ @@ -205,10 +205,10 @@ void AfterLoad_LinkGraphPauseControl() void OnTick_LinkGraph() { if (_date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK) return; - Date offset = _date % _settings_game.linkgraph.recalc_interval; + Date offset = _date % (_settings_game.linkgraph.recalc_interval / SECONDS_PER_DAY); if (offset == 0) { LinkGraphSchedule::instance.SpawnNext(); - } else if (offset == _settings_game.linkgraph.recalc_interval / 2) { + } else if (offset == (_settings_game.linkgraph.recalc_interval / SECONDS_PER_DAY) / 2) { if (!_networking || _network_server) { PerformanceMeasurer::SetInactive(PFE_GL_LINKGRAPH); LinkGraphSchedule::instance.JoinNext(); diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 4749f42c58..1d3c018a49 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -799,6 +799,12 @@ bool AfterLoadGame() _settings_game.game_creation.ending_year = DEF_END_YEAR; } + /* Convert linkgraph update settings from days to seconds. */ + if (IsSavegameVersionBefore(SLV_LINKGRAPH_SECONDS)) { + _settings_game.linkgraph.recalc_interval *= SECONDS_PER_DAY; + _settings_game.linkgraph.recalc_time *= SECONDS_PER_DAY; + } + /* Load the sprites */ GfxLoadSprites(); LoadStringWidthTable(); diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 0cc9cbaaa3..60fc0adbe9 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -350,6 +350,7 @@ enum SaveLoadVersion : uint16 { SLV_VELOCITY_NAUTICAL, ///< 305 PR#10594 Separation of land and nautical velocity (knots!) SLV_CONSISTENT_PARTIAL_Z, ///< 306 PR#10570 Conversion from an inconsistent partial Z calculation for slopes, to one that is (more) consistent. SLV_MORE_CARGO_AGE, ///< 307 PR#10596 Track cargo age for a longer period. + SLV_LINKGRAPH_SECONDS, ///< 308 PR#10610 Store linkgraph update intervals in seconds instead of days. SL_MAX_VERSION, ///< Highest possible saveload version }; diff --git a/src/settings.cpp b/src/settings.cpp index bc3db783fb..fd8823b843 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -154,11 +154,12 @@ public: * location. These versions assist with situations like that. */ enum IniFileVersion : uint32 { - IFV_0, ///< 0 All versions prior to introduction. - IFV_PRIVATE_SECRETS, ///< 1 PR#9298 Moving of settings from openttd.cfg to private.cfg / secrets.cfg. - IFV_GAME_TYPE, ///< 2 PR#9515 Convert server_advertise to server_game_type. + IFV_0, ///< 0 All versions prior to introduction. + IFV_PRIVATE_SECRETS, ///< 1 PR#9298 Moving of settings from openttd.cfg to private.cfg / secrets.cfg. + IFV_GAME_TYPE, ///< 2 PR#9515 Convert server_advertise to server_game_type. + IFV_LINKGRAPH_SECONDS, ///< 3 PR#10610 Store linkgraph update intervals in seconds instead of days. - IFV_MAX_VERSION, ///< Highest possible ini-file version. + IFV_MAX_VERSION, ///< Highest possible ini-file version. }; const uint16 INIFILE_VERSION = (IniFileVersion)(IFV_MAX_VERSION - 1); ///< Current ini-file version of OpenTTD. @@ -1236,6 +1237,11 @@ void LoadFromConfig(bool startup) } } + if (generic_version < IFV_LINKGRAPH_SECONDS) { + _settings_newgame.linkgraph.recalc_interval *= SECONDS_PER_DAY; + _settings_newgame.linkgraph.recalc_time *= SECONDS_PER_DAY; + } + _grfconfig_newgame = GRFLoadConfig(generic_ini, "newgrf", false); _grfconfig_static = GRFLoadConfig(generic_ini, "newgrf-static", true); AILoadConfig(generic_ini, "ai_players"); diff --git a/src/table/settings/linkgraph_settings.ini b/src/table/settings/linkgraph_settings.ini index 04ef32d621..efcc9871b0 100644 --- a/src/table/settings/linkgraph_settings.ini +++ b/src/table/settings/linkgraph_settings.ini @@ -37,26 +37,26 @@ startup = false var = linkgraph.recalc_interval type = SLE_UINT16 from = SLV_183 -def = 4 -min = 2 -max = 32 -interval = 2 -str = STR_CONFIG_SETTING_LINKGRAPH_INTERVAL +def = 8 +min = 4 +max = 90 +interval = 1 +str = STR_CONFIG_SETTING_LINKGRAPH_RECALC_INTERVAL strval = STR_JUST_COMMA -strhelp = STR_CONFIG_SETTING_LINKGRAPH_INTERVAL_HELPTEXT +strhelp = STR_CONFIG_SETTING_LINKGRAPH_RECALC_INTERVAL_HELPTEXT extra = offsetof(LinkGraphSettings, recalc_interval) [SDT_VAR] var = linkgraph.recalc_time type = SLE_UINT16 from = SLV_183 -def = 16 +def = 32 min = 1 -max = 4096 +max = 9000 interval = 1 -str = STR_CONFIG_SETTING_LINKGRAPH_TIME +str = STR_CONFIG_SETTING_LINKGRAPH_RECALC_TIME strval = STR_JUST_COMMA -strhelp = STR_CONFIG_SETTING_LINKGRAPH_TIME_HELPTEXT +strhelp = STR_CONFIG_SETTING_LINKGRAPH_RECALC_TIME_HELPTEXT extra = offsetof(LinkGraphSettings, recalc_time) [SDT_VAR]