Change: Standardise unit conversions and allow decimal places.

Previously the decimal_places member was mostly ignored except for
specific conversions. {DECIMAL} with 0 is the same as {COMMA} so there
is no downside to allowing any conversion to have decimals.
This commit is contained in:
Peter Nelson 2023-05-08 23:04:16 +01:00 committed by Michael Lutz
parent 3c2f87ce1f
commit e2f583a34f
2 changed files with 58 additions and 53 deletions

View File

@ -191,15 +191,15 @@ STR_COLOUR_WHITE :White
STR_COLOUR_RANDOM :Random
# Units used in OpenTTD
STR_UNITS_VELOCITY_IMPERIAL :{COMMA}{NBSP}mph
STR_UNITS_VELOCITY_METRIC :{COMMA}{NBSP}km/h
STR_UNITS_VELOCITY_SI :{COMMA}{NBSP}m/s
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_KNOTS :{COMMA}{NBSP}knots
STR_UNITS_VELOCITY_KNOTS :{DECIMAL}{NBSP}knots
STR_UNITS_POWER_IMPERIAL :{COMMA}{NBSP}hp
STR_UNITS_POWER_METRIC :{COMMA}{NBSP}hp
STR_UNITS_POWER_SI :{COMMA}{NBSP}kW
STR_UNITS_POWER_IMPERIAL :{DECIMAL}{NBSP}hp
STR_UNITS_POWER_METRIC :{DECIMAL}{NBSP}hp
STR_UNITS_POWER_SI :{DECIMAL}{NBSP}kW
STR_UNITS_POWER_IMPERIAL_TO_WEIGHT_IMPERIAL :{DECIMAL}{NBSP}hp/t
STR_UNITS_POWER_IMPERIAL_TO_WEIGHT_METRIC :{DECIMAL}{NBSP}hp/t
@ -211,29 +211,29 @@ STR_UNITS_POWER_SI_TO_WEIGHT_IMPERIAL :{DECIMAL}{NBSP}
STR_UNITS_POWER_SI_TO_WEIGHT_METRIC :{DECIMAL}{NBSP}kW/t
STR_UNITS_POWER_SI_TO_WEIGHT_SI :{DECIMAL}{NBSP}W/kg
STR_UNITS_WEIGHT_SHORT_IMPERIAL :{COMMA}{NBSP}t
STR_UNITS_WEIGHT_SHORT_METRIC :{COMMA}{NBSP}t
STR_UNITS_WEIGHT_SHORT_SI :{COMMA}{NBSP}kg
STR_UNITS_WEIGHT_SHORT_IMPERIAL :{DECIMAL}{NBSP}t
STR_UNITS_WEIGHT_SHORT_METRIC :{DECIMAL}{NBSP}t
STR_UNITS_WEIGHT_SHORT_SI :{DECIMAL}{NBSP}kg
STR_UNITS_WEIGHT_LONG_IMPERIAL :{COMMA}{NBSP}ton{P "" s}
STR_UNITS_WEIGHT_LONG_METRIC :{COMMA}{NBSP}tonne{P "" s}
STR_UNITS_WEIGHT_LONG_SI :{COMMA}{NBSP}kg
STR_UNITS_WEIGHT_LONG_IMPERIAL :{DECIMAL}{NBSP}ton{P "" s}
STR_UNITS_WEIGHT_LONG_METRIC :{DECIMAL}{NBSP}tonne{P "" s}
STR_UNITS_WEIGHT_LONG_SI :{DECIMAL}{NBSP}kg
STR_UNITS_VOLUME_SHORT_IMPERIAL :{COMMA}{NBSP}gal
STR_UNITS_VOLUME_SHORT_METRIC :{COMMA}{NBSP}l
STR_UNITS_VOLUME_SHORT_SI :{COMMA}{NBSP}m³
STR_UNITS_VOLUME_SHORT_IMPERIAL :{DECIMAL}{NBSP}gal
STR_UNITS_VOLUME_SHORT_METRIC :{DECIMAL}{NBSP}l
STR_UNITS_VOLUME_SHORT_SI :{DECIMAL}{NBSP}m³
STR_UNITS_VOLUME_LONG_IMPERIAL :{COMMA}{NBSP}gallon{P "" s}
STR_UNITS_VOLUME_LONG_METRIC :{COMMA}{NBSP}litre{P "" s}
STR_UNITS_VOLUME_LONG_SI :{COMMA}{NBSP}m³
STR_UNITS_VOLUME_LONG_IMPERIAL :{DECIMAL}{NBSP}gallon{P "" s}
STR_UNITS_VOLUME_LONG_METRIC :{DECIMAL}{NBSP}litre{P "" s}
STR_UNITS_VOLUME_LONG_SI :{DECIMAL}{NBSP}m³
STR_UNITS_FORCE_IMPERIAL :{COMMA}{NBSP}lbf
STR_UNITS_FORCE_METRIC :{COMMA}{NBSP}kgf
STR_UNITS_FORCE_SI :{COMMA}{NBSP}kN
STR_UNITS_FORCE_IMPERIAL :{DECIMAL}{NBSP}lbf
STR_UNITS_FORCE_METRIC :{DECIMAL}{NBSP}kgf
STR_UNITS_FORCE_SI :{DECIMAL}{NBSP}kN
STR_UNITS_HEIGHT_IMPERIAL :{COMMA}{NBSP}ft
STR_UNITS_HEIGHT_METRIC :{COMMA}{NBSP}m
STR_UNITS_HEIGHT_SI :{COMMA}{NBSP}m
STR_UNITS_HEIGHT_IMPERIAL :{DECIMAL}{NBSP}ft
STR_UNITS_HEIGHT_METRIC :{DECIMAL}{NBSP}m
STR_UNITS_HEIGHT_SI :{DECIMAL}{NBSP}m
# Common window strings
STR_LIST_FILTER_TITLE :{BLACK}Filter:

View File

@ -700,6 +700,7 @@ struct UnitsLong {
UnitConversion c; ///< Conversion
StringID s; ///< String for the short variant of the unit
StringID l; ///< String for the long variant of the unit
unsigned int decimal_places; ///< Number of decimal places embedded in the value. For example, 1 if the value is in tenths, and 3 if the value is in thousandths.
};
/** Unit conversions for velocity. */
@ -733,16 +734,16 @@ static const Units _units_power_to_weight[] = {
/** Unit conversions for weight. */
static const UnitsLong _units_weight[] = {
{ { 1.102311 }, STR_UNITS_WEIGHT_SHORT_IMPERIAL, STR_UNITS_WEIGHT_LONG_IMPERIAL },
{ { 1.0 }, STR_UNITS_WEIGHT_SHORT_METRIC, STR_UNITS_WEIGHT_LONG_METRIC },
{ { 1000.0 }, STR_UNITS_WEIGHT_SHORT_SI, STR_UNITS_WEIGHT_LONG_SI },
{ { 1.102311 }, STR_UNITS_WEIGHT_SHORT_IMPERIAL, STR_UNITS_WEIGHT_LONG_IMPERIAL, 0 },
{ { 1.0 }, STR_UNITS_WEIGHT_SHORT_METRIC, STR_UNITS_WEIGHT_LONG_METRIC, 0 },
{ { 1000.0 }, STR_UNITS_WEIGHT_SHORT_SI, STR_UNITS_WEIGHT_LONG_SI, 0 },
};
/** Unit conversions for volume. */
static const UnitsLong _units_volume[] = {
{ { 264.172 }, STR_UNITS_VOLUME_SHORT_IMPERIAL, STR_UNITS_VOLUME_LONG_IMPERIAL },
{ { 1000.0 }, STR_UNITS_VOLUME_SHORT_METRIC, STR_UNITS_VOLUME_LONG_METRIC },
{ { 1.0 }, STR_UNITS_VOLUME_SHORT_SI, STR_UNITS_VOLUME_LONG_SI },
{ { 264.172 }, STR_UNITS_VOLUME_SHORT_IMPERIAL, STR_UNITS_VOLUME_LONG_IMPERIAL, 0 },
{ { 1000.0 }, STR_UNITS_VOLUME_SHORT_METRIC, STR_UNITS_VOLUME_LONG_METRIC, 0 },
{ { 1.0 }, STR_UNITS_VOLUME_SHORT_SI, STR_UNITS_VOLUME_LONG_SI, 0 },
};
/** Unit conversions for force. */
@ -1269,36 +1270,36 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg
case SCC_FORCE: { // {FORCE}
assert(_settings_game.locale.units_force < lengthof(_units_force));
int64 args_array[1] = {_units_force[_settings_game.locale.units_force].c.ToDisplay(args->GetInt64())};
const auto &x = _units_force[_settings_game.locale.units_force];
int64 args_array[] = {x.c.ToDisplay(args->GetInt64()), x.decimal_places};
StringParameters tmp_params(args_array);
buff = FormatString(buff, GetStringPtr(_units_force[_settings_game.locale.units_force].s), &tmp_params, last);
buff = FormatString(buff, GetStringPtr(x.s), &tmp_params, last);
break;
}
case SCC_HEIGHT: { // {HEIGHT}
assert(_settings_game.locale.units_height < lengthof(_units_height));
int64 args_array[] = {_units_height[_settings_game.locale.units_height].c.ToDisplay(args->GetInt64())};
const auto &x = _units_height[_settings_game.locale.units_height];
int64 args_array[] = {x.c.ToDisplay(args->GetInt64()), x.decimal_places};
StringParameters tmp_params(args_array);
buff = FormatString(buff, GetStringPtr(_units_height[_settings_game.locale.units_height].s), &tmp_params, last);
buff = FormatString(buff, GetStringPtr(x.s), &tmp_params, last);
break;
}
case SCC_POWER: { // {POWER}
assert(_settings_game.locale.units_power < lengthof(_units_power));
int64 args_array[1] = {_units_power[_settings_game.locale.units_power].c.ToDisplay(args->GetInt64())};
const auto &x = _units_power[_settings_game.locale.units_power];
int64 args_array[] = {x.c.ToDisplay(args->GetInt64()), x.decimal_places};
StringParameters tmp_params(args_array);
buff = FormatString(buff, GetStringPtr(_units_power[_settings_game.locale.units_power].s), &tmp_params, last);
buff = FormatString(buff, GetStringPtr(x.s), &tmp_params, last);
break;
}
case SCC_POWER_TO_WEIGHT: { // {POWER_TO_WEIGHT}
auto setting = _settings_game.locale.units_power * 3u + _settings_game.locale.units_weight;
assert(setting < lengthof(_units_power_to_weight));
auto const &x = _units_power_to_weight[setting];
const auto &x = _units_power_to_weight[setting];
int64 args_array[] = {x.c.ToDisplay(args->GetInt64()), x.decimal_places};
StringParameters tmp_params(args_array);
buff = FormatString(buff, GetStringPtr(x.s), &tmp_params, last);
break;
@ -1310,42 +1311,46 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg
VehicleType vt = static_cast<VehicleType>(GB(arg, 56, 8));
byte units = GetVelocityUnits(vt);
assert(units < lengthof(_units_velocity));
unsigned int decimal_places = _units_velocity[units].decimal_places;
uint64 args_array[] = {ConvertKmhishSpeedToDisplaySpeed(GB(arg, 0, 56), vt), decimal_places};
StringParameters tmp_params(args_array, decimal_places ? 2 : 1, nullptr);
buff = FormatString(buff, GetStringPtr(_units_velocity[units].s), &tmp_params, last);
const auto &x = _units_velocity[units];
int64 args_array[] = {ConvertKmhishSpeedToDisplaySpeed(GB(arg, 0, 56), vt), x.decimal_places};
StringParameters tmp_params(args_array);
buff = FormatString(buff, GetStringPtr(x.s), &tmp_params, last);
break;
}
case SCC_VOLUME_SHORT: { // {VOLUME_SHORT}
assert(_settings_game.locale.units_volume < lengthof(_units_volume));
int64 args_array[1] = {_units_volume[_settings_game.locale.units_volume].c.ToDisplay(args->GetInt64())};
const auto &x = _units_volume[_settings_game.locale.units_volume];
int64 args_array[] = {x.c.ToDisplay(args->GetInt64()), x.decimal_places};
StringParameters tmp_params(args_array);
buff = FormatString(buff, GetStringPtr(_units_volume[_settings_game.locale.units_volume].s), &tmp_params, last);
buff = FormatString(buff, GetStringPtr(x.s), &tmp_params, last);
break;
}
case SCC_VOLUME_LONG: { // {VOLUME_LONG}
assert(_settings_game.locale.units_volume < lengthof(_units_volume));
int64 args_array[1] = {_units_volume[_settings_game.locale.units_volume].c.ToDisplay(args->GetInt64(SCC_VOLUME_LONG))};
const auto &x = _units_volume[_settings_game.locale.units_volume];
int64 args_array[] = {x.c.ToDisplay(args->GetInt64(SCC_VOLUME_LONG)), x.decimal_places};
StringParameters tmp_params(args_array);
buff = FormatString(buff, GetStringPtr(_units_volume[_settings_game.locale.units_volume].l), &tmp_params, last);
buff = FormatString(buff, GetStringPtr(x.l), &tmp_params, last);
break;
}
case SCC_WEIGHT_SHORT: { // {WEIGHT_SHORT}
assert(_settings_game.locale.units_weight < lengthof(_units_weight));
int64 args_array[1] = {_units_weight[_settings_game.locale.units_weight].c.ToDisplay(args->GetInt64())};
const auto &x = _units_weight[_settings_game.locale.units_weight];
int64 args_array[] = {x.c.ToDisplay(args->GetInt64()), x.decimal_places};
StringParameters tmp_params(args_array);
buff = FormatString(buff, GetStringPtr(_units_weight[_settings_game.locale.units_weight].s), &tmp_params, last);
buff = FormatString(buff, GetStringPtr(x.s), &tmp_params, last);
break;
}
case SCC_WEIGHT_LONG: { // {WEIGHT_LONG}
assert(_settings_game.locale.units_weight < lengthof(_units_weight));
int64 args_array[1] = {_units_weight[_settings_game.locale.units_weight].c.ToDisplay(args->GetInt64(SCC_WEIGHT_LONG))};
const auto &x = _units_weight[_settings_game.locale.units_weight];
int64 args_array[] = {x.c.ToDisplay(args->GetInt64(SCC_WEIGHT_LONG)), x.decimal_places};
StringParameters tmp_params(args_array);
buff = FormatString(buff, GetStringPtr(_units_weight[_settings_game.locale.units_weight].l), &tmp_params, last);
buff = FormatString(buff, GetStringPtr(x.l), &tmp_params, last);
break;
}