Change: Let AI developers edit non-editable AI/Game Script Parameters (#8895)
[openttd-github.git] / src / currency.cpp
blob841f00d56aec1c6c0d2f2b7856dbf9a18b1f060a
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, u8"\u00a3", "", 0, STR_GAME_OPTIONS_CURRENCY_GBP }, ///< british pound
30 { 2, "", CF_NOEURO, "$", "", 0, STR_GAME_OPTIONS_CURRENCY_USD }, ///< american dollar
31 { 2, "", CF_ISEURO, u8"\u20ac", "", 0, STR_GAME_OPTIONS_CURRENCY_EUR }, ///< euro
32 { 220, "", CF_NOEURO, u8"\u00a5", "", 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 u8"K\u010d", 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 u8"z\u0142", 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, u8"\u20a9", "", 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, u8"\u00a5", "", 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, u8"\u20b9", "", 0, STR_GAME_OPTIONS_CURRENCY_INR }, ///< Indian Rupee
69 { 19, "", CF_NOEURO, "Rp", "", 0, STR_GAME_OPTIONS_CURRENCY_IDR }, ///< Indonesian Rupiah
70 { 5, "", CF_NOEURO, "RM", "", 0, STR_GAME_OPTIONS_CURRENCY_MYR }, ///< Malaysian Ringgit
73 /** Array of currencies used by the system */
74 CurrencySpec _currency_specs[CURRENCY_END];
76 /**
77 * This array represent the position of OpenTTD's currencies,
78 * compared to TTDPatch's ones.
79 * When a grf sends currencies, they are based on the order defined by TTDPatch.
80 * So, we must reindex them to our own order.
82 const byte TTDPatch_To_OTTDIndex[] =
84 CURRENCY_GBP,
85 CURRENCY_USD,
86 CURRENCY_FRF,
87 CURRENCY_DEM,
88 CURRENCY_JPY,
89 CURRENCY_ESP,
90 CURRENCY_HUF,
91 CURRENCY_PLN,
92 CURRENCY_ATS,
93 CURRENCY_BEF,
94 CURRENCY_DKK,
95 CURRENCY_FIM,
96 CURRENCY_GRD,
97 CURRENCY_CHF,
98 CURRENCY_NLG,
99 CURRENCY_ITL,
100 CURRENCY_SEK,
101 CURRENCY_RUR,
102 CURRENCY_EUR,
106 * Will return the ottd's index correspondence to
107 * the ttdpatch's id. If the id is bigger than the array,
108 * it is a grf written for ottd, thus returning the same id.
109 * Only called from newgrf.cpp
110 * @param grfcurr_id currency id coming from newgrf
111 * @return the corrected index
113 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
115 return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
119 * get a mask of the allowed currencies depending on the year
120 * @return mask of currencies
122 uint64 GetMaskOfAllowedCurrencies()
124 uint64 mask = 0LL;
125 uint i;
127 for (i = 0; i < CURRENCY_END; i++) {
128 Year to_euro = _currency_specs[i].to_euro;
130 if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
131 if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
132 SetBit(mask, i);
134 SetBit(mask, CURRENCY_CUSTOM); // always allow custom currency
135 return mask;
139 * Verify if the currency chosen by the user is about to be converted to Euro
141 void CheckSwitchToEuro()
143 if (_currency_specs[_settings_game.locale.currency].to_euro != CF_NOEURO &&
144 _currency_specs[_settings_game.locale.currency].to_euro != CF_ISEURO &&
145 _cur_year >= _currency_specs[_settings_game.locale.currency].to_euro) {
146 _settings_game.locale.currency = 2; // this is the index of euro above.
147 AddNewsItem(STR_NEWS_EURO_INTRODUCTION, NT_ECONOMY, NF_NORMAL);
152 * Will fill _currency_specs array with
153 * default values from origin_currency_specs
154 * Called only from newgrf.cpp and settings.cpp.
155 * @param preserve_custom will not reset custom currency
157 void ResetCurrencies(bool preserve_custom)
159 for (uint i = 0; i < CURRENCY_END; i++) {
160 if (preserve_custom && i == CURRENCY_CUSTOM) continue;
161 _currency_specs[i] = origin_currency_specs[i];
166 * Build a list of currency names StringIDs to use in a dropdown list
167 * @return Pointer to a (static) array of StringIDs
169 StringID *BuildCurrencyDropdown()
171 /* Allow room for all currencies, plus a terminator entry */
172 static StringID names[CURRENCY_END + 1];
173 uint i;
175 /* Add each name */
176 for (i = 0; i < CURRENCY_END; i++) {
177 names[i] = _currency_specs[i].name;
179 /* Terminate the list */
180 names[i] = INVALID_STRING_ID;
182 return names;