OpenTTD
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, "\xC2\xA3", "", 0, STR_GAME_OPTIONS_CURRENCY_GBP },
30  { 2, "", CF_NOEURO, "$", "", 0, STR_GAME_OPTIONS_CURRENCY_USD },
31  { 2, "", CF_ISEURO, "\xE2\x82\xAC", "", 0, STR_GAME_OPTIONS_CURRENCY_EUR },
32  { 220, "", CF_NOEURO, "\xC2\xA5", "", 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 "K\xC4\x8D", 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 "z\xC5\x82", 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, "\xE2\x82\xA9", "", 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, "\xC2\xA5", "", 0, STR_GAME_OPTIONS_CURRENCY_CNY },
67  { 10, "", CF_NOEURO, "HKD" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_HKD },
68 };
69 
72 
79 const byte TTDPatch_To_OTTDIndex[] =
80 {
100 };
101 
110 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
111 {
112  return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
113 }
114 
120 {
121  uint64 mask = 0LL;
122  uint i;
123 
124  for (i = 0; i < CURRENCY_END; i++) {
125  Year to_euro = _currency_specs[i].to_euro;
126 
127  if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
128  if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
129  SetBit(mask, i);
130  }
131  SetBit(mask, CURRENCY_CUSTOM); // always allow custom currency
132  return mask;
133 }
134 
139 {
140  if (_currency_specs[_settings_game.locale.currency].to_euro != CF_NOEURO &&
141  _currency_specs[_settings_game.locale.currency].to_euro != CF_ISEURO &&
142  _cur_year >= _currency_specs[_settings_game.locale.currency].to_euro) {
143  _settings_game.locale.currency = 2; // this is the index of euro above.
144  AddNewsItem(STR_NEWS_EURO_INTRODUCTION, NT_ECONOMY, NF_NORMAL);
145  }
146 }
147 
154 void ResetCurrencies(bool preserve_custom)
155 {
156  for (uint i = 0; i < CURRENCY_END; i++) {
157  if (preserve_custom && i == CURRENCY_CUSTOM) continue;
158  _currency_specs[i] = origin_currency_specs[i];
159  }
160 }
161 
167 {
168  /* Allow room for all currencies, plus a terminator entry */
169  static StringID names[CURRENCY_END + 1];
170  uint i;
171 
172  /* Add each name */
173  for (i = 0; i < CURRENCY_END; i++) {
174  names[i] = _currency_specs[i].name;
175  }
176  /* Terminate the list */
177  names[i] = INVALID_STRING_ID;
178 
179  return names;
180 }
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:79
Russian Rouble.
Definition: currency.h:46
Finish Markka.
Definition: currency.h:36
Italian Lira.
Definition: currency.h:41
void ResetCurrencies(bool preserve_custom)
Will fill _currency_specs array with default values from origin_currency_specs Called only from newgr...
Definition: currency.cpp:154
Dutch Gulden.
Definition: currency.h:42
Custom currency.
Definition: currency.h:56
Functions related to dates.
CurrencySpec _currency_specs[CURRENCY_END]
Array of currencies used by the system.
Definition: currency.cpp:71
Functions to handle different currencies.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Euro.
Definition: currency.h:27
Functions related to bit mathematics.
int32 Year
Type for the year, note: 0 based, i.e. starts at the year 0.
Definition: date_type.h:18
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:24
Japanese Yen.
Definition: currency.h:28
US Dollar.
Definition: currency.h:26
Danish Krona.
Definition: currency.h:34
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
static const int CF_NOEURO
Currency never switches to the Euro (as far as known).
Definition: currency.h:16
Spanish Peseta.
Definition: currency.h:35
A number of safeguards to prevent using unsafe methods.
Swedish Krona.
Definition: currency.h:48
StringID * BuildCurrencyDropdown()
Build a list of currency names StringIDs to use in a dropdown list.
Definition: currency.cpp:166
Normal news item. (Newspaper with text only)
Definition: news_type.h:78
British Pound.
Definition: currency.h:25
static const CurrencySpec origin_currency_specs[CURRENCY_END]
The original currency specifications.
Definition: currency.cpp:28
const byte TTDPatch_To_OTTDIndex[]
This array represent the position of OpenTTD&#39;s currencies, compared to TTDPatch&#39;s ones...
Definition: currency.cpp:79
Belgian Franc.
Definition: currency.h:30
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
always the last item
Definition: currency.h:64
byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
Will return the ottd&#39;s index correspondence to the ttdpatch&#39;s id.
Definition: currency.cpp:110
Greek Drachma.
Definition: currency.h:38
French Franc.
Definition: currency.h:37
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Year to_euro
Year of switching to the Euro. May also be CF_NOEURO or CF_ISEURO.
Definition: currency.h:71
void CheckSwitchToEuro()
Verify if the currency chosen by the user is about to be converted to Euro.
Definition: currency.cpp:138
Austrian Schilling.
Definition: currency.h:29
Polish Zloty.
Definition: currency.h:44
LocaleSettings locale
settings related to used currency/unit system in the current game
Specification of a currency.
Definition: currency.h:68
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
uint64 GetMaskOfAllowedCurrencies()
get a mask of the allowed currencies depending on the year
Definition: currency.cpp:119
Types for strings.
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1=NR_NONE, uint32 ref1=UINT32_MAX, NewsReferenceType reftype2=NR_NONE, uint32 ref2=UINT32_MAX, void *free_data=nullptr)
Add a new newsitem to be shown.
Definition: news_gui.cpp:745
static const int CF_ISEURO
Currency is the Euro.
Definition: currency.h:17
Functions related to news.
Swiss Franc.
Definition: currency.h:31
#define NBSP
A non-breaking space.
Definition: string_type.h:18
Deutsche Mark.
Definition: currency.h:33
Hungarian Forint.
Definition: currency.h:39
Economic changes (recession, industry up/dowm)
Definition: news_type.h:28
byte currency
currency we currently use