From 0b4311bf13737bb2bd4a00b18402c40707950646 Mon Sep 17 00:00:00 2001 From: Tyler Trahan Date: Thu, 28 Sep 2023 09:40:02 -0400 Subject: [PATCH] Add: Tiles/second wallclock mode for tiles/day velocity unit --- src/lang/english.txt | 5 +++-- src/strings.cpp | 48 +++++++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/lang/english.txt b/src/lang/english.txt index a60d1e5fd9..1cba7692e2 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -214,7 +214,8 @@ STR_COLOUR_SECONDARY_SAME_AS_PRIMARY :Same As Primary STR_UNITS_VELOCITY_IMPERIAL :{DECIMAL}{NBSP}mph STR_UNITS_VELOCITY_METRIC :{DECIMAL}{NBSP}km/h STR_UNITS_VELOCITY_SI :{DECIMAL}{NBSP}m/s -STR_UNITS_VELOCITY_GAMEUNITS :{DECIMAL}{NBSP}tiles/day +STR_UNITS_VELOCITY_GAMEUNITS_DAY :{DECIMAL}{NBSP}tiles/day +STR_UNITS_VELOCITY_GAMEUNITS_SEC :{DECIMAL}{NBSP}tiles/sec STR_UNITS_VELOCITY_KNOTS :{DECIMAL}{NBSP}knots STR_UNITS_POWER_IMPERIAL :{DECIMAL}{NBSP}hp @@ -2025,7 +2026,7 @@ STR_CONFIG_SETTING_LOCALISATION_UNITS_VELOCITY_HELPTEXT :Whenever a spee STR_CONFIG_SETTING_LOCALISATION_UNITS_VELOCITY_IMPERIAL :Imperial (mph) STR_CONFIG_SETTING_LOCALISATION_UNITS_VELOCITY_METRIC :Metric (km/h) STR_CONFIG_SETTING_LOCALISATION_UNITS_VELOCITY_SI :SI (m/s) -STR_CONFIG_SETTING_LOCALISATION_UNITS_VELOCITY_GAMEUNITS :Game units (tiles/day) +STR_CONFIG_SETTING_LOCALISATION_UNITS_VELOCITY_GAMEUNITS :Game units ({TKM "tiles/day" "tiles/sec"}) STR_CONFIG_SETTING_LOCALISATION_UNITS_VELOCITY_KNOTS :Knots STR_CONFIG_SETTING_LOCALISATION_UNITS_POWER :Vehicle power units: {STRING2} diff --git a/src/strings.cpp b/src/strings.cpp index af23786050..bbf02aaf83 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -746,12 +746,21 @@ struct UnitsLong { }; /** Unit conversions for velocity. */ -static const Units _units_velocity[] = { - { { 1.0 }, STR_UNITS_VELOCITY_IMPERIAL, 0 }, - { { 1.609344 }, STR_UNITS_VELOCITY_METRIC, 0 }, - { { 0.44704 }, STR_UNITS_VELOCITY_SI, 0 }, - { { 0.578125 }, STR_UNITS_VELOCITY_GAMEUNITS, 1 }, - { { 0.868976 }, STR_UNITS_VELOCITY_KNOTS, 0 }, +static const Units _units_velocity_calendar[] = { + { { 1.0 }, STR_UNITS_VELOCITY_IMPERIAL, 0 }, + { { 1.609344 }, STR_UNITS_VELOCITY_METRIC, 0 }, + { { 0.44704 }, STR_UNITS_VELOCITY_SI, 0 }, + { { 0.578125 }, STR_UNITS_VELOCITY_GAMEUNITS_DAY, 1 }, + { { 0.868976 }, STR_UNITS_VELOCITY_KNOTS, 0 }, +}; + +/** Unit conversions for velocity. */ +static const Units _units_velocity_realtime[] = { + { { 1.0 }, STR_UNITS_VELOCITY_IMPERIAL, 0 }, + { { 1.609344 }, STR_UNITS_VELOCITY_METRIC, 0 }, + { { 0.44704 }, STR_UNITS_VELOCITY_SI, 0 }, + { { 0.289352 }, STR_UNITS_VELOCITY_GAMEUNITS_SEC, 1 }, + { { 0.868976 }, STR_UNITS_VELOCITY_KNOTS, 0 }, }; /** Unit conversions for power. */ @@ -827,15 +836,20 @@ static const Units _units_time_years_or_minutes[] = { }; /** - * Get index for velocity conversion units for a vehicle type. + * Get the correct velocity units depending on the vehicle type and whether we're using real-time units. * @param type VehicleType to convert velocity for. - * @return Index within velocity conversion units for vehicle type. + * @return The Units for the proper vehicle and time mode. */ -static byte GetVelocityUnits(VehicleType type) +static const Units GetVelocityUnits(VehicleType type) { - if (type == VEH_SHIP || type == VEH_AIRCRAFT) return _settings_game.locale.units_velocity_nautical; + byte setting = (type == VEH_SHIP || type == VEH_AIRCRAFT) ? _settings_game.locale.units_velocity_nautical : _settings_game.locale.units_velocity; - return _settings_game.locale.units_velocity; + assert(setting < lengthof(_units_velocity_calendar)); + assert(setting < lengthof(_units_velocity_realtime)); + + if (TimerGameEconomy::UsingWallclockUnits()) return _units_velocity_realtime[setting]; + + return _units_velocity_calendar[setting]; } /** @@ -848,7 +862,7 @@ uint ConvertSpeedToDisplaySpeed(uint speed, VehicleType type) /* For historical reasons we don't want to mess with the * conversion for speed. So, don't round it and keep the * original conversion factors instead of the real ones. */ - return _units_velocity[GetVelocityUnits(type)].c.ToDisplay(speed, false); + return GetVelocityUnits(type).c.ToDisplay(speed, false); } /** @@ -858,7 +872,7 @@ uint ConvertSpeedToDisplaySpeed(uint speed, VehicleType type) */ uint ConvertDisplaySpeedToSpeed(uint speed, VehicleType type) { - return _units_velocity[GetVelocityUnits(type)].c.FromDisplay(speed); + return GetVelocityUnits(type).c.FromDisplay(speed); } /** @@ -868,7 +882,7 @@ uint ConvertDisplaySpeedToSpeed(uint speed, VehicleType type) */ uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type) { - return _units_velocity[GetVelocityUnits(type)].c.ToDisplay(speed * 10, false) / 16; + return GetVelocityUnits(type).c.ToDisplay(speed * 10, false) / 16; } /** @@ -878,7 +892,7 @@ uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type) */ uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type) { - return _units_velocity[GetVelocityUnits(type)].c.FromDisplay(speed * 16, true, 10); + return GetVelocityUnits(type).c.FromDisplay(speed * 16, true, 10); } /** @@ -1358,9 +1372,7 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara int64_t arg = args.GetNextParameter(); // Unpack vehicle type from packed argument to get desired units. VehicleType vt = static_cast(GB(arg, 56, 8)); - byte units = GetVelocityUnits(vt); - assert(units < lengthof(_units_velocity)); - const auto &x = _units_velocity[units]; + const auto &x = GetVelocityUnits(vt); auto tmp_params = MakeParameters(ConvertKmhishSpeedToDisplaySpeed(GB(arg, 0, 56), vt), x.decimal_places); FormatString(builder, GetStringPtr(x.s), tmp_params); break;