2005-07-24 14:12:37 +00:00
/* $Id$ */
2009-08-21 20:21:05 +00:00
/*
* This file is part of OpenTTD .
* OpenTTD is free software ; you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation , version 2.
* OpenTTD is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
* See the GNU General Public License for more details . You should have received a copy of the GNU General Public License along with OpenTTD . If not , see < http : //www.gnu.org/licenses/>.
*/
2008-05-06 15:11:33 +00:00
/** @file strings.cpp Handling of translated strings. */
2007-04-04 01:35:16 +00:00
2004-08-09 17:04:08 +00:00
# include "stdafx.h"
2005-08-06 14:59:54 +00:00
# include "currency.h"
2008-03-31 00:06:17 +00:00
# include "station_base.h"
2004-08-09 17:04:08 +00:00
# include "town.h"
2004-11-15 19:25:59 +00:00
# include "screenshot.h"
2009-07-22 10:18:19 +00:00
# include "waypoint_base.h"
2005-07-15 18:30:13 +00:00
# include "industry.h"
2006-04-21 03:00:20 +00:00
# include "newgrf_text.h"
2008-08-31 10:50:05 +00:00
# include "fileio_func.h"
2007-05-19 09:40:18 +00:00
# include "group.h"
2008-03-31 07:25:49 +00:00
# include "signs_base.h"
2009-01-31 20:16:06 +00:00
# include "cargotype.h"
2007-12-16 18:38:19 +00:00
# include "fontcache.h"
# include "gui.h"
2007-12-21 19:49:27 +00:00
# include "strings_func.h"
2008-05-04 21:53:36 +00:00
# include "rev.h"
2007-12-22 21:01:49 +00:00
# include "core/endian_func.hpp"
2007-12-26 13:50:40 +00:00
# include "date_func.h"
2007-12-27 13:35:39 +00:00
# include "vehicle_base.h"
2008-04-29 21:31:29 +00:00
# include "engine_base.h"
2008-11-24 11:20:07 +00:00
# include "strgen/strgen.h"
2009-09-22 13:54:54 +00:00
# include "townname_func.h"
2010-01-15 16:41:15 +00:00
# include "string_func.h"
# include "company_base.h"
2010-03-28 12:25:09 +00:00
# include "industrytype.h"
# include "smallmap_gui.h"
2004-08-09 17:04:08 +00:00
2008-01-13 01:21:35 +00:00
# include "table/strings.h"
# include "table/control_codes.h"
2006-08-04 23:45:20 +00:00
2009-07-25 07:49:49 +00:00
DynamicLanguages _dynlang ; ///< Language information of the program.
uint64 _decode_parameters [ 20 ] ; ///< Global array of string parameters. To access, use #SetDParam.
2005-07-26 19:04:19 +00:00
2008-11-24 20:51:55 +00:00
static char * StationGetSpecialString ( char * buff , int x , const char * last ) ;
static char * GetSpecialTownNameString ( char * buff , int ind , uint32 seed , const char * last ) ;
2009-05-24 20:29:04 +00:00
static char * GetSpecialNameString ( char * buff , int ind , int64 * argv , const char * last ) ;
2004-08-09 17:04:08 +00:00
2009-05-24 20:29:04 +00:00
static char * FormatString ( char * buff , const char * str , int64 * argv , uint casei , const char * last ) ;
2004-08-09 17:04:08 +00:00
2008-11-24 11:20:07 +00:00
struct LanguagePack : public LanguagePackHeader {
2009-06-01 11:49:46 +00:00
char data [ ] ; // list of strings
2007-03-07 12:11:48 +00:00
} ;
2005-02-06 14:47:56 +00:00
static char * * _langpack_offs ;
static LanguagePack * _langpack ;
2007-04-04 01:35:16 +00:00
static uint _langtab_num [ 32 ] ; // Offset into langpack offs
2005-02-06 14:47:56 +00:00
static uint _langtab_start [ 32 ] ; // Offset into langpack offs
2010-02-20 23:42:11 +00:00
static bool _keep_gender_data = false ; ///< Should we retain the gender data in the current string?
2004-08-09 17:04:08 +00:00
2007-04-04 01:35:16 +00:00
/** Read an int64 from the argv array. */
2009-05-24 20:29:04 +00:00
static inline int64 GetInt64 ( int64 * * argv )
2005-07-15 14:53:44 +00:00
{
assert ( argv ) ;
2007-06-21 17:25:17 +00:00
return * ( * argv ) + + ;
2005-07-15 14:53:44 +00:00
}
2007-04-04 01:35:16 +00:00
/** Read an int32 from the argv array. */
2009-05-24 20:29:04 +00:00
static inline int32 GetInt32 ( int64 * * argv )
2005-07-15 14:53:44 +00:00
{
2007-06-21 17:25:17 +00:00
return ( int32 ) GetInt64 ( argv ) ;
2005-07-15 14:53:44 +00:00
}
2007-04-04 01:35:16 +00:00
/** Read an array from the argv array. */
2009-05-24 20:29:04 +00:00
static inline int64 * GetArgvPtr ( int64 * * argv , int n )
2005-07-15 14:53:44 +00:00
{
2009-05-24 20:29:04 +00:00
int64 * result ;
2005-07-15 14:53:44 +00:00
assert ( * argv ) ;
result = * argv ;
( * argv ) + = n ;
return result ;
}
2008-01-29 17:09:00 +00:00
const char * GetStringPtr ( StringID string )
2004-08-09 17:04:08 +00:00
{
2008-01-29 17:09:00 +00:00
switch ( GB ( string , 11 , 5 ) ) {
case 28 : return GetGRFStringPtr ( GB ( string , 0 , 11 ) ) ;
case 29 : return GetGRFStringPtr ( GB ( string , 0 , 11 ) + 0x0800 ) ;
case 30 : return GetGRFStringPtr ( GB ( string , 0 , 11 ) + 0x1000 ) ;
default : return _langpack_offs [ _langtab_start [ string > > 11 ] + ( string & 0x7FF ) ] ;
}
2004-08-09 17:04:08 +00:00
}
2007-04-04 01:35:16 +00:00
/** The highest 8 bits of string contain the "case index".
* These 8 bits will only be set when FormatString wants to print
* the string in a different case . No one else except FormatString
* should set those bits , therefore string CANNOT be StringID , but uint32 .
* @ param buffr
* @ param string
* @ param argv
* @ param last
* @ return a formatted string of char
*/
2009-09-22 13:54:54 +00:00
char * GetStringWithArgs ( char * buffr , uint string , int64 * argv , const char * last )
2004-08-09 17:04:08 +00:00
{
2007-10-30 18:32:26 +00:00
if ( GB ( string , 0 , 16 ) = = 0 ) return GetStringWithArgs ( buffr , STR_UNDEFINED , argv , last ) ;
2005-07-20 15:29:28 +00:00
uint index = GB ( string , 0 , 11 ) ;
uint tab = GB ( string , 11 , 5 ) ;
2004-08-09 17:04:08 +00:00
2005-02-06 16:56:04 +00:00
switch ( tab ) {
case 4 :
2005-07-15 14:53:44 +00:00
if ( index > = 0xC0 )
2006-10-21 23:31:34 +00:00
return GetSpecialTownNameString ( buffr , index - 0xC0 , GetInt32 ( & argv ) , last ) ;
2005-02-06 16:56:04 +00:00
break ;
2004-08-09 17:04:08 +00:00
2005-02-06 16:56:04 +00:00
case 14 :
2005-07-15 14:53:44 +00:00
if ( index > = 0xE4 )
2008-09-30 20:39:50 +00:00
return GetSpecialNameString ( buffr , index - 0xE4 , argv , last ) ;
2005-02-06 16:56:04 +00:00
break ;
2004-08-09 17:04:08 +00:00
2005-02-06 16:56:04 +00:00
case 15 :
2008-01-29 17:09:00 +00:00
/* Old table for custom names. This is no longer used */
error ( " Incorrect conversion of custom name string. " ) ;
2004-08-09 17:04:08 +00:00
2006-10-03 09:25:42 +00:00
case 26 :
/* Include string within newgrf text (format code 81) */
2007-11-19 21:02:30 +00:00
if ( HasBit ( index , 10 ) ) {
2006-10-03 09:25:42 +00:00
StringID string = GetGRFStringID ( 0 , 0xD000 + GB ( index , 0 , 10 ) ) ;
2006-10-21 23:31:34 +00:00
return GetStringWithArgs ( buffr , string , argv , last ) ;
2006-10-03 09:25:42 +00:00
}
break ;
2006-04-21 03:00:20 +00:00
case 28 :
2008-01-29 17:09:00 +00:00
return FormatString ( buffr , GetGRFStringPtr ( index ) , argv , 0 , last ) ;
2006-04-21 03:00:20 +00:00
case 29 :
2008-01-29 17:09:00 +00:00
return FormatString ( buffr , GetGRFStringPtr ( index + 0x0800 ) , argv , 0 , last ) ;
2006-04-21 03:00:20 +00:00
case 30 :
2008-01-29 17:09:00 +00:00
return FormatString ( buffr , GetGRFStringPtr ( index + 0x1000 ) , argv , 0 , last ) ;
2006-04-21 03:00:20 +00:00
2005-07-15 14:53:44 +00:00
case 31 :
2008-07-17 13:47:04 +00:00
NOT_REACHED ( ) ;
2004-08-09 17:04:08 +00:00
}
2005-11-14 19:48:04 +00:00
if ( index > = _langtab_num [ tab ] ) {
2009-07-24 09:14:36 +00:00
error ( " String 0x%X is invalid. You are probably using an old version of the .lng file. \n " , string ) ;
2005-11-14 19:48:04 +00:00
}
2004-08-09 17:04:08 +00:00
2006-10-21 23:31:34 +00:00
return FormatString ( buffr , GetStringPtr ( GB ( string , 0 , 16 ) ) , argv , GB ( string , 24 , 8 ) , last ) ;
2005-07-15 14:53:44 +00:00
}
2008-11-24 20:51:55 +00:00
char * GetString ( char * buffr , StringID string , const char * last )
2005-07-15 14:53:44 +00:00
{
2007-06-21 17:25:17 +00:00
return GetStringWithArgs ( buffr , string , ( int64 * ) _decode_parameters , last ) ;
2004-08-09 17:04:08 +00:00
}
2005-07-15 14:53:44 +00:00
2006-11-16 22:05:33 +00:00
char * InlineString ( char * buf , StringID string )
{
buf + = Utf8Encode ( buf , SCC_STRING_ID ) ;
buf + = Utf8Encode ( buf , string ) ;
return buf ;
}
2007-04-04 01:35:16 +00:00
/** This function is used to "bind" a C string to a OpenTTD dparam slot.
* @ param n slot of the string
* @ param str string to bind
*/
2005-07-14 09:43:59 +00:00
void SetDParamStr ( uint n , const char * str )
{
2008-07-31 22:55:12 +00:00
SetDParam ( n , ( uint64 ) ( size_t ) str ) ;
2005-07-14 09:43:59 +00:00
}
2009-07-25 07:49:49 +00:00
/**
* Shift the string parameters in the global string parameter array by \ a amount positions , making room at the beginning .
* @ param amount Number of positions to shift .
*/
2008-09-28 13:50:11 +00:00
void InjectDParam ( uint amount )
2004-08-09 17:04:08 +00:00
{
2008-09-28 13:50:11 +00:00
assert ( ( uint ) amount < lengthof ( _decode_parameters ) ) ;
2007-06-21 19:08:47 +00:00
memmove ( _decode_parameters + amount , _decode_parameters , sizeof ( _decode_parameters ) - amount * sizeof ( uint64 ) ) ;
2004-08-09 17:04:08 +00:00
}
2009-11-19 13:53:54 +00:00
static char * FormatNumber ( char * buff , int64 number , const char * last , const char * separator , int zerofill_from = 19 )
2004-08-09 17:04:08 +00:00
{
2007-07-29 12:13:22 +00:00
uint64 divisor = 10000000000000000000ULL ;
2004-08-09 17:04:08 +00:00
if ( number < 0 ) {
2009-08-06 20:21:53 +00:00
buff + = seprintf ( buff , last , " - " ) ;
2004-08-09 17:04:08 +00:00
number = - number ;
}
2009-11-19 13:53:54 +00:00
uint64 num = number ;
uint64 tot = 0 ;
for ( int i = 0 ; i < 20 ; i + + ) {
uint64 quot = 0 ;
2007-07-29 12:13:22 +00:00
if ( num > = divisor ) {
quot = num / divisor ;
num = num % divisor ;
2004-08-09 17:04:08 +00:00
}
2009-11-19 13:53:54 +00:00
if ( tot | = quot | | i > = zerofill_from ) {
2009-04-30 12:11:51 +00:00
buff + = seprintf ( buff , last , " %i " , ( int ) quot ) ;
2009-04-23 16:17:10 +00:00
if ( ( i % 3 ) = = 1 & & i ! = 19 ) buff = strecpy ( buff , separator , last ) ;
2004-08-09 17:04:08 +00:00
}
2007-07-29 12:13:22 +00:00
divisor / = 10 ;
2004-08-09 17:04:08 +00:00
}
2005-02-06 11:23:41 +00:00
* buff = ' \0 ' ;
2004-08-09 17:04:08 +00:00
return buff ;
}
2009-04-23 16:17:10 +00:00
static char * FormatCommaNumber ( char * buff , int64 number , const char * last )
2004-08-09 17:04:08 +00:00
{
2009-04-23 21:05:00 +00:00
const char * separator = _settings_game . locale . digit_group_separator ;
if ( separator = = NULL ) separator = _langpack - > digit_group_separator ;
return FormatNumber ( buff , number , last , separator ) ;
2009-04-23 16:17:10 +00:00
}
2004-08-09 17:04:08 +00:00
2009-04-23 16:17:10 +00:00
static char * FormatNoCommaNumber ( char * buff , int64 number , const char * last )
{
return FormatNumber ( buff , number , last , " " ) ;
2004-08-09 17:04:08 +00:00
}
2009-11-19 13:53:54 +00:00
static char * FormatZerofillNumber ( char * buff , int64 number , int64 count , const char * last )
{
return FormatNumber ( buff , number , last , " " , 20 - count ) ;
}
2008-09-14 14:31:37 +00:00
static char * FormatHexNumber ( char * buff , int64 number , const char * last )
{
2008-10-28 14:42:31 +00:00
return buff + seprintf ( buff , last , " 0x%x " , ( uint32 ) number ) ;
2008-09-14 14:31:37 +00:00
}
2004-08-09 17:04:08 +00:00
2009-01-16 12:59:47 +00:00
/**
* Format a given number as a number of bytes with the SI prefix .
* @ param buff the buffer to write to
* @ param number the number of bytes to write down
* @ param last the last element in the buffer
* @ return till where we wrote
*/
static char * FormatBytes ( char * buff , int64 number , const char * last )
{
assert ( number > = 0 ) ;
2009-08-05 16:14:40 +00:00
/* 1 2^10 2^20 2^30 2^40 2^50 2^60 */
const char * const iec_prefixes [ ] = { " " , " Ki " , " Mi " , " Gi " , " Ti " , " Pi " , " Ei " } ;
2009-01-16 14:02:40 +00:00
uint id = 1 ;
2009-01-16 12:59:47 +00:00
while ( number > = 1024 * 1024 ) {
number / = 1024 ;
id + + ;
}
2009-08-12 01:28:11 +00:00
const char * decimal_separator = _settings_game . locale . digit_decimal_separator ;
if ( decimal_separator = = NULL ) decimal_separator = _langpack - > digit_decimal_separator ;
2009-01-16 12:59:47 +00:00
if ( number < 1024 ) {
id = 0 ;
buff + = seprintf ( buff , last , " %i " , ( int ) number ) ;
} else if ( number < 1024 * 10 ) {
2009-08-12 01:28:11 +00:00
buff + = seprintf ( buff , last , " %i%s%02i " , ( int ) number / 1024 , decimal_separator , ( int ) ( number % 1024 ) * 100 / 1024 ) ;
2009-01-16 12:59:47 +00:00
} else if ( number < 1024 * 100 ) {
2009-08-12 01:28:11 +00:00
buff + = seprintf ( buff , last , " %i%s%01i " , ( int ) number / 1024 , decimal_separator , ( int ) ( number % 1024 ) * 10 / 1024 ) ;
2009-01-16 12:59:47 +00:00
} else {
assert ( number < 1024 * 1024 ) ;
buff + = seprintf ( buff , last , " %i " , ( int ) number / 1024 ) ;
}
2009-08-05 16:14:40 +00:00
assert ( id < lengthof ( iec_prefixes ) ) ;
buff + = seprintf ( buff , last , " %sB " , iec_prefixes [ id ] ) ;
2009-01-16 12:59:47 +00:00
return buff ;
}
2008-11-24 20:51:55 +00:00
static char * FormatYmdString ( char * buff , Date date , const char * last )
2004-08-09 17:04:08 +00:00
{
YearMonthDay ymd ;
2006-08-15 16:55:40 +00:00
ConvertDateToYMD ( date , & ymd ) ;
2004-08-09 17:04:08 +00:00
2009-04-21 23:40:56 +00:00
int64 args [ 3 ] = { ymd . day + STR_ORDINAL_NUMBER_1ST - 1 , STR_MONTH_ABBREV_JAN + ymd . month , ymd . year } ;
2009-07-20 11:21:57 +00:00
return FormatString ( buff , GetStringPtr ( STR_FORMAT_DATE_LONG ) , args , 0 , last ) ;
2004-08-09 17:04:08 +00:00
}
2008-11-24 20:51:55 +00:00
static char * FormatMonthAndYear ( char * buff , Date date , const char * last )
2004-08-09 17:04:08 +00:00
{
YearMonthDay ymd ;
2006-08-15 16:55:40 +00:00
ConvertDateToYMD ( date , & ymd ) ;
2004-08-09 17:04:08 +00:00
2007-06-21 17:25:17 +00:00
int64 args [ 2 ] = { STR_MONTH_JAN + ymd . month , ymd . year } ;
2009-07-20 11:21:57 +00:00
return FormatString ( buff , GetStringPtr ( STR_FORMAT_DATE_SHORT ) , args , 0 , last ) ;
2004-08-09 17:04:08 +00:00
}
2009-01-03 00:56:16 +00:00
static char * FormatTinyOrISODate ( char * buff , Date date , StringID str , const char * last )
2005-01-22 23:13:20 +00:00
{
YearMonthDay ymd ;
2006-08-15 16:55:40 +00:00
ConvertDateToYMD ( date , & ymd ) ;
2005-01-22 23:13:20 +00:00
2007-02-26 00:36:57 +00:00
char day [ 3 ] ;
char month [ 3 ] ;
/* We want to zero-pad the days and months */
snprintf ( day , lengthof ( day ) , " %02i " , ymd . day ) ;
snprintf ( month , lengthof ( month ) , " %02i " , ymd . month + 1 ) ;
2008-07-31 22:55:12 +00:00
int64 args [ 3 ] = { ( int64 ) ( size_t ) day , ( int64 ) ( size_t ) month , ymd . year } ;
2009-01-03 00:56:16 +00:00
return FormatString ( buff , GetStringPtr ( str ) , args , 0 , last ) ;
2005-01-22 23:13:20 +00:00
}
2008-11-24 20:51:55 +00:00
static char * FormatGenericCurrency ( char * buff , const CurrencySpec * spec , Money number , bool compact , const char * last )
2004-08-09 17:04:08 +00:00
{
2007-10-20 14:51:09 +00:00
/* We are going to make number absolute for printing, so
* keep this piece of data as we need it later on */
bool negative = number < 0 ;
const char * multiplier = " " ;
2004-08-09 17:04:08 +00:00
2007-10-20 14:51:09 +00:00
number * = spec - > rate ;
2004-08-09 17:04:08 +00:00
2007-04-04 01:35:16 +00:00
/* convert from negative */
2005-02-06 11:23:41 +00:00
if ( number < 0 ) {
2007-08-07 15:20:31 +00:00
if ( buff + Utf8CharLen ( SCC_RED ) > last ) return buff ;
buff + = Utf8Encode ( buff , SCC_RED ) ;
2006-10-21 23:31:34 +00:00
buff = strecpy ( buff , " - " , last ) ;
2005-02-06 11:23:41 +00:00
number = - number ;
}
2004-09-10 19:02:27 +00:00
2006-09-15 02:52:17 +00:00
/* Add prefix part, folowing symbol_pos specification.
* Here , it can can be either 0 ( prefix ) or 2 ( both prefix anf suffix ) .
* The only remaining value is 1 ( suffix ) , so everything that is not 1 */
2006-10-21 23:31:34 +00:00
if ( spec - > symbol_pos ! = 1 ) buff = strecpy ( buff , spec - > prefix , last ) ;
2004-08-09 17:04:08 +00:00
2007-04-04 01:35:16 +00:00
/* for huge numbers, compact the number into k or M */
2004-08-09 17:04:08 +00:00
if ( compact ) {
if ( number > = 1000000000 ) {
number = ( number + 500000 ) / 1000000 ;
2006-10-21 23:31:34 +00:00
multiplier = " M " ;
2004-08-09 17:04:08 +00:00
} else if ( number > = 1000000 ) {
number = ( number + 500 ) / 1000 ;
2006-10-21 23:31:34 +00:00
multiplier = " k " ;
2004-09-10 19:02:27 +00:00
}
2004-08-09 17:04:08 +00:00
}
2004-09-10 19:02:27 +00:00
2009-04-23 21:05:00 +00:00
const char * separator = _settings_game . locale . digit_group_separator_currency ;
2009-04-25 20:16:56 +00:00
if ( separator = = NULL & & ! StrEmpty ( _currency - > separator ) ) separator = _currency - > separator ;
2009-04-23 21:05:00 +00:00
if ( separator = = NULL ) separator = _langpack - > digit_group_separator_currency ;
buff = FormatNumber ( buff , number , last , separator ) ;
2006-10-21 23:31:34 +00:00
buff = strecpy ( buff , multiplier , last ) ;
2004-08-09 17:04:08 +00:00
2006-09-15 02:52:17 +00:00
/* Add suffix part, folowing symbol_pos specification.
* Here , it can can be either 1 ( suffix ) or 2 ( both prefix anf suffix ) .
* The only remaining value is 1 ( prefix ) , so everything that is not 0 */
2006-10-21 23:31:34 +00:00
if ( spec - > symbol_pos ! = 0 ) buff = strecpy ( buff , spec - > suffix , last ) ;
2004-08-09 17:04:08 +00:00
2007-10-20 14:51:09 +00:00
if ( negative ) {
2007-08-07 15:20:31 +00:00
if ( buff + Utf8CharLen ( SCC_PREVIOUS_COLOUR ) > last ) return buff ;
buff + = Utf8Encode ( buff , SCC_PREVIOUS_COLOUR ) ;
* buff = ' \0 ' ;
}
2004-08-09 17:04:08 +00:00
return buff ;
}
2008-03-25 19:56:16 +00:00
static int DeterminePluralForm ( int64 count )
2005-07-16 17:12:32 +00:00
{
2007-04-04 01:35:16 +00:00
/* The absolute value determines plurality */
2008-03-25 19:56:16 +00:00
uint64 n = abs ( count ) ;
2005-07-16 17:12:32 +00:00
2006-02-01 07:36:15 +00:00
switch ( _langpack - > plural_form ) {
2008-03-25 19:56:16 +00:00
default :
NOT_REACHED ( ) ;
/* Two forms, singular used for one only
* Used in :
* Danish , Dutch , English , German , Norwegian , Swedish , Estonian , Finnish ,
* Greek , Hebrew , Italian , Portuguese , Spanish , Esperanto */
case 0 :
return n ! = 1 ;
/* Only one form
* Used in :
* Hungarian , Japanese , Korean , Turkish */
case 1 :
return 0 ;
/* Two forms, singular used for zero and one
* Used in :
* French , Brazilian Portuguese */
case 2 :
return n > 1 ;
/* Three forms, special case for zero
* Used in :
* Latvian */
case 3 :
return n % 10 = = 1 & & n % 100 ! = 11 ? 0 : n ! = 0 ? 1 : 2 ;
/* Three forms, special case for one and two
* Used in :
* Gaelige ( Irish ) */
case 4 :
return n = = 1 ? 0 : n = = 2 ? 1 : 2 ;
/* Three forms, special case for numbers ending in 1[2-9]
* Used in :
* Lithuanian */
case 5 :
return n % 10 = = 1 & & n % 100 ! = 11 ? 0 : n % 10 > = 2 & & ( n % 100 < 10 | | n % 100 > = 20 ) ? 1 : 2 ;
/* Three forms, special cases for numbers ending in 1 and 2, 3, 4, except those ending in 1[1-4]
* Used in :
2010-03-18 16:34:50 +00:00
* Croatian , Russian , Ukrainian */
2008-03-25 19:56:16 +00:00
case 6 :
return n % 10 = = 1 & & n % 100 ! = 11 ? 0 : n % 10 > = 2 & & n % 10 < = 4 & & ( n % 100 < 10 | | n % 100 > = 20 ) ? 1 : 2 ;
/* Three forms, special case for one and some numbers ending in 2, 3, or 4
* Used in :
* Polish */
case 7 :
return n = = 1 ? 0 : n % 10 > = 2 & & n % 10 < = 4 & & ( n % 100 < 10 | | n % 100 > = 20 ) ? 1 : 2 ;
/* Four forms, special case for one and all numbers ending in 02, 03, or 04
* Used in :
* Slovenian */
case 8 :
return n % 100 = = 1 ? 0 : n % 100 = = 2 ? 1 : n % 100 = = 3 | | n % 100 = = 4 ? 2 : 3 ;
/* Two forms; singular used for everything ending in 1 but not in 11.
* Used in :
* Icelandic */
case 9 :
return n % 10 = = 1 & & n % 100 ! = 11 ? 0 : 1 ;
2009-04-07 00:05:44 +00:00
/* Three forms, special cases for one and 2, 3, or 4
* Used in :
2010-03-18 16:34:50 +00:00
* Czech , Slovak */
2009-04-07 00:05:44 +00:00
case 10 :
return n = = 1 ? 0 : n > = 2 & & n < = 4 ? 1 : 2 ;
2009-07-13 14:42:09 +00:00
/* Two forms, special 'hack' for Korean; singular for numbers ending
* in a consonant and plural for numbers ending in a vowel .
* Korean doesn ' t have the concept of plural , but depending on how a
* number is pronounced it needs another version of a particle .
* As such the plural system is misused to give this distinction .
*/
case 11 :
switch ( n % 10 ) {
case 0 : // yeong
case 1 : // il
case 3 : // sam
case 6 : // yuk
case 7 : // chil
case 8 : // pal
return 0 ;
case 2 : // i
case 4 : // sa
case 5 : // o
case 9 : // gu
return 1 ;
default :
NOT_REACHED ( ) ;
}
2005-07-16 17:12:32 +00:00
}
}
2009-08-06 20:21:53 +00:00
static const char * ParseStringChoice ( const char * b , uint form , char * * dst , const char * last )
2005-07-16 17:12:32 +00:00
{
2009-03-15 00:32:18 +00:00
/* <NUM> {Length of each string} {each string} */
2005-07-16 17:12:32 +00:00
uint n = ( byte ) * b + + ;
2009-08-06 20:21:53 +00:00
uint pos , i , mypos = 0 ;
2006-02-01 07:36:15 +00:00
for ( i = pos = 0 ; i ! = n ; i + + ) {
2005-07-16 17:12:32 +00:00
uint len = ( byte ) * b + + ;
2009-08-06 20:21:53 +00:00
if ( i = = form ) mypos = pos ;
2005-07-16 17:12:32 +00:00
pos + = len ;
}
2009-08-06 20:21:53 +00:00
* dst + = seprintf ( * dst , last , " %s " , b + mypos ) ;
2005-07-16 17:12:32 +00:00
return b + pos ;
}
2007-03-07 12:11:48 +00:00
struct Units {
2006-03-26 21:50:57 +00:00
int s_m ; ///< Multiplier for velocity
int s_s ; ///< Shift for velocity
StringID velocity ; ///< String for velocity
int p_m ; ///< Multiplier for power
int p_s ; ///< Shift for power
StringID power ; ///< String for velocity
int w_m ; ///< Multiplier for weight
int w_s ; ///< Shift for weight
StringID s_weight ; ///< Short string for weight
StringID l_weight ; ///< Long string for weight
int v_m ; ///< Multiplier for volume
int v_s ; ///< Shift for volume
StringID s_volume ; ///< Short string for volume
StringID l_volume ; ///< Long string for volume
2006-04-09 18:25:31 +00:00
int f_m ; ///< Multiplier for force
int f_s ; ///< Shift for force
StringID force ; ///< String for force
2007-03-07 12:11:48 +00:00
} ;
2006-03-26 21:50:57 +00:00
2006-04-09 18:25:31 +00:00
/* Unit conversions */
2006-03-26 21:50:57 +00:00
static const Units units [ ] = {
2006-12-28 17:03:36 +00:00
{ // Imperial (Original, mph, hp, metric ton, litre, kN)
2007-01-30 21:10:04 +00:00
1 , 0 , STR_UNITS_VELOCITY_IMPERIAL ,
2006-03-26 21:50:57 +00:00
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 ,
2006-12-28 17:03:36 +00:00
1 , 0 , STR_UNITS_FORCE_SI ,
2006-03-26 21:50:57 +00:00
} ,
2006-12-28 17:03:36 +00:00
{ // Metric (km/h, hp, metric ton, litre, kN)
2007-01-30 21:10:04 +00:00
103 , 6 , STR_UNITS_VELOCITY_METRIC ,
2006-03-26 21:50:57 +00:00
1 , 0 , STR_UNITS_POWER_METRIC ,
1 , 0 , STR_UNITS_WEIGHT_SHORT_METRIC , STR_UNITS_WEIGHT_LONG_METRIC ,
1000 , 0 , STR_UNITS_VOLUME_SHORT_METRIC , STR_UNITS_VOLUME_LONG_METRIC ,
2006-12-28 17:03:36 +00:00
1 , 0 , STR_UNITS_FORCE_SI ,
2006-03-26 21:50:57 +00:00
} ,
2006-04-09 18:25:31 +00:00
{ // SI (m/s, kilowatt, kilogram, cubic metres, kilonewton)
2007-01-30 21:10:04 +00:00
1831 , 12 , STR_UNITS_VELOCITY_SI ,
2006-03-26 21:50:57 +00:00
764 , 10 , STR_UNITS_POWER_SI ,
1000 , 0 , STR_UNITS_WEIGHT_SHORT_SI , STR_UNITS_WEIGHT_LONG_SI ,
2006-04-09 14:49:45 +00:00
1 , 0 , STR_UNITS_VOLUME_SHORT_SI , STR_UNITS_VOLUME_LONG_SI ,
2006-04-09 18:25:31 +00:00
1 , 0 , STR_UNITS_FORCE_SI ,
2006-03-26 21:50:57 +00:00
} ,
} ;
2005-07-16 17:12:32 +00:00
2008-04-12 10:00:52 +00:00
/**
* Convert the given ( internal ) speed to the display speed .
* @ param speed the speed to convert
* @ return the converted speed .
*/
uint ConvertSpeedToDisplaySpeed ( uint speed )
{
2009-10-04 20:51:50 +00:00
return ( speed * units [ _settings_game . locale . units ] . s_m ) > > units [ _settings_game . locale . units ] . s_s ;
2008-04-12 10:00:52 +00:00
}
/**
* Convert the given display speed to the ( internal ) speed .
* @ param speed the speed to convert
* @ return the converted speed .
*/
uint ConvertDisplaySpeedToSpeed ( uint speed )
{
2008-06-05 11:26:38 +00:00
return ( ( speed < < units [ _settings_game . locale . units ] . s_s ) + units [ _settings_game . locale . units ] . s_m / 2 ) / units [ _settings_game . locale . units ] . s_m ;
2008-04-12 10:00:52 +00:00
}
2009-05-24 20:29:04 +00:00
static char * FormatString ( char * buff , const char * str , int64 * argv , uint casei , const char * last )
2004-08-09 17:04:08 +00:00
{
2006-11-16 22:05:33 +00:00
WChar b ;
2009-05-24 20:29:04 +00:00
int64 * argv_orig = argv ;
2005-07-17 10:18:23 +00:00
uint modifier = 0 ;
2004-08-09 17:04:08 +00:00
2006-11-16 22:05:33 +00:00
while ( ( b = Utf8Consume ( & str ) ) ! = ' \0 ' ) {
2007-09-22 23:55:34 +00:00
if ( SCC_NEWGRF_FIRST < = b & & b < = SCC_NEWGRF_LAST ) {
/* We need to pass some stuff as it might be modified; oh boy. */
2009-05-24 20:29:04 +00:00
b = RemapNewGRFStringControlCode ( b , & buff , & str , argv ) ;
2007-09-22 23:55:34 +00:00
if ( b = = 0 ) continue ;
}
2005-02-06 11:23:41 +00:00
switch ( b ) {
2006-11-16 22:05:33 +00:00
case SCC_SETX : // {SETX}
if ( buff + Utf8CharLen ( SCC_SETX ) + 1 < last ) {
buff + = Utf8Encode ( buff , SCC_SETX ) ;
* buff + + = * str + + ;
}
break ;
case SCC_SETXY : // {SETXY}
if ( buff + Utf8CharLen ( SCC_SETXY ) + 2 < last ) {
buff + = Utf8Encode ( buff , SCC_SETXY ) ;
* buff + + = * str + + ;
* buff + + = * str + + ;
}
break ;
case SCC_STRING_ID : // {STRINL}
buff = GetStringWithArgs ( buff , Utf8Consume ( & str ) , argv , last ) ;
break ;
2008-07-17 13:47:04 +00:00
case SCC_RAW_STRING_POINTER : { // {RAW_STRING}
2008-07-31 22:55:12 +00:00
const char * str = ( const char * ) ( size_t ) GetInt64 ( & argv ) ;
2008-07-17 13:47:04 +00:00
buff = FormatString ( buff , str , argv , casei , last ) ;
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_DATE_LONG : // {DATE_LONG}
buff = FormatYmdString ( buff , GetInt32 ( & argv ) , last ) ;
break ;
case SCC_DATE_SHORT : // {DATE_SHORT}
buff = FormatMonthAndYear ( buff , GetInt32 ( & argv ) , last ) ;
break ;
case SCC_VELOCITY : { // {VELOCITY}
2007-06-21 17:25:17 +00:00
int64 args [ 1 ] ;
2008-06-05 11:26:38 +00:00
assert ( _settings_game . locale . units < lengthof ( units ) ) ;
2009-02-01 17:14:39 +00:00
args [ 0 ] = ConvertSpeedToDisplaySpeed ( GetInt32 ( & argv ) * 10 / 16 ) ;
2008-06-05 11:26:38 +00:00
buff = FormatString ( buff , GetStringPtr ( units [ _settings_game . locale . units ] . velocity ) , args , modifier > > 24 , last ) ;
2006-11-16 22:05:33 +00:00
modifier = 0 ;
break ;
2006-10-21 23:31:34 +00:00
}
2005-09-10 15:14:35 +00:00
2008-11-24 20:51:55 +00:00
case SCC_CURRENCY_COMPACT : // {CURRCOMPACT}
2007-06-21 15:37:05 +00:00
buff = FormatGenericCurrency ( buff , _currency , GetInt64 ( & argv ) , true , last ) ;
2004-08-09 17:04:08 +00:00
break ;
2006-11-16 22:05:33 +00:00
2008-11-24 20:51:55 +00:00
case SCC_REVISION : // {REV}
2006-10-21 23:31:34 +00:00
buff = strecpy ( buff , _openttd_revision , last ) ;
2004-08-09 17:04:08 +00:00
break ;
2006-11-16 22:05:33 +00:00
2008-11-24 20:51:55 +00:00
case SCC_CARGO_SHORT : { // {SHORTCARGO}
2007-04-04 01:35:16 +00:00
/* Short description of cargotypes. Layout:
* 8 - bit = cargo type
* 16 - bit = cargo count */
2009-07-16 19:00:13 +00:00
StringID cargo_str = CargoSpec : : Get ( GetInt32 ( & argv ) ) - > units_volume ;
2006-03-26 21:50:57 +00:00
switch ( cargo_str ) {
case STR_TONS : {
2007-06-21 17:25:17 +00:00
int64 args [ 1 ] ;
2008-06-05 11:26:38 +00:00
assert ( _settings_game . locale . units < lengthof ( units ) ) ;
args [ 0 ] = GetInt32 ( & argv ) * units [ _settings_game . locale . units ] . w_m > > units [ _settings_game . locale . units ] . w_s ;
buff = FormatString ( buff , GetStringPtr ( units [ _settings_game . locale . units ] . l_weight ) , args , modifier > > 24 , last ) ;
2006-03-26 21:50:57 +00:00
modifier = 0 ;
break ;
}
case STR_LITERS : {
2007-06-21 17:25:17 +00:00
int64 args [ 1 ] ;
2008-06-05 11:26:38 +00:00
assert ( _settings_game . locale . units < lengthof ( units ) ) ;
args [ 0 ] = GetInt32 ( & argv ) * units [ _settings_game . locale . units ] . v_m > > units [ _settings_game . locale . units ] . v_s ;
buff = FormatString ( buff , GetStringPtr ( units [ _settings_game . locale . units ] . l_volume ) , args , modifier > > 24 , last ) ;
2006-03-26 21:50:57 +00:00
modifier = 0 ;
break ;
}
default :
2007-03-22 23:19:40 +00:00
if ( cargo_str > = 0xE000 & & cargo_str < 0xF800 ) {
/* NewGRF strings from Action 4 use a different format here,
* of e . g . " x tonnes of coal " , so process accordingly . */
buff = GetStringWithArgs ( buff , cargo_str , argv + + , last ) ;
} else {
buff = FormatCommaNumber ( buff , GetInt32 ( & argv ) , last ) ;
buff = strecpy ( buff , " " , last ) ;
buff = strecpy ( buff , GetStringPtr ( cargo_str ) , last ) ;
}
2006-03-26 21:50:57 +00:00
break ;
}
2005-02-06 11:23:41 +00:00
} break ;
2006-11-16 22:05:33 +00:00
2008-11-24 20:51:55 +00:00
case SCC_STRING1 : { // {STRING1}
2007-04-04 01:35:16 +00:00
/* String that consumes ONE argument */
2005-07-17 10:18:23 +00:00
uint str = modifier + GetInt32 ( & argv ) ;
2006-10-21 23:31:34 +00:00
buff = GetStringWithArgs ( buff , str , GetArgvPtr ( & argv , 1 ) , last ) ;
2005-07-17 10:18:23 +00:00
modifier = 0 ;
2005-07-15 14:53:44 +00:00
break ;
}
2006-11-16 22:05:33 +00:00
2008-11-24 20:51:55 +00:00
case SCC_STRING2 : { // {STRING2}
2007-04-04 01:35:16 +00:00
/* String that consumes TWO arguments */
2005-07-17 10:18:23 +00:00
uint str = modifier + GetInt32 ( & argv ) ;
2006-10-21 23:31:34 +00:00
buff = GetStringWithArgs ( buff , str , GetArgvPtr ( & argv , 2 ) , last ) ;
2005-07-17 10:18:23 +00:00
modifier = 0 ;
2005-07-15 14:53:44 +00:00
break ;
}
2006-11-16 22:05:33 +00:00
2008-11-24 20:51:55 +00:00
case SCC_STRING3 : { // {STRING3}
2007-04-04 01:35:16 +00:00
/* String that consumes THREE arguments */
2005-07-17 10:18:23 +00:00
uint str = modifier + GetInt32 ( & argv ) ;
2006-10-21 23:31:34 +00:00
buff = GetStringWithArgs ( buff , str , GetArgvPtr ( & argv , 3 ) , last ) ;
2005-07-17 10:18:23 +00:00
modifier = 0 ;
2005-07-15 14:53:44 +00:00
break ;
}
2006-11-16 22:05:33 +00:00
2008-11-24 20:51:55 +00:00
case SCC_STRING4 : { // {STRING4}
2007-04-04 01:35:16 +00:00
/* String that consumes FOUR arguments */
2005-07-17 10:18:23 +00:00
uint str = modifier + GetInt32 ( & argv ) ;
2006-10-21 23:31:34 +00:00
buff = GetStringWithArgs ( buff , str , GetArgvPtr ( & argv , 4 ) , last ) ;
2005-07-17 10:18:23 +00:00
modifier = 0 ;
2005-07-15 14:53:44 +00:00
break ;
}
2006-11-16 22:05:33 +00:00
2008-11-24 20:51:55 +00:00
case SCC_STRING5 : { // {STRING5}
2007-04-04 01:35:16 +00:00
/* String that consumes FIVE arguments */
2005-07-17 10:18:23 +00:00
uint str = modifier + GetInt32 ( & argv ) ;
2006-10-21 23:31:34 +00:00
buff = GetStringWithArgs ( buff , str , GetArgvPtr ( & argv , 5 ) , last ) ;
2005-07-17 10:18:23 +00:00
modifier = 0 ;
2005-07-15 14:53:44 +00:00
break ;
}
2008-11-24 20:51:55 +00:00
case SCC_STATION_FEATURES : { // {STATIONFEATURES}
2006-10-21 23:31:34 +00:00
buff = StationGetSpecialString ( buff , GetInt32 ( & argv ) , last ) ;
2005-07-15 14:53:44 +00:00
break ;
}
2004-08-09 17:04:08 +00:00
2008-11-24 20:51:55 +00:00
case SCC_INDUSTRY_NAME : { // {INDUSTRY}
2009-05-16 23:34:14 +00:00
const Industry * i = Industry : : Get ( GetInt32 ( & argv ) ) ;
2007-06-21 17:25:17 +00:00
int64 args [ 2 ] ;
2005-07-15 18:30:13 +00:00
2007-04-04 01:35:16 +00:00
/* industry not valid anymore? */
2009-05-22 15:13:50 +00:00
assert ( i ! = NULL ) ;
2005-07-15 18:30:13 +00:00
2009-02-14 01:29:18 +00:00
/* First print the town name and the industry type name. */
2005-07-15 18:30:13 +00:00
args [ 0 ] = i - > town - > index ;
2006-10-24 19:19:25 +00:00
args [ 1 ] = GetIndustrySpec ( i - > type ) - > name ;
2009-07-20 11:21:57 +00:00
buff = FormatString ( buff , GetStringPtr ( STR_FORMAT_INDUSTRY_NAME ) , args , modifier > > 24 , last ) ;
2005-07-17 10:18:23 +00:00
modifier = 0 ;
2005-07-15 18:30:13 +00:00
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_VOLUME : { // {VOLUME}
2007-06-21 17:25:17 +00:00
int64 args [ 1 ] ;
2008-06-05 11:26:38 +00:00
assert ( _settings_game . locale . units < lengthof ( units ) ) ;
args [ 0 ] = GetInt32 ( & argv ) * units [ _settings_game . locale . units ] . v_m > > units [ _settings_game . locale . units ] . v_s ;
buff = FormatString ( buff , GetStringPtr ( units [ _settings_game . locale . units ] . l_volume ) , args , modifier > > 24 , last ) ;
2005-07-17 10:18:23 +00:00
modifier = 0 ;
2005-07-16 20:58:04 +00:00
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_GENDER_LIST : { // {G 0 Der Die Das}
2010-02-20 23:42:11 +00:00
/* First read the meta data from the language file. */
WChar fmt = SCC_CONTROL_START + ( byte ) * str + + ;
byte offset = ( byte ) * str + + ;
/* Now we need to figure out what text to resolve, i.e.
* what do we need to draw ? So get the actual raw string
* first using the control code to get said string . */
char input [ 4 + 1 ] ;
char * p = input + Utf8Encode ( input , fmt ) ;
* p = ' \0 ' ;
/* Now do the string formatting. */
char buf [ 256 ] ;
bool old_kgd = _keep_gender_data ;
_keep_gender_data = true ;
p = FormatString ( buf , input , argv_orig + offset , 0 , lastof ( buf ) ) ;
_keep_gender_data = old_kgd ;
* p = ' \0 ' ;
/* And determine the string. */
2005-07-16 20:58:04 +00:00
int gender = 0 ;
2010-02-20 23:42:11 +00:00
const char * s = buf ;
WChar c = Utf8Consume ( & s ) ;
/* Does this string have a gender, if so, set it */
if ( c = = SCC_GENDER_INDEX ) gender = ( byte ) s [ 0 ] ;
2009-08-06 20:21:53 +00:00
str = ParseStringChoice ( str , gender , & buff , last ) ;
2005-07-16 20:58:04 +00:00
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_DATE_TINY : { // {DATE_TINY}
2009-07-20 11:21:57 +00:00
buff = FormatTinyOrISODate ( buff , GetInt32 ( & argv ) , STR_FORMAT_DATE_TINY , last ) ;
2009-01-03 00:56:16 +00:00
break ;
}
case SCC_DATE_ISO : { // {DATE_ISO}
2009-07-20 11:21:57 +00:00
buff = FormatTinyOrISODate ( buff , GetInt32 ( & argv ) , STR_FORMAT_DATE_ISO , last ) ;
2005-07-17 10:18:23 +00:00
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_CARGO : { // {CARGO}
2009-11-14 15:37:33 +00:00
/* First parameter is cargo type, second parameter is cargo count */
2006-10-20 19:48:25 +00:00
CargoID cargo = GetInt32 ( & argv ) ;
2009-08-05 17:59:21 +00:00
StringID cargo_str = ( cargo = = CT_INVALID ) ? STR_QUANTITY_N_A : CargoSpec : : Get ( cargo ) - > quantifier ;
2006-10-21 23:31:34 +00:00
buff = GetStringWithArgs ( buff , cargo_str , argv + + , last ) ;
2005-07-17 10:18:23 +00:00
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_POWER : { // {POWER}
2007-06-21 17:25:17 +00:00
int64 args [ 1 ] ;
2008-06-05 11:26:38 +00:00
assert ( _settings_game . locale . units < lengthof ( units ) ) ;
args [ 0 ] = GetInt32 ( & argv ) * units [ _settings_game . locale . units ] . p_m > > units [ _settings_game . locale . units ] . p_s ;
buff = FormatString ( buff , GetStringPtr ( units [ _settings_game . locale . units ] . power ) , args , modifier > > 24 , last ) ;
2006-03-26 21:50:57 +00:00
modifier = 0 ;
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_VOLUME_SHORT : { // {VOLUME_S}
2007-06-21 17:25:17 +00:00
int64 args [ 1 ] ;
2008-06-05 11:26:38 +00:00
assert ( _settings_game . locale . units < lengthof ( units ) ) ;
args [ 0 ] = GetInt32 ( & argv ) * units [ _settings_game . locale . units ] . v_m > > units [ _settings_game . locale . units ] . v_s ;
buff = FormatString ( buff , GetStringPtr ( units [ _settings_game . locale . units ] . s_volume ) , args , modifier > > 24 , last ) ;
2006-03-26 21:50:57 +00:00
modifier = 0 ;
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_WEIGHT : { // {WEIGHT}
2007-06-21 17:25:17 +00:00
int64 args [ 1 ] ;
2008-06-05 11:26:38 +00:00
assert ( _settings_game . locale . units < lengthof ( units ) ) ;
args [ 0 ] = GetInt32 ( & argv ) * units [ _settings_game . locale . units ] . w_m > > units [ _settings_game . locale . units ] . w_s ;
buff = FormatString ( buff , GetStringPtr ( units [ _settings_game . locale . units ] . l_weight ) , args , modifier > > 24 , last ) ;
2006-03-26 21:50:57 +00:00
modifier = 0 ;
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_WEIGHT_SHORT : { // {WEIGHT_S}
2007-06-21 17:25:17 +00:00
int64 args [ 1 ] ;
2008-06-05 11:26:38 +00:00
assert ( _settings_game . locale . units < lengthof ( units ) ) ;
args [ 0 ] = GetInt32 ( & argv ) * units [ _settings_game . locale . units ] . w_m > > units [ _settings_game . locale . units ] . w_s ;
buff = FormatString ( buff , GetStringPtr ( units [ _settings_game . locale . units ] . s_weight ) , args , modifier > > 24 , last ) ;
2006-03-26 21:50:57 +00:00
modifier = 0 ;
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_FORCE : { // {FORCE}
2007-06-21 17:25:17 +00:00
int64 args [ 1 ] ;
2008-06-05 11:26:38 +00:00
assert ( _settings_game . locale . units < lengthof ( units ) ) ;
args [ 0 ] = GetInt32 ( & argv ) * units [ _settings_game . locale . units ] . f_m > > units [ _settings_game . locale . units ] . f_s ;
buff = FormatString ( buff , GetStringPtr ( units [ _settings_game . locale . units ] . force ) , args , modifier > > 24 , last ) ;
2006-04-09 18:25:31 +00:00
modifier = 0 ;
break ;
}
2007-04-04 01:35:16 +00:00
/* 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 . */
2006-11-16 22:05:33 +00:00
case SCC_GENDER_INDEX : // {GENDER 0}
2010-02-20 23:42:11 +00:00
if ( _keep_gender_data ) {
buff + = Utf8Encode ( buff , SCC_GENDER_INDEX ) ;
* buff + + = * str + + ;
} else {
str + + ;
}
2006-11-16 22:05:33 +00:00
break ;
2004-09-10 19:02:27 +00:00
2006-11-16 22:05:33 +00:00
case SCC_STRING : { // {STRING}
uint str = modifier + GetInt32 ( & argv ) ;
2007-04-04 01:35:16 +00:00
/* WARNING. It's prohibited for the included string to consume any arguments.
* For included strings that consume argument , you should use STRING1 , STRING2 etc .
* To debug stuff you can set argv to NULL and it will tell you */
2006-11-16 22:05:33 +00:00
buff = GetStringWithArgs ( buff , str , argv , last ) ;
modifier = 0 ;
break ;
}
2004-08-09 17:04:08 +00:00
2006-11-16 22:05:33 +00:00
case SCC_COMMA : // {COMMA}
2007-07-29 12:13:22 +00:00
buff = FormatCommaNumber ( buff , GetInt64 ( & argv ) , last ) ;
2006-11-16 22:05:33 +00:00
break ;
2005-09-10 15:14:35 +00:00
2006-11-16 22:05:33 +00:00
case SCC_ARG_INDEX : // Move argument pointer
argv = argv_orig + ( byte ) * str + + ;
break ;
2005-09-10 15:14:35 +00:00
2006-11-16 22:05:33 +00:00
case SCC_PLURAL_LIST : { // {P}
2007-07-29 12:13:22 +00:00
int64 v = argv_orig [ ( byte ) * str + + ] ; // contains the number that determines plural
2009-08-06 20:21:53 +00:00
str = ParseStringChoice ( str , DeterminePluralForm ( v ) , & buff , last ) ;
2006-11-16 22:05:33 +00:00
break ;
}
2005-09-10 15:14:35 +00:00
2006-11-16 22:05:33 +00:00
case SCC_NUM : // {NUM}
2007-07-29 12:13:22 +00:00
buff = FormatNoCommaNumber ( buff , GetInt64 ( & argv ) , last ) ;
2006-11-16 22:05:33 +00:00
break ;
2005-09-10 15:14:35 +00:00
2009-11-19 13:53:54 +00:00
case SCC_ZEROFILL_NUM : { // {ZEROFILL_NUM}
int64 num = GetInt64 ( & argv ) ;
buff = FormatZerofillNumber ( buff , num , GetInt64 ( & argv ) , last ) ;
} break ;
2008-09-14 14:31:37 +00:00
case SCC_HEX : // {HEX}
buff = FormatHexNumber ( buff , GetInt64 ( & argv ) , last ) ;
break ;
2009-01-16 12:59:47 +00:00
case SCC_BYTES : // {BYTES}
buff = FormatBytes ( buff , GetInt64 ( & argv ) , last ) ;
break ;
2006-11-16 22:05:33 +00:00
case SCC_CURRENCY : // {CURRENCY}
2007-06-21 15:37:05 +00:00
buff = FormatGenericCurrency ( buff , _currency , GetInt64 ( & argv ) , false , last ) ;
2006-11-16 22:05:33 +00:00
break ;
2005-09-10 15:14:35 +00:00
2006-11-16 22:05:33 +00:00
case SCC_WAYPOINT_NAME : { // {WAYPOINT}
2009-05-16 23:34:14 +00:00
Waypoint * wp = Waypoint : : Get ( GetInt32 ( & argv ) ) ;
2008-01-12 19:58:06 +00:00
2009-05-22 15:13:50 +00:00
assert ( wp ! = NULL ) ;
2008-09-12 17:11:28 +00:00
if ( wp - > name ! = NULL ) {
2008-01-12 19:58:06 +00:00
buff = strecpy ( buff , wp - > name , last ) ;
2006-11-16 22:05:33 +00:00
} else {
2008-01-12 19:58:06 +00:00
int64 temp [ 2 ] ;
2009-07-16 23:02:39 +00:00
temp [ 0 ] = wp - > town - > index ;
2006-11-16 22:05:33 +00:00
temp [ 1 ] = wp - > town_cn + 1 ;
2009-08-05 17:59:21 +00:00
StringID str = ( ( wp - > string_id = = STR_SV_STNAME_BUOY ) ? STR_FORMAT_BUOY_NAME : STR_FORMAT_WAYPOINT_NAME ) ;
2009-07-22 08:59:57 +00:00
if ( wp - > town_cn ! = 0 ) str + + ;
2008-01-12 19:58:06 +00:00
buff = GetStringWithArgs ( buff , str , temp , last ) ;
2006-11-16 22:05:33 +00:00
}
break ;
2005-07-17 10:18:23 +00:00
}
2004-08-09 17:04:08 +00:00
2006-11-16 22:05:33 +00:00
case SCC_STATION_NAME : { // {STATION}
2008-09-12 17:14:07 +00:00
StationID sid = GetInt32 ( & argv ) ;
2009-05-18 16:21:28 +00:00
const Station * st = Station : : GetIfValid ( sid ) ;
2005-07-15 14:53:44 +00:00
2009-05-18 16:21:28 +00:00
if ( st = = NULL ) {
2008-09-12 17:14:07 +00:00
/* The station doesn't exist anymore. The only place where we might
* be " drawing " an invalid station is in the case of cargo that is
* in transit . */
2008-09-12 17:16:04 +00:00
buff = GetStringWithArgs ( buff , STR_UNKNOWN_STATION , NULL , last ) ;
2008-09-12 17:14:07 +00:00
break ;
}
if ( st - > name ! = NULL ) {
2008-01-12 19:58:06 +00:00
buff = strecpy ( buff , st - > name , last ) ;
2006-11-16 22:05:33 +00:00
} else {
2008-11-19 23:55:34 +00:00
StringID str = st - > string_id ;
if ( st - > indtype ! = IT_INVALID ) {
/* Special case where the industry provides the name for the station */
const IndustrySpec * indsp = GetIndustrySpec ( st - > indtype ) ;
/* Industry GRFs can change which might remove the station name and
* thus cause very strange things . Here we check for that before we
* actually set the station name . */
if ( indsp - > station_name ! = STR_NULL & & indsp - > station_name ! = STR_UNDEFINED ) {
str = indsp - > station_name ;
}
}
2007-06-25 10:22:48 +00:00
int64 temp [ 3 ] ;
2009-07-20 11:21:57 +00:00
temp [ 0 ] = STR_TOWN_NAME ;
2007-06-18 23:00:55 +00:00
temp [ 1 ] = st - > town - > index ;
2007-06-25 10:22:48 +00:00
temp [ 2 ] = st - > index ;
2008-11-19 23:55:34 +00:00
buff = GetStringWithArgs ( buff , str , temp , last ) ;
2006-11-16 22:05:33 +00:00
}
break ;
2004-08-16 13:54:37 +00:00
}
2005-11-14 19:48:04 +00:00
2006-11-16 22:05:33 +00:00
case SCC_TOWN_NAME : { // {TOWN}
2009-05-16 23:34:14 +00:00
const Town * t = Town : : Get ( GetInt32 ( & argv ) ) ;
2005-11-13 13:43:55 +00:00
2009-05-22 15:13:50 +00:00
assert ( t ! = NULL ) ;
2004-08-09 17:04:08 +00:00
2008-01-12 19:58:06 +00:00
if ( t - > name ! = NULL ) {
buff = strecpy ( buff , t - > name , last ) ;
2007-06-18 23:00:55 +00:00
} else {
2009-09-22 13:54:54 +00:00
buff = GetTownName ( buff , t , last ) ;
2007-06-18 23:00:55 +00:00
}
2006-11-16 22:05:33 +00:00
break ;
}
2004-08-09 17:04:08 +00:00
2007-05-19 09:40:18 +00:00
case SCC_GROUP_NAME : { // {GROUP}
2009-05-16 23:34:14 +00:00
const Group * g = Group : : Get ( GetInt32 ( & argv ) ) ;
2007-05-19 09:40:18 +00:00
2009-05-22 15:13:50 +00:00
assert ( g ! = NULL ) ;
2007-05-19 09:40:18 +00:00
2008-01-12 19:58:06 +00:00
if ( g - > name ! = NULL ) {
buff = strecpy ( buff , g - > name , last ) ;
} else {
int64 args [ 1 ] ;
2007-05-19 09:40:18 +00:00
2008-01-12 19:58:06 +00:00
args [ 0 ] = g - > index ;
2009-07-20 11:21:57 +00:00
buff = GetStringWithArgs ( buff , STR_FORMAT_GROUP_NAME , args , last ) ;
2008-01-12 19:58:06 +00:00
}
2007-05-19 09:40:18 +00:00
break ;
}
2007-06-25 14:46:32 +00:00
case SCC_ENGINE_NAME : { // {ENGINE}
EngineID engine = ( EngineID ) GetInt32 ( & argv ) ;
2009-05-16 23:34:14 +00:00
const Engine * e = Engine : : Get ( engine ) ;
2007-06-25 14:46:32 +00:00
2009-05-22 15:13:50 +00:00
assert ( e ! = NULL ) ;
2008-01-12 19:58:06 +00:00
if ( e - > name ! = NULL ) {
buff = strecpy ( buff , e - > name , last ) ;
} else {
2008-04-29 21:31:29 +00:00
buff = GetStringWithArgs ( buff , e - > info . string_id , NULL , last ) ;
2008-01-12 19:58:06 +00:00
}
2007-06-25 14:46:32 +00:00
break ;
}
2007-06-24 22:42:11 +00:00
case SCC_VEHICLE_NAME : { // {VEHICLE}
2009-05-16 23:34:14 +00:00
const Vehicle * v = Vehicle : : Get ( GetInt32 ( & argv ) ) ;
2007-06-24 22:42:11 +00:00
2009-05-22 15:13:50 +00:00
assert ( v ! = NULL ) ;
2008-01-12 19:58:06 +00:00
if ( v - > name ! = NULL ) {
buff = strecpy ( buff , v - > name , last ) ;
} else {
int64 args [ 1 ] ;
args [ 0 ] = v - > unitnumber ;
StringID str ;
switch ( v - > type ) {
default : NOT_REACHED ( ) ;
case VEH_TRAIN : str = STR_SV_TRAIN_NAME ; break ;
2009-07-22 22:44:56 +00:00
case VEH_ROAD : str = STR_SV_ROAD_VEHICLE_NAME ; break ;
2008-01-12 19:58:06 +00:00
case VEH_SHIP : str = STR_SV_SHIP_NAME ; break ;
case VEH_AIRCRAFT : str = STR_SV_AIRCRAFT_NAME ; break ;
}
2007-06-24 22:42:11 +00:00
2008-01-12 19:58:06 +00:00
buff = GetStringWithArgs ( buff , str , args , last ) ;
}
2007-06-24 22:42:11 +00:00
break ;
}
2007-06-25 10:40:56 +00:00
case SCC_SIGN_NAME : { // {SIGN}
2009-05-16 23:34:14 +00:00
const Sign * si = Sign : : Get ( GetInt32 ( & argv ) ) ;
2008-01-12 19:58:06 +00:00
if ( si - > name ! = NULL ) {
buff = strecpy ( buff , si - > name , last ) ;
} else {
2009-08-05 17:59:21 +00:00
buff = GetStringWithArgs ( buff , STR_DEFAULT_SIGN_NAME , NULL , last ) ;
2008-01-12 19:58:06 +00:00
}
2007-06-25 10:40:56 +00:00
break ;
}
2007-06-25 13:30:38 +00:00
case SCC_COMPANY_NAME : { // {COMPANY}
2009-05-16 23:34:14 +00:00
const Company * c = Company : : Get ( ( CompanyID ) GetInt32 ( & argv ) ) ;
2008-01-12 19:58:06 +00:00
2008-09-30 20:39:50 +00:00
if ( c - > name ! = NULL ) {
buff = strecpy ( buff , c - > name , last ) ;
2008-01-12 19:58:06 +00:00
} else {
int64 args [ 1 ] ;
2008-09-30 20:39:50 +00:00
args [ 0 ] = c - > name_2 ;
buff = GetStringWithArgs ( buff , c - > name_1 , args , last ) ;
2008-01-12 19:58:06 +00:00
}
2007-06-25 13:30:38 +00:00
break ;
}
case SCC_COMPANY_NUM : { // {COMPANYNUM}
2008-09-30 20:39:50 +00:00
CompanyID company = ( CompanyID ) GetInt32 ( & argv ) ;
2007-06-25 13:30:38 +00:00
2008-09-30 20:39:50 +00:00
/* Nothing is added for AI or inactive companies */
2009-06-10 22:11:39 +00:00
if ( Company : : IsValidHumanID ( company ) ) {
2007-06-25 13:30:38 +00:00
int64 args [ 1 ] ;
2008-09-30 20:39:50 +00:00
args [ 0 ] = company + 1 ;
2009-07-20 11:21:57 +00:00
buff = GetStringWithArgs ( buff , STR_FORMAT_COMPANY_NUM , args , last ) ;
2007-06-25 13:30:38 +00:00
}
break ;
}
2008-09-30 21:18:28 +00:00
case SCC_PRESIDENT_NAME : { // {PRESIDENTNAME}
2009-05-16 23:34:14 +00:00
const Company * c = Company : : Get ( ( CompanyID ) GetInt32 ( & argv ) ) ;
2008-01-12 19:58:06 +00:00
2008-09-30 20:39:50 +00:00
if ( c - > president_name ! = NULL ) {
buff = strecpy ( buff , c - > president_name , last ) ;
2008-01-12 19:58:06 +00:00
} else {
int64 args [ 1 ] ;
2008-09-30 20:39:50 +00:00
args [ 0 ] = c - > president_name_2 ;
buff = GetStringWithArgs ( buff , c - > president_name_1 , args , last ) ;
2008-01-12 19:58:06 +00:00
}
2007-06-25 13:30:38 +00:00
break ;
}
2006-11-16 22:05:33 +00:00
case SCC_SETCASE : { // {SETCASE}
2007-04-04 01:35:16 +00:00
/* This is a pseudo command, it's outputted when someone does {STRING.ack}
* The modifier is added to all subsequent GetStringWithArgs that accept the modifier . */
2006-11-16 22:05:33 +00:00
modifier = ( byte ) * str + + < < 24 ;
break ;
}
case SCC_SWITCH_CASE : { // {Used to implement case switching}
2007-04-04 01:35:16 +00:00
/* <0x9E> <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
* Each LEN is printed using 2 bytes in big endian order . */
2006-11-16 22:05:33 +00:00
uint num = ( byte ) * str + + ;
while ( num ) {
if ( ( byte ) str [ 0 ] = = casei ) {
2007-04-04 01:35:16 +00:00
/* Found the case, adjust str pointer and continue */
2006-11-16 22:05:33 +00:00
str + = 3 ;
break ;
}
2007-04-04 01:35:16 +00:00
/* Otherwise skip to the next case */
2006-11-16 22:05:33 +00:00
str + = 3 + ( str [ 1 ] < < 8 ) + str [ 2 ] ;
num - - ;
2005-07-17 10:18:23 +00:00
}
2006-11-16 22:05:33 +00:00
break ;
2005-07-17 10:18:23 +00:00
}
2005-01-22 23:13:20 +00:00
2006-11-16 22:05:33 +00:00
default :
if ( buff + Utf8CharLen ( b ) < last ) buff + = Utf8Encode ( buff , b ) ;
break ;
2004-08-09 17:04:08 +00:00
}
}
2005-02-06 11:23:41 +00:00
* buff = ' \0 ' ;
2004-08-09 17:04:08 +00:00
return buff ;
}
2008-11-24 20:51:55 +00:00
static char * StationGetSpecialString ( char * buff , int x , const char * last )
2004-08-09 17:04:08 +00:00
{
2008-04-19 18:03:24 +00:00
if ( ( x & FACIL_TRAIN ) & & ( buff + Utf8CharLen ( SCC_TRAIN ) < last ) ) buff + = Utf8Encode ( buff , SCC_TRAIN ) ;
if ( ( x & FACIL_TRUCK_STOP ) & & ( buff + Utf8CharLen ( SCC_LORRY ) < last ) ) buff + = Utf8Encode ( buff , SCC_LORRY ) ;
if ( ( x & FACIL_BUS_STOP ) & & ( buff + Utf8CharLen ( SCC_BUS ) < last ) ) buff + = Utf8Encode ( buff , SCC_BUS ) ;
if ( ( x & FACIL_AIRPORT ) & & ( buff + Utf8CharLen ( SCC_PLANE ) < last ) ) buff + = Utf8Encode ( buff , SCC_PLANE ) ;
if ( ( x & FACIL_DOCK ) & & ( buff + Utf8CharLen ( SCC_SHIP ) < last ) ) buff + = Utf8Encode ( buff , SCC_SHIP ) ;
2006-11-16 22:05:33 +00:00
* buff = ' \0 ' ;
2004-08-09 17:04:08 +00:00
return buff ;
}
2008-11-24 20:51:55 +00:00
static char * GetSpecialTownNameString ( char * buff , int ind , uint32 seed , const char * last )
2005-02-06 11:23:41 +00:00
{
2009-03-16 10:51:49 +00:00
return GenerateTownNameString ( buff , last , ind , seed ) ;
2004-08-09 17:04:08 +00:00
}
2008-11-24 20:51:55 +00:00
static const char * const _silly_company_names [ ] = {
2004-08-09 17:04:08 +00:00
" Bloggs Brothers " ,
" Tiny Transport Ltd. " ,
" Express Travel " ,
" Comfy-Coach & Co. " ,
" Crush & Bump Ltd. " ,
" Broken & Late Ltd. " ,
" Sam Speedy & Son " ,
" Supersonic Travel " ,
" Mike's Motors " ,
" Lightning International " ,
" Pannik & Loozit Ltd. " ,
" Inter-City Transport " ,
2005-11-16 11:17:52 +00:00
" Getout & Pushit Ltd. "
2004-08-09 17:04:08 +00:00
} ;
2008-11-24 20:51:55 +00:00
static const char * const _surname_list [ ] = {
2004-08-09 17:04:08 +00:00
" Adams " ,
" Allan " ,
" Baker " ,
" Bigwig " ,
" Black " ,
" Bloggs " ,
" Brown " ,
" Campbell " ,
" Gordon " ,
" Hamilton " ,
" Hawthorn " ,
" Higgins " ,
" Green " ,
" Gribble " ,
" Jones " ,
" McAlpine " ,
" MacDonald " ,
" McIntosh " ,
" Muir " ,
" Murphy " ,
" Nelson " ,
" O'Donnell " ,
" Parker " ,
" Phillips " ,
" Pilkington " ,
" Quigley " ,
" Sharkey " ,
" Thomson " ,
2005-11-16 11:17:52 +00:00
" Watkins "
} ;
2008-11-24 20:51:55 +00:00
static const char * const _silly_surname_list [ ] = {
2004-08-09 17:04:08 +00:00
" Grumpy " ,
" Dozy " ,
" Speedy " ,
" Nosey " ,
" Dribble " ,
" Mushroom " ,
" Cabbage " ,
" Sniffle " ,
" Fishy " ,
" Swindle " ,
" Sneaky " ,
2005-11-16 11:17:52 +00:00
" Nutkins "
2004-08-09 17:04:08 +00:00
} ;
2005-02-06 09:52:06 +00:00
static const char _initial_name_letters [ ] = {
2005-02-06 16:56:04 +00:00
' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' , ' G ' , ' H ' , ' I ' , ' J ' ,
' K ' , ' L ' , ' M ' , ' N ' , ' P ' , ' R ' , ' S ' , ' T ' , ' W ' ,
2004-08-09 17:04:08 +00:00
} ;
2008-11-24 20:51:55 +00:00
static char * GenAndCoName ( char * buff , uint32 arg , const char * last )
2004-08-09 17:04:08 +00:00
{
2008-11-24 20:51:55 +00:00
const char * const * base ;
2005-11-16 11:17:52 +00:00
uint num ;
2004-08-09 17:04:08 +00:00
2008-05-29 15:13:28 +00:00
if ( _settings_game . game_creation . landscape = = LT_TOYLAND ) {
2005-11-16 11:17:52 +00:00
base = _silly_surname_list ;
num = lengthof ( _silly_surname_list ) ;
} else {
base = _surname_list ;
num = lengthof ( _surname_list ) ;
2004-08-09 17:04:08 +00:00
}
2006-10-21 23:31:34 +00:00
buff = strecpy ( buff , base [ num * GB ( arg , 16 , 8 ) > > 8 ] , last ) ;
buff = strecpy ( buff , " & Co. " , last ) ;
2004-08-09 17:04:08 +00:00
return buff ;
}
2008-11-24 20:51:55 +00:00
static char * GenPresidentName ( char * buff , uint32 x , const char * last )
2004-08-09 17:04:08 +00:00
{
2006-10-21 23:31:34 +00:00
char initial [ ] = " ?. " ;
2008-11-24 20:51:55 +00:00
const char * const * base ;
2005-11-16 11:17:52 +00:00
uint num ;
uint i ;
2004-08-09 17:04:08 +00:00
2006-10-21 23:31:34 +00:00
initial [ 0 ] = _initial_name_letters [ sizeof ( _initial_name_letters ) * GB ( x , 0 , 8 ) > > 8 ] ;
buff = strecpy ( buff , initial , last ) ;
2004-08-09 17:04:08 +00:00
2005-07-21 06:31:02 +00:00
i = ( sizeof ( _initial_name_letters ) + 35 ) * GB ( x , 8 , 8 ) > > 8 ;
2004-08-09 17:04:08 +00:00
if ( i < sizeof ( _initial_name_letters ) ) {
2006-10-21 23:31:34 +00:00
initial [ 0 ] = _initial_name_letters [ i ] ;
buff = strecpy ( buff , initial , last ) ;
2004-08-09 17:04:08 +00:00
}
2008-05-29 15:13:28 +00:00
if ( _settings_game . game_creation . landscape = = LT_TOYLAND ) {
2005-11-16 11:17:52 +00:00
base = _silly_surname_list ;
num = lengthof ( _silly_surname_list ) ;
} else {
base = _surname_list ;
num = lengthof ( _surname_list ) ;
2004-08-09 17:04:08 +00:00
}
2006-10-21 23:31:34 +00:00
buff = strecpy ( buff , base [ num * GB ( x , 16 , 8 ) > > 8 ] , last ) ;
2004-08-09 17:04:08 +00:00
return buff ;
}
2009-05-24 20:29:04 +00:00
static char * GetSpecialNameString ( char * buff , int ind , int64 * argv , const char * last )
2004-08-09 17:04:08 +00:00
{
2005-02-06 11:23:41 +00:00
switch ( ind ) {
2005-02-06 16:56:04 +00:00
case 1 : // not used
2006-10-21 23:31:34 +00:00
return strecpy ( buff , _silly_company_names [ GetInt32 ( & argv ) & 0xFFFF ] , last ) ;
2004-08-09 17:04:08 +00:00
2005-02-06 16:56:04 +00:00
case 2 : // used for Foobar & Co company names
2006-10-21 23:31:34 +00:00
return GenAndCoName ( buff , GetInt32 ( & argv ) , last ) ;
2004-08-09 17:04:08 +00:00
2005-02-06 16:56:04 +00:00
case 3 : // President name
2006-10-21 23:31:34 +00:00
return GenPresidentName ( buff , GetInt32 ( & argv ) , last ) ;
2004-08-09 17:04:08 +00:00
}
2007-04-04 01:35:16 +00:00
/* town name? */
2008-04-18 04:37:06 +00:00
if ( IsInsideMM ( ind - 6 , 0 , SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1 ) ) {
2006-10-21 23:31:34 +00:00
buff = GetSpecialTownNameString ( buff , ind - 6 , GetInt32 ( & argv ) , last ) ;
return strecpy ( buff , " Transport " , last ) ;
2004-08-09 17:04:08 +00:00
}
2007-04-04 01:35:16 +00:00
/* language name? */
2007-11-24 10:38:43 +00:00
if ( IsInsideMM ( ind , ( SPECSTR_LANGUAGE_START - 0x70E4 ) , ( SPECSTR_LANGUAGE_END - 0x70E4 ) + 1 ) ) {
2004-08-09 17:04:08 +00:00
int i = ind - ( SPECSTR_LANGUAGE_START - 0x70E4 ) ;
2005-05-22 07:43:18 +00:00
return strecpy ( buff ,
2006-10-21 23:31:34 +00:00
i = = _dynlang . curr ? _langpack - > own_name : _dynlang . ent [ i ] . name , last ) ;
2004-08-09 17:04:08 +00:00
}
2007-04-04 01:35:16 +00:00
/* resolution size? */
2007-11-24 10:38:43 +00:00
if ( IsInsideMM ( ind , ( SPECSTR_RESOLUTION_START - 0x70E4 ) , ( SPECSTR_RESOLUTION_END - 0x70E4 ) + 1 ) ) {
2004-08-09 17:04:08 +00:00
int i = ind - ( SPECSTR_RESOLUTION_START - 0x70E4 ) ;
2008-10-28 14:42:31 +00:00
buff + = seprintf (
2009-06-27 20:53:45 +00:00
buff , last , " %ux%u " , _resolutions [ i ] . width , _resolutions [ i ] . height
2006-10-21 23:31:34 +00:00
) ;
return buff ;
2004-08-09 17:04:08 +00:00
}
2007-04-04 01:35:16 +00:00
/* screenshot format name? */
2007-11-24 10:38:43 +00:00
if ( IsInsideMM ( ind , ( SPECSTR_SCREENSHOT_START - 0x70E4 ) , ( SPECSTR_SCREENSHOT_END - 0x70E4 ) + 1 ) ) {
2004-08-09 17:04:08 +00:00
int i = ind - ( SPECSTR_SCREENSHOT_START - 0x70E4 ) ;
2006-10-21 23:31:34 +00:00
return strecpy ( buff , GetScreenshotFormatDesc ( i ) , last ) ;
2004-08-09 17:04:08 +00:00
}
2009-07-31 13:25:20 +00:00
NOT_REACHED ( ) ;
2004-08-09 17:04:08 +00:00
}
2007-06-13 17:34:05 +00:00
# ifdef ENABLE_NETWORK
2007-06-04 16:07:22 +00:00
extern void SortNetworkLanguages ( ) ;
2007-06-13 17:34:05 +00:00
# else /* ENABLE_NETWORK */
static inline void SortNetworkLanguages ( ) { }
# endif /* ENABLE_NETWORK */
2007-06-04 16:07:22 +00:00
2005-02-06 11:23:41 +00:00
bool ReadLanguagePack ( int lang_index )
{
2010-02-12 23:39:44 +00:00
/* Current language pack */
2004-08-09 17:04:08 +00:00
size_t len ;
2010-02-12 23:39:44 +00:00
LanguagePack * lang_pack = ( LanguagePack * ) ReadFileToMem ( _dynlang . ent [ lang_index ] . file , & len , 200000 ) ;
if ( lang_pack = = NULL ) return false ;
2004-08-09 17:04:08 +00:00
2010-02-12 23:39:44 +00:00
/* End of read data (+ terminating zero added in ReadFileToMem()) */
const char * end = ( char * ) lang_pack + len + 1 ;
2007-03-17 22:21:05 +00:00
2010-02-12 23:39:44 +00:00
/* We need at least one byte of lang_pack->data */
if ( end < = lang_pack - > data | |
2005-02-06 14:47:56 +00:00
lang_pack - > ident ! = TO_LE32 ( LANGUAGE_PACK_IDENT ) | |
lang_pack - > version ! = TO_LE32 ( LANGUAGE_PACK_VERSION ) ) {
2004-08-09 17:04:08 +00:00
free ( lang_pack ) ;
return false ;
}
2004-09-10 19:02:27 +00:00
2008-06-17 19:38:00 +00:00
# if TTD_ENDIAN == TTD_BIG_ENDIAN
2010-02-12 23:39:44 +00:00
for ( uint i = 0 ; i < 32 ; i + + ) {
2006-02-03 21:51:42 +00:00
lang_pack - > offsets [ i ] = ReadLE16Aligned ( & lang_pack - > offsets [ i ] ) ;
2004-08-09 17:04:08 +00:00
}
2008-06-17 19:38:00 +00:00
# endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
2004-08-09 17:04:08 +00:00
2010-02-12 23:39:44 +00:00
uint count = 0 ;
for ( uint i = 0 ; i < 32 ; i + + ) {
2005-02-06 14:47:56 +00:00
uint num = lang_pack - > offsets [ i ] ;
2010-02-12 23:39:44 +00:00
_langtab_start [ i ] = count ;
2004-08-09 17:04:08 +00:00
_langtab_num [ i ] = num ;
2010-02-12 23:39:44 +00:00
count + = num ;
2004-08-09 17:04:08 +00:00
}
2007-04-04 01:35:16 +00:00
/* Allocate offsets */
2010-02-12 23:39:44 +00:00
char * * langpack_offs = MallocT < char * > ( count ) ;
2004-08-09 17:04:08 +00:00
2007-04-04 01:35:16 +00:00
/* Fill offsets */
2010-02-12 23:39:44 +00:00
char * s = lang_pack - > data ;
len = ( byte ) * s + + ;
for ( uint i = 0 ; i < count ; i + + ) {
if ( s + len > = end ) {
free ( lang_pack ) ;
free ( langpack_offs ) ;
return false ;
}
if ( len > = 0xC0 ) {
len = ( ( len & 0x3F ) < < 8 ) + ( byte ) * s + + ;
if ( s + len > = end ) {
free ( lang_pack ) ;
free ( langpack_offs ) ;
return false ;
}
}
2004-08-09 17:04:08 +00:00
langpack_offs [ i ] = s ;
s + = len ;
2010-02-12 23:39:44 +00:00
len = ( byte ) * s ;
* s + + = ' \0 ' ; // zero terminate the string
2004-08-09 17:04:08 +00:00
}
2005-02-06 16:56:04 +00:00
free ( _langpack ) ;
2004-08-09 17:04:08 +00:00
_langpack = lang_pack ;
2005-02-06 16:56:04 +00:00
free ( _langpack_offs ) ;
2004-08-09 17:04:08 +00:00
_langpack_offs = langpack_offs ;
2007-03-20 14:01:21 +00:00
const char * c_file = strrchr ( _dynlang . ent [ lang_index ] . file , PATHSEPCHAR ) + 1 ;
2008-11-02 11:20:15 +00:00
strecpy ( _dynlang . curr_file , c_file , lastof ( _dynlang . curr_file ) ) ;
2004-08-09 17:04:08 +00:00
_dynlang . curr = lang_index ;
2008-10-17 17:42:51 +00:00
_dynlang . text_dir = ( TextDirection ) lang_pack - > text_dir ;
2009-03-13 00:30:26 +00:00
SetCurrentGrfLangID ( _langpack - > newgrflangid ) ;
2010-04-01 19:48:28 +00:00
InitializeSortedCargoSpecs ( ) ;
2010-03-28 12:25:09 +00:00
SortIndustryTypes ( ) ;
BuildIndustriesLegend ( ) ;
2007-06-04 16:07:22 +00:00
SortNetworkLanguages ( ) ;
2004-08-09 17:04:08 +00:00
return true ;
}
2009-03-15 00:32:18 +00:00
/* Win32 implementation in win32.cpp.
* OS X implementation in os / macosx / macos . mm . */
2007-08-11 15:52:34 +00:00
# if !(defined(WIN32) || defined(__APPLE__))
2006-03-25 09:22:10 +00:00
/** Determine the current charset based on the environment
* First check some default values , after this one we passed ourselves
* and if none exist return the value for $ LANG
2007-04-04 01:35:16 +00:00
* @ param param environment variable to check conditionally if default ones are not
2006-03-25 09:22:10 +00:00
* set . Pass NULL if you don ' t want additional checks .
* @ return return string containing current charset , or NULL if not - determinable */
const char * GetCurrentLocale ( const char * param )
{
const char * env ;
env = getenv ( " LANGUAGE " ) ;
if ( env ! = NULL ) return env ;
env = getenv ( " LC_ALL " ) ;
if ( env ! = NULL ) return env ;
if ( param ! = NULL ) {
env = getenv ( param ) ;
if ( env ! = NULL ) return env ;
}
return getenv ( " LANG " ) ;
}
2008-04-19 13:04:29 +00:00
# else
const char * GetCurrentLocale ( const char * param ) ;
2007-08-11 15:52:34 +00:00
# endif /* !(defined(WIN32) || defined(__APPLE__)) */
2006-03-25 09:22:10 +00:00
2009-09-13 17:38:07 +00:00
int CDECL StringIDSorter ( const StringID * a , const StringID * b )
2007-06-04 16:07:22 +00:00
{
char stra [ 512 ] ;
char strb [ 512 ] ;
2009-09-13 17:38:07 +00:00
GetString ( stra , * a , lastof ( stra ) ) ;
GetString ( strb , * b , lastof ( strb ) ) ;
2007-06-04 16:07:22 +00:00
return strcmp ( stra , strb ) ;
}
2007-03-17 22:21:05 +00:00
/**
* Checks whether the given language is already found .
* @ param langs languages we ' ve found so fa
* @ param max the length of the language list
* @ param language name of the language to check
* @ return true if and only if a language file with the same name has not been found
*/
static bool UniqueLanguageFile ( const Language * langs , uint max , const char * language )
{
for ( uint i = 0 ; i < max ; i + + ) {
2007-03-20 14:01:21 +00:00
const char * f_name = strrchr ( langs [ i ] . file , PATHSEPCHAR ) + 1 ;
2007-03-17 22:21:05 +00:00
if ( strcmp ( f_name , language ) = = 0 ) return false ; // duplicates
}
return true ;
}
/**
* Reads the language file header and checks compatability .
* @ param file the file to read
* @ param hdr the place to write the header information to
* @ return true if and only if the language file is of a compatible version
*/
static bool GetLanguageFileHeader ( const char * file , LanguagePack * hdr )
{
FILE * f = fopen ( file , " rb " ) ;
if ( f = = NULL ) return false ;
size_t read = fread ( hdr , sizeof ( * hdr ) , 1 , f ) ;
fclose ( f ) ;
2008-11-24 18:53:17 +00:00
bool ret = read = = 1 & &
2007-03-17 22:21:05 +00:00
hdr - > ident = = TO_LE32 ( LANGUAGE_PACK_IDENT ) & &
hdr - > version = = TO_LE32 ( LANGUAGE_PACK_VERSION ) ;
2008-11-24 18:53:17 +00:00
/* Convert endianness for the windows language ID */
if ( ret ) hdr - > winlangid = FROM_LE16 ( hdr - > winlangid ) ;
return ret ;
2006-08-04 23:45:20 +00:00
}
2007-03-17 22:21:05 +00:00
/**
* Gets a list of languages from the given directory .
* @ param langs the list to write to
* @ param start the initial offset in the list
* @ param max the length of the language list
* @ param path the base directory to search in
* @ return the number of added languages
*/
static int GetLanguageList ( Language * langs , int start , int max , const char * path )
2006-08-04 23:45:20 +00:00
{
2007-03-17 22:21:05 +00:00
int i = start ;
2006-08-04 23:45:20 +00:00
2007-03-17 22:21:05 +00:00
DIR * dir = ttd_opendir ( path ) ;
2006-08-04 23:45:20 +00:00
if ( dir ! = NULL ) {
2007-03-17 22:21:05 +00:00
struct dirent * dirent ;
while ( ( dirent = readdir ( dir ) ) ! = NULL & & i < max ) {
const char * d_name = FS2OTTD ( dirent - > d_name ) ;
const char * extension = strrchr ( d_name , ' . ' ) ;
2006-08-04 23:45:20 +00:00
2007-03-17 22:21:05 +00:00
/* Not a language file */
if ( extension = = NULL | | strcmp ( extension , " .lng " ) ! = 0 ) continue ;
/* Filter any duplicate language-files, first-come first-serve */
if ( ! UniqueLanguageFile ( langs , i , d_name ) ) continue ;
langs [ i ] . file = str_fmt ( " %s%s " , path , d_name ) ;
/* Check whether the file is of the correct version */
LanguagePack hdr ;
if ( ! GetLanguageFileHeader ( langs [ i ] . file , & hdr ) ) {
free ( langs [ i ] . file ) ;
continue ;
2006-08-04 23:45:20 +00:00
}
2007-03-17 22:21:05 +00:00
i + + ;
2006-08-04 23:45:20 +00:00
}
closedir ( dir ) ;
}
2007-03-17 22:21:05 +00:00
return i - start ;
2006-08-04 23:45:20 +00:00
}
2007-03-17 22:21:05 +00:00
/**
* Make a list of the available language packs . put the data in
* _dynlang struct .
*/
2007-03-07 11:47:46 +00:00
void InitializeLanguagePacks ( )
2004-08-09 17:04:08 +00:00
{
2007-06-17 15:48:57 +00:00
Searchpath sp ;
2007-03-17 22:21:05 +00:00
Language files [ MAX_LANG ] ;
2007-06-17 15:48:57 +00:00
uint language_count = 0 ;
FOR_ALL_SEARCHPATHS ( sp ) {
char path [ MAX_PATH ] ;
FioAppendDirectory ( path , lengthof ( path ) , sp , LANG_DIR ) ;
language_count + = GetLanguageList ( files , language_count , lengthof ( files ) , path ) ;
}
2008-06-05 20:54:52 +00:00
if ( language_count = = 0 ) usererror ( " No available language packs (invalid versions?) " ) ;
2004-08-09 17:04:08 +00:00
2007-03-17 22:21:05 +00:00
/* Acquire the locale of the current system */
const char * lang = GetCurrentLocale ( " LC_MESSAGES " ) ;
if ( lang = = NULL ) lang = " en_GB " ;
2004-09-10 19:02:27 +00:00
2007-03-17 22:21:05 +00:00
int chosen_language = - 1 ; ///< Matching the language in the configuartion file or the current locale
int language_fallback = - 1 ; ///< Using pt_PT for pt_BR locale when pt_BR is not available
int en_GB_fallback = 0 ; ///< Fallback when no locale-matching language has been found
2004-08-09 17:04:08 +00:00
2007-03-17 22:21:05 +00:00
DynamicLanguages * dl = & _dynlang ;
dl - > num = 0 ;
/* Fill the dynamic languages structures */
for ( uint i = 0 ; i < language_count ; i + + ) {
/* File read the language header */
LanguagePack hdr ;
if ( ! GetLanguageFileHeader ( files [ i ] . file , & hdr ) ) continue ;
dl - > ent [ dl - > num ] . file = files [ i ] . file ;
dl - > ent [ dl - > num ] . name = strdup ( hdr . name ) ;
/* We are trying to find a default language. The priority is by
* configuration file , local environment and last , if nothing found ,
* english . If def equals - 1 , we have not picked a default language */
2007-03-20 14:01:21 +00:00
const char * lang_file = strrchr ( dl - > ent [ dl - > num ] . file , PATHSEPCHAR ) + 1 ;
if ( strcmp ( lang_file , dl - > curr_file ) = = 0 ) chosen_language = dl - > num ;
2007-03-17 22:21:05 +00:00
if ( chosen_language = = - 1 ) {
if ( strcmp ( hdr . isocode , " en_GB " ) = = 0 ) en_GB_fallback = dl - > num ;
if ( strncmp ( hdr . isocode , lang , 5 ) = = 0 ) chosen_language = dl - > num ;
if ( strncmp ( hdr . isocode , lang , 2 ) = = 0 ) language_fallback = dl - > num ;
}
2004-08-09 17:04:08 +00:00
2007-03-17 22:21:05 +00:00
dl - > num + + ;
}
2004-09-10 19:02:27 +00:00
2008-06-05 20:54:52 +00:00
if ( dl - > num = = 0 ) usererror ( " Invalid version of language packs " ) ;
2004-08-09 17:04:08 +00:00
2007-03-17 22:21:05 +00:00
/* We haven't found the language in the config nor the one in the locale.
* Now we set it to one of the fallback languages */
if ( chosen_language = = - 1 ) {
chosen_language = ( language_fallback ! = - 1 ) ? language_fallback : en_GB_fallback ;
}
2004-08-09 17:04:08 +00:00
2008-06-05 20:54:52 +00:00
if ( ! ReadLanguagePack ( chosen_language ) ) usererror ( " Can't read language pack '%s' " , dl - > ent [ chosen_language ] . file ) ;
2005-07-15 21:28:26 +00:00
}
2007-12-16 18:38:19 +00:00
2009-10-17 20:34:09 +00:00
/**
* Get the ISO language code of the currently loaded language .
* @ return the ISO code .
*/
const char * GetCurrentLanguageIsoCode ( )
{
return _langpack - > isocode ;
}
2007-12-16 18:38:19 +00:00
/**
* Check whether the currently loaded language pack
* uses characters that the currently loaded font
* does not support . If this is the case an error
* message will be shown in English . The error
* message will not be localized because that would
* mean it might use characters that are not in the
* font , which is the whole reason this check has
* been added .
*/
void CheckForMissingGlyphsInLoadedLanguagePack ( )
{
2008-11-24 18:53:17 +00:00
# ifdef WITH_FREETYPE
/* Reset to the original state; switching languages might cause us to
* automatically choose another font . This resets that choice . */
UninitFreeType ( ) ;
InitFreeType ( ) ;
2008-11-24 20:51:55 +00:00
bool retry = false ;
2008-11-24 18:53:17 +00:00
# endif
for ( ; ; ) {
const Sprite * question_mark = GetGlyph ( FS_NORMAL , ' ? ' ) ;
for ( uint i = 0 ; i ! = 32 ; i + + ) {
for ( uint j = 0 ; j < _langtab_num [ i ] ; j + + ) {
const char * string = _langpack_offs [ _langtab_start [ i ] + j ] ;
WChar c ;
while ( ( c = Utf8Consume ( & string ) ) ! = ' \0 ' ) {
if ( c = = SCC_SETX ) {
/*
2008-11-24 20:51:55 +00:00
* SetX is , together with SetXY as special character that
2008-11-24 18:53:17 +00:00
* uses the next ( two ) characters as data points . We have
* to skip those , otherwise the UTF8 reading will go
* haywire .
*/
string + + ;
} else if ( c = = SCC_SETXY ) {
string + = 2 ;
} else if ( IsPrintable ( c ) & & c ! = ' ? ' & & GetGlyph ( FS_NORMAL , c ) = = question_mark ) {
# ifdef WITH_FREETYPE
if ( ! retry ) {
/* We found an unprintable character... lets try whether we can
* find a fallback font that can print the characters in the
* current language . */
retry = true ;
FreeTypeSettings backup ;
memcpy ( & backup , & _freetype , sizeof ( backup ) ) ;
2009-10-17 22:36:43 +00:00
bool success = SetFallbackFont ( & _freetype , _langpack - > isocode , _langpack - > winlangid , string ) ;
2008-11-24 18:53:17 +00:00
if ( success ) {
UninitFreeType ( ) ;
InitFreeType ( ) ;
}
memcpy ( & _freetype , & backup , sizeof ( backup ) ) ;
if ( success ) continue ;
} else {
/* Our fallback font does miss characters too, so keep the
* user chosen font as that is more likely to be any good than
* the wild guess we made */
UninitFreeType ( ) ;
InitFreeType ( ) ;
}
# endif
/*
* The character is printable , but not in the normal font .
* This is the case we were testing for . In this case we
* have to show the error . As we do not want the string to
* be translated by the translators , we ' force ' it into the
* binary and ' load ' it via a BindCString . To do this
2009-02-09 02:57:15 +00:00
* properly we have to set the colour of the string ,
* otherwise we end up with a lot of artefacts . The colour
2008-11-24 18:53:17 +00:00
* ' character ' might change in the future , so for safety
* we just Utf8 Encode it into the string , which takes
* exactly three characters , so it replaces the " XXX " with
2009-02-09 02:57:15 +00:00
* the colour marker .
2008-11-24 18:53:17 +00:00
*/
static char * err_str = strdup ( " XXXThe current font is missing some of the characters used in the texts for this language. Read the readme to see how to solve this. " ) ;
Utf8Encode ( err_str , SCC_YELLOW ) ;
SetDParamStr ( 0 , err_str ) ;
2010-02-24 14:46:15 +00:00
ShowErrorMessage ( STR_JUST_RAW_STRING , INVALID_STRING_ID , WL_WARNING ) ;
2009-02-23 18:14:18 +00:00
/* Reset the font width */
LoadStringWidthTable ( ) ;
2008-11-24 18:53:17 +00:00
return ;
}
2007-12-16 18:38:19 +00:00
}
}
}
2008-11-24 18:53:17 +00:00
break ;
2007-12-16 18:38:19 +00:00
}
2008-10-17 17:42:51 +00:00
2009-02-23 18:14:18 +00:00
/* Update the font with cache */
LoadStringWidthTable ( ) ;
2008-10-17 17:42:51 +00:00
# if !defined(WITH_ICU)
/*
* For right - to - left languages we need the ICU library . If
* we do not have support for that library we warn the user
* about it with a message . As we do not want the string to
* be translated by the translators , we ' force ' it into the
* binary and ' load ' it via a BindCString . To do this
2009-02-09 02:57:15 +00:00
* properly we have to set the colour of the string ,
* otherwise we end up with a lot of artefacts . The colour
2008-10-17 17:42:51 +00:00
* ' character ' might change in the future , so for safety
* we just Utf8 Encode it into the string , which takes
* exactly three characters , so it replaces the " XXX " with
2009-02-09 02:57:15 +00:00
* the colour marker .
2008-10-17 17:42:51 +00:00
*/
if ( _dynlang . text_dir ! = TD_LTR ) {
static char * err_str = strdup ( " XXXThis version of OpenTTD does not support right-to-left languages. Recompile with icu enabled. " ) ;
Utf8Encode ( err_str , SCC_YELLOW ) ;
SetDParamStr ( 0 , err_str ) ;
2010-02-24 14:46:15 +00:00
ShowErrorMessage ( STR_JUST_RAW_STRING , INVALID_STRING_ID , WL_ERROR ) ;
2008-10-17 17:42:51 +00:00
}
# endif
2007-12-16 18:38:19 +00:00
}