OpenTTD Source  20241120-master-g6d3adc6169
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 "string_type.h"
17 #include "timer/timer.h"
19 
20 #include "table/strings.h"
21 
22 #include "safeguards.h"
23 
24  /* exchange rate prefix code
25  * | separator | postfix | symbol_pos
26  * | | Euro year | | | | name
27  * | | | | | | | | */
29 static const std::array<CurrencySpec, CURRENCY_END> origin_currency_specs = {{
30  { 1, "", CF_NOEURO, "\u00a3", "", "GBP", 0, STR_GAME_OPTIONS_CURRENCY_GBP },
31  { 2, "", CF_NOEURO, "$", "", "USD", 0, STR_GAME_OPTIONS_CURRENCY_USD },
32  { 2, "", CF_ISEURO, "\u20ac", "", "EUR", 0, STR_GAME_OPTIONS_CURRENCY_EUR },
33  { 220, "", CF_NOEURO, "\u00a5", "", "JPY", 0, STR_GAME_OPTIONS_CURRENCY_JPY },
34  { 27, "", 2002, "", NBSP "S.", "ATS", 1, STR_GAME_OPTIONS_CURRENCY_ATS },
35  { 81, "", 2002, "BEF" NBSP, "", "BEF", 0, STR_GAME_OPTIONS_CURRENCY_BEF },
36  { 2, "", CF_NOEURO, "CHF" NBSP, "", "CHF", 0, STR_GAME_OPTIONS_CURRENCY_CHF },
37  { 41, "", CF_NOEURO, "", NBSP "K\u010d", "CZK", 1, STR_GAME_OPTIONS_CURRENCY_CZK },
38  { 4, "", 2002, "DM" NBSP, "", "DEM", 0, STR_GAME_OPTIONS_CURRENCY_DEM },
39  { 11, "", CF_NOEURO, "", NBSP "kr", "DKK", 1, STR_GAME_OPTIONS_CURRENCY_DKK },
40  { 333, "", 2002, "Pts" NBSP, "", "ESP", 0, STR_GAME_OPTIONS_CURRENCY_ESP },
41  { 12, "", 2002, "", NBSP "mk", "FIM", 1, STR_GAME_OPTIONS_CURRENCY_FIM },
42  { 13, "", 2002, "FF" NBSP, "", "FRF", 0, STR_GAME_OPTIONS_CURRENCY_FRF },
43  { 681, "", 2002, "", "Dr.", "GRD", 1, STR_GAME_OPTIONS_CURRENCY_GRD },
44  { 378, "", CF_NOEURO, "", NBSP "Ft", "HUF", 1, STR_GAME_OPTIONS_CURRENCY_HUF },
45  { 130, "", CF_NOEURO, "", NBSP "Kr", "ISK", 1, STR_GAME_OPTIONS_CURRENCY_ISK },
46  { 3873, "", 2002, "", NBSP "L.", "ITL", 1, STR_GAME_OPTIONS_CURRENCY_ITL },
47  { 4, "", 2002, "NLG" NBSP, "", "NLG", 0, STR_GAME_OPTIONS_CURRENCY_NLG },
48  { 12, "", CF_NOEURO, "", NBSP "Kr", "NOK", 1, STR_GAME_OPTIONS_CURRENCY_NOK },
49  { 6, "", CF_NOEURO, "", NBSP "z\u0142", "PLN", 1, STR_GAME_OPTIONS_CURRENCY_PLN },
50  { 5, "", CF_NOEURO, "", NBSP "Lei", "RON", 1, STR_GAME_OPTIONS_CURRENCY_RON },
51  { 50, "", CF_NOEURO, "", NBSP "p", "RUR", 1, STR_GAME_OPTIONS_CURRENCY_RUR },
52  { 479, "", 2007, "", NBSP "SIT", "SIT", 1, STR_GAME_OPTIONS_CURRENCY_SIT },
53  { 13, "", CF_NOEURO, "", NBSP "Kr", "SEK", 1, STR_GAME_OPTIONS_CURRENCY_SEK },
54  { 3, "", CF_NOEURO, "", NBSP "TL", "TRY", 1, STR_GAME_OPTIONS_CURRENCY_TRY },
55  { 60, "", 2009, "", NBSP "Sk", "SKK", 1, STR_GAME_OPTIONS_CURRENCY_SKK },
56  { 4, "", CF_NOEURO, "R$" NBSP, "", "BRL", 0, STR_GAME_OPTIONS_CURRENCY_BRL },
57  { 31, "", 2011, "", NBSP "EEK", "EEK", 1, STR_GAME_OPTIONS_CURRENCY_EEK },
58  { 4, "", 2015, "", NBSP "Lt", "LTL", 1, STR_GAME_OPTIONS_CURRENCY_LTL },
59  { 1850, "", CF_NOEURO, "\u20a9", "", "KRW", 0, STR_GAME_OPTIONS_CURRENCY_KRW },
60  { 13, "", CF_NOEURO, "R" NBSP, "", "ZAR", 0, STR_GAME_OPTIONS_CURRENCY_ZAR },
61  { 1, "", CF_NOEURO, "", "", "", 2, STR_GAME_OPTIONS_CURRENCY_CUSTOM },
62  { 3, "", CF_NOEURO, "", NBSP "GEL", "GEL", 1, STR_GAME_OPTIONS_CURRENCY_GEL },
63  { 4901, "", CF_NOEURO, "", NBSP "Rls", "IRR", 1, STR_GAME_OPTIONS_CURRENCY_IRR },
64  { 80, "", CF_NOEURO, "", NBSP "rub", "RUB", 1, STR_GAME_OPTIONS_CURRENCY_RUB },
65  { 24, "", CF_NOEURO, "$", "", "MXN", 0, STR_GAME_OPTIONS_CURRENCY_MXN },
66  { 40, "", CF_NOEURO, "NTD" NBSP, "", "NTD", 0, STR_GAME_OPTIONS_CURRENCY_NTD },
67  { 8, "", CF_NOEURO, "\u00a5", "", "CNY", 0, STR_GAME_OPTIONS_CURRENCY_CNY },
68  { 10, "", CF_NOEURO, "HKD" NBSP, "", "HKD", 0, STR_GAME_OPTIONS_CURRENCY_HKD },
69  { 90, "", CF_NOEURO, "\u20b9", "", "INR", 0, STR_GAME_OPTIONS_CURRENCY_INR },
70  { 19, "", CF_NOEURO, "Rp", "", "IDR", 0, STR_GAME_OPTIONS_CURRENCY_IDR },
71  { 5, "", CF_NOEURO, "RM", "", "MYR", 0, STR_GAME_OPTIONS_CURRENCY_MYR },
72  { 1, "", 2014, "", NBSP "Ls", "LVL", 1, STR_GAME_OPTIONS_CURRENCY_LVL },
73  { 400, "", 2002, "", "$00", "PTE", 1, STR_GAME_OPTIONS_CURRENCY_PTE },
74 }};
75 
77 std::array<CurrencySpec, CURRENCY_END> _currency_specs;
78 
85 const uint8_t TTDPatch_To_OTTDIndex[] =
86 {
100  CURRENCY_CHF,
101  CURRENCY_NLG,
102  CURRENCY_ITL,
103  CURRENCY_SEK,
104  CURRENCY_RUR,
105  CURRENCY_EUR,
106 };
107 
116 uint8_t GetNewgrfCurrencyIdConverted(uint8_t grfcurr_id)
117 {
118  return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
119 }
120 
126 {
127  uint64_t mask = 0LL;
128  uint i;
129 
130  for (i = 0; i < CURRENCY_END; i++) {
131  TimerGameCalendar::Year to_euro = _currency_specs[i].to_euro;
132 
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;
135  SetBit(mask, i);
136  }
137  SetBit(mask, CURRENCY_CUSTOM); // always allow custom currency
138  return mask;
139 }
140 
144 static IntervalTimer<TimerGameCalendar> _check_switch_to_euro({TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE}, [](auto)
145 {
149  _settings_game.locale.currency = 2; // this is the index of euro above.
150  AddNewsItem(STR_NEWS_EURO_INTRODUCTION, NT_ECONOMY, NF_NORMAL);
151  }
152 });
153 
160 void ResetCurrencies(bool preserve_custom)
161 {
162  for (uint i = 0; i < CURRENCY_END; i++) {
163  if (preserve_custom && i == CURRENCY_CUSTOM) continue;
165  }
166 }
Functions related to bit mathematics.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
static Year year
Current year, starting at 0.
void ResetCurrencies(bool preserve_custom)
Will fill _currency_specs array with default values from origin_currency_specs Called only from newgr...
Definition: currency.cpp:160
static const std::array< CurrencySpec, CURRENCY_END > origin_currency_specs
The original currency specifications.
Definition: currency.cpp:29
uint64_t GetMaskOfAllowedCurrencies()
get a mask of the allowed currencies depending on the year
Definition: currency.cpp:125
const uint8_t TTDPatch_To_OTTDIndex[]
This array represent the position of OpenTTD's currencies, compared to TTDPatch's ones.
Definition: currency.cpp:85
static IntervalTimer< TimerGameCalendar > _check_switch_to_euro({TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE}, [](auto) { if(_currency_specs[_settings_game.locale.currency].to_euro !=CF_NOEURO &&_currency_specs[_settings_game.locale.currency].to_euro !=CF_ISEURO &&TimerGameCalendar::year >=_currency_specs[_settings_game.locale.currency].to_euro) { _settings_game.locale.currency=2;AddNewsItem(STR_NEWS_EURO_INTRODUCTION, NT_ECONOMY, NF_NORMAL);} })
Verify if the currency chosen by the user is about to be converted to Euro.
uint8_t GetNewgrfCurrencyIdConverted(uint8_t grfcurr_id)
Will return the ottd's index correspondence to the ttdpatch's id.
Definition: currency.cpp:116
std::array< CurrencySpec, CURRENCY_END > _currency_specs
Array of currencies used by the system.
Definition: currency.cpp:77
Functions to handle different currencies.
static constexpr TimerGameCalendar::Year CF_ISEURO
Currency is the Euro.
Definition: currency.h:18
static constexpr TimerGameCalendar::Year CF_NOEURO
Currency never switches to the Euro (as far as known).
Definition: currency.h:17
@ CURRENCY_GBP
British Pound.
Definition: currency.h:27
@ CURRENCY_ESP
Spanish Peseta.
Definition: currency.h:37
@ CURRENCY_FRF
French Franc.
Definition: currency.h:39
@ CURRENCY_SEK
Swedish Krona.
Definition: currency.h:50
@ CURRENCY_GRD
Greek Drachma.
Definition: currency.h:40
@ CURRENCY_JPY
Japanese Yen.
Definition: currency.h:30
@ CURRENCY_DKK
Danish Krona.
Definition: currency.h:36
@ CURRENCY_ATS
Austrian Schilling.
Definition: currency.h:31
@ CURRENCY_USD
US Dollar.
Definition: currency.h:28
@ CURRENCY_END
always the last item
Definition: currency.h:71
@ CURRENCY_EUR
Euro.
Definition: currency.h:29
@ CURRENCY_FIM
Finish Markka.
Definition: currency.h:38
@ CURRENCY_DEM
Deutsche Mark.
Definition: currency.h:35
@ CURRENCY_BEF
Belgian Franc.
Definition: currency.h:32
@ CURRENCY_ITL
Italian Lira.
Definition: currency.h:43
@ CURRENCY_PLN
Polish Zloty.
Definition: currency.h:46
@ CURRENCY_RUR
Russian Rouble.
Definition: currency.h:48
@ CURRENCY_CHF
Swiss Franc.
Definition: currency.h:33
@ CURRENCY_CUSTOM
Custom currency.
Definition: currency.h:58
@ CURRENCY_NLG
Dutch Gulden.
Definition: currency.h:44
@ CURRENCY_HUF
Hungarian Forint.
Definition: currency.h:41
Functions related to news.
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1=NR_NONE, uint32_t ref1=UINT32_MAX, NewsReferenceType reftype2=NR_NONE, uint32_t ref2=UINT32_MAX, const NewsAllocatedData *data=nullptr)
Add a new newsitem to be shown.
Definition: news_gui.cpp:829
@ NT_ECONOMY
Economic changes (recession, industry up/dowm)
Definition: news_type.h:31
@ NF_NORMAL
Normal news item. (Newspaper with text only)
Definition: news_type.h:81
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition: stdafx.h:280
Types for strings.
#define NBSP
A non-breaking space.
Definition: string_type.h:16
LocaleSettings locale
settings related to used currency/unit system in the current game
uint8_t currency
currency we currently use
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition of Interval and OneShot timers.
Definition of the game-calendar-timer.