OpenTTD Source: src/currency.cpp Source File

OpenTTD Source 15.0-beta1

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"
18#include "timer/timer_game_calendar.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
77std::array<CurrencySpec, CURRENCY_END> _currency_specs;
78
85 const uint8_t TTDPatch_To_OTTDIndex[] =
86{
87 CURRENCY_GBP,
88 CURRENCY_USD,
89 CURRENCY_FRF,
90 CURRENCY_DEM,
91 CURRENCY_JPY,
92 CURRENCY_ESP,
93 CURRENCY_HUF,
94 CURRENCY_PLN,
95 CURRENCY_ATS,
96 CURRENCY_BEF,
97 CURRENCY_DKK,
98 CURRENCY_FIM,
99 CURRENCY_GRD,
100 CURRENCY_CHF,
101 CURRENCY_NLG,
102 CURRENCY_ITL,
103 CURRENCY_SEK,
104 CURRENCY_RUR,
105 CURRENCY_EUR,
106};
107
116uint8_t GetNewgrfCurrencyIdConverted(uint8_t grfcurr_id)
117{
118 return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
119}
120
125uint64_t GetMaskOfAllowedCurrencies()
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{
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);
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;
164 _currency_specs[i] = origin_currency_specs[i];
165 }
166}
bitmath_func.hpp
Functions related to bit mathematics.
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
IntervalTimer
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
TimerGameCalendar::year
static Year year
Current year, starting at 0.
Definition timer_game_calendar.h:31
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:160
origin_currency_specs
static const std::array< CurrencySpec, CURRENCY_END > origin_currency_specs
The original currency specifications.
Definition currency.cpp:29
GetMaskOfAllowedCurrencies
uint64_t GetMaskOfAllowedCurrencies()
get a mask of the allowed currencies depending on the year
Definition currency.cpp:125
TTDPatch_To_OTTDIndex
const uint8_t TTDPatch_To_OTTDIndex[]
This array represent the position of OpenTTD's currencies, compared to TTDPatch's ones.
Definition currency.cpp:85
GetNewgrfCurrencyIdConverted
uint8_t GetNewgrfCurrencyIdConverted(uint8_t grfcurr_id)
Will return the ottd's index correspondence to the ttdpatch's id.
Definition currency.cpp:116
_currency_specs
std::array< CurrencySpec, CURRENCY_END > _currency_specs
Array of currencies used by the system.
Definition currency.cpp:77
_check_switch_to_euro
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.
currency.h
Functions to handle different currencies.
CF_ISEURO
static constexpr TimerGameCalendar::Year CF_ISEURO
Currency is the Euro.
Definition currency.h:18
CF_NOEURO
static constexpr TimerGameCalendar::Year CF_NOEURO
Currency never switches to the Euro (as far as known).
Definition currency.h:17
CURRENCY_GBP
@ CURRENCY_GBP
British Pound.
Definition currency.h:27
CURRENCY_ESP
@ CURRENCY_ESP
Spanish Peseta.
Definition currency.h:37
CURRENCY_FRF
@ CURRENCY_FRF
French Franc.
Definition currency.h:39
CURRENCY_SEK
@ CURRENCY_SEK
Swedish Krona.
Definition currency.h:50
CURRENCY_GRD
@ CURRENCY_GRD
Greek Drachma.
Definition currency.h:40
CURRENCY_JPY
@ CURRENCY_JPY
Japanese Yen.
Definition currency.h:30
CURRENCY_DKK
@ CURRENCY_DKK
Danish Krona.
Definition currency.h:36
CURRENCY_ATS
@ CURRENCY_ATS
Austrian Schilling.
Definition currency.h:31
CURRENCY_USD
@ CURRENCY_USD
US Dollar.
Definition currency.h:28
CURRENCY_END
@ CURRENCY_END
always the last item
Definition currency.h:71
CURRENCY_EUR
@ CURRENCY_EUR
Euro.
Definition currency.h:29
CURRENCY_FIM
@ CURRENCY_FIM
Finish Markka.
Definition currency.h:38
CURRENCY_DEM
@ CURRENCY_DEM
Deutsche Mark.
Definition currency.h:35
CURRENCY_BEF
@ CURRENCY_BEF
Belgian Franc.
Definition currency.h:32
CURRENCY_ITL
@ CURRENCY_ITL
Italian Lira.
Definition currency.h:43
CURRENCY_PLN
@ CURRENCY_PLN
Polish Zloty.
Definition currency.h:46
CURRENCY_RUR
@ CURRENCY_RUR
Russian Rouble.
Definition currency.h:48
CURRENCY_CHF
@ CURRENCY_CHF
Swiss Franc.
Definition currency.h:33
CURRENCY_CUSTOM
@ CURRENCY_CUSTOM
Custom currency.
Definition currency.h:58
CURRENCY_NLG
@ CURRENCY_NLG
Dutch Gulden.
Definition currency.h:44
CURRENCY_HUF
@ CURRENCY_HUF
Hungarian Forint.
Definition currency.h:41
news_func.h
Functions related to news.
AddNewsItem
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, std::unique_ptr< NewsAllocatedData > &&data=nullptr)
Add a new newsitem to be shown.
Definition news_gui.cpp:897
NT_ECONOMY
@ NT_ECONOMY
Economic changes (recession, industry up/dowm)
Definition news_type.h:31
NF_NORMAL
@ NF_NORMAL
Normal news item. (Newspaper with text only)
Definition news_type.h:81
safeguards.h
A number of safeguards to prevent using unsafe methods.
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:57
settings_type.h
Types related to global configuration settings.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:280
string_type.h
Types for strings.
NBSP
#define NBSP
A non-breaking space.
Definition string_type.h:16
GameSettings::locale
LocaleSettings locale
settings related to used currency/unit system in the current game
Definition settings_type.h:605
LocaleSettings::currency
uint8_t currency
currency we currently use
Definition settings_type.h:268
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:150
timer.h
Definition of Interval and OneShot timers.
timer_game_calendar.h
Definition of the game-calendar-timer.