Changeset - r17286:b2895c5ab812
[Not reviewed]
master
0 1 0
rubidium - 13 years ago 2011-02-09 13:05:04
rubidium@openttd.org
(svn r22036) -Codechange: rename units to match coding style, and give conversion variables a more descriptive name
1 file changed with 45 insertions and 45 deletions:
0 comments (0 inline, 0 general)
src/strings.cpp
Show inline comments
 
@@ -561,42 +561,42 @@ struct UnitConversion {
 
	/**
 
	 * Convert the displayed value back into a value of OpenTTD's internal unit.
 
	 * @param input The input to convert.
 
	 * @return The converted value.
 
	 */
 
	int FromDisplay(int input) const
 
	{
 
		return ((input << this->shift) + this->multiplier / 2) / this->multiplier;
 
	}
 
};
 

	
 
struct Units {
 
	UnitConversion s;  ///< Multiplier for velocity
 
	StringID velocity; ///< String for velocity
 
	UnitConversion p;  ///< Multiplier for power
 
	StringID power;    ///< String for velocity
 
	UnitConversion w;  ///< Multiplier for weight
 
	StringID s_weight; ///< Short string for weight
 
	StringID l_weight; ///< Long string for weight
 
	UnitConversion v;  ///< Multiplier for volume
 
	StringID s_volume; ///< Short string for volume
 
	StringID l_volume; ///< Long string for volume
 
	UnitConversion f;  ///< Multiplier for force
 
	StringID force;    ///< String for force
 
	UnitConversion h;  ///< Multiplier for height
 
	StringID height;   ///< String for height
 
	UnitConversion c_velocity; ///< Conversion for velocity
 
	StringID velocity;         ///< String for velocity
 
	UnitConversion c_power;    ///< Conversion for power
 
	StringID power;            ///< String for velocity
 
	UnitConversion c_weight;   ///< Conversion for weight
 
	StringID s_weight;         ///< Short string for weight
 
	StringID l_weight;         ///< Long string for weight
 
	UnitConversion c_volume;   ///< Conversion for volume
 
	StringID s_volume;         ///< Short string for volume
 
	StringID l_volume;         ///< Long string for volume
 
	UnitConversion c_force;    ///< Conversion for force
 
	StringID force;            ///< String for force
 
	UnitConversion c_height;   ///< Conversion for height
 
	StringID height;           ///< String for height
 
};
 

	
 
/* Unit conversions */
 
static const Units units[] = {
 
static const Units _units[] = {
 
	{ // Imperial (Original, mph, hp, metric ton, litre, kN, ft)
 
		{   1,  0}, STR_UNITS_VELOCITY_IMPERIAL,
 
		{   1,  0}, STR_UNITS_POWER_IMPERIAL,
 
		{   1,  0}, STR_UNITS_WEIGHT_SHORT_METRIC, STR_UNITS_WEIGHT_LONG_METRIC,
 
		{1000,  0}, STR_UNITS_VOLUME_SHORT_METRIC, STR_UNITS_VOLUME_LONG_METRIC,
 
		{   1,  0}, STR_UNITS_FORCE_SI,
 
		{   3,  0}, STR_UNITS_HEIGHT_IMPERIAL,
 
	},
 
	{ // Metric (km/h, hp, metric ton, litre, kN, metre)
 
		{ 103,  6}, STR_UNITS_VELOCITY_METRIC,
 
		{   1,  0}, STR_UNITS_POWER_METRIC,
 
		{   1,  0}, STR_UNITS_WEIGHT_SHORT_METRIC, STR_UNITS_WEIGHT_LONG_METRIC,
 
@@ -612,35 +612,35 @@ static const Units units[] = {
 
		{   1,  0}, STR_UNITS_FORCE_SI,
 
		{   1,  0}, STR_UNITS_HEIGHT_SI,
 
	},
 
};
 

	
 
/**
 
 * Convert the given (internal) speed to the display speed.
 
 * @param speed the speed to convert
 
 * @return the converted speed.
 
 */
 
uint ConvertSpeedToDisplaySpeed(uint speed)
 
{
 
	return units[_settings_game.locale.units].s.ToDisplay(speed);
 
	return _units[_settings_game.locale.units].c_velocity.ToDisplay(speed);
 
}
 

	
 
/**
 
 * Convert the given display speed to the (internal) speed.
 
 * @param speed the speed to convert
 
 * @return the converted speed.
 
 */
 
uint ConvertDisplaySpeedToSpeed(uint speed)
 
{
 
	return units[_settings_game.locale.units].s.FromDisplay(speed);
 
	return _units[_settings_game.locale.units].c_velocity.FromDisplay(speed);
 
}
 

	
 
/**
 
 * Parse most format codes within a string and write the result to a buffer.
 
 * @param buff  The buffer to write the final string to.
 
 * @param str   The original string with format codes.
 
 * @param argv  Pointer to an array with extra arguments used by various string codes.
 
 * @param argve Pointer to just past the end of the argv array.
 
 * @param casei
 
 * @param last  Pointer to just past the end of the buff array.
 
 * @param argt  Pointer to an array with the string codes used to parse the argv array.
 
 * @param dry_run True when the argt array is not yet initialized.
 
@@ -724,66 +724,66 @@ static char *FormatString(char *buff, co
 
			}
 

	
 
			case SCC_DATE_LONG: // {DATE_LONG}
 
				buff = FormatYmdString(buff, GetInt32(&argv, argve, &argt, SCC_DATE_LONG), modifier, last);
 
				break;
 

	
 
			case SCC_DATE_SHORT: // {DATE_SHORT}
 
				buff = FormatMonthAndYear(buff, GetInt32(&argv, argve, &argt, SCC_DATE_SHORT), modifier, last);
 
				break;
 

	
 
			case SCC_VELOCITY: { // {VELOCITY}
 
				int64 args[1];
 
				assert(_settings_game.locale.units < lengthof(units));
 
				assert(_settings_game.locale.units < lengthof(_units));
 
				args[0] = ConvertSpeedToDisplaySpeed(GetInt32(&argv, argve, &argt, SCC_VELOCITY) * 10 / 16);
 
				buff = FormatString(buff, GetStringPtr(units[_settings_game.locale.units].velocity), args, endof(args), modifier >> 24, last);
 
				buff = FormatString(buff, GetStringPtr(_units[_settings_game.locale.units].velocity), args, endof(args), modifier >> 24, last);
 
				modifier = 0;
 
				break;
 
			}
 

	
 
			case SCC_HEIGHT: { // {HEIGHT}
 
				int64 args[1] = {units[_settings_game.locale.units].h.ToDisplay(GetInt32(&argv, argve, &argt))};
 
				buff = FormatString(buff, GetStringPtr(units[_settings_game.locale.units].height), args, endof(args), modifier >> 24, last);
 
				int64 args[1] = {_units[_settings_game.locale.units].c_height.ToDisplay(GetInt32(&argv, argve, &argt))};
 
				buff = FormatString(buff, GetStringPtr(_units[_settings_game.locale.units].height), args, endof(args), modifier >> 24, last);
 
				modifier = 0;
 
				break;
 
			}
 

	
 
			case SCC_CURRENCY_COMPACT: // {CURRCOMPACT}
 
				buff = FormatGenericCurrency(buff, _currency, GetInt64(&argv, argve, &argt), true, last);
 
				break;
 

	
 
			case SCC_REVISION: // {REV}
 
				buff = strecpy(buff, _openttd_revision, last);
 
				break;
 

	
 
			case SCC_CARGO_SHORT: { // {SHORTCARGO}
 
				/* Short description of cargotypes. Layout:
 
				 * 8-bit = cargo type
 
				 * 16-bit = cargo count */
 
				StringID cargo_str = CargoSpec::Get(GetInt32(&argv, argve, &argt, SCC_CARGO_SHORT))->units_volume;
 
				switch (cargo_str) {
 
					case STR_TONS: {
 
						int64 args[1];
 
						assert(_settings_game.locale.units < lengthof(units));
 
						args[0] = units[_settings_game.locale.units].w.ToDisplay(GetInt32(&argv, argve, &argt));
 
						buff = FormatString(buff, GetStringPtr(units[_settings_game.locale.units].l_weight), args, endof(args), modifier >> 24, last);
 
						assert(_settings_game.locale.units < lengthof(_units));
 
						args[0] = _units[_settings_game.locale.units].c_weight.ToDisplay(GetInt32(&argv, argve, &argt));
 
						buff = FormatString(buff, GetStringPtr(_units[_settings_game.locale.units].l_weight), args, endof(args), modifier >> 24, last);
 
						modifier = 0;
 
						break;
 
					}
 

	
 
					case STR_LITERS: {
 
						int64 args[1];
 
						assert(_settings_game.locale.units < lengthof(units));
 
						args[0] = units[_settings_game.locale.units].v.ToDisplay(GetInt32(&argv, argve, &argt));
 
						buff = FormatString(buff, GetStringPtr(units[_settings_game.locale.units].l_volume), args, endof(args), modifier >> 24, last);
 
						assert(_settings_game.locale.units < lengthof(_units));
 
						args[0] = _units[_settings_game.locale.units].c_volume.ToDisplay(GetInt32(&argv, argve, &argt));
 
						buff = FormatString(buff, GetStringPtr(_units[_settings_game.locale.units].l_volume), args, endof(args), modifier >> 24, last);
 
						modifier = 0;
 
						break;
 
					}
 

	
 
					default:
 
						buff = GetStringWithArgs(buff, cargo_str, argv++, argve, last, argt++);
 
						break;
 
				}
 
				break;
 
			}
 

	
 
			case SCC_STRING1: { // {STRING1}
 
@@ -849,27 +849,27 @@ static char *FormatString(char *buff, co
 
				assert(i != NULL);
 

	
 
				/* First print the town name and the industry type name. */
 
				args[0] = i->town->index;
 
				args[1] = GetIndustrySpec(i->type)->name;
 
				buff = FormatString(buff, GetStringPtr(STR_FORMAT_INDUSTRY_NAME), args, endof(args), modifier >> 24, last);
 
				modifier = 0;
 
				break;
 
			}
 

	
 
			case SCC_VOLUME: { // {VOLUME}
 
				int64 args[1];
 
				assert(_settings_game.locale.units < lengthof(units));
 
				args[0] = units[_settings_game.locale.units].v.ToDisplay(GetInt32(&argv, argve, &argt, SCC_VOLUME));
 
				buff = FormatString(buff, GetStringPtr(units[_settings_game.locale.units].l_volume), args, endof(args), modifier >> 24, last);
 
				assert(_settings_game.locale.units < lengthof(_units));
 
				args[0] = _units[_settings_game.locale.units].c_volume.ToDisplay(GetInt32(&argv, argve, &argt, SCC_VOLUME));
 
				buff = FormatString(buff, GetStringPtr(_units[_settings_game.locale.units].l_volume), args, endof(args), modifier >> 24, last);
 
				modifier = 0;
 
				break;
 
			}
 

	
 
			case SCC_GENDER_LIST: { // {G 0 Der Die Das}
 
				/* First read the meta data from the language file. */
 
				byte offset = (byte)*str++;
 
				assert(argv_orig + offset < argve);
 
				int gender = 0;
 
				if (!dry_run && argt != NULL && argt_orig[offset] != 0) {
 
					/* Now we need to figure out what text to resolve, i.e.
 
					 * what do we need to draw? So get the actual raw string
 
@@ -907,63 +907,63 @@ static char *FormatString(char *buff, co
 
			}
 

	
 
			case SCC_CARGO: { // {CARGO}
 
				/* First parameter is cargo type, second parameter is cargo count */
 
				CargoID cargo = GetInt32(&argv, argve, &argt, SCC_CARGO);
 
				StringID cargo_str = (cargo == CT_INVALID) ? STR_QUANTITY_N_A : CargoSpec::Get(cargo)->quantifier;
 
				buff = GetStringWithArgs(buff, cargo_str, argv++, argve, last);
 
				break;
 
			}
 

	
 
			case SCC_POWER: { // {POWER}
 
				int64 args[1];
 
				assert(_settings_game.locale.units < lengthof(units));
 
				args[0] = units[_settings_game.locale.units].p.ToDisplay(GetInt32(&argv, argve, &argt));
 
				buff = FormatString(buff, GetStringPtr(units[_settings_game.locale.units].power), args, endof(args), modifier >> 24, last);
 
				assert(_settings_game.locale.units < lengthof(_units));
 
				args[0] = _units[_settings_game.locale.units].c_power.ToDisplay(GetInt32(&argv, argve, &argt));
 
				buff = FormatString(buff, GetStringPtr(_units[_settings_game.locale.units].power), args, endof(args), modifier >> 24, last);
 
				modifier = 0;
 
				break;
 
			}
 

	
 
			case SCC_VOLUME_SHORT: { // {VOLUME_S}
 
				int64 args[1];
 
				assert(_settings_game.locale.units < lengthof(units));
 
				args[0] = units[_settings_game.locale.units].v.ToDisplay(GetInt32(&argv, argve, &argt));
 
				buff = FormatString(buff, GetStringPtr(units[_settings_game.locale.units].s_volume), args, endof(args), modifier >> 24, last);
 
				assert(_settings_game.locale.units < lengthof(_units));
 
				args[0] = _units[_settings_game.locale.units].c_volume.ToDisplay(GetInt32(&argv, argve, &argt));
 
				buff = FormatString(buff, GetStringPtr(_units[_settings_game.locale.units].s_volume), args, endof(args), modifier >> 24, last);
 
				modifier = 0;
 
				break;
 
			}
 

	
 
			case SCC_WEIGHT: { // {WEIGHT}
 
				int64 args[1];
 
				assert(_settings_game.locale.units < lengthof(units));
 
				args[0] = units[_settings_game.locale.units].w.ToDisplay(GetInt32(&argv, argve, &argt, SCC_WEIGHT));
 
				buff = FormatString(buff, GetStringPtr(units[_settings_game.locale.units].l_weight), args, endof(args), modifier >> 24, last);
 
				assert(_settings_game.locale.units < lengthof(_units));
 
				args[0] = _units[_settings_game.locale.units].c_weight.ToDisplay(GetInt32(&argv, argve, &argt, SCC_WEIGHT));
 
				buff = FormatString(buff, GetStringPtr(_units[_settings_game.locale.units].l_weight), args, endof(args), modifier >> 24, last);
 
				modifier = 0;
 
				break;
 
			}
 

	
 
			case SCC_WEIGHT_SHORT: { // {WEIGHT_S}
 
				int64 args[1];
 
				assert(_settings_game.locale.units < lengthof(units));
 
				args[0] = units[_settings_game.locale.units].w.ToDisplay(GetInt32(&argv, argve, &argt));
 
				buff = FormatString(buff, GetStringPtr(units[_settings_game.locale.units].s_weight), args, endof(args), modifier >> 24, last);
 
				assert(_settings_game.locale.units < lengthof(_units));
 
				args[0] = _units[_settings_game.locale.units].c_weight.ToDisplay(GetInt32(&argv, argve, &argt));
 
				buff = FormatString(buff, GetStringPtr(_units[_settings_game.locale.units].s_weight), args, endof(args), modifier >> 24, last);
 
				modifier = 0;
 
				break;
 
			}
 

	
 
			case SCC_FORCE: { // {FORCE}
 
				int64 args[1];
 
				assert(_settings_game.locale.units < lengthof(units));
 
				args[0] = units[_settings_game.locale.units].f.ToDisplay(GetInt32(&argv, argve, &argt));
 
				buff = FormatString(buff, GetStringPtr(units[_settings_game.locale.units].force), args, endof(args), modifier >> 24, last);
 
				assert(_settings_game.locale.units < lengthof(_units));
 
				args[0] = _units[_settings_game.locale.units].c_force.ToDisplay(GetInt32(&argv, argve, &argt));
 
				buff = FormatString(buff, GetStringPtr(_units[_settings_game.locale.units].force), args, endof(args), modifier >> 24, last);
 
				modifier = 0;
 
				break;
 
			}
 

	
 
			/* This sets up the gender for the string.
 
			 * We just ignore this one. It's used in {G 0 Der Die Das} to determine the case. */
 
			case SCC_GENDER_INDEX: // {GENDER 0}
 
				if (_keep_gender_data) {
 
					buff += Utf8Encode(buff, SCC_GENDER_INDEX);
 
					*buff++ = *str++;
 
				} else {
 
					str++;
0 comments (0 inline, 0 general)