# currency.cpp

Go to the documentation of this file.

    00001 /* $Id: currency.cpp 16061 2009-04-14 21:20:03Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "currency.h"
    00007 #include "news_func.h"
    00008 #include "settings_type.h"
    00009 #include "date_func.h"
    00010
    00011 #include "table/strings.h"
    00012
    00013   /*   exchange rate    prefix                      symbol_pos
    00014    *   |  separator        |           postfix          |
    00015    *   |   |   Euro year   |              |             | name
    00016    *   |   |    |          |              |             |  | */
    00017 static const CurrencySpec origin_currency_specs[NUM_CURRENCY] = {
    00018   {    1, ',', CF_NOEURO, "\xC2\xA3",     "",           0, STR_CURR_GBP    },
    00019   {    2, ',', CF_NOEURO, "$",            "",           0, STR_CURR_USD    },
    00020   {    2, ',', CF_ISEURO, "\xE2\x82\xAC", "",           0, STR_CURR_EUR    },
    00021   {  220, ',', CF_NOEURO, "\xC2\xA5",     "",           0, STR_CURR_YEN    },
    00022   {   20, ',', 2002,      "",             " S.",        1, STR_CURR_ATS    },
    00023   {   59, ',', 2002,      "BEF ",         "",           0, STR_CURR_BEF    },
    00024   {    2, ',', CF_NOEURO, "CHF ",         "",           0, STR_CURR_CHF    },
    00025   {   41, ',', CF_NOEURO, "",             " K\xC4\x8D", 1, STR_CURR_CZK    },
    00026   {    3, '.', 2002,      "DM ",          "",           0, STR_CURR_DEM    },
    00027   {   11, '.', CF_NOEURO, "",             " kr",        1, STR_CURR_DKK    },
    00028   {  245, '.', 2002,      "Pts ",         "",           0, STR_CURR_ESP    },
    00029   {    9, ',', 2002,      "",             " mk",        1, STR_CURR_FIM    },
    00030   {   10, '.', 2002,      "FF ",          "",           0, STR_CURR_FRF    },
    00031   {  500, ',', 2002,      "",             "Dr.",        1, STR_CURR_GRD    },
    00032   {  378, ',', CF_NOEURO, "",             " Ft",        1, STR_CURR_HUF    },
    00033   {  130, '.', CF_NOEURO, "",             " Kr",        1, STR_CURR_ISK    },
    00034   { 2850, ',', 2002,      "",             " L.",        1, STR_CURR_ITL    },
    00035   {    3, ',', 2002,      "NLG ",         "",           0, STR_CURR_NLG    },
    00036   {   12, '.', CF_NOEURO, "",             " Kr",        1, STR_CURR_NOK    },
    00037   {    6, ' ', CF_NOEURO, "",             " zl",        1, STR_CURR_PLN    },
    00038   {    5, '.', CF_NOEURO, "",             " Lei",       1, STR_CURR_RON    },
    00039   {   50, ' ', CF_NOEURO, "",             " p",         1, STR_CURR_RUR    },
    00040   {  352, '.', 2007,      "",             " SIT",       1, STR_CURR_SIT    },
    00041   {   13, '.', CF_NOEURO, "",             " Kr",        1, STR_CURR_SEK    },
    00042   {    3, '.', CF_NOEURO, "",             " TL",        1, STR_CURR_TRY    },
    00043   {   52, ',', 2009,      "",             " Sk",        1, STR_CURR_SKK    },
    00044   {    4, ',', CF_NOEURO, "R$ ",          "",           0, STR_CURR_BRL    },
    00045   {   20, '.', CF_NOEURO, "",             " EEK",       1, STR_CURR_EEK    },
    00046   {    1, ' ', CF_NOEURO, "",             "",           2, STR_CURR_CUSTOM },
    00047 };
    00048
    00049 /* Array of currencies used by the system */
    00050 CurrencySpec _currency_specs[NUM_CURRENCY];
    00051
    00058 enum {
    00059   CURR_GBP,
    00060   CURR_USD,
    00061   CURR_EUR,
    00062   CURR_YEN,
    00063   CURR_ATS,
    00064   CURR_BEF,
    00065   CURR_CHF,
    00066   CURR_CZK,
    00067   CURR_DEM,
    00068   CURR_DKK,
    00069   CURR_ESP,
    00070   CURR_FIM,
    00071   CURR_FRF,
    00072   CURR_GRD,
    00073   CURR_HUF,
    00074   CURR_ISK,
    00075   CURR_ITL,
    00076   CURR_NLG,
    00077   CURR_NOK,
    00078   CURR_PLN,
    00079   CURR_RON,
    00080   CURR_RUR,
    00081   CURR_SIT,
    00082   CURR_SEK,
    00083   CURR_YTL,
    00084   CURR_SKK,
    00085   CURR_BRL,
    00086   CURR_EEK,
    00087 };
    00088
    00095 const byte TTDPatch_To_OTTDIndex[] =
    00096 {
    00097   CURR_GBP,
    00098   CURR_USD,
    00099   CURR_FRF,
    00100   CURR_DEM,
    00101   CURR_YEN,
    00102   CURR_ESP,
    00103   CURR_HUF,
    00104   CURR_PLN,
    00105   CURR_ATS,
    00106   CURR_BEF,
    00107   CURR_DKK,
    00108   CURR_FIM,
    00109   CURR_GRD,
    00110   CURR_CHF,
    00111   CURR_NLG,
    00112   CURR_ITL,
    00113   CURR_SEK,
    00114   CURR_RUR,
    00115   CURR_EUR,
    00116 };
    00117
    00126 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
    00127 {
    00128   return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
    00129 }
    00130
    00135 uint GetMaskOfAllowedCurrencies()
    00136 {
    00137   uint mask = 0;
    00138   uint i;
    00139
    00140   for (i = 0; i < NUM_CURRENCY; i++) {
    00141     Year to_euro = _currency_specs[i].to_euro;
    00142
    00143     if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
    00144     if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
    00145     mask |= (1 << i);
    00146   }
    00147   mask |= (1 << CUSTOM_CURRENCY_ID); // always allow custom currency
    00148   return mask;
    00149 }
    00150
    00154 void CheckSwitchToEuro()
    00155 {
    00156   if (_currency_specs[_settings_game.locale.currency].to_euro != CF_NOEURO &&
    00157       _currency_specs[_settings_game.locale.currency].to_euro != CF_ISEURO &&
    00158       _cur_year >= _currency_specs[_settings_game.locale.currency].to_euro) {
    00159     _settings_game.locale.currency = 2; // this is the index of euro above.
    00160     AddNewsItem(STR_EURO_INTRODUCE, NS_ECONOMY, 0, 0);
    00161   }
    00162 }
    00163
    00173 void ResetCurrencies(bool preserve_custom)
    00174 {
    00175   memcpy(&_currency_specs, &origin_currency_specs, sizeof(origin_currency_specs) - (preserve_custom ? sizeof(_custom_currency) : 0));
    00176 }
    00177
    00182 StringID *BuildCurrencyDropdown()
    00183 {
    00184   /* Allow room for all currencies, plus a terminator entry */
    00185   static StringID names[NUM_CURRENCY + 1];
    00186   uint i;
    00187
    00188   /* Add each name */
    00189   for (i = 0; i < NUM_CURRENCY; i++) {
    00190     names[i] = _currency_specs[i].name;
    00191   }
    00192   /* Terminate the list */
    00193   names[i] = INVALID_STRING_ID;
    00194
    00195   return names;
    00196 }