# currency.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 "core/bitmath_func.hpp"
    00014
    00015 #include "currency.h"
    00016 #include "news_func.h"
    00017 #include "settings_type.h"
    00018 #include "date_func.h"
    00019 #include "string_type.h"
    00020
    00021 #include "table/strings.h"
    00022
    00023   /*   exchange rate    prefix                         symbol_pos
    00024    *   |  separator        |           postfix             |
    00025    *   |   |   Euro year   |              |                | name
    00026    *   |   |    |          |              |                |  | */
    00028 static const CurrencySpec origin_currency_specs[CURRENCY_END] = {
    00029   {    1, "", CF_NOEURO, "\xC2\xA3",     "",               0, STR_GAME_OPTIONS_CURRENCY_GBP    },
    00030   {    2, "", CF_NOEURO, "$",            "",               0, STR_GAME_OPTIONS_CURRENCY_USD    },
    00031   {    2, "", CF_ISEURO, "\xE2\x82\xAC", "",               0, STR_GAME_OPTIONS_CURRENCY_EUR    },
    00032   {  220, "", CF_NOEURO, "\xC2\xA5",     "",               0, STR_GAME_OPTIONS_CURRENCY_JPY    },
    00033   {   27, "", 2002,      "",             NBSP "S.",        1, STR_GAME_OPTIONS_CURRENCY_ATS    },
    00034   {   81, "", 2002,      "BEF" NBSP,     "",               0, STR_GAME_OPTIONS_CURRENCY_BEF    },
    00035   {    2, "", CF_NOEURO, "CHF" NBSP,     "",               0, STR_GAME_OPTIONS_CURRENCY_CHF    },
    00036   {   41, "", CF_NOEURO, "",             NBSP "K\xC4\x8D", 1, STR_GAME_OPTIONS_CURRENCY_CZK    },
    00037   {    4, "", 2002,      "DM" NBSP,      "",               0, STR_GAME_OPTIONS_CURRENCY_DEM    },
    00038   {   11, "", CF_NOEURO, "",             NBSP "kr",        1, STR_GAME_OPTIONS_CURRENCY_DKK    },
    00039   {  333, "", 2002,      "Pts" NBSP,     "",               0, STR_GAME_OPTIONS_CURRENCY_ESP    },
    00040   {   12, "", 2002,      "",             NBSP "mk",        1, STR_GAME_OPTIONS_CURRENCY_FIM    },
    00041   {   13, "", 2002,      "FF" NBSP,      "",               0, STR_GAME_OPTIONS_CURRENCY_FRF    },
    00042   {  681, "", 2002,      "",             "Dr.",            1, STR_GAME_OPTIONS_CURRENCY_GRD    },
    00043   {  378, "", CF_NOEURO, "",             NBSP "Ft",        1, STR_GAME_OPTIONS_CURRENCY_HUF    },
    00044   {  130, "", CF_NOEURO, "",             NBSP "Kr",        1, STR_GAME_OPTIONS_CURRENCY_ISK    },
    00045   { 3873, "", 2002,      "",             NBSP "L.",        1, STR_GAME_OPTIONS_CURRENCY_ITL    },
    00046   {    4, "", 2002,      "NLG" NBSP,     "",               0, STR_GAME_OPTIONS_CURRENCY_NLG    },
    00047   {   12, "", CF_NOEURO, "",             NBSP "Kr",        1, STR_GAME_OPTIONS_CURRENCY_NOK    },
    00048   {    6, "", CF_NOEURO, "",             NBSP "z\xC5\x82", 1, STR_GAME_OPTIONS_CURRENCY_PLN    },
    00049   {    5, "", CF_NOEURO, "",             NBSP "Lei",       1, STR_GAME_OPTIONS_CURRENCY_RON    },
    00050   {   50, "", CF_NOEURO, "",             NBSP "p",         1, STR_GAME_OPTIONS_CURRENCY_RUR    },
    00051   {  479, "", 2007,      "",             NBSP "SIT",       1, STR_GAME_OPTIONS_CURRENCY_SIT    },
    00052   {   13, "", CF_NOEURO, "",             NBSP "Kr",        1, STR_GAME_OPTIONS_CURRENCY_SEK    },
    00053   {    3, "", CF_NOEURO, "",             NBSP "TL",        1, STR_GAME_OPTIONS_CURRENCY_TRY    },
    00054   {   60, "", 2009,      "",             NBSP "Sk",        1, STR_GAME_OPTIONS_CURRENCY_SKK    },
    00055   {    4, "", CF_NOEURO, "R$" NBSP,      "",               0, STR_GAME_OPTIONS_CURRENCY_BRL    },
    00056   {   31, "", 2011,      "",             NBSP "EEK",       1, STR_GAME_OPTIONS_CURRENCY_EEK    },
    00057   {    4, "", 2014,      "",             NBSP "Lt",        1, STR_GAME_OPTIONS_CURRENCY_LTL    },
    00058   { 1850, "", CF_NOEURO, "\xE2\x82\xA9", "",               0, STR_GAME_OPTIONS_CURRENCY_KRW    },
    00059   {   13, "", CF_NOEURO, "R" NBSP,       "",               0, STR_GAME_OPTIONS_CURRENCY_ZAR    },
    00060   {    1, "", CF_NOEURO, "",             "",               2, STR_GAME_OPTIONS_CURRENCY_CUSTOM },
    00061   {    3, "", CF_NOEURO, "",             NBSP "GEL",       1, STR_GAME_OPTIONS_CURRENCY_GEL    },
    00062   { 4901, "", CF_NOEURO, "",             NBSP "Rls",       1, STR_GAME_OPTIONS_CURRENCY_IRR    },
    00063 };
    00064
    00066 CurrencySpec _currency_specs[CURRENCY_END];
    00067
    00074 const byte TTDPatch_To_OTTDIndex[] =
    00075 {
    00076   CURRENCY_GBP,
    00077   CURRENCY_USD,
    00078   CURRENCY_FRF,
    00079   CURRENCY_DEM,
    00080   CURRENCY_JPY,
    00081   CURRENCY_ESP,
    00082   CURRENCY_HUF,
    00083   CURRENCY_PLN,
    00084   CURRENCY_ATS,
    00085   CURRENCY_BEF,
    00086   CURRENCY_DKK,
    00087   CURRENCY_FIM,
    00088   CURRENCY_GRD,
    00089   CURRENCY_CHF,
    00090   CURRENCY_NLG,
    00091   CURRENCY_ITL,
    00092   CURRENCY_SEK,
    00093   CURRENCY_RUR,
    00094   CURRENCY_EUR,
    00095 };
    00096
    00105 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
    00106 {
    00107   return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
    00108 }
    00109
    00114 uint64 GetMaskOfAllowedCurrencies()
    00115 {
    00116   uint64 mask = 0LL;
    00117   uint i;
    00118
    00119   for (i = 0; i < CURRENCY_END; i++) {
    00120     Year to_euro = _currency_specs[i].to_euro;
    00121
    00122     if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
    00123     if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
    00124     SetBit(mask, i);
    00125   }
    00126   SetBit(mask, CURRENCY_CUSTOM); // always allow custom currency
    00127   return mask;
    00128 }
    00129
    00133 void CheckSwitchToEuro()
    00134 {
    00135   if (_currency_specs[_settings_game.locale.currency].to_euro != CF_NOEURO &&
    00136       _currency_specs[_settings_game.locale.currency].to_euro != CF_ISEURO &&
    00137       _cur_year >= _currency_specs[_settings_game.locale.currency].to_euro) {
    00138     _settings_game.locale.currency = 2; // this is the index of euro above.
    00139     AddNewsItem(STR_NEWS_EURO_INTRODUCTION, NT_ECONOMY, NF_NORMAL);
    00140   }
    00141 }
    00142
    00152 void ResetCurrencies(bool preserve_custom)
    00153 {
    00154   memcpy(&_currency_specs, &origin_currency_specs, sizeof(origin_currency_specs) - (preserve_custom ? sizeof(_custom_currency) : 0));
    00155 }
    00156
    00161 StringID *BuildCurrencyDropdown()
    00162 {
    00163   /* Allow room for all currencies, plus a terminator entry */
    00164   static StringID names[CURRENCY_END + 1];
    00165   uint i;
    00166
    00167   /* Add each name */
    00168   for (i = 0; i < CURRENCY_END; i++) {
    00169     names[i] = _currency_specs[i].name;
    00170   }
    00171   /* Terminate the list */
    00172   names[i] = INVALID_STRING_ID;
    00173
    00174   return names;
    00175 }