Add: INR currency (#8136)
[openttd-github.git] / src / currency.cpp
blobb68584f01535e87936fc867ca21f2decb0a163bd
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file currency.cpp Support for different currencies. */
10 #include "stdafx.h"
11 #include "core/bitmath_func.hpp"
13 #include "currency.h"
14 #include "news_func.h"
15 #include "settings_type.h"
16 #include "date_func.h"
17 #include "string_type.h"
19 #include "table/strings.h"
21 #include "safeguards.h"
23 /* exchange rate prefix symbol_pos
24 * | separator | postfix |
25 * | | Euro year | | | name
26 * | | | | | | | */
27 /** The original currency specifications. */
28 static const CurrencySpec origin_currency_specs[CURRENCY_END] = {
29 { 1, "", CF_NOEURO, "\xC2\xA3", "", 0, STR_GAME_OPTIONS_CURRENCY_GBP }, ///< british pound
30 { 2, "", CF_NOEURO, "$", "", 0, STR_GAME_OPTIONS_CURRENCY_USD }, ///< american dollar
31 { 2, "", CF_ISEURO, "\xE2\x82\xAC", "", 0, STR_GAME_OPTIONS_CURRENCY_EUR }, ///< euro
32 { 220, "", CF_NOEURO, "\xC2\xA5", "", 0, STR_GAME_OPTIONS_CURRENCY_JPY }, ///< japanese yen
33 { 27, "", 2002, "", NBSP "S.", 1, STR_GAME_OPTIONS_CURRENCY_ATS }, ///< austrian schilling
34 { 81, "", 2002, "BEF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_BEF }, ///< belgian franc
35 { 2, "", CF_NOEURO, "CHF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_CHF }, ///< swiss franc
36 { 41, "", CF_NOEURO, "", NBSP "K\xC4\x8D", 1, STR_GAME_OPTIONS_CURRENCY_CZK }, ///< czech koruna
37 { 4, "", 2002, "DM" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_DEM }, ///< deutsche mark
38 { 11, "", CF_NOEURO, "", NBSP "kr", 1, STR_GAME_OPTIONS_CURRENCY_DKK }, ///< danish krone
39 { 333, "", 2002, "Pts" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_ESP }, ///< spanish peseta
40 { 12, "", 2002, "", NBSP "mk", 1, STR_GAME_OPTIONS_CURRENCY_FIM }, ///< finnish markka
41 { 13, "", 2002, "FF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_FRF }, ///< french franc
42 { 681, "", 2002, "", "Dr.", 1, STR_GAME_OPTIONS_CURRENCY_GRD }, ///< greek drachma
43 { 378, "", CF_NOEURO, "", NBSP "Ft", 1, STR_GAME_OPTIONS_CURRENCY_HUF }, ///< hungarian forint
44 { 130, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_ISK }, ///< icelandic krona
45 { 3873, "", 2002, "", NBSP "L.", 1, STR_GAME_OPTIONS_CURRENCY_ITL }, ///< italian lira
46 { 4, "", 2002, "NLG" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_NLG }, ///< dutch gulden
47 { 12, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_NOK }, ///< norwegian krone
48 { 6, "", CF_NOEURO, "", NBSP "z\xC5\x82", 1, STR_GAME_OPTIONS_CURRENCY_PLN }, ///< polish zloty
49 { 5, "", CF_NOEURO, "", NBSP "Lei", 1, STR_GAME_OPTIONS_CURRENCY_RON }, ///< romanian leu
50 { 50, "", CF_NOEURO, "", NBSP "p", 1, STR_GAME_OPTIONS_CURRENCY_RUR }, ///< russian rouble
51 { 479, "", 2007, "", NBSP "SIT", 1, STR_GAME_OPTIONS_CURRENCY_SIT }, ///< slovenian tolar
52 { 13, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_SEK }, ///< swedish krona
53 { 3, "", CF_NOEURO, "", NBSP "TL", 1, STR_GAME_OPTIONS_CURRENCY_TRY }, ///< turkish lira
54 { 60, "", 2009, "", NBSP "Sk", 1, STR_GAME_OPTIONS_CURRENCY_SKK }, ///< slovak koruna
55 { 4, "", CF_NOEURO, "R$" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_BRL }, ///< brazil real
56 { 31, "", 2011, "", NBSP "EEK", 1, STR_GAME_OPTIONS_CURRENCY_EEK }, ///< estonian krooni
57 { 4, "", 2015, "", NBSP "Lt", 1, STR_GAME_OPTIONS_CURRENCY_LTL }, ///< lithuanian litas
58 { 1850, "", CF_NOEURO, "\xE2\x82\xA9", "", 0, STR_GAME_OPTIONS_CURRENCY_KRW }, ///< south korean won
59 { 13, "", CF_NOEURO, "R" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_ZAR }, ///< south african rand
60 { 1, "", CF_NOEURO, "", "", 2, STR_GAME_OPTIONS_CURRENCY_CUSTOM }, ///< custom currency (add further languages below)
61 { 3, "", CF_NOEURO, "", NBSP "GEL", 1, STR_GAME_OPTIONS_CURRENCY_GEL }, ///< Georgian Lari
62 { 4901, "", CF_NOEURO, "", NBSP "Rls", 1, STR_GAME_OPTIONS_CURRENCY_IRR }, ///< Iranian Rial
63 { 80, "", CF_NOEURO, "", NBSP "rub", 1, STR_GAME_OPTIONS_CURRENCY_RUB }, ///< New Russian Ruble
64 { 24, "", CF_NOEURO, "$", "", 0, STR_GAME_OPTIONS_CURRENCY_MXN }, ///< Mexican peso
65 { 40, "", CF_NOEURO, "NTD" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_NTD }, ///< new taiwan dollar
66 { 8, "", CF_NOEURO, "\xC2\xA5", "", 0, STR_GAME_OPTIONS_CURRENCY_CNY }, ///< chinese renminbi
67 { 10, "", CF_NOEURO, "HKD" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_HKD }, ///< hong kong dollar
68 { 90, "", CF_NOEURO, "\xE2\x82\xB9", "", 0, STR_GAME_OPTIONS_CURRENCY_INR }, ///< Indian Rupee
71 /** Array of currencies used by the system */
72 CurrencySpec _currency_specs[CURRENCY_END];
74 /**
75 * This array represent the position of OpenTTD's currencies,
76 * compared to TTDPatch's ones.
77 * When a grf sends currencies, they are based on the order defined by TTDPatch.
78 * So, we must reindex them to our own order.
80 const byte TTDPatch_To_OTTDIndex[] =
82 CURRENCY_GBP,
83 CURRENCY_USD,
84 CURRENCY_FRF,
85 CURRENCY_DEM,
86 CURRENCY_JPY,
87 CURRENCY_ESP,
88 CURRENCY_HUF,
89 CURRENCY_PLN,
90 CURRENCY_ATS,
91 CURRENCY_BEF,
92 CURRENCY_DKK,
93 CURRENCY_FIM,
94 CURRENCY_GRD,
95 CURRENCY_CHF,
96 CURRENCY_NLG,
97 CURRENCY_ITL,
98 CURRENCY_SEK,
99 CURRENCY_RUR,
100 CURRENCY_EUR,
104 * Will return the ottd's index correspondence to
105 * the ttdpatch's id. If the id is bigger than the array,
106 * it is a grf written for ottd, thus returning the same id.
107 * Only called from newgrf.cpp
108 * @param grfcurr_id currency id coming from newgrf
109 * @return the corrected index
111 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
113 return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
117 * get a mask of the allowed currencies depending on the year
118 * @return mask of currencies
120 uint64 GetMaskOfAllowedCurrencies()
122 uint64 mask = 0LL;
123 uint i;
125 for (i = 0; i < CURRENCY_END; i++) {
126 Year to_euro = _currency_specs[i].to_euro;
128 if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
129 if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
130 SetBit(mask, i);
132 SetBit(mask, CURRENCY_CUSTOM); // always allow custom currency
133 return mask;
137 * Verify if the currency chosen by the user is about to be converted to Euro
139 void CheckSwitchToEuro()
141 if (_currency_specs[_settings_game.locale.currency].to_euro != CF_NOEURO &&
142 _currency_specs[_settings_game.locale.currency].to_euro != CF_ISEURO &&
143 _cur_year >= _currency_specs[_settings_game.locale.currency].to_euro) {
144 _settings_game.locale.currency = 2; // this is the index of euro above.
145 AddNewsItem(STR_NEWS_EURO_INTRODUCTION, NT_ECONOMY, NF_NORMAL);
150 * Will fill _currency_specs array with
151 * default values from origin_currency_specs
152 * Called only from newgrf.cpp and settings.cpp.
153 * @param preserve_custom will not reset custom currency
155 void ResetCurrencies(bool preserve_custom)
157 for (uint i = 0; i < CURRENCY_END; i++) {
158 if (preserve_custom && i == CURRENCY_CUSTOM) continue;
159 _currency_specs[i] = origin_currency_specs[i];
164 * Build a list of currency names StringIDs to use in a dropdown list
165 * @return Pointer to a (static) array of StringIDs
167 StringID *BuildCurrencyDropdown()
169 /* Allow room for all currencies, plus a terminator entry */
170 static StringID names[CURRENCY_END + 1];
171 uint i;
173 /* Add each name */
174 for (i = 0; i < CURRENCY_END; i++) {
175 names[i] = _currency_specs[i].name;
177 /* Terminate the list */
178 names[i] = INVALID_STRING_ID;
180 return names;