# currency.cpp

Go to the documentation of this file.

    00001 /* $Id: currency.cpp 18434 2009-12-08 20:12:43Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "currency.h"
    00014 #include "news_func.h"
    00015 #include "settings_type.h"
    00016 #include "date_func.h"
    00017
    00018 #include "table/strings.h"
    00019
    00020   /*   exchange rate    prefix                      symbol_pos
    00021    *   |  separator        |           postfix          |
    00022    *   |   |   Euro year   |              |             | name
    00023    *   |   |    |          |              |             |  | */
    00024 static const CurrencySpec origin_currency_specs[NUM_CURRENCY] = {
    00025   {    1, "", CF_NOEURO, "\xC2\xA3",     "",           0, STR_GAME_OPTIONS_CURRENCY_GBP    },
    00026   {    2, "", CF_NOEURO, "$",            "",           0, STR_GAME_OPTIONS_CURRENCY_USD    },
    00027   {    2, "", CF_ISEURO, "\xE2\x82\xAC", "",           0, STR_GAME_OPTIONS_CURRENCY_EUR    },
    00028   {  220, "", CF_NOEURO, "\xC2\xA5",     "",           0, STR_GAME_OPTIONS_CURRENCY_YEN    },
    00029   {   20, "", 2002,      "",             " S.",        1, STR_GAME_OPTIONS_CURRENCY_ATS    },
    00030   {   59, "", 2002,      "BEF ",         "",           0, STR_GAME_OPTIONS_CURRENCY_BEF    },
    00031   {    2, "", CF_NOEURO, "CHF ",         "",           0, STR_GAME_OPTIONS_CURRENCY_CHF    },
    00032   {   41, "", CF_NOEURO, "",             " K\xC4\x8D", 1, STR_GAME_OPTIONS_CURRENCY_CZK    },
    00033   {    3, "", 2002,      "DM ",          "",           0, STR_GAME_OPTIONS_CURRENCY_DEM    },
    00034   {   11, "", CF_NOEURO, "",             " kr",        1, STR_GAME_OPTIONS_CURRENCY_DKK    },
    00035   {  245, "", 2002,      "Pts ",         "",           0, STR_GAME_OPTIONS_CURRENCY_ESP    },
    00036   {    9, "", 2002,      "",             " mk",        1, STR_GAME_OPTIONS_CURRENCY_FIM    },
    00037   {   10, "", 2002,      "FF ",          "",           0, STR_GAME_OPTIONS_CURRENCY_FRF    },
    00038   {  500, "", 2002,      "",             "Dr.",        1, STR_GAME_OPTIONS_CURRENCY_GRD    },
    00039   {  378, "", CF_NOEURO, "",             " Ft",        1, STR_GAME_OPTIONS_CURRENCY_HUF    },
    00040   {  130, "", CF_NOEURO, "",             " Kr",        1, STR_GAME_OPTIONS_CURRENCY_ISK    },
    00041   { 2850, "", 2002,      "",             " L.",        1, STR_GAME_OPTIONS_CURRENCY_ITL    },
    00042   {    3, "", 2002,      "NLG ",         "",           0, STR_GAME_OPTIONS_CURRENCY_NLG    },
    00043   {   12, "", CF_NOEURO, "",             " Kr",        1, STR_GAME_OPTIONS_CURRENCY_NOK    },
    00044   {    6, "", CF_NOEURO, "",             " z\xC5\x82", 1, STR_GAME_OPTIONS_CURRENCY_PLN    },
    00045   {    5, "", CF_NOEURO, "",             " Lei",       1, STR_GAME_OPTIONS_CURRENCY_RON    },
    00046   {   50, "", CF_NOEURO, "",             " p",         1, STR_GAME_OPTIONS_CURRENCY_RUR    },
    00047   {  352, "", 2007,      "",             " SIT",       1, STR_GAME_OPTIONS_CURRENCY_SIT    },
    00048   {   13, "", CF_NOEURO, "",             " Kr",        1, STR_GAME_OPTIONS_CURRENCY_SEK    },
    00049   {    3, "", CF_NOEURO, "",             " TL",        1, STR_GAME_OPTIONS_CURRENCY_TRY    },
    00050   {   52, "", 2009,      "",             " Sk",        1, STR_GAME_OPTIONS_CURRENCY_SKK    },
    00051   {    4, "", CF_NOEURO, "R$ ",          "",           0, STR_GAME_OPTIONS_CURRENCY_BRL    },
    00052   {   20, "", CF_NOEURO, "",             " EEK",       1, STR_GAME_OPTIONS_CURRENCY_EEK    },
    00053   {    1, "", CF_NOEURO, "",             "",           2, STR_GAME_OPTIONS_CURRENCY_CUSTOM },
    00054 };
    00055
    00056 /* Array of currencies used by the system */
    00057 CurrencySpec _currency_specs[NUM_CURRENCY];
    00058
    00065 enum {
    00066   CURR_GBP,
    00067   CURR_USD,
    00068   CURR_EUR,
    00069   CURR_YEN,
    00070   CURR_ATS,
    00071   CURR_BEF,
    00072   CURR_CHF,
    00073   CURR_CZK,
    00074   CURR_DEM,
    00075   CURR_DKK,
    00076   CURR_ESP,
    00077   CURR_FIM,
    00078   CURR_FRF,
    00079   CURR_GRD,
    00080   CURR_HUF,
    00081   CURR_ISK,
    00082   CURR_ITL,
    00083   CURR_NLG,
    00084   CURR_NOK,
    00085   CURR_PLN,
    00086   CURR_RON,
    00087   CURR_RUR,
    00088   CURR_SIT,
    00089   CURR_SEK,
    00090   CURR_YTL,
    00091   CURR_SKK,
    00092   CURR_BRL,
    00093   CURR_EEK,
    00094 };
    00095
    00102 const byte TTDPatch_To_OTTDIndex[] =
    00103 {
    00104   CURR_GBP,
    00105   CURR_USD,
    00106   CURR_FRF,
    00107   CURR_DEM,
    00108   CURR_YEN,
    00109   CURR_ESP,
    00110   CURR_HUF,
    00111   CURR_PLN,
    00112   CURR_ATS,
    00113   CURR_BEF,
    00114   CURR_DKK,
    00115   CURR_FIM,
    00116   CURR_GRD,
    00117   CURR_CHF,
    00118   CURR_NLG,
    00119   CURR_ITL,
    00120   CURR_SEK,
    00121   CURR_RUR,
    00122   CURR_EUR,
    00123 };
    00124
    00133 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
    00134 {
    00135   return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
    00136 }
    00137
    00142 uint GetMaskOfAllowedCurrencies()
    00143 {
    00144   uint mask = 0;
    00145   uint i;
    00146
    00147   for (i = 0; i < NUM_CURRENCY; i++) {
    00148     Year to_euro = _currency_specs[i].to_euro;
    00149
    00150     if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
    00151     if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
    00152     mask |= (1 << i);
    00153   }
    00154   mask |= (1 << CUSTOM_CURRENCY_ID); // always allow custom currency
    00155   return mask;
    00156 }
    00157
    00161 void CheckSwitchToEuro()
    00162 {
    00163   if (_currency_specs[_settings_game.locale.currency].to_euro != CF_NOEURO &&
    00164       _currency_specs[_settings_game.locale.currency].to_euro != CF_ISEURO &&
    00165       _cur_year >= _currency_specs[_settings_game.locale.currency].to_euro) {
    00166     _settings_game.locale.currency = 2; // this is the index of euro above.
    00167     AddNewsItem(STR_NEWS_EURO_INTRODUCTION, NS_ECONOMY);
    00168   }
    00169 }
    00170
    00180 void ResetCurrencies(bool preserve_custom)
    00181 {
    00182   memcpy(&_currency_specs, &origin_currency_specs, sizeof(origin_currency_specs) - (preserve_custom ? sizeof(_custom_currency) : 0));
    00183 }
    00184
    00189 StringID *BuildCurrencyDropdown()
    00190 {
    00191   /* Allow room for all currencies, plus a terminator entry */
    00192   static StringID names[NUM_CURRENCY + 1];
    00193   uint i;
    00194
    00195   /* Add each name */
    00196   for (i = 0; i < NUM_CURRENCY; i++) {
    00197     names[i] = _currency_specs[i].name;
    00198   }
    00199   /* Terminate the list */
    00200   names[i] = INVALID_STRING_ID;
    00201
    00202   return names;
    00203 }