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/>.
8 /** @file currency.cpp Support for different currencies. */
11 #include "core/bitmath_func.hpp"
14 #include "news_func.h"
15 #include "settings_type.h"
16 #include "string_type.h"
17 #include "timer/timer.h"
18 #include "timer/timer_game_calendar.h"
20 #include "table/strings.h"
22 #include "safeguards.h"
24 /* exchange rate prefix code
25 * | separator | postfix | symbol_pos
26 * | | Euro year | | | | name
28 /** The original currency specifications. */
29 static const std::array
<CurrencySpec
, CURRENCY_END
> origin_currency_specs
= {{
30 { 1, "", CF_NOEURO
, "\u00a3", "", "GBP", 0, STR_GAME_OPTIONS_CURRENCY_GBP
}, ///< british pound
31 { 2, "", CF_NOEURO
, "$", "", "USD", 0, STR_GAME_OPTIONS_CURRENCY_USD
}, ///< american dollar
32 { 2, "", CF_ISEURO
, "\u20ac", "", "EUR", 0, STR_GAME_OPTIONS_CURRENCY_EUR
}, ///< euro
33 { 220, "", CF_NOEURO
, "\u00a5", "", "JPY", 0, STR_GAME_OPTIONS_CURRENCY_JPY
}, ///< japanese yen
34 { 27, "", 2002, "", NBSP
"S.", "ATS", 1, STR_GAME_OPTIONS_CURRENCY_ATS
}, ///< austrian schilling
35 { 81, "", 2002, "BEF" NBSP
, "", "BEF", 0, STR_GAME_OPTIONS_CURRENCY_BEF
}, ///< belgian franc
36 { 2, "", CF_NOEURO
, "CHF" NBSP
, "", "CHF", 0, STR_GAME_OPTIONS_CURRENCY_CHF
}, ///< swiss franc
37 { 41, "", CF_NOEURO
, "", NBSP
"K\u010d", "CZK", 1, STR_GAME_OPTIONS_CURRENCY_CZK
}, ///< czech koruna
38 { 4, "", 2002, "DM" NBSP
, "", "DEM", 0, STR_GAME_OPTIONS_CURRENCY_DEM
}, ///< deutsche mark
39 { 11, "", CF_NOEURO
, "", NBSP
"kr", "DKK", 1, STR_GAME_OPTIONS_CURRENCY_DKK
}, ///< danish krone
40 { 333, "", 2002, "Pts" NBSP
, "", "ESP", 0, STR_GAME_OPTIONS_CURRENCY_ESP
}, ///< spanish peseta
41 { 12, "", 2002, "", NBSP
"mk", "FIM", 1, STR_GAME_OPTIONS_CURRENCY_FIM
}, ///< finnish markka
42 { 13, "", 2002, "FF" NBSP
, "", "FRF", 0, STR_GAME_OPTIONS_CURRENCY_FRF
}, ///< french franc
43 { 681, "", 2002, "", "Dr.", "GRD", 1, STR_GAME_OPTIONS_CURRENCY_GRD
}, ///< greek drachma
44 { 378, "", CF_NOEURO
, "", NBSP
"Ft", "HUF", 1, STR_GAME_OPTIONS_CURRENCY_HUF
}, ///< hungarian forint
45 { 130, "", CF_NOEURO
, "", NBSP
"Kr", "ISK", 1, STR_GAME_OPTIONS_CURRENCY_ISK
}, ///< icelandic krona
46 { 3873, "", 2002, "", NBSP
"L.", "ITL", 1, STR_GAME_OPTIONS_CURRENCY_ITL
}, ///< italian lira
47 { 4, "", 2002, "NLG" NBSP
, "", "NLG", 0, STR_GAME_OPTIONS_CURRENCY_NLG
}, ///< dutch gulden
48 { 12, "", CF_NOEURO
, "", NBSP
"Kr", "NOK", 1, STR_GAME_OPTIONS_CURRENCY_NOK
}, ///< norwegian krone
49 { 6, "", CF_NOEURO
, "", NBSP
"z\u0142", "PLN", 1, STR_GAME_OPTIONS_CURRENCY_PLN
}, ///< polish zloty
50 { 5, "", CF_NOEURO
, "", NBSP
"Lei", "RON", 1, STR_GAME_OPTIONS_CURRENCY_RON
}, ///< romanian leu
51 { 50, "", CF_NOEURO
, "", NBSP
"p", "RUR", 1, STR_GAME_OPTIONS_CURRENCY_RUR
}, ///< russian rouble
52 { 479, "", 2007, "", NBSP
"SIT", "SIT", 1, STR_GAME_OPTIONS_CURRENCY_SIT
}, ///< slovenian tolar
53 { 13, "", CF_NOEURO
, "", NBSP
"Kr", "SEK", 1, STR_GAME_OPTIONS_CURRENCY_SEK
}, ///< swedish krona
54 { 3, "", CF_NOEURO
, "", NBSP
"TL", "TRY", 1, STR_GAME_OPTIONS_CURRENCY_TRY
}, ///< turkish lira
55 { 60, "", 2009, "", NBSP
"Sk", "SKK", 1, STR_GAME_OPTIONS_CURRENCY_SKK
}, ///< slovak koruna
56 { 4, "", CF_NOEURO
, "R$" NBSP
, "", "BRL", 0, STR_GAME_OPTIONS_CURRENCY_BRL
}, ///< brazil real
57 { 31, "", 2011, "", NBSP
"EEK", "EEK", 1, STR_GAME_OPTIONS_CURRENCY_EEK
}, ///< estonian krooni
58 { 4, "", 2015, "", NBSP
"Lt", "LTL", 1, STR_GAME_OPTIONS_CURRENCY_LTL
}, ///< lithuanian litas
59 { 1850, "", CF_NOEURO
, "\u20a9", "", "KRW", 0, STR_GAME_OPTIONS_CURRENCY_KRW
}, ///< south korean won
60 { 13, "", CF_NOEURO
, "R" NBSP
, "", "ZAR", 0, STR_GAME_OPTIONS_CURRENCY_ZAR
}, ///< south african rand
61 { 1, "", CF_NOEURO
, "", "", "", 2, STR_GAME_OPTIONS_CURRENCY_CUSTOM
}, ///< custom currency (add further languages below)
62 { 3, "", CF_NOEURO
, "", NBSP
"GEL", "GEL", 1, STR_GAME_OPTIONS_CURRENCY_GEL
}, ///< Georgian Lari
63 { 4901, "", CF_NOEURO
, "", NBSP
"Rls", "IRR", 1, STR_GAME_OPTIONS_CURRENCY_IRR
}, ///< Iranian Rial
64 { 80, "", CF_NOEURO
, "", NBSP
"rub", "RUB", 1, STR_GAME_OPTIONS_CURRENCY_RUB
}, ///< New Russian Ruble
65 { 24, "", CF_NOEURO
, "$", "", "MXN", 0, STR_GAME_OPTIONS_CURRENCY_MXN
}, ///< Mexican peso
66 { 40, "", CF_NOEURO
, "NTD" NBSP
, "", "NTD", 0, STR_GAME_OPTIONS_CURRENCY_NTD
}, ///< new taiwan dollar
67 { 8, "", CF_NOEURO
, "\u00a5", "", "CNY", 0, STR_GAME_OPTIONS_CURRENCY_CNY
}, ///< chinese renminbi
68 { 10, "", CF_NOEURO
, "HKD" NBSP
, "", "HKD", 0, STR_GAME_OPTIONS_CURRENCY_HKD
}, ///< hong kong dollar
69 { 90, "", CF_NOEURO
, "\u20b9", "", "INR", 0, STR_GAME_OPTIONS_CURRENCY_INR
}, ///< Indian Rupee
70 { 19, "", CF_NOEURO
, "Rp", "", "IDR", 0, STR_GAME_OPTIONS_CURRENCY_IDR
}, ///< Indonesian Rupiah
71 { 5, "", CF_NOEURO
, "RM", "", "MYR", 0, STR_GAME_OPTIONS_CURRENCY_MYR
}, ///< Malaysian Ringgit
72 { 1, "", 2014, "", NBSP
"Ls", "LVL", 1, STR_GAME_OPTIONS_CURRENCY_LVL
}, ///< latvian lats
73 { 400, "", 2002, "", "$00", "PTE", 1, STR_GAME_OPTIONS_CURRENCY_PTE
}, ///< portuguese escudo
76 /** Array of currencies used by the system */
77 std::array
<CurrencySpec
, CURRENCY_END
> _currency_specs
;
80 * This array represent the position of OpenTTD's currencies,
81 * compared to TTDPatch's ones.
82 * When a grf sends currencies, they are based on the order defined by TTDPatch.
83 * So, we must reindex them to our own order.
85 const uint8_t TTDPatch_To_OTTDIndex
[] =
109 * Will return the ottd's index correspondence to
110 * the ttdpatch's id. If the id is bigger than the array,
111 * it is a grf written for ottd, thus returning the same id.
112 * Only called from newgrf.cpp
113 * @param grfcurr_id currency id coming from newgrf
114 * @return the corrected index
116 uint8_t GetNewgrfCurrencyIdConverted(uint8_t grfcurr_id
)
118 return (grfcurr_id
>= lengthof(TTDPatch_To_OTTDIndex
)) ? grfcurr_id
: TTDPatch_To_OTTDIndex
[grfcurr_id
];
122 * get a mask of the allowed currencies depending on the year
123 * @return mask of currencies
125 uint64_t GetMaskOfAllowedCurrencies()
130 for (i
= 0; i
< CURRENCY_END
; i
++) {
131 TimerGameCalendar::Year to_euro
= _currency_specs
[i
].to_euro
;
133 if (to_euro
!= CF_NOEURO
&& to_euro
!= CF_ISEURO
&& TimerGameCalendar::year
>= to_euro
) continue;
134 if (to_euro
== CF_ISEURO
&& TimerGameCalendar::year
< 2000) continue;
137 SetBit(mask
, CURRENCY_CUSTOM
); // always allow custom currency
142 * Verify if the currency chosen by the user is about to be converted to Euro
144 static IntervalTimer
<TimerGameCalendar
> _check_switch_to_euro({TimerGameCalendar::YEAR
, TimerGameCalendar::Priority::NONE
}, [](auto)
146 if (_currency_specs
[_settings_game
.locale
.currency
].to_euro
!= CF_NOEURO
&&
147 _currency_specs
[_settings_game
.locale
.currency
].to_euro
!= CF_ISEURO
&&
148 TimerGameCalendar::year
>= _currency_specs
[_settings_game
.locale
.currency
].to_euro
) {
149 _settings_game
.locale
.currency
= 2; // this is the index of euro above.
150 AddNewsItem(STR_NEWS_EURO_INTRODUCTION
, NT_ECONOMY
, NF_NORMAL
);
155 * Will fill _currency_specs array with
156 * default values from origin_currency_specs
157 * Called only from newgrf.cpp and settings.cpp.
158 * @param preserve_custom will not reset custom currency
160 void ResetCurrencies(bool preserve_custom
)
162 for (uint i
= 0; i
< CURRENCY_END
; i
++) {
163 if (preserve_custom
&& i
== CURRENCY_CUSTOM
) continue;
164 _currency_specs
[i
] = origin_currency_specs
[i
];