Feature: Use real-time "wallclock" timekeeping units (#11341)

This commit is contained in:
Tyler Trahan
2024-01-23 11:36:09 -05:00
committed by GitHub
parent bbdbf9a589
commit fd9e72a7e7
30 changed files with 546 additions and 140 deletions

View File

@@ -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. */
@@ -802,16 +811,45 @@ static const Units _units_height[] = {
{ { 1.0 }, STR_UNITS_HEIGHT_SI, 0 },
};
/**
* Get index for velocity conversion units for a vehicle type.
* @param type VehicleType to convert velocity for.
* @return Index within velocity conversion units for vehicle type.
*/
static byte GetVelocityUnits(VehicleType type)
{
if (type == VEH_SHIP || type == VEH_AIRCRAFT) return _settings_game.locale.units_velocity_nautical;
/** Unit conversions for time in calendar days or wallclock seconds */
static const Units _units_time_days_or_seconds[] = {
{ { 1 }, STR_UNITS_DAYS, 0 },
{ { 2 }, STR_UNITS_SECONDS, 0 },
};
return _settings_game.locale.units_velocity;
/** Unit conversions for time in calendar months or wallclock minutes */
static const Units _units_time_months_or_minutes[] = {
{ { 1 }, STR_UNITS_MONTHS, 0 },
{ { 1 }, STR_UNITS_MINUTES, 0 },
};
/** Unit conversions for time in calendar years or economic periods */
static const Units _units_time_years_or_periods[] = {
{ { 1 }, STR_UNITS_YEARS, 0 },
{ { 1 }, STR_UNITS_PERIODS, 0 },
};
/** Unit conversions for time in calendar years or wallclock minutes */
static const Units _units_time_years_or_minutes[] = {
{ { 1 }, STR_UNITS_YEARS, 0 },
{ { 12 }, STR_UNITS_MINUTES, 0 },
};
/**
* 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 The Units for the proper vehicle and time mode.
*/
static const Units GetVelocityUnits(VehicleType type)
{
byte setting = (type == VEH_SHIP || type == VEH_AIRCRAFT) ? _settings_game.locale.units_velocity_nautical : _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];
}
/**
@@ -824,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);
}
/**
@@ -834,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);
}
/**
@@ -844,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;
}
/**
@@ -854,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);
}
/**
@@ -1334,9 +1372,7 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara
int64_t arg = args.GetNextParameter<int64_t>();
// Unpack vehicle type from packed argument to get desired units.
VehicleType vt = static_cast<VehicleType>(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;
@@ -1374,6 +1410,43 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara
break;
}
case SCC_UNITS_DAYS_OR_SECONDS: { // {UNITS_DAYS_OR_SECONDS}
uint8_t realtime = TimerGameEconomy::UsingWallclockUnits(_game_mode == GM_MENU);
const auto &x = _units_time_days_or_seconds[realtime];
auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetNextParameter<int64_t>()), x.decimal_places);
FormatString(builder, GetStringPtr(x.s), tmp_params);
break;
}
case SCC_UNITS_MONTHS_OR_MINUTES: { // {UNITS_MONTHS_OR_MINUTES}
uint8_t realtime = TimerGameEconomy::UsingWallclockUnits(_game_mode == GM_MENU);
const auto &x = _units_time_months_or_minutes[realtime];
auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetNextParameter<int64_t>()), x.decimal_places);
FormatString(builder, GetStringPtr(x.s), tmp_params);
break;
}
case SCC_UNITS_YEARS_OR_PERIODS: { // {UNITS_YEARS_OR_PERIODS}
uint8_t realtime = TimerGameEconomy::UsingWallclockUnits(_game_mode == GM_MENU);
const auto &x = _units_time_years_or_periods[realtime];
auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetNextParameter<int64_t>()), x.decimal_places);
FormatString(builder, GetStringPtr(x.s), tmp_params);
break;
}
case SCC_UNITS_YEARS_OR_MINUTES: { // {UNITS_YEARS_OR_MINUTES}
uint8_t realtime = TimerGameEconomy::UsingWallclockUnits(_game_mode == GM_MENU);
const auto &x = _units_time_years_or_minutes[realtime];
auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetNextParameter<int64_t>()), x.decimal_places);
FormatString(builder, GetStringPtr(x.s), tmp_params);
break;
}
case SCC_TIMEKEEPING_MODE_LIST: { // {TKM}
str = ParseStringChoice(str, (uint8_t)TimerGameEconomy::UsingWallclockUnits(_game_mode == GM_MENU), builder);
break;
}
case SCC_COMPANY_NAME: { // {COMPANY}
const Company *c = Company::GetIfValid(args.GetNextParameter<CompanyID>());
if (c == nullptr) break;