src/currency.cpp
changeset 5584 1111b4d36e35
parent 5475 2e6990a8c7c4
child 6123 04eb770ec17e
equal deleted inserted replaced
5583:136d8764c7e6 5584:1111b4d36e35
       
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 #include "openttd.h"
       
     5 #include "currency.h"
       
     6 #include "news.h"
       
     7 #include "variables.h"
       
     8 #include "table/strings.h"
       
     9 #include "date.h"
       
    10 
       
    11 	//   exchange rate    prefix             symbol_pos
       
    12 	//   |  separator        |     postfix   |
       
    13 	//   |   |    Euro year  |       |       |    name
       
    14 	//   |   |    |          |       |       |    |
       
    15 static const CurrencySpec origin_currency_specs[NUM_CURRENCY] = {
       
    16 	{    1, ',', CF_NOEURO, "£",    "",      0,  STR_CURR_GBP    }, // british pounds
       
    17 	{    2, ',', CF_NOEURO, "$",    "",      0,  STR_CURR_USD    }, // us dollars
       
    18 	{    2, ',', CF_ISEURO, "€",    "",      0,  STR_CURR_EUR    }, // Euro
       
    19 	{  220, ',', CF_NOEURO, "¥",    "",      0,  STR_CURR_YEN    }, // yen
       
    20 	{   20, ',', 2002,      "",     " S.",   1,  STR_CURR_ATS    }, // austrian schilling
       
    21 	{   59, ',', 2002,      "BEF ", "",      0,  STR_CURR_BEF    }, // belgian franc
       
    22 	{    2, ',', CF_NOEURO, "CHF ", "",      0,  STR_CURR_CHF    }, // swiss franc
       
    23 	{   41, ',', CF_NOEURO, "",     " Kč",   1,  STR_CURR_CZK    }, // czech koruna
       
    24 	{    3, '.', 2002,      "DM ",  "",      0,  STR_CURR_DEM    }, // deutsche mark
       
    25 	{   11, '.', CF_NOEURO, "",     " kr",   1,  STR_CURR_DKK    }, // danish krone
       
    26 	{  245, '.', 2002,      "Pts ", "",      0,  STR_CURR_ESP    }, // spanish pesetas
       
    27 	{    9, ',', 2002,      "",     " mk",   1,  STR_CURR_FIM    }, // finnish markka
       
    28 	{   10, '.', 2002,      "FF ",  "",      0,  STR_CURR_FRF    }, // french francs
       
    29 	{  500, ',', 2002,      "",     "Dr.",   1,  STR_CURR_GRD    }, // greek drachma
       
    30 	{  378, ',', 2010,      "",     " Ft",   1,  STR_CURR_HUF    }, // hungarian forint
       
    31 	{  130, '.', CF_NOEURO, "",     " Kr",   1,  STR_CURR_ISK    }, // icelandic krona
       
    32 	{ 2850, ',', 2002,      "",     " L.",   1,  STR_CURR_ITL    }, // italian lira
       
    33 	{    3, ',', 2002,      "NLG ", "",      0,  STR_CURR_NLG    }, // dutch gulden
       
    34 	{   12, '.', CF_NOEURO, "",     " Kr",   1,  STR_CURR_NOK    }, // norwegian krone
       
    35 	{    6, ' ', CF_NOEURO, "",     " zl",   1,  STR_CURR_PLN    }, // polish zloty
       
    36 	{    5, '.', CF_NOEURO, "",     " Lei",  1,  STR_CURR_ROL    }, // romanian Lei
       
    37 	{   50, ' ', CF_NOEURO, "",     " p",    1,  STR_CURR_RUR    }, // russian rouble
       
    38 	{  352, '.', CF_NOEURO, "",     " SIT",  1,  STR_CURR_SIT    }, // slovenian tolar
       
    39 	{   13, '.', CF_NOEURO, "",     " Kr",   1,  STR_CURR_SEK    }, // swedish krona
       
    40 	{    3, '.', CF_NOEURO, "",     " YTL",  1,  STR_CURR_YTL    }, // turkish lira
       
    41 	{   52, ',', CF_NOEURO, "",     " Sk",   1,  STR_CURR_SKK    }, // slovak koruna
       
    42 	{    4, ',', CF_NOEURO, "R$ ",  "",      0,  STR_CURR_BRR    }, // brazil real
       
    43 	{    1, ' ', CF_NOEURO, "",     "",      2,  STR_CURR_CUSTOM }, // custom currency
       
    44 };
       
    45 
       
    46 /* Array of currencies used by the system */
       
    47 CurrencySpec _currency_specs[NUM_CURRENCY];
       
    48 
       
    49 /**
       
    50  * These enums are only declared in order to make sens
       
    51  * out of the TTDPatch_To_OTTDIndex array that will follow
       
    52  * Every currency used by Ottd is there, just in case TTDPatch will
       
    53  * add those missing in its code
       
    54  **/
       
    55 enum {
       
    56 	CURR_GBP,
       
    57 	CURR_USD,
       
    58 	CURR_EUR,
       
    59 	CURR_YEN,
       
    60 	CURR_ATS,
       
    61 	CURR_BEF,
       
    62 	CURR_CHF,
       
    63 	CURR_CZK,
       
    64 	CURR_DEM,
       
    65 	CURR_DKK,
       
    66 	CURR_ESP,
       
    67 	CURR_FIM,
       
    68 	CURR_FRF,
       
    69 	CURR_GRD,
       
    70 	CURR_HUF,
       
    71 	CURR_ISK,
       
    72 	CURR_ITL,
       
    73 	CURR_NLG,
       
    74 	CURR_NOK,
       
    75 	CURR_PLN,
       
    76 	CURR_ROL,
       
    77 	CURR_RUR,
       
    78 	CURR_SIT,
       
    79 	CURR_SEK,
       
    80 	CURR_YTL,
       
    81 };
       
    82 
       
    83 /**
       
    84  * This array represent the position of OpenTTD's currencies,
       
    85  * compared to TTDPatch's ones.
       
    86  * When a grf sends currencies, they are based on the order defined by TTDPatch.
       
    87  * So, we must reindex them to our own order.
       
    88  **/
       
    89 const byte TTDPatch_To_OTTDIndex[] =
       
    90 {
       
    91 	CURR_GBP,
       
    92 	CURR_USD,
       
    93 	CURR_FRF,
       
    94 	CURR_DEM,
       
    95 	CURR_YEN,
       
    96 	CURR_ESP,
       
    97 	CURR_HUF,
       
    98 	CURR_PLN,
       
    99 	CURR_ATS,
       
   100 	CURR_BEF,
       
   101 	CURR_DKK,
       
   102 	CURR_FIM,
       
   103 	CURR_GRD,
       
   104 	CURR_CHF,
       
   105 	CURR_NLG,
       
   106 	CURR_ITL,
       
   107 	CURR_SEK,
       
   108 	CURR_RUR,
       
   109 	CURR_EUR,
       
   110 };
       
   111 
       
   112 /**
       
   113  * Will return the ottd's index correspondance to
       
   114  * the ttdpatch's id.  If the id is bigger then the array,
       
   115  * it is  a grf written for ottd, thus returning the same id.
       
   116  * Only called from newgrf.c
       
   117  * @param grfcurr_id currency id coming from newgrf
       
   118  * @return the corrected index
       
   119  **/
       
   120 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
       
   121 {
       
   122 	return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
       
   123 }
       
   124 
       
   125 /* get a mask of the allowed currencies depending on the year */
       
   126 uint GetMaskOfAllowedCurrencies(void)
       
   127 {
       
   128 	uint mask = 0;
       
   129 	uint i;
       
   130 
       
   131 	for (i = 0; i < NUM_CURRENCY; i++) {
       
   132 		Year to_euro = _currency_specs[i].to_euro;
       
   133 
       
   134 		if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
       
   135 		if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
       
   136 		mask |= (1 << i);
       
   137 	}
       
   138 	mask |= (1 << CUSTOM_CURRENCY_ID); // always allow custom currency
       
   139 	return mask;
       
   140 }
       
   141 
       
   142 /**
       
   143  * Verify if the currency chosen by the user is about to be converted to Euro
       
   144  **/
       
   145 void CheckSwitchToEuro(void)
       
   146 {
       
   147 	if (_currency_specs[_opt.currency].to_euro != CF_NOEURO &&
       
   148 			_currency_specs[_opt.currency].to_euro != CF_ISEURO &&
       
   149 			_cur_year >= _currency_specs[_opt.currency].to_euro) {
       
   150 		_opt.currency = 2; // this is the index of euro above.
       
   151 		AddNewsItem(STR_EURO_INTRODUCE, NEWS_FLAGS(NM_NORMAL, 0, NT_ECONOMY, 0), 0, 0);
       
   152 	}
       
   153 }
       
   154 
       
   155 /**
       
   156  * Called only from newgrf.c.  Will fill _currency_specs array with
       
   157  * default values from origin_currency_specs
       
   158  **/
       
   159 void ResetCurrencies(void)
       
   160 {
       
   161 	memcpy(&_currency_specs, &origin_currency_specs, sizeof(origin_currency_specs));
       
   162 }
       
   163 
       
   164 /**
       
   165  * Build a list of currency names StringIDs to use in a dropdown list
       
   166  * @return Pointer to a (static) array of StringIDs
       
   167  */
       
   168 StringID* BuildCurrencyDropdown(void)
       
   169 {
       
   170 	/* Allow room for all currencies, plus a terminator entry */
       
   171 	static StringID names[CUSTOM_CURRENCY_ID];
       
   172 	uint i;
       
   173 
       
   174 	/* Add each name */
       
   175 	for (i = 0; i < NUM_CURRENCY; i++) {
       
   176 		names[i] = _currency_specs[i].name;
       
   177 	}
       
   178 	/* Terminate the list */
       
   179 	names[i] = INVALID_STRING_ID;
       
   180 
       
   181 	return names;
       
   182 }