# currency.cpp

Go to the documentation of this file.

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