OpenTTD: src/currency.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

currency.cpp
Go to the documentation of this file.
1 /* $Id: currency.cpp 26989 2014-10-10 23:07:59Z planetmaker $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "stdafx.h"
13 #include "core/bitmath_func.hpp"
14
15 #include "currency.h"
16 #include "news_func.h"
17 #include "settings_type.h"
18 #include "date_func.h"
19 #include "string_type.h"
20
21 #include "table/strings.h"
22
23 #include "safeguards.h"
24
25 /* exchange rate prefix symbol_pos
26 * | separator | postfix |
27 * | | Euro year | | | name
28 * | | | | | | | */
30 static const CurrencySpec origin_currency_specs[CURRENCY_END] = {
31 { 1, "", CF_NOEURO, "\xC2\xA3", "", 0, STR_GAME_OPTIONS_CURRENCY_GBP },
32 { 2, "", CF_NOEURO, "$", "", 0, STR_GAME_OPTIONS_CURRENCY_USD },
33 { 2, "", CF_ISEURO, "\xE2\x82\xAC", "", 0, STR_GAME_OPTIONS_CURRENCY_EUR },
34 { 220, "", CF_NOEURO, "\xC2\xA5", "", 0, STR_GAME_OPTIONS_CURRENCY_JPY },
35 { 27, "", 2002, "", NBSP "S.", 1, STR_GAME_OPTIONS_CURRENCY_ATS },
36 { 81, "", 2002, "BEF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_BEF },
37 { 2, "", CF_NOEURO, "CHF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_CHF },
38 { 41, "", CF_NOEURO, "", NBSP "K\xC4\x8D", 1, STR_GAME_OPTIONS_CURRENCY_CZK },
39 { 4, "", 2002, "DM" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_DEM },
40 { 11, "", CF_NOEURO, "", NBSP "kr", 1, STR_GAME_OPTIONS_CURRENCY_DKK },
41 { 333, "", 2002, "Pts" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_ESP },
42 { 12, "", 2002, "", NBSP "mk", 1, STR_GAME_OPTIONS_CURRENCY_FIM },
43 { 13, "", 2002, "FF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_FRF },
44 { 681, "", 2002, "", "Dr.", 1, STR_GAME_OPTIONS_CURRENCY_GRD },
45 { 378, "", CF_NOEURO, "", NBSP "Ft", 1, STR_GAME_OPTIONS_CURRENCY_HUF },
46 { 130, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_ISK },
47 { 3873, "", 2002, "", NBSP "L.", 1, STR_GAME_OPTIONS_CURRENCY_ITL },
48 { 4, "", 2002, "NLG" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_NLG },
49 { 12, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_NOK },
50 { 6, "", CF_NOEURO, "", NBSP "z\xC5\x82", 1, STR_GAME_OPTIONS_CURRENCY_PLN },
51 { 5, "", CF_NOEURO, "", NBSP "Lei", 1, STR_GAME_OPTIONS_CURRENCY_RON },
52 { 50, "", CF_NOEURO, "", NBSP "p", 1, STR_GAME_OPTIONS_CURRENCY_RUR },
53 { 479, "", 2007, "", NBSP "SIT", 1, STR_GAME_OPTIONS_CURRENCY_SIT },
54 { 13, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_SEK },
55 { 3, "", CF_NOEURO, "", NBSP "TL", 1, STR_GAME_OPTIONS_CURRENCY_TRY },
56 { 60, "", 2009, "", NBSP "Sk", 1, STR_GAME_OPTIONS_CURRENCY_SKK },
57 { 4, "", CF_NOEURO, "R$" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_BRL },
58 { 31, "", 2011, "", NBSP "EEK", 1, STR_GAME_OPTIONS_CURRENCY_EEK },
59 { 4, "", 2015, "", NBSP "Lt", 1, STR_GAME_OPTIONS_CURRENCY_LTL },
60 { 1850, "", CF_NOEURO, "\xE2\x82\xA9", "", 0, STR_GAME_OPTIONS_CURRENCY_KRW },
61 { 13, "", CF_NOEURO, "R" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_ZAR },
62 { 1, "", CF_NOEURO, "", "", 2, STR_GAME_OPTIONS_CURRENCY_CUSTOM },
63 { 3, "", CF_NOEURO, "", NBSP "GEL", 1, STR_GAME_OPTIONS_CURRENCY_GEL },
64 { 4901, "", CF_NOEURO, "", NBSP "Rls", 1, STR_GAME_OPTIONS_CURRENCY_IRR },
65 };
66
68 CurrencySpec _currency_specs[CURRENCY_END];
69
76 const byte TTDPatch_To_OTTDIndex[] =
77 {
78 CURRENCY_GBP,
79 CURRENCY_USD,
80 CURRENCY_FRF,
81 CURRENCY_DEM,
82 CURRENCY_JPY,
83 CURRENCY_ESP,
84 CURRENCY_HUF,
85 CURRENCY_PLN,
86 CURRENCY_ATS,
87 CURRENCY_BEF,
88 CURRENCY_DKK,
89 CURRENCY_FIM,
90 CURRENCY_GRD,
91 CURRENCY_CHF,
92 CURRENCY_NLG,
93 CURRENCY_ITL,
94 CURRENCY_SEK,
95 CURRENCY_RUR,
96 CURRENCY_EUR,
97 };
98
107 byte GetNewgrfCurrencyIdConverted(byte grfcurr_id)
108 {
109 return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id];
110 }
111
116 uint64 GetMaskOfAllowedCurrencies()
117 {
118 uint64 mask = 0LL;
119 uint i;
120
121 for (i = 0; i < CURRENCY_END; i++) {
122 Year to_euro = _currency_specs[i].to_euro;
123
124 if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
125 if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
126 SetBit(mask, i);
127 }
128 SetBit(mask, CURRENCY_CUSTOM); // always allow custom currency
129 return mask;
130 }
131
135 void CheckSwitchToEuro()
136 {
137 if (_currency_specs[_settings_game.locale.currency].to_euro != CF_NOEURO &&
138 _currency_specs[_settings_game.locale.currency].to_euro != CF_ISEURO &&
139 _cur_year >= _currency_specs[_settings_game.locale.currency].to_euro) {
140 _settings_game.locale.currency = 2; // this is the index of euro above.
141 AddNewsItem(STR_NEWS_EURO_INTRODUCTION, NT_ECONOMY, NF_NORMAL);
142 }
143 }
144
151 void ResetCurrencies(bool preserve_custom)
152 {
153 for (uint i = 0; i < CURRENCY_END; i++) {
154 if (preserve_custom && i == CURRENCY_CUSTOM) continue;
155 _currency_specs[i] = origin_currency_specs[i];
156 }
157 }
158
163 StringID *BuildCurrencyDropdown()
164 {
165 /* Allow room for all currencies, plus a terminator entry */
166 static StringID names[CURRENCY_END + 1];
167 uint i;
168
169 /* Add each name */
170 for (i = 0; i < CURRENCY_END; i++) {
171 names[i] = _currency_specs[i].name;
172 }
173 /* Terminate the list */
174 names[i] = INVALID_STRING_ID;
175
176 return names;
177 }