# currency.cpp

Go to the documentation of this file.

    00001 /* $Id: currency.cpp 11834 2008-01-13 14:37:30Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "currency.h"
    00008 #include "news.h"
    00009 #include "settings_type.h"
    00010 #include "date_func.h"
    00011
    00012 #include "table/strings.h"
    00013
    00014   //   exchange rate    prefix             symbol_pos
    00015   //   |  separator        |     postfix   |
    00016   //   |   |    Euro year  |       |       |    name
    00017   //   |   |    |          |       |       |    |
    00018 static const CurrencySpec origin_currency_specs[NUM_CURRENCY] = {
    00019   {    1, ',', CF_NOEURO, "£",    "",      0,  STR_CURR_GBP    },
    00020   {    2, ',', CF_NOEURO, "$",    "",      0,  STR_CURR_USD    },
    00021   {    2, ',', CF_ISEURO, "€",    "",      0,  STR_CURR_EUR    },
    00022   {  220, ',', CF_NOEURO, "¥",    "",      0,  STR_CURR_YEN    },
    00023   {   20, ',', 2002,      "",     " S.",   1,  STR_CURR_ATS    },
    00024   {   59, ',', 2002,      "BEF ", "",      0,  STR_CURR_BEF    },
    00025   {    2, ',', CF_NOEURO, "CHF ", "",      0,  STR_CURR_CHF    },
    00026   {   41, ',', CF_NOEURO, "",     " Kč",   1,  STR_CURR_CZK    },
    00027   {    3, '.', 2002,      "DM ",  "",      0,  STR_CURR_DEM    },
    00028   {   11, '.', CF_NOEURO, "",     " kr",   1,  STR_CURR_DKK    },
    00029   {  245, '.', 2002,      "Pts ", "",      0,  STR_CURR_ESP    },
    00030   {    9, ',', 2002,      "",     " mk",   1,  STR_CURR_FIM    },
    00031   {   10, '.', 2002,      "FF ",  "",      0,  STR_CURR_FRF    },
    00032   {  500, ',', 2002,      "",     "Dr.",   1,  STR_CURR_GRD    },
    00033   {  378, ',', 2010,      "",     " Ft",   1,  STR_CURR_HUF    },
    00034   {  130, '.', CF_NOEURO, "",     " Kr",   1,  STR_CURR_ISK    },
    00035   { 2850, ',', 2002,      "",     " L.",   1,  STR_CURR_ITL    },
    00036   {    3, ',', 2002,      "NLG ", "",      0,  STR_CURR_NLG    },
    00037   {   12, '.', CF_NOEURO, "",     " Kr",   1,  STR_CURR_NOK    },
    00038   {    6, ' ', CF_NOEURO, "",     " zl",   1,  STR_CURR_PLN    },
    00039   {    5, '.', CF_NOEURO, "",     " Lei",  1,  STR_CURR_ROL    },
    00040   {   50, ' ', CF_NOEURO, "",     " p",    1,  STR_CURR_RUR    },
    00041   {  352, '.', CF_NOEURO, "",     " SIT",  1,  STR_CURR_SIT    },
    00042   {   13, '.', CF_NOEURO, "",     " Kr",   1,  STR_CURR_SEK    },
    00043   {    3, '.', CF_NOEURO, "",     " YTL",  1,  STR_CURR_YTL    },
    00044   {   52, ',', CF_NOEURO, "",     " Sk",   1,  STR_CURR_SKK    },
    00045   {    4, ',', CF_NOEURO, "R$ ",  "",      0,  STR_CURR_BRR    },
    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_ROL,
    00080   CURR_RUR,
    00081   CURR_SIT,
    00082   CURR_SEK,
    00083   CURR_YTL,
    00084 };
    00085
    00092 const byte TTDPatch_To_OTTDIndex[] =
    00093 {
    00094   CURR_GBP,
    00095   CURR_USD,
    00096   CURR_FRF,
    00097   CURR_DEM,
    00098   CURR_YEN,
    00099   CURR_ESP,
    00100   CURR_HUF,
    00101   CURR_PLN,
    00102   CURR_ATS,
    00103   CURR_BEF,
    00104   CURR_DKK,
    00105   CURR_FIM,
    00106   CURR_GRD,
    00107   CURR_CHF,
    00108   CURR_NLG,
    00109   CURR_ITL,
    00110   CURR_SEK,
    00111   CURR_RUR,
    00112   CURR_EUR,
    00113 };
    00114
    00123 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
    00124 {
    00125   return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
    00126 }
    00127
    00132 uint GetMaskOfAllowedCurrencies()
    00133 {
    00134   uint mask = 0;
    00135   uint i;
    00136
    00137   for (i = 0; i < NUM_CURRENCY; i++) {
    00138     Year to_euro = _currency_specs[i].to_euro;
    00139
    00140     if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
    00141     if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
    00142     mask |= (1 << i);
    00143   }
    00144   mask |= (1 << CUSTOM_CURRENCY_ID); // always allow custom currency
    00145   return mask;
    00146 }
    00147
    00151 void CheckSwitchToEuro()
    00152 {
    00153   if (_currency_specs[_opt.currency].to_euro != CF_NOEURO &&
    00154       _currency_specs[_opt.currency].to_euro != CF_ISEURO &&
    00155       _cur_year >= _currency_specs[_opt.currency].to_euro) {
    00156     _opt.currency = 2; // this is the index of euro above.
    00157     AddNewsItem(STR_EURO_INTRODUCE, NEWS_FLAGS(NM_NORMAL, 0, NT_ECONOMY, 0), 0, 0);
    00158   }
    00159 }
    00160
    00170 void ResetCurrencies(bool preserve_custom)
    00171 {
    00172   memcpy(&_currency_specs, &origin_currency_specs, sizeof(origin_currency_specs) - (preserve_custom ? sizeof(_custom_currency) : 0));
    00173 }
    00174
    00179 StringID* BuildCurrencyDropdown()
    00180 {
    00181   /* Allow room for all currencies, plus a terminator entry */
    00182   static StringID names[NUM_CURRENCY + 1];
    00183   uint i;
    00184
    00185   /* Add each name */
    00186   for (i = 0; i < NUM_CURRENCY; i++) {
    00187     names[i] = _currency_specs[i].name;
    00188   }
    00189   /* Terminate the list */
    00190   names[i] = INVALID_STRING_ID;
    00191
    00192   return names;
    00193 }