File diff r27049:913f3ce60451 → r27050:d85c65824c1e
src/strings.cpp
Show inline comments
 
@@ -700,16 +700,17 @@ struct UnitsLong {
 
	StringID s;       ///< String for the short variant of the unit
 
	StringID l;       ///< String for the long variant of the unit
 
};
 

	
 
/** Unit conversions for velocity. */
 
static const Units _units_velocity[] = {
 
	{ {    1,  0}, STR_UNITS_VELOCITY_IMPERIAL,     0 },
 
	{ {  103,  6}, STR_UNITS_VELOCITY_METRIC,       0 },
 
	{ { 1831, 12}, STR_UNITS_VELOCITY_SI,           0 },
 
	{ {37888, 16}, STR_UNITS_VELOCITY_GAMEUNITS,    1 },
 
	{ {       1,  0}, STR_UNITS_VELOCITY_IMPERIAL,     0 },
 
	{ {     103,  6}, STR_UNITS_VELOCITY_METRIC,       0 },
 
	{ {    1831, 12}, STR_UNITS_VELOCITY_SI,           0 },
 
	{ {   37888, 16}, STR_UNITS_VELOCITY_GAMEUNITS,    1 },
 
	{ { 7289499, 23}, STR_UNITS_VELOCITY_KNOTS,        0 },
 
};
 

	
 
/** Unit conversions for power. */
 
static const Units _units_power[] = {
 
	{ {   1,  0}, STR_UNITS_POWER_IMPERIAL, 0 },
 
	{ {4153, 12}, STR_UNITS_POWER_METRIC,   0 },
 
@@ -755,52 +756,64 @@ static const Units _units_height[] = {
 
	{ {   3,  0}, STR_UNITS_HEIGHT_IMPERIAL, 0 }, // "Wrong" conversion factor for more nicer GUI values
 
	{ {   1,  0}, STR_UNITS_HEIGHT_METRIC,   0 },
 
	{ {   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;
 

	
 
	return _settings_game.locale.units_velocity;
 
}
 

	
 
/**
 
 * Convert the given (internal) speed to the display speed.
 
 * @param speed the speed to convert
 
 * @return the converted speed.
 
 */
 
uint ConvertSpeedToDisplaySpeed(uint speed)
 
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[_settings_game.locale.units_velocity].c.ToDisplay(speed, false);
 
	return _units_velocity[GetVelocityUnits(type)].c.ToDisplay(speed, false);
 
}
 

	
 
/**
 
 * Convert the given display speed to the (internal) speed.
 
 * @param speed the speed to convert
 
 * @return the converted speed.
 
 */
 
uint ConvertDisplaySpeedToSpeed(uint speed)
 
uint ConvertDisplaySpeedToSpeed(uint speed, VehicleType type)
 
{
 
	return _units_velocity[_settings_game.locale.units_velocity].c.FromDisplay(speed);
 
	return _units_velocity[GetVelocityUnits(type)].c.FromDisplay(speed);
 
}
 

	
 
/**
 
 * Convert the given km/h-ish speed to the display speed.
 
 * @param speed the speed to convert
 
 * @return the converted speed.
 
 */
 
uint ConvertKmhishSpeedToDisplaySpeed(uint speed)
 
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type)
 
{
 
	return _units_velocity[_settings_game.locale.units_velocity].c.ToDisplay(speed * 10, false) / 16;
 
	return _units_velocity[GetVelocityUnits(type)].c.ToDisplay(speed * 10, false) / 16;
 
}
 

	
 
/**
 
 * Convert the given display speed to the km/h-ish speed.
 
 * @param speed the speed to convert
 
 * @return the converted speed.
 
 */
 
uint ConvertDisplaySpeedToKmhishSpeed(uint speed)
 
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type)
 
{
 
	return _units_velocity[_settings_game.locale.units_velocity].c.FromDisplay(speed * 16, true, 10);
 
	return _units_velocity[GetVelocityUnits(type)].c.FromDisplay(speed * 16, true, 10);
 
}
 

	
 
static std::vector<const char *> _game_script_raw_strings;
 

	
 
/**
 
 * Parse most format codes within a string and write the result to a buffer.
 
@@ -1288,17 +1301,21 @@ static char *FormatString(char *buff, co
 
				StringParameters tmp_params(args_array);
 
				buff = FormatString(buff, GetStringPtr(x.s), &tmp_params, last);
 
				break;
 
			}
 

	
 
			case SCC_VELOCITY: { // {VELOCITY}
 
				assert(_settings_game.locale.units_velocity < lengthof(_units_velocity));
 
				unsigned int decimal_places = _units_velocity[_settings_game.locale.units_velocity].decimal_places;
 
				uint64 args_array[] = {ConvertKmhishSpeedToDisplaySpeed(args->GetInt64(SCC_VELOCITY)), decimal_places};
 
				int64 arg = args->GetInt64(SCC_VELOCITY);
 
				// 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));
 
				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[_settings_game.locale.units_velocity].s), &tmp_params, last);
 
				buff = FormatString(buff, GetStringPtr(_units_velocity[units].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())};