OpenTTD Source  12.0-beta2
currency.cpp
Go to the documentation of this file.
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  */
7 
10 #include "stdafx.h"
11 #include "core/bitmath_func.hpp"
12 
13 #include "currency.h"
14 #include "news_func.h"
15 #include "settings_type.h"
16 #include "date_func.h"
17 #include "string_type.h"
18 
19 #include "table/strings.h"
20 
21 #include "safeguards.h"
22 
23  /* exchange rate prefix symbol_pos
24  * | separator | postfix |
25  * | | Euro year | | | name
26  * | | | | | | | */
29  { 1, "", CF_NOEURO, u8"\u00a3", "", 0, STR_GAME_OPTIONS_CURRENCY_GBP },
30  { 2, "", CF_NOEURO, "$", "", 0, STR_GAME_OPTIONS_CURRENCY_USD },
31  { 2, "", CF_ISEURO, u8"\u20ac", "", 0, STR_GAME_OPTIONS_CURRENCY_EUR },
32  { 220, "", CF_NOEURO, u8"\u00a5", "", 0, STR_GAME_OPTIONS_CURRENCY_JPY },
33  { 27, "", 2002, "", NBSP "S.", 1, STR_GAME_OPTIONS_CURRENCY_ATS },
34  { 81, "", 2002, "BEF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_BEF },
35  { 2, "", CF_NOEURO, "CHF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_CHF },
36  { 41, "", CF_NOEURO, "", NBSP u8"K\u010d", 1, STR_GAME_OPTIONS_CURRENCY_CZK },
37  { 4, "", 2002, "DM" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_DEM },
38  { 11, "", CF_NOEURO, "", NBSP "kr", 1, STR_GAME_OPTIONS_CURRENCY_DKK },
39  { 333, "", 2002, "Pts" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_ESP },
40  { 12, "", 2002, "", NBSP "mk", 1, STR_GAME_OPTIONS_CURRENCY_FIM },
41  { 13, "", 2002, "FF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_FRF },
42  { 681, "", 2002, "", "Dr.", 1, STR_GAME_OPTIONS_CURRENCY_GRD },
43  { 378, "", CF_NOEURO, "", NBSP "Ft", 1, STR_GAME_OPTIONS_CURRENCY_HUF },
44  { 130, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_ISK },
45  { 3873, "", 2002, "", NBSP "L.", 1, STR_GAME_OPTIONS_CURRENCY_ITL },
46  { 4, "", 2002, "NLG" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_NLG },
47  { 12, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_NOK },
48  { 6, "", CF_NOEURO, "", NBSP u8"z\u0142", 1, STR_GAME_OPTIONS_CURRENCY_PLN },
49  { 5, "", CF_NOEURO, "", NBSP "Lei", 1, STR_GAME_OPTIONS_CURRENCY_RON },
50  { 50, "", CF_NOEURO, "", NBSP "p", 1, STR_GAME_OPTIONS_CURRENCY_RUR },
51  { 479, "", 2007, "", NBSP "SIT", 1, STR_GAME_OPTIONS_CURRENCY_SIT },
52  { 13, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_SEK },
53  { 3, "", CF_NOEURO, "", NBSP "TL", 1, STR_GAME_OPTIONS_CURRENCY_TRY },
54  { 60, "", 2009, "", NBSP "Sk", 1, STR_GAME_OPTIONS_CURRENCY_SKK },
55  { 4, "", CF_NOEURO, "R$" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_BRL },
56  { 31, "", 2011, "", NBSP "EEK", 1, STR_GAME_OPTIONS_CURRENCY_EEK },
57  { 4, "", 2015, "", NBSP "Lt", 1, STR_GAME_OPTIONS_CURRENCY_LTL },
58  { 1850, "", CF_NOEURO, u8"\u20a9", "", 0, STR_GAME_OPTIONS_CURRENCY_KRW },
59  { 13, "", CF_NOEURO, "R" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_ZAR },
60  { 1, "", CF_NOEURO, "", "", 2, STR_GAME_OPTIONS_CURRENCY_CUSTOM },
61  { 3, "", CF_NOEURO, "", NBSP "GEL", 1, STR_GAME_OPTIONS_CURRENCY_GEL },
62  { 4901, "", CF_NOEURO, "", NBSP "Rls", 1, STR_GAME_OPTIONS_CURRENCY_IRR },
63  { 80, "", CF_NOEURO, "", NBSP "rub", 1, STR_GAME_OPTIONS_CURRENCY_RUB },
64  { 24, "", CF_NOEURO, "$", "", 0, STR_GAME_OPTIONS_CURRENCY_MXN },
65  { 40, "", CF_NOEURO, "NTD" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_NTD },
66  { 8, "", CF_NOEURO, u8"\u00a5", "", 0, STR_GAME_OPTIONS_CURRENCY_CNY },
67  { 10, "", CF_NOEURO, "HKD" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_HKD },
68  { 90, "", CF_NOEURO, u8"\u20b9", "", 0, STR_GAME_OPTIONS_CURRENCY_INR },
69  { 19, "", CF_NOEURO, "Rp", "", 0, STR_GAME_OPTIONS_CURRENCY_IDR },
70  { 5, "", CF_NOEURO, "RM", "", 0, STR_GAME_OPTIONS_CURRENCY_MYR },
71 };
72 
75 
82 const byte TTDPatch_To_OTTDIndex[] =
83 {
100  CURRENCY_SEK,
101  CURRENCY_RUR,
102  CURRENCY_EUR,
103 };
104 
113 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
114 {
115  return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
116 }
117 
123 {
124  uint64 mask = 0LL;
125  uint i;
126 
127  for (i = 0; i < CURRENCY_END; i++) {
128  Year to_euro = _currency_specs[i].to_euro;
129 
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);
133  }
134  SetBit(mask, CURRENCY_CUSTOM); // always allow custom currency
135  return mask;
136 }
137 
142 {
146  _settings_game.locale.currency = 2; // this is the index of euro above.
147  AddNewsItem(STR_NEWS_EURO_INTRODUCTION, NT_ECONOMY, NF_NORMAL);
148  }
149 }
150 
157 void ResetCurrencies(bool preserve_custom)
158 {
159  for (uint i = 0; i < CURRENCY_END; i++) {
160  if (preserve_custom && i == CURRENCY_CUSTOM) continue;
162  }
163 }
164 
170 {
171  /* Allow room for all currencies, plus a terminator entry */
172  static StringID names[CURRENCY_END + 1];
173  uint i;
174 
175  /* Add each name */
176  for (i = 0; i < CURRENCY_END; i++) {
177  names[i] = _currency_specs[i].name;
178  }
179  /* Terminate the list */
180  names[i] = INVALID_STRING_ID;
181 
182  return names;
183 }
CF_NOEURO
static const int CF_NOEURO
Currency never switches to the Euro (as far as known).
Definition: currency.h:17
CURRENCY_DEM
@ CURRENCY_DEM
Deutsche Mark.
Definition: currency.h:34
CURRENCY_HUF
@ CURRENCY_HUF
Hungarian Forint.
Definition: currency.h:40
_cur_year
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:26
currency.h
CURRENCY_RUR
@ CURRENCY_RUR
Russian Rouble.
Definition: currency.h:47
AddNewsItem
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1=NR_NONE, uint32 ref1=UINT32_MAX, NewsReferenceType reftype2=NR_NONE, uint32 ref2=UINT32_MAX, const NewsAllocatedData *data=nullptr)
Add a new newsitem to be shown.
Definition: news_gui.cpp:807
CURRENCY_PLN
@ CURRENCY_PLN
Polish Zloty.
Definition: currency.h:45
origin_currency_specs
static const CurrencySpec origin_currency_specs[CURRENCY_END]
The original currency specifications.
Definition: currency.cpp:28
Year
int32 Year
Type for the year, note: 0 based, i.e. starts at the year 0.
Definition: date_type.h:18
CURRENCY_BEF
@ CURRENCY_BEF
Belgian Franc.
Definition: currency.h:31
NBSP
#define NBSP
A non-breaking space.
Definition: string_type.h:18
BuildCurrencyDropdown
StringID * BuildCurrencyDropdown()
Build a list of currency names StringIDs to use in a dropdown list.
Definition: currency.cpp:169
bitmath_func.hpp
CURRENCY_GBP
@ CURRENCY_GBP
British Pound.
Definition: currency.h:26
CF_ISEURO
static const int CF_ISEURO
Currency is the Euro.
Definition: currency.h:18
CURRENCY_CHF
@ CURRENCY_CHF
Swiss Franc.
Definition: currency.h:32
CURRENCY_USD
@ CURRENCY_USD
US Dollar.
Definition: currency.h:27
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:53
safeguards.h
CURRENCY_DKK
@ CURRENCY_DKK
Danish Krona.
Definition: currency.h:35
CheckSwitchToEuro
void CheckSwitchToEuro()
Verify if the currency chosen by the user is about to be converted to Euro.
Definition: currency.cpp:141
settings_type.h
CURRENCY_FIM
@ CURRENCY_FIM
Finish Markka.
Definition: currency.h:37
ResetCurrencies
void ResetCurrencies(bool preserve_custom)
Will fill _currency_specs array with default values from origin_currency_specs Called only from newgr...
Definition: currency.cpp:157
date_func.h
string_type.h
stdafx.h
CurrencySpec
Specification of a currency.
Definition: currency.h:72
NF_NORMAL
@ NF_NORMAL
Normal news item. (Newspaper with text only)
Definition: news_type.h:78
CURRENCY_ESP
@ CURRENCY_ESP
Spanish Peseta.
Definition: currency.h:36
CURRENCY_SEK
@ CURRENCY_SEK
Swedish Krona.
Definition: currency.h:49
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
CURRENCY_END
@ CURRENCY_END
always the last item
Definition: currency.h:68
CURRENCY_GRD
@ CURRENCY_GRD
Greek Drachma.
Definition: currency.h:39
LocaleSettings::currency
byte currency
currency we currently use
Definition: settings_type.h:228
CURRENCY_ITL
@ CURRENCY_ITL
Italian Lira.
Definition: currency.h:42
TTDPatch_To_OTTDIndex
const byte TTDPatch_To_OTTDIndex[]
This array represent the position of OpenTTD's currencies, compared to TTDPatch's ones.
Definition: currency.cpp:82
CURRENCY_NLG
@ CURRENCY_NLG
Dutch Gulden.
Definition: currency.h:43
CurrencySpec::to_euro
Year to_euro
Year of switching to the Euro. May also be CF_NOEURO or CF_ISEURO.
Definition: currency.h:75
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
CURRENCY_JPY
@ CURRENCY_JPY
Japanese Yen.
Definition: currency.h:29
NT_ECONOMY
@ NT_ECONOMY
Economic changes (recession, industry up/dowm)
Definition: news_type.h:28
GameSettings::locale
LocaleSettings locale
settings related to used currency/unit system in the current game
Definition: settings_type.h:588
GetNewgrfCurrencyIdConverted
byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
Will return the ottd's index correspondence to the ttdpatch's id.
Definition: currency.cpp:113
CURRENCY_EUR
@ CURRENCY_EUR
Euro.
Definition: currency.h:28
CURRENCY_FRF
@ CURRENCY_FRF
French Franc.
Definition: currency.h:38
GetMaskOfAllowedCurrencies
uint64 GetMaskOfAllowedCurrencies()
get a mask of the allowed currencies depending on the year
Definition: currency.cpp:122
_currency_specs
CurrencySpec _currency_specs[CURRENCY_END]
Array of currencies used by the system.
Definition: currency.cpp:74
CURRENCY_CUSTOM
@ CURRENCY_CUSTOM
Custom currency.
Definition: currency.h:57
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
CURRENCY_ATS
@ CURRENCY_ATS
Austrian Schilling.
Definition: currency.h:30
news_func.h